From de60ec8ce1ba03d1da2a42b2f4729e2e1dc78a2b Mon Sep 17 00:00:00 2001 From: Paul Wolok Date: Fri, 5 Sep 2025 15:56:10 +0200 Subject: [PATCH] added Circle and Spline vor Bindbox calculation --- lib/dxf2lib.py | 51 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/dxf2lib.py b/lib/dxf2lib.py index 1ab0f22..4922496 100644 --- a/lib/dxf2lib.py +++ b/lib/dxf2lib.py @@ -1,6 +1,6 @@ import os import ezdxf -from ezdxf.math import Vec2 +from ezdxf.math import Vec2, BoundingBox import math import argparse import sys @@ -68,7 +68,16 @@ def get_bbox(entities): for px, py in candidates: min_x, min_y = min(min_x, px), min(min_y, py) max_x, max_y = max(max_x, px), max(max_y, py) - elif e.dxftype() == "LINE": + elif e.dxftype() == "CIRCLE": + # Handle CIRCLE entities via center and radius + try: + cx, cy = e.dsf.center.x, e.dsf.center.y + except Exception: + cx, cy = e.dxf.center + r = float(e.dxf.radius) + min_x, min_y = min(min_x, cx - r), min(min_y, cy - r) + max_x, max_y = max(max_x, cx + r), max(max_y, cy + r) + elif e.dxftype() == "LINE" or e.dxftype()== "Line": # Handle simple line entities by their start/end points try: sx, sy = e.dxf.start.x, e.dxf.start.y @@ -76,8 +85,34 @@ def get_bbox(entities): except Exception: # Some ezdxf versions provide tuples (sx, sy), (ex, ey) = e.dxf.start, e.dxf.end - min_x, min_y = min(min_x, sx), min(min_y, sy) - max_x, max_y = max(max_x, ex), max(max_y, ey) + min_x, min_y = min(min_x, sx, ex ), min(min_y, sy, ey) + max_x, max_y = max(max_x, sx, ex), max(max_y, sy, ey) + elif e.dxftype() == "SPLINE": + # Approximate spline to compute bounding box + points = [] + try: + points = e.approximate(60) + except Exception: + try: + points = list(e.flattening(1.0)) + except Exception: + points = [] + if points: + for pt in points: + try: + px, py = pt.x, pt.y + except Exception: + px, py = pt[0], pt[1] + min_x, min_y = min(min_x, px), min(min_y, py) + max_x, max_y = max(max_x, px), max(max_y, py) + else: + box = e.bbox() + if box: + (x1, y1), (x2, y2) = box.extmin, box.extmax + min_x, min_y = min(min_x, x1), min(min_y, y1) + max_x, max_y = max(max_x, x2), max(max_y, y2) + + else: # Für andere Entity-Typen: Standard bbox() verwenden @@ -92,7 +127,7 @@ def get_bbox(entities): if min_x == float('inf'): return None, (0,0) - + return Vec2((min_x + max_x) / 2, (min_y + max_y) / 2), (max_x -min_x, max_y -min_y) @@ -131,6 +166,7 @@ def create_block_library(input_dir, output_file, config, logger=None): if not filename.lower().endswith(".dxf"): continue + filepath = os.path.join(input_dir, filename) name = os.path.splitext(filename)[0] @@ -140,7 +176,7 @@ def create_block_library(input_dir, output_file, config, logger=None): entities = list(src_msp) allowed_types = {"LINE", "LWPOLYLINE", "POLYLINE", "SPLINE", "CIRCLE", "ARC"} filtered_entities = [e for e in entities if e.dxftype() in allowed_types] - entities = filtered_entities + except Exception as e: error_msg = f"Fehler beim Lesen von {filename}: {e}" if logger: @@ -183,8 +219,7 @@ def create_block_library(input_dir, output_file, config, logger=None): blk.add_attdef( tag="NAME", insert=(0.2, 0.2), # Position relativ zum Blockursprung - height=0.25, - text="Default Text" + text="Default Text", ) msp.add_blockref(name, insert=(x_offset, y_offset)) # Text mit Blocknamen über dem Block