Mastering 2D Design: A Beginner’s Guide to Inspire's Sketching Module

Karthi Kandasamy
Karthi Kandasamy
Altair Employee
edited September 12 in Altair HyperWorks

In this blog, we will explore the power of the Inspire Sketching Module. With Inspire’s Sketch API, you can create, manipulate, and refine 2D sketches, which form the foundation for many 3D modeling operations. Sketching is a fundamental step for designing intricate shapes, structures, and models. Let's dive into key sketching techniques and tools provided by Inspire, demonstrating how to create points, lines, arcs, circles, and complex shapes like splines and polygons.

 

1. Creating Points and Lines

Points and lines are the basic building blocks of any sketch. Here’s how you can create and manipulate them using Inspire’s API:

from hwx import inspire model = inspire.newModel()  # Create a new sketch instance s = inspire.Sketch()  # Add points p1 = s.addPoint(2, 2) p2 = s.addPoint(1, 1)  # Add line segments line1 = s.addLineSegment(p1, p2) line2 = s.addLineSegment((3, 3), (5, 5))  # Add a line by specifying a point and direction line3 = s.addLine((2, 2), (1, -1))  # Finalize sketch sketchPart = s.realize()  # Querying linear feature.  featsLinear =sketchPart.getFeatures('FeatureLinear') # Querying Point feature.  featsPoint = sketchPart.getFeatures('FeaturePoint') 

By calling the addPoint, addLine and addLineSegment methods, you can generate points and lines within your model. The sketch becomes a part of the model once you call the realize() function.

 

2. Drawing Circles

The Sketch API offers multiple ways to create circles — by defining a center and radius, two points, or three points. Below is an example:

 from hwx import inspire model = inspire.newModel() s = inspire.Sketch()  # Add circle using center Point and radius center = s.addPoint(0, 0) circle1 = s.addCircleCenterRadius(center, 2.0)  #Adding circle with center point co-ordinate and radius circle1 = s.addCircleCenterRadius((5,5), 2.0)  # Add circle using two points (diameter endpoints) circle2 = s.addCircle2Points((3, 0), (4, 1))  # Add circle defined by three points circle3 = s.addCircle3Points((-6, 0), (-4, 0), (-6, -4))  # Realize sketch sketchPart = s.realize()  inspire.orientView(direction="top")  # Querying area feature. featsArea = sketchPart.getFeatures(type ='FeatureArea')  

The addCircleCenterRadius, addCircle2Points, and addCircle3Points methods give flexibility in defining circles based on your sketching needs.

 

3. Creating Arcs

Inspire allows arcs to be created in multiple ways — using a center with two endpoints, or three points defining the arc. Here’s an example:

 #Creating a mickey mouse sketch  from hwx import inspire  # Initialize a new model and sketch model = inspire.newModel() s = inspire.Sketch()  # Create the head - large circle head_center = s.addPoint(0, 0) head = s.addCircleCenterRadius(head_center, 5)  # Create the left ear - smaller circle left_ear_center = s.addPoint(-4, 6) left_ear = s.addCircleCenterRadius(left_ear_center, 2.5)  # Create the right ear - smaller circle right_ear_center = s.addPoint(4, 6) right_ear = s.addCircleCenterRadius(right_ear_center, 2.5)  # Creating Mickey's face (use arcs for a smile and eyes) # Left eye - small circle left_eye_center = s.addPoint(-2, 2) left_eye = s.addCircleCenterRadius(left_eye_center, 0.7)  # Right eye - small circle right_eye_center = s.addPoint(2, 2) right_eye = s.addCircleCenterRadius(right_eye_center, 0.7)  # Nose - medium circle nose_center = s.addPoint(0, 0.5) nose = s.addCircleCenterRadius(nose_center, 1.0)  # Smile - use an arc for the smile smile = s.addArcCenter2Points((0, -1.5), (-2.5, -2), (2.5, -2), ccw=True)  # Realize the sketch and set the view to top s.realize() inspire.orientView(direction="top")

 

# Creating a smiley face  from hwx import inspire  # Initialize a new model and sketch model = inspire.newModel() s = inspire.Sketch()  # Create the face (large circle) face_center = s.addPoint(0, 0) face = s.addCircleCenterRadius(face_center, 5)  # Create the left eye (small circle) left_eye_center = s.addPoint(-2, 2) left_eye = s.addCircleCenterRadius(left_eye_center, 0.7)  # Create the right eye (small circle) right_eye_center = s.addPoint(2, 2) right_eye = s.addCircleCenterRadius(right_eye_center, 0.7)  # Create the smile (arc) smile_center = (0, -1.5)  # Center of the smile's arc smile = s.addArcCenter2Points(smile_center, (-2.5, -2), (2.5, -2), ccw=True)  # Realize the sketch and set the view to top s.realize() inspire.orientView(direction="top")

 

