Looping through children of parent compound object

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
ChrisFC
Posts: 27
Joined: Mon Jul 26, 2021 8:30 am

Looping through children of parent compound object

Post by ChrisFC »

Hi all,

I have a series of parts, most of which are compound objects. I would like to loop through each of these parent objects, and for each parent, loop through each child part within the parent compound object, and extract the vertexes. The reason I want to do this is because I need to append a 'nan' to a list of the x,y,z coords of these points, between each child object. This is because if I do not do this, and go to plot the points, you would get lines joining up different children objects that aren't always spatially connected.

Here is my code so far:

Code: Select all

sel = FreeCADGui.Selection.getSelection()
for o in sel:
  print('Checking o:')
  print(o)
  comp = o.Shape
  print(comp)
  print(comp.Vertexes)
  print(comp.Faces)
  print(comp.Solids)
Looping over the different parent objects is easy, and if for example I do o.Shape I get ALL of the vertexes of that entire compound object in a list. This isn't what I want; I need to loop over each child object within the compound to get their specific vertexes. comp.Solids and comp.Faces in the above code return empty lists for me :/ Any ideas?

Cheers,
Chris
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Looping through children of parent compound object

Post by onekk »

From the Python console introspection, explicited with the print statement.

maybe:

Code: Select all

>>> print(Part.Compound.childShapes.__doc__)
Return a list of sub-shapes that are direct children of this shape.
childShapes([cumOri=True, cumLoc=True]) -> list
--
 * If cumOri is true, the function composes all
   sub-shapes with the orientation of this shape.
 * If cumLoc is true, the function multiplies all
   sub-shapes by the location of this shape, i.e. it applies to
   each sub-shape the transformation that is associated with this shape.
        
>>> 
But the concept of subshapes and children may be not the required.

Hope it helps

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/
edwilliams16
Veteran
Posts: 3111
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Looping through children of parent compound object

Post by edwilliams16 »

If you have solids nested in compounds, how about

Code: Select all

sel = Gui.Selection.getSelectionEx()[0]
vlist=[sol.Vertexes for sol in sel.Object.Shape.Solids]
giving a list of vertex lists for the contained solids.
Post Reply