Create shapes like cones with Client Graphics

By Wayne Brill

ClientGraphics in the Inventor API allow you to draw your own graphics through some basic primitive. (other ways are available too). The following VBA and VB.NET examples create a circle or a cone with the ClientGraphics API. (Open a part and run TestProcCG)

This Mod the Machine post on client graphics may also be of interest.

VBA

‘A small utility method that will compute
‘a normal vector to the one in input
Public Function GetNormalVector _
         (oVector As Vector) As UnitVector

    Dim oOutput As UnitVector

    If oVector.X <> 0 Then
   
        Set oOutput = ThisApplication. _
        TransientGeometry.CreateUnitVector _
(-(oVector.Y + oVector.Z) / oVector.X, 0, 1)
        Set GetNormalVector = oOutput
       
    Else
   
        Set oOutput = ThisApplication. _
                    TransientGeometry. _
               CreateUnitVector(1, 0, 0)
        Set GetNormalVector = oOutput
   
    End If

End Function

Public Sub DrawCone(oDataSets As GraphicsDataSets, _
            oClientGraphics As ClientGraphics, _
            Center As point, _
            Height As Double, _
            Normal As Vector, _
            Radius As Double, _
            NbTriangles As Long, _
            oStyle As RenderStyle)

    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry
              
    Dim oCoordSet As GraphicsCoordinateSet
    Set oCoordSet = oDataSets. _
      CreateCoordinateSet(oDataSets.count + 1)
   
   ‘Normalize direction
    Dim NormalUnit As UnitVector
    Set NormalUnit = Normal.AsUnitVector
   
    ‘Get a normal vector to the direction
    Dim DirUnit As UnitVector
    Set DirUnit = GetNormalVector(Normal)
   
    ‘Angle of rotation
    Dim AngleRad As Double
    AngleRad = _
          Math.Atn(1) * 8 / NbTriangles
          ‘ = 2 * Pi / NbTriangles
   
   ‘Create a rotation matrix to compute
    ‘triangle points
    Dim oRotMatrix As Matrix
    Set oRotMatrix = oTG.CreateMatrix
    Call oRotMatrix.SetToRotation _
            (AngleRad, Normal, Center)
   
   
    Dim oPointCoords() As Double
   
    ReDim oPointCoords _
            (1 To 3 * (2 + NbTriangles))
          
    ‘First point is the center
    oPointCoords(1) = _
    Center.X + Height * NormalUnit.X
    oPointCoords(2) = _
    Center.Y + Height * NormalUnit.Y
    oPointCoords(3) = _
    Center.Z + Height * NormalUnit.Z
   
   ‘Other points are the base points
    ‘of the triangles
    Dim index As Long
    For index = 1 To NbTriangles
   
        Dim oBasePoint As point
        Set oBasePoint = oTG.CreatePoint( _
        Center.X + DirUnit.X * Radius, _
        Center.Y + DirUnit.Y * Radius, _
        Center.Z + DirUnit.Z * Radius)
       
        Call DirUnit.TransformBy(oRotMatrix)
   
        oPointCoords(3 * index + 1) = oBasePoint.X
        oPointCoords(3 * index + 2) = oBasePoint.Y
        oPointCoords(3 * index + 3) = oBasePoint.Z
   
    Next
                                              
    Call oCoordSet.PutCoordinates(oPointCoords)
       
    Dim oGraphicNode As GraphicsNode
    Set oGraphicNode = oClientGraphics.AddNode _
                       (oClientGraphics.count + 1)
   
    Dim oTriangleSet As TriangleFanGraphics
    Set oTriangleSet = oGraphicNode. _
                           AddTriangleFanGraphics
   
    Dim oIndex As GraphicsIndexSet
    Set oIndex = oDataSets.CreateIndexSet _
                           (oDataSets.count + 1)
   
    For index = 1 To (NbTriangles + 1)
        Call oIndex.Add(index, index)
    Next
   
    ‘Last triangle join the last and first point
    Call oIndex
