diff --git a/lib/drawdxf.py b/lib/drawdxf.py index 67920fc..eb830c3 100644 --- a/lib/drawdxf.py +++ b/lib/drawdxf.py @@ -148,6 +148,94 @@ def check_file_in_work(work_dir, filename): mypath = filename return (mypath, fexists) +##### + +def collect_text_entities(msp): + texts = [] + for entity in msp.query("TEXT MTEXT"): + try: + text = entity.dxf.text + pos = tuple(round(c, 4) for c in entity.dxf.insert) + layer = entity.dxf.layer + texts.append((text, pos, layer)) + except Exception as e: + print(f"Fehler beim Lesen eines Textobjekts: {e}") + return texts + +def compare_texts(source_texts, target_texts): + missing_in_target = [t for t in source_texts if t not in target_texts] + extra_in_target = [t for t in target_texts if t not in source_texts] + + if not missing_in_target and not extra_in_target: + print("✅ Alle TEXT/MTEXT-Objekte wurden korrekt kopiert.") + else: + if missing_in_target: + print("\n❌ Fehlende Texte im Ziel:") + for text in missing_in_target: + print(f" - {text}") + if extra_in_target: + print("\n⚠️ Zusätzliche Texte im Ziel:") + for text in extra_in_target: + print(f" - {text}") + +def collect_block_references(doc): + msp = doc.modelspace() + blocks = [] + for insert in msp.query("INSERT"): + try: + name = insert.dxf.name + pos = tuple(round(c, 4) for c in insert.dxf.insert) + layer = insert.dxf.layer + blocks.append((name, pos, layer)) + except Exception as e: + print(f"Fehler beim Lesen eines INSERT-Objekts: {e}") + return blocks + +def serialize_entity(e): + """Wandelt eine Entity in ein vergleichbares Tupel um""" + dxfattribs = {} + for key in e.dxf.__dir__(): + try: + value = getattr(e.dxf, key) + if callable(value) or key.startswith("_"): + continue + if isinstance(value, (list, tuple)): + value = tuple(round(v, 4) for v in value) + elif isinstance(value, float): + value = round(value, 4) + dxfattribs[key] = value + except Exception: + continue + return (e.dxftype(), dxfattribs) + +def compare_blocks(source_doc, target_doc, used_block_names): + for name in used_block_names: + if name not in source_doc.blocks or name not in target_doc.blocks: + print(f"❌ Block '{name}' fehlt in einer der Dateien.") + continue + + source_block = source_doc.blocks.get(name) + target_block = target_doc.blocks.get(name) + + source_entities = [serialize_entity(e) for e in source_block] + target_entities = [serialize_entity(e) for e in target_block] + + missing_in_target = [e for e in source_entities if e not in target_entities] + extra_in_target = [e for e in target_entities if e not in source_entities] + + if not missing_in_target and not extra_in_target: + print(f"✅ Block '{name}' ist identisch.") + else: + print(f"\n🔍 Block '{name}' hat Unterschiede:") + if missing_in_target: + print(f" ❌ Fehlt im Ziel:") + for e in missing_in_target: + print(f" - {e}") + if extra_in_target: + print(f" ⚠️ Zusätzlich im Ziel:") + for e in extra_in_target: + print(f" - {e}") + def copy_layers_into_dxf_by_filter(dxf_source: ezdxf.document.Drawing, dxf_target:ezdxf.document.Drawing): msp_source = dxf_source.modelspace() @@ -184,33 +272,24 @@ def copy_layers_into_dxf_by_filter(dxf_source: ezdxf.document.Drawing, dxf_targe if style.dxf.name not in dxf_target.styles: dxf_target.styles.new(name=style.dxf.name) - # 2. Bestimmte alle Blockdefinitionen - used_block_names = set() - for insert in msp_source.query("INSERT"): - block_name = insert.dxf.name - used_block_names.add(block_name) - # print(f"Blockname: {block_name} | Layer: {insert.dxf.layer} | Position: {insert.dxf.insert}") + # 2. alle Blockdefinitionen kopieren die benötigt werden + used_blocks = {ins.dxf.name for ins in msp_source.query("INSERT")} + for name in used_blocks: + # skip vorhandene + if name in dxf_target.blocks: + continue + src_block = dxf_source.blocks.get(name) + # neues BLOCK-Objekt anlegen mit Base-Point + new_block = dxf_target.blocks.new(name=name, base_point=src_block.base_point) + # **ALLE** Entities im Block kopieren + for ent in src_block: + new_block.add_entity(ent.copy()) - # block = dxf_source.blocks.get(block_name) - # print(f"Inhalte von Block '{block_name}':") - # for entity in block: - # print(f" Typ: {entity.dxftype()}, Layer: {entity.dxf.layer}") - # for attrib in insert.attribs: - # if len(insert.attribs) == 0: - # continue # Überspringe Blöcke ohne Attribute - # print(f"Attribut Name: {attrib.dxf.tag}, Wert: {attrib.dxf.text}") - # has_mtext = any(e.dxftype() == "MTEXT" for e in block) - # if has_mtext: - # print(f"Block '{block_name}' enthält MTEXT:") + # 3. Jetzt die INSERTs in den Modelspace kopieren + for ins in msp_source.query("INSERT"): + new_ins = ins.copy() + msp_target.add_entity(new_ins) - # 3. Blockdefinitionen kopieren - for block_name in used_block_names: - if block_name not in dxf_target.blocks: - block = dxf_source.blocks.get(block_name) - new_block = dxf_target.blocks.new(name=block_name) - new_block.block.dxf.base_point = block.block.dxf.base_point - for e in block: - new_block.add_entity(e.copy()) # 4. Filter-Layernamen bestimmen for layername in layernames: @@ -230,6 +309,14 @@ def copy_layers_into_dxf_by_filter(dxf_source: ezdxf.document.Drawing, dxf_targe entities = msp_source.query(f"*[layer=='{layername}']") for entity in entities: msp_target.add_entity(entity.copy()) + + texts_source = collect_text_entities(msp_source) + texts_target = collect_text_entities(msp_target) + + compare_texts(texts_source, texts_target) + + used_block_names = set(insert.dxf.name for insert in dxf_source.modelspace().query("INSERT")) + compare_blocks(dxf_source, dxf_target, used_block_names) if __name__ == '__main__':