def main(): acad = Autocad() print u'Примерный подсчет занимаемого места' print '%-20s| %s' % (u'Имя щита', u'Общее число модулей') for layout in acad.iter_layouts(): table = acad.find_one('table', layout.Block, lambda x: x.Columns == 5) if not table: continue total_modules = 0 row = -1 while row < table.Rows: row += 1 item_str = mtext_to_string(table.GetText(row, 2)) item_str = item_str.replace(u'четырехполюсный', u'4-х')\ .replace(u'трехполюсный', u'3-х')\ .replace(u'двухполюсный', u'2-х')\ .replace(u'однополюсный', u'1-но') m = re.match(ur'.*(\d)-([xх]|но).*', item_str) if m: n_modules = int(m.group(1)) quantity = int(mtext_to_string(table.GetText(row, 3))) row += 1 # skip next row else: m = re.match(ur'(\d)[PР].*', item_str) if not m: continue n_modules = int(m.group(1)) quantity = int(mtext_to_string(table.GetText(row - 1, 3))) total_modules += n_modules * quantity print '%-20s| %s' % (layout.Name, total_modules)
def main(): acad = Autocad() layout = acad.doc.ActiveLayout table = acad.find_one('table', layout.Block) if not table: return print_table_info(table, 3)
class ApiTestCase(unittest.TestCase): def setUp(self): self.acad = Autocad(True) self.doc = self.acad.app.Documents.Add() print 'Current', self.doc.Name def tearDown(self): self.doc.Close(False) pass 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_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) with self.assertRaises(COMError): model.AddLine(aDouble(0, 0), APoint(0, 0, 0)) 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_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_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_find_objects(self): p1 = APoint(0, 0) model = self.acad.model for i in range(5): text = 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')
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) print text.TextString from pyautocad import ACAD for text in acad.iter_objects('Text'): old_insertion_point = APoint(text.InsertionPoint) text.Alignment = ACAD.acAlignmentRight text.TextAlignmentPoint = old_insertion_point for line in acad.iter_objects('Line'): p1 = APoint(line.StartPoint) line.EndPoint = p1 - APoint(20, 0)
''' 按类型查找出所有某种图元(如所有Text对象) ''' for _text in acad.iter_objects('Text'): print(_text.TextString, _text.InsertionPoint) ''' 在类型选择时填入多种类型 ''' for _obj2 in acad.iter_objects(['tExt', 'Line']): # 按照类型查找可以混淆大小写,也可以只输入类型的一部分, # 比如查找”te”类型就可以自动匹配到text类型,输入”li“就自动匹配到Ellipse和Line类型 print(_obj2.ObjectName) def text_contains_3(text_obj): """ 查找符合条件的第一个对象. 查找第一个text item包含3的text """ return '3' in text_obj.TextString text2 = acad.find_one('Text', predicate=text_contains_3) print(text2.TextString) # 在文档中修改对象, 需要找到 interesting objects 并改变其属性. # 一些属性被描述为 constants, 例如 text alignment.这些 constants 可通过 ACAD. 来访问 ''' 改变所有text objects的text alignment ''' for text in acad.iter_objects('Text'): # text.InsertionPoint 转 APoint, 存储text的插入点 # 当 setting properties时, 不能直接使用tuple, 例如 text.TextAlignmentPoint. old_insertion_point = APoint(text.InsertionPoint) text.Alignment = ACAD.acAlignmentRight # 对齐方式调整为 右对齐 text.TextAlignmentPoint = old_insertion_point # 恢复插入点位置 # 改变对象位置要用 APoint, ''' 改变line端点位置 ''' for line in acad.iter_objects('Line'):