Scripted references to body origin planes

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
vectronic
Posts: 52
Joined: Sun Feb 18, 2018 9:44 pm

Scripted references to body origin planes

Post by vectronic »

Hello,

I can reference a body created in the PartDesign workbench using this (just an example):

b = Gui.getDocument('Unnamed').ActiveView.getActiveObject('pdbody')

If I wanted to reference the YZ plane of the Origin of this body is there any preferred way other than the following:

yz = b.Origin.OriginFeatures[5]

Thanks very much.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Scripted references to body origin planes

Post by DeepSOIC »

vectronic wrote: Sat Oct 13, 2018 9:53 pm If I wanted to reference the YZ plane of the Origin of this body is there any preferred way other than the following:
Apparently, in python - no. In C++, there is a plenty of methods for obtaining specific pieces:
https://github.com/FreeCAD/FreeCAD/blob ... igin.h#L75
So it's just a matter of adding py interface. I invite you to write a feature request in tracker, please ;)
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Scripted references to body origin planes

Post by TheMarkster »

Accessing directly by index is probably a bad idea because if the implementation changes it could break your code. This might be a more robust approach:

Code: Select all

b = Gui.getDocument('Unnamed').ActiveView.getActiveObject('pdbody')
for feat in b.Origin.OriginFeatures:
    if "XZ_Plane" in feat.Label:
        xz = feat
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Scripted references to body origin planes

Post by DeepSOIC »

TheMarkster wrote: Mon Oct 15, 2018 11:28 pm if "XZ_Plane" in feat.Label
and this one is not language-proof. Although these particular labels are not translatable yet, by the looks of it.
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Scripted references to body origin planes

Post by TheMarkster »

DeepSOIC wrote: Tue Oct 16, 2018 12:04 am
TheMarkster wrote: Mon Oct 15, 2018 11:28 pm if "XZ_Plane" in feat.Label
and this one is not language-proof. Although these particular labels are not translatable yet, by the looks of it.
Maybe should use feat.Name instead of feat.Label?
User avatar
wandererfan
Veteran
Posts: 6324
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Scripted references to body origin planes

Post by wandererfan »

TheMarkster wrote: Tue Oct 16, 2018 12:37 am Maybe should use feat.Name instead of feat.Label?
Yes, absolutely. Names are guaranteed unique and constant. Labels are neither.
vectronic
Posts: 52
Joined: Sun Feb 18, 2018 9:44 pm

Re: Scripted references to body origin planes

Post by vectronic »

DeepSOIC wrote: Sat Oct 13, 2018 11:24 pm Apparently, in python - no. In C++, there is a plenty of methods for obtaining specific pieces:
https://github.com/FreeCAD/FreeCAD/blob ... igin.h#L75
So it's just a matter of adding py interface. I invite you to write a feature request in tracker, please ;)
Just following up on this I have raised a request:

https://www.freecadweb.org/tracker/view.php?id=3770

Thanks very much.
Post Reply