Ah, oops! You still need to call db_update_nref(dbip, &rt_uniresource) after db_dirbuild(), same as in the tops.c code.
What's really curious is that it (perhaps coincidentally) worked on some models without updating the reference counts! Cheers! Sean > On Aug 3, 2016, at 6:28 PM, (Asad) Syed, Asadullah Hussain > <asadullah.huss...@tum.de> wrote: > > Hi, I’ve already tried db_ls () but it doesn’t work i.e., when I run tops > command on: > > moss.g: I get all.g > ktank.g: I get tank, etc. > > But when I execute your given example with moss.g it just outputs all > objects. Maybe I’m doing something wrong, I’ve uploaded the example here > (https://github.com/asadpiz/brlcad-viewer/blob/master/test.c) > > Compilation (make sure moss.g is in the same directory) > > gcc -o test test.c -L/usr/brlcad/rel-7.26.1/lib -lrt -lbu > -I/usr/brlcad/rel-7.26.1/include -I/usr/brlcad/rel-7.26.1/include/brlcad > > # ./test > found 14 top level objects > top path is platform.s > top path is platform.r > top path is ellipse.s > top path is ellipse.r > top path is light.r > top path is cone.s > top path is cone.r > top path is sph1.s > top path is box.s > top path is box.r > top path is tor.r > top path is all.g > top path is LIGHT > top path is tor > > > From: Christopher Sean Morrison [mailto:brl...@mac.com] > Sent: 03 August 2016 04:57 > To: BRL-CAD Developer Mailing List <brlcad-devel@lists.sourceforge.net> > Subject: Re: [brlcad-devel] Finding top level objects > > > On Aug 2, 2016, at 4:04 PM, (Asad) Syed, Asadullah Hussain > <asadullah.huss...@tum.de> 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 > brlcad-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/brlcad-devel ------------------------------------------------------------------------------ _______________________________________________ BRL-CAD Developer mailing list brlcad-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/brlcad-devel