> 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

Reply via email to