def testBaseDirSelector(self): # BaseDirSelector isn't intended to be instantiated, use subclass # ParallelDirSelector to test the code in BaseDirSelector loose_selector = ParallelDirSelector(Vector(0, 0, 1), tolerance=10) c = Workplane(makeUnitCube(centered=True)) # BaseDirSelector should filter out everything but Faces and Edges with # geomType LINE self.assertNotEqual(c.vertices().size(), 0) self.assertEqual(c.vertices(loose_selector).size(), 0) # This has an edge that is not a LINE c_curves = Workplane().sphere(1) self.assertNotEqual(c_curves.edges(), 0) self.assertEqual(c_curves.edges(loose_selector).size(), 0) # this has a Face that is not a PLANE face_dir = c_curves.faces().val().normalAt(None) self.assertNotEqual(c_curves.faces(), 0) self.assertEqual( c_curves.faces(ParallelDirSelector(face_dir, tolerance=10)).size(), 0) self.assertNotEqual(c.solids().size(), 0) self.assertEqual(c.solids(loose_selector).size(), 0) comp = Workplane(makeUnitCube()).workplane().move(10, 10).box(1, 1, 1) self.assertNotEqual(comp.compounds().size(), 0) self.assertEqual(comp.compounds(loose_selector).size(), 0)
def testParallelPlaneFaceFilter(self): c = CQ(makeUnitCube(centered=False)) # faces parallel to Z axis # these two should produce the same behaviour: for s in ["|Z", selectors.ParallelDirSelector(Vector(0, 0, 1))]: parallel_faces = c.faces(s) self.assertEqual(2, parallel_faces.size()) for f in parallel_faces.vals(): self.assertAlmostEqual( abs(f.normalAt(None).dot(Vector(0, 0, 1))), 1) self.assertEqual(2, c.faces( selectors.ParallelDirSelector(Vector( (0, 0, -1)))).size()) # same thing as above # just for fun, vertices on faces parallel to z self.assertEqual(8, c.faces("|Z").vertices().size()) # check that the X & Y center of these faces is the same as the box (ie. we haven't selected the wrong face) faces = c.faces(selectors.ParallelDirSelector(Vector( (0, 0, 1)))).vals() for f in faces: c = f.Center() self.assertAlmostEqual(c.x, 0.5) self.assertAlmostEqual(c.y, 0.5)
def testChamfer(self): """ Test chamfer API with a box shape """ cube = CQ(makeUnitCube()).faces(">Z").chamfer(0.1) self.saveModel(cube) self.assertEqual(10, cube.faces().size())
def testTranslateSolid(self): c = CQ(makeUnitCube()) self.assertAlmostEqual(0.0,c.faces("<Z").vertices().item(0).val().Z, 3 ) #TODO: it might be nice to provide a version of translate that modifies the existing geometry too d = c.translate(Vector(0,0,1.5)) self.assertAlmostEqual(1.5,d.faces("<Z").vertices().item(0).val().Z, 3 )
def testFillet(self): """ Tests filleting edges on a solid """ c = CQ( makeUnitCube()).faces(">Z").workplane().circle(0.25).extrude(0.25,True).edges("|Z").fillet(0.2) self.saveModel(c) self.assertEqual(12,c.faces().size() )
def testFaceDirFilter(self): c = CQ(makeUnitCube()) # a cube has one face in each direction self.assertEqual(1, c.faces("+Z").size()) self.assertTupleAlmostEquals((0, 0, 1), c.faces("+Z").val().Center().toTuple(), 3) self.assertEqual(1, c.faces("-Z").size()) self.assertTupleAlmostEquals((0, 0, 0), c.faces("-Z").val().Center().toTuple(), 3) self.assertEqual(1, c.faces("+X").size()) self.assertTupleAlmostEquals((0.5, 0, 0.5), c.faces("+X").val().Center().toTuple(), 3) self.assertEqual(1, c.faces("-X").size()) self.assertTupleAlmostEquals((-0.5, 0, 0.5), c.faces("-X").val().Center().toTuple(), 3) self.assertEqual(1, c.faces("+Y").size()) self.assertTupleAlmostEquals((0, 0.5, 0.5), c.faces("+Y").val().Center().toTuple(), 3) self.assertEqual(1, c.faces("-Y").size()) self.assertTupleAlmostEquals((0, -0.5, 0.5), c.faces("-Y").val().Center().toTuple(), 3) self.assertEqual(0, c.faces("XY").size()) self.assertEqual(1, c.faces("X").size()) # should be same as +X self.assertEqual( c.faces("+X").val().Center(), c.faces("X").val().Center()) self.assertNotEqual( c.faces("+X").val().Center(), c.faces("-X").val().Center())
def testFaceTypesFilter(self): "Filters by face type" c = CQ(makeUnitCube()) self.assertEqual(c.faces().size(), c.faces("%PLANE").size()) self.assertEqual(c.faces().size(), c.faces("%plane").size()) self.assertEqual(0, c.faces("%sphere").size()) self.assertEqual(0, c.faces("%cone").size()) self.assertEqual(0, c.faces("%SPHERE").size())
def testSolidReferencesCombine(self): "test that solid references are updated correctly" c = CQ( makeUnitCube()) #the cube is the context solid 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 self.assertEqual(8,r.faces().size()) #just the boss faces self.assertEqual(8,c.faces().size()) #original is modified too
def testFrontReference(self): s = CQ(makeUnitCube()).faces("front").workplane() #make a workplane on the top face r = s.circle(0.125).cutBlind(-2.0) self.saveModel(r) #the result should have 7 faces self.assertEqual(7,r.faces().size() ) self.assertEqual(type(r.val()), Solid) self.assertEqual(type(r.first().val()),Solid)
def testFaceTypesFilter(self): "Filters by face type" c = CQ(makeUnitCube()) self.assertEqual(c.faces().size(), c.faces('%PLANE').size()) self.assertEqual(c.faces().size(), c.faces('%plane').size()) self.assertEqual(0, c.faces('%sphere').size()) self.assertEqual(0, c.faces('%cone').size()) self.assertEqual(0, c.faces('%SPHERE').size())
def testSolidReferencesCombine(self): "test that solid references are updated correctly" c = CQ(makeUnitCube()) #the cube is the context solid 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 self.assertEqual(8, r.faces().size()) #just the boss faces self.assertEqual(8, c.faces().size()) #original is modified too
def testTranslateSolid(self): c = CQ(makeUnitCube()) self.assertAlmostEqual(0.0, c.faces("<Z").vertices().item(0).val().Z, 3) #TODO: it might be nice to provide a version of translate that modifies the existing geometry too d = c.translate(Vector(0, 0, 1.5)) self.assertAlmostEqual(1.5, d.faces("<Z").vertices().item(0).val().Z, 3)
def testFrontReference(self): s = CQ(makeUnitCube()).faces( "front").workplane() #make a workplane on the top face r = s.circle(0.125).cutBlind(-2.0) self.saveModel(r) #the result should have 7 faces self.assertEqual(7, r.faces().size()) self.assertEqual(type(r.val()), Solid) self.assertEqual(type(r.first().val()), Solid)
def testLengthNthSelector_EdgesOfUnitCube(self): """ Selecting all edges of unit cube """ w1 = Workplane(makeUnitCube()).edges(selectors.LengthNthSelector(0)) self.assertEqual( 12, w1.size(), msg="Failed to select edges of a unit cube: wrong number of edges", )
def testParallelPlaneFaceFilter(self): c = CQ(makeUnitCube()) #faces parallel to Z axis self.assertEqual(2, c.faces("|Z").size()) #TODO: provide short names for ParallelDirSelector self.assertEqual(2, c.faces(selectors.ParallelDirSelector(Vector((0,0,1)))).size()) #same thing as above self.assertEqual(2, c.faces(selectors.ParallelDirSelector(Vector((0,0,-1)))).size()) #same thing as above #just for fun, vertices on faces parallel to z self.assertEqual(8, c.faces("|Z").vertices().size())
def testFaceDirFilter(self): c = CQ(makeUnitCube()) # a cube has one face in each direction self.assertEqual(1, c.faces("+Z").size()) self.assertEqual(1, c.faces("-Z").size()) self.assertEqual(1, c.faces("+X").size()) self.assertEqual(1, c.faces("X").size()) # should be same as +X self.assertEqual(1, c.faces("-X").size()) self.assertEqual(1, c.faces("+Y").size()) self.assertEqual(1, c.faces("-Y").size()) self.assertEqual(0, c.faces("XY").size())
def testParallelPlaneFaceFilter(self): c = CQ(makeUnitCube()) #faces parallel to Z axis self.assertEqual(2, c.faces("|Z").size()) #TODO: provide short names for ParallelDirSelector self.assertEqual(2, c.faces(selectors.ParallelDirSelector(Vector((0,0,1)))).size()) #same thing as above self.assertEqual(2, c.faces(selectors.ParallelDirSelector(Vector((0,0,-1)))).size()) #same thing as above #just for fun, vertices on faces parallel to z self.assertEqual(8, c.faces("|Z").vertices().size())
def testFaceDirFilter(self): c = CQ(makeUnitCube()) #a cube has one face in each direction self.assertEqual(1, c.faces("+Z").size()) self.assertEqual(1, c.faces("-Z").size()) self.assertEqual(1, c.faces("+X").size()) self.assertEqual(1, c.faces("X").size()) #should be same as +X self.assertEqual(1, c.faces("-X").size()) self.assertEqual(1, c.faces("+Y").size()) self.assertEqual(1, c.faces("-Y").size()) self.assertEqual(0, c.faces("XY").size())
def testSubtractSelector(self): c = CQ(makeUnitCube()) S = selectors.StringSyntaxSelector fl = c.faces(selectors.SubtractSelector(S("#Z"), S(">X"))).vals() self.assertEqual(3, len(fl)) # test the subtract operator fl = c.faces(S("#Z") - S(">X")).vals() self.assertEqual(3, len(fl))
def testMinDistance(self): c = CQ(makeUnitCube()) #should select the topmost face self.assertEqual(1, c.faces("<Z").size()) self.assertEqual(4, c.faces("<Z").vertices().size()) #vertices should all be at z=1, if this is the top face self.assertEqual(4, len(c.faces("<Z").vertices().vals() )) for v in c.faces("<Z").vertices().vals(): self.assertAlmostEqual(0.0,v.Z,3)
def testSubtractSelector(self): c = CQ(makeUnitCube()) S = selectors.StringSyntaxSelector fl = c.faces(selectors.SubtractSelector(S("#Z"), S(">X"))).vals() self.assertEqual(3, len(fl)) # test the subtract operator fl = c.faces(S("#Z") - S(">X")).vals() self.assertEqual(3, len(fl))
def testParallelEdgeFilter(self): c = CQ(makeUnitCube()) for sel, vec in zip( ["|X", "|Y", "|Z"], [Vector(1, 0, 0), Vector(0, 1, 0), Vector(0, 0, 1)] ): edges = c.edges(sel) # each direction should have 4 edges self.assertEqual(4, edges.size()) # each edge should be parallel with vec and have a cross product with a length of 0 for e in edges.vals(): self.assertAlmostEqual(e.tangentAt(0).cross(vec).Length, 0.0)
def testRadiusNthSelector(self): # test the key method behaves rad = 2.3 arc = Edge.makeCircle(radius=rad) sel = selectors.RadiusNthSelector(0) self.assertAlmostEqual(rad, sel.key(arc), 3) line = Edge.makeLine(Vector(0, 0, 0), Vector(1, 1, 1)) with self.assertRaises(ValueError): sel.key(line) solid = makeUnitCube() with self.assertRaises(ValueError): sel.key(solid) part = (Workplane().box( 10, 10, 1).edges(">(1, 1, 0) and |Z").fillet(1).edges( ">(-1, 1, 0) and |Z").fillet(1).edges(">(-1, -1, 0) and |Z"). fillet(2).edges(">(1, -1, 0) and |Z").fillet(3).faces(">Z")) # smallest radius is 1.0 self.assertAlmostEqual( part.edges(selectors.RadiusNthSelector(0)).val().radius(), 1.0) # there are two edges with the smallest radius self.assertEqual( len(part.edges(selectors.RadiusNthSelector(0)).vals()), 2) # next radius is 2.0 self.assertAlmostEqual( part.edges(selectors.RadiusNthSelector(1)).val().radius(), 2.0) # largest radius is 3.0 self.assertAlmostEqual( part.edges(selectors.RadiusNthSelector(-1)).val().radius(), 3.0) # accessing index 3 should be an IndexError with self.assertRaises(IndexError): part.edges(selectors.RadiusNthSelector(3)) # reversed self.assertAlmostEqual( part.edges(selectors.RadiusNthSelector( 0, directionMax=False)).val().radius(), 3.0, ) # test the selector on wires wire_circles = (Workplane().circle(2).moveTo(10, 0).circle(2).moveTo( 20, 0).circle(4).consolidateWires()) self.assertEqual( len(wire_circles.wires(selectors.RadiusNthSelector(0)).vals()), 2) self.assertEqual( len(wire_circles.wires(selectors.RadiusNthSelector(1)).vals()), 1) self.assertAlmostEqual( wire_circles.wires(selectors.RadiusNthSelector(0)).val().radius(), 2) self.assertAlmostEqual( wire_circles.wires(selectors.RadiusNthSelector(1)).val().radius(), 4)
def testSplitKeepingHalf(self): "Tests splitting a solid" #drill a hole in the side c = CQ(makeUnitCube()).faces(">Z").workplane().circle(0.25).cutThruAll() self.assertEqual(7,c.faces().size() ) #now cut it in half sideways c.faces(">Y").workplane(-0.5).split(keepTop=True) self.saveModel(c) self.assertEqual(8,c.faces().size())
def testPerpendicularDirFilter(self): c = CQ(makeUnitCube()) perp_edges = c.edges("#Z") self.assertEqual(8, perp_edges.size()) # 8 edges are perp. to z # dot product of perpendicular vectors is zero for e in perp_edges.vals(): self.assertAlmostEqual(e.tangentAt(0).dot(Vector(0, 0, 1)), 0.0) perp_faces = c.faces("#Z") self.assertEqual(4, perp_faces.size()) # 4 faces are perp to z too! for f in perp_faces.vals(): self.assertAlmostEqual(f.normalAt(None).dot(Vector(0, 0, 1)), 0.0)
def testVertexFilter(self): "test selecting vertices on a face" c = CQ(makeUnitCube()) #TODO: filters work ok, but they are in global coordinates which sux. it would be nice #if they were available in coordinates local to the selected face v2 = c.faces("+Z").vertices("<XY") self.assertEqual(1,v2.size() ) #another way #make sure the vertex is the right one self.assertTupleAlmostEquals((0.0,0.0,1.0),v2.val().toTuple() ,3)
def testPointList(self): "Tests adding points and using them" c = CQ(makeUnitCube()) s = c.faces(">Z").workplane().pushPoints([(-0.3,0.3),(0.3,0.3),(0,0)]) self.assertEqual(3,s.size()) #TODO: is the ability to iterate over points with circle really worth it? #maybe we should just require using all() and a loop for this. the semantics and #possible combinations got too hard ( ie, .circle().circle() ) was really odd body = s.circle(0.05).cutThruAll() self.saveModel(body) self.assertEqual(9,body.faces().size())
def testVertexFilter(self): "test selecting vertices on a face" c = CQ(makeUnitCube()) # TODO: filters work ok, but they are in global coordinates which sux. it would be nice # if they were available in coordinates local to the selected face v2 = c.faces("+Z").vertices("<XY") self.assertEqual(1, v2.size()) # another way # make sure the vertex is the right one self.assertTupleAlmostEquals((0.0, 0.0, 1.0), v2.val().toTuple(), 3)
def testAndSelector(self): c = CQ(makeUnitCube()) S = selectors.StringSyntaxSelector BS = selectors.BoxSelector el = c.edges(selectors.AndSelector(S('|X'), BS((-2,-2,0.1), (2,2,2)))).vals() self.assertEqual(2, len(el)) # test 'and' (intersection) operator el = c.edges(S('|X') & BS((-2,-2,0.1), (2,2,2))).vals() self.assertEqual(2, len(el))
def testPointList(self): "Tests adding points and using them" c = CQ(makeUnitCube()) s = c.faces(">Z").workplane().pushPoints([(-0.3, 0.3), (0.3, 0.3), (0, 0)]) self.assertEqual(3, s.size()) #TODO: is the ability to iterate over points with circle really worth it? #maybe we should just require using all() and a loop for this. the semantics and #possible combinations got too hard ( ie, .circle().circle() ) was really odd body = s.circle(0.05).cutThruAll() self.saveModel(body) self.assertEqual(9, body.faces().size())
def testChamferAsymmetrical(self): """ Test chamfer API with a box shape for asymmetrical lengths """ cube = CQ(makeUnitCube()).faces(">Z").chamfer(0.1, 0.2) self.saveModel(cube) self.assertEqual(10, cube.faces().size()) # test if edge lengths are different edge = cube.edges(">Z").vals()[0] self.assertAlmostEqual(0.6, edge.Length(), 3) edge = cube.edges("|Z").vals()[0] self.assertAlmostEqual(0.9, edge.Length(), 3)
def testSplitKeepingHalf(self): "Tests splitting a solid" #drill a hole in the side c = CQ( makeUnitCube()).faces(">Z").workplane().circle(0.25).cutThruAll() self.assertEqual(7, c.faces().size()) #now cut it in half sideways c.faces(">Y").workplane(-0.5).split(keepTop=True) self.saveModel(c) self.assertEqual(8, c.faces().size())
def testSplitKeepingBottom(self): """ Tests splitting a solid improperly """ # Drill a hole in the side c = CQ(makeUnitCube()).faces(">Z").workplane().circle(0.25).cutThruAll() self.assertEqual(7, c.faces().size()) # Now cut it in half sideways result = c.faces(">Y").workplane(-0.5).split(keepTop=False, keepBottom=True) #stack will have both halves, original will be unchanged self.assertEqual(1, result.solids().size()) # one solid is on the stack self.assertEqual(8, result.solids().item(0).faces().size())
def testAndSelector(self): c = CQ(makeUnitCube()) S = selectors.StringSyntaxSelector BS = selectors.BoxSelector el = c.edges( selectors.AndSelector(S('|X'), BS((-2, -2, 0.1), (2, 2, 2)))).vals() self.assertEqual(2, len(el)) # test 'and' (intersection) operator el = c.edges(S('|X') & BS((-2, -2, 0.1), (2, 2, 2))).vals() self.assertEqual(2, len(el))
def testSplitKeepingBoth(self): "Tests splitting a solid" #drill a hole in the side c = CQ(makeUnitCube()).faces(">Z").workplane().circle(0.25).cutThruAll() self.assertEqual(7,c.faces().size() ) #now cut it in half sideways result = c.faces(">Y").workplane(-0.5).split(keepTop=True,keepBottom=True) #stack will have both halves, original will be unchanged self.assertEqual(2, result.solids().size()) #two solids are on the stack, eac self.assertEqual(8,result.solids().item(0).faces().size()) self.assertEqual(8,result.solids().item(1).faces().size())
def testSolid(self): c = CQ(makeUnitCube()) #make sure all the counts are right for a cube self.assertEqual(1,c.solids().size() ) self.assertEqual(6,c.faces().size() ) self.assertEqual(12,c.edges().size()) self.assertEqual(8,c.vertices().size() ) self.assertEqual(0,c.compounds().size()) #now any particular face should result in 4 edges and four vertices self.assertEqual(4,c.faces().first().edges().size() ) self.assertEqual(1,c.faces().first().size() ) self.assertEqual(4,c.faces().first().vertices().size() ) self.assertEqual(4,c.faces().last().edges().size() )
def testInverseSelector(self): c = CQ(makeUnitCube()) S = selectors.StringSyntaxSelector fl = c.faces(selectors.InverseSelector(S('>Z'))).vals() self.assertEqual(5, len(fl)) el = c.faces('>Z').edges(selectors.InverseSelector(S('>X'))).vals() self.assertEqual(3, len(el)) # test invert operator fl = c.faces(-S('>Z')).vals() self.assertEqual(5, len(fl)) el = c.faces('>Z').edges(-S('>X')).vals() self.assertEqual(3, len(el))
def testMinDistance(self): c = CQ(makeUnitCube()) #should select the topmost face self.assertEqual(1, c.faces("<Z").size()) self.assertEqual(4, c.faces("<Z").vertices().size()) #vertices should all be at z=1, if this is the top face self.assertEqual(4, len(c.faces("<Z").vertices().vals() )) for v in c.faces("<Z").vertices().vals(): 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 testMinDistance(self): c = CQ(makeUnitCube()) # should select the topmost face self.assertEqual(1, c.faces("<Z").size()) self.assertEqual(4, c.faces("<Z").vertices().size()) # vertices should all be at z=1, if this is the top face self.assertEqual(4, len(c.faces("<Z").vertices().vals())) for v in c.faces("<Z").vertices().vals(): 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 testSolid(self): c = CQ(makeUnitCube()) # make sure all the counts are right for a cube self.assertEqual(1, c.solids().size()) self.assertEqual(6, c.faces().size()) self.assertEqual(12, c.edges().size()) self.assertEqual(8, c.vertices().size()) self.assertEqual(0, c.compounds().size()) # now any particular face should result in 4 edges and four vertices self.assertEqual(4, c.faces().first().edges().size()) self.assertEqual(1, c.faces().first().size()) self.assertEqual(4, c.faces().first().vertices().size()) self.assertEqual(4, c.faces().last().edges().size())
def testSumSelector(self): c = CQ(makeUnitCube()) S = selectors.StringSyntaxSelector fl = c.faces(selectors.SumSelector(S(">Z"), S("<Z"))).vals() self.assertEqual(2, len(fl)) el = c.edges(selectors.SumSelector(S("|X"), S("|Y"))).vals() self.assertEqual(8, len(el)) # test the sum operator fl = c.faces(S(">Z") + S("<Z")).vals() self.assertEqual(2, len(fl)) el = c.edges(S("|X") + S("|Y")).vals() self.assertEqual(8, len(el))
def testInverseSelector(self): c = CQ(makeUnitCube()) S = selectors.StringSyntaxSelector fl = c.faces(selectors.InverseSelector(S('>Z'))).vals() self.assertEqual(5, len(fl)) el = c.faces('>Z').edges(selectors.InverseSelector(S('>X'))).vals() self.assertEqual(3, len(el)) # test invert operator fl = c.faces(-S('>Z')).vals() self.assertEqual(5, len(fl)) el = c.faces('>Z').edges(-S('>X')).vals() self.assertEqual(3, len(el))
def testSumSelector(self): c = CQ(makeUnitCube()) S = selectors.StringSyntaxSelector fl = c.faces(selectors.SumSelector(S(">Z"), S("<Z"))).vals() self.assertEqual(2, len(fl)) el = c.edges(selectors.SumSelector(S("|X"), S("|Y"))).vals() self.assertEqual(8, len(el)) # test the sum operator fl = c.faces(S(">Z") + S("<Z")).vals() self.assertEqual(2, len(fl)) el = c.edges(S("|X") + S("|Y")).vals() self.assertEqual(8, len(el))
def testNearestTo(self): c = CQ(makeUnitCube()) #nearest vertex to origin is (0,0,0) t = (0.1,0.1,0.1) v = c.vertices(selectors.NearestToPointSelector(t)).vals()[0] self.assertTupleAlmostEquals((0.0,0.0,0.0),(v.X,v.Y,v.Z),3) t = (0.1,0.1,0.2) #nearest edge is the vertical side edge, 0,0,0 -> 0,0,1 e = c.edges(selectors.NearestToPointSelector(t)).vals()[0] v = c.edges(selectors.NearestToPointSelector(t)).vertices().vals() self.assertEqual(2,len(v)) #nearest solid is myself s = c.solids(selectors.NearestToPointSelector(t)).vals() self.assertEqual(1,len(s))
def testSplitKeepingBoth(self): "Tests splitting a solid" #drill a hole in the side c = CQ( makeUnitCube()).faces(">Z").workplane().circle(0.25).cutThruAll() self.assertEqual(7, c.faces().size()) #now cut it in half sideways result = c.faces(">Y").workplane(-0.5).split(keepTop=True, keepBottom=True) #stack will have both halves, original will be unchanged self.assertEqual( 2, result.solids().size()) #two solids are on the stack, eac self.assertEqual(8, result.solids().item(0).faces().size()) self.assertEqual(8, result.solids().item(1).faces().size())
def testNearestTo(self): c = CQ(makeUnitCube()) # nearest vertex to origin is (0,0,0) t = (0.1, 0.1, 0.1) v = c.vertices(selectors.NearestToPointSelector(t)).vals()[0] self.assertTupleAlmostEquals((0.0, 0.0, 0.0), (v.X, v.Y, v.Z), 3) t = (0.1, 0.1, 0.2) # nearest edge is the vertical side edge, 0,0,0 -> 0,0,1 e = c.edges(selectors.NearestToPointSelector(t)).vals()[0] v = c.edges(selectors.NearestToPointSelector(t)).vertices().vals() self.assertEqual(2, len(v)) # nearest solid is myself s = c.solids(selectors.NearestToPointSelector(t)).vals() self.assertEqual(1, len(s))
def testAndSelector(self): c = CQ(makeUnitCube()) S = selectors.StringSyntaxSelector BS = selectors.BoxSelector el = c.edges( selectors.AndSelector(S("|X"), BS((-2, -2, 0.1), (2, 2, 2)))).vals() self.assertEqual(2, len(el)) # test 'and' (intersection) operator el = c.edges(S("|X") & BS((-2, -2, 0.1), (2, 2, 2))).vals() self.assertEqual(2, len(el)) # test using extended string syntax v = c.vertices(">X and >Y").vals() self.assertEqual(2, len(v))
def testInverseSelector(self): c = CQ(makeUnitCube()) S = selectors.StringSyntaxSelector fl = c.faces(selectors.InverseSelector(S(">Z"))).vals() self.assertEqual(5, len(fl)) el = c.faces(">Z").edges(selectors.InverseSelector(S(">X"))).vals() self.assertEqual(3, len(el)) # test invert operator fl = c.faces(-S(">Z")).vals() self.assertEqual(5, len(fl)) el = c.faces(">Z").edges(-S(">X")).vals() self.assertEqual(3, len(el)) # test using extended string syntax fl = c.faces("not >Z").vals() self.assertEqual(5, len(fl)) el = c.faces(">Z").edges("not >X").vals() self.assertEqual(3, len(el))
def testPointList(self): """ Tests adding points and using them """ c = CQ(makeUnitCube()) s = c.faces(">Z").workplane().pushPoints([(-0.3, 0.3), (0.3, 0.3), (0, 0)]) self.assertEqual(3, s.size()) #TODO: is the ability to iterate over points with circle really worth it? #maybe we should just require using all() and a loop for this. the semantics and #possible combinations got too hard ( ie, .circle().circle() ) was really odd body = s.circle(0.05).cutThruAll() self.saveModel(body) self.assertEqual(9, body.faces().size()) # Test the case when using eachpoint with only a blank workplane def callback_fn(pnt): self.assertEqual((0.0, 0.0), (pnt.x, pnt.y)) r = Workplane('XY') r.objects = [] r.eachpoint(callback_fn)
def testFaceCount(self): c = CQ(makeUnitCube()) self.assertEqual( 6, c.faces().size() ) self.assertEqual( 2, c.faces("|Z").size() )
def testBox(self): c = CQ(makeUnitCube()) # test vertice selection test_data_vertices = [ # box point0, box point1, selected vertice ((0.9, 0.9, 0.9), (1.1, 1.1, 1.1), (1.0, 1.0, 1.0)), ((-0.1, 0.9, 0.9), (0.9, 1.1, 1.1), (0.0, 1.0, 1.0)), ((-0.1, -0.1, 0.9), (0.1, 0.1, 1.1), (0.0, 0.0, 1.0)), ((-0.1, -0.1, -0.1), (0.1, 0.1, 0.1), (0.0, 0.0, 0.0)), ((0.9, -0.1, -0.1), (1.1, 0.1, 0.1), (1.0, 0.0, 0.0)), ((0.9, 0.9, -0.1), (1.1, 1.1, 0.1), (1.0, 1.0, 0.0)), ((-0.1, 0.9, -0.1), (0.1, 1.1, 0.1), (0.0, 1.0, 0.0)), ((0.9, -0.1, 0.9), (1.1, 0.1, 1.1), (1.0, 0.0, 1.0)) ] for d in test_data_vertices: vl = c.vertices(selectors.BoxSelector(d[0], d[1])).vals() self.assertEqual(1, len(vl)) v = vl[0] self.assertTupleAlmostEquals(d[2], (v.X, v.Y, v.Z), 3) # this time box points are swapped vl = c.vertices(selectors.BoxSelector(d[1], d[0])).vals() self.assertEqual(1, len(vl)) v = vl[0] self.assertTupleAlmostEquals(d[2], (v.X, v.Y, v.Z), 3) # test multiple vertices selection vl = c.vertices(selectors.BoxSelector((-0.1, -0.1, 0.9),(0.1, 1.1, 1.1))).vals() self.assertEqual(2, len(vl)) vl = c.vertices(selectors.BoxSelector((-0.1, -0.1, -0.1),(0.1, 1.1, 1.1))).vals() self.assertEqual(4, len(vl)) # test edge selection test_data_edges = [ # box point0, box point1, edge center ((0.4, -0.1, -0.1), (0.6, 0.1, 0.1), (0.5, 0.0, 0.0)), ((-0.1, -0.1, 0.4), (0.1, 0.1, 0.6), (0.0, 0.0, 0.5)), ((0.9, 0.9, 0.4), (1.1, 1.1, 0.6), (1.0, 1.0, 0.5)), ((0.4, 0.9, 0.9), (0.6, 1.1, 1.1,), (0.5, 1.0, 1.0)) ] for d in test_data_edges: el = c.edges(selectors.BoxSelector(d[0], d[1])).vals() self.assertEqual(1, len(el)) ec = el[0].Center() self.assertTupleAlmostEquals(d[2], (ec.x, ec.y, ec.z), 3) # test again by swapping box points el = c.edges(selectors.BoxSelector(d[1], d[0])).vals() self.assertEqual(1, len(el)) ec = el[0].Center() self.assertTupleAlmostEquals(d[2], (ec.x, ec.y, ec.z), 3) # test multiple edge selection el = c.edges(selectors.BoxSelector((-0.1, -0.1, -0.1), (0.6, 0.1, 0.6))).vals() self.assertEqual(2, len(el)) el = c.edges(selectors.BoxSelector((-0.1, -0.1, -0.1), (1.1, 0.1, 0.6))).vals() self.assertEqual(3, len(el)) # test face selection test_data_faces = [ # box point0, box point1, face center ((0.4, -0.1, 0.4), (0.6, 0.1, 0.6), (0.5, 0.0, 0.5)), ((0.9, 0.4, 0.4), (1.1, 0.6, 0.6), (1.0, 0.5, 0.5)), ((0.4, 0.4, 0.9), (0.6, 0.6, 1.1), (0.5, 0.5, 1.0)), ((0.4, 0.4, -0.1), (0.6, 0.6, 0.1), (0.5, 0.5, 0.0)) ] for d in test_data_faces: fl = c.faces(selectors.BoxSelector(d[0], d[1])).vals() self.assertEqual(1, len(fl)) fc = fl[0].Center() self.assertTupleAlmostEquals(d[2], (fc.x, fc.y, fc.z), 3) # test again by swapping box points fl = c.faces(selectors.BoxSelector(d[1], d[0])).vals() self.assertEqual(1, len(fl)) fc = fl[0].Center() self.assertTupleAlmostEquals(d[2], (fc.x, fc.y, fc.z), 3) # test multiple face selection fl = c.faces(selectors.BoxSelector((0.4, 0.4, 0.4), (0.6, 1.1, 1.1))).vals() self.assertEqual(2, len(fl)) fl = c.faces(selectors.BoxSelector((0.4, 0.4, 0.4), (1.1, 1.1, 1.1))).vals() self.assertEqual(3, len(fl)) # test boundingbox option el = c.edges(selectors.BoxSelector((-0.1, -0.1, -0.1), (1.1, 0.1, 0.6), True)).vals() self.assertEqual(1, len(el)) fl = c.faces(selectors.BoxSelector((0.4, 0.4, 0.4), (1.1, 1.1, 1.1), True)).vals() self.assertEqual(0, len(fl)) fl = c.faces(selectors.BoxSelector((-0.1, 0.4, -0.1), (1.1, 1.1, 1.1), True)).vals() self.assertEqual(1, len(fl))
def testFirst(self): c = CQ(makeUnitCube()) self.assertEqual(type(c.vertices().first().val()), Vertex) self.assertEqual(type(c.vertices().first().first().first().val()), Vertex)
def testAll(self): "all returns a list of CQ objects, so that you can iterate over them individually" c = CQ(makeUnitCube()) self.assertEqual(6, c.faces().size()) self.assertEqual(6, len(c.faces().all())) self.assertEqual(4, c.faces().all()[0].vertices().size())
def testAll(self): "all returns a list of CQ objects, so that you can iterate over them individually" c = CQ(makeUnitCube()) self.assertEqual(6,c.faces().size()) self.assertEqual(6,len(c.faces().all())) self.assertEqual(4,c.faces().all()[0].vertices().size() )
def testFaceCount(self): c = CQ(makeUnitCube()) self.assertEqual(6, c.faces().size()) self.assertEqual(2, c.faces("|Z").size())
def testComplexStringSelector(self): c = CQ(makeUnitCube()) v = c.vertices("(>X and >Y) or (<X and <Y)").vals() self.assertEqual(4, len(v))
def testComplexStringSelector(self): c = CQ(makeUnitCube()) v = c.vertices('(>X and >Y) or (<X and <Y)').vals() self.assertEqual(4, len(v))
def testWorkplaneOnExistingSolid(self): "Tests extruding on an existing solid" c = CQ( makeUnitCube()).faces(">Z").workplane().circle(0.25).circle(0.125).extrude(0.25) self.saveModel(c) self.assertEqual(10,c.faces().size() )
def testParallelEdgeFilter(self): c = CQ(makeUnitCube()) self.assertEqual(4, c.edges("|Z").size()) self.assertEqual(4, c.edges("|X").size()) self.assertEqual(4, c.edges("|Y").size())
def testFirst(self): c = CQ( makeUnitCube()) self.assertEqual(type(c.vertices().first().val()),Vertex) self.assertEqual(type(c.vertices().first().first().first().val()),Vertex)