But this variant of ged_tops () seems to work (except for moss.g, where it also
shows “sph1.s” in addition to all.g:
/* Get list of top level objects */
int obj_cnt = 0;
struct directory *dp;
db_update_nref(dbip, &rt_uniresource);
if (db_version(dbip) < 5) {
for (i = 0; i < RT_DBNHASH; i++)
for (dp = dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
if (dp->d_nref == 0 && !(dp->d_flags & RT_DIR_HIDDEN))
printf ("Found the TLO %s\n", dp->d_namep);
obj_cnt++;
}
}
else {
for (i = 0; i < RT_DBNHASH; i++)
for (dp = dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
if (dp->d_nref == 0 && !(dp->d_flags & RT_DIR_HIDDEN)) {
obj_cnt++;
printf ("Found the TLO in else %s\n", dp->d_namep);
}
else {
continue;
}
}
}
printf ("TOP LEVEL OBJECTS %d\n", obj_cnt);
From: Christopher Sean Morrison [mailto:[email protected]]
Sent: 03 August 2016 04:57
To: BRL-CAD Developer Mailing List <[email protected]>
Subject: Re: [brlcad-devel] Finding top level objects
On Aug 2, 2016, at 4:04 PM, (Asad) Syed, Asadullah Hussain
<[email protected]<mailto:[email protected]>> wrote:
Hi, I want to programmatically extract top level objects for a g file (the ones
you get when “tops” command is issued in mged). According to the definition top
level objects are the ones which aren’t referenced by any other combination.
Looking at the ged_tops function I deduced that the only check is to see if
“dp->d_nref == 0”, where dp is a “struct directory” pointer.
But the problem that I have is that all objects seem to have dp->d_nref==0 and
even when I use db_ls using “DB_LS_TOPS” flag still I get ALL objects. Am I
missing something?
Yes, as that’s a potentially unnecessary and sometimes expensive calculation,
so it doesn’t get computed automatically. You have to call db_update_nref().
See libged/tops.c:84.
That said, probably a better way than calculating tops yourself is to call
db_ls(). Here’s a functioning example*:
int main(void) {
struct db_i *dbip = db_open("test.g", DB_OPEN_READONLY);
struct directory **tops;
db_dirbuild(dbip);
int count = db_ls(dbip, DB_LS_TOPS, NULL, &tops);
bu_log("found %d top level objects\n", count);
while (count > 0) {
bu_log("top path is %s\n", tops[count-1]->d_namep);
count--;
}
if (tops)
bu_free(tops, "free tops");
return 0;
}
Cheers!
Sean
* note this is a quick example that assumes c99
------------------------------------------------------------------------------
_______________________________________________
BRL-CAD Developer mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-devel