Merge branch 'main' of http://gitea.schoenenberger.de/mistangl/kabellaengen
This commit is contained in:
+52
-2
@@ -1,4 +1,54 @@
|
||||
@echo off
|
||||
CALL setenv.bat
|
||||
|
||||
python %PROJECT_LIB%\portalexport.py %*
|
||||
if [%1]==[] goto usage
|
||||
for %%i in ("%~1") do (
|
||||
set "FILENAME=%%~ni"
|
||||
set "EXT=%%~xi"
|
||||
set "DIR=%%~dpi"
|
||||
)
|
||||
|
||||
REM echo Dateiname ohne Erweiterung: %FILENAME%
|
||||
REM echo Erweiterung: %EXT%
|
||||
REM echo Verzeichnis: %DIR%
|
||||
REM
|
||||
REM Namen der Zwischenergebnis Dateien
|
||||
set JSON_POS=%FILENAME%_positions.json
|
||||
set JSON_TODRAW=%FILENAME%_todraw.json
|
||||
REM Namen der Ergebnisdateien
|
||||
set ERROR_DOUBLE=%FILENAME%_errors.json
|
||||
|
||||
|
||||
call C:\kabellaengen\bin\setenv.bat
|
||||
if exist "_setenv_local.bat" (
|
||||
call _setenv_local.bat
|
||||
)
|
||||
|
||||
|
||||
echo.
|
||||
echo === Fetching Positions ===
|
||||
call getpositions.bat --filename %1 -s -r -w %JSON_POS% -e %ERROR_DOUBLE%
|
||||
if exist "%PROJECT_WORK%\%ERROR_DOUBLE%" (
|
||||
@echo -failed- duplicate IDs in given layout
|
||||
pause
|
||||
move %PROJECT_WORK%\%ERROR_DOUBLE% %INSTALL_DIR%
|
||||
goto :eof
|
||||
)
|
||||
if not exist "%PROJECT_WORK%\%JSON_POS%" (
|
||||
@echo -failed- getpositions
|
||||
pause
|
||||
goto :eof
|
||||
)
|
||||
echo === Creating Excel Files for TIA, WSCAD, .. ===
|
||||
call ioconvert.bat --filename %JSON_POS% -outname %FILENAME%
|
||||
|
||||
mkdir %INSTALL_DIR%\%FILENAME%
|
||||
move %PROJECT_WORK%\%FILENAME%_* %INSTALL_DIR%\%FILENAME%
|
||||
goto :eof
|
||||
|
||||
|
||||
:usage
|
||||
@echo Usage: %0 ^<dxfinWorkOrdner.dxf^>
|
||||
exit /B 1
|
||||
goto :eof
|
||||
|
||||
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
@echo off
|
||||
|
||||
if [%1]==[] goto usage
|
||||
for %%i in ("%~1") do (
|
||||
set "FILENAME=%%~ni"
|
||||
set "EXT=%%~xi"
|
||||
set "DIR=%%~dpi"
|
||||
)
|
||||
|
||||
REM echo Dateiname ohne Erweiterung: %FILENAME%
|
||||
REM echo Erweiterung: %EXT%
|
||||
REM echo Verzeichnis: %DIR%
|
||||
REM
|
||||
REM Namen der Zwischenergebnis Dateien
|
||||
set JSON_POS=%FILENAME%_positions.json
|
||||
set JSON_TODRAW=%FILENAME%_todraw.json
|
||||
REM Namen der Ergebnisdateien
|
||||
set ERROR_DOUBLE=%FILENAME%_errors.json
|
||||
|
||||
|
||||
call C:\10-Develop\gitrepos\kabellaengen\bin\setenv.bat
|
||||
|
||||
|
||||
echo --hole Positionen
|
||||
call portalexport.bat --filename %1 -w %JSON_POS% -e %ERROR_DOUBLE%
|
||||
if exist "%PROJECT_WORK%\%ERROR_DOUBLE%" (
|
||||
@echo -failed- given items with the same ids
|
||||
pause
|
||||
move %PROJECT_WORK%\%ERROR_DOUBLE% %INSTALL_DIR%
|
||||
goto :eof
|
||||
)
|
||||
if not exist "%PROJECT_WORK%\%JSON_POS%" (
|
||||
@echo -failed- getpositions
|
||||
pause
|
||||
goto :eof
|
||||
)
|
||||
|
||||
mkdir %INSTALL_DIR%\%FILENAME%
|
||||
move %PROJECT_WORK%\%FILENAME%_* %INSTALL_DIR%\%FILENAME%
|
||||
goto :eof
|
||||
|
||||
|
||||
:usage
|
||||
@echo Usage: %0 ^<dxfinWorkOrdner.dxf^>
|
||||
exit /B 1
|
||||
goto :eof
|
||||
|
||||
|
||||
+143
-56
@@ -2,87 +2,174 @@ import os
|
||||
import sys
|
||||
import argparse
|
||||
from ioconverter import ExcelConverter
|
||||
import configparser
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
def prepare_data(rawdata:dict):
|
||||
sensors = rawdata["sensors"]
|
||||
dsensors = dict()
|
||||
lsensors = list()
|
||||
for sname, sdata in sensors.items():
|
||||
data = {
|
||||
"id": sdata.get("IO", ""),
|
||||
"bezeichnung": sdata.get("BEZEICHNUNG", ""),
|
||||
"verwendung": sdata.get("VERW", ""),
|
||||
"kennzeichnung": sdata.get("KENNZEICHNUNG", ""),
|
||||
"text_d": sdata.get("TEXT-D", ""),
|
||||
"text_e": sdata.get("TEXT-E", ""),
|
||||
"text_es": sdata.get("TEXT-ES", ""),
|
||||
"text_f": sdata.get("TEXT-F", ""),
|
||||
"sps": sdata.get("SPS", None),
|
||||
}
|
||||
reihenfolge = ["id", "bezeichnung", "verwendung", "kennzeichnung", "text_d", "text_e", "text_es", "text_f"]
|
||||
# CSV-String erzeugen
|
||||
csvstr = "','".join(str(data[key]) for key in reihenfolge)
|
||||
csvstr = "'"+csvstr+"'"
|
||||
|
||||
sps_nr = None
|
||||
if "SPS" in sdata:
|
||||
sps_nr = sdata.get("SPS", None)
|
||||
|
||||
if sps_nr:
|
||||
if sps_nr not in dsensors:
|
||||
dsensors[sps_nr] = list()
|
||||
dsensors[sps_nr].append(csvstr)
|
||||
else:
|
||||
lsensors.append(csvstr)
|
||||
|
||||
return (dsensors, lsensors)
|
||||
|
||||
|
||||
# einfache Funktionen
|
||||
def load_json(jsonfilename):
|
||||
with open(jsonfilename, encoding='utf-8') as fh:
|
||||
return json.load(fh)
|
||||
|
||||
def check_file_in_work(work_dir:Path, filename:Path):
|
||||
fexists = True
|
||||
if not filename.exists(): # dann schau im Work Ordner nach
|
||||
mypath = work_dir.joinpath(filename)
|
||||
ex = mypath.exists()
|
||||
if not mypath.exists():
|
||||
fexists = False
|
||||
else:
|
||||
mypath = filename
|
||||
return (mypath, fexists)
|
||||
|
||||
# Helper für Export
|
||||
def do_export(mode_str, output_dir):
|
||||
def do_export(conv_obj:ExcelConverter, mode_str, base_input, output_dir, lines):
|
||||
# Für WSCAD Dateinamen anpassen (Export-Typ bleibt "WSCAD" oder "WSCAD mit Bezug")
|
||||
if mode_str == "WSCAD":
|
||||
export_type = "WSCAD" if args.no_bezug else "WSCAD mit Bezug"
|
||||
filename = f"{input_basename}_{'WSCAD_no_Bezug' if args.no_bezug else 'WSCAD'}.xlsx"
|
||||
if mode_str == "WSCAD":
|
||||
if args.no_bezug:
|
||||
export_type = "WSCAD"
|
||||
filename = f"{base_input}_WSCAD_no_Bezug.xlsx"
|
||||
else:
|
||||
export_type = "WSCAD mit Bezug"
|
||||
filename = f"{base_input}_WSCAD.xlsx"
|
||||
else:
|
||||
export_type = mode_str
|
||||
filename = f"{input_basename}_{mode_str}.xlsx"
|
||||
filename = f"{base_input}_{mode_str}.xlsx"
|
||||
|
||||
export_path = os.path.join(output_dir, filename)
|
||||
|
||||
export_path = args.output if args.output and args.mode == mode_str else os.path.join(output_dir, filename)
|
||||
try:
|
||||
converter.export_excel(lines, export_type, export_path)
|
||||
conv_obj.export_excel(lines, export_type, export_path)
|
||||
print(f"Export '{export_type}' erfolgreich: {export_path}")
|
||||
except Exception as e:
|
||||
print(f"Fehler bei Export '{export_type}': {e}")
|
||||
|
||||
def do_exports(converter, export_mode, output_dir, base_input, lines):
|
||||
if export_mode:
|
||||
do_export(converter, export_mode, base_input, output_dir, lines)
|
||||
else:
|
||||
# Kein Modus → alle drei exportieren
|
||||
do_export(converter, "EA", base_input, output_dir, lines)
|
||||
do_export(converter, "TIA", base_input, output_dir, lines)
|
||||
do_export(converter, "WSCAD", base_input, output_dir, lines)
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Excel-Konverter für BMK-Daten")
|
||||
parser.add_argument("--mode", type=str, choices=["EA", "TIA", "WSCAD"],
|
||||
help="Art des Exports (EA, TIA, WSCAD). Wenn nicht gesetzt, werden alle drei erzeugt.")
|
||||
parser.add_argument("--input", type=str, required=True, help="Pfad zur Eingabedatei (.txt)")
|
||||
parser.add_argument("--output", type=str, help="Pfad zur Zieldatei (.xlsx)")
|
||||
parser.add_argument("--no-bezug", action="store_true", help="(Nur bei WSCAD) Export ohne Bezugsinformationen")
|
||||
parser.add_argument("--mode", type=str, choices=["EA", "TIA", "WSCAD", "WSCAD_OBZG"],
|
||||
help="Art des Exports (EA, TIA, WSCAD, WSCAD_OBZG). Wenn nicht gesetzt, werden die ersten drei erzeugt.")
|
||||
parser.add_argument("--no_bezug", action="store_true", help="(Nur bei WSCAD) Export ohne Bezugsinformationen")
|
||||
|
||||
parser.add_argument("--input", type=str, help="Pfad zur Eingabedatei in Form einer Kommaseparierten .csv Datei (.txt)")
|
||||
parser.add_argument("--filename", type=str, help="Json Datei von getpositions.py als Eingabe (.json)")
|
||||
parser.add_argument("--export_dir", type=str, help="Pfad zum Ordner für die Ausgabe. Default ist PROJECT_WORK")
|
||||
parser.add_argument("--outname", type=str, help="Basisname der Zieldateien wie <name>_TIA.xlsx)")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
|
||||
|
||||
# Umgebungsvariablen
|
||||
work_dir = os.environ.get("PROJECT_WORK")
|
||||
config_dir = os.environ.get("PROJECT_CFG")
|
||||
work_dir = Path(os.environ.get("PROJECT_WORK"))
|
||||
config_dir = Path(os.environ.get("PROJECT_CFG"))
|
||||
|
||||
# Wenn Input Pfad kein absoluter Pfad, dann in work nach Dateinamen suchen
|
||||
if not os.path.isabs(args.input):
|
||||
if work_dir:
|
||||
args.input = os.path.join(work_dir, args.input)
|
||||
|
||||
# Converter initialisieren
|
||||
converter = ExcelConverter()
|
||||
output_dir = work_dir
|
||||
if args.export_dir:
|
||||
output_dir = args.export_dir
|
||||
|
||||
# Konfigurationsdatei
|
||||
# config = configparser.ConfigParser(allow_no_value=True, delimiters=("="))
|
||||
# config.optionxform = lambda option: option # preserve case for letters
|
||||
# config.read(os.path.join(config_dir, "xxxx.cfg"))
|
||||
|
||||
# Datei einlesen
|
||||
try:
|
||||
with open(args.input, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Lesen der Datei: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
# Converter initialisieren
|
||||
converter = ExcelConverter()
|
||||
input_basename = os.path.splitext(os.path.basename(args.input))[0]
|
||||
output_dir = os.environ.get("PROJECT_WORK", os.getcwd())
|
||||
|
||||
# Modus bestimmen
|
||||
export_mode = None
|
||||
if args.mode:
|
||||
export_mode = args.mode
|
||||
|
||||
if export_mode == "WSCAD":
|
||||
# WSCAD bleibt immer der Export-Typ
|
||||
export_type = "WSCAD" if args.no_bezug else "WSCAD mit Bezug"
|
||||
# Dateiname hängt vom --no-bezug-Flag ab
|
||||
filename_suffix = "WSCAD_no_Bezug" if args.no_bezug else "WSCAD"
|
||||
filename = f"{input_basename}_{filename_suffix}.xlsx"
|
||||
export_path = args.output if args.output else os.path.join(output_dir, filename)
|
||||
try:
|
||||
converter.export_excel(lines, export_type, export_path)
|
||||
print(f"Export {export_type} erfolgreich: {export_path}")
|
||||
except Exception as e:
|
||||
print(f"Fehler bei Export {export_type}: {e}")
|
||||
else:
|
||||
do_export(export_mode, output_dir)
|
||||
else:
|
||||
# Kein Modus → alle drei exportieren
|
||||
do_export("EA", output_dir)
|
||||
do_export("TIA", output_dir)
|
||||
do_export("WSCAD", output_dir)
|
||||
|
||||
if args.outname:
|
||||
basename_out = args.outname
|
||||
elif args.filename:
|
||||
basename_out = args.filename.split('_')[0]
|
||||
elif args.input:
|
||||
basename_out = os.path.splitext(os.path.basename(args.input))[0]
|
||||
else:
|
||||
raise Exception()
|
||||
|
||||
# Datei einlesen
|
||||
if args.input:
|
||||
# Wenn Input Pfad kein absoluter Pfad, dann in work nach Dateinamen suchen
|
||||
if not os.path.isabs(args.input):
|
||||
if work_dir:
|
||||
args.input = os.path.join(work_dir, args.input)
|
||||
|
||||
try:
|
||||
with open(args.input, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Lesen der Datei: {e}")
|
||||
sys.exit(1)
|
||||
do_exports(converter, export_mode, output_dir, basename_out, lines)
|
||||
|
||||
elif args.filename:
|
||||
filename = Path(args.filename)
|
||||
if not filename.suffix == ".json":
|
||||
print("only available for .json files")
|
||||
exit()
|
||||
|
||||
json_file = Path(args.filename)
|
||||
(json_path, dexists) = check_file_in_work(work_dir, json_file)
|
||||
if not dexists:
|
||||
print(f"file {json_file} does not exist")
|
||||
parser.print_help()
|
||||
exit()
|
||||
|
||||
rawdata = load_json(json_path)
|
||||
(dsensors, lsensors) = prepare_data(rawdata)
|
||||
if len(dsensors) == 0:
|
||||
do_exports(converter, export_mode, output_dir, basename_out, lsensors)
|
||||
else:
|
||||
for sps_nr, lines in dsensors.items():
|
||||
name = f"{basename_out}-{sps_nr}"
|
||||
do_exports(converter, export_mode, output_dir, name, lines)
|
||||
|
||||
else:
|
||||
parser.print_help()
|
||||
exit()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user