# Creating a Sad Face   from hwx import inspire  # Initialize a new model and sketch model = inspire.newModel() s = inspire.Sketch()  # Create the face (large circle) face_center = s.addPoint(0, 0) face = s.addCircleCenterRadius(face_center, 5)  # Create the left eye (small circle) left_eye_center = s.addPoint(-2, 2) left_eye = s.addCircleCenterRadius(left_eye_center, 0.7)  # Create the right eye (small circle) right_eye_center = s.addPoint(2, 2) right_eye = s.addCircleCenterRadius(right_eye_center, 0.7)  # Create the sad mouth (arc) sad_mouth_center = (0, -2)  # Center of the sad mouth's arc sad_mouth = s.addArcCenter2Points(sad_mouth_center, (-2.5, -1.5), (2.5, -1.5), ccw=False)  # Realize the sketch and set the view to top s.realize() inspire.orientView(direction="top")

The clockwise (ccw) or counterclockwise orientation can be controlled during the arc’s creation.

Using addArcCenter2Points and addArc3Points or addArcTangentTo, you can create beautiful arcs based on geometric rules, making it easier to model curved shapes in your design.

 

4. Ellipses

For more complex curves, ellipse are useful tools. Here's how to create them:

 

from hwx import inspire  model = inspire.newModel() s = inspire.Sketch()  e = s.addEllipse((0.01, 0.01), (0, 1), 0.3, 0.5)  # we can also query radius and center of ellipse. print(f'Major Radius of ellipse : {e.majorRadius}, '       f'Minor Radius of ellipse : {e.minorRadius}, '       f'Center of ellipse : {e.center}')  be = s.addBoundedEllipse((0, 0), (0, 0.3), (0.1, 0), (-0.1, 0), (0, -0.3))  s.realize() inspire.orientView(direction="top") 

 

 from hwx import inspire  model = inspire.newModel() s = inspire.Sketch()  # Piggy Face - Main Head s.addEllipse(center=(0, 0), verticalAxis=(0, 1), majorRadius=2.5, minorRadius=2.0)  # Piggy Eyes # Left Eye s.addEllipse(center=(-1, 1), verticalAxis=(-1, 2), majorRadius=0.5, minorRadius=0.3) # Right Eye s.addEllipse(center=(1, 1), verticalAxis=(1, 2), majorRadius=0.5, minorRadius=0.3)  # Piggy Snout s.addEllipse(center=(0, -0.5), verticalAxis=(0, 0), majorRadius=1.0, minorRadius=0.6)  # Piggy Nostrils # Left Nostril s.addEllipse(center=(-0.5, -0.5), verticalAxis=(-0.5, -0.6), majorRadius=0.2, minorRadius=0.1) # Right Nostril s.addEllipse(center=(0.5, -0.5), verticalAxis=(0.5, -0.6), majorRadius=0.2, minorRadius=0.1)  s.realize() inspire.orientView(direction="top") 

 

from hwx import inspire  # Initialize a new model model = inspire.newModel() s = inspire.Sketch()  # Define points for the butterfly's wings # Left Wing wing1 = s.addSpline([(0, 0), (-1, 1), (-2, 1.5), (-2, 0), (-1, -1), (0, -1)])  # Right Wing wing2 = s.addSpline([(0, 0), (1, 1), (2, 1.5), (2, 0), (1, -1), (0, -1)])  # Body of the Butterfly body = s.addEllipse(center=(0, 0), verticalAxis=(1, 0), majorRadius=0.2, minorRadius=0.6)  # Antennae left_ant = s.addLineSegment((-0.1, 0.7), (-0.5, 1.2)) right_ant = s.addLineSegment((0.1, 0.7), (0.5, 1.2))  # Realize the sketch s.realize()  # Adjust the view to top inspire.orientView(direction="top")

 

5. PolyLines

Polylines offer the ability to combine straight lines and arcs in a single command, forming complex paths with minimal effort.

from hwx import inspire model = inspire.newModel() s = inspire.Sketch()  # Create a polyline pl = s.addPolyline([(0, 0), (1, 2), (3, 3)])  # Combine lines and arcs in the polyline pl = s.polylineStart(0.1, 0.1) s.polylineAddLineTo(pl, (0.5, 0.5)) s.polylineAddArcTo(pl, (1.5, 1.2), inspire.Sketch.TANGENT_OUTWARD)  # Realize the sketch s.realize() 

 

