visibleBodies = [body for body in FreeCAD.ActiveDocument.Objects if body.isDerivedFrom("PartDesign::Body") and body.VisibleFeature]
Unfortunately it also selects Body that got sketch under Revolution effect. Even when it's parent is disabled.
It also selects in some other cases when the body should not be visible.
def AreParentsVisible(obj):
if len(obj.Parents)==0:
return True
#print(obj)
for parent in obj.Parents:
#print(parent)
if type(parent)==tuple:
parent = parent[0]
#print(parent.Name)
if parent.Visibility:
if AreParentsVisible(parent):
return True
return False
#print(AreParentsVisible(Gui.Selection.getSelection()[0]))
Gui.Selection.clearSelection()
for obj in FreeCAD.ActiveDocument.Objects:
if obj.ViewObject.Visibility and obj.isDerivedFrom("PartDesign::Body") and obj.VisibleFeature and AreParentsVisible(obj) :
Gui.Selection.addSelection(obj)
def GetParents(obj):
"""Returns a list of parent objects. If there are none an empty list is returned."""
parents = obj.Parents
if len(parents) == 0:
return []
else:
doc = obj.Document
return [parents[0][0]] + [doc.getObject(name) for name in parents[0][1].split(".")[:-2]]
def AreParentsVisible(obj):
for parent in GetParents(obj):
if not parent.Visibility:
return False
return True
Gui.Selection.clearSelection()
for obj in FreeCAD.ActiveDocument.Objects:
if obj.ViewObject.Visibility and obj.isDerivedFrom("PartDesign::Body") and obj.VisibleFeature and AreParentsVisible(obj):
Gui.Selection.addSelection(obj)
def GetParents(obj):
"""Returns a list of parent objects. If there are none an empty list is returned."""
parents = obj.Parents
if len(parents) == 0:
return []
else:
doc = obj.Document
return [parents[0][0]] + [doc.getObject(name) for name in parents[0][1].split(".")[:-2]]
def AreParentsVisible(obj):
for parent in GetParents(obj):
if not parent.Visibility:
return False
return True
Gui.Selection.clearSelection()
for obj in FreeCAD.ActiveDocument.Objects:
if obj.ViewObject.Visibility and obj.isDerivedFrom("PartDesign::Body") and obj.VisibleFeature and AreParentsVisible(obj):
Gui.Selection.addSelection(obj)
You don't like my method
I have to ask you for some explanation.
def getParents(obj):
if hasattr(obj,"Parents") and obj.Parents:
return [obj.Parents[0][0]]+[FreeCAD.ActiveDocument.getObject(p) for p in obj.Parents[0][1].split('.') if obj != FreeCAD.ActiveDocument.getObject(p) and p != '']
def isVisible(obj):
if not obj.ViewObject.Visibility:
return False
if not obj.VisibleFeature:
return False
parents = getParents(obj)
for par in parents:
if not par.ViewObject.Visibility:
return False
return True
bodies = [obj for obj in FreeCAD.ActiveDocument.Objects if obj.TypeId == "PartDesign::Body"]
FreeCADGui.Selection.clearSelection()
for body in bodies:
if isVisible(body):
FreeCADGui.Selection.addSelection(FreeCAD.ActiveDocument.Name,body.Name)