From 50845d839f6d8231a76979647748629297d7ae53 Mon Sep 17 00:00:00 2001 From: mozman Date: Tue, 26 Mar 2024 08:59:05 +0100 Subject: [PATCH] add flattening of curved VIEWPORT clipping paths --- examples_dxf/text_in_viewports.dxf | 212 +++++++++++++-------------- notes/pages/CHANGELOG.md | 1 + src/ezdxf/addons/drawing/pipeline.py | 22 ++- 3 files changed, 116 insertions(+), 119 deletions(-) diff --git a/examples_dxf/text_in_viewports.dxf b/examples_dxf/text_in_viewports.dxf index 5de42a2e2..edc7ce43c 100644 --- a/examples_dxf/text_in_viewports.dxf +++ b/examples_dxf/text_in_viewports.dxf @@ -501,11 +501,11 @@ $TDUCREATE 9 $TDUPDATE 40 -2460394.657969283 +2460396.368890197 9 $TDUUPDATE 40 -2460394.616302616 +2460396.32722353 9 $TDINDWG 40 @@ -553,7 +553,7 @@ $SPLINESEGS 9 $HANDSEED 5 -E8 +F5 9 $SURFTAB1 70 @@ -833,7 +833,7 @@ $PEXTMIN 9 $PEXTMAX 10 -270.578743418374 +261.92383953979 20 181.044151849561 30 @@ -2315,17 +2315,17 @@ AcDbViewport 30 0.0 40 -572.797274873525 +409.140910623947 41 -311.052 +222.18 68 1 69 1 12 -151.770517448728 +161.830055873204 22 -91.728486295107 +97.541897177166 13 0.0 23 @@ -2357,7 +2357,7 @@ AcDbViewport 44 0.0 45 -311.052 +222.18 50 0.0 51 @@ -2441,7 +2441,7 @@ AcDbViewport 41 35.401949669218 68 - 5 + 3 69 2 12 @@ -2699,7 +2699,7 @@ AcDbViewport 41 46.946296795953 68 - 6 + 4 69 3 12 @@ -2845,11 +2845,11 @@ AcDbPolyline 10 111.905505644343 20 -47.142617829677 +33.847799369409 10 27.192355560026 20 -47.142617829677 +33.847799369409 10 27.192355560026 20 @@ -2879,7 +2879,7 @@ AcDbViewport 41 57.699359190556 68 - 7 + 5 69 4 12 @@ -3001,7 +3001,7 @@ AcDbViewport 41 35.401949669218 68 - 4 + 6 69 5 12 @@ -3147,7 +3147,7 @@ AcDbText 40 2.5 1 -Convex Viewport +Elliptic Viewport 7 COMIC 100 @@ -3183,11 +3183,11 @@ AcDbText 0 LWPOLYLINE 5 -DB +DE 102 {ACAD_REACTORS 330 -DC +DF 102 } 330 @@ -3201,43 +3201,47 @@ AcDbEntity 100 AcDbPolyline 90 - 7 + 8 70 1 43 0.0 10 -154.917755222758 +133.673900248053 20 -117.168658301852 +62.616536885326 10 -134.198439877058 +133.673900248053 20 -106.415595907249 +4.91717769477 10 -139.181566352606 +261.92383953979 20 -79.664074827991 +4.91717769477 10 -185.603323519554 +261.92383953979 20 -74.943218166945 +62.616536885326 10 -233.336429759014 +244.614031782623 20 -86.483090005056 +62.616536885326 10 -239.368635492572 +244.614031782623 20 -109.825103495781 +34.784477278346 10 -187.701482035574 +159.900881698306 20 -121.889514962898 +34.784477278346 + 10 +159.900881698306 + 20 +62.616536885326 0 VIEWPORT 5 -DC +DF 330 1B 100 @@ -3249,23 +3253,23 @@ AcDbEntity 100 AcDbViewport 10 -186.783537684815 +197.798869893922 20 -98.416366564922 +33.766857290048 30 0.0 40 -105.170195615514 +128.249939291737 41 -46.946296795953 +57.699359190556 68 - 3 + 7 69 6 12 -280.61808849971 +281.003969981589 22 -229.272654524292 +230.573405104131 13 0.0 23 @@ -3297,7 +3301,7 @@ AcDbViewport 44 0.0 45 -29.4734659601 +29.705466257669 50 0.0 51 @@ -3307,7 +3311,7 @@ AcDbViewport 90 884832 340 -DB +DE 1 281 @@ -3355,15 +3359,15 @@ DB 63 250 361 -DD +E0 0 -LWPOLYLINE +ELLIPSE 5 -DE +E8 102 {ACAD_REACTORS 330 -DF +E9 102 } 330 @@ -3375,49 +3379,35 @@ AcDbEntity 8 0 100 -AcDbPolyline - 90 - 8 - 70 - 1 - 43 -0.0 - 10 -133.673900248053 - 20 -62.616536885326 - 10 -133.673900248053 - 20 -4.91717769477 - 10 -261.92383953979 - 20 -4.91717769477 +AcDbEllipse 10 -261.92383953979 - 20 -62.616536885326 - 10 -244.614031782623 - 20 -62.616536885326 - 10 -244.614031782623 - 20 -47.142617829677 - 10 -159.900881698306 - 20 -47.142617829677 - 10 -159.900881698306 +192.679256065929 20 -62.616536885326 +102.406614456996 + 30 +0.0 + 11 +-59.28799555009778 + 21 +0.0 + 31 +0.0 +210 +0.0 +220 +0.0 +230 +1.0 + 40 +0.26656124442 + 41 +0.0 + 42 +6.283185307179586 0 VIEWPORT 5 -DF +E9 330 1B 100 @@ -3429,23 +3419,23 @@ AcDbEntity 100 AcDbViewport 10 -197.798869893922 +192.679256065929 20 -33.766857290048 +102.406614456996 30 0.0 40 -128.249939291737 +118.575991100196 41 -57.699359190556 +31.607763745991 68 2 69 7 12 -281.003969981589 +275.603494374104 22 -230.573405104131 +229.342098887316 13 0.0 23 @@ -3477,7 +3467,7 @@ AcDbViewport 44 0.0 45 -29.705466257669 +14.080555830957 50 0.0 51 @@ -3487,7 +3477,7 @@ AcDbViewport 90 884832 340 -DE +E8 1 281 @@ -3535,7 +3525,7 @@ DE 63 250 361 -E0 +EA 0 ENDSEC 0 @@ -3615,7 +3605,7 @@ ACAD_VISUALSTYLE 3 ACDB_RECOMPOSE_DATA 350 -E7 +F4 3 AcDbVariableDictionary 350 @@ -3819,9 +3809,9 @@ AcDbSun 0 SUN 5 -DD +E0 330 -DC +DF 100 AcDbSun 90 @@ -3851,9 +3841,9 @@ AcDbSun 0 SUN 5 -E0 +EA 330 -DF +E9 100 AcDbSun 90 @@ -4463,7 +4453,7 @@ X-Ray 0 XRECORD 5 -E7 +F4 102 {ACAD_REACTORS 330 @@ -4757,7 +4747,7 @@ Layout1 34 0.0 15 -270.578743418374 +261.92383953979 25 181.044151849561 35 @@ -5767,7 +5757,7 @@ TABLESTYLE 102 {ACAD_XDICTIONARY 360 -E5 +F2 102 } 330 @@ -12737,7 +12727,7 @@ AcDbDictionary 3 ACAD_XREC_ROUNDTRIP 360 -E3 +F0 0 DICTIONARY 5 @@ -12753,11 +12743,11 @@ AcDbDictionary 3 ACAD_XREC_ROUNDTRIP 360 -E4 +F1 0 DICTIONARY 5 -E5 +F2 330 6B 100 @@ -12769,11 +12759,11 @@ AcDbDictionary 3 ACAD_ROUNDTRIP_2008_TABLESTYLE_CELLSTYLEMAP 360 -E6 +F3 0 XRECORD 5 -E3 +F0 102 {ACAD_REACTORS 330 @@ -12797,7 +12787,7 @@ FLAGS 0 XRECORD 5 -E4 +F1 102 {ACAD_REACTORS 330 @@ -12821,15 +12811,15 @@ FLAGS 0 CELLSTYLEMAP 5 -E6 +F3 102 {ACAD_REACTORS 330 -E5 +F2 102 } 330 -E5 +F2 100 AcDbCellStyleMap 90 diff --git a/notes/pages/CHANGELOG.md b/notes/pages/CHANGELOG.md index 455785346..59ae364ed 100644 --- a/notes/pages/CHANGELOG.md +++ b/notes/pages/CHANGELOG.md @@ -16,6 +16,7 @@ id:: 65e30c28-021e-4c24-ab6e-a9e9fa7c6a51 - NEW: `ezdxf.select` module for location based entity selection - CHANGE: class `RenderContext` accepts ctb files as instances of `acadctb.ColorDependentPlotStyles` - CHANGE: replaced the `Designer` class of the `drawing` add-on by a render pipeline with separated render stages + - CHANGE: better support for viewports with curved clipping paths for the `drawing` add-on - BUGFIX: clipping errors for filled paths - {{issue 1055}} - diff --git a/src/ezdxf/addons/drawing/pipeline.py b/src/ezdxf/addons/drawing/pipeline.py index 38c888065..68dbef61f 100644 --- a/src/ezdxf/addons/drawing/pipeline.py +++ b/src/ezdxf/addons/drawing/pipeline.py @@ -6,7 +6,6 @@ Optional, Iterable, Tuple, - TYPE_CHECKING, Iterator, Callable, ) @@ -33,7 +32,7 @@ ClippingShape, find_best_clipping_shape, ) - +from ezdxf.layouts import Layout from .backend import BackendInterface, BkPath2d, BkPoints2d, ImageData from .config import LinePolicy, TextPolicy, ColorPolicy, Configuration from .properties import BackendProperties, Filling @@ -41,9 +40,6 @@ from .type_hints import Color from .unified_text_renderer import UnifiedTextRenderer -if TYPE_CHECKING: - from ezdxf.layouts import Layout - PatternKey: TypeAlias = Tuple[str, float] DrawEntitiesCallback: TypeAlias = Callable[[RenderContext, Iterable[DXFGraphic]], None] @@ -242,7 +238,7 @@ def set_draw_entities_callback(self, callback: DrawEntitiesCallback) -> None: def set_config(self, config: Configuration) -> None: self.backend.configure(config) - self.config = config + self.config = config stage = self._pipeline while True: stage.set_config(config) @@ -288,8 +284,18 @@ def enter_viewport(self, vp: Viewport) -> bool: m = vp.get_transformation_matrix() clipping_path = make_path(vp) if len(clipping_path): - # TODO: flatten clipping path! max_sagitta = ? - clipping_shape = find_best_clipping_shape(clipping_path.control_vertices()) + vertices = clipping_path.control_vertices() + if clipping_path.has_curves: + layout = vp.get_layout() + if isinstance(layout, Layout): + # plot paper units: + # 0: inches, max sagitta = 1/254 = 0.1 mm + # 1: millimeters, max sagitta = 0.1 mm + # 2: pixels, max sagitta = 0.1 pixel + units = layout.dxf.get("plot_paper_units", 1) + max_sagitta = 1.0 / 254.0 if units == 0 else 0.1 + vertices = list(clipping_path.flattening(max_sagitta)) + clipping_shape = find_best_clipping_shape(vertices) self.clipping_portal.push(clipping_shape, m) return True return False