def test_world_coords(self):
     cs1 = CoordSystem(origin=(1, 2, 3))
     cs2 = CoordSystem(origin=(1, 1, 1))
     box = Box()
     box.world_coords = cs2
     m = Mate(box, cs1)
     self.assertEqual(m.world_coords, cs1 + cs2)
 def test_basic(self):
     mate1 = Mate(Box())
     mate2 = Mate(Box())
     c = Coincident(mate1, mate2)
     # assert composition
     self.assertEqual(id(mate1), id(c.mate))
     self.assertEqual(id(mate2), id(c.to_mate))
Beispiel #3
0
 def make_components(self):
     base = Box(length=20, width=20, height=12)
     top = Box(length=18, width=18, height=18)
     return {
         'base': base,
         'top': top,
         'fastener': ScrewFastener(parts=[base, top]),
     }
 def test_mate(self):
     mate = Mate(Box())
     cs1 = CoordSystem(origin=(1, 1, 1))
     box = Box()
     box.world_coords = cs1
     cs2 = CoordSystem(origin=(2, 3, 4))
     coords = Mate(box, cs2)
     c = Fixed(mate, coords)  # given world_coords is from a mate
     self.assertEqual(cs1 + cs2, c.world_coords)
    def test_solution(self):
        (box1, box2) = (Box(), Box())
        # set box1 world location: sit it on top of xy plane
        box1.world_coords = CoordSystem((0, 0, box1.height / 2))

        # +'ve rotation
        c = Coincident(box2.mate_origin, box1.mate_top)
        solution = list(solver([c]))
        self.assertEqual(len(solution), 1)
        (part, coords) = solution[0]
        self.assertEqual(id(part), id(box2))
        self.assertEqual(coords, CoordSystem((0, 0, box1.height)))
    def test_basic(self):
        mate = Mate(Box())
        cs = CoordSystem()

        c = Fixed(mate, cs)
        # assert composition
        self.assertEqual(id(mate), id(c.mate))
        self.assertEqual(cs, c.world_coords)
    def test_origin(self):
        box = Box()
        c = Fixed(Mate(box), CoordSystem())

        # default origin (0, 0, 0)
        (part, coords) = list(solver([c]))[0]
        self.assertEqual(coords, CoordSystem((0, 0, 0)))

        # origin displaced
        (part, coords) = list(solver([c], CoordSystem(origin=(1, 2, 3))))[0]
        self.assertEqual(coords, CoordSystem((1, 2, 3)))
    def test_coincident_backward(self):
        (box1, box2) = (Box(), Box())
        constraints = [  # stack box2 on box1 (in reversed logical order)
            Coincident(box2.mate_bottom, box1.mate_top),
            Fixed(box1.mate_bottom, CoordSystem()),
        ]
        solution = solver(constraints)

        # 1st solution : box1
        (part, coords) = next(solution)
        self.assertEqual(id(part), id(box1))

        part.world_coords = coords

        # 2nd solution : box2
        (part, coords) = next(solution)
        self.assertEqual(id(part), id(box2))

        with self.assertRaises(StopIteration):
            next(solution)
    def test_rotation(self):
        (box1, box2) = (Box(), Box())
        # set box1 world location: sit it on top of xy plane
        box1.world_coords = CoordSystem((0, 0, box1.height / 2))

        # +'ve rotation
        c = Coincident(box2.mate_origin,
                       box1.mate_top + CoordSystem(xDir=(1, 0.1, 0)))
        (part, coords) = list(solver([c]))[0]
        self.assertEqual(coords.origin, cadquery.Vector(0, 0, box1.height))
        self.assertEqual(coords.xDir, cadquery.Vector(1, 0.1, 0).normalized())
        self.assertEqual(coords.zDir, cadquery.Vector(0, 0, 1))

        # -'ve rotation
        c = Coincident(box2.mate_origin + CoordSystem(xDir=(1, 0.1, 0)),
                       box1.mate_top)
        (part, coords) = list(solver([c]))[0]
        self.assertEqual(coords.origin, cadquery.Vector(0, 0, box1.height))
        self.assertEqual(coords.xDir, cadquery.Vector(1, -0.1, 0).normalized())
        self.assertEqual(coords.zDir, cadquery.Vector(0, 0, 1))
    def test_translation(self):
        box = Box()

        # +'ve translation
        c = Fixed(Mate(box, CoordSystem()), CoordSystem(origin=(1, 2, 3)))
        (part, coords) = list(solver([c]))[0]
        self.assertEqual(coords.origin, cadquery.Vector(1, 2, 3))
        self.assertEqual(coords.xDir, cadquery.Vector(1, 0, 0))
        self.assertEqual(coords.zDir, cadquery.Vector(0, 0, 1))

        # -'ve translation
        c = Fixed(Mate(box, CoordSystem(origin=(1, 2, 3))), CoordSystem())
        (part, coords) = list(solver([c]))[0]
        self.assertEqual(coords.origin, cadquery.Vector(-1, -2, -3))
        self.assertEqual(coords.xDir, cadquery.Vector(1, 0, 0))
        self.assertEqual(coords.zDir, cadquery.Vector(0, 0, 1))
    def test_rotation(self):
        box = Box()

        # +'ve rotation
        c = Fixed(Mate(box, CoordSystem()), CoordSystem(xDir=(1, 0.1, 0)))
        (part, coords) = list(solver([c]))[0]
        self.assertEqual(coords.origin, cadquery.Vector())
        self.assertEqual(coords.xDir, cadquery.Vector(1, 0.1, 0).normalized())
        self.assertEqual(coords.zDir, cadquery.Vector(0, 0, 1))

        # -'ve rotation
        c = Fixed(Mate(box, CoordSystem(xDir=(1, 0.1, 0))), CoordSystem())
        (part, coords) = list(solver([c]))[0]
        self.assertEqual(coords.origin, cadquery.Vector())
        self.assertEqual(coords.xDir, cadquery.Vector(1, -0.1, 0).normalized())
        self.assertEqual(coords.zDir, cadquery.Vector(0, 0, 1))
    def test_random(self):
        box = Box()

        for (s1, s2) in [(1, 2), (3, 4), (5, 6), (7, 8)]:
            cs1 = CoordSystem.random(seed=s1)
            cs2 = CoordSystem.random(seed=s2)

            # create constraint
            c = Fixed(Mate(box, cs1), cs2)
            # solve
            solution = list(solver([c]))
            # assert results
            self.assertEqual(len(solution), 1)
            (part, coords) = solution[0]
            self.assertEqual(id(part), id(box))
            self.assertEqual(coords, cs2 + (CoordSystem() - cs1))
    def test_init(self):
        obj = Box()
        cs = CoordSystem()

        # normal
        m = Mate(obj, cs)
        self.assertEqual(id(m.component), id(obj))
        self.assertEqual(id(m.local_coords), id(cs))

        # no component
        m = Mate(None, cs)
        self.assertIsNone(m.component)
        self.assertEqual(id(m.local_coords), id(cs))

        # no coords
        m = Mate(obj)
        self.assertEqual(id(m.component), id(obj))
        self.assertEqual(m.local_coords, CoordSystem())
 def test_bad_to_mate(self):
     self.assertRaises(TypeError, Coincident, Mate(Box()), 1)
 def test_bad_coords(self):
     self.assertRaises(TypeError, Fixed, Mate(Box()), 1)
 def test_repr(self):
     "%r" % Mate(Box(), CoordSystem())
 def test_add_badtype(self):
     m = Mate(Box())
     with self.assertRaises(TypeError):
         m + 100
 def test_add_coordsys(self):
     cs1 = CoordSystem(origin=(1, 2, 3))
     cs2 = CoordSystem(origin=(0, 2, 4))
     m1 = Mate(Box(), cs1)
     m2 = m1 + cs2
     self.assertEqual(m2.local_coords, cs1 + cs2)
 def test_world_coords_badcmp(self):
     cs = CoordSystem(origin=(1, 2, 3))
     box = Box()
     m = Mate(box, cs)
     with self.assertRaises(ValueError):
         m.world_coords
 def test_bad_coords(self):
     self.assertRaises(TypeError, Mate, Box(), 123)
 def test_no_solution(self):
     (box1, box2) = (Box(), Box())  # neither have world_coords
     c = Coincident(box2.mate_origin, box1.mate_top)
     self.assertIsNone(box1.world_coords)  # test criteria
     with self.assertRaises(ValueError):
         list(solver([c]))
 def test_default(self):
     c = Fixed(Mate(Box()))
     self.assertEqual(c.world_coords, CoordSystem())