Horizontal Align Constraint with the Center Point of the Sketch

By Xiaodong Liang

The code below aligns the middle point of a sketch line with the center point of the sketch.

 

Sub AlignPoints()           ' assume we have got Inventor application     Dim oDocDef As PartComponentDefinition = _         _InvApplication.ActiveDocument.ComponentDefinition()          Dim oSketch As PlanarSketch = _                 oDocDef.Sketches(1)          Dim oSketchLine As SketchLine = _                _InvApplication.ActiveDocument.SelectSet(1)          'get center workpoint     Dim oCenterWorkPt As WorkPoint = _                   oDocDef.WorkPoints("Center Point")          'create the center point on sketch     Dim oCenterPt As SketchPoint = _                   oSketch.SketchPoints.Add( _                    oSketch.ModelToSketchSpace                      (oCenterWorkPt.Point))          'create the mid point of the line     Dim oMidPt As SketchPoint = _                   oSketch.SketchPoints.Add( _                   oSketchLine.Geometry.MidPoint)           'fix the center point      oSketch.GeometricConstraints.AddGround(oCenterPt)      'creates a new midpoint constraint between the point       ‘and line      oSketch.GeometricConstraints.AddMidpoint(                               oMidPt,                               oSketchLine)     'constraint the midpoint and center point by HorizontalAlign       oSketch.GeometricConstraints.AddHorizontalAlign(                              oCenterPt,                               oMidPt)     End Sub

Comments

One response to “Horizontal Align Constraint with the Center Point of the Sketch”

  1. Hi,
    I’m using VB 2010. When I run this code error comes “Parameter error” in
    Dim oSketchLine As SketchLine = _InvApplication.ActiveDocument.SelectSet(1)
    There is one part file and there is one sketch and there is one line object in sketch. I run this code when I in sketch edit mode. But not working?…
    Regards,
    Mucip:)

Leave a Reply to MucipCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading