add or remove nodes from client graphics

By Xiaodong Liang

Issue

If I create my own instance of ClientGraphics, is it possible to add/remove nodes from it?

Solution

Yes, it's possible. When you create your own instance of ClientGraphics you need to use the ClientGraphics.AddNode method to add graphics nodes under it. Each node can contain various graphics primitives (lines, line strips, triangles, triangle strips etc.). When adding your node, you need to specify Node Id (of type Long), like below code does:

Dim oClientGraphics As ClientGraphics
'... ...
Dim oNode As GraphicsNode
oNode = oClientGraphics.AddNode(1)

The Node Id can be used to retrieve a node from the ClientGraphics instance to modify its attributes or delete it, as below code:

Dim oNode As GraphicsNode =
     oClientGraphics.ItemById(1)
  oNode.Delete()

The following is the sample code (VB.NET sample code is attached at the bottom), which shows usage of such GraphicsNode manipulation. It contains the following routines: CreateBaseGraphics – initializes GraphicsDataSets, creates ClientGraphics and displays triangle from line primitives.

' constants
Private Const kDataSetName = "DevTech" 'name of data set
Private Const kGraphicsName = "DevTechGraphics" ' name of
Private Const kCoordSetID = 1
Private Const kIndexSetID = 2
Private Const kLineNodeID = 1
Private Const kPointNodeID = 2
 
' add the client graphics for test
' 3 client lines
Private Sub CreateBaseGraphics()
 

Dim m_inventorApp As Application
m_inventorApp =
    System.Runtime.InteropServices.Marshal.
    GetActiveObject("Inventor.Application")
 
Dim oDoc As PartDocument
oDoc = m_inventorApp.ActiveDocument
Dim oCompDef As PartComponentDefinition
 
oCompDef = oDoc.ComponentDefinition
If oCompDef Is Nothing Then
    Exit Sub
End If
 
Dim oDataSets As GraphicsDataSets
' try find data set, if doesn't exist create new one
Try
    oDataSets =
        oDoc.GraphicsDataSetsCollection.Item(
            kDataSetName)
Catch ex As Exception
End Try
 
If oDataSets Is Nothing Then
    oDataSets =
        oDoc.GraphicsDataSetsCollection.Add(
            kDataSetName)
End If
 
' find coordinate set
Dim oCoordSet As GraphicsCoordinateSet
Try
    oCoordSet =
        oDataSets.ItemById(kCoordSetID)
Catch ex As Exception
End Try
 
If oCoordSet Is Nothing Then
    ' doesn't exist yet, create new one
    oCoordSet =
        oDataSets.CreateCoordinateSet(kCoordSetID)
    ' set coordinates
    Dim coords(9) As Double
 
    coords(1) = 0.0#
    coords(2) = 0.0#
    coords(3) = 0.0#
    coords(4) = 4.0#
    coords(5) = 0.0#
    coords(6) = 0.0#
    coords(7) = 2.0#
    coords(8) = 4.0#
    coords(9) = 0.0#
    oCoordSet.PutCoordinates(coords)
End If
' find data set
Dim oIndexSet As GraphicsIndexSet
Try
    oIndexSet =
        oDataSets.ItemById(kIndexSetID)
Catch ex As Exception
End Try
If oIndexSet Is Nothing Then
    ' create new one
    oIndexSet =
        oDataSets.CreateIndexSet(kIndexSetID)
 
    'set indices
    Dim indices(5) As Integer
    indices(0) = 1
    indices(1) = 2
    indices(2) = 2
    indices(3) = 3
    indices(4) = 3
    indices(5) = 1
    oIndexSet.PutIndices(indices)
End If
 
' find client graphics
Dim oClientGraphics As ClientGraphics
Try
    oClientGraphics =
        oCompDef.ClientGraphicsCollection.Item(
            kGraphicsName)
Catch ex As Exception
End Try
 
If oClientGraphics Is Nothing Then
    ' doesn't exist, create new one
    oClientGraphics =
        oCompDef.ClientGraphicsCollection.Add(
            kGraphicsName)
    ' add nodes
    Dim oNode As GraphicsNode
 
    oNode =
        oClientGraphics.AddNode(kLineNodeID)
    Dim oLineGraphics As LineGraphics
 
    oLineGraphics = oNode.AddLineGraphics
    oLineGraphics.CoordinateSet = oCoordSet
    oLineGraphics.CoordinateIndexSet = oIndexSet
    oLineGraphics.DepthPriority = 1
