From 0bc6e56bbd53888350e1e442fb25e552f17a9a75 Mon Sep 17 00:00:00 2001 From: Paul Wolok Date: Thu, 4 Sep 2025 13:30:41 +0200 Subject: [PATCH] =?UTF-8?q?statt=20statischer,=20dynamische=20setzung=20de?= =?UTF-8?q?r=20Bl=C3=B6ccke=20in=20dxf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/dxf2lib.py | 50 ++++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/lib/dxf2lib.py b/lib/dxf2lib.py index cccfb14..5690275 100644 --- a/lib/dxf2lib.py +++ b/lib/dxf2lib.py @@ -91,7 +91,7 @@ def get_bbox(entities): continue if min_x == float('inf'): - return None, None + return None, (0,0) return Vec2((min_x + max_x) / 2, (min_y + max_y) / 2), (max_x -min_x, max_y -min_y) @@ -126,7 +126,7 @@ def create_block_library(input_dir, output_file, config, logger=None): error_files = [] processed_files = [] - + max_blockspacing_y = 0 for filename in os.listdir(input_dir): if not filename.lower().endswith(".dxf"): continue @@ -184,36 +184,27 @@ def create_block_library(input_dir, output_file, config, logger=None): # Werte aus Config holen (Block: [dxf2lib]) section = "dxf2lib" - text_y = y_offset + get_cfg_value(section, "text_offset_y", DEFAULTS["text_offset_y"]) - text_offset_x = get_cfg_value(section, "text_offset_x", DEFAULTS["text_offset_x"]) - max_len = get_cfg_value(section, "max_len", DEFAULTS["max_len"]) text_height = get_cfg_value(section, "text_height", DEFAULTS["text_height"]) - line_spacing = get_cfg_value(section, "line_spacing", DEFAULTS["line_spacing"]) - # block_spacing_x = get_cfg_value(section, "block_spacing_x", DEFAULTS["block_spacing_x"]) extra_block_space_x = get_cfg_value(section, "extra_block_space_x", DEFAULTS["extra_block_space_x"]) - block_spacing_x = ausdehnung[0] + extra_block_space_x blocks_per_row = get_cfg_value(section, "blocks_per_row", DEFAULTS["blocks_per_row"]) - # block_spacing_y = get_cfg_value(section, "block_spacing_y", DEFAULTS["block_spacing_y"]) - block_spacing_y = ausdehnung[1] - + extra_text_space_y = get_cfg_value(section, "extra_text_space_y", DEFAULTS["extra_text_space_y"]) - if len(name) > max_len: - first_line = name[:max_len] - second_line = name[max_len:] - # obere Zeile - msp.add_text(first_line, dxfattribs={'height': text_height, 'insert': (x_offset + text_offset_x, text_y)}) - # untere Zeile, etwas niedriger (y kleiner) - msp.add_text(second_line, dxfattribs={'height': text_height, 'insert': (x_offset + text_offset_x, text_y - line_spacing)}) - else: - msp.add_text(name, dxfattribs={'height': text_height, 'insert': (x_offset + text_offset_x, text_y)}) + ausdehnung_x, ausdehung_y = ausdehnung[0], ausdehnung[1] + block_spacing_y = ausdehung_y + block_spacing_x = ausdehnung_x + extra_block_space_x + max_blockspacing_y = max(max_blockspacing_y, block_spacing_y) + + msp.add_text(name, dxfattribs={'height': text_height, 'insert': (x_offset -ausdehnung_x/2 , y_offset + ausdehung_y/2 + extra_text_space_y)}) processed_files.append(filename) blocks_in_row += 1 x_offset += block_spacing_x # Abstand zwischen Blöcken in einer Reihe if blocks_in_row == blocks_per_row: + blocks_in_row = 0 x_offset = 0 - y_offset -= block_spacing_y # Neue Zeile, nach unten versetzt + y_offset -= max_blockspacing_y # Neue Zeile, nach unten versetzt + max_blockspacing_y = 0 if logger: logger.info(f"Bibliotheks-DXF gespeichert: {output_file}") @@ -243,15 +234,12 @@ def create_block_library(input_dir, output_file, config, logger=None): # Standardwerte (falls nicht in der Config) DEFAULTS = { - "text_offset_y": 175, # Vertikaler Abstand des Texts über dem Block (in DXF-Einheiten) - "text_offset_x": -150, # Horizontaler Abstand des Texts links vom Block (in DXF-Einheiten) - "max_len": 9, # Maximale Zeichenlänge pro Textzeile (bei Überschreitung wird umgebrochen) "text_height": 20, # Schriftgröße des Texts (in DXF-Einheiten) - "line_spacing": 22, # Abstand zwischen zwei Textzeilen bei Umbrüchen (in DXF-Einheiten) "blocks_per_row": 20, # Anzahl Blöcke pro Zeile im Raster - "extra_block_space_x" : 50 # Extra Platz damit sich Blöcke nicht überlappen -} - + "extra_block_space_x" : 50, # Extra Platz damit sich Blöcke nicht überlappen + "extra_text_space_y" : 50 # Abstand der Überschrift über dem Symbol + +} def get_cfg_value(section, key, fallback): try: return int(config.get(section, key)) @@ -262,7 +250,7 @@ if __name__ == "__main__": # Argumentparser für Kommandozeilenoptionen parser = argparse.ArgumentParser(description="SVG/XML zu DXF Konverter") parser.add_argument('-i', '--input', type=str, help='Input-Verzeichnis mit SVG/XML-Dateien') - parser.add_argument('-n', '--name', required=False, type=str, help='Name der zu erzeugenden Bibliothek (optional, wird sonst abgefragt)') + parser.add_argument('-n', '--name', required=False, type=str, help='Name der zu erzeugenden Bibliothek (optional, wird sonst abgefragt)', default="test") if len(sys.argv) == 2 and sys.argv[1] in ("-h", "--help"): parser.print_help() @@ -284,7 +272,7 @@ if __name__ == "__main__": INPUT_DIR = check_environment_var("PROJECT_DATA") / "dxf" print(f"Kein Input-Verzeichnis angegeben, verwende Standard: {INPUT_DIR} \n") - OUTPUT_FILE = check_environment_var("PROJECT_DATA") / "block_libaries" / f"{args.name}.dxf" + OUTPUT_FILE = check_environment_var("PROJECT_DATA") / "block_libraries" / f"{args.name}.dxf" # Prüfe und erstelle log-Verzeichnis falls nötig log_dir = check_environment_var("PROJECT_LOG") @@ -307,6 +295,4 @@ if __name__ == "__main__": # Erstelle die Block-Bibliothek logger.info(f"Erstelle Block-Bibliothek aus {INPUT_DIR}...") create_block_library(INPUT_DIR, OUTPUT_FILE, config, logger) - - logger.info("=== DXF2LIB Verarbeitung abgeschlossen ===")