How to get planes names belonging to a Body or Part?

92 Views Asked by At

I am trying to generate a Sketch from a body or a part. If I record a macro of my work, FreeCAD gives me this:

### Begin command PartDesign_NewSketch
Gui.getDocument('my_doc_name').ActiveView.setActiveObject('pdbody',App.getDocument('my_doc_name').getObject('my_part_name'),'my_body_name.')
### End command PartDesign_NewSketch
# Gui.Selection.clearSelection()
# Gui.Selection.addSelection('my_doc_name','my_part_name','my_body_name.Origin008.XZ_Plane008.')
App.getDocument('my_doc_name').getObject('my_body_name').newObject('Sketcher::SketchObject','Sketch')
App.getDocument('my_doc_name').getObject('my_sketch_name').Support = (App.getDocument('my_doc_name').getObject('XZ_Plane008'),[''])
App.getDocument('my_doc_name').getObject('my_sketch_name').MapMode = 'FlatFace'
App.ActiveDocument.recompute()

My goal is to reproduce the following above commands in my Python script. The issue is that when I created my Body or Part, I didn't (yet) knew what will be the names of the Origin, Axis or Planes. Here in my example, we can see that it is Origin008 and one of the plane is XZ_Plane008

So my question is, how can I have a getListOfPlanes method like, or something like:

for plane in self.freecad_document.getObject(body_name).getListOfPlanes():
  print(plane.name)

Output would be like:

XY_Plane008
XZ_Plane008
YZ_Plane008

Exactly like: Tree view in FreeCAD

Notice that when I generate my 3D structure, I could need either the XY, XZ or YZ plane. This is why I would require the list of planes available and select the best one that fits my needs.

I also try to do some reverse engineering with some dir and __dict__ but I didn't find any attribute value that have a *008* name. The wikidoc documentation did not help me either.

Any idea?

Using

  • FreeCAD 0.20.2 (2022/12/07)
  • Python 3.11.5 (2023/08/24)
1

There are 1 best solutions below

0
user12642493 On

I wrote the answer here: https://forum.freecad.org/viewtopic.php?p=710380#p710380

In short:

for origin_feature in App.getDocument('my_doc_name').getObject('my_body_or_part_name').Origin.OriginFeatures:
print("New OriginFeature detected - Name = [{0}] - Label = [{1}]".format(origin_feature.Name, origin_feature.Label))

Produces the following output:

New OriginFeature detected - Name = [X_Axis008] - Label = [X_Axis008]
New OriginFeature detected - Name = [Y_Axis008] - Label = [Y_Axis008]
New OriginFeature detected - Name = [Z_Axis008] - Label = [Z_Axis008]
New OriginFeature detected - Name = [XY_Plane008] - Label = [XY_Plane008]
New OriginFeature detected - Name = [XZ_Plane008] - Label = [XZ_Plane008]
New OriginFeature detected - Name = [YZ_Plane008] - Label = [YZ_Plane008]

Be careful:

Using __dict__ to debug or search for a value does NOT work. For instance:

App.getDocument('my_doc_name').getObject('my_body_name').Origin.OriginFeatures[3].__dict__
App.getDocument('my_doc_name').getObject('my_body_name').Origin.OriginFeatures[3].Label

Produces the following output:

{'ExpressionEngine': '', 'Label': '', 'Label2': '', 'Placement': '', 'Role': '', 'Visibility': ''}
XY_Plane008

As you can see, the __dict__ do NOT print the real value of Label, and Name attribute is not even shown. So do not use __dict__ for debug.