ModelToSketchSpace Transformation Matrix

By Augusto Goncalves

The transformation matrix  for ModelToSketchSpace can be obtained from the PlanarSketch object. It wouldn’t be too difficult to calculate it ourselves, but a combination of the OriginPointGeometry, PlanarEntityGeometry, AxisEntityGeometry, and AxisIsX properties give us everything we need to compute this matrix.

The  function shown below accepts a PlanarSketch object as an argument and returns Matrix object which is required for Model to Sketch Space transformation. Try go through the code reading the comments.

Public Function createModelToSkecthMatrix( _

            ByVal sketch As PlanarSketch) As Matrix

  ‘ get the sketch origin point

  Dim origin As Point = sketch.OriginPointGeometry

  ‘ and the sketch axis

  Dim lineAxis As Line = sketch.AxisEntityGeometry

  ‘ to get the sketch direction

  Dim direction As UnitVector = lineAxis.Direction

 

  ‘ now the sketch plane

  Dim plane As Plane = sketch.PlanarEntityGeometry

  ‘ to then get the sketch normal (unit vector)

  Dim zAxisUnit As UnitVector = plane.Normal

  ‘ and get the vector

  Dim zAxis As Vector = zAxisUnit.AsVector

 

  ‘ finally get the X and Y axis according

  ‘ to the sketch property

  Dim xAxis As Vector

  Dim yAxis As Vector

  If (sketch.AxisIsX) Then

    ‘ X axis along the sketch direction

    xAxis = direction.AsVector

    ‘ and the Y axis as the cross product X-Z

    yAxis = zAxis.CrossProduct(xAxis)

  Else

    ‘ or the Y axis as the sketch direction

    yAxis = direction.AsVector

    ‘ and the X axis as the cross product Y-Z

    xAxis = yAxis.CrossProduct(zAxis)

  End If

 

  ‘ create a blank matrix

  Dim coordMatrix As Matrix = sketch.Application. _

    TransientGeometry.CreateMatrix

  ‘ finally set the matrix as a coordinate

  ‘ system matrix using the calculate data

  coordMatrix.SetCoordinateSystem( _

    origin, xAxis, yAxis, zAxis)

 

  Return coordMatrix

End Function


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading