0.19: Why is there a ID in document object?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

0.19: Why is there a ID in document object?

Post by ickby »

I port some custom code from 0.18 to 0.19 rigth know, and detected a difference in the DocumentObject. There is a new ID field in Document Object. Why is that? Did we not have name as a unique identifier?

I'm also interested for what it is used, to see if I need to handle it somehow in my code.
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: 0.19: Why is there a ID in document object?

Post by wmayer »

See git commit ff1d1cd3414360e
* getID(), return object internal identifier. Each object is now
assigned an integer identifier that is unique within its containing
document.
* getObjectByID(), get object by its identifier
Why is that? Did we not have name as a unique identifier?
From the commit message it's not very clear why this has been added but I think it's to speed up the search for an object. Searching an object by string is much more time intensive than by an integer.
I'm also interested for what it is used, to see if I need to handle it somehow in my code.
It's mainly for internal use so you probably won't have to handle it in your code.
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: 0.19: Why is there a ID in document object?

Post by ickby »

thanks for the infos.
It's mainly for internal use so you probably won't have to handle it in your code.
I'm a bit worried, as this ID gets saved in the document file. Hence it seems to be important that the exact number stays the same over file reloads. Maybe it is used somewhere as reference to objects, maybe the links?

I'm asking as my code duplicates objects to exact and interchangeable copies. Right now in 0.19 that is not possible, the ID will be different. If this is a transient number only used for some searches that's fine, it can be different However, if someone stores the id as kind of reference to a certain object my code fails.

I will try to search the code a bit more.
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: 0.19: Why is there a ID in document object?

Post by wmayer »

Maybe it is used somewhere as reference to objects, maybe the links?
So far I have found two places where the id is used. It's in the tree view and for the links. For the former a transient behaviour might be OK but I guess for the latter it's not.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: 0.19: Why is there a ID in document object?

Post by vocx »

ickby wrote: Tue Apr 28, 2020 4:44 pm ...
I'm a bit worried, as this ID gets saved in the document file. Hence it seems to be important that the exact number stays the same over file reloads. Maybe it is used somewhere as reference to objects, maybe the links?
...
realthunder wrote: ping
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
realthunder
Veteran
Posts: 2190
Joined: Tue Jan 03, 2017 10:55 am

Re: 0.19: Why is there a ID in document object?

Post by realthunder »

ickby wrote: Tue Apr 28, 2020 2:28 pm I port some custom code from 0.18 to 0.19 rigth know, and detected a difference in the DocumentObject. There is a new ID field in Document Object. Why is that? Did we not have name as a unique identifier?

I'm also interested for what it is used, to see if I need to handle it somehow in my code.
The object ID has an important role in my TopoNaming algorithm. The ID is expected to be persistent and unique within the same document.
Try Assembly3 with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: 0.19: Why is there a ID in document object?

Post by ickby »

ok, thanks for the info. That means a object has two unique and persistent identifiers, Name and ID, one string and one int. For my code I need to make exact copies of objects, however, ID cannot be chosen the same way Name can, it is dependent on creation order. I see two solutions:

1. Make ID accessible to the user via the "addObject" function as optional parameter
2. Make the ID dependent on Name: ID == hash(Name)

Option 2 seems for me the most sensible. This would also allow to remove it from the file again and make it fully compatible to 0.18. From what I have seen now this does not affect the purpose of ID in any way.

@realthunder what do you think is the best option?
realthunder
Veteran
Posts: 2190
Joined: Tue Jan 03, 2017 10:55 am

Re: 0.19: Why is there a ID in document object?

Post by realthunder »

ickby wrote: Wed Apr 29, 2020 4:31 am ok, thanks for the info. That means a object has two unique and persistent identifiers, Name and ID, one string and one int. For my code I need to make exact copies of objects, however, ID cannot be chosen the same way Name can, it is dependent on creation order. I see two solutions:

1. Make ID accessible to the user via the "addObject" function as optional parameter
2. Make the ID dependent on Name: ID == hash(Name)
But there is no guarantee of uniqueness in hash. The same goes for option 1, because the ID may have been taken. Can you please explain first what's the intended use of the "exact copy". How are you going to deal with the duplication of name? Are you copying the object into a new document?
Try Assembly3 with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: 0.19: Why is there a ID in document object?

Post by ickby »

I'm aware that neither of those methods is perfect. However, this is a significant change to 0.18 behavior and we need to deal with it somehow.

realthunder wrote: Wed Apr 29, 2020 4:48 am But there is no guarantee of uniqueness in hash.
probability of same hash for different names is "1.0/std::numeric_limits<size_t>::max()" with std::hash, so pretty close to a guarantee.

realthunder wrote: Wed Apr 29, 2020 4:48 am The same goes for option 1, because the ID may have been taken.".
But than addObject can easily fail with an error.

realthunder wrote: Wed Apr 29, 2020 4:48 am How are you going to deal with the duplication of name? Are you copying the object into a new document?
Yes, i rebuild part of a document in a new one. For that all links and references need to still work. As I not entirely sure how ID is to be used in either your code or any python code out there I need to duplicate it.
realthunder
Veteran
Posts: 2190
Joined: Tue Jan 03, 2017 10:55 am

Re: 0.19: Why is there a ID in document object?

Post by realthunder »

ickby wrote: Wed Apr 29, 2020 4:31 am 2. Make the ID dependent on Name: ID == hash(Name)
Forgot to mention, there is another requirement of ID, that it cannot be reused (within the same document), unlike Name. Because the ID will be used for tracing back model history. If a historical object is deleted, the tracing can continue to the previous step. If the ID can be reused, then the trace may be trapped to the wrong object.

ickby wrote: Wed Apr 29, 2020 5:04 am Yes, i rebuild part of a document in a new one. For that all links and references need to still work. As I not entirely sure how ID is to be used in either your code or any python code out there I need to duplicate it.
I don't think ID will cause any problem for copying. BTW, maybe you can try the Document.copyObject() API. I have made many changes to make sure the copied object continues to work with all the dependencies, in various use case, like copy objects from multiple documents, copy into the same document, etc. Also with App::Link, it is possible to copy to another document without dependency. It takes quite some effort to make sure everything works, such as expression, spreadsheet, and the need to recompute topo names, etc. There is also the moveObject() API in case you want to move.
Try Assembly3 with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
Post Reply