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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
|
# Copyright (c) 2011-2024, Manfred Moitzi
# License: MIT License
import pytest
import ezdxf
from ezdxf.lldxf.const import VTX_3D_POLYLINE_VERTEX
from ezdxf.lldxf.tagwriter import TagCollector
from ezdxf.tools.test import load_entities
from ezdxf.sections.entities import EntitySection
from ezdxf.entities import factory, Polyline
@pytest.fixture(scope="module")
def doc():
return ezdxf.new("R2000")
@pytest.fixture(scope="module")
def layout(doc):
return doc.modelspace()
def test_create_polyline2D(layout):
polyline = layout.add_polyline2d([(0, 0), (1, 1)])
assert (0.0, 0.0) == polyline[0].dxf.location
assert (1.0, 1.0) == polyline[1].dxf.location
assert "AcDb2dPolyline" == polyline.get_mode()
def test_create_polyline3D(layout):
polyline = layout.add_polyline3d([(1, 2, 3), (4, 5, 6)])
assert (1.0, 2.0, 3.0) == polyline[0].dxf.location
assert (4.0, 5.0, 6.0) == polyline[1].dxf.location
assert VTX_3D_POLYLINE_VERTEX == polyline[0].dxf.flags
assert "AcDb3dPolyline" == polyline.get_mode()
def test_add_new_sub_entities_to_entity_database(layout, doc):
db = doc.entitydb
db_len = len(db)
polyline = layout.add_polyline2d([(0, 0), (1, 1)])
assert len(db) == db_len + 4 # POLYLINE-VERTEX-VERTEX-SEQEND
assert polyline.seqend.is_alive is True
db_len = len(db)
polyline.append_vertices([(2, 2), (3, 3)])
assert len(polyline) == 4
assert polyline.vertices[-1].dxf.handle is not None
assert len(db) == db_len + 2, "new VERTEX entities should be added to db"
def test_export_sub_entities_to_dxf(layout, doc):
from ezdxf.lldxf.tagwriter import TagCollector
layout.add_polyline2d([(0, 0), (1, 1)])
writer = TagCollector()
layout.entity_space.export_dxf(tagwriter=writer)
structure_tags = [tag for tag in writer.tags if tag[0] == 0]
assert structure_tags[0] == (0, "POLYLINE")
assert structure_tags[1] == (0, "VERTEX")
assert structure_tags[2] == (0, "VERTEX")
assert structure_tags[3] == (0, "SEQEND")
def test_vertex_layer(layout):
attribs = {"layer": "polyline_layer"}
polyline = layout.add_polyline3d([(1, 2, 3), (4, 5, 6)], dxfattribs=attribs)
for vertex in polyline.vertices:
assert (
"polyline_layer" == vertex.dxf.layer
), "VERTEX entity not on the same layer as the POLYLINE entity."
def test_change_polyline_layer(layout):
attribs = {"layer": "polyline_layer"}
polyline = layout.add_polyline3d([(1, 2, 3), (4, 5, 6)], dxfattribs=attribs)
polyline.dxf.layer = "changed_layer"
for vertex in polyline.vertices:
assert (
"changed_layer" == vertex.dxf.layer
), "VERTEX entity not on the same layer as the POLYLINE entity."
def test_set_vertex(layout):
polyline = layout.add_polyline2d([(0, 0), (1, 1), (2, 2), (3, 3)])
polyline[2].dxf.location = (7, 7)
assert (7.0, 7.0) == polyline[2].dxf.location
def test_points(layout):
points = [(0, 0), (1, 1), (2, 2), (3, 3)]
polyline = layout.add_polyline2d(points)
assert points == list(polyline.points())
def test_points_in_wcs(layout):
points = [(0, 0), (1, 1), (2, 2), (3, 3)]
polyline = layout.add_polyline2d(points)
assert points == list(polyline.points_in_wcs())
def test_points_in_wcs_with_elevation(layout):
points = [(0, 0), (1, 1), (2, 2), (3, 3)]
polyline = layout.add_polyline2d(points)
polyline.dxf.elevation = (0, 0, 2)
expected = [(p[0], p[1], 2) for p in points]
assert expected == list(polyline.points_in_wcs())
def test_points_in_wcs_with_ocs(layout):
points = [(0, 0), (1, 1), (2, 2), (3, 3)]
polyline = layout.add_polyline2d(points)
polyline.dxf.elevation = (0, 0, 7)
polyline.dxf.extrusion = (0, 0, -1)
expected = [(0, 0, -7), (-1, 1, -7), (-2, 2, -7), (-3, 3, -7)]
assert expected == list(polyline.points_in_wcs())
def test_point_slicing(layout):
points = [(0, 0), (1, 1), (2, 2), (3, 3)]
polyline = layout.add_polyline2d(points)
assert [(1, 1), (2, 2)] == list(polyline.points())[1:3]
def test_append_vertices(layout):
polyline = layout.add_polyline2d([(0, 0), (1, 1)])
polyline.append_vertices([(7, 7), (8, 8)])
assert (7.0, 7.0) == polyline[2].dxf.location
assert 4 == len(polyline)
def test_insert_vertices(layout):
polyline = layout.add_polyline2d([(0, 0), (1, 1)])
polyline.insert_vertices(0, [(7, 7), (8, 8)])
assert (7, 7) == polyline[0].dxf.location
assert (1, 1) == polyline[3].dxf.location
assert 4 == len(polyline)
def test_delete_one_vertex(layout):
polyline = layout.add_polyline2d([(0, 0), (1, 1), (2, 2), (3, 3)])
del polyline.vertices[0]
assert (1, 1) == polyline[0].dxf.location
assert 3 == len(polyline)
def test_delete_two_vertices(layout):
polyline = layout.add_polyline2d([(0, 0), (1, 1), (2, 2), (3, 3)])
del polyline.vertices[0:2]
assert (2, 2) == polyline[0].dxf.location
assert 2 == len(polyline)
def test_polymesh_correct_casting(layout):
mesh1 = layout.add_polymesh((4, 4))
assert mesh1.dxf.handle is not None
mesh2 = layout[-1]
assert mesh1 is mesh2
assert mesh1.vertices is mesh2.vertices
def test_polymesh_set_vertex(layout):
mesh = layout.add_polymesh((4, 4))
mesh.set_mesh_vertex((1, 1), (1, 2, 3))
assert (1, 2, 3) == mesh.get_mesh_vertex((1, 1)).dxf.location
def test_polymesh_error_n_index(layout):
mesh = layout.add_polymesh((4, 4))
with pytest.raises(IndexError):
mesh.get_mesh_vertex((0, 4))
def test_polymesh_error_m_index(layout):
mesh = layout.add_polymesh((4, 4))
with pytest.raises(IndexError):
mesh.get_mesh_vertex((4, 0))
def test_polymesh_mesh_cache(layout):
pos = (2, 1)
mesh = layout.add_polymesh((4, 4))
cache = mesh.get_mesh_vertex_cache()
cache[pos] = (1, 2, 3)
vertex = mesh.get_mesh_vertex(pos)
assert vertex.dxf.location == cache[pos]
with pytest.raises(ezdxf.DXFIndexError):
cache[4, 0]
def test_polyface_correct_casting(layout):
polyface1 = layout.add_polyface()
polyface2 = layout[-1]
assert polyface1 is polyface2
assert polyface1.vertices is polyface2.vertices
def test_polyface_create_face(layout):
face = layout.add_polyface()
assert 0 == len(face)
def test_polyface_add_face(layout):
face = layout.add_polyface()
face.append_face([(0, 0), (1, 1), (2, 2), (3, 3)])
assert [(0, 0), (1, 1) == (2, 2), (3, 3), (0, 0, 0)], list(face.points())
def test_polyface_face_indices(layout):
face = layout.add_polyface()
face.append_face([(0, 0), (1, 1), (2, 2), (3, 3)])
face_record = face[4]
assert 1 == face_record.dxf.vtx0
assert 2 == face_record.dxf.vtx1
assert 3 == face_record.dxf.vtx2
assert 4 == face_record.dxf.vtx3
def test_polyface_add_two_face_indices(layout):
face = layout.add_polyface()
face.append_face([(0, 0), (1, 1), (2, 2), (3, 3)])
# second face has same vertices as the first face
face.append_face([(0, 0), (1, 1), (2, 2)])
face_record = face[5] # second face
assert 1 == face_record.dxf.vtx0
assert 2 == face_record.dxf.vtx1
assert 3 == face_record.dxf.vtx2
assert 4 == face.dxf.m_count # vertices count
assert 2 == face.dxf.n_count # faces count
def test_polyface_faces(layout):
face = layout.add_polyface()
face.append_face([(0, 0), (1, 1), (2, 2), (3, 3)])
face.append_face([(0, 0), (1, 1), (2, 2)])
result = list(face.faces())
assert 2 == len(result)
points1 = [vertex.dxf.location for vertex in result[0]]
# the last vertex is the face_record and is always (0,0,0)
# the face_record contains indices to the face building vertices
assert [(0, 0), (1, 1), (2, 2), (3, 3), (0, 0, 0)] == points1
def test_polyface_optimized_cube(layout):
face = layout.add_polyface()
# a cube consist of 6 faces and 24 vertices
# duplicated vertices should be removed
face.append_faces(cube_faces())
assert 8 == face.dxf.m_count # vertices count
assert 6 == face.dxf.n_count # faces count
def cube_faces():
# cube corner points
p1 = (0, 0, 0)
p2 = (0, 0, 1)
p3 = (0, 1, 0)
p4 = (0, 1, 1)
p5 = (1, 0, 0)
p6 = (1, 0, 1)
p7 = (1, 1, 0)
p8 = (1, 1, 1)
# define the 6 cube faces
# look into -x direction
# Every add_face adds 4 vertices 6x4 = 24 vertices
return [
[p1, p5, p7, p3],
[p1, p5, p6, p2],
[p5, p7, p8, p6],
[p7, p8, p4, p3],
[p1, p3, p4, p2],
[p2, p6, p8, p4],
]
def test_export_polyline2d(layout):
polyline = layout.add_polyline2d([(0, 0), (1, 1)])
collector = TagCollector()
polyline.export_dxf(collector)
tags = collector.tags
assert (100, "AcDb2dPolyline") in tags
collector.reset()
polyline[0].export_dxf(collector)
tags = collector.tags
assert (100, "AcDbVertex") in tags
assert (100, "AcDb2dVertex") in tags
def test_export_polyline3d(layout):
polyline = layout.add_polyline3d([(0, 0), (1, 1)])
collector = TagCollector()
polyline.export_dxf(collector)
tags = collector.tags
assert (100, "AcDb3dPolyline") in tags
collector.reset()
polyline[0].export_dxf(collector)
tags = collector.tags
assert (100, "AcDbVertex") in tags
assert (100, "AcDb3dPolylineVertex") in tags
def test_internals_polymesh(layout):
mesh = layout.add_polymesh((4, 4))
collector = TagCollector()
mesh.export_dxf(collector)
tags = collector.tags
assert (100, "AcDbPolygonMesh") in tags
collector.reset()
mesh[0].export_dxf(collector)
tags = collector.tags
assert (100, "AcDbVertex") in tags
assert (100, "AcDbPolygonMeshVertex") in tags
def test_internals_polyface(layout):
face = layout.add_polyface()
face.append_face([(0, 0), (1, 1), (2, 2), (3, 3)])
collector = TagCollector()
face.export_dxf(collector)
tags = collector.tags
assert (100, "AcDbPolyFaceMesh") in tags
collector.reset()
face[0].export_dxf(collector)
tags = collector.tags
assert (100, "AcDbVertex") in tags
assert (100, "AcDbPolyFaceMeshVertex") in tags
collector.reset()
face[4].export_dxf(collector)
tags = collector.tags
assert (100, "AcDbVertex") not in tags
assert (100, "AcDbFaceRecord") in tags
def test_new_style_polyface_face_count():
doc = ezdxf.new()
section = EntitySection(doc, load_entities(NEW_STYLE_POLYFACE, "ENTITIES"))
entities = list(section)
polyface = entities[0]
faces = list(polyface.faces())
assert 6 == len(faces)
def test_add_virtual_polyline_to_layout(doc, layout):
polyline = Polyline()
polyline.append_vertex((0, 0))
layout.add_entity(polyline)
assert factory.is_bound(polyline, doc) is True, "POLYLINE must be bound to document"
assert (
factory.is_bound(polyline.seqend, doc) is True
), "SEQEND must be bound to document"
assert len(polyline) == 1
assert (
factory.is_bound(polyline.vertices[0], doc) is True
), "VERTEX must be bound to document"
NEW_STYLE_POLYFACE = """ 0
SECTION
2
ENTITIES
0
POLYLINE
5
9A
330
6B
100
AcDbEntity
8
0
100
AcDbPolyFaceMesh
66
1
10
0.0
20
0.0
30
0.0
70
64
71
8
72
6
0
VERTEX
5
9B
330
9A
100
AcDbEntity
8
0
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
0.0
20
0.0
30
0.802929163112954
70
192
0
VERTEX
5
9C
330
9A
100
AcDbEntity
8
0
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
0.4434395109072581
20
0.0
30
0.802929163112954
70
192
0
VERTEX
5
9D
330
9A
100
AcDbEntity
8
0
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
0.4434395109072581
20
0.4434395109072581
30
0.802929163112954
70
192
0
VERTEX
5
9E
330
9A
100
AcDbEntity
8
0
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
0.0
20
0.4434395109072581
30
0.802929163112954
70
192
0
VERTEX
5
9F
330
9A
100
AcDbEntity
8
0
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
0.4434395109072581
20
0.0
30
1.246368674020211
70
192
0
VERTEX
5
A0
330
9A
100
AcDbEntity
8
0
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
0.0
20
0.0
30
1.246368674020211
70
192
0
VERTEX
5
A1
330
9A
100
AcDbEntity
8
0
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
0.4434395109072581
20
0.4434395109072581
30
1.246368674020211
70
192
0
VERTEX
5
A2
330
9A
100
AcDbEntity
8
0
100
AcDbVertex
100
AcDbPolyFaceMeshVertex
10
0.0
20
0.4434395109072581
30
1.246368674020211
70
192
0
VERTEX
5
A3
330
9A
100
AcDbEntity
8
0
62
1
100
AcDbFaceRecord
10
0.0
20
0.0
30
0.0
70
128
71
1
72
2
73
3
74
4
0
VERTEX
5
A4
330
9A
100
AcDbEntity
8
0
62
2
100
AcDbFaceRecord
10
0.0
20
0.0
30
0.0
70
128
71
1
72
2
73
5
74
6
0
VERTEX
5
A5
330
9A
100
AcDbEntity
8
0
62
3
100
AcDbFaceRecord
10
0.0
20
0.0
30
0.0
70
128
71
2
72
3
73
7
74
5
0
VERTEX
5
A6
330
9A
100
AcDbEntity
8
0
62
4
100
AcDbFaceRecord
10
0.0
20
0.0
30
0.0
70
128
71
3
72
7
73
8
74
4
0
VERTEX
5
A7
330
9A
100
AcDbEntity
8
0
62
5
100
AcDbFaceRecord
10
0.0
20
0.0
30
0.0
70
128
71
1
72
4
73
8
74
6
0
VERTEX
5
A8
330
9A
100
AcDbEntity
8
0
62
6
100
AcDbFaceRecord
10
0.0
20
0.0
30
0.0
70
128
71
6
72
5
73
7
74
8
0
SEQEND
5
A9
330
9A
100
AcDbEntity
8
0
0
ENDSEC
"""
|