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
|
# Copyright (c) 2020-2022, Manfred Moitzi
# License: MIT License
import pathlib
import ezdxf
from ezdxf.addons.pycsg import CSG
from ezdxf.render.forms import cube, cylinder_2p
CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
CWD = pathlib.Path(".")
# ------------------------------------------------------------------------------
# This example shows how to use the pycsg add-on (Constructive Solid Geometry).
#
# docs: https://ezdxf.mozman.at/docs/addons/pycsg.html
# ------------------------------------------------------------------------------
def main():
cube1 = cube()
cylinder1 = cylinder_2p(
count=32, base_center=(0, -1, 0), top_center=(0, 1, 0), radius=0.25
)
doc = ezdxf.new()
doc.set_modelspace_vport(6, center=(5, 0))
msp = doc.modelspace()
# build solid union
union = CSG(cube1) + CSG(cylinder1)
# convert to mesh and render mesh to modelspace
union.mesh().merge_coplanar_faces().render_mesh(
msp, dxfattribs={"color": 1}
)
# build solid difference
difference = CSG(cube1) - CSG(cylinder1)
# convert to mesh, translate mesh and render mesh to modelspace
difference.mesh().merge_coplanar_faces().translate(1.5).render_mesh(
msp, dxfattribs={"color": 3}
)
# build solid intersection
intersection = CSG(cube1) * CSG(cylinder1)
# convert to mesh, translate mesh and render mesh to modelspace
intersection.mesh().translate(2.75).render_mesh(
msp, dxfattribs={"color": 5}
)
doc.saveas(CWD / "pycsg01.dxf")
if __name__ == "__main__":
main()
|