From 31ca5bc02e69d1342c8e3d63ebee9f1b3f646f34 Mon Sep 17 00:00:00 2001 From: UR-0 Date: Wed, 10 Feb 2021 12:17:25 +0100 Subject: [PATCH] upgrade getBoundBoxOfAllDocumentShapes to handle more objects --- src/Mod/Fem/femcommands/commands.py | 18 ++++++++------ src/Mod/Fem/femtools/femutils.py | 37 +++++++++++++++++------------ 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/Mod/Fem/femcommands/commands.py b/src/Mod/Fem/femcommands/commands.py index 5dfa994d24..832c1b84d0 100644 --- a/src/Mod/Fem/femcommands/commands.py +++ b/src/Mod/Fem/femcommands/commands.py @@ -83,13 +83,17 @@ class _ClippingPlaneAdd(CommandManager): from femtools.femutils import getBoundBoxOfAllDocumentShapes from femtools.femutils import getSelectedFace - overalboundbox = getBoundBoxOfAllDocumentShapes(FreeCAD.ActiveDocument) - # print(overalboundbox) - min_bb_length = (min(set([ - overalboundbox.XLength, - overalboundbox.YLength, - overalboundbox.ZLength - ]))) + overallboundbox = getBoundBoxOfAllDocumentShapes(FreeCAD.ActiveDocument) + # print(overallboundbox) + if overallboundbox: + min_bb_length = (min(set([ + overallboundbox.XLength, + overallboundbox.YLength, + overallboundbox.ZLength + ]))) + else: + min_bb_length = 10. # default + dbox = min_bb_length * 0.2 aFace = getSelectedFace(FreeCADGui.Selection.getSelectionEx()) diff --git a/src/Mod/Fem/femtools/femutils.py b/src/Mod/Fem/femtools/femutils.py index 660f4fcb97..d8aec06cc5 100644 --- a/src/Mod/Fem/femtools/femutils.py +++ b/src/Mod/Fem/femtools/femutils.py @@ -268,30 +268,37 @@ def getBoundBoxOfAllDocumentShapes(doc): Part and PartDesign objects). If the document contains no such objects or no objects at all return ``None``. """ - overalboundbox = None + overallboundbox = None # netgen mesh obj has an attribute Shape which is an Document obj, which has no BB # a FemMesh without a Shape could be clipped too # https://forum.freecadweb.org/viewtopic.php?f=18&t=52920 for o in doc.Objects: bb = None - if hasattr(o, "Shape") and hasattr(o.Shape, "BoundBox"): - try: - bb = o.Shape.BoundBox - except Exception: - pass - elif hasattr(o, "FemMesh") and hasattr(o.FemMesh, "BoundBox"): - try: - bb = o.FemMesh.BoundBox - except Exception: - pass + Types = ["Shape", "FemMesh", "Mesh", "Points"] + + for Type in Types: + FreeCAD.Console.PrintMessage("trying: " + str(o) + ":" + Type + "\n") # debug only + if hasattr(o, Type): + try: + bb = getattr(getattr(o,Type),"BoundBox") + FreeCAD.Console.PrintMessage(str(bb) + "\n") # debug only + break + except Exception: + FreeCAD.Console.PrintMessage("exception \n") # debug only + pass + if bb: if bb.isValid(): - if not overalboundbox: - overalboundbox = bb + if not overallboundbox: + overallboundbox = bb else: - overalboundbox.add(bb) - return overalboundbox + overallboundbox.add(bb) + else: # debug only + FreeCAD.Console.PrintMessage("no bb\n") # debug only + + FreeCAD.Console.PrintMessage(str(overallboundbox) + "\n") # debug only + return overallboundbox def getSelectedFace(selectionex):