File: extrude_and_tessellate_concave_profile.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (41 lines) | stat: -rw-r--r-- 1,417 bytes parent folder | download
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
#  Copyright (c) 2022, Manfred Moitzi
#  License: MIT License

from pathlib import Path
import ezdxf
from ezdxf.render import forms

CWD = Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = Path(".")

# ------------------------------------------------------------------------------
# This example shows how to use the ezdxf.forms.extrude method to create a prism
# from a concave base polygon. The added face-normals show if the face-orientation
# follows the usual count-clockwise order to build outside pointing faces.
#
# docs: https://ezdxf.mozman.at/docs/render/forms.html#ezdxf.render.forms.extrude
# ------------------------------------------------------------------------------


def main(filepath):
    doc = ezdxf.new()
    msp = doc.modelspace()

    profile = [(0, 0), (10, 0), (10, 10), (8, 10), (8, 2), (0, 2)]
    concave_prism = forms.extrude(
        profile, [(0, 0, 0), (0, 0, 10)], close=True, caps=True
    )
    concave_prism.render_mesh(msp, dxfattribs={"color": 2})
    concave_prism.render_normals(msp, dxfattribs={"color": 6})

    # tessellate prism into triangles:
    triangle_mesh = concave_prism.mesh_tessellation(3)
    triangle_mesh.translate(20, 0, 0)
    triangle_mesh.render_mesh(msp, dxfattribs={"color": 1})
    triangle_mesh.render_normals(msp, dxfattribs={"color": 6})
    doc.saveas(filepath)


if __name__ == "__main__":
    main(CWD / "concave_prism.dxf")