How to get the plane names in a body in python

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!
Post Reply
EskiBrew
Posts: 100
Joined: Fri Apr 24, 2015 10:21 am

How to get the plane names in a body in python

Post by EskiBrew »

I'm trying in python to set the support property of a sketch to a specific plane in the body.

Assuming I am working on the first body in the file, I can simply do
sketch.Support = body.getObject('XY_Plane')
for example but the plane names could be different, e.g. 'XY_Plane001' etc.

How can I get the plane name which corresponds to the notional XY_Plane or is there a better way of setting the support property of a sketch in python?
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to get the plane names in a body in python

Post by openBrain »

Code: Select all

body.Origin
To get planes & axis :

Code: Select all

body.Origin.OutList
EskiBrew
Posts: 100
Joined: Fri Apr 24, 2015 10:21 am

Re: How to get the plane names in a body in python

Post by EskiBrew »

Thank you.

So it looks like if I use
obj.Origin.FullName
it returns a string similar to "Unnamed#Origin001"
so I could strip off what I don't need and be left with 001 which I can append to "XY_Plane" to give "XY_Plane001".

Does that sound sensible?

Edit: It certainly works.
'XY_Plane' + body.Origin.FullName.split('Origin')[1]
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to get the plane names in a body in python

Post by openBrain »

IMO not using strings is always far better.
Here what I would do :

Code: Select all

[o for o in App.ActiveDocument.Body001.Origin.OutList if o.isDerivedFrom('App::Plane') and o.Placement.Rotation.isSame(App.Rotation(0,0,0,1), 1e-15)][0] # XY_Plane
[o for o in App.ActiveDocument.Body001.Origin.OutList if o.isDerivedFrom('App::Plane') and o.Placement.Rotation.isSame(App.Rotation(1,0,0,1), 1e-15)][0] # XZ_Plane
[o for o in App.ActiveDocument.Body001.Origin.OutList if o.isDerivedFrom('App::Plane') and o.Placement.Rotation.isSame(App.Rotation(1,1,1,1), 1e-15)][0] # YZ_Plane
EskiBrew
Posts: 100
Joined: Fri Apr 24, 2015 10:21 am

Re: How to get the plane names in a body in python

Post by EskiBrew »

Ah yes, that's good. Thanks 8-)
User avatar
jonasb
Posts: 162
Joined: Tue Dec 22, 2020 7:57 pm

Re: How to get the plane names in a body in python

Post by jonasb »

How about querying the GeoFeature's Role? I assume this has enum character and is "safe" to string-compare.

Code: Select all

>>> body = App.getDocument("Unnamed").getObject("Body001")
>>> xy_name = [o.Label for o in body.Origin.OriginFeatures if o.Role == 'XY_Plane'][0]
>>> xy_name
'XY_Plane001'
EskiBrew
Posts: 100
Joined: Fri Apr 24, 2015 10:21 am

Re: How to get the plane names in a body in python

Post by EskiBrew »

jonasb wrote: Fri Jan 21, 2022 9:33 pm How about querying the GeoFeature's Role? I assume this has enum character and is "safe" to string-compare.

Code: Select all

>>> body = App.getDocument("Unnamed").getObject("Body001")
>>> xy_name = [o.Label for o in body.Origin.OriginFeatures if o.Role == 'XY_Plane'][0]
>>> xy_name
'XY_Plane001'
Oh yes, just tried that and that works.
o.Role returns 'XY_Plane'
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to get the plane names in a body in python

Post by openBrain »

jonasb wrote: Fri Jan 21, 2022 9:33 pm How about querying the GeoFeature's Role?
This is great solution. ;)
Post Reply