Drawing in a Sketch

This is a continuation of the getting started tutorial that’s discussing the creation of a new command to draw a slot.  In this installment I discuss drawing geometry in a sketch.

When creating sketch geometry using the API, there are a couple of concepts that are important to understand that aren’t very obvious when you create sketches interactively.  Let’s look at what you really have when you draw two lines in a sketch.  You can see from the picture below it just looks like two lines and nothing else.

 Lines

If you know what to look for, you’ll also notice something else.  If you move the mouse over the end of the lines, a point is displayed, as shown below.  At first you might think this is just a handle being displayed to indicate that you can drag the end of the line.  It’s not a handle, it’s a real sketch point that’s being displayed. 

LinesWithPoint

In fact, to better see these are real points you can select a point and using the context menu, change the display style of the point to a center point.  With this display style it remains visible all the time and not just when the cursor is over it.  The picture below shows all three of the points being displayed with a center point style.

LineWithCenterPoint

The thing to learn from this is that all sketch geometry is always associated to  sketch points.  Sketch geometry doesn’t exist on its own.  The position of sketch geometry is controlled by the position of the points.  It’s like the geometry is fastened to the points and if the points move the geometry attached to them also moves. 

Another very important concept is that the two lines are connected by the fact that they share the same point.  Both lines are attached to the same point.  That’s how Inventor determines connected curves in a profile.  We’ll see an example of this later in this post.

Below is a picture of the two lines after placing a fillet.  The geometry consists of two lines and an arc.  The position of each of the lines is still defined by two sketch points and the arc shares the sketch points where it connects to the lines.  In addition, the arc has an additional sketch point that defines its center.

LinesWithFillet

When you interactively draw a sketch, Inventor automatically creates these points behind the scenes.  It also automatically deletes them when the associated geometry is deleted.  When using the API it’s possible to have it automatically create these points too, but without understanding these concepts of how geometry is connected to points and each other by sharing points it’s easy to get results you weren’t expecting.  When interactively drawing a sketch, it’s natural to get the correct result because Inventor infers connections based on where you click the mouse.  When using the API, none of this inference can take place because you’re not moving the mouse over the geometry.

The code below creates a sketch line on an existing sketch.  You can paste this into the bottom of the sub from the previous post in this series. 

‘ Get a reference to the TransientGeometry object.
Dim tg As Inventor.TransientGeometry = invApp.TransientGeometry

‘ Get a reference to the SketchLines collection.
Dim lines As Inventor.SketchLines = sketch.SketchLines

‘ Draw a line between (0,0) and (4,0).
Dim line1 As Inventor.SketchLine
line1 = lines.AddByTwoPoints(tg.CreatePoint2d(0, 0), _
                             tg.CreatePoint2d(4, 0))

The first thing this does is get a reference to the TransientGeometry object. This is a utility object in the API that lets you create various types of geometry.  However, the geometry created is not ever displayed or saved by Inventor, which is why they are called transient.  These objects are simply the mathematical definition of the geometry and are not visible entities.  I’m using it in this case to create two coordinate points to define the end points of the line.  The CreatePoint2d method creates a transient 2d point.  The Point2d object is a simple API object that wraps the x-y coordinates of a point in 2d space.  In Inventor’s API, instead of passing two values to define x-y coordinates a Point2d object is used instead.  There’s also a Point object which wraps x-y-z coordinates for a 3d point.  When working with a sketch you want to make sure you’re using the CreatePoint2d method and not the CreatePoint method.  It’s a common mistake.

The next thing the program above does is get the SketchLines collection.  In a previous post I briefly discussed collection objects.  The SketchLines object lets you access all of the lines on the sketch and it supports add methods to let you create new lines.  This examples uses the AddByTwoPoints method to create a new sketch line.

The final section of the code above creates the new line.  A variable is declared with the type of SketchLine and the newly created line is assigned to the variable.  The AddByTwoPoints method takes to points as input.  These can either be coordinate points (defined by a Point2d object as in the example above) or they can be existing SketchPoint objects, which you’ll see demonstrated below.

Another t
hing that’s important to understand are the units that Inventor uses. Internally, Inventor is consistent in the units it uses.  Lengths are always defined in centimeters and angles are always defined in radians.  It doesn’t matter what units the user has specified as their document units.  The API is consistent with the Inventor internal units.  When you create or query geometry, any length or coordinate values will always be in centimeters and any angles will always be in radians.  The example above creates a 4 centimeter long line starting from the origin and going in the sketch X direction.  We’ll see in a later post how to deal with user defined units.

Below is some code that creates a second and third line to create a triangle.

‘ Draw a line between (4,0) and (4,3).
Dim line2 As Inventor.SketchLine
line2 = lines.AddByTwoPoints(tg.CreatePoint2d(4, 0), _
                             tg.CreatePoint2d(4, 3))

‘ Draw a line between (4,3) and (0,0).
Dim line3 As Inventor.SketchLine
line3 = lines.AddByTwoPoints(tg.CreatePoint2d(4, 3), _
                             tg.CreatePoint2d(0, 0))

