diff --git a/cad.py b/cad.py new file mode 100644 index 0000000..8779e05 --- /dev/null +++ b/cad.py @@ -0,0 +1,24 @@ +from typing import List +import os +import subprocess + + +def convert_dxf_to_dwg(dxf_files: List[str]): + cad_file_path = r"D:/Program Files/Cad2022/AutoCAD 2022/acad.exe" + first_dwg_file = dxf_files[0] + dir_path = os.path.dirname(first_dwg_file) + script_path = f"{dir_path}/batch.scr" + with open(script_path, "w", encoding="ansi") as f: + for dxf in dxf_files: + f.write("FILEDIA 0\n") + f.write(f'_open "{dxf}"\n') + file_name_path, _ = os.path.splitext(dxf) + new_dwg_name = f'{file_name_path}.dwg' + if os.path.exists(new_dwg_name): + os.remove(new_dwg_name) + f.write(f'saveas 2004 "{new_dwg_name}"\n') + f.write("FILEDIA 1\n") + f.write(f"_close\n") + f.write("_exit ") + cmd = rf'"{cad_file_path}" /b "{script_path}"' + subprocess.Popen(cmd) diff --git a/dem.py b/dem.py index 898ea6d..159ef1f 100644 --- a/dem.py +++ b/dem.py @@ -9,6 +9,7 @@ import pandas as pd from pw import DFile, ControlFile import dem_utils from nwed import Nwed +import cad class Dem: @@ -48,6 +49,7 @@ class Dem: def write(self): # TODO:不应该设置缩放因数 + dxfs=[] zoom_factor = 1 excel_pfs = self._read_path_file() segments = [] @@ -178,11 +180,13 @@ class Dem: ) from ezdxf.addons.odafc import export_dwg - export_dwg( - dm_doc, - f"{out_dxf_file_dir}/D{100+int(start_point_name[1:])}.dwg", - replace=True, - ) # 写断面文件 + # export_dwg( + # dm_doc, + # f"{out_dxf_file_dir}/D{100+int(start_point_name[1:])}.dwg", + # replace=True, + # ) # 写断面文件 + dm_doc.saveas(f"{out_dxf_file_dir}/D{100+int(start_point_name[1:])}.dxf",) + dxfs.append(f"{out_dxf_file_dir}/D{100+int(start_point_name[1:])}.dxf") # 写Z文件 z_file_path = f"{out_dxf_file_dir}/ZD{100+int(start_point_name[1:])}" with open(z_file_path, "w") as z_file: @@ -266,13 +270,18 @@ class Dem: f"{out_dxf_file_dir}/{100+int(start_point_name[1:])}.nwed", ) # 写整个断面文件 - export_dwg( - dm_whole_doc, - f"{out_dxf_file_dir}/DA.dwg", - replace=True, - ) + # export_dwg( + # dm_whole_doc, + # f"{out_dxf_file_dir}/DA.dwg", + # replace=True, + # ) + dm_whole_doc.saveas(f"{out_dxf_file_dir}/DA.dxf") + # cad.convert_dxf_to_dwg([f"{out_dxf_file_dir}/DA.dxf"]) + dxfs.append(f"{out_dxf_file_dir}/DA.dxf") # 写平面文件 plate_doc.saveas(f"{out_dxf_file_dir}/plate.dxf") + dxfs.append(f"{out_dxf_file_dir}/plate.dxf") + cad.convert_dxf_to_dwg(dxf_files=dxfs) def get_elevation(self, site_x_y): """Get the elevation of given locations from DEM in GCS.