From c2485849a67a3a50347584f423d9af258f1a13e6 Mon Sep 17 00:00:00 2001 From: lertlmaier Date: Mon, 23 Jun 2025 15:55:00 +0200 Subject: [PATCH] Erste Version der Excel Ausgabe einer BOM --- lib/drawdxf.py | 60 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/lib/drawdxf.py b/lib/drawdxf.py index 565376b..0a8be5c 100644 --- a/lib/drawdxf.py +++ b/lib/drawdxf.py @@ -294,14 +294,15 @@ def mark_missings(all_artnrs): if artnr not in bezeichner_cfg["Sivasnummern"]: bezeichner_cfg["Missing"][artnr] = "" -def write_excel_from_json(plines:Polylines, sens2cable: dict, outpath:str): - wb = Workbook() +def write_excel_from_json(plines:Polylines, sens2cable: dict, outpath:str, with_bom = True): + wb1 = Workbook() # Dicts für Anzahl bzw kummulierte Länge count_summary = defaultdict(int) length_summary = defaultdict(float) + sensor_summary = defaultdict(int) #Worksheet 1 - Kabellängen nach Kabel-ID - ws1 = wb.active + ws1 = wb1.active ws1.title = "Length by ID" ws1.append(["Cable-ID", "True Length (m)", "Cable-ArtNr", "Cable-Name (short)"]) @@ -310,8 +311,17 @@ def write_excel_from_json(plines:Polylines, sens2cable: dict, outpath:str): ws1.column_dimensions['C'].width = 15 ws1.column_dimensions['D'].width = 25 + seen_sensors = set() + for pl in plines.kabel: length = round(pl.length /1000 , 1) # Umrechnung von mm in m + + # Sensor Artikelnummern sammeln + sensor_name = pl.id.split("-")[-1] # z.B. "BX0001" + if sensor_name not in seen_sensors: + seen_sensors.add(sensor_name) + sensor_summary[pl.s_artinr] += 1 + for artnr in sens2cable[pl.id]: cable_name = "" if artnr.isdigit(): @@ -325,7 +335,7 @@ def write_excel_from_json(plines:Polylines, sens2cable: dict, outpath:str): count_summary[artnr] += 1 # Worksheet 2 - Kabelnummern und Stückzahlen - ws2 = wb.create_sheet("Cables SIVAS") + ws2 = wb1.create_sheet("Cables SIVAS") ws2.append(["Cable-ArtNr", "Amount (pcs)", "Cumm. Length (m)"]) ws2.column_dimensions['A'].width = 20 @@ -344,7 +354,7 @@ def write_excel_from_json(plines:Polylines, sens2cable: dict, outpath:str): if len(plines.errors_sensors) > 0 or len(plines.errors_dists) > 0: # Worksheet 3 - Nicht an Racks gekoppeltes Equipment - ws3 = wb.create_sheet("ERR-Equipment-Connection") + ws3 = wb1.create_sheet("ERR-Equipment-Connection") ws3.append(["Type", "ID", "x", "y"]) ws3.column_dimensions['A'].width = 20 @@ -356,10 +366,9 @@ def write_excel_from_json(plines:Polylines, sens2cable: dict, outpath:str): for error in plines.errors_dists: ws3.append(["Subistributor", error.name, error.coords.x, error.coords.y]) - if len(plines.errors_routing) > 0: # Worksheet 4 - Fehlgeschlagenes Routing - ws4 = wb.create_sheet("ERR-Routing") + ws4 = wb1.create_sheet("ERR-Routing") ws4.append(["Subdistributor", "Sensor / Actuator", "Details"]) ws4.column_dimensions['C'].width = 35 @@ -391,15 +400,46 @@ def write_excel_from_json(plines:Polylines, sens2cable: dict, outpath:str): if len(plines.errors_missing_attributes) > 0: # Worksheet 5 - Fehlende Attribute -> kein Routing - ws5 = wb.create_sheet("ERR-Attributes") + ws5 = wb1.create_sheet("ERR-Attributes") ws5.append(["ID", "Error Detail"]) ws5.column_dimensions['B'].width = 35 for sname, err_msg in plines.errors_missing_attributes.items(): ws5.append([sname, err_msg]) + wb1.save(outpath) + print("Cable-Summary exported to Excel file") - wb.save(outpath) + if with_bom: + wb2 = Workbook() + ws1 = wb2.active + ws1.title = "BOM" + ws1.append(["Type", "Art.-Number", "Name (SIVAS)", "Amount (pcs)", "Length (m)"]) + + # Alle Summaries kombinieren + all_summaries = set(count_summary) | set(length_summary) | set(sensor_summary) + + for artnr in sorted(all_summaries): + name = bezeichner_cfg["Sivasnummern"].get(artnr, "") + count = sensor_summary.get(artnr, count_summary.get(artnr, "")) + length = length_summary.get(artnr, "") + typ = "Sensor" if artnr in sensor_summary else "Kabel" + + if artnr == "": + name = "Keine Artikelnummer vergeben. Layout prüfen." + elif artnr != "" and name =="": + name = (f"Kein Eintrag zu Art.-Nr: {artnr} in bezeichner.cfg.") + ws1.append([ + typ, + artnr, + name, + count, + length if typ == "Kabel" else "" + ]) + + bom_path = outpath.replace("_cables.xlsx", "_BOM.xlsx") + wb2.save(bom_path) + print(f"BOM saved as an excel file") def check_file_in_work(work_dir, filename): fexists = True @@ -617,4 +657,4 @@ if __name__ == '__main__': # 5. Excel schreiben excel_path = os.path.join(work_dir, args.excel) - export_excel(plines, sens2cable, excel_path) \ No newline at end of file + write_excel_from_json(plines, sens2cable, excel_path) \ No newline at end of file