added Circle and Spline vor Bindbox calculation
This commit is contained in:
+42
-7
@@ -1,6 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import ezdxf
|
import ezdxf
|
||||||
from ezdxf.math import Vec2
|
from ezdxf.math import Vec2, BoundingBox
|
||||||
import math
|
import math
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
@@ -68,7 +68,16 @@ def get_bbox(entities):
|
|||||||
for px, py in candidates:
|
for px, py in candidates:
|
||||||
min_x, min_y = min(min_x, px), min(min_y, py)
|
min_x, min_y = min(min_x, px), min(min_y, py)
|
||||||
max_x, max_y = max(max_x, px), max(max_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
|
# Handle simple line entities by their start/end points
|
||||||
try:
|
try:
|
||||||
sx, sy = e.dxf.start.x, e.dxf.start.y
|
sx, sy = e.dxf.start.x, e.dxf.start.y
|
||||||
@@ -76,8 +85,34 @@ def get_bbox(entities):
|
|||||||
except Exception:
|
except Exception:
|
||||||
# Some ezdxf versions provide tuples
|
# Some ezdxf versions provide tuples
|
||||||
(sx, sy), (ex, ey) = e.dxf.start, e.dxf.end
|
(sx, sy), (ex, ey) = e.dxf.start, e.dxf.end
|
||||||
min_x, min_y = min(min_x, sx), min(min_y, sy)
|
min_x, min_y = min(min_x, sx, ex ), min(min_y, sy, ey)
|
||||||
max_x, max_y = max(max_x, ex), max(max_y, 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:
|
else:
|
||||||
|
|
||||||
# Für andere Entity-Typen: Standard bbox() verwenden
|
# Für andere Entity-Typen: Standard bbox() verwenden
|
||||||
@@ -131,6 +166,7 @@ def create_block_library(input_dir, output_file, config, logger=None):
|
|||||||
if not filename.lower().endswith(".dxf"):
|
if not filename.lower().endswith(".dxf"):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
filepath = os.path.join(input_dir, filename)
|
filepath = os.path.join(input_dir, filename)
|
||||||
name = os.path.splitext(filename)[0]
|
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)
|
entities = list(src_msp)
|
||||||
allowed_types = {"LINE", "LWPOLYLINE", "POLYLINE", "SPLINE", "CIRCLE", "ARC"}
|
allowed_types = {"LINE", "LWPOLYLINE", "POLYLINE", "SPLINE", "CIRCLE", "ARC"}
|
||||||
filtered_entities = [e for e in entities if e.dxftype() in allowed_types]
|
filtered_entities = [e for e in entities if e.dxftype() in allowed_types]
|
||||||
entities = filtered_entities
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_msg = f"Fehler beim Lesen von {filename}: {e}"
|
error_msg = f"Fehler beim Lesen von {filename}: {e}"
|
||||||
if logger:
|
if logger:
|
||||||
@@ -183,8 +219,7 @@ def create_block_library(input_dir, output_file, config, logger=None):
|
|||||||
blk.add_attdef(
|
blk.add_attdef(
|
||||||
tag="NAME",
|
tag="NAME",
|
||||||
insert=(0.2, 0.2), # Position relativ zum Blockursprung
|
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))
|
msp.add_blockref(name, insert=(x_offset, y_offset))
|
||||||
# Text mit Blocknamen über dem Block
|
# Text mit Blocknamen über dem Block
|
||||||
|
|||||||
Reference in New Issue
Block a user