From e0af4c3c95fababf9b7b272d795bb5f3a8fba396 Mon Sep 17 00:00:00 2001 From: lertlmaier Date: Fri, 27 Jun 2025 12:17:57 +0200 Subject: [PATCH] =?UTF-8?q?draw=5Fsensors()=20und=20draw=5Fsubdists()=20eb?= =?UTF-8?q?enfalls=20aufgeteilt.=20Hilfsfunktion=20zur=20findung=20der=20p?= =?UTF-8?q?lacement=20info=20(Stapelung=20bei=20mehreren=20Sensoren)=20ein?= =?UTF-8?q?gef=C3=BChrt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/drawdxf.py | 126 +++++++++++++++++++++++-------------------------- 1 file changed, 60 insertions(+), 66 deletions(-) diff --git a/lib/drawdxf.py b/lib/drawdxf.py index 2201121..314af8c 100644 --- a/lib/drawdxf.py +++ b/lib/drawdxf.py @@ -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):