Files
kabellaengen/lib/create_numbers.py
T

112 lines
3.4 KiB
Python

import argparse
import configparser
import json
import os
import re
import sys
from pathlib import Path
import ezdxf
from ezdxf.addons import iterdxf
from shapely.geometry import Point
from ezdxf.lldxf.const import DXFStructureError
from error_collector import ErrorCollector, write_json_file
"""
Dieses Programm liest eine dxf Datei und holt sich vom Layer RENAME die Angaben
für die Bereiche in der Anlage, welche Motoren, Sensoren und Aktoren welchem
Unterverteiler zugeordnet werden müssen. (Attribut KENNZEICHNUNG)
Alle Symbol Templates werden entsprechend der Richtungsangaben in DIRECTION nummeriert
DIRECTION kann TOP_BOTTOM, BOTTOM_TOP, LEFT_RIGHT oder RIGHT_LEFT sein
Ein Renamer Symbol enthält z.B. die folgenden Angaben:
für POT-RA, POT-MA und POT-UC:
NAME1=POT-RA@@
NAME2=POT-MA@@
NAME3=POT-UC@@
KENNZEICHNUNG=A01+UC…
LAYER_NAME1=ILS_POT-RA
LAYER_NAME2=ILS_POT-MA
LAYER_NAME3=ILS_POT-UC
DIRECTION = LEFT_RIGHT
für BG, MB:
NAME1=BG-1@@@
NAME2=MB-1@@@
NAME3=
KENNZEICHNUNG=
LAYER_NAME1=ILS_Eingang
LAYER_NAME2=ILS_Ausgang
LAYER_NAME3=
DIRECTION = LEFT_RIGHT
oder nur zur Nummerierung der MA Symbole
NAME=MA-1@@
KENNZEICHNUNG=A01+UH00
LAYER_NAME=ILS_MOTOR
DIRECTION = TOP_BOTTOM/LEFT_RIGHT
Diese Symbole auf dem RENAMER Layer sind immer in einem Block mit einer Polylinie oder einen REchteck.
Alle Sensoren, Motoren, Aktoren etc. innerhalb der zugehörigen Polylinie nehmen an der Nummerierung teil, falls diese das @ Zeichen enthalten, also noch ein Template sind.
"""
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_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')
parser.add_argument('-w', '--write', action='store', help='write results into a json file')
args = parser.parse_args()
out_dir = check_environment_var('PROJECT_DATA')
work_dir = check_environment_var('PROJECT_WORK')
config_dir = check_environment_var("PROJECT_CFG")
filename = Path(args.filename)
if not filename.suffix == ".dxf":
print("only available for .dxf files")
exit()
(dxf_path, dexists) = check_file_in_work(work_dir, filename)
if dexists == False:
print("no such file ")
parser.print_help()
exit()
if dxf_is_binary(dxf_path): # Wenn dxf eine binary ist, dann komplett parsen und modelspace anlegen
print("Given .dxf-file is binary dxf. Proceeding to read file. Watch RAM-usage.")
doc = get_dxf_file(dxf_path)
msp = doc.modelspace()
use_iter = False
else:
print("Given .dxf-file is ASCII-dxf. Proceeding to use iterative functions. Process may take longer.")
use_iter = True
else:
parser.print_help()