97 lines
2.6 KiB
Python
97 lines
2.6 KiB
Python
"""
|
|
Erzeugt benannte Koordinatensystem-Bloecke in DXF-Dateien.
|
|
|
|
Jeder Block enthaelt drei Linien vom Ursprung:
|
|
- rot(1) X-Achse, Laenge 1
|
|
- gruen(3) Y-Achse, Laenge 2
|
|
- blau(5) Z-Achse, Laenge 3
|
|
|
|
Die 3D-Rotation erfolgt ueber die Transformation des INSERT-Entities.
|
|
"""
|
|
|
|
import math
|
|
import ezdxf
|
|
from ezdxf.math import Matrix44
|
|
|
|
|
|
# Block-Linien: (color, dx, dy, dz)
|
|
KS_LINES = [
|
|
(1, 1.0, 0.0, 0.0), # rot, X-Achse, Laenge 1
|
|
(3, 0.0, 2.0, 0.0), # gruen, Y-Achse, Laenge 2
|
|
(5, 0.0, 0.0, 3.0), # blau, Z-Achse, Laenge 3
|
|
]
|
|
|
|
|
|
def _ensure_block(doc, block_name):
|
|
"""Erzeugt den Block falls er noch nicht existiert."""
|
|
if block_name not in doc.blocks:
|
|
blk = doc.blocks.new(name=block_name)
|
|
for color, dx, dy, dz in KS_LINES:
|
|
blk.add_line(
|
|
start=(0, 0, 0),
|
|
end=(dx, dy, dz),
|
|
dxfattribs={"color": color},
|
|
)
|
|
return block_name
|
|
|
|
|
|
def _ensure_layer(doc, layer_name="_KOORDINATENSYSTEME"):
|
|
"""Stellt sicher, dass der Layer existiert."""
|
|
if layer_name not in doc.layers:
|
|
doc.layers.add(layer_name)
|
|
|
|
|
|
def insert_ks(msp, name, point, rx=0, ry=0, rz=0):
|
|
"""
|
|
Erzeugt ein benanntes Koordinatensystem als Block-Referenz.
|
|
|
|
Args:
|
|
msp: Modelspace oder Block des Ziel-Dokuments.
|
|
name: Block-Name (z.B. "K1", "K2", "K3", "K4").
|
|
point: Einfuegepunkt als (x, y, z) Tupel.
|
|
rx: Rotation um X-Achse in Grad.
|
|
ry: Rotation um Y-Achse in Grad.
|
|
rz: Rotation um Z-Achse in Grad.
|
|
|
|
Returns:
|
|
Das erzeugte INSERT-Entity.
|
|
"""
|
|
doc = msp.doc
|
|
_ensure_layer(doc)
|
|
_ensure_block(doc, name)
|
|
|
|
insert = msp.add_blockref(
|
|
name,
|
|
insert=point,
|
|
dxfattribs={"layer": "_KOORDINATENSYSTEME"},
|
|
)
|
|
|
|
# 3D-Rotation via Transformationsmatrix
|
|
m = Matrix44.chain(
|
|
Matrix44.translate(-point[0], -point[1], -point[2]),
|
|
Matrix44.x_rotate(math.radians(rx)),
|
|
Matrix44.y_rotate(math.radians(ry)),
|
|
Matrix44.z_rotate(math.radians(rz)),
|
|
Matrix44.translate(point[0], point[1], point[2]),
|
|
)
|
|
insert.transform(m)
|
|
|
|
return insert
|
|
|
|
|
|
if __name__ == "__main__":
|
|
doc = ezdxf.new(dxfversion="R2010")
|
|
msp = doc.modelspace()
|
|
|
|
insert_ks(msp, "K1", (0, 0, 0))
|
|
insert_ks(msp, "K2", (10, 0, 0), rz=90)
|
|
insert_ks(msp, "K3", (20, 0, 0), ry=45)
|
|
insert_ks(msp, "K4", (30, 0, 0), rx=30, ry=45, rz=60)
|
|
|
|
import os
|
|
results_dir = os.environ.get("DXFM_RESULTS", "results")
|
|
os.makedirs(results_dir, exist_ok=True)
|
|
out_path = os.path.join(results_dir, "ks_test.dxf")
|
|
doc.saveas(out_path)
|
|
print(f"Testdatei gespeichert: {out_path}")
|