# ---
# jupyter:
#   jupytext:
#     default_lexer: ipython3
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.3'
#       jupytext_version: 1.19.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# %% [markdown]
# # Sketching: Basic usage
#
# This example shows how to use basic PyAnsys Geometry sketching capabilities.

# %% [markdown]
# ## Perform required imports
#
# Perform the required imports.

# %%
from ansys.geometry.core.misc.units import UNITS as u
from ansys.geometry.core.sketch import Sketch

# %% [markdown]
# ## Create a sketch
#
# Sketches are fundamental objects for drawing basic shapes like lines, segments, circles,
# ellipses, arcs, and polygons.
#
# You create a ``Sketch`` instance by defining a drawing plane. To define a plane, you
# declare a point and two fundamental orthogonal directions.

# %%
from ansys.geometry.core.math import Plane, Point2D, Point3D

# %% [markdown]
# Define a plane for creating a sketch.

# %%
# Define the origin point of the plane
origin = Point3D([1, 1, 1])

# Create a plane located in previous point with desired fundamental directions
plane = Plane(
    origin, direction_x=[1, 0, 0], direction_y=[0, -1, 1]
)

# Instantiate a new sketch object from previous plane
sketch = Sketch(plane)

# %% [markdown]
# ## Draw shapes
#
# To draw different shapes in the sketch, you use ``draw`` methods.

# %% [markdown]
# ### Draw a circle
#
# You draw a circle in a sketch by specifying the center and radius.

# %%
sketch.circle(Point2D([2, 1]), radius=30 * u.cm, tag="Circle")
sketch.select("Circle")
sketch.plot_selection()

# %% [markdown]
# ### Draw an ellipse
#
# You draw an ellipse in a sketch by specifying the center, major radius, and minor radius.

# %%
sketch.ellipse(
    Point2D([1, 1]), major_radius=2*u.m, minor_radius=1*u.m, tag="Ellipse"
)
sketch.select("Ellipse")
sketch.plot_selection()

# %% [markdown]
# ### Draw a polygon
#
# You draw a regular polygon by specifying the center, radius, and desired number of sides.

# %%
sketch.polygon(
    Point2D([1, 1]), inner_radius=3*u.m, sides=5, tag="Polygon"
)
sketch.select("Polygon")
sketch.plot_selection()

# %% [markdown]
# ### Draw an arc
#
# You draw an arc of circumference by specifying the center, starting point, and ending point.

# %%
start_point, end_point = Point2D([2, 1], unit=u.m), Point2D([0, 1], unit=u.meter)
sketch.arc(start_point, end_point, Point2D([1,1]), tag="Arc")
sketch.select("Arc")
sketch.plot_selection()

# %% [markdown]
# There are also additional ways to draw arcs, such as by specifying the start, center point, and angle.

# %%
start_point = Point2D([2, 1], unit=u.m)
center_point = Point2D([1, 1], unit=u.m)
angle = 90
sketch.arc_from_start_center_and_angle(
    start_point, center_point, angle=90, tag="Arc_from_start_center_angle"
)
sketch.select("Arc_from_start_center_angle")
sketch.plot_selection()

# %% [markdown]
# Or by specifying the start, end point, and radius.

# %%
start_point, end_point = Point2D([2, 1], unit=u.m), Point2D([0, 1], unit=u.meter)
radius = 1 * u.m
sketch.arc_from_start_end_and_radius(
    start_point, end_point, radius, tag="Arc_from_start_end_radius"
)
sketch.select("Arc_from_start_end_radius")
sketch.plot_selection()

# %% [markdown]
# ### Draw a slot
#
# You draw a slot by specifying the center, width, and height.

# %%
sketch.slot(Point2D([2, 0]), 4, 3, tag="Slot")
sketch.select("Slot")
sketch.plot_selection()

# %% [markdown]
# ### Draw a box
#
# You draw a box by specifying the center, width, and height.

# %%
sketch.box(Point2D([2, 0]), 4, 5, tag="Box")
sketch.select("Box")
sketch.plot_selection()

# %% [markdown]
# ### Draw a segment
#
# You draw a segment by specifying the starting point and ending point.

# %%
start_point, end_point = Point2D([2, 1], unit=u.m), Point2D([0, 1], unit=u.meter)
sketch.segment(start_point, end_point, "Segment")
sketch.select("Segment")
sketch.plot_selection()

# %% [markdown]
# ## Plot the sketch
#
# The ``Plotter`` class provides capabilities for plotting different PyAnsys Geometry objects.
# PyAnsys Geometry uses PyVista as the visualization backend.
#
# You use the ``plot_sketch`` method to plot a sketch. This method accepts a ``Sketch`` instance and
# some extra arguments to further customize the visualization of the sketch. These arguments include
# showing the plane of the sketch and its frame.

# %%
# Plot the sketch in the whole scene
from ansys.geometry.core.plotting import GeometryPlotter

pl = GeometryPlotter()
pl.add_sketch(sketch, show_plane=True, show_frame=True)
pl.show()
