diff --git a/lib/getpositions.py b/lib/getpositions.py index 6b8f39f..8d6a0d4 100644 --- a/lib/getpositions.py +++ b/lib/getpositions.py @@ -65,6 +65,21 @@ def matches_cabinet_pattern(name: str) -> [bool, str]: return (False, "") +def matches_tunnel_pattern(name: str) -> bool: + """Check if the given name matches any Tunnel-Pattern from BMK.cfg""" + if not config_BMK.has_section("Tunnel-Pattern"): + return False + + patterns = [pattern for pattern, _ in config_BMK.items("Tunnel-Pattern")] + + for pattern in patterns: + m = re.search(pattern, name) + if m: + return True + + return False + + def get_attributes_of_insert(d_insert: dict, d_pos: dict) -> tuple[dict, str, str]: """ Diese Funktion schaut nach den aktuell definierten Attributen in den Blöcken @@ -116,7 +131,7 @@ def get_attributes_of_insert(d_insert: dict, d_pos: dict) -> tuple[dict, str, st else: ld["pos"] = (pos[0], pos[1]) - # die neueren Bläcke heissen nicht IO, sondern haben einen Namen + # die neueren Blöcke heissen nicht IO, sondern haben einen Namen if "NAME" in d_insert: typ = get_type_of_name_cfg(d_insert["NAME"]) id_ = d_insert["NAME"] @@ -451,6 +466,38 @@ def get_subdistributor_position_of_symbol(d_insert: dict, d_pos: dict) -> tuple[ return ld, id_ return None, None + +def get_tunnel_position_of_symbol(d_insert: dict, d_pos: dict) -> tuple[dict, str]: + """ + Diese Funktion schaut nach den aktuell definierten Attributen in allen Tunnel Blöcken + Tunnel haben einen Namen der den Tunnel-Mustern aus BMK.cfg entspricht + + """ + id_ = "" + ld = d_insert + + # Tunnel haben einen Namen + if "NAME" in d_insert: + id_ = d_insert["NAME"] + pos = d_pos["NAME"] + ld["pos"] = (pos[0], pos[1]) + + # Länge des Tunnels aus dem LAENGE Attribut extrahieren + if "LAENGE" in d_insert: + ld["laenge"] = d_insert["LAENGE"] + + if "REALE_POSITION" in d_insert and d_insert["REALE_POSITION"] == 'x': + pos = d_pos["REALE_POSITION"] + + # Hoehe und Breite von "x" addieren, um Mittelpunkt zu finden + breite_marker = config.getfloat("GetPos-Geom-Sensor", "Breite") + hoehe_marker = config.getfloat("GetPos-Geom-Sensor", "Hoehe") + midx = pos[0] + breite_marker * 0.5 + midy = pos[1] + hoehe_marker * 0.5 + ld["pos"] = (round(midx, 1), round(midy, 1)) + return ld, id_ + return None, None + def get_subdistributor_positions_from_symbols(all_inserts:list, all_positions:list, dist2sensors: dict) -> dict: """Ermittelt Unterverteiler-Positionen aus allen Symbolen """ @@ -472,6 +519,48 @@ def get_subdistributor_positions_from_symbols(all_inserts:list, all_positions:li return ret +def get_tunnel_positions_from_symbols(all_inserts: list, all_positions: list) -> dict: + """Ermittelt Tunnel-Positionen aus allen Symbolen + + Args: + all_inserts: Liste von Dictionaries mit Attribut-Tags und deren Textwerten + all_positions: Liste von Dictionaries mit Attribut-Tags und deren (x, y, z)-Positionen + + Returns: + Dictionary mit Tunnelnamen als Keys und Listen von Positionen als Values + Format: {'Tunnel1': [(x1, y1), (x2, y2)], ..., 'length': {'Tunnel1': 'laenge_wert', ...}} + """ + ret = {} + tunnel_length = {} + + for insert, pos in zip(all_inserts, all_positions): + if "NAME" not in insert: # Tunnel haben immer einen eindeutigen Namen + continue + + ld, id_ = get_tunnel_position_of_symbol(insert, pos) + if not ld or not id_: + continue + + # Check if id_ matches Tunnel-Pattern from BMK.cfg + if not matches_tunnel_pattern(id_): + continue + + # Sammle alle Positionen für den gleichen Tunnelnamen + if id_ not in ret: + ret[id_] = [] + ret[id_].append(ld["pos"]) + + # Sammle Längeninformation falls vorhanden + if "laenge" in ld: + tunnel_length[id_] = ld["laenge"] + + # Füge Längeninformation hinzu, falls welche gefunden wurde + if len(tunnel_length) > 0: + ret['length'] = tunnel_length + + return ret + + def get_subdistributor_positions_from_entities(entities, dist2sensors: dict) -> dict: """Ermittelt Unterverteiler-Positionen aus einer beliebigen Entity-Iterable. @@ -830,7 +919,11 @@ if __name__ == '__main__': t_entities = iterdxf.modelspace(dxf_path) else: t_entities = msp.query('MTEXT') + # die Infos aus den Texten (alter Stil) res_tunnel = get_tunnel_positions_from_entities(t_entities) + # die Infos aus den Blöcken (neuer Stil) + symbol_tunnels = get_tunnel_positions_from_symbols(all_inserts, all_positions) + res_tunnel = merge_two_dicts(res_tunnel, symbol_tunnels) output_results['tunnels'] = res_tunnel if args.console: print(to_json(res_tunnel))