In the previous post in this series I discussed creating sketch geometry. The last post ended by showing how to create lines and arcs that define the shape of a slot. It also illustrated that the lines and arcs didn’t behave quite like expected because of missing constraints.
In this post we’ll look at how to create geometric sketch constraints using the API. There are two types of sketch constraints; geometric and dimension. I’ll focus on geometric constraints for this post.
Geometric sketch constraints are used extensively within Inventor, probably in more ways than you would initially think. Below is a portion of the sketch ribbon, where I’ve highlighted the commands you typically think of as the geometric constraint commands.
These constraints define the behavior of sketch geometry. There are two types of geometric constraints; those that act on a single geometric entity and those that define some type of relationship between two geometric entities. For example, the Fix (Ground), Horizontal, and Vertical constraints act on a single entity, whereas all of the others control two entities.
All of these are quite easy to use through the API. The diagram below shows the object model for 2d sketch geometric constraints. You’ve already learned from the previous posts how to access a sketch through the API. The SketchLines and SketchArcs collections on the Sketch were used to drawing the slot geometry. Now you’ll use the GeometricConstraints collection to add the needed geometric constraints. Unlike the sketch geometry where there was a unique collection for each type of geometry, for geometric constraints there is a single collection for all of the constraint types. The GeometricConstraints collections supports many Add methods to allow the creation of each of the types of constraints. It’s also through this collection where you can access existing constraints.
Below is the program from the last post that creates the slot shape. The new portion of code at the bottom, highlighted in yellow, creates the constraints. You can see that it uses the variables that reference geometry created earlier in the program.
‘ 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.SketchLine
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))
‘ Get a reference to the GeometricConstraints collection.
Dim geomConstraints As Inventor.GeometricConstraints
geomConstraints = sketch.GeometricConstraints
‘ Create the four tangent constraints.
geomConstraints.AddTangent(arc1, line1)
geomConstraints.AddTangent(line1, arc2)
geomConstraints.AddTangent(arc2, line2)
geomConstraints.AddTangent(line2, arc1)
‘ Create a parallel constraint.
geomConstraints.AddParallel(line1, line2)
‘ Create a horizontal constraint.
geomConstraints.AddHorizontal(line1)
Creating any of the other types of constraints is similar to the above with the difference being the add method that’s used. The API help is a great resource to help in understanding the various add methods of the GeometricConstraints object.
I mentioned earlier that geometric constraints are used by Inventor in more ways than most people think. In addition to the typical geometric constraints as discussed above, there are several other geometry creation commands that result in the creation of geometric constraints. For example, when you create an offset, new geometry is created and Inventor creates constraints between the original geometry and the new geometry so the new geometry behaves as an offset. You can delete the constraint to remove the offset behavior. The picture below shows a simple sketch.
Below is the same sketch after offsetting the outside profile and creating a rectangular pattern of the circle.
Finally, the picture below shows the constraint symbols turned on so you can see all of the constraints that were created automatically as a result of the previous operations. The API provides query access to all of these constraints through the GeometricConstraints collection.







Leave a Reply