drawer_template.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import json
  2. import os
  3. import sys
  4. import time
  5. from abc import ABC, abstractmethod
  6. import ezdxf
  7. import requests
  8. import yaml
  9. from tqdm import tqdm
  10. from dict.model_config import _get_app_name, _global_model_configs
  11. class DrawerTemplate(ABC):
  12. def __init__(self, draw_type, model_id):
  13. self.draw_type = draw_type
  14. self.model_id = model_id
  15. self.dict_manager = None
  16. self.app_name = _get_app_name()
  17. self.doc = None
  18. self.msp = None
  19. self.global_config = _global_model_configs[draw_type]
  20. def request_data(self, model_id):
  21. url = self.global_config.request_url
  22. url = f'{url}?modelid={model_id}'
  23. with open('D:\workspace\py\\vent_DXF_2\\last_request.json', 'r', encoding='utf-8') as f:
  24. cad_json = json.load(f)
  25. # response = requests.get(url, timeout=10)
  26. # print(f"请求耗时{url} {response.elapsed.total_seconds()} 秒")
  27. # cad_json = {}
  28. # if response.status_code == 200:
  29. # cad_json = response.json() # 将响应内容解析为JSON格式
  30. # with open('save/last_request.json', 'w') as json_file:
  31. # json.dump(cad_json, json_file,ensure_ascii=False)
  32. # else:
  33. # print(f"请求失败,状态码:{response.status_code}")
  34. return cad_json
  35. def read_template(self):
  36. default_template_file = f"config/{self.app_name}/default_{self.draw_type}.dxf"
  37. if os.path.exists(default_template_file):
  38. self.doc = ezdxf.readfile(default_template_file)
  39. else:
  40. self.doc = ezdxf.new('R2013')
  41. self.doc.styles.add("msyh", font="data/msyh.ttc")
  42. self.msp = self.doc.modelspace()
  43. def save_as(self):
  44. path = f'save/{str(time.time())}.dxf'
  45. self.doc.saveas(path)
  46. print(f'保存文件{os.path.abspath(path)}')
  47. return os.path.abspath(path)
  48. def export(self):
  49. self.load_dict_manager()
  50. self.read_template()
  51. self.draw_cad()
  52. return self.save_as()
  53. def _draw_items(self, items, desc, drawer_class, style):
  54. """
  55. 通用方法:绘制 items
  56. :param items: 需要绘制的对象列表
  57. :param desc: tqdm 进度条描述
  58. :param drawer_class: 绘制器类(如 Tun2dDrawer, WindowDrawer 等)
  59. """
  60. for item in tqdm(items, desc=desc):
  61. drawer = drawer_class(item, self.msp, style)
  62. drawer.draw()
  63. @abstractmethod
  64. def load_dict_manager(self):
  65. pass
  66. @abstractmethod
  67. def draw_cad(self):
  68. pass