End If
m_inventorApp.ActiveView.Update()

End Sub
' add client points to each vertex of the lines
'  It's necessary to run CreateBaseGraphics first.
Sub AddPointGraphics()
 

Dim m_inventorApp As Application
m_inventorApp =
    System.Runtime.InteropServices.Marshal.
    GetActiveObject("Inventor.Application")
 
Dim oDoc As PartDocument
 
On Error Resume Next
oDoc = m_inventorApp.ActiveDocument
Dim oCompDef As PartComponentDefinition
 
oCompDef = oDoc.ComponentDefinition
If oCompDef Is Nothing Then
    Exit Sub
End If
 
Dim oDataSets As GraphicsDataSets
 
oDataSets = oDoc.GraphicsDataSetsCollection(kDataSetName)
If oDataSets Is Nothing Then
    Exit Sub
End If
Dim oCoordSet As GraphicsCoordinateSet
 
oCoordSet = oDataSets.ItemById(kCoordSetID)
If oCoordSet Is Nothing Then
    Exit Sub
End If
Dim oIndexSet As GraphicsIndexSet
 
oIndexSet = oDataSets.ItemById(kIndexSetID)
If oIndexSet Is Nothing Then
    Exit Sub
End If
'now create point graphics
Dim oClientGraphics As Inventor.ClientGraphics
 
oClientGraphics =
    oCompDef.ClientGraphicsCollection.Item(kGraphicsName)
If oClientGraphics Is Nothing Then
    Exit Sub
End If
Dim oNode As GraphicsNode
 
oNode =
    oClientGraphics.AddNode(kPointNodeID)
If oNode Is Nothing Then
    Exit Sub
End If
Dim oPointGraphics As PointGraphics
 
oPointGraphics = oNode.AddPointGraphics
oPointGraphics.CoordinateSet = oCoordSet
oPointGraphics.CoordinateIndexSet = oIndexSet
oPointGraphics.PointRenderStyle =
    PointRenderStyleEnum.kFilledCirclePointStyle
oPointGraphics.DepthPriority = 2
'repaint view
m_inventorApp.ActiveView.Update()

End Sub
' remove client points  
Sub RemovePointGraphics()
 

Dim m_inventorApp As Application
m_inventorApp =
    System.Runtime.InteropServices.Marshal.GetActiveObject(
        "Inventor.Application")
Dim oDoc As PartDocument
oDoc = m_inventorApp.ActiveDocument
Dim oCompDef As PartComponentDefinition
oCompDef = oDoc.ComponentDefinition
If oCompDef Is Nothing Then
    Exit Sub
End If
Dim oClientGraphics As ClientGraphics
 
Try
    oClientGraphics =
        oCompDef.ClientGraphicsCollection.Item(
            kGraphicsName)
Catch ex As Exception
End Try
If oClientGraphics Is Nothing Then
    Exit Sub
End If
Dim oNode As GraphicsNode
 
oNode =
    oClientGraphics.ItemById(kPointNodeID)
oNode.Delete()
'repaint
m_inventorApp.ActiveView.Update()

End Sub
 
 

' change style of client point
Sub ChangePointStyle()

Dim m_inventorApp As Application  
m_inventorApp =
    System.Runtime.InteropServices.Marshal.
    GetActiveObject("Inventor.Application")
 
Dim oDoc As PartDocument
oDoc = m_inventorApp.ActiveDocument
Dim oCompDef As PartComponentDefinition
 
oCompDef = oDoc.ComponentDefinition
If oCompDef Is Nothing Then
    Exit Sub
End If
Dim oClientGraphics As Inventor.ClientGraphics
 
oClientGraphics = oCompDef.ClientGraphicsCollection.Item(kGraphicsName)
If oClientGraphics Is Nothing Then
    Exit Sub
End If
Dim oNode As GraphicsNode
 
oNode = oClientGraphics.ItemById(kPointNodeID)
For i = 1 To oNode.Count
    Dim oPointGraphics As PointGraphics
 
    oPointGraphics = oNode.Item(i)
    oPointGraphics.PointRenderStyle = 
        PointRenderStyleEnum.kCrossPointStyle
Next
'repaint
m_inventorApp.ActiveView.Update()

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