Files

104 lines
3.2 KiB
Python

import os
import sys
import json
import argparse
import win32com.client
def read_custom_properties(par_path):
"""Liest benutzerdefinierte Variablen 'Mittiges_Koordinatensystem_X/Y_Translation'."""
try:
app = win32com.client.Dispatch("SolidEdge.Application")
app.Visible = False # Optional auf True für Debug
doc = app.Documents.Open(par_path)
variables = doc.Variables
breite = None
hoehe = None
for i in range(1, variables.Count + 1):
variable = variables.Item(i)
name = variable.Name
value = variable.Value
if name == 'Mittiges_Koordinatensystem_X_Translation':
breite = round(value*2000, 3)
elif name == 'Mittiges_Koordinatensystem_Y_Translation':
hoehe = round(value*2000, 3)
doc.Close(False)
if breite is not None and hoehe is not None:
return breite, hoehe
else:
raise ValueError("Benötigte Variablen nicht gefunden.")
except Exception as e:
print(f"Fehler beim Lesen von {par_path}: {e}")
return None, None
finally:
try:
if doc:
doc.Close(False)
except:
pass
def extract_all(input_dir):
"""Verarbeitet alle .par Dateien im Verzeichnis."""
result = {}
for file in os.listdir(input_dir):
if file.lower().endswith(".par"):
par_path = os.path.join(input_dir, file)
print(f"Processing: {file}")
breite, hoehe = read_custom_properties(par_path)
if breite is not None and hoehe is not None:
key = os.path.splitext(file)[0] # Dateiname ohne .par
result[key] = {
"breite": breite,
"hoehe": hoehe
}
return result
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Extrahiert 'Breite' und 'Höhe' aus .par-Dateien in JSON.")
parser.add_argument("--in", dest="input_dir", required=False, help="Ordner mit .par-Dateien (Standard: PROJECT_INPUT oder ./input)")
parser.add_argument("--out", dest="output_dir", required=False, help="Ordner für JSON-Ausgabe (Standard: PROJECT_WORK oder ./work)")
if len(sys.argv) == 2 and sys.argv[1] in ("-h", "--help"):
parser.print_help()
sys.exit(0)
args = parser.parse_args()
input_dir = args.input_dir or os.environ.get("PROJECT_INPUT", "./input")
output_dir = args.output_dir or os.environ.get("PROJECT_WORK", "./work")
# Hole den Namen des Input-Ordners (ohne Pfad)
input_basename = os.path.basename(os.path.normpath(input_dir))
output_filename = input_basename + ".json"
# Pfad zur Zieldatei
json_output_path = os.path.join(output_dir, output_filename)
print(f"Input directory: {input_dir}")
print(f"Output JSON will be saved to: {json_output_path}\n")
if not os.path.exists(input_dir):
print(f"Input folder not found: {input_dir}")
sys.exit(1)
os.makedirs(output_dir, exist_ok=True)
data = extract_all(input_dir)
with open(json_output_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
print("\nDone! Data saved to:", json_output_path)