def add_cables_list_to_autocad(block, data): insert_point = APoint(20, 0) distance = APoint(TABLE_WIDTH + TABLE_GAP, 0, 0) add_cables_table(block, data[:FIRST_TABLE_ROWS], APoint(20, 0)) for chunk in chunks(data[FIRST_TABLE_ROWS:], NEXT_TABLE_ROWS): insert_point += distance add_cables_table(block, chunk, insert_point) # TODO names of pivot tables margin = APoint(0, TEXT_HEIGHT, 0) insert_point += distance block.AddText(u'Сводная таблица длин кабелей', insert_point + margin, TEXT_HEIGHT) add_pivot_table(block, insert_point, list(calc_pivot_table(data))) insert_point += distance block.AddText(u'Сводная таблица кабельных разделок', insert_point + margin, TEXT_HEIGHT) pivot_dcount = list(calc_pivot_table(data, count_double_pivot)) add_pivot_table(block, insert_point, pivot_dcount) insert_point += distance block.AddText( u'ВНИМАНИЕ! У кабелей со сложным сечением (например 4х(5х70)' u' и т.п.) указано количество разделок', insert_point + margin * 4, TEXT_HEIGHT) block.AddText(u'Сводная таблица наконечников', insert_point + margin, TEXT_HEIGHT) add_pivot_table(block, insert_point, list(calc_pivot_tips(pivot_dcount)))
def test_multitext(self): model = self.acad.model text1 = 'Line1\nLine2\nLine3\\' text2 = 'Line1\\PLine2\\PLine3\\P' t1 = model.AddMText(APoint(0, 0, 0), 10, text1) t2 = model.AddMText(APoint(10, 10, 0), 10, text2) self.assertEqual(t1.TextString, text1) self.assertEqual(t2.TextString, text2)
def test_points_arguments(self): model = self.acad.model p1 = APoint(0, 0, 0) for i in range(10): model.AddCircle(p1 * i, i + 1) p1 += i for circle in self.acad.iter_objects('circle'): cp = APoint(circle.Center) model.AddCircle(-cp, circle.Radius)
def test_text(self): model = self.acad.model text1 = u'Русский текст' text2 = u'With paragraph \PYes' t1 = model.AddText(text1, APoint(0, 0, 0), 10) t2 = model.AddText(text2, APoint(10, 10, 0), 10) self.assertEqual(type(t1.TextString), unicode) self.assertEqual(t1.TextString, text1) self.assertEqual(t2.InsertionPoint, (10, 10, 0)) self.assertNotEqual(t2.InsertionPoint, (10, 10, 1))
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_types(self): model = self.acad.model p1 = APoint(0, 0, 0) p2 = APoint(10, 10, 0) p3 = tuple(p + 10 for p in p2) model.AddLine(p1, p2) model.AddLine(p2, APoint(p3)) lines = list(self.acad.iter_objects()) self.assertEqual(len(lines), 2) self.assertEqual(lines[0].StartPoint, p1) self.assertEqual(lines[0].EndPoint, p2) self.assertEqual(lines[1].StartPoint, p2) self.assertEqual(lines[1].EndPoint, p3)
def iter_drawings_names(acad, doc): num_layouts = doc.Layouts.Count - 2 for layout_number, layout in enumerate(acad.iter_layouts(doc)): utils.dynamic_print(' Layout %02d/%02d' % (layout_number, num_layouts)) # first we need to find our main stamp with name 'f4' block = acad.find_one('blockreference', layout.Block, lambda x: 'f4' in x.EffectiveName) if not block: continue block_pos = APoint(block.InsertionPoint) # approximate position of drawing name name_pos = block_pos + APoint(-90, 12) for mt in acad.iter_objects("mtext", layout.Block): if name_pos.distance_to(mt.InsertionPoint) < 5.0: text = mt.TextString yield text.replace(" \\P", " ").replace("\\P", " ") break print
def test_iter_objects(self): model = self.acad.model p1 = APoint(0, 0, 0) p2 = APoint(10, 10, 0) n_lines = 10 n_texts = 15 for i in range(n_lines): model.AddLine(p1, p2) for i in range(n_texts): model.AddMText(p2, 10, u'Dummy') lines_count = len(list(self.acad.iter_objects('Line'))) texts_count = len(list(self.acad.iter_objects('MText'))) all_count = len(list(self.acad.iter_objects(['MText', 'Line']))) self.assertEqual(lines_count, n_lines) self.assertEqual(texts_count, n_texts) self.assertEqual(all_count, n_lines + n_texts)
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 * p2, p1) self.assertEqual(p3 * p4, (4, 4, 4)) self.assertEqual(p3 / p4, p1) self.assertEqual(p1 + 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_find_objects(self): p1 = APoint(0, 0) model = self.acad.model for i in range(5): model.AddText(u'test %s' % i, p1, 2.5) def text_contains_3(text_obj): return '3' in text_obj.TextString text = self.acad.find_one('Text', predicate=text_contains_3) self.assertEqual(text.TextString, 'test 3')
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))
#!/usr/bin/env python # -*- coding: utf-8 -*- from pyzwcad.pyautocad import Autocad, APoint acad = Autocad(create_if_not_exists=True) acad.prompt("Hello, Autocad from Python\n") print(acad.doc.Name) p1 = APoint(0, 0) p2 = APoint(50, 25) for i in range(5): text = acad.model.AddText(u'Hi %s!' % i, p1, 2.5) acad.model.AddLine(p1, p2) acad.model.AddCircle(p1, 10) p1.y += 10 for obj in acad.iter_objects(): print(obj.ObjectName) for text in acad.iter_objects('Text'): print(text.TextString, text.InsertionPoint) for obj in acad.iter_objects(['Text', 'Line']): print(obj.ObjectName) def text_contains_3(text_obj): return '3' in text_obj.TextString text = acad.find_one('Text', predicate=text_contains_3)
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))