#!/usr/bin/env python # -*- coding: utf-8 -*- from pyautocad import Autocad, APoint from pyautocad.contrib.tables import Table acad = Autocad() p1 = APoint(0, 0) for i in range(5): obj = acad.model.AddText(u'Hi %s!' % i, p1, 2.5) p1.y += 10 table = Table() for obj in acad.iter_objects('Text'): x, y, z = obj.InsertionPoint table.writerow([obj.TextString, x, y, z]) table.save('data.xls', 'xls') data = Table.data_from_file('data.xls')
def main(): acad = Autocad() parser = optparse.OptionParser(usage=u'%prog [опции] [файл для результатов]') parser.add_option('-f', '--format', choices=available_write_formats(), dest='format', metavar='FORMAT', default='xls', help=u"Формат файла (%s) по умолчанию - %%default" % ', '.join(available_write_formats())) parser.add_option('-k', '--known', dest='known_targets', metavar='FILE', default='', action='store', help=u'Файл с заполненым полем "Конец" и очередностью ' u' записей. По умолчанию берется из существующего файла') parser.add_option('-q', '--quiet', action='callback', callback=lambda *x: logging.disable(logging.WARNING), help=u'"Тихий" режим (не печатать в консоль)') parser.add_option('-s', '--single', dest='single_doc', action='store_true', default=False, help=u'Собрать данные только из текущего документа ' u'(Собирает из всех по умолчанию)') parser.add_option('-c', '--no-known', dest='dont_use_known', action='store_true', default=False, help=u'Не использовать данные об очередности и поле "Конец"') options, args = parser.parse_args() output_file = args[0] if args else u"cables_from_%s.%s" % (acad.doc.Name, options.format) if not options.known_targets and not options.dont_use_known: options.known_targets = output_file known_targets = get_known_targets(options.known_targets) output_table = Table() if options.single_doc: documents = [acad.doc] else: documents = acad.app.Documents for doc in documents: try: cables = get_cables(acad, doc.Modelspace, known_targets) sorted_cables = sort_cables_by_targets(cables, known_targets) for row in sorted_cables: output_table.writerow([s for s in row]) except Exception: logger.exception('Error while processing %s', doc.Name) output_table.save(output_file, options.format)
def count_blocks_excel(filename, savingPath, uselayer0, layers_contain, blocks_contain): ''' This function iterates over all the layers in the opened DWG and summing up all the blocks in the file into one MS-Excel sheet. Parameters needed: 1. Name of an MS-Excel file (doesn't have to exist) 2. Should it count objects in Layer 0? 3. Should it count objects only in specific layers? ''' os.chdir(savingPath) tableFilename = filename + '.xls' table = Table() block_list = [] total_blocks = [] acad.prompt("Creating a table of blocks count") layer0counter = 0 for block in acad.iter_objects('block'): ''' This if statement checks if the layer is Layer0. Some people workflow includes leaving "garbage" in layer 0, and we don't want it to count these objects.''' if (uselayer0 == "no") & (unicodedata.normalize( 'NFKD', block.Layer).encode('ascii', 'ignore') == "0"): # print "block was on layer 0" layer0counter += 1 continue if layers_contain in block.Layer: # print block.Layer if blocks_contain in block.name: if block.name in block_list: i = block_list.index(block.name) total_blocks[i] += 1 else: block_list.append(block.name) total_blocks.append(1) print block_list print total_blocks if (uselayer0 == "no"): print str(layer0counter) + " blocks counted and ignored on layer 0" acad.prompt("Saving file AAC_blocks_" + filename + ".xls at " + savingPath) # Add headlines to table table.writerow([ "NADRASH LTD.", "Blocks Count", "Created:", today_date_designed, acad.ActiveDocument.Name ]) table.writerow([ "Block", "Amount", "", "", "Blocks counted only in layers contain: {}".format(layers_contain) ]) # Add data to table for i in range(len(block_list)): table.writerow([block_list[i], total_blocks[i], "", "", ""]) # Save table in xls table.save(tableFilename, 'xls')
def count_blocks_per_layer(filename, savingPath, uselayer0, layers_contain, blocks_contain): ''' This function iterates over all the layers in the opened DWG and summing up all the blocks in each layer into one MS-Excel sheet. Parameters needed: 1. Name of an MS-Excel file (doesn't have to exist) 2. Should it count objects in Layer 0? 3. Should it count objects only in specific layers? ''' os.chdir(savingPath) tableFilename = filename + '.xls' table = Table() block_list = [] block_name_list = [] block_layer = [] total_blocks = [] acad.prompt("Creating a table of blocks count") layer0counter = 0 for block in acad.iter_objects('block'): ''' This if statement checks if the layer is Layer0. Some people workflow includes leaving "garbage" in layer 0, and we don't want it to count these objects.''' if (uselayer0 == "no") & (unicodedata.normalize('NFKD', block.Layer).encode('ascii', 'ignore') == "0"): # print "block was on layer 0" layer0counter += 1 continue if layers_contain in block.Layer: # print block.Layer if blocks_contain in block.name: if block.Layer + " " + block.name in block_list: i = block_list.index(block.Layer + " " + block.name) total_blocks[i] += 1 else: block_list.append(block.Layer + " " + block.name) block_name_list.append(block.name) block_layer.append(block.Layer) total_blocks.append(1) print block_list print total_blocks if (uselayer0 == "no"): print str(layer0counter) + " blocks counted and ignored on layer 0" acad.prompt("Saving file AAC_blocks_per_layer" + filename + ".xls at " + savingPath) # Add headlines to table table.writerow(["NADRASH LTD.", "Blocks Count", "Created:", today_date_designed, acad.ActiveDocument.Name]) table.writerow(["Layer", "Block Name", "Amount", "", "Blocks counted only in layers contain: {}".format(layers_contain)]) # Add data to table for i in range(len(block_list)): table.writerow([block_layer[i], block_name_list[i], total_blocks[i], "", ""]) # Save table in xls table.save(tableFilename, 'xls')
def line_lengths_excel(filename, savingPath, draw_units, layers_contain): ''' This function iterates over all the layers in the opened DWG and write sum of line lengths of each layer into one MS-Excel sheet. Parameters needed: 1. Name of an MS-Excel file (doesn't have to exist) 2. Units of the drwaing ''' os.chdir(savingPath) acad.prompt("Creating a table of line lengths") tableFilename = filename + '.xls' table = Table() layers = [] total_length = [] units_scale = {"m": 1, "cm": 100, "mm": 1000} units = draw_units scale = units_scale[units] for line in acad.iter_objects('polyline'): l1 = line.length if layers_contain in line.Layer: # print line.Layer if line.Layer in layers: i = layers.index(line.Layer) total_length[i] += l1 else: layers.append(line.Layer) total_length.append(l1) print layers print total_length acad.prompt("Saving file AAC_lines_" + filename + ".xls at " + savingPath) # Add headlines to table table.writerow([ "NADRASH LTD.", "Lines Lengths", "Created:", today_date_designed, acad.ActiveDocument.Name ]) table.writerow(["Layer", "Length [" + units + "]", "Length [m]", "", ""]) # Add data to table for i in range(len(layers)): table.writerow( [layers[i], total_length[i], total_length[i] / scale, "", ""]) # Save table in xls table.save(tableFilename, 'xls')
def sum_areas_excel(filename, savingPath, draw_units, layers_contain): ''' This function iterates over all the layers in the opened DWG and write sum of areas in each layer into one MS-Excel sheet. Parameters needed: 1. Name of an MS-Excel file (doesn't have to exist) 2. Units of the drwaing ''' os.chdir(savingPath) acad.prompt("Creating a table of areas sum per layer") tableFilename = filename + '.xls' table = Table() layers = [] total_sum = [] units_scale = {"m": 1, "cm": 100, "mm": 1000} units = draw_units scale = units_scale[units] for object in acad.iter_objects(): obj1 = object.area if layers_contain in object.Layer: # print line.Layer if object.Layer in layers: i = layers.index(object.Layer) total_sum[i] += obj1 else: layers.append(object.Layer) total_sum.append(obj1) print layers print total_sum acad.prompt("Saving file AAC_areas_" + filename + ".xls at " + savingPath) # Add headlines to table table.writerow([ "NADRASH LTD.", "Area sum", "Created:", today_date_designed, acad.ActiveDocument.Name ]) table.writerow(["Layer", "[" + units + "^2]", "[m^2]", "", ""]) # Add data to table for i in range(len(layers)): table.writerow( [layers[i], total_sum[i], total_sum[i] / scale**2, "", ""]) # Save table in xls table.save(tableFilename, 'xls')
def sum_areas_excel(filename, savingPath, draw_units, layers_contain): ''' This function iterates over all the layers in the opened DWG and write sum of areas in each layer into one MS-Excel sheet. Parameters needed: 1. Name of an MS-Excel file (doesn't have to exist) 2. Units of the drwaing ''' os.chdir(savingPath) acad.prompt("Creating a table of areas sum per layer") tableFilename = filename + '.xls' table = Table() layers = [] total_sum = [] units_scale = {"m": 1, "cm": 100, "mm": 1000} units = draw_units scale = units_scale[units] for object in acad.iter_objects(): obj1 = object.area if layers_contain in object.Layer: # print line.Layer if object.Layer in layers: i = layers.index(object.Layer) total_sum[i] += obj1 else: layers.append(object.Layer) total_sum.append(obj1) print layers print total_sum acad.prompt("Saving file AAC_areas_" + filename + ".xls at " + savingPath) # Add headlines to table table.writerow(["NADRASH LTD.", "Area sum", "Created:", today_date_designed, acad.ActiveDocument.Name]) table.writerow(["Layer", "[" + units + "^2]", "[m^2]", "", ""]) # Add data to table for i in range(len(layers)): table.writerow([layers[i], total_sum[i], total_sum[i] / scale**2, "", ""]) # Save table in xls table.save(tableFilename, 'xls')
def line_lengths_excel(filename, savingPath, draw_units, layers_contain): ''' This function iterates over all the layers in the opened DWG and write sum of line lengths of each layer into one MS-Excel sheet. Parameters needed: 1. Name of an MS-Excel file (doesn't have to exist) 2. Units of the drwaing ''' os.chdir(savingPath) acad.prompt("Creating a table of line lengths") tableFilename = filename + '.xls' table = Table() layers = [] total_length = [] units_scale = {"m": 1, "cm": 100, "mm": 1000} units = draw_units scale = units_scale[units] for line in acad.iter_objects('polyline'): l1 = line.length if layers_contain in line.Layer: # print line.Layer if line.Layer in layers: i = layers.index(line.Layer) total_length[i] += l1 else: layers.append(line.Layer) total_length.append(l1) print layers print total_length acad.prompt("Saving file AAC_lines_" + filename + ".xls at " + savingPath) # Add headlines to table table.writerow(["NADRASH LTD.", "Lines Lengths", "Created:", today_date_designed, acad.ActiveDocument.Name]) table.writerow(["Layer", "Length [" + units + "]", "Length [m]", "", ""]) # Add data to table for i in range(len(layers)): table.writerow([layers[i], total_length[i], total_length[i] / scale, "", ""]) # Save table in xls table.save(tableFilename, 'xls')