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 183 184 185 186 187 188 189 190 191 192 193 194
|
# Copyright (c) 2020, Manfred Moitzi
# License: MIT License
import pytest
import ezdxf
from ezdxf.render.trace import TraceBuilder, LinearTrace, CurvedTrace
from ezdxf.math import BSpline, Vec2, BoundingBox
def test_trace_builder_init():
t = TraceBuilder()
assert len(t) == 0
def test_add_station_2d():
t = LinearTrace()
t.add_station((0, 0), 1, 2)
assert len(t) == 1
assert t[0].vertex == (0, 0)
assert t[0].start_width == 1
assert t[0].end_width == 2
t.add_station((1, 0), 1)
assert len(t) == 2
assert t[1].vertex == (1, 0)
assert t[1].start_width == 1
assert t[1].end_width == 1
def test_add_station_3d():
# z-axis is ignored
t = LinearTrace()
t.add_station((0, 0, 0), 1, 2)
assert t[0].vertex == (0, 0)
def test_add_spline_segment():
t = CurvedTrace.from_spline(
BSpline.from_fit_points([(1, 0), (3, 1), (5, -1), (6, 0)]),
start_width=2,
end_width=1,
segments=10,
)
assert len(t) == 11
def test_square_face():
t = LinearTrace()
t.add_station((0, 0), 1, 1)
t.add_station((1, 0), 0, 0)
face = list(t.faces())[0]
assert face[0].isclose(Vec2(0, +0.5))
assert face[1].isclose(Vec2(0, -0.5))
assert face[2].isclose(Vec2(1, -0.5))
assert face[3].isclose(Vec2(1, +0.5))
def test_closed_linear_path():
t = LinearTrace()
t.add_station((0, 0), 1, 1)
t.add_station((1, 0), 1, 1)
t.add_station((1, 1), 1, 1)
t.add_station((0, 1), 1, 1)
t.add_station((0, 0), 1, 1)
faces = list(t.faces())
assert len(faces) == 4
assert faces[0] == (
Vec2(0.5, 0.5),
Vec2(-0.5, -0.5),
Vec2(1.5, -0.5),
Vec2(0.5, 0.5),
)
assert faces[3] == (
Vec2(0.5, 0.5),
Vec2(-0.5, 1.5),
Vec2(-0.5, -0.5),
Vec2(0.5, 0.5),
)
def test_two_straight_faces():
t = LinearTrace()
t.add_station((0, 0), 1, 1)
t.add_station((2, 0), 1, 1)
t.add_station((4, 0), 1, 1)
face1, face2 = list(t.faces())
assert face1[0].isclose(Vec2(0, +0.5))
assert face1[1].isclose(Vec2(0, -0.5))
assert face1[2].isclose(Vec2(2, -0.5))
assert face1[3].isclose(Vec2(2, +0.5))
assert face2[0].isclose(Vec2(2, +0.5))
assert face2[1].isclose(Vec2(2, -0.5))
assert face2[2].isclose(Vec2(4, -0.5))
assert face2[3].isclose(Vec2(4, +0.5))
def test_two_angled_faces():
t = LinearTrace()
t.add_station((0, 0), 1, 0.5)
t.add_station((2, 0), 1, 1)
t.add_station((4, 2), 1, 1)
face1, face2 = list(t.faces())
assert face1[0].isclose(Vec2(0, +0.5))
assert face1[1].isclose(Vec2(0, -0.5))
assert face1[2].isclose(Vec2(2.5224077499274835, -0.18469903125906456))
assert face1[3].isclose(Vec2(1.5936828611675133, 0.3007896423540608))
assert face2[2].isclose(Vec2(4.353553390593274, 1.6464466094067263))
assert face2[3].isclose(Vec2(3.646446609406726, 2.353553390593274))
def test_linear_trace_polygon():
t = LinearTrace()
t.add_station((0, 0), 1, 1)
t.add_station((2, 0), 1, 1)
t.add_station((4, 0), 1, 1)
polygon = t.polygon()
assert len(polygon) == 6
assert polygon[0].isclose(Vec2(0, -0.5))
assert polygon[-1].isclose(Vec2(0, +0.5))
def test_virtual_entities_added_to_entity_database():
doc = ezdxf.new()
msp = doc.modelspace()
t = LinearTrace()
t.add_station((0, 0), 1, 1)
t.add_station((1, 0), 0, 0)
dxfattribs = {"layer": "TEST"}
solid = list(t.virtual_entities("SOLID", dxfattribs, doc))[0]
assert solid.DXFTYPE == "SOLID"
assert solid.dxf.layer == "TEST"
assert solid.dxf.handle in doc.entitydb
msp.add_entity(solid)
trace = list(t.virtual_entities("TRACE", dxfattribs, doc))[0]
assert trace.DXFTYPE == "TRACE"
assert trace.dxf.layer == "TEST"
assert trace.dxf.handle in doc.entitydb
msp.add_entity(trace)
face = list(t.virtual_entities("3DFACE", dxfattribs, doc))[0]
assert face.DXFTYPE == "3DFACE"
assert face.dxf.layer == "TEST"
assert face.dxf.handle in doc.entitydb
msp.add_entity(face)
assert len(msp) == 3
def test_issue_191():
from ezdxf.entities import factory
e = factory.new(
"LWPOLYLINE",
dxfattribs={
"flags": 1,
},
)
e.set_points(
[
(421846.9857097387, -36908.41493252141, 0.0, 50.0, 0.52056705),
(421846.9857097387, -36908.41493252139, 0.0, 50.0, 0.52056705),
]
)
trace = TraceBuilder.from_polyline(e, segments=64)
assert len(trace) == 1
assert len(list(trace.faces())) == 32
def test_nearly_horizontal_parallel_lines_in_linear_trace_builder():
width = 30
trace = LinearTrace()
trace.add_station((100, 0), width, width)
trace.add_station((100_000, 0), width, width)
trace.add_station((0, 0.001), width, width)
bbox = BoundingBox()
for face in trace.faces():
bbox.extend(face)
assert bbox.extmin.x > -100
def test_nearly_vertical_parallel_lines_in_linear_trace_builder():
width = 30
trace = LinearTrace()
trace.add_station((0, 100), width, width)
trace.add_station((0, 100_000), width, width)
trace.add_station((0.001, 0), width, width)
bbox = BoundingBox()
for face in trace.faces():
bbox.extend(face)
assert bbox.extmin.y > -100
if __name__ == "__main__":
pytest.main([__file__])
|