File: diff_dxf_files.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (55 lines) | stat: -rw-r--r-- 1,658 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#  Copyright (c) 2023, Manfred Moitzi
#  License: MIT License
from typing import Optional, Iterable

from ezdxf.lldxf.tags import Tags
from ezdxf.lldxf.tagger import tag_compiler
from ezdxf.tools.rawloader import raw_structure_loader
from ezdxf.tools.difftags import diff_tags, print_diff, OpCode

FILE1 = r"C:\Users\mozman\Desktop\Outbox\906_polylines.dxf"
FILE2 = r"C:\Users\mozman\Desktop\Outbox\906_copy.dxf"


def get_handle(tags: Tags):
    try:
        return tags.get_handle()
    except ValueError:
        return "0"


def cmp_section(sec1, sec2):
    for e1 in sec1:
        handle = get_handle(e1)
        if handle is None or handle == "0":
            continue
        e2 = entity_tags(sec2, handle)
        if e2 is None:
            print(f"entity handle #{handle} not found in second file")
            continue
        e1 = Tags(tag_compiler(iter(e1)))
        a, b = e2, e1
        diff = list(diff_tags(a, b, ndigits=6))
        has_diff = any(op.opcode != OpCode.equal for op in diff)
        if has_diff:
            print("-"*79)
            print(f"comparing {e1.dxftype()}(#{handle})")
            print_diff(a, b, diff)


def cmp_dxf_files(filename1: str, filename2: str):
    doc1 = raw_structure_loader(filename1)
    doc2 = raw_structure_loader(filename2)
    for section in ["TABLES", "BLOCKS", "ENTITIES", "OBJECTS"]:
        cmp_section(doc1[section], doc2[section])


def entity_tags(entities: Iterable[Tags], handle: str) -> Optional[Tags]:
    for e in entities:
        if get_handle(e) == handle:
            return Tags(tag_compiler(iter(e)))
    return None


if __name__ == "__main__":
    cmp_dxf_files(FILE1, FILE2)