Skip to content

Commit

Permalink
add test for polygon orientation to revcloud.add_entity()
Browse files Browse the repository at this point in the history
  • Loading branch information
mozman committed Mar 19, 2024
1 parent 386e093 commit d164880
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
8 changes: 8 additions & 0 deletions examples/revcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ def revcloud_entity():
revcloud.add_entity(msp, [(0, 0), (1, 0), (1, 1), (0, 1)], segment_length=0.1)
doc.saveas(CWD / "revcloud-entity.dxf")

def revcloud_reverse_entity():
doc = ezdxf.new()
msp = doc.modelspace()

revcloud.add_entity(msp, [(0, 0), (0, 1), (1, 1), (1, 0)], segment_length=0.1)
doc.saveas(CWD / "revcloud-reverse-entity.dxf")


if __name__ == "__main__":
revcloud_manually()
revcloud_entity()
revcloud_reverse_entity()
18 changes: 15 additions & 3 deletions src/ezdxf/revcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations
from typing import Sequence, TYPE_CHECKING, Iterable, Any
import math
from ezdxf.math import UVec, Vec2
from ezdxf.math import UVec, Vec2, has_clockwise_orientation
from ezdxf.entities import LWPolyline, DXFEntity

if TYPE_CHECKING:
Expand Down Expand Up @@ -83,8 +83,19 @@ def add_entity(
dxfattribs: additional DXF attributes
"""
_vertices: list[Vec2] = Vec2.list(vertices)
if len(_vertices) < 3:
raise ValueError("3 or more points required.")
if not _vertices[0].isclose(_vertices[-1]):
_vertices.append(_vertices[0])
if len(_vertices) < 4:
raise ValueError("3 or more points required.")
bulge = REQUIRED_BULGE
if has_clockwise_orientation(_vertices):
bulge = -bulge

end_width = segment_length * 0.1 if calligraphy else 0.0
lw_points = points(vertices, segment_length, end_width=end_width)
lw_points = points(vertices, segment_length, bulge=bulge, end_width=end_width)
lwp = layout.add_lwpolyline(lw_points, close=True, dxfattribs=dxfattribs)
lwp.set_xdata(REVCLOUD_PROPS, [(1070, 0), (1040, segment_length)])
doc = layout.doc
Expand All @@ -103,5 +114,6 @@ def is_revcloud(entity: DXFEntity) -> bool:
if not lwpolyline.has_xdata(REVCLOUD_PROPS):
return False
return all(
abs(p[0] - REQUIRED_BULGE) < 0.02 for p in lwpolyline.get_points(format="b")
abs(REQUIRED_BULGE- abs(p[0])) < 0.02
for p in lwpolyline.get_points(format="b")
)
5 changes: 3 additions & 2 deletions tests/test_05_tools/test_544_revcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ def test_too_small_segment_length_raises_exception():
def test_add_entity():
doc = ezdxf.new()
msp = doc.modelspace()
# counter-clockwise oriented revision cloud:
lwp = revcloud.add_entity(msp, [(0, 0), (1, 0), (1, 1), (0, 1)], 0.1)

assert doc.appids.has_entry(revcloud.REVCLOUD_PROPS)
assert revcloud.is_revcloud(lwp) is True

# adding a second revision cloud should be ok:
lwp = revcloud.add_entity(msp, [(0, 0), (1, 0), (1, 1), (0, 1)], 0.1)
# clockwise oriented revision cloud:
lwp = revcloud.add_entity(msp, [(0, 0), (0, 1), (1, 1), (1, 0)], 0.1)
assert revcloud.is_revcloud(lwp) is True


Expand Down

0 comments on commit d164880

Please sign in to comment.