# Creating Star using polyLine from hwx import inspire  model = inspire.newModel() s = inspire.Sketch()  # Define the points for the star points = [     (0, 2),     # Top vertex     (0.5, 0.5), # Right-top vertex     (2, 0.5),   # Right vertex     (0.8, -0.5),# Right-bottom vertex     (1.5, -2),  # Bottom vertex     (0, -1.0),  # Bottom-left vertex     (-1.5, -2), # Bottom vertex (left side)     (-0.8, -0.5),# Left-bottom vertex     (-2, 0.5),  # Left vertex     (-0.5, 0.5), # Right-top vertex      (0, 2) #(return to the start) ]  # Start the polyline at the first point pl = s.polylineStart(*points[0])  # Add line segments to the polyline for point in points[1:]:     s.polylineAddLineTo(pl, point)  # Finish the polyline result = s.polylineEnd(pl)  s.realize() inspire.orientView(direction="top") 

 

from hwx import inspire  # Initialize a new model model = inspire.newModel() s = inspire.Sketch()  # Define the points for the rounded rectangle bottom_left = (0, 0) bottom_right = (4, 0) top_right = (4, 3) top_left = (0, 3)  # Define the radius for the arcs radius = 0.5  # Start the polyline at the bottom-left corner pl = s.polylineStart(*bottom_left)  # Add line segments to the polyline s.polylineAddLineTo(pl, bottom_right)  # Add arc to the polyline for the bottom-right corner s.polylineAddArcTo(pl, top_right, direction=1)  # 1 for counter-clockwise direction  # Add line segment to the polyline s.polylineAddLineTo(pl, top_left)  # Add arc to the polyline for the top-left corner s.polylineAddArcTo(pl, bottom_left, direction=1)  # 1 for counter-clockwise direction  # Finish the polyline result = s.polylineEnd(pl)  # Realize the sketch and orient the view s.realize() inspire.orientView(direction="top") 

 

6. Creating Splines and Bezier Curves

Splines are smooth curves through a series of points. They’re great for organic shapes and flowing designs:

 from hwx import inspire model = inspire.newModel() s = inspire.Sketch()  # Create a spline passing through specified points spline = s.addSpline([(0, 0), (1, 2), (2, 1), (3, 3)])  # Modify the degree of the spline spline.degree = 5  s.realize()  

 

from hwx import inspire  # Initialize a new model model = inspire.newModel() s = inspire.Sketch()  # Define the control points for the spline control_points = [     (0, 0),   # Start point     (1, 2),   # Control point 1     (2, -1),  # Control point 2     (3, 1),   # Control point 3     (4, 0)    # End point ]  # Create a spline with the defined control points # Using a cubic spline (degree=3) that is not periodic and interpolated spline = s.addSpline(points=control_points, degree=3, periodic=False, interpolated=True)  # Realize the sketch and orient the view s.realize() inspire.orientView(direction="top") 

 

from hwx import inspire  # Initialize a new model model = inspire.newModel() s = inspire.Sketch()  # Define the control points for the heart shape control_points = [     (0, 0),         # Starting point at the bottom     (-1, 1),        # Left top point     (-2, 2),        # Left upper curve control point     (0, 3),         # Top middle point     (2, 2),         # Right upper curve control point     (1, 1),         # Right top point     (0, 0)          # Close the shape back to the starting point ]  # Create a spline with the defined control points # Using a cubic spline (degree=3) that is not periodic and interpolated spline = s.addSpline(points=control_points, degree=3, periodic=False, interpolated=True)  # Realize the sketch and orient the view s.realize() inspire.orientView(direction="top") 

Splines can be queried and modified, making them versatile tools in your design workflow.

 

7. Advanced Shapes: Rectangles, Parallelograms, and Polygons

You can also create standard shapes like rectangles, parallelograms, and polygons with ease:

#Example 1: Rectangle using two opposite vertices from hwx import inspire model = inspire.newModel() s = inspire.Sketch()  # Create a rectangle using two opposite corners corner1 = (0, 0) corner2 = (3, 2) rectangle = s.addRectangle2Vertex(corner1, corner2)  # Realize and display the sketch s.realize() inspire.orientView(direction="top")  #Example 2: Rectangle using center, width, and height 

 