The resulting geometry is shown below on the left.  If I try to create an extrusion using this sketch I can’t select the triangle, but only the individual lines.  That’s because each line has it’s own unique pair of sketch points for its end points, instead of sharing a single point between two lines.  This becomes obvious if I drag any of the end points, the triangle pulls apart, as shown in the picture on the right.

LinesTriangle

LinesBrokenTriangle

The code below is a variation of the previous code but creates a set of connected lines.  The difference is that instead of defining new coordinates for the end of each line it uses the EndSketchPoint and StartSketchPoint properties of the SketchLine object to get the sketch points from the existing lines.

‘ Draw a line between (0,0) and (4,0).
Dim line1 As Inventor.SketchLine
line1 = lines.AddByTwoPoints(tg.CreatePoint2d(0, 0), _
                             tg.CreatePoint2d(4, 0))

‘ Draw a second line from the end of the previous line to (4,3).
Dim line2 As Inventor.SketchLine
line2 = lines.AddByTwoPoints(line1.EndSketchPoint, _
                             tg.CreatePoint2d(4, 3))

‘ Draw a line connecting the last point to the first point.
Dim line3 As Inventor.SketchLine
line3 = lines.AddByTwoPoints(line2.EndSketchPoint, _
                             line1.StartSketchPoint)

 

The approach to creating sketch geometry that I’ve had the best personal success with is to create the sketch points first and then use those as input when creating the various sketch entities.  It’s easier for me to visualize the points I need if I map it out before hand and having the map helps to eliminate mistakes in my code.  For example, below is an example of what I sketched up to help me visualize the points that will be needed to create the slot.  Remember that arcs and circles require a sketch point at their center.  I’ve numbered the points starting with zero.  Those numbers will correspond to indices in an array in my program.

SlotSketch

Below is the code that demonstrates this approach.  It begins by creating the sketch points and then it uses those points to create the sketch geometry.  It becomes a dot-to-dot exercise to draw the geometry.  This examples illustrates lines and arcs, but all of sketch geometry uses the same concepts.  You’ll just be using different collection objects and different add methods within those collections.

‘ Get a reference to the SketchPoints collection.
Dim points As Inventor.SketchPoints = sketch.SketchPoints

‘ Get a reference to the SketchLines collection.
Dim lines As Inventor.SketchLines = sketch.SketchLines

‘ Get a reference to the SketchArcs collection.
Dim arcs As Inventor.SketchArcs = sketch.SketchArcs

‘ Create the sketch points.
Dim pointArray(5) As Inventor.SketchPoint
pointArray(0) = points.Add(tg.CreatePoint2d(0, 1), False)
pointArray(1) = points.Add(tg.CreatePoint2d(0, 0), False)
pointArray(2) = points.Add(tg.CreatePoint2d(0, -1), False)
pointArray(3) = points.Add(tg.CreatePoint2d(4, -1), False)
pointArray(4) = points.Add(tg.CreatePoint2d(4, 0), False)
pointArray(5) = points.Add(tg.CreatePoint2d(4, 1), False)

‘ Draw the geometry.
Dim arc1 As Inventor.SketchArc
arc1 = arcs.AddByCenterStartEndPoint(pointArray(1), pointArray(0), _
                                     pointArray(2))

Dim line1 As Inventor.SketchLi
ne
line1 = lines.AddByTwoPoints(pointArray(2), pointArray(3))

Dim arc2 As Inventor.SketchArc
arc2 = arcs.AddByCenterStartEndPoint(pointArray(4), pointArray(3), _
                                     pointArray(5))

Dim line2 As Inventor.SketchLine
line2 = lines.AddByTwoPoints(pointArray(5), pointArray(0))

The result after running the program is shown below, which is what’s expected.  Because the entities share sketch points at the connections, Inventor sees this as being closed so it can be used as input to create an extrusion.

SlotResult

However, if you try and edit the sketch by dragging the points it may not behave as you expect, as shown in the picture below.  This is because there aren’t any constraints to define the behavior.  Interactively, constraints are inferred during the sketching process by how you move your mouse, which can’t happen when using the API.  I’ll discuss adding constraints in an upcoming post.

ModifiedSlotResult

Note: One issue that I just discovered while writing this post is that the AddByCenterStartEndPoint method is not associating the input center sketch point with the new arc, but is instead creating a new point at that position.  This is not expected behavior and I’ll discuss how you can work around this issue in a later post.  Just know that when you see the extra center points that you didn’t do anything wrong.

-Brian


Comments

One response to “Drawing in a Sketch”

  1. Anton Avatar
    Anton

    Although very clear described one question remains:
    How is the 2Dpoint “linked” to the active sketch in the following code fragment.
    Dim tg As Inventor.TransientGeometry = invApp.TransientGeometry
    ‘ Draw a line between (0,0) and (4,0).
    Dim line1 As Inventor.SketchLine
    line1 = lines.AddByTwoPoints(tg.CreatePoint2d(0, 0), _
    tg.CreatePoint2d(4, 0))
    I’ll assume the active sketch supplies the Z-coordinate but how is the tg object linked to the active sketch?

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading