How to get the plane names in a body in python
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
How to get the plane names in a body in python
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?
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?
Re: How to get the plane names in a body in python
Code: Select all
body.Origin
Code: Select all
body.Origin.OutList
Re: How to get the plane names in a body in python
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]
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]
Re: How to get the plane names in a body in python
IMO not using strings is always far better.
Here what I would do :
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
Re: How to get the plane names in a body in python
Ah yes, that's good. Thanks
Re: How to get the plane names in a body in python
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'
Re: How to get the plane names in a body in python
Oh yes, just tried that and that works.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'
o.Role returns 'XY_Plane'