File: meshex_export.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 (117 lines) | stat: -rw-r--r-- 3,177 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
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
#  Copyright (c) 2022, Manfred Moitzi
#  License: MIT License
import pathlib
import ezdxf
from ezdxf.addons import meshex
from ezdxf.render.forms import cylinder, sphere

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

# ------------------------------------------------------------------------------
# This example shows how to export MeshBuilder instances as 3D file formats:
# STL, OBJ, PLY and IFC4
#
# docs: https://ezdxf.mozman.at/docs/addons/meshex.html
# ------------------------------------------------------------------------------

SIDES = 16
NAME = "sphere"


def make_mesh(sides: int):
    if NAME == "sphere":
        return sphere(count=sides, stacks=sides // 2)
    return cylinder(count=sides)


def export_scad(filename):
    with open(filename, "wt") as fp:
        mesh = make_mesh(SIDES)
        mesh.flip_normals()
        fp.write(meshex.scad_dumps(mesh))


def export_stl_asc(filename):
    with open(filename, "wt") as fp:
        mesh = make_mesh(SIDES)
        fp.write(meshex.stl_dumps(mesh))


def export_stl_bin(filename):
    with open(filename, "wb") as fp:
        mesh = make_mesh(SIDES)
        fp.write(meshex.stl_dumpb(mesh))


def export_off(filename):
    with open(filename, "wt") as fp:
        mesh = make_mesh(SIDES)
        fp.write(meshex.off_dumps(mesh))


def export_obj(filename):
    with open(filename, "wt") as fp:
        mesh = make_mesh(SIDES)
        fp.write(meshex.obj_dumps(mesh))


def export_ply(filename):
    with open(filename, "wb") as fp:
        mesh = make_mesh(SIDES)
        fp.write(meshex.ply_dumpb(mesh))


def export_ifc4(filename, kind):
    with open(filename, "wt") as fp:
        mesh = make_mesh(SIDES)
        fp.write(meshex.ifc4_dumps(mesh, kind, color=(1.0, 0.1, 0.1)))


def export_ifc4ZIP(filename, kind):
    mesh = make_mesh(SIDES)
    meshex.export_ifcZIP(filename, mesh, kind, color=(1.0, 0.1, 0.1))


def export_ifc4_open_cylinder(filename, kind):
    with open(filename, "wt") as fp:
        mesh = cylinder(count=SIDES, caps=False)
        fp.write(meshex.ifc4_dumps(mesh, kind, color=(0.1, 1.0, 0.1)))


def export_dxf(filename):
    doc = ezdxf.new()
    mesh = make_mesh(SIDES)
    mesh.render_mesh(doc.modelspace())
    doc.saveas(filename)


def main():
    export_scad(CWD / f"{NAME}.scad")
    export_stl_asc(CWD / f"{NAME}_asc.stl")
    export_stl_bin(CWD / f"{NAME}_bin.stl")
    export_off(CWD / f"{NAME}.off")
    export_obj(CWD / f"{NAME}.obj")
    export_ply(CWD / f"{NAME}.ply")
    export_dxf(CWD / f"{NAME}.dxf")
    export_ifc4(
        CWD / f"{NAME}_polygon_face_set.ifc",
        meshex.IfcEntityType.POLYGON_FACE_SET,
    )
    export_ifc4(
        CWD / f"{NAME}_closed_shell.ifc", meshex.IfcEntityType.CLOSED_SHELL
    )
    export_ifc4ZIP(
        CWD / f"{NAME}_closed_shell.ifcZIP", meshex.IfcEntityType.CLOSED_SHELL
    )
    export_ifc4_open_cylinder(
        CWD / f"open_cylinder_open_shell.ifc", meshex.IfcEntityType.OPEN_SHELL
    )
    export_ifc4_open_cylinder(
        CWD / f"open_cylinder_polygon_face_set.ifc", meshex.IfcEntityType.POLYGON_FACE_SET
    )


if __name__ == "__main__":
    main()