.Add(NbTriangles + 2, 2)
   
   
    oGraphicNode.RenderStyle = oStyle

    oTriangleSet.CoordinateSet = oCoordSet
    oTriangleSet.CoordinateIndexSet = oIndex
   
End Sub

Public Sub TestProcCG()

    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
   
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry
   
    Dim oCompDef As ComponentDefinition
    Set oCompDef = oDoc.ComponentDefinition
      
    On Error Resume Next
      
    Dim oDataSets As GraphicsDataSets
    Set oDataSets = oDoc. _
       GraphicsDataSetsCollection.Add("TestCG")
   
    If Err Then
        Set oDataSets = _
      oDoc.GraphicsDataSetsCollection("TestCG")
    End If
   
    Dim oClientGraphics As ClientGraphics
    Set oClientGraphics = oCompDef. _
         ClientGraphicsCollection.Add("TestCG")
   
    If Err Then
        Set oClientGraphics = _
    oCompDef.ClientGraphicsCollection("TestCG")
    End If
   
       
    Call DrawCone(oDataSets, oClientGraphics, _
                oTG.CreatePoint(0, 0, 0), 10, _
                   oTG.CreateVector(0, 1, 0), _
                                       5, 10, _
    oDoc.RenderStyles.Item("Glass (Limo Tint)"))
   
    Call DrawCone(oDataSets, oClientGraphics, _
               oTG.CreatePoint(15, 0, 0), 10, _
                   oTG.CreateVector(0, 1, 0), _
                                      5, 100, _
   oDoc.RenderStyles.Item("Glass (Limo Tint)"))
   
    Call DrawCone(oDataSets, oClientGraphics, _
                oTG.CreatePoint(30, 0, 0), 0, _
                   oTG.CreateVector(0, 1, 0), _
                                      5, 100, _
   oDoc.RenderStyles.Item("Glass (Limo Tint)"))
   
    ThisApplication.ActiveView.Update

End Sub

VB.NET