from hwx import inspire  # Initialize a new model model = inspire.newModel() s = inspire.Sketch()  # Central Rectangle center = (0, 0) vertex = (1, 1) r1 = s.addRectangleCenterVertex(center, vertex)  # Surrounding Rectangles # Rectangle with two vertices vertex1 = (-2, -2) vertex2 = (-1, -1) r2 = s.addRectangle2Vertex(vertex1, vertex2)  # Rectangle with three vertices lowerLeft = (2, 1) lowerRight = (4, 1) upperRight = (4, 3) r3 = s.addRectangle3Vertex(lowerLeft, lowerRight, upperRight)  # Rectangle with center and two vertices center2 = (0, 4) lengthPoint = (-2, 4) widthPoint = (0, 6) r4 = s.addRectangleCenter2Points(center2, lengthPoint, widthPoint)  # Adjust view to visualize the pattern s.realize() inspire.orientView(direction="top")  # Optionally: Draw some additional lines or shapes for better visual context 

 

from hwx import inspire  # Initialize a new model model = inspire.newModel() s = inspire.Sketch()  # Define parallelogram parameters lowerLeft = (0, 0) lowerRight = (2, 0) upperRight = (2.5, 1)  # Create a grid of parallelograms rows = 3 cols = 4 offset = 3  for row in range(rows):     for col in range(cols):         # Calculate the position for each parallelogram         x_offset = col * offset         y_offset = row * offset         s.addParallelogram(             (lowerLeft[0] + x_offset, lowerLeft[1] + y_offset),             (lowerRight[0] + x_offset, lowerRight[1] + y_offset),             (upperRight[0] + x_offset, upperRight[1] + y_offset)         )  # Adjust view to visualize the pattern s.realize() inspire.orientView(direction="top")  # Optionally: Draw additional shapes or lines for context 

 

#Example 1: Create the pentagon from hwx import inspire  # Instantiate sketch s = inspire.Sketch()  # Create a pentagon center = (0, 0) vertex = (0.1, 0) numSides = 5  # Create the pentagon pentagon = s.addRegularPolygon(center, vertex, numSides, beMidPoint=False) s.realize()   #Example 2:  Create the octagon # Instantiate sketch s = inspire.Sketch()  # Create a regular octagon with the provided point as a midpoint of a side center = (0, 0) midpoint = (0.1, 0)  # Midpoint of one of the octagon's sides numSides = 8  # Create the octagon octagon = s.addRegularPolygon(center, midpoint, numSides, beMidPoint=True) s.realize()  #Example 3: Create the hexagon  # Instantiate sketch s = inspire.Sketch()  # Create a regular hexagon center = (0, 0) vertex = (0.1, 0)  # Vertex of the hexagon numSides = 6  # Create the hexagon hexagon = s.addRegularPolygon(center, vertex, numSides, beMidPoint=False) s.realize() 

8. Adding Constraints and Dimensions

Adding constraints and dimensions helps maintain design intent and precision. Here’s how you can do it:

 from hwx import inspire model = inspire.newModel() s = inspire.Sketch()  # Add vertical constraint l = s.addLineSegment((0, 0), (1, 1)) s.addConstraint(Sketch.CONSTRAINT_VERTICAL, item1=l)  # Add perpendicular constraint l2 = s.addLineSegment((0, 0), (1, 2)) s.addConstraint(Sketch.CONSTRAINT_PERPENDICULAR, item1=l, item2=l2)  s.update() s.realize() inspire.orientView(direction="top")  # Add dimension constraints p1 = s.addPoint(0, 0) p2 = s.addPoint(1, 1) l = s.addLineSegment(p1, p2) d = s.addDimension(Sketch.DIMENSION_DISTANCE_HORIZONTAL, 3, item1=p1, item2=p2)  # Add angle dimension l2 = s.addLineSegment((0, 0), (1, 2)) s.addDimension(Sketch.DIMENSION_ANGLE, 45, item1=l, item2=l2)  s.update() s.realize() inspire.orientView(direction="top") 

Constraints ensure certain geometrical relationships between objects, while dimensions can be applied to control sizes.

 

9. Splitting, Trimming, and Extending Sketch Items

Sometimes, you need to split, trim, or extend existing sketch elements. Here's how:

from hwx import inspire model = inspire.newModel() s = inspire.Sketch()  # Add and trim lines s.addLineSegment((0, 0), (0, 10)) s.addLineSegment((5, 0), (5, 10)) s.addLineSegment((-5, 5), (15, 5))  # Trimming a sketch item result = s.trim(2.5, 5)  # Extending a sketch item result = s.extend((1, 1), (4, 2))  s.realize() inspire.orientView(direction="top") 

These operations allow you to fine-tune your sketch by modifying individual elements.

 

10. Fillets, Offset and Mirroring

