212 lines
7.5 KiB
Python
212 lines
7.5 KiB
Python
# -*- coding: utf-8 -*-
|
||
import math
|
||
import plant2dxf
|
||
from ezdxf import units
|
||
from ezdxf.entities import Line
|
||
from ezdxf.addons import importer
|
||
from ezdxf.math import Matrix44, X_AXIS, Y_AXIS, Z_AXIS
|
||
|
||
|
||
def dreh_block(block_name: str, to_doc, lib_doc, winkel):
|
||
"""Nimmt ein schon importierten Block und erstellt einen neuen der an der y_axis oder x_axis gedreht wird"""
|
||
dreh_block_name = block_name + f"_{math.degrees(winkel)}"
|
||
layer, color = get_insert_color_layer(lib_doc, block_name)
|
||
if dreh_block_name in to_doc.blocks:
|
||
return dreh_block_name
|
||
block_zwischen = to_doc.blocks.new(
|
||
name=block_name + f"zwischenschrit_{winkel}", base_point=(0, 0, 0)
|
||
)
|
||
block = to_doc.blocks.new(name=dreh_block_name, base_point=(0, 0, 0))
|
||
if (
|
||
block_name == "200000146_ES-Element_90_rechts"
|
||
or block_name == "400102632_ES-Element_90_links"
|
||
or block_name == "200000241_AS-Element_90_rechts"
|
||
or block_name == "200000217_AS-Element_90_links"
|
||
):
|
||
rotation_matrix = Matrix44.axis_rotate(X_AXIS, winkel)
|
||
else:
|
||
rotation_matrix = Matrix44.axis_rotate(Y_AXIS, winkel)
|
||
|
||
block_zwischen.add_blockref(
|
||
block_name, (0, 0, 0), dxfattribs={"layer": layer, "color": color}
|
||
)
|
||
for e in block_zwischen:
|
||
copy = e.copy()
|
||
copy.transform(rotation_matrix)
|
||
block.add_entity(copy)
|
||
return dreh_block_name
|
||
|
||
|
||
def import_block(block_name: str, from_doc, to_doc, winkel=None) -> None:
|
||
"""Importiert Blockdefinition block_name von from_doc nach to_doc.
|
||
|
||
- Kopiert alle Entities des Blocks
|
||
- Stellt sicher, dass benutzte Layer im Ziel existieren (mit Eigenschaften)
|
||
- Übernimmt Basispunkt und Block-Layer, falls vorhanden
|
||
"""
|
||
|
||
src = from_doc.blocks[block_name]
|
||
att_def = {}
|
||
if block_name == "Pinbereich":
|
||
imp = importer.Importer(from_doc, to_doc)
|
||
|
||
# Alle Linientypen importieren
|
||
imp.import_table("linetypes")
|
||
if block_name in to_doc.blocks:
|
||
# speichern der attdef elemente in eine Liste falks diese verhanden sind und gibt diese zurück
|
||
for ent in src:
|
||
copy = ent.copy()
|
||
|
||
if ent.dxftype() == "ATTDEF":
|
||
att_def[ent.dxf.tag] = ent.dxf.text
|
||
if ent.dxftype() == "INSERT":
|
||
|
||
import_block(ent.dxf.name, from_doc, to_doc, None)
|
||
|
||
if att_def != {}:
|
||
return att_def
|
||
return
|
||
|
||
if block_name not in from_doc.blocks:
|
||
raise ValueError(f"Block '{block_name}' nicht in Bibliothek gefunden.")
|
||
# Sicherstellen, dass alle verwendeten Layer existieren
|
||
try:
|
||
used_layer_names = {e.dxf.layer for e in src if hasattr(e.dxf, "layer")}
|
||
for layer_name in used_layer_names:
|
||
if layer_name and layer_name not in to_doc.layers:
|
||
try:
|
||
src_layer = from_doc.layers.get(layer_name)
|
||
to_doc.layers.add(
|
||
name=layer_name,
|
||
color=getattr(src_layer.dxf, "color", None),
|
||
linetype=getattr(src_layer.dxf, "linetype", None),
|
||
lineweight=getattr(src_layer.dxf, "lineweight", None),
|
||
)
|
||
except Exception:
|
||
# Fallback: Layer mit Standardwerten anlegen
|
||
to_doc.layers.add(name=layer_name)
|
||
except Exception:
|
||
pass
|
||
tgt = to_doc.blocks.new(name=block_name)
|
||
# Basispunkt/Layer des Blocks übernehmen, wenn vorhanden
|
||
try:
|
||
tgt.block.dxf.base_point = src.block.dxf.base_point
|
||
except Exception:
|
||
pass
|
||
try:
|
||
tgt.block.dxf.layer = src.block.dxf.layer
|
||
except Exception:
|
||
pass
|
||
# kopiert die elemente von dem element aus dem block und speichert diese in den block für dass Modelspace auf und # speichern der attdef elemente in eine Liste falks diese verhanden sind und gibt diese zurück
|
||
|
||
for ent in src:
|
||
copy = ent.copy()
|
||
|
||
if ent.dxftype() == "ATTDEF":
|
||
att_def[ent.dxf.tag] = ent.dxf.text
|
||
if ent.dxftype() == "INSERT":
|
||
import_block(ent.dxf.name, from_doc, to_doc, None)
|
||
tgt.add_entity(copy)
|
||
|
||
if att_def != {}:
|
||
return att_def
|
||
|
||
|
||
def get_insert_color_layer(lib_doc, blockname):
|
||
"""Gibt den Layer und die Color für den Jeweiligen block in der Libary datei zurück"""
|
||
msp_lib = lib_doc.modelspace()
|
||
color = 0
|
||
layer = 0
|
||
for insert in msp_lib.query("INSERT"):
|
||
if insert.dxf.name == blockname:
|
||
color = insert.dxf.color
|
||
layer = insert.dxf.layer
|
||
return layer, color
|
||
|
||
|
||
def rotatate_and_left_motor_umlenk(doc, lib_doc, config):
|
||
motor_rotation = float(config.get("Ils 2.0 core winkel", "winkel_motor"))
|
||
umlenk_rotation = float(config.get("Ils 2.0 core winkel", "winkel_umlenk"))
|
||
block_Vario_Umlenkstation_500mm = "Vario_Umlenkstation_500mm"
|
||
block_Vario_Motorstation_500mm = "Vario_Motorstation_500mm"
|
||
blockname_motor_links = block_Vario_Motorstation_500mm + "_links"
|
||
blockname_umlenk_links = block_Vario_Umlenkstation_500mm + "_links"
|
||
import_block(block_Vario_Umlenkstation_500mm, lib_doc, doc)
|
||
import_block(block_Vario_Motorstation_500mm, lib_doc, doc)
|
||
turn_two_blocks_left(
|
||
doc,
|
||
block_Vario_Umlenkstation_500mm,
|
||
block_Vario_Motorstation_500mm,
|
||
blockname_motor_links,
|
||
blockname_umlenk_links,
|
||
)
|
||
|
||
block_Vario_Umlenkstation_500mm = dreh_block(
|
||
block_Vario_Umlenkstation_500mm, doc, lib_doc, math.radians(umlenk_rotation)
|
||
)
|
||
block_Vario_Motorstation_500mm = dreh_block(
|
||
block_Vario_Motorstation_500mm, doc, lib_doc, math.radians(motor_rotation)
|
||
)
|
||
blockname_motor_links = dreh_block(
|
||
blockname_motor_links, doc, lib_doc, math.radians(umlenk_rotation)
|
||
)
|
||
blockname_umlenk_links = dreh_block(
|
||
blockname_umlenk_links, doc, lib_doc, math.radians(motor_rotation)
|
||
)
|
||
return (
|
||
block_Vario_Umlenkstation_500mm,
|
||
block_Vario_Motorstation_500mm,
|
||
blockname_motor_links,
|
||
blockname_umlenk_links,
|
||
)
|
||
|
||
|
||
def turn_two_blocks_left(
|
||
doc,
|
||
block_1_name_zwischen,
|
||
block_2_name_zwischen,
|
||
block_2_left_name,
|
||
block_1_left_name,
|
||
):
|
||
blockname1 = block_1_name_zwischen
|
||
blockname2 = block_2_name_zwischen
|
||
if block_2_left_name not in doc.blocks:
|
||
matrix = Matrix44.scale(1, -1, 1)
|
||
block1_zwischen = doc.blocks.new(
|
||
name=block_1_name_zwischen + "zwischenschrit", base_point=(0, 0, 0)
|
||
)
|
||
block2_zwischen = doc.blocks.new(
|
||
name=block_2_name_zwischen + "zwischenschrit", base_point=(0, 0, 0)
|
||
)
|
||
block_2 = doc.blocks.new(name=block_2_left_name, base_point=(0, 0, 0))
|
||
block_1_left_name = doc.blocks.new(name=block_1_left_name, base_point=(0, 0, 0))
|
||
block1_zwischen.add_blockref(blockname1, (0, 0, 0))
|
||
block2_zwischen.add_blockref(blockname2, (0, 0, 0))
|
||
for e in block2_zwischen:
|
||
copy = e.copy()
|
||
copy.transform(matrix)
|
||
block_2.add_entity(copy)
|
||
|
||
for e in block1_zwischen:
|
||
copy = e.copy()
|
||
copy.transform(matrix)
|
||
block_1_left_name.add_entity(copy)
|
||
|
||
|
||
def add_attributes_to_block(block, attributes):
|
||
"""
|
||
Fügt einem bestehenden ezdxf-Block Attribut-Definitionen hinzu.
|
||
|
||
Parameter:
|
||
block – ein ezdxf.blocks.BlockLayout Objekt
|
||
attributes – Liste von Tupeln [(tag, value), ...]
|
||
"""
|
||
for tag, value in attributes.items():
|
||
tag = tag
|
||
value = value
|
||
a = block.add_attrib(
|
||
tag=tag,
|
||
text=value,
|
||
)
|
||
a.is_invisible = True
|