utils: use new 'Rotation' attribute of geometry

This commit is contained in:
Zheng, Lei 2018-08-17 11:59:18 +08:00
parent f881db0229
commit 46165f73d4

View File

@ -327,38 +327,20 @@ def getElementPos(obj):
return edge.BoundBox.Center return edge.BoundBox.Center
return pos return pos
def getEdgeRotation(edge):
def getElementRotation(obj,reverse=False): curve = edge.Curve
axis = None base = getattr(curve,'BasisCurve',None)
face = getElementShape(obj,Part.Face) if base:
if face: curve = base
if face.Orientation == 'Reversed': rot = getattr(curve,'Rotation',None)
reverse = not reverse if rot:
surface = face.Surface return rot
if hasattr(surface,'Axis'): if isLine(curve):
axis = surface.Axis axis = curve.tangent(0)[0]
elif str(surface).startswith('<SurfaceOfRevolution'): elif hasattr( curve, 'Axis'): #circular curve
axis = face.Edge1.Curve.Axis axis = curve.Axis
else: #numerically approximating surface
plane_norm, _plane_pos, error = \
fit_plane_to_surface1(face.Surface)
error_normalized = error / face.BoundBox.DiagonalLength
if error_normalized < 10**-6: #then good plane fit
axis = FreeCAD.Vector(plane_norm)
axis_fitted, _center, error = \
fit_rotation_axis_to_surface1(face.Surface)
error_normalized = error / face.BoundBox.DiagonalLength
if error_normalized < 10**-6: #then good rotation_axis fix
axis = FreeCAD.Vector(axis_fitted)
else: else:
edge = getElementShape(obj,Part.Edge) BSpline = curve.toBSpline()
if edge:
if isLine(edge.Curve):
axis = edge.Curve.tangent(0)[0]
elif hasattr( edge.Curve, 'Axis'): #circular curve
axis = edge.Curve.Axis
else:
BSpline = edge.Curve.toBSpline()
arcs = BSpline.toBiArcs(10**-6) arcs = BSpline.toBiArcs(10**-6)
if all( hasattr(a,'Center') for a in arcs ): if all( hasattr(a,'Center') for a in arcs ):
centers = np.array([a.Center for a in arcs]) centers = np.array([a.Center for a in arcs])
@ -371,6 +353,43 @@ def getElementRotation(obj,reverse=False):
[L.tangent(0)[0] for L in lines]) #D(irections) [L.tangent(0)[0] for L in lines]) #D(irections)
if np.std( D, axis=0 ).max() < 10**-9: #then linear curve if np.std( D, axis=0 ).max() < 10**-9: #then linear curve
return D[0] 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 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('<SurfaceOfRevolution'):
return getEdgeRotation(face.Edge1)
else: #numerically approximating surface
plane_norm, _plane_pos, error = \
fit_plane_to_surface1(face.Surface)
error_normalized = error / face.BoundBox.DiagonalLength
if error_normalized < 10**-6: #then good plane fit
axis = FreeCAD.Vector(plane_norm)
axis_fitted, _center, error = \
fit_rotation_axis_to_surface1(face.Surface)
error_normalized = error / face.BoundBox.DiagonalLength
if error_normalized < 10**-6: #then good rotation_axis fix
axis = FreeCAD.Vector(axis_fitted)
if not axis: if not axis:
return FreeCAD.Rotation() return FreeCAD.Rotation()
return FreeCAD.Rotation(FreeCAD.Vector(0,0,-1 if reverse else 1),axis) return FreeCAD.Rotation(FreeCAD.Vector(0,0,-1 if reverse else 1),axis)
@ -402,6 +421,7 @@ def getNormal(obj):
else: else:
rot = getElementRotation(obj) rot = getElementRotation(obj)
q = rot.Q q = rot.Q
# return as w,x,y,z
return q[3],q[0],q[1],q[2] return q[3],q[0],q[1],q[2]
def getElementDirection(obj,pla=None): def getElementDirection(obj,pla=None):