mehrere oft verwendete Routinen in eigene utils.py eingebaut

This commit is contained in:
2026-01-23 10:23:18 +01:00
parent 37208f61b1
commit 3deebb81f3
9 changed files with 453 additions and 175 deletions
+9 -56
View File
@@ -12,6 +12,15 @@ from shapely.geometry import Point
from ezdxf.lldxf.const import DXFStructureError
from error_collector import ErrorCollector, write_json_file
from utils import (
check_environment_var,
check_file_in_work,
dxf_is_binary,
get_dxf_file,
merge_two_dicts,
to_json,
write_results,
)
"""
@@ -24,21 +33,6 @@ Dieses Programm:
"""
def write_results(jsn_results: str, out_dir: Path, filename: str) -> None:
"""Write results to a JSON file."""
print("writing results to a json file ...")
outfile = os.path.join(out_dir, filename)
with open(outfile, 'w', encoding='utf-8') as fh:
fh.write(jsn_results)
print("done")
def merge_two_dicts(x: dict, y: dict) -> dict:
z = x.copy()
z.update(y)
return z
def get_type_of_name_cfg(name: str) -> str:
prefix = name[:2]
@@ -733,34 +727,6 @@ def scan(dxf_source) -> dict:
ret['used_blocks'] = used_block_names
ret['all_blocks'] = alle_block_defs
return ret
def to_json(d: object, pretty: bool = True) -> str:
return json.dumps(d, indent=2 if pretty else None, ensure_ascii=False, default=str) #ensure_ascii false für darstellung von "ue"
def get_dxf_file(filepath: Path):
"""Hole das dxf file."""
try:
print("reading file ..", end='')
doc = ezdxf.filemanagement.readfile(filepath)
print("done")
except IOError:
print("Not a DXF file or a generic I/O error.")
sys.exit(1)
except DXFStructureError:
print("Invalid or corrupted DXF file.")
sys.exit(2)
return doc
def check_file_in_work(work_dir: Path, filename: Path) -> tuple[Path, bool]:
fexists = True
if not filename.exists():
mypath = work_dir.joinpath(filename)
ex = mypath.exists()
if not mypath.exists():
fexists = False
else:
mypath = filename
return mypath, fexists
def check_rack_z_coordinates(res_racks: dict, error_collector, config) -> None:
"""
@@ -829,11 +795,6 @@ def check_existance(res_mappings: dict, res_dist: dict, res_pos: dict, res_tunne
ret["overdefined_tunnel"] = list()
ret["overdefined_tunnel"].append(tname)
return ret
def dxf_is_binary(dxf_path: Path) -> bool:
with open(dxf_path, 'rb') as f:
header = f.read(22)
return b'AutoCAD Binary DXF' in header
def validate_configs() -> None:
errors = []
@@ -869,14 +830,6 @@ def validate_configs() -> None:
else:
print("No inconsistencies found. Continuing with routing process.")
def check_environment_var(env_str: str) -> Path:
out_path = os.environ.get(env_str)
if out_path:
return Path(out_path)
else:
print(f"Umgebungsvariable {env_str} ist nicht gesetzt oder leer.")
exit()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='fetches the x/y positions from a dxf file', prog='getpositions')
parser.add_argument('-f', '--filename', action='store', required=True, default="ST_6300_Steuerungstestlayout1_neueBloecke.dwg", help='which file should be fetched', metavar='myfile.dxf')