Looking for 1st geometric parent of an object

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
Roy_043
Veteran
Posts: 8548
Joined: Thu Dec 27, 2018 12:28 pm

Looking for 1st geometric parent of an object

Post by Roy_043 »

What is the 'standard' way to look for the 1st geometric parent of an object. By geometric parent I mean a parent with a coordinate system:

Code: Select all

Part
    Group
    Group001
        Part001
            Wire
            Part002
                Group002
                    Wire001
For Wire001 it is Part002. For Wire it is Part001.

I can come up with the function below.
But I do not know how reliable this is. When would the Parents method return a list with more than one item?
The solution is also a bit convoluted, the return value of the Parents method is not ideal for this purpose.

Code: Select all

def object_get_parent (obj):
    parents = obj.Parents
    if parents:
        lst = [parents[0][0].Name] + parents[0][1].split(".")
        if len(lst) > 2:
            return obj.Document.getObject(lst[len(lst)-3])
        return None
    return None
User avatar
Pauvres_honteux
Posts: 728
Joined: Sun Feb 16, 2014 12:05 am
Location: Far side of the moon

Re: Looking for 1st geometric parent of an object

Post by Pauvres_honteux »

User avatar
Roy_043
Veteran
Posts: 8548
Joined: Thu Dec 27, 2018 12:28 pm

Re: Looking for 1st geometric parent of an object

Post by Roy_043 »

No, your FR is unrelated. But I suppose you already knew this.
User avatar
onekk
Veteran
Posts: 6205
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Looking for 1st geometric parent of an object

Post by onekk »

It is not possible to inspect the tree?

As usual the problem could be the ".Name" versus ".Label" difference present in FreeCAD.

But once obtained the Object (TreeView show the Label property if I don't go wrong) the Name could be obtained rather easily.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8548
Joined: Thu Dec 27, 2018 12:28 pm

Re: Looking for 1st geometric parent of an object

Post by Roy_043 »

onekk wrote: Fri Jun 25, 2021 1:41 pm It is not possible to inspect the tree?
The Parents property is giving the required result, it is just that the implementation is rather awkward.
User avatar
onekk
Veteran
Posts: 6205
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Looking for 1st geometric parent of an object

Post by onekk »

Sorry I could not find a way to use your code.

Could you kindly make a simple working example?

You have made me very curious about the problem, and surely it will be a learning experience.

Only if this could not cause you too much trouble, obviously.


TIA and Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8548
Joined: Thu Dec 27, 2018 12:28 pm

Re: Looking for 1st geometric parent of an object

Post by Roy_043 »

Code: Select all

>>> wr0 = App.ActiveDocument.Wire
>>> wr1 = App.ActiveDocument.Wire001
>>> object_get_parent(wr0).Label
'Part001'
>>> object_get_parent(wr1).Label
'Part002'
IMO the Parents property has a strange format:

Code: Select all

>>> wr1.Parents
[(<Part object>, 'Part001.Part002.Wire001.')]
Why is there a tuple inside a list? Why return the top-parent as an object, and the rest as names in a string that needs to be split to be useful. And why add the object itself to that string? And why does the string end in "."? Unless I am missing something, and that may very well be the case, this seems very inconvenient.
Attachments
geom-parent-test.FCStd
(14.44 KiB) Downloaded 33 times
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Looking for 1st geometric parent of an object

Post by carlopav »

Could maybe be because an object can appear 2 Times in 2 different positions since link implementation?
Ex:

Code: Select all

Part001-Part002-Wire001
LinkToPart001-Part002-Wire001
follow my experiments on BIM modelling for architecture design
User avatar
Roy_043
Veteran
Posts: 8548
Joined: Thu Dec 27, 2018 12:28 pm

Re: Looking for 1st geometric parent of an object

Post by Roy_043 »

Ah yes, you are right.

Code: Select all

>>> wr1 = App.ActiveDocument.Wire001
>>> wr1.Parents
[(<App::Link object>, 'Part002.Wire001.'), (<Part object>, 'Part001.Part002.Wire001.')]
Note that the getGlobalPlacement() method does not take this possibility (Wire001 has two geometric parents, and appears in two places in the GCS) into account.
User avatar
onekk
Veteran
Posts: 6205
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Looking for 1st geometric parent of an object

Post by onekk »

Why is there a tuple inside a list? Why return the top-parent as an object, and the rest as names in a string that needs to be split to be useful. And why add the object itself to that string? And why does the string end in "."? Unless I am missing something, and that may very well be the case, this seems very inconvenient.
From a programmers point of view tuple is very handy, as it is immutable and use less memory.

The trailing dot maybe is simply a consequences of the name composition methods, if you append a dot, and add something after you have not to take in account to check if the dot exist or not, plus the .split() function will return an empty string or nothing that could be parsed with a simple if statement.

For the two objects parents, I think that the method is returning the tree in a schematic way, the string has to be parsed maybe to obtain proper "Names" without retrieving the object itself and checking for his "Name" property .

It will be interesting to see how in the existing code the property is used, maybe a grep in the source tree will be interesting, or maybe some comments in the code will bring some light on the matter. Let me see.

Edit I: I've seen a similar writing somewhere in "programmin world", but now I don't rembember where.

EDIT II: Sorry no results, using Parents or .Parents in a grep -nR over the source tree of the 0.19 that I have handy on the computer.

I've searched the item on the /Mod/. tree, so maybe it is in the C++ section, or is derived from somewhere else, like an external library, I've searched also in Qt (using the Web Qt search field on Qt official pages but no luck)

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply