draw_sensors() und draw_subdists() ebenfalls aufgeteilt. Hilfsfunktion zur findung der placement info (Stapelung bei mehreren Sensoren) eingeführt

This commit is contained in:
2025-06-27 12:17:57 +02:00
parent 73dad69083
commit e0af4c3c95
+60 -66
View File
@@ -145,47 +145,18 @@ def draw_sensors(plines, doc):
pos2sensors[(pt2.x, pt2.y)].append(pl)
# Sensor Blöcke zeichnen
for (x,y), pls in pos2sensors.items():
for i, pl in enumerate(pls):
for (x,y), pls in pos2sensors.items():
for i, pl in enumerate(pls):
sensor_name = pl.id.split('-')[-1]
pt1 = pl.coords[-2]
pt2 = pl.coords[-1]
pt1, pt2 = pl.coords[-2], pl.coords[-1]
# Platzierungsinfo über Hilfsfunktion
placement_info = _calculate_text_placement(pt1, pt2, 'sensor', i, len(pls))
dx = pt2.x - pt1.x
dy = pt2.y - pt1.y
text = msp.add_text(sensor_name, dxfattribs=dxfattribs_sensors)
# Offsets für Beschriftungen
offsetx = 0
offsety = 0
# Kabel Horizontal
if abs(dx) > abs(dy):
n = len(pls)
center_offset = i - (n - 1) / 2 # Falls mehrere Sensoren -> Verschiebung so, dass Kabel immer in Mitte ankommt
offsety = -80 + center_offset * 110 # Wert -80 über Try-and-Error zur Mitte der Beschriftung angepasst
if dx > 0:
halign = 0 # LEFT
offsetx = 50 # Wert 50 durch Try-and-Error sodass etwas Abstand zu Kabelpritsche
else:
halign = 2 # RIGHT
offsetx = -50 # Wert 50 durch Try-and-Error sodass etwas Abstand zu Kabelpritsche
valign = 1 # BOTTOM
# Kabel vertikal
else:
if dy > 0:
valign = 0 # BASELINE
offsety = 50 + i * 110 # nach oben anfügen
else:
valign = 3 # TOP
offsety = -50 - i * 110 # nach unten anfügen
halign = 1 # CENTER
# Alignments setzen
text.dxf.halign = halign
text.dxf.valign = valign
text.set_placement((pt2.x + offsetx, pt2.y + offsety))
text.dxf.halign = placement_info["halign"]
text.dxf.valign = placement_info["valign"]
text.set_placement(placement_info["placement"])
def draw_subdists(plines, doc):
msp = doc.modelspace()
@@ -211,36 +182,58 @@ def draw_subdists(plines, doc):
subdist_name = pl.id.split('-')[0]
pt2 = pl.coords[1]
dx = pt2.x - pt1.x
dy = pt2.y - pt1.y
placement_info = _calculate_text_placement(pt1, pt2, 'subdist')
# Offsets für Beschriftungen
offsetx = 0
offsety = 0
if abs(dx) > abs(dy): # Horizontal
offsety = -80 # Wert -80 über Try-and-Error zur Mitte der Beschriftung angepasst
if dx < 0:
halign = 0 # LEFT
offsetx = 50 # Wert 50 durch Try-and-Error sodass etwas Abstand zu Kabelpritsche
else:
halign = 2 # RIGHT
offsetx = -50 # Wert 50 durch Try-and-Error sodass etwas Abstand zu Kabelpritsche
valign = 1 # BOTTOM
else: # Vertikal
if dy < 0:
valign = 0 # BASELINE
offsety = 50
else:
valign = 3 # TOP
offsety = -50
halign = 1 # CENTER
# Text platzieren
text = msp.add_text(subdist_name, dxfattribs=dxfattribs_subdists)
text.dxf.halign = halign
text.dxf.valign = valign
text.set_placement((pt1.x + offsetx, pt1.y + offsety))
text.dxf.halign = placement_info["halign"]
text.dxf.valign = placement_info["valign"]
text.set_placement(placement_info["placement"])
def _calculate_text_placement(pt1, pt2, text_type='sensor', item_index=0, total_items=1):
"""
Berechnet die optimale Position und Ausrichtung für Beschriftungen.
Gibt ein Dictionary mit Platzierungskoordinaten und Ausrichtungs-Flags zurück.
"""
dx = pt2.x - pt1.x
dy = pt2.y - pt1.y
# Lese Offsets aus der Konfiguration, mit Fallback-Werten
offset_h_dist = 50.0
offset_v_dist = 50.0
offset_y_center = -80.0
offset_y_stack = 110.0
offsetx, offsety = 0, 0
if abs(dx) > abs(dy): # Kabel verläuft hauptsächlich horizontal
valign = 1 # BOTTOM
if text_type == 'sensor':
center_offset = item_index - (total_items - 1) / 2
offsety = offset_y_center + center_offset * offset_y_stack
else: # subdist
offsety = offset_y_center
if dx > 0:
halign = 0 # LEFT
offsetx = offset_h_dist
else:
halign = 2 # RIGHT
offsetx = -offset_h_dist
else: # Kabel verläuft hauptsächlich vertikal
halign = 1 # CENTER
if dy > 0:
valign = 0 # BASELINE
offsety = offset_v_dist + item_index * offset_y_stack
else:
valign = 3 # TOP
offsety = -offset_v_dist - item_index * offset_y_stack
return {
"placement": (pt2.x + offsetx, pt2.y + offsety),
"halign": halign,
"valign": valign,
}
def model_from_json(json_file):
with open(json_file, encoding='utf-8') as fh:
@@ -519,6 +512,7 @@ def _create_bom_workbook(outpath, processed_data, bezeichner_cfg):
bom_path = outpath.replace("_cables.xlsx", "_BOM.xlsx")
wb.save(bom_path)
print(f"BOM saved as an excel file")
def check_file_in_work(work_dir, filename):
fexists = True
if not os.path.exists(filename):