|
| 1 | +# Copyright (c) 2024, Manfred Moitzi |
| 2 | +# License: MIT License |
| 3 | +from __future__ import annotations |
| 4 | +from typing import cast, TYPE_CHECKING |
| 5 | +from pathlib import Path |
| 6 | +import ezdxf |
| 7 | +from ezdxf import colors |
| 8 | +from ezdxf.gfxattribs import GfxAttribs |
| 9 | +from ezdxf.math import Vec2 |
| 10 | +from ezdxf.entities import Viewport |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from ezdxf.layouts import Modelspace |
| 14 | + |
| 15 | +CWD = Path("~/Desktop/Outbox").expanduser() |
| 16 | +if not CWD.exists(): |
| 17 | + CWD = Path(".") |
| 18 | + |
| 19 | + |
| 20 | +def render_viewport_modelspace_limits(vp: Viewport, msp: Modelspace, dxfattribs): |
| 21 | + print(f" {str(vp)}") |
| 22 | + x0, y0, x1, y1 = vp.get_modelspace_limits() |
| 23 | + vertices = Vec2(x0, y0), Vec2(x1, y0), Vec2(x1, y1), Vec2(x0, y1) |
| 24 | + msp.add_lwpolyline(vertices, close=True, dxfattribs=dxfattribs) |
| 25 | + |
| 26 | + |
| 27 | +def main(filename: str) -> None: |
| 28 | + print(f"loading DXF: {filename}") |
| 29 | + doc = ezdxf.readfile(filename) |
| 30 | + msp = doc.modelspace() |
| 31 | + |
| 32 | + for num, name in enumerate(doc.layout_names_in_taborder()): |
| 33 | + if name == "Model": |
| 34 | + continue |
| 35 | + print(f"processing layout: {name}") |
| 36 | + layer_name = f"EZDXF_VP_BORDER_{num}" |
| 37 | + doc.layers.add(layer_name, color=colors.MAGENTA) |
| 38 | + dxfattribs = GfxAttribs(layer=layer_name) |
| 39 | + psp = doc.layout(name) |
| 40 | + for viewport in psp.query("VIEWPORT")[1:]: |
| 41 | + # skip the first viewport - defines the paperspace representation in |
| 42 | + # the CAD application |
| 43 | + render_viewport_modelspace_limits( |
| 44 | + cast(Viewport, viewport), msp, dxfattribs=dxfattribs |
| 45 | + ) |
| 46 | + |
| 47 | + filename = CWD / "vp_limits.dxf" |
| 48 | + print(f"saving DXF: {filename}") |
| 49 | + doc.saveas(filename) |
| 50 | + print("finish.") |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + main(r"C:\Users\mozman\Desktop\Now\ezdxf\1298\problematic_viewports.dxf") |
0 commit comments