def importActionTrigger(self): file_path, ext = QFileDialog.getOpenFileName(filter="*.dxf") if file_path:#如果选择了文件 if ext != '*.dxf': QMessageBox.warning(None, "文件格式错误","只能导入.dxf格式的cad文件", QMessageBox.Yes) try: dxfReader = DxfReader(file_path) sections = dxfReader.ParseSections() layers = []#解析出的图层数据 shapes = []#解析出的图元数据 for section in sections: if isinstance(section, TablesSection):#先解析出图层 tables = section.ParseTables(dxfTable.LAYER) for table in tables: entries = table.ParseEntries() for entry in entries: layers.append(entry.parse()) elif isinstance(section, EntitiesSection): entities = section.ParseEntities(None)#解析出所有的图元 for entity in entities: shapes.append(entity.parse()) boards = [] for layer in layers: boards.append(ExInterFace.addBorad(layer["name"])) paintShape = ExInterFace.getPlugin('PaintShape') for shape in shapes: for board in boards: if board.name == shape['layer']: paintShape.addShape(board, MainPlugin.ShapeFromObject(shape)) break except Exception as e: QMessageBox.critical(None, "文件解析出错", str(e),QMessageBox.Yes) print("异常:"+str(e))
#-*- encoding:utf-8 -*- import sys from PyQt5.QtWidgets import QApplication import PyQt5.sip from business import MainWindow, ExInterFace if __name__ == '__main__': app = QApplication(sys.argv) mw = MainWindow() ExInterFace.init(mw) ExInterFace.addBorad("new") mw.showMaximized() sys.exit(app.exec())