Kabeldaten auch extrahiert
This commit is contained in:
+107
-92
@@ -36,115 +36,128 @@ def merge_two_dicts(x, y):
|
|||||||
z.update(y)
|
z.update(y)
|
||||||
return z
|
return z
|
||||||
|
|
||||||
def get_input_positions(msp: ezdxf.document.Drawing.modelspace):
|
def get_attributes_of_insert(insert):
|
||||||
"""hole alle Positionen der Eingänge !!Sensor Positionen erst nach Offsett-Addition "Mitte-Mitte"!!
|
|
||||||
"""
|
|
||||||
allIds = dict()
|
|
||||||
SpecialKeys = ["MB", # Separator
|
SpecialKeys = ["MB", # Separator
|
||||||
"MA", # Motor
|
"MA", # Motor
|
||||||
"BG", # Stausensor
|
"BG", # Stausensor
|
||||||
"FC" # Motorschutzschalter
|
"BP" # Schalter Druckluft
|
||||||
]
|
]
|
||||||
|
|
||||||
# Über alle Blockreferenzen (INSERT) im Modelspace laufen
|
KabelKey = "WD_"
|
||||||
for insert in msp.query('INSERT'):
|
DropKeys = [
|
||||||
|
"FC", # Motorschutzschalter
|
||||||
|
"PF", # Leuchtmelder
|
||||||
|
"SF",
|
||||||
|
"DI", # Feedback vom Gerät
|
||||||
|
"QA", # Hauptschütz
|
||||||
|
"SF" # Drucktaster
|
||||||
|
]
|
||||||
|
id = ""
|
||||||
|
ld = dict()
|
||||||
|
for attrib in insert.attribs:
|
||||||
|
attr_tag = attrib.dxf.tag
|
||||||
|
attr_text = attrib.dxf.text
|
||||||
if len(insert.attribs) == 0:
|
if len(insert.attribs) == 0:
|
||||||
continue # Überspringe Blöcke ohne Attribute
|
continue # Überspringe Blöcke ohne Attribute
|
||||||
id = ""
|
#print(f"Attribut Name: {attrib.dxf.tag}, Wert: {attrib.dxf.text}")
|
||||||
ld = dict()
|
ld[attr_tag] = attr_text
|
||||||
for attrib in insert.attribs:
|
typ = 'unknown'
|
||||||
attr_tag = attrib.dxf.tag
|
if attr_tag == "IO":
|
||||||
attr_text = attrib.dxf.text
|
id = attr_text
|
||||||
if len(insert.attribs) == 0:
|
#print(f"-- coord io {id}--: {attrib.dxf.insert}") # position des Blocks
|
||||||
continue # Überspringe Blöcke ohne Attribute
|
pos = attrib.dxf.insert #Position aufzeichnen und bei Bedarf später mit REAL_POS überschreiben
|
||||||
#print(f"Attribut Name: {attrib.dxf.tag}, Wert: {attrib.dxf.text}")
|
ld["pos"] = (round(pos.x, 1), round(pos.y, 1))
|
||||||
ld[attr_tag] = attr_text
|
typ = "Sensor"
|
||||||
if attr_tag == "IO":
|
|
||||||
|
if attr_tag == "B":
|
||||||
|
# Suche nach Sensoren
|
||||||
|
for spec in SpecialKeys:
|
||||||
|
if spec in attr_text:
|
||||||
|
id = attr_text
|
||||||
|
typ = "Sensor"
|
||||||
|
|
||||||
|
# Suche nach Kabel
|
||||||
|
if KabelKey in attr_text:
|
||||||
id = attr_text
|
id = attr_text
|
||||||
#print(f"-- coord io {id}--: {attrib.dxf.insert}") # position des Blocks
|
typ = "Kabel"
|
||||||
pos = attrib.dxf.insert #Position aufzeichnen und bei Bedarf später mit REAL_POS überschreiben
|
|
||||||
ld["pos"] = (round(pos.x, 1), round(pos.y, 1))
|
# suche nach Items die wir nicht weiter verfolgen
|
||||||
|
for dropkey in DropKeys:
|
||||||
|
if dropkey in attr_text:
|
||||||
|
typ = "Schaltschrankelement"
|
||||||
|
id = attr_text
|
||||||
|
return {}, id, typ
|
||||||
|
|
||||||
if attr_tag == "B":
|
if attr_tag == "REALE_POSITION" and attr_text == "x":
|
||||||
for spec in SpecialKeys:
|
#print(f"-- coord real --: {attrib.dxf.insert}")
|
||||||
if spec in attr_text:
|
pos = attrib.dxf.insert #Position Ecke unten links von "x"-Marker auslesen
|
||||||
id = attr_text
|
|
||||||
#print(f"-- coord {attrib.dxf.text} --: {attrib.dxf.insert}")
|
# Hoehe und Breite von "x" addieren, um Mittelpunkt zu finden
|
||||||
if attr_tag == "REALE_POSITION" and attr_text == "x":
|
breite_marker = config.getfloat("GetPos-Geom-Sensor", "Breite")
|
||||||
#print(f"-- coord real --: {attrib.dxf.insert}")
|
hoehe_marker = config.getfloat("GetPos-Geom-Sensor", "Hoehe")
|
||||||
pos = attrib.dxf.insert #Position Ecke unten links von "x"-Marker auslesen
|
midx = pos[0] + breite_marker * 0.5
|
||||||
|
midy = pos[1] + hoehe_marker * 0.5
|
||||||
# Hoehe und Breite von "x" addieren, um Mittelpunkt zu finden
|
ld["pos"] = (round(midx, 1), round(midy, 1))
|
||||||
breite_marker = config.getfloat("GetPos-Geom-Sensor", "Breite")
|
typ = "Sensor"
|
||||||
hoehe_marker = config.getfloat("GetPos-Geom-Sensor", "Hoehe")
|
|
||||||
midx = pos[0] + breite_marker * 0.5
|
return ld, id, typ
|
||||||
midy = pos[1] + hoehe_marker * 0.5
|
|
||||||
ld["pos"] = (round(midx, 1), round(midy, 1))
|
|
||||||
|
|
||||||
|
def get_input_positions(msp: ezdxf.document.Drawing.modelspace)-> tuple:
|
||||||
|
"""hole alle Positionen der Eingänge !!Sensor Positionen erst nach Offsett-Addition "Mitte-Mitte"!!
|
||||||
|
"""
|
||||||
|
allSensors = dict()
|
||||||
|
allCables = dict()
|
||||||
|
|
||||||
# Nur wenn eine ID vorhanden ist, und eine gültige Position existiert
|
# Über alle Blockreferenzen (INSERT) im Modelspace laufen
|
||||||
|
for insert in msp.query('INSERT'):
|
||||||
|
# Überspringe Blöcke ohne Attribute
|
||||||
|
if len(insert.attribs) == 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
ld, id, typ = get_attributes_of_insert(insert)
|
||||||
|
|
||||||
|
# Nur wenn eine ID vorhanden ist, und eine gültige Position existiert
|
||||||
|
if typ == "Sensor":
|
||||||
if id and "pos" in ld and isinstance(ld["pos"], tuple) and len(ld["pos"]) == 2:
|
if id and "pos" in ld and isinstance(ld["pos"], tuple) and len(ld["pos"]) == 2:
|
||||||
if id in allIds:
|
if id in allSensors:
|
||||||
allIds[id] = merge_two_dicts(allIds[id], ld) #Kombiniert alle infos aus dxf und "pos"
|
allSensors[id] = merge_two_dicts(allSensors[id], ld) #Kombiniert alle infos aus dxf und "pos"
|
||||||
else:
|
else:
|
||||||
allIds[id] = ld
|
allSensors[id] = ld
|
||||||
return allIds
|
elif typ == "Kabel":
|
||||||
|
allCables[id] = ld
|
||||||
|
else:
|
||||||
|
# Falls das Objekt ein Teil des Schaltschranks o.ä ist, dann nichts merken
|
||||||
|
pass
|
||||||
|
return allSensors, allCables
|
||||||
|
|
||||||
def get_input_positions_iter(dxf_path) -> dict:
|
def get_input_positions_iter(dxf_path) -> tuple:
|
||||||
"""
|
"""
|
||||||
Iterative Version für große DXF-Dateien mit iterdxf (kein Context Manager!).
|
Iterative Version für große DXF-Dateien mit iterdxf (kein Context Manager!).
|
||||||
"""
|
"""
|
||||||
|
allSensors = dict()
|
||||||
SpecialKeys = ["MB", "MA", "BG", "FC"]
|
allCables = dict()
|
||||||
allIds = dict()
|
|
||||||
|
|
||||||
|
|
||||||
for insert in iterdxf.modelspace(dxf_path):
|
for insert in iterdxf.modelspace(dxf_path):
|
||||||
if insert.dxftype() != 'INSERT':
|
if insert.dxftype() != 'INSERT':
|
||||||
continue
|
continue
|
||||||
|
|
||||||
attribs = {att.dxf.tag: att.dxf.text for att in insert.attribs or []}
|
ld, id, typ = get_attributes_of_insert(insert)
|
||||||
id = ""
|
|
||||||
ld = dict()
|
|
||||||
|
|
||||||
for attrib in insert.attribs:
|
# Nur wenn eine ID vorhanden ist, und eine gültige Position existiert
|
||||||
attr_tag = attrib.dxf.tag
|
if typ == "Sensor":
|
||||||
attr_text = attrib.dxf.text
|
|
||||||
if len(insert.attribs) == 0:
|
|
||||||
continue # Überspringe Blöcke ohne Attribute
|
|
||||||
#print(f"Attribut Name: {attrib.dxf.tag}, Wert: {attrib.dxf.text}")
|
|
||||||
ld[attr_tag] = attr_text
|
|
||||||
if attr_tag == "IO":
|
|
||||||
id = attr_text
|
|
||||||
#print(f"-- coord io {id}--: {attrib.dxf.insert}") # position des Blocks
|
|
||||||
pos = attrib.dxf.insert #Position aufzeichnen und bei Bedarf später mit REAL_POS überschreiben
|
|
||||||
ld["pos"] = (round(pos.x, 1), round(pos.y, 1))
|
|
||||||
|
|
||||||
if attr_tag == "B":
|
|
||||||
for spec in SpecialKeys:
|
|
||||||
if spec in attr_text:
|
|
||||||
id = attr_text
|
|
||||||
#print(f"-- coord {attrib.dxf.text} --: {attrib.dxf.insert}")
|
|
||||||
if attr_tag == "REALE_POSITION" and attr_text == "x":
|
|
||||||
#print(f"-- coord real --: {attrib.dxf.insert}")
|
|
||||||
pos = attrib.dxf.insert #Position Ecke unten links von "x"-Marker auslesen
|
|
||||||
|
|
||||||
# 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")
|
|
||||||
pos_midx = pos.x + breite_marker*0.5
|
|
||||||
pos_midy = pos.y + hoehe_marker*0.5
|
|
||||||
|
|
||||||
ld["pos"] = (round(pos_midx, 1), round(pos_midy, 1))
|
|
||||||
|
|
||||||
# Nur wenn eine ID vorhanden ist, und eine gültige Position existiert
|
|
||||||
if id and "pos" in ld and isinstance(ld["pos"], tuple) and len(ld["pos"]) == 2:
|
if id and "pos" in ld and isinstance(ld["pos"], tuple) and len(ld["pos"]) == 2:
|
||||||
if id in allIds:
|
if id in allSensors:
|
||||||
allIds[id] = merge_two_dicts(allIds[id], ld) #Kombiniert alle infos aus dxf und "pos"
|
allSensors[id] = merge_two_dicts(allSensors[id], ld) #Kombiniert alle infos aus dxf und "pos"
|
||||||
else:
|
else:
|
||||||
allIds[id] = ld
|
allSensors[id] = ld
|
||||||
|
elif typ == "Kabel":
|
||||||
|
allCables[id] = ld
|
||||||
|
else:
|
||||||
|
# Falls das Objekt ein Teil des Schaltschranks o.ä ist, dann nichts merken
|
||||||
|
pass
|
||||||
|
|
||||||
return allIds
|
return allSensors, allCables
|
||||||
|
|
||||||
def create_mappings(positions:dict) -> dict:
|
def create_mappings(positions:dict) -> dict:
|
||||||
unterverteiler_pfad = ""
|
unterverteiler_pfad = ""
|
||||||
@@ -435,7 +448,8 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
res_pos = dict()
|
res_sens = dict()
|
||||||
|
res_cables = dict()
|
||||||
res_dist = dict()
|
res_dist = dict()
|
||||||
res_rac = dict()
|
res_rac = dict()
|
||||||
res_mappings = dict()
|
res_mappings = dict()
|
||||||
@@ -452,17 +466,18 @@ if __name__ == '__main__':
|
|||||||
if args.sensors:
|
if args.sensors:
|
||||||
# Sensoren auslesen
|
# Sensoren auslesen
|
||||||
if use_iter:
|
if use_iter:
|
||||||
res_pos = get_input_positions_iter(dxf_path)
|
res_sens, res_cables = get_input_positions_iter(dxf_path)
|
||||||
else:
|
else:
|
||||||
res_pos = get_input_positions(msp)
|
res_sens, res_cables = get_input_positions(msp)
|
||||||
|
|
||||||
output_results['sensors'] = res_pos
|
output_results['sensors'] = res_sens
|
||||||
|
output_results['cables'] = res_cables
|
||||||
|
|
||||||
if args.console:
|
if args.console:
|
||||||
print(to_json(res_pos))
|
print(to_json(res_sens))
|
||||||
|
|
||||||
# Mapping zu Sensoren auslesen
|
# Mapping zu Sensoren auslesen
|
||||||
(res_mappings, warnings) = create_mappings(res_pos)
|
(res_mappings, warnings) = create_mappings(res_sens)
|
||||||
output_results['mappings'] = res_mappings
|
output_results['mappings'] = res_mappings
|
||||||
if args.console:
|
if args.console:
|
||||||
print(to_json(res_mappings))
|
print(to_json(res_mappings))
|
||||||
@@ -503,7 +518,7 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
if args.write:
|
if args.write:
|
||||||
basename = os.path.splitext(args.write)[0]
|
basename = os.path.splitext(args.write)[0]
|
||||||
res_not_found = check_existance(res_mappings, res_dist, res_pos)
|
res_not_found = check_existance(res_mappings, res_dist, res_sens)
|
||||||
output_results["not_found"] = res_not_found
|
output_results["not_found"] = res_not_found
|
||||||
write_results(to_json(output_results), work_dir, f"{basename}.json")
|
write_results(to_json(output_results), work_dir, f"{basename}.json")
|
||||||
zeitende = time.time()
|
zeitende = time.time()
|
||||||
|
|||||||
Reference in New Issue
Block a user