diff --git a/utils.py b/utils.py index e26cfc9..e0db6fe 100644 --- a/utils.py +++ b/utils.py @@ -327,18 +327,57 @@ def getElementPos(obj): return edge.BoundBox.Center return pos +def getEdgeRotation(edge): + curve = edge.Curve + base = getattr(curve,'BasisCurve',None) + if base: + curve = base + rot = getattr(curve,'Rotation',None) + if rot: + return rot + if isLine(curve): + axis = curve.tangent(0)[0] + elif hasattr( curve, 'Axis'): #circular curve + axis = curve.Axis + else: + BSpline = curve.toBSpline() + arcs = BSpline.toBiArcs(10**-6) + if all( hasattr(a,'Center') for a in arcs ): + centers = np.array([a.Center for a in arcs]) + sigma = np.std( centers, axis=0 ) + if max(sigma) < 10**-6: #then circular curce + axis = arcs[0].Axis + if all(isLine(a) for a in arcs): + lines = arcs + D = np.array( + [L.tangent(0)[0] for L in lines]) #D(irections) + if np.std( D, axis=0 ).max() < 10**-9: #then linear curve + return D[0] + if not axis: + return FreeCAD.Rotation() + return FreeCAD.Rotation(FreeCAD.Vector(0,0,1),axis) def getElementRotation(obj,reverse=False): axis = None face = getElementShape(obj,Part.Face) - if face: + if not face: + edge = getElementShape(obj,Part.Edge) + if edge: + return getEdgeRotation(edge) + else: if face.Orientation == 'Reversed': reverse = not reverse surface = face.Surface + base = getattr(surface,'BasisSurface',None) + if base: + surface = base + rot = getattr(surface,'Rotation',None) + if rot: + return rot if hasattr(surface,'Axis'): axis = surface.Axis elif str(surface).startswith('