Erweiterung der Bounding Box-Berechnung für POLYLINE-Entities und Filterung der Entities nach erlaubten Typen. Anpassung der Platzierung von Blöcken in Reihen und Spalten.
This commit is contained in:
+30
-9
@@ -23,18 +23,31 @@ def get_bbox(entities):
|
|||||||
max_x, max_y = float('-inf'), float('-inf')
|
max_x, max_y = float('-inf'), float('-inf')
|
||||||
for e in entities:
|
for e in entities:
|
||||||
try:
|
try:
|
||||||
box = e.bbox()
|
# Für POLYLINE-Entities: Bounding Box manuell berechnen
|
||||||
if box:
|
if e.dxftype() == "POLYLINE":
|
||||||
(x1, y1), (x2, y2) = box.extmin, box.extmax
|
if hasattr(e, 'vertices') and len(e.vertices) > 0:
|
||||||
min_x, min_y = min(min_x, x1), min(min_y, y1)
|
for vertex in e.vertices:
|
||||||
max_x, max_y = max(max_x, x2), max(max_y, y2)
|
if hasattr(vertex, 'dxf') and hasattr(vertex.dxf, 'location'):
|
||||||
except Exception:
|
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)
|
||||||
|
else:
|
||||||
|
# Für andere Entity-Typen: Standard bbox() verwenden
|
||||||
|
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)
|
||||||
|
except Exception as err:
|
||||||
|
print(f" BBox Fehler für {e.dxftype()}: {err}")
|
||||||
continue
|
continue
|
||||||
if min_x == float('inf'):
|
if min_x == float('inf'):
|
||||||
return None
|
return None
|
||||||
return Vec2((min_x + max_x) / 2, (min_y + max_y) / 2)
|
return Vec2((min_x + max_x) / 2, (min_y + max_y) / 2)
|
||||||
|
|
||||||
x_offset = 0
|
x_offset = 0
|
||||||
|
y_offset = 0
|
||||||
|
blocks_in_row = 0
|
||||||
|
|
||||||
for filename in os.listdir(INPUT_DIR):
|
for filename in os.listdir(INPUT_DIR):
|
||||||
if not filename.lower().endswith(".dxf"):
|
if not filename.lower().endswith(".dxf"):
|
||||||
@@ -47,6 +60,9 @@ for filename in os.listdir(INPUT_DIR):
|
|||||||
src_doc = ezdxf.readfile(filepath)
|
src_doc = ezdxf.readfile(filepath)
|
||||||
src_msp = src_doc.modelspace()
|
src_msp = src_doc.modelspace()
|
||||||
entities = list(src_msp)
|
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:
|
except Exception as e:
|
||||||
print(f"Fehler beim Lesen von {filename}: {e}")
|
print(f"Fehler beim Lesen von {filename}: {e}")
|
||||||
continue
|
continue
|
||||||
@@ -67,9 +83,14 @@ for filename in os.listdir(INPUT_DIR):
|
|||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(f"Fehler in {filename}: {err}")
|
print(f"Fehler in {filename}: {err}")
|
||||||
|
|
||||||
# Optional Vorschau
|
# Platzierung in Reihen und Spalten
|
||||||
msp.add_blockref(name, insert=(x_offset, 0))
|
msp.add_blockref(name, insert=(x_offset, y_offset))
|
||||||
x_offset += 200 # Abstand zwischen Blöcken
|
blocks_in_row += 1
|
||||||
|
x_offset += 250 # Abstand zwischen Blöcken in einer Reihe
|
||||||
|
if blocks_in_row == 20:
|
||||||
|
blocks_in_row = 0
|
||||||
|
x_offset = 0
|
||||||
|
y_offset -= 500 # Neue Zeile, nach unten versetzt
|
||||||
|
|
||||||
print(f"Bibliotheks-DXF gespeichert: {OUTPUT_FILE}")
|
print(f"Bibliotheks-DXF gespeichert: {OUTPUT_FILE}")
|
||||||
doc.saveas(OUTPUT_FILE)
|
doc.saveas(OUTPUT_FILE)
|
||||||
|
|||||||
Reference in New Issue
Block a user