Finding objects based on GUID

A forum dedicated to the Draft, Arch and BIM workbenches development.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
lambda
Posts: 91
Joined: Sat Feb 25, 2017 3:10 pm
Contact:

Finding objects based on GUID

Post by lambda »

Is there any function similar to Document::getObjectsByLabel() but working with GUIDs?

Or even more general: Are the any functions yet, which do anything useful with GUIDs?
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Finding objects based on GUID

Post by openBrain »

Just use '{document}.{GUID}' to get a pointer. No need to call a function.
lambda
Posts: 91
Joined: Sat Feb 25, 2017 3:10 pm
Contact:

Re: Finding objects based on GUID

Post by lambda »

openBrain wrote: Wed Jul 01, 2020 12:41 pm Just use '{document}.{GUID}' to get a pointer. No need to call a function.
I don't know how to translate this into valid syntax.

TIA, Harald
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Finding objects based on GUID

Post by openBrain »

Eg. object with name 'Sketch' in active document is accessed with :

Code: Select all

App.ActiveDocument.Sketch
lambda
Posts: 91
Joined: Sat Feb 25, 2017 3:10 pm
Contact:

Re: Finding objects based on GUID

Post by lambda »

Yes, I know how to do this with objects. ;-)

But a GlobalId is a property of an object, not an object itself.

I have

Code: Select all

>>> FreeCAD.ActiveDocument.Wall.GlobalId
'2Uv8iBq25ChAfktTCU0To2'
So suppose I know '2Uv8iBq25ChAfktTCU0To2', but not to which object it belongs.
Is there any way to arrive at 'Wall' or a reference to FreeCAD.ActiveDocument.Wall without iterating over FreeCAD.ActiveDocument.Objects?
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Finding objects based on GUID

Post by vocx »

lambda wrote: Wed Jul 01, 2020 8:34 pm ...
But a GlobalId is a property of an object, not an object itself.
...
Is there any way to arrive at 'Wall' or a reference to FreeCAD.ActiveDocument.Wall without iterating over FreeCAD.ActiveDocument.Objects?
I don't know if there are functions that handle the GUID, but the important question is what do you want to do with it?

The "Name" of an object is a global unique identifier; instead of using a complex string like "2Uv8iBq25ChAfktTCU0To2", you can just use "Wall". This name is unique in the document, so you can always get this object with this simple string "Wall".

Object name
Last edited by vocx on Fri Jul 03, 2020 2:31 pm, edited 1 time in total.
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.
lambda
Posts: 91
Joined: Sat Feb 25, 2017 3:10 pm
Contact:

Re: Finding objects based on GUID

Post by lambda »

What I want to do right now is process data from other BIM applications, that don't know about the object names in FreeCAD document, but know the GUID, because they are working on the same IFC file.

But I think pretty much everybody using GlobalIds will want to find objects based on them. Even if it is just updating objects from a newer version of your IFC file, you will want to find them efficiently.
User avatar
yorik
Founder
Posts: 13659
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Finding objects based on GUID

Post by yorik »

All Arch/BIM objects have a GlobalID property which is an IFC global ID. What @vocx says is also true, the object name is unique in FreeCAD and serves as an ID.

You indeed need to iterate over objects to get one with the ID you want, but you could do that with an easy function like:

Code: Select all

def by_guid(guid):
    for obj in FreeCAD.ActiveDocument.Objects:
        if hasattr(obj,"GlobalID") and (obj.GlobalID == guid):
            return obj
    return None
lambda
Posts: 91
Joined: Sat Feb 25, 2017 3:10 pm
Contact:

Re: Finding objects based on GUID

Post by lambda »

Sure, I can do that, but it will be very slow on big files.

However I guess this answers my question: There is no such function yet. :-)
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Finding objects based on GUID

Post by bernd »

lambda wrote: Tue Jul 07, 2020 2:16 pm Sure, I can do that, but it will be very slow on big files.
I do not think so. Once the file is loaded such python method will be blind fast, even with 10000 objects. Slow (really slow) will be any interaction with the mouse and the tree view. For example if you would like to select 1000 of the 10000 objects in the tree view with the mouse (possible with shift and start and end object).
Post Reply