File: hpgl2_export.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 (182 lines) | stat: -rw-r--r-- 5,712 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Copyright (c) 2023, Manfred Moitzi
# License: MIT License
import pathlib
import time

import ezdxf
from ezdxf.addons.drawing import Frontend, RenderContext
from ezdxf.addons.drawing.config import (
    Configuration,
    BackgroundPolicy,
    ColorPolicy,
    LineweightPolicy,
)
from ezdxf.addons.drawing import hpgl2, layout
from ezdxf.math import global_bspline_interpolation

CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = pathlib.Path(".")
EXAMPLE_DXF = pathlib.Path(__file__).parent.parent.parent.parent / "examples_dxf"

# ------------------------------------------------------------------------------
# This example shows how to export the modelspace by the drawing add-on and the
# native HPGL2 backend.
#
# docs: https://ezdxf.mozman.at/docs/addons/drawing.html
# ------------------------------------------------------------------------------

CADKIT = "CADKitSamples"
CADKIT_FILES = [
    "A_000217.dxf",
    "AEC Plan Elev Sample.dxf",
    "backhoe.dxf",
    "BIKE.DXF",
    "Controller-M128-top.dxf",
    "drilling_machine.dxf",
    "fanuc-430-arm.dxf",
    "Floor plan.dxf",
    "gekko.DXF",
    "house design for two family with comman staircasedwg.dxf",
    "house design.dxf",
    "kit-dev-coldfire-xilinx_5213.dxf",
    "Lock-Off.dxf",
    "Mc Cormik-D3262.DXF",
    "Mechanical Sample.dxf",
    "Nikon_D90_Camera.DXF",
    "pic_programmer.dxf",
    "Proposed Townhouse.dxf",
    "Shapefont.dxf",
    "SMA-Controller.dxf",
    "Tamiya TT-01.DXF",
    "Tyrannosaurus.DXF",
    "WOOD DETAILS.dxf",
]

wave = [
    (0.0, 0.0),
    (0.897597901, 0.78183148),
    (1.79519580, 0.97492791),
    (2.69279370, 0.433883739),
    (3.59039160, -0.43388373),
    (4.48798950, -0.97492791),
    (5.38558740, -0.78183148),
    (6.28318530, 0.0),
]


def export(filepath: pathlib.Path, layout_names=("Model",)):
    print(f"\nprocessing: {filepath.name}")
    t0 = time.perf_counter()
    doc = ezdxf.readfile(filepath)
    t1 = time.perf_counter()
    print(f"loading time: {t1 - t0: .3f} seconds")
    for layout_name in layout_names:
        outname = filepath.stem + f"-[{layout_name}]" + ".plt"
        print(outname)
        t1 = time.perf_counter()
        if layout_name == "Model":
            dxf_layout = doc.modelspace()
            page = layout.Page(
                0,  # auto-detect
                0,  # auto-detect
                layout.Units.mm,  # 1 drawing unit = 1mm
                layout.Margins.all(10),  # 10mm margin on all sides of the page
                max_width=1189,  # limit page width to 1189mm
                max_height=841,  # limit page height to 841mm
            )
            settings = layout.Settings()
        else:
            try:
                dxf_layout = doc.paperspace(layout_name)
            except KeyError:
                print(f"Layout '{layout_name}' not found")
                continue
            page = layout.Page.from_dxf_layout(dxf_layout)
            settings = layout.Settings(
                fit_page=False,
                scale=dxf_layout.get_plot_unit_scale_factor(),
            )

        backend = hpgl2.PlotterBackend()
        Frontend(
            RenderContext(doc),
            backend,
            config=Configuration(
                color_policy=ColorPolicy.BLACK,
                lineweight_policy=LineweightPolicy.ABSOLUTE,
            ),
        ).draw_layout(dxf_layout)

        data = backend.get_bytes(page, settings=settings, curves=True, decimal_places=0)
        t2 = time.perf_counter()
        print(f"render time: {t2 - t1: .3f} seconds")
        (CWD / outname).write_bytes(data)


def export_cadkit_samples():
    for name in CADKIT_FILES[:]:
        filename = ezdxf.options.test_files_path / CADKIT / name
        export(filename)


def simple():
    doc = ezdxf.new()
    msp = doc.modelspace()
    s = global_bspline_interpolation(wave)
    msp.add_spline(dxfattribs={"color": 2}).apply_construction_tool(s)
    msp.add_lwpolyline(wave, dxfattribs={"color": 3})

    backend = hpgl2.PlotterBackend()
    Frontend(RenderContext(doc), backend).draw_layout(msp)
    data = backend.get_bytes(layout.Page(100, 40, layout.Units.mm))
    (CWD / "wave.plt").write_bytes(data)


def triangle():
    doc = ezdxf.new()
    msp = doc.modelspace()
    msp.add_line((0, 100), (500, 100))
    msp.add_solid([(0, 0), (500, 0), (250, 500)], dxfattribs={"color": 1})

    backend = hpgl2.PlotterBackend()
    Frontend(RenderContext(doc), backend).draw_layout(msp)
    data = backend.get_bytes(layout.Page(0, 0))
    (CWD / "triangle.plt").write_bytes(data)


def text():
    doc = ezdxf.new()
    doc.styles.add("ARIAL", font="Arial.ttf")
    msp = doc.modelspace()
    msp.add_lwpolyline([(0, 0), (1000, 0), (1000, 800), (0, 800)], close=True)
    msp.add_text("0123456789", height=2.5, dxfattribs={"style": "ARIAL"}).set_placement(
        (100, 400)
    )
    backend = hpgl2.PlotterBackend()
    Frontend(RenderContext(doc), backend).draw_layout(msp)
    data = backend.low_quality(layout.Page(0, 0))
    (CWD / "text.plt").write_bytes(data)


def transparency():
    doc = ezdxf.readfile(EXAMPLE_DXF / "transparency_checker.dxf")
    msp = doc.modelspace()
    backend = hpgl2.PlotterBackend()
    Frontend(RenderContext(doc), backend).draw_layout(msp)
    data = backend.get_bytes(
        layout.Page(0, 0, layout.Units.mm), settings=layout.Settings(scale=10)
    )
    (CWD / "transparency.plt").write_bytes(data)


if __name__ == "__main__":
    text()
    # export(
    #     pathlib.Path(r"C:\Source\dxftest\CADKitSamples\A_000217.dxf"),
    #     ["Model"]  #, "PLAN", "SECTION"],
    # )
    export_cadkit_samples()
    # simple()
    # transparency()
    # triangle()