unique object id

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
User avatar
bernd
Posts: 10976
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

unique object id

Postby bernd » Fri Aug 28, 2020 5:30 pm

Is there some unique id like the name of the object. For ifc export we need a id similar to a git commit id. This has to be unique and a object should not change this id during live time.

At the moment this is generated on ifc export and saved in a dict. Works well but if the object is copied in FC the id is copird too and bang the next exported ifc has two identical ids.

We could check on duplicate ids on export of ifc but if we find duplicate ids we do not know which is the original one and which is the copy.Further information can be found here: https://forum.freecadweb.org/viewtopic. ... 13#p426883

the id has 22 diggits, an example:
0kEY3dMYL4nguYLQ7LgyaC
aapo
Posts: 213
Joined: Mon Oct 29, 2018 6:41 pm

Re: unique object id

Postby aapo » Fri Aug 28, 2020 6:52 pm

bernd wrote:
Fri Aug 28, 2020 5:30 pm
Is there some unique id like the name of the object. For ifc export we need a id similar to a git commit id. This has to be unique and a object should not change this id during live time.
I remember a related discussion earlier, and if I recall correctly, there is no such ID. I think the problem was that it is perfectly legal for the user to copy a .FCStd file at the filesystem level (e.g. "cp my_part.FCStd my_better_part.FCStd") and include them both in the same document later. The filesystem copy obviously duplicates any "unique" identifier anyway, so that any such identifier would need to be re-created to a new value when opening a file, just to be sure it really is unique after loading a new file. Irritating problem! :D

At the moment this is generated on ifc export and saved in a dict. Works well but if the object is copied in FC the id is copird too and bang the next exported ifc has two identical ids.

We could check on duplicate ids on export of ifc but if we find duplicate ids we do not know which is the original one and which is the copy.Further information can be found here: https://forum.freecadweb.org/viewtopic. ... 13#p426883

the id has 22 diggits, an example:
0kEY3dMYL4nguYLQ7LgyaC
I might be wrong, but I think your best bet is to do exactly as you propose, and if there would be a conflict, just assign a new id to one of them. Maybe make a new id for the one you detect later, and hope that logic is sound enough? :D
User avatar
vanuan
Posts: 465
Joined: Wed Oct 24, 2018 9:49 pm

Re: unique object id

Postby vanuan » Fri Aug 28, 2020 8:32 pm

What's the issue with using "Name"?
aapo
Posts: 213
Joined: Mon Oct 29, 2018 6:41 pm

Re: unique object id

Postby aapo » Fri Aug 28, 2020 9:17 pm

vanuan wrote:
Fri Aug 28, 2020 8:32 pm
What's the issue with using "Name"?
I would guess the fact that "Name" is only unique within a single document might cause problems. I mean if someone App->Link:s some containers from other files, the objects within the linked container (a file) may well have duplicate object names (in respect to the original document). I think that the "Document name" + (object) "Name" combo would always be unique, though.
vocx
Posts: 5206
Joined: Thu Oct 18, 2018 9:18 pm

Re: unique object id

Postby vocx » Fri Aug 28, 2020 9:43 pm

aapo wrote:
Fri Aug 28, 2020 9:17 pm
...I think that the "Document name" + (object) "Name" combo would always be unique, though.

Code: Select all

>>> App.ActiveDocument.Rectangle.FullName
'Unnamed#Rectangle'
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.
ezzieyguywuf
Posts: 656
Joined: Tue May 19, 2015 1:11 am

Re: unique object id

Postby ezzieyguywuf » Fri Aug 28, 2020 10:25 pm

aapo wrote:
Fri Aug 28, 2020 9:17 pm
. I think that the "Document name" + (object) "Name" combo would always be unique, though.
came here to say this.
User avatar
vanuan
Posts: 465
Joined: Wed Oct 24, 2018 9:49 pm

Re: unique object id

Postby vanuan » Fri Aug 28, 2020 11:03 pm

aapo wrote:
Fri Aug 28, 2020 9:17 pm
I mean if someone App->Link:s some containers from other files, the objects within the linked container (a file) may well have duplicate object names (in respect to the original document)
Wouldn't those linked object names be scoped by the App->Link object?
carlopav
Posts: 1570
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: unique object id

Postby carlopav » Sat Aug 29, 2020 6:59 am

I remember an earlier discussion with realthunder with the point of having an unique object id some months ago.
realthunder wrote:
Thu Aug 27, 2020 1:44 am
follow my experiments on BIM modelling for architecture design
wmayer
Site Admin
Posts: 16649
Joined: Thu Feb 19, 2009 10:32 am

Re: unique object id

Postby wmayer » Sat Aug 29, 2020 7:06 am

You can use the ID attribute of an object:

Code: Select all

doc=App.newDocument()
cyl1=doc.addObject("Part::Cylinder")
cyl2=doc.copyObject(cyl1)
cyl1.ID
cyl2.ID
The ID property is a normal integer.

If you save this to a project file then the IDs are saved too. If you copy the file then after loading both files the IDs are not unique application-wide any more, they are only unique document-wide.

To generate a really application-wide unique hash you can combine it with a uuid:

Code: Select all

import uuid

uid = uuid.UUID(int=id(App.ActiveDocument))
obj_id = str(uid) + "-{}".format(cyl1.ID)
The resulting string looks then like this:
'00000000-0000-0000-0000-7f46ec00f8f8-1824'
In my case the first four groups consist of only "0". So, maybe if they are annoying you can remove the leading 25 characters.

Code: Select all

obj_id = obj_id[24:]
NOTE: This approach is based on Python's id() value of a document. This means when closing and reloading the project you get a completely different set of uuids.
paullee
Posts: 2770
Joined: Wed May 04, 2016 3:58 pm

Re: unique object id

Postby paullee » Sat Aug 29, 2020 8:19 am

FWIF, there was a discussion on sketch geometry level unique id sketch.Geometry[index].Tag == Unique identification survive changes?


BTW, wondering if an UUID can be generated for any new file created and put in Meta ? Make sense at all ?