#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ test_kreisel_weichen_json.py - Uebernahme der LISP-Kreisel-Beruehrpunkte. Der LISP-Export (export.lsp csv:kreisel-weichen-schreiben, Kern KreiselInsert.lsp kreisel-bp-*) schreibt kreisel_weichen.json neben export_raw.json. export_csv.py/export_sivas.py lesen sie statt selbst zu rechnen; fehlt sie, bleibt es beim alten Python-Rueckfall. Laeuft ohne BricsCAD. """ import json import os import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib")) from export_neighbors import ( # noqa: E402 lisp_touch_switches, load_lisp_kreisel_weichen, ) import export_sivas # noqa: E402 BOX = (250.0, 100.0, 30.0) BEISPIEL = { "toleranz_mm": 1.0, "beruehrungen": [ # Rad an Laufprofil, Radiale senkrecht (90 Grad) {"a": "0001", "b": "0002", "a_handle": "2A", "b_handle": "2B", "a_typ": "KREISEL", "b_typ": "KREISEL", "x": 22503.49, "y": -2821.76, "z": 5000.0, "art": "Kreis/Tangente", "detail": "AN8/Profil", "achse_von": "0002", "achse_rot_grad": 270.0}, # Zweiter Punkt desselben Paares -> eigener Schluessel {"a": "0001", "b": "0002", "a_handle": "2A", "b_handle": "2B", "a_typ": "KREISEL", "b_typ": "KREISEL", "x": 23000.0, "y": -2821.76, "z": 5000.0, "art": "Kreis/Kreis", "detail": "SP8/AN8", "achse_von": "0001", "achse_rot_grad": 0.0}, # Eckrad ohne ID -> Handle {"a": "", "b": "0003", "a_handle": "3F", "b_handle": "3C", "a_typ": "ECKRAD", "b_typ": "KREISEL", "x": 1.0, "y": 2.0, "z": 0.0, "art": "Kreis/Kreis", "detail": "ECKRAD/AN8", "achse_von": "", "achse_rot_grad": 0.0}, ], "kollisionen": [ {"a": "0004", "b": "0005", "x": 0.0, "y": 0.0, "z": 0.0, "art": "Ueberschneidung", "detail": "Achsabstand 500.0", "achse_von": "0004", "achse_rot_grad": 0.0}, ], } def _schreibe(tmp_path, daten): raw = tmp_path / "export_raw.json" raw.write_text("[]", encoding="utf-8") (tmp_path / "kreisel_weichen.json").write_text(json.dumps(daten), encoding="utf-8") return str(raw) def test_datei_fehlt_liefert_none(tmp_path): raw = tmp_path / "export_raw.json" raw.write_text("[]", encoding="utf-8") assert load_lisp_kreisel_weichen(str(raw)) is None def test_kaputte_datei_liefert_none(tmp_path): raw = tmp_path / "export_raw.json" raw.write_text("[]", encoding="utf-8") (tmp_path / "kreisel_weichen.json").write_text("{kaputt", encoding="utf-8") assert load_lisp_kreisel_weichen(str(raw)) is None def test_switches_aus_lisp(tmp_path): daten = load_lisp_kreisel_weichen(_schreibe(tmp_path, BEISPIEL)) sw = lisp_touch_switches(daten, element_box_mm=BOX) # Kollisionen werden keine Weichen assert len(sw) == 3 assert [s["key"] for s in sw] == ["WEICHE#0001#0002", "WEICHE#0001#0002#2", "WEICHE#3F#0003"] assert sw[0]["nachbarn"] == ["0001", "0002"] assert sw[0]["beruehrung"] == "Kreis/Tangente" assert sw[0]["achse_von"] == "0002" assert (sw[0]["x"], sw[0]["y"], sw[0]["z"]) == (22503.49, -2821.76, 5000.0) # Radiale 270 Grad: Laengsseite (250) quer zur Radialen -> entlang X assert abs(sw[0]["dx"] - 250.0) < 1e-6 and abs(sw[0]["dy"] - 100.0) < 1e-6 # Radiale 0 Grad: Laengsseite entlang Y assert abs(sw[1]["dx"] - 100.0) < 1e-6 and abs(sw[1]["dy"] - 250.0) < 1e-6 # Ohne achse_von -> erster Partner assert sw[2]["achse_von"] == "3F" def test_sivas_4403_zaehlt_beruehrungen(): _, counters = export_sivas.process_blocks([], anzahl_weichen_ils=10) assert counters["anzahl_weichen_ils"] == 10 _, counters = export_sivas.process_blocks([]) assert counters["anzahl_weichen_ils"] == 0 def test_sivas_6216_summenzeile(): """Eine Zeile 6216, je Beruehrpunkt 1x aktiv (3371) + 1x passiv (3372).""" items, _ = export_sivas.process_blocks([], anzahl_weichen_ils=10) weichen = [i for i in items if i["sivasnr"] == "6216"] assert len(weichen) == 1 w = weichen[0] assert w["teileart"] == "ILS 2.0 Weiche" assert w["anzahl"] == 1 assert w["details"] == {"3371": "10", "3372": "10"} # CSV-Zeile laesst sich formatieren assert ';"6216";' in export_sivas.format_csv_line(w) def test_sivas_6216_ohne_weichen_keine_zeile(): items, _ = export_sivas.process_blocks([]) assert not [i for i in items if i["sivasnr"] == "6216"]