Optimierung der Bounding Box-Berechnung in dxf2lib.py: Vereinfachung der POLYLINE- und LWPOLYLINE-Logik, um die Berechnung der minimalen und maximalen Koordinaten zu verbessern. Anpassung der Blockerstellung, um die Geometrie korrekt zu verschieben.

This commit is contained in:
2025-07-30 11:31:51 +02:00
parent 91fffadc1f
commit 4033fcbd5a
+15 -9
View File
@@ -17,14 +17,16 @@ def get_bbox(entities):
max_x, max_y = float('-inf'), float('-inf')
for e in entities:
try:
# Für POLYLINE-Entities: Bounding Box manuell berechnen
if e.dxftype() == "POLYLINE":
if hasattr(e, 'vertices') and len(e.vertices) > 0:
for vertex in e.vertices:
if hasattr(vertex, 'dxf') and hasattr(vertex.dxf, 'location'):
x, y = vertex.dxf.location.x, vertex.dxf.location.y
min_x, min_y = min(min_x, x), min(min_y, y)
max_x, max_y = max(max_x, x), max(max_y, y)
for vertex in e.vertices:
if hasattr(vertex.dxf, 'location'):
x, y = vertex.dxf.location.x, vertex.dxf.location.y
min_x, min_y = min(min_x, x), min(min_y, y)
max_x, max_y = max(max_x, x), max(max_y, y)
elif e.dxftype() == "LWPOLYLINE":
for x, y, *_ in e.get_points("xy"):
min_x, min_y = min(min_x, x), min(min_y, y)
max_x, max_y = max(max_x, x), max(max_y, y)
else:
# Für andere Entity-Typen: Standard bbox() verwenden
box = e.bbox()
@@ -35,8 +37,10 @@ def get_bbox(entities):
except Exception as err:
print(f" BBox Fehler für {e.dxftype()}: {err}")
continue
if min_x == float('inf'):
return None
return Vec2((min_x + max_x) / 2, (min_y + max_y) / 2)
if __name__ == "__main__":
@@ -101,11 +105,13 @@ if __name__ == "__main__":
if name in doc.blocks:
doc.blocks.delete_block(name)
blk = doc.blocks.new(name=name, base_point=center)
blk = doc.blocks.new(name=name, base_point=(0,0))
for e in entities:
try:
blk.add_entity(e.copy())
cp = e.copy()
cp.translate(-center.x, -center.y, 0) # Geometrie verschieben!
blk.add_entity(cp)
except Exception as err:
print(f"Fehler in {filename}: {err}")