Adding fillets (rounded corners) and mirroring objects in the sketch are useful for symmetrical designs and smoothing corners.

 from hwx import inspire model = inspire.newModel() s = inspire.Sketch()  # Fillet operation r = s.addRectangle2Vertex((0, 0), (1, 1)) s.fillet(r[0], 0.2)  # Offset operation pl = s.addPolyline([(0, 0), (0.1, 0.3), (0.3, 0.2), (0.4, 0.4)]) s.offset((0, 0), 0.1)  # Mirror operation ls1 = s.addLineSegment((0, 0), (0, 1)) a1 = s.addArcCenter2Points((1, 1), (0, 1), (2, 1)) s.mirror([ls1, a1], s.addLineSegment((5, -1), (5, 4)))  s.realize() inspire.orientView(direction="top")   

 

11. Patterns: Linear and Circular

You can duplicate elements in a linear or circular pattern to create repeated features in your design.

 from hwx import inspire model = inspire.newModel() s = inspire.Sketch()  # Create a linear pattern p1 = s.addPoint(1, 0) p2 = s.addPoint(2, 0) ls1 = s.addLineSegment(p1, p2) dir = s.addLineSegment(s.addPoint(0, 1), s.addPoint(0, 2))  s.linearPattern(ls1, dir, 5, 0.1, False) s.realize() inspire.orientView(direction="top")   from hwx import inspire model = inspire.newModel() s = inspire.Sketch()  # Create a circular pattern circle = s.addCircleCenterRadius((0, 0), 0.3) p = s.addPoint(0, 0) s.circularPattern(circle, p, 5, 45, False, equalSpacing=True)  s.realize() inspire.orientView(direction="top")  

 

#How to perform a linear pattern using sketch APIs  from hwx import inspire  model = inspire.newModel() s = inspire.Sketch()  # line 1 p1 = s.addPoint(1, 0) p2 = s.addPoint(2, 0) p1.position = (0, 0) ls1 = s.addLineSegment(p1, p2)  # direction p1 = s.addPoint(0, 1) p2 = s.addPoint(0, 2) dir = s.addLineSegment(p1, p2)  pattern = s.linearPattern(ls1, dir, 5, 0.1, False)  s.realize() inspire.orientView(direction="top")  #How to perform a circular pattern using sketch APIs  from hwx import inspire  model = inspire.newModel() s = inspire.Sketch()  # Point1 p = s.addPoint(1, 0)  # line p1 = s.addPoint(0, 1) p2 = s.addPoint(0, 2) line = s.addLineSegment(p1, p2)  pattern = s.circularPattern(line, p, 5, 20, False)  s.realize() inspire.orientView(direction="top")

Patterns allow you to efficiently create repetitive features.

 

12. Using Variables for Flexibility

Variables allow dynamic changes to the design, making it adaptable to different parameter values:

model = inspire.newModel() from hwx import inspire  model.variables.add("copy", type="unitless", expression=10)  model.variables.add("angle1", type="angle", expression="20.0 deg")  model.variables.add("width", type="length", expression='1 mm')

 

# Example on Sketch Circular Pattern   from hwx import inspire from hwx.common import math  model = inspire.newModel() s = inspire.Sketch() # Point1 p = s.addPoint(1, 0)  # line p1 = s.addPoint(0, 1) p2 = s.addPoint(0, 2) circle = s.addCircleCenterRadius(p1, 0.05)  #Creating a circular patten  for circle object with an center as point object p, # number of copy as 5, # angle between patterened object is 20 deg  # createDimension is false  # equalSpacing is true.  pattern = s.circularPattern(circle, p, 5, 45, False, equalSpacing=True)  #Creating a Variable named 'copy'  model.variables.add("copy", type="unitless", expression=10) # Associating the variable 'Copy' with pattern copy pattern.copy = "copy"  # Creating a variable named 'angle' model.variables.add("angle1", type="angle", expression="20.0 deg") # Associating the variable 'angle' with pattern angle pattern.angle = "angle1" sketchItem = s.realize() #how to get feature planar from the created sketch for extrude or pushpull Operation to make 3D Object.  feats = sketchItem.getFeatures(type =inspire.FeaturePlanar) if feats:   print(feats)

Variables offer great flexibility in controlling dimensions and patterns.

 

Conclusion

The Inspire Sketching Module is a powerful tool for creating 2D designs with ease and precision. Whether you're creating simple geometric shapes, complex splines, or intricate patterns, the Sketch API provides all the tools you need to bring your ideas to life. With advanced features like constraints, dimensioning, trimming, and more, your 2D sketches can quickly become the foundation of professional-grade 3D models.

Happy Scripting!

 

Comments

  • rdominguez
    rdominguez
    Altair Employee
    edited September 12

    Great publication, Karthi! It's extremely helpful.