"""
.. _clipping_ex:

Clipping the model
==================

This example demonstrates the how to use the
:meth:`qtcad.builder.Builder.clip_model` operation:

 .. automethod:: qtcad.builder.Builder.clip_model
    :no-index:

We begin by importing the necessary modules and defining some
shapes to work with.
"""

from qtcad.builder import Builder
from qtcad.builder import Mask, Polygon, Circle

circle_1 = Circle(radius=0.5).translated(-0.6, 0)
circle_2 = Circle(radius=0.5).translated(0.6, 0)
top_mask = Mask(name="top", shapes=[circle_1, circle_2])

square = Polygon.box(3, 3).centered()
bottom_mask = Mask(name="bottom", shapes=[square])

clip_shape = Polygon.regular(3, 2).centered()
clip_mask = Mask(name="clip", shapes=[clip_shape])


# %%
#
# Next we create a :class:`qtcad.builder.Builder` instance, add the
# masks defined above, and build a model based on extrusion of the
# shapes in the ``bottom`` and ``top`` masks.


builder = Builder().add_mask(bottom_mask).add_mask(top_mask).add_mask(clip_mask)

(
    builder.use_mask("bottom")
    .extrude(0.1)
    .use_mask("top")
    .extrude(0.1)
    .quick_mesh()
    .view(
        show_volume_faces=True,
        save="figs/clip_before.png",  # caption: Model before clipping;
        angles=(-45, 0, 45),
    )
)

# %%
#
# Finally, we use :meth:`qtcad.builder.Builder.clip_model` to clip the
# model using the shape defined in the ``clip`` mask.

(
    builder.use_mask("clip")
    .clip_model()
    .quick_mesh()
    .view(
        show_volume_faces=True,
        save="figs/clip_after.png",  # caption: Model after clipping;
        angles=(-45, 0, 45),
    )
)


# %%
#
# We see that the model has been clipped according to the triangle
# shape defined in the ``clip`` mask.
#
# To illustrate the clipping shape, we can also extrude it on its own:

(
    builder.set_z(0)
    .fill_mode()
    .extrude(2)
    .view(
        show_volume_faces=True,
        save="figs/clip_full.svg",  # caption: Visualization of the shape we clipped to;
        angles=(-45, 0, 45),
        volume_labels=True,
    )
)
