utils: imporve element checking performance

This commit is contained in:
Zheng, Lei 2018-07-13 11:16:05 +08:00
parent 8f7a92634c
commit 7a7b9b49e1

View File

@ -67,15 +67,15 @@ def deduceSelectedElement(obj,subname):
shape = obj.getSubObject(subname) shape = obj.getSubObject(subname)
if not shape: if not shape:
return return
count = len(shape.Faces) count = shape.countElement('Face')
if count==1: if count==1:
return 'Face1' return 'Face1'
elif not count: elif not count:
count = len(shape.Edges) count = shape.countElement('Edge')
if count==1: if count==1:
return 'Edge1' return 'Edge1'
elif not count: elif not count:
count = len(shape.Vertexes) count = shape.countElement('Vertex')
if count==1: if count==1:
return 'Vertex1' return 'Vertex1'
@ -122,21 +122,18 @@ def getElementShape(obj,tp=None,transform=False,noElementMap=True):
logger.trace('wrong shape type {}'.format(obj)) logger.trace('wrong shape type {}'.format(obj))
return return
elif tp is Part.Vertex: elif tp is Part.Vertex:
if len(shape.Edges): if shape.countElement('Edge'):
return return
v = shape.Vertexes if shape.countElement('Vertex')==1:
if len(v)==1: return shape.Vertex1
return v[0]
elif tp is Part.Edge: elif tp is Part.Edge:
if len(shape.Faces): if shape.countElement('Face'):
return return
e = shape.Edges if shape.countElement('Edge')==1:
if len(e)==1: return shape.Edge1
return e[0]
elif tp is Part.Face: elif tp is Part.Face:
f = shape.Faces if shape.countElement('Face')==1:
if len(f)==1: return shape.Face1
return f[0]
else: else:
logger.trace('wrong shape type {}'.format(obj)) logger.trace('wrong shape type {}'.format(obj))
@ -172,9 +169,9 @@ def isElement(obj):
if isinstance(obj,(Part.Vertex,Part.Face,Part.Edge)): if isinstance(obj,(Part.Vertex,Part.Face,Part.Edge)):
return True return True
if isinstance(shape,Part.Shape) and not shape.isNull(): if isinstance(shape,Part.Shape) and not shape.isNull():
return len(shape.Vertexes)==1 or \ return shape.countElement('Vertex')==1 or \
len(shape.Edges)==1 or \ shape.countElement('Edge')==1 or \
len(shape.Faces)==1 shape.countElement('Face')==1
def isPlanar(obj): def isPlanar(obj):
if isCircularEdge(obj): if isCircularEdge(obj):
@ -290,7 +287,7 @@ def getElementPos(obj):
elif all( hasattr(surface,a) for a in ['Axis','Center','Radius'] ): elif all( hasattr(surface,a) for a in ['Axis','Center','Radius'] ):
pos = surface.Center pos = surface.Center
elif str(surface).startswith('<SurfaceOfRevolution'): elif str(surface).startswith('<SurfaceOfRevolution'):
pos = face.Edges1.Curve.Center pos = face.Edge1.Curve.Center
else: #numerically approximating surface else: #numerically approximating surface
_plane_norm, plane_pos, error = \ _plane_norm, plane_pos, error = \
fit_plane_to_surface1(face.Surface) fit_plane_to_surface1(face.Surface)
@ -307,7 +304,7 @@ def getElementPos(obj):
if edge: if edge:
if isLine(edge.Curve): if isLine(edge.Curve):
# pos = edge.Vertexes[-1].Point # pos = edge.Vertexes[-1].Point
pos = (edge.Vertexes[0].Point+edge.Vertexes[1].Point)*0.5 pos = (edge.Vertex1.Point+edge.Vertex2.Point)*0.5
elif hasattr( edge.Curve, 'Center'): #circular curve elif hasattr( edge.Curve, 'Center'): #circular curve
pos = edge.Curve.Center pos = edge.Curve.Center
else: else:
@ -338,7 +335,7 @@ def getElementRotation(obj,reverse=False):
if hasattr(surface,'Axis'): if hasattr(surface,'Axis'):
axis = surface.Axis axis = surface.Axis
elif str(surface).startswith('<SurfaceOfRevolution'): elif str(surface).startswith('<SurfaceOfRevolution'):
axis = face.Edges[0].Curve.Axis axis = face.Edge1.Curve.Axis
else: #numerically approximating surface else: #numerically approximating surface
plane_norm, _plane_pos, error = \ plane_norm, _plane_pos, error = \
fit_plane_to_surface1(face.Surface) fit_plane_to_surface1(face.Surface)
@ -407,8 +404,8 @@ def getNormal(obj):
def getElementDirection(obj,pla=None): def getElementDirection(obj,pla=None):
if isLinearEdge(obj): if isLinearEdge(obj):
shape = getElementShape(obj,Part.Edge) shape = getElementShape(obj,Part.Edge)
vs = shape.Edge1.Vertexes e = shape.Edge1
v = vs[0].Point - vs[1].Point v = e.Vertex1.Point - e.Vertex2.Point
else: else:
rot = getElementRotation(obj) rot = getElementRotation(obj)
v = rot.multVec(FreeCAD.Vector(0,0,1)) v = rot.multVec(FreeCAD.Vector(0,0,1))