File: make_polylines.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 (59 lines) | stat: -rw-r--r-- 1,728 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
56
57
58
59
# Copyright (c) 2025, Manfred Moitzi
# License: MIT License
from __future__ import annotations
from pathlib import Path

import ezdxf

from ezdxf import colors
from ezdxf import edgeminer as em
from ezdxf import edgesmith as es

CWD = Path(__file__).parent
OUTBOX = Path("~/Desktop/Outbox").expanduser()


def load(filename: str) -> list[em.Edge]:
    doc = ezdxf.readfile(CWD / filename)
    msp = doc.modelspace()
    edges = list(es.edges_from_entities_2d(msp))
    return edges


def make_polyline_with_arcs(edges: list[em.Edge], outname: str) -> None:
    doc = ezdxf.new()
    doc.layers.add("LWPOLYLINE", color=colors.RED)
    msp = doc.modelspace()
    dxfattribs = {"layer": "LWPOLYLINE"}
    deposit = em.Deposit(edges)
    for loop in em.find_all_simple_chains(deposit):
        polyline = es.lwpolyline_from_chain(loop, dxfattribs=dxfattribs, max_sagitta=.1)
        msp.add_entity(polyline)
    doc.saveas(OUTBOX / outname)


def make_polyline_without_arcs(edges: list[em.Edge], outname: str) -> None:
    doc = ezdxf.new()
    doc.layers.add("LWPOLYLINE", color=colors.RED)
    msp = doc.modelspace()
    dxfattribs = {"layer": "LWPOLYLINE"}
    deposit = em.Deposit(edges)
    for loop in em.find_all_simple_chains(deposit):
        # 2D path as intermediate_step:
        path2d = es.path2d_from_chain(loop)
        # flatten everything:
        msp.add_lwpolyline(path2d.flattening(distance=0.1), dxfattribs=dxfattribs)
    doc.saveas(OUTBOX / outname)


FILE_6 = "6_closed_loop_with_arcs.dxf"


def main():
    edges = load(FILE_6)
    make_polyline_with_arcs(edges, "make_polyline_with_arcs.dxf")
    make_polyline_without_arcs(edges, "make_polyline_without_arcs.dxf")


if __name__ == "__main__":
    main()