Finding objects based on GUID

A forum dedicated to the Draft, Arch and BIM workbenches development.
lambda
Posts: 60
Joined: Sat Feb 25, 2017 3:10 pm
Contact:

Finding objects based on GUID

Postby lambda » Wed Jul 01, 2020 9:09 am

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
Posts: 4213
Joined: Fri Nov 09, 2018 5:38 pm

Re: Finding objects based on GUID

Postby openBrain » Wed Jul 01, 2020 12:41 pm

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

Re: Finding objects based on GUID

Postby lambda » Wed Jul 01, 2020 7:52 pm

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
Posts: 4213
Joined: Fri Nov 09, 2018 5:38 pm

Re: Finding objects based on GUID

Postby openBrain » Wed Jul 01, 2020 8:07 pm

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

Code: Select all

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

Re: Finding objects based on GUID

Postby lambda » Wed Jul 01, 2020 8:34 pm

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
Posts: 4333
Joined: Thu Oct 18, 2018 9:18 pm

Re: Finding objects based on GUID

Postby vocx » Wed Jul 01, 2020 9:26 pm

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.
To support the documentation effort, and code development, your donation is appreciated: paypal.
lambda
Posts: 60
Joined: Sat Feb 25, 2017 3:10 pm
Contact:

Re: Finding objects based on GUID

Postby lambda » Thu Jul 02, 2020 4:26 pm

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
Site Admin
Posts: 11945
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: Finding objects based on GUID

Postby yorik » Tue Jul 07, 2020 9:35 am

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: 60
Joined: Sat Feb 25, 2017 3:10 pm
Contact:

Re: Finding objects based on GUID

Postby lambda » Tue Jul 07, 2020 2:16 pm

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
Posts: 10184
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Finding objects based on GUID

Postby bernd » Tue Jul 07, 2020 2:37 pm

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).