Inventor API: Create constraints for 3D Sketch

by Vladimir Ananyev

Q: I need an example of creating constraints for Sketch3D entities, and I also need to control these entities through parameters. How can this be done using the API?

A: Constraints and dimensions of Sketch3D are similar to those of Sketch 2D. Here is a VBA example. The code will add a new Sketch3D object to an existing Part, then create two lines, add a parallel constraint between them and finally add a length constraint to one of the lines. You can then use the parameter of the length constraint to drive programmatically the dimension of the constrained line.

Note: a part document needs to be the active.

 

Sub AddSketch3DConstraints()

 

  Dim oDoc As PartDocument

  Set oDoc = ThisApplication.ActiveDocument

 

  Dim oCompDef As PartComponentDefinition

  Set oCompDef = oDoc.ComponentDefinition

 

  ‘Add a new Sketch3D

  Dim oSketch3d As Sketch3D

  Set oSketch3d = oCompDef.Sketches3D.Add

 

  Dim oTG As TransientGeometry

  Set oTG = ThisApplication.TransientGeometry

 

  ‘Add two lines to the sketch

  Dim oLine1 As SketchLine3D

  Dim oLine2 As SketchLine3D

 

  Set oLine1 = oSketch3d.SketchLines3D _

    .AddByTwoPoints(oTG.CreatePoint(0, 0, 0), _

                    oTG.CreatePoint(10, 10, 10))

  Set oLine2 = oSketch3d.SketchLines3D _

    .AddByTwoPoints(oTG.CreatePoint(0, 10, 0), _

                    oTG.CreatePoint(10, 15, 15))

  

  ‘ Create a parallel constraint between the lines

  Dim oParallelConstr3D   As ParallelConstraint3D

  Set oParallelConstr3D = oSketch3d _

    .GeometricConstraints3D.AddParallel(oLine1, oLine2)

 

  ‘ Create length constraint for line 1

  Dim oLineLengthConstr3D As LineLengthDimConstraint3D

  Set oLineLengthConstr3D = oSketch3d _

    .DimensionConstraints3D.AddLineLength(oLine1)

 

  ‘ Set Driven Property to False so we are sure

  ‘ Parameter is a ModelParameter

  oLineLengthConstr3D.Driven = False

 

  Dim oModelParam As ModelParameter

  Set oModelParam = oLineLengthConstr3D.Parameter

 

  ‘set new value in base units (cm)

  oModelParam.value = 15#

   

<

p style=”line-height: normal;margin: 0cm 0cm 0pt” class=”MsoNoSpacing”>End Sub


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading