Merge pull request #111 from hyOzd/fix-minmaxselector
Fix min/max selector random behaviour
This commit is contained in:
commit
3503f7bfaf
|
@ -298,6 +298,10 @@ class CQ(object):
|
||||||
For now you can work around by creating a workplane and then offsetting the center
|
For now you can work around by creating a workplane and then offsetting the center
|
||||||
afterwards.
|
afterwards.
|
||||||
"""
|
"""
|
||||||
|
if len(self.objects) > 1:
|
||||||
|
raise ValueError("Workplane cannot be created if more than"
|
||||||
|
" 1 object is selected.")
|
||||||
|
|
||||||
obj = self.objects[0]
|
obj = self.objects[0]
|
||||||
|
|
||||||
def _computeXdir(normal):
|
def _computeXdir(normal):
|
||||||
|
|
|
@ -291,13 +291,13 @@ class DirectionMinMaxSelector(Selector):
|
||||||
allow '>(0,0,1)' to work.
|
allow '>(0,0,1)' to work.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def __init__(self,vector,directionMax=True):
|
def __init__(self, vector, directionMax=True, tolerance=0.0001):
|
||||||
self.vector = vector
|
self.vector = vector
|
||||||
self.max = max
|
self.max = max
|
||||||
self.directionMax = directionMax
|
self.directionMax = directionMax
|
||||||
|
self.TOLERANCE = tolerance
|
||||||
def filter(self,objectList):
|
def filter(self,objectList):
|
||||||
|
|
||||||
#then sort by distance from origin, along direction specified
|
|
||||||
def distance(tShape):
|
def distance(tShape):
|
||||||
return tShape.Center().dot(self.vector)
|
return tShape.Center().dot(self.vector)
|
||||||
#if tShape.ShapeType == 'Vertex':
|
#if tShape.ShapeType == 'Vertex':
|
||||||
|
@ -306,10 +306,14 @@ class DirectionMinMaxSelector(Selector):
|
||||||
# pnt = tShape.Center()
|
# pnt = tShape.Center()
|
||||||
#return pnt.dot(self.vector)
|
#return pnt.dot(self.vector)
|
||||||
|
|
||||||
|
# find out the max/min distance
|
||||||
if self.directionMax:
|
if self.directionMax:
|
||||||
return [ max(objectList,key=distance) ]
|
d = max(map(distance, objectList))
|
||||||
else:
|
else:
|
||||||
return [ min(objectList,key=distance) ]
|
d = min(map(distance, objectList))
|
||||||
|
|
||||||
|
# return all objects at the max/min distance (within a tolerance)
|
||||||
|
return filter(lambda o: abs(d - distance(o)) < self.TOLERANCE, objectList)
|
||||||
|
|
||||||
class BinarySelector(Selector):
|
class BinarySelector(Selector):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -147,6 +147,10 @@ class TestCQSelectors(BaseTest):
|
||||||
for v in c.faces(">Z").vertices().vals():
|
for v in c.faces(">Z").vertices().vals():
|
||||||
self.assertAlmostEqual(1.0,v.Z,3)
|
self.assertAlmostEqual(1.0,v.Z,3)
|
||||||
|
|
||||||
|
# test the case of multiple objects at the same distance
|
||||||
|
el = c.edges("<Z").vals()
|
||||||
|
self.assertEqual(4, len(el))
|
||||||
|
|
||||||
def testMinDistance(self):
|
def testMinDistance(self):
|
||||||
c = CQ(makeUnitCube())
|
c = CQ(makeUnitCube())
|
||||||
|
|
||||||
|
@ -159,6 +163,10 @@ class TestCQSelectors(BaseTest):
|
||||||
for v in c.faces("<Z").vertices().vals():
|
for v in c.faces("<Z").vertices().vals():
|
||||||
self.assertAlmostEqual(0.0,v.Z,3)
|
self.assertAlmostEqual(0.0,v.Z,3)
|
||||||
|
|
||||||
|
# test the case of multiple objects at the same distance
|
||||||
|
el = c.edges("<Z").vals()
|
||||||
|
self.assertEqual(4, len(el))
|
||||||
|
|
||||||
def testNearestTo(self):
|
def testNearestTo(self):
|
||||||
c = CQ(makeUnitCube())
|
c = CQ(makeUnitCube())
|
||||||
|
|
||||||
|
|
|
@ -442,7 +442,7 @@ class TestCadQuery(BaseTest):
|
||||||
c = CQ( makeUnitCube()) #the cube is the context solid
|
c = CQ( makeUnitCube()) #the cube is the context solid
|
||||||
self.assertEqual(6,c.faces().size()) #cube has six faces
|
self.assertEqual(6,c.faces().size()) #cube has six faces
|
||||||
|
|
||||||
r = c.faces().workplane().circle(0.125).extrude(0.5,True) #make a boss, not updating the original
|
r = c.faces('>Z').workplane().circle(0.125).extrude(0.5,True) #make a boss, not updating the original
|
||||||
self.assertEqual(8,r.faces().size()) #just the boss faces
|
self.assertEqual(8,r.faces().size()) #just the boss faces
|
||||||
self.assertEqual(8,c.faces().size()) #original is modified too
|
self.assertEqual(8,c.faces().size()) #original is modified too
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user