def __init__(self, start_point, end_point): if isinstance(start_point, APoint): self.start = start_point else: self.start = APoint(*start_point) if isinstance(end_point, APoint): self.end = end_point else: self.end = APoint(*end_point)
def test_args(self): wrong_args = ['123', (1, 2), [1, 2, 3, 4]] for arg in wrong_args: with self.assertRaises(TypeError): p = APoint(arg) p = APoint(0, 0, 0) for arg in wrong_args: try: p += arg self.fail('Wrong arg passed') except Exception: pass
def test_point_ops(self): p1 = APoint(1, 1, 1) p2 = APoint(1, 1, 1) p3 = APoint(2, 2, 2) p4 = APoint(2, 2, 2) self.assertEqual(p1 + p2, (2, 2, 2)) self.assertEqual(p1 - p3, (-1, -1, -1)) self.assertEqual(p1 + [1, 1, 1], (2, 2, 2)) self.assertEqual(p2 * 4, p3 * 2) self.assertEqual(p3 * 10, (20, 20, 20)) self.assertEqual(p3 / 2, p1) self.assertEqual(p1 / 0.5, p3) self.assertEqual(-p1, (-1, -1, -1))
def test_ALine(self): l1 = ALine([10, 10], [20, 20]) self.assertEqual(str(l1), 'Aline(APoint(10.00, 10.00, 0.00), APoint(20.00, 20.00, 0.00))') # create Line by Point and Vector p1 = APoint(1, 0, 0) l2 = ALine.create_from_vector(Vector([3, 4, 0]), p1) l3 = ALine(p1, p1 + [0.6, 0.8, 0]) self.assertEqual(l2, l3)
def append(self, pnt_or_list): """Add node into polyline, node need to be `APoint` or (x, y)/(x, y, z) `OCS` coordinate (z coordinate will be ignored) :param pnt_or_list: Node :class: `APoint`, `list` or `tuple` """ if pnt_or_list is None: return if isinstance(pnt_or_list, APoint): self.points.append(pnt_or_list) if (isinstance(pnt_or_list, list) or isinstance(pnt_or_list, tuple)) \ and len(pnt_or_list) >= 2: self.points.append(APoint(pnt_or_list))
class ALine(object): """3D Line work with APoint and support in draw in `AutoCAD` Usage:: >>> l1 = ALine([10, 10], [20, 20]) Aline(APoint(10.00, 10.00, 0.00), APoint(20.00, 20.00, 0.00)) """ def __init__(self, start_point, end_point): if isinstance(start_point, APoint): self.start = start_point else: self.start = APoint(*start_point) if isinstance(end_point, APoint): self.end = end_point else: self.end = APoint(*end_point) @property def length(self): """The length of 3D line""" return self.start.distance_to(self.end) @property def middle(self): """The middle point of 3D line""" return APoint((self.start.x + self.end.x) / 2, (self.start.y + self.end.y) / 2, (self.start.z + self.end.z) / 2) @staticmethod def create_from_vector(v, pnt): """ """ if v is None or not isinstance(v, Vector) or pnt is None: return None v = v.normalized() # TODO: Change into APoint + Vector return ALine(pnt, pnt + v) def __str__(self): return 'Aline(%s, %s)' % (self.start, self.end) def __eq__(self, v): return isinstance(v, ALine) and self.start == v.start and self.end == v.end
def test_point_iops(self): p1 = APoint(1, 1, 1) p2 = APoint(2, 2, 2) p3 = APoint(3, 3, 3) p1 += 2 p2 += p3 self.assertEqual(p1, p3) self.assertEqual(p2, (5, 5, 5)) p1 = APoint(1, 1, 1) p2 = APoint(2, 2, 2) p1 -= 2 p2 -= p3 self.assertEqual(p1, (-1, -1, -1)) self.assertEqual(p2, (-1, -1, -1)) p = APoint(5, 5, 5) p /= 2.0 self.assertEqual(p, (2.5, 2.5, 2.5)) p *= 2 self.assertEqual(p, (5, 5, 5))
def test_distance(self): p1 = APoint(10, 10, 10) p2 = APoint(15, 15, 15) self.assertAlmostEqual(p1.distance_to(p2), 8.660254037844387) self.assertEqual(distance(p1, p2), distance(p2, p1))
def test_attributes(self): p1 = APoint(1, 1, 1) p1.x += 1 p1.y += 1 p1.z += 1 self.assertEqual(p1, (2, 2, 2))
def middle(self): """The middle point of 3D line""" return APoint((self.start.x + self.end.x) / 2, (self.start.y + self.end.y) / 2, (self.start.z + self.end.z) / 2)
def test_create_block(self): acad = self.cad p1 = APoint(100, 0) block = acad.create_block(p1, 'create') block.AddCircle(p1, 10) acad.model.InsertBlock(p1, 'create', 1, 1, 1, 0)
def test_vector_point_operator(self): p1 = APoint(1, 2, 1) v1 = Vector([1, 0, 0]) self.assertEqual(p1 + v1, [2, 2, 1])
def test_point_operator(self): p1 = APoint(1, 2, 2) p2 = [1, 0, 0] + p1 self.assertEqual(p2, [2, 2, 2])