1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import json
- import os
- import sys
- import time
- from abc import ABC, abstractmethod
- import ezdxf
- import requests
- import yaml
- from tqdm import tqdm
- from dict.model_config import _get_app_name, _global_model_configs
- class DrawerTemplate(ABC):
- def __init__(self, draw_type, model_id):
- self.draw_type = draw_type
- self.model_id = model_id
- self.dict_manager = None
- self.app_name = _get_app_name()
- self.doc = None
- self.msp = None
- self.global_config = _global_model_configs[draw_type]
- def request_data(self, model_id):
- url = self.global_config.request_url
- url = f'{url}?modelid={model_id}'
- with open('D:\workspace\py\\vent_DXF_2\\last_request.json', 'r', encoding='utf-8') as f:
- cad_json = json.load(f)
- # response = requests.get(url, timeout=10)
- # print(f"请求耗时{url} {response.elapsed.total_seconds()} 秒")
- # cad_json = {}
- # if response.status_code == 200:
- # cad_json = response.json() # 将响应内容解析为JSON格式
- # with open('save/last_request.json', 'w') as json_file:
- # json.dump(cad_json, json_file,ensure_ascii=False)
- # else:
- # print(f"请求失败,状态码:{response.status_code}")
- return cad_json
- def read_template(self):
- default_template_file = f"config/{self.app_name}/default_{self.draw_type}.dxf"
- if os.path.exists(default_template_file):
- self.doc = ezdxf.readfile(default_template_file)
- else:
- self.doc = ezdxf.new('R2013')
- self.doc.styles.add("msyh", font="data/msyh.ttc")
- self.msp = self.doc.modelspace()
- def save_as(self):
- path = f'save/{str(time.time())}.dxf'
- self.doc.saveas(path)
- print(f'保存文件{os.path.abspath(path)}')
- return os.path.abspath(path)
- def export(self):
- self.load_dict_manager()
- self.read_template()
- self.draw_cad()
- return self.save_as()
- def _draw_items(self, items, desc, drawer_class, style):
- """
- 通用方法:绘制 items
- :param items: 需要绘制的对象列表
- :param desc: tqdm 进度条描述
- :param drawer_class: 绘制器类(如 Tun2dDrawer, WindowDrawer 等)
- """
- for item in tqdm(items, desc=desc):
- drawer = drawer_class(item, self.msp, style)
- drawer.draw()
- @abstractmethod
- def load_dict_manager(self):
- pass
- @abstractmethod
- def draw_cad(self):
- pass
|