Public Class Form1             Dim m_inventorApp As Inventor.Application _                                          = Nothing             Private Sub Button1_Click(ByVal sender As  _                                    System.Object, _                      ByVal e As System.EventArgs) _                            Handles Button1.Click                 ' Get an active instance of Inventor            Try                m_inventorApp = System.Runtime. _                       InteropServices.Marshal. _           GetActiveObject("Inventor.Application")                 Catch 'Inventor not started                System.Windows.Forms.MessageBox. _                 Show("Start an Inventor session")                Exit Sub            End Try                 'Call the Sub            TestProcCG()        End Sub             Public Sub TestProcCG()                 Dim oDoc As Document            oDoc = m_inventorApp.ActiveDocument            Dim oTG As TransientGeometry            oTG = m_inventorApp.TransientGeometry            Dim oCompDef As ComponentDefinition            oCompDef = oDoc.ComponentDefinition                 Dim oDataSets As GraphicsDataSets            Try                oDataSets = oDoc. _              GraphicsDataSetsCollection.Add("TestCG")                Try                    oDataSets = oDoc. _                     GraphicsDataSetsCollection("TestCG")                    Dim oClientGraphics As ClientGraphics                    oClientGraphics = oCompDef. _                   ClientGraphicsCollection.Add("TestCG")                    Try                        oClientGraphics = oCompDef. _                      ClientGraphicsCollection("TestCG")                        DrawCone(oDataSets, _                                      oClientGraphics, _                         oTG.CreatePoint(0, 0, 0), 10, _                            oTG.CreateVector(0, 1, 0), _                                                5, 10, _            oDoc.RenderStyles.Item("Glass (Limo Tint)"))                             DrawCone(oDataSets, _                                      oClientGraphics, _                        oTG.CreatePoint(15, 0, 0), 10, _                            oTG.CreateVector(0, 1, 0), _                                               5, 100, _            oDoc.RenderStyles.Item("Glass (Limo Tint)"))                             DrawCone(oDataSets, _                                     oClientGraphics, _                        oTG.CreatePoint(30, 0, 0), 0, _                           oTG.CreateVector(0, 1, 0), _                                              5, 100, _           oDoc.RenderStyles.Item("Glass (Limo Tint)"))                             m_inventorApp.ActiveView.Update()                    Catch ex As Exception                    End Try                Catch ex As Exception                End Try            Catch ex As Exception            End Try        End Sub             'A small utility method that will         'compute a normal vector to the         'one in input        Public Function GetNormalVector _            (ByVal oVector As Vector) As  _                                UnitVector                 Dim oOutput As UnitVector            If oVector.X  0 Then                oOutput = m_inventorApp. _      TransientGeometry.CreateUnitVector _             (-(oVector.Y + oVector.Z) / _                          oVector.X, 0, 1)                GetNormalVector = oOutput            Else                oOutput = m_inventorApp. _                      TransientGeometry. _                 CreateUnitVector(1, 0, 0)                GetNormalVector = oOutput            End If        End Function        Public Sub DrawCone(ByVal oDataSets _                       As GraphicsDataSets, _    ByVal oClientGraphics As ClientGraphics, _        ByVal Center As Point, _        ByVal Height As Double, _        ByVal Normal As Vector, _        ByVal Radius As Double, _        ByVal NbTriangles As Long, _        ByVal oStyle As RenderStyle)            Dim oTG As TransientGeometry            oTG = m_inventorApp. _                       TransientGeometry            Dim oCoordSet As  _                   GraphicsCoordinateSet            oCoordSet = oDataSets. _                     CreateCoordinateSet _                     (oDataSets.Count + 1)            'Normalize direction            Dim NormalUnit As UnitVector            NormalUnit = Normal.AsUnitVector            'Get a normal vector to             'the direction            Dim DirUnit As UnitVector            DirUnit = GetNormalVector(Normal)            'Angle of rotation            Dim AngleRad As Double            AngleRad = _                Math.Atan(1) * 8 / NbTriangles            ' = 2 * Pi / NbTriangles            'Create a rotation matrix to             'compute triangle points            Dim oRotMatrix As Matrix            oRotMatrix = oTG.CreateMatrix            Call oRotMatrix.SetToRotation _                (AngleRad, Normal, Center)                 Dim oPointCoords() As Double            ReDim oPointCoords _                (0 To 3 * (2 + NbTriangles))            'First point is the center            oPointCoords(1) = _            Center.X + Height * NormalUnit.X            oPointCoords(2) = _            Center.Y + Height * NormalUnit.Y            oPointCoords(3) = _            Center.Z + Height * NormalUnit.Z            'Other points are the base             'points of the triangles            Dim index As Long            For index = 1 To NbTriangles                Dim oBasePoint As Point                oBasePoint = oTG.CreatePoint( _                Center.X + DirUnit.X * Radius, _                Center.Y + DirUnit.Y * Radius, _                Center.Z + DirUnit.Z * Radius)                Call DirUnit.TransformBy _                                    (oRotMatrix)                oPointCoords(3 * index + 1) = _                                    oBasePoint.X                oPointCoords(3 * index + 2) = _                                    oBasePoint.Y                oPointCoords(3 * index + 3) = _                                    oBasePoint.Z            Next            Call oCoordSet.PutCoordinates _                                  (oPointCoords)            Dim oGraphicNode As GraphicsNode            oGraphicNode = oClientGraphics. _             AddNode(oClientGraphics.Count + 1)            Dim oTriangleSet As  _                            TriangleFanGraphics            oTriangleSet = oGraphicNode. _                         AddTriangleFanGraphics            Dim oIndex As GraphicsIndexSet            oIndex = oDataSets.CreateIndexSet _                          (oDataSets.Count + 1)            For index = 1 To (NbTriangles + 1)                Call oIndex.Add(index, index)            Next            'Last triangle join the last            ' and first point            Call oIndex.Add(NbTriangles + 2, 2)                 oGraphicNode.RenderStyle = oStyle            oTriangleSet.CoordinateSet = oCoordSet            oTriangleSet.CoordinateIndexSet = oIndex        End Sub         End Class

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading