In this post, on our slow journey towards creating a command that creates a slot shape in a sketch, I’m going to discuss the main principles when working with sketches.
You can create a sketch in a part or assembly but I’m only going to address parts for now. A sketch is basically a 2D coordinate system positioned in 3D space. The API has a special type of object for this type of sketch called a PlanarSketch since it’s defining a plane in 3D space. There is also a DrawingSketch which is slightly different since a drawing is already 2D. Typically, sketches are created by referencing an existing planar entity, either a planar face or a work plane. The sketch is associatively related to the entity so that if the entity moves the sketch moves with it.
When you pick a face or work plane, Inventor has some logic that it uses to determine the orientation and origin of the 2D coordinate system that’s defined during the creation of the sketch. You typically don’t care about this orientation because the default is usually what you expect and the main thing it affects is the horizontal and vertical directions. The origin doesn’t matter since you dimension the sketch geometry relative to other geometry in the model so the actual x-y coordinates of the sketch geometry is immaterial. The picture below shows a selected face on a part where I want to create a sketch.
After creating the sketch it is possible to see and even edit the coordinate system that was defined for this sketch. You can do this using the “Edit Coordinate System” command that’s accessed through the context menu for the sketch. A 2D triad is displayed indicating the origin and the direction of the x and y axes. While in this command you can edit these to reorient the sketch on the plane.
The yellow lines you see in the picture above are sketch lines that were automatically created by projecting the edges of the selected face onto the sketch. This is the default behavior of Inventor but is controlled using the setting in the Application Options dialog as shown below.
To create a sketch using the API you need to use the collection object for sketches. Remember from the discussion in the previous post that collection objects provide access to existing objects of a certain type and also support methods to create new objects of that type. Below is a simplified view of the object hierarchy to get to the PlanarSketches object. We’ve already discussed how to create a PartDocument object but most of the data that we think of as part specific data, (features, work features, parameters, imates, etc.) are part of the PartComponentDefinition object. There is always a one-to-one relationship between a PartDocument and a PartComponentDefinition object. Don’t get too caught up in why there is a PartComponentDefinition object but instead just remember that for part information you’ll need to take that one extra step to get the PartComponentDefinition from the PartDocument.
Below is some code that creates a new part document and creates a new sketch on the X-Y work plane of the part.
' Connect to a running instance of Inventor.
Dim invApp As Inventor.Application
invApp = System.Runtime.InteropServices.Marshal.GetActiveObject _
("Inventor.Application")
' Get the Documents collection.
Dim docs As Inventor.Documents docs = invApp.Documents
' Create a new part document.
Dim partDoc As Inventor.PartDocument
partDoc = docs.Add(Inventor.DocumentTypeEnum.kPartDocumentObject)
' Get the component definition.
Dim partDef As Inventor.PartComponentDefinition
partDef = partDoc.ComponentDefinition
' Get the X-Y work plane.
Dim xyPlane As Inventor.WorkPlane = partDef.WorkPlanes.Item(3)
' Create a new sketch.
Dim sketch As Inventor.PlanarSketch
sketch = partDef.Sketches.Add(xyPlane, False)
The code above uses the ComponentDefinition property of the PartDocument object to get the PartComponentDefinition object associated with the part. In most cases the name of the property matches the name of the object is returns but that’s not always the case as in this example.
Next it gets the X-Y work plane from the part. It does this by getting the WorkPlanes collection object from the PartComponentDefinition and gets a work plane from it using the Item property. The WorkPlanes collection object contains all of the work planes in a part and also supports several Add methods to create new work planes. All parts will always have at least three work planes, three work axes, and one work point. Below is a picture of the browser for a new, empty part. You can see the base work features that exist in every part. When accessing these through the API they are returned by the API collection objects in the same order they’re shown in the browser. For example, getting WorkPlanes.Item(1) will return the YZ Plane. Getting WorkAxes.item(2) will return the Y axis. and getting WorkPoints.Item(1) will always return the origin work point. The user cannot delete or reorder these base work features so this is guaranteed to always be true. In this case I’m getting item 3 from the WorkPlanes collection, which is the XY Plane.
The last section of code, shown below, creates a new sketch. There are a couple of things that I want to point out here. There are several things happening on the line where the sketch gets created. First, the Sketches property of the PartComponentDefinition is called. This returns a PlanarSketches object. Next, the Add method of the PlanarSketches object is called. This line could have been broken up into multiple lines by first getting the PlanarSketches object and then calling the Add method. Most languages support the ability to combine these calls into a single line to allow for less code. Just be aware that there are several calls being made on that single line, which can sometimes make problems a little more difficult to debug because if that line fails it’s difficult to know which call failed.
' Create a new sketch.
Dim sketch As Inventor.PlanarSketch
sketch = partDef.Sketches.Add(xyPlane, False)
The Add method to create a new sketch is fairly simple. It has two arguments. The first is the planar object you want to create the sketch on. This can be a work plane, like in this example, or it can be a planar face. The second argument indicates if the edges of the face should be copied into the sketch. This is the equivalent of the “Auto Project” option discussed above. The API ignores the application setting for the auto project and allows you to control it explicitly when you create a sketch. This setting is only used when you’re creating a sketch on a face and is ignored when a work plane is used. In fact the second argument is defined to be an optional argument so I could have left it out. It defaults to False if you don’t specify it.
The work plane created is the same as if you had interactively created it in Inventor, in that the origin and orientation of the plane is automatically inferred by Inventor. The PlanarSketches collection also supports an AddWithOrientation method that lets you specify the origin and orientation when you create it. In practice this is rarely used.
If you run the code above it will create a new part and you’ll see a new empty sketch in the part. Each time you run the program you get a new part with a sketch. Not too exciting yet, but in the next post we’ll look at drawing geometry on the sketch. Then it gets a lot more fun .
-Brian