save client graphics information along with the document and retrieve it back

By Xiaodong Liang

Issue

Is it possible to save client graphics created in a part document with the Inventor API? Is there an API for redrawing the client graphics using the saved graphics data?

Solution

Using the GraphicsDataSetsCollection.Add2 method will allow ClientGraphics to be saved with the file. 

If ClientGraphics are added to a ClientFeature along with a data set then the ClientFeature will take care of the redraw. Currently using a ClientFeature is the only way to persist the complete set of ClientGraphics. For more information, please take a look at "Visual Cues – ClientGraphics and InteractionGraphics" in the Overviews section of the Inventor API Help file for better understanding on GraphicsDataSet and how to use them.

To test the code below select a ClientFeature and run the macro. ClientGraphics will be added to the ClientFeature. The attached part file has a ClientFeature named Pocket1 that you can use to test. (Select ClientFeature "Pocket1").

  Download Testpart-Inventor2015

C# code 

publicstaticvoid testCS()
{            
 
    // assume we have had Inventor Application
    // _InventorApp
    try
    {                
        ClientFeature invCF =
                _InventorApp.ActiveDocument.SelectSet[1] 
                    asClientFeature;
 
        ClientFeatureDefinition invCFD = 
                                invCF.Definition;
        if (invCFD.ClientGraphicsCollection.Count > 0)
        {
            // Delete everything.
            for (int i = 0; 
                i < invCFD.ClientGraphicsCollection.Count;
                i++)
            {
                invCFD.ClientGraphicsCollection[i].Delete();
                _InventorApp.ActiveView.Update();
            }
        }
        else
        {
            // Get an arbitrary point on an edge of the             // feature, if the client feature has any              //elements.
            Inventor.Point oOrigin = null;
            if (invCFD.ClientFeatureElements.Count > 0)
            {
                PartFeature element = 
                    invCFD.ClientFeatureElements[1].Element
                                asPartFeature;
                oOrigin = 
                    element.Faces[1].Edges[1].PointOnEdge;
            }
            else
            {
                oOrigin =                       _InventorApp.TransientGeometry.CreatePoint(0, 0, 0);
            }
 
            GraphicsDataSets oGraphicsData = null;
            try
            {
                oGraphicsData = 
                    _InventorApp.ActiveDocument.
                        GraphicsDataSetsCollection[
                        "Client Feature Test"];
            }
            catch
            {
                oGraphicsData =                     _InventorApp.ActiveDocument.GraphicsDataSetsCollection.Add2(
                        "Client Feature Test", true);
            }
            GraphicsCoordinateSet oCoordSet = 
                oGraphicsData.CreateCoordinateSet(2);
 
            double[] adPoints = newdouble[9];
            adPoints[0] = oOrigin.X;
            adPoints[1] = oOrigin.Y;
            adPoints[2] = oOrigin.Z;
            adPoints[3] = oOrigin.X + 1;
            adPoints[4] = oOrigin.Y;
            adPoints[5] = oOrigin.Z;
            adPoints[6] = oOrigin.X + 1;
            adPoints[7] = oOrigin.Y + 0.5;
            adPoints[8] = oOrigin.Z;
            oCoordSet.PutCoordinates(ref adPoints);
 
            GraphicsNormalSet oNormalSet = 
                oGraphicsData.CreateNormalSet(2);
 
            oNormalSet.Add(1,
   _InventorApp.TransientGeometry.CreateUnitVector(0, 0, 1));
 
            GraphicsColorSet oColorSet = 
                   oGraphicsData.CreateColorSet(2);
            oColorSet.Add(1, 255, 255, 0);
            // Create some client graphics.
            ClientGraphics oClientGraphics = 
                invCFD.ClientGraphicsCollection.Add(
                                 "Client Feature Test");
            GraphicsNode oNode = 
                oClientGraphics.AddNode(2);
            //Create a triangle.
            TriangleGraphics oTriangle =
                oNode.AddTriangleGraphics();
            oTriangle.CoordinateSet = oCoordSet;
            oTriangle.NormalSet = oNormalSet;
            oTriangle.ColorSet = oColorSet;
            oTriangle.DepthPriority = 1;
            oNode.Selectable = true;
            _InventorApp.ActiveView.Update();
        }
    }
    catch
    {
    }
}

VBA code

Sub testVBA()
    'get the client feature
    Dim invCF As ClientFeature
    Set invCF = _
        ThisApplication.ActiveDocument.SelectSet(1)
    ' get defintion of the client feature
    Dim invCFD As ClientFeatureDefinition
    Set invCFD = invCF.Definition
    If invCFD.ClientGraphicsCollection.Count > 0 Then
        ' Delete everything.
        Dim i As Integer
        For i = 1 To invCFD.ClientGraphicsCollection.Count
            invCFD.ClientGraphicsCollection.Item(1).Delete
            ThisApplication.ActiveView.Update
        Next
    Else
        ' Get an arbitrary point on an edge of the feature,
        ' if the client feature has any elements.
        Dim oOrigin As Point
        If invCFD.ClientFeatureElements.Count > 0 Then
            Set oOrigin = invCFD.ClientFeatureElements.Item(1). _
               Element.Faces.Item(1).Edges.Item(1).PointOnEdge
        Else
            ' There isn't any geometry so just place
            ' the graphics at the model origin.
            Set oOrigin = _
              ThisApplication.TransientGeometry.CreatePoint
        End If
        Dim oGraphicsData As GraphicsDataSets
        On Error Resume Next
        Set oGraphicsData = _
           ThisApplication.ActiveDocument. _
              GraphicsDataSetsCollection.Item("Client Feature Test")
        If Err Then
            ' then the data will be stored with document saving
            Set oGraphicsData = _
               ThisApplication.ActiveDocument. _
                  GraphicsDataSetsCollection.Add2( _
                                 "Client Feature Test", True)
        End If
        Dim oCoordSet As GraphicsCoordinateSet
        Set oCoordSet = oGraphicsData.CreateCoordinateSet(2)
        Dim adPoints(8) As Double
        adPoints(0) = oOrigin.X
        adPoints(1) = oOrigin.Y
        adPoints(2) = oOrigin.Z
        adPoints(3) = oOrigin.X + 1
        adPoints(4) = oOrigin.Y
        adPoints(5) = oOrigin.Z
        adPoints(6) = oOrigin.X + 1
        adPoints(7) = oOrigin.Y + 0.5
        adPoints(8) = oOrigin.Z
        Call oCoordSet.PutCoordinates(adPoints)
        Dim oNormalSet As GraphicsNormalSet
        Set oNormalSet = oGraphicsData.CreateNormalSet(2)
        Call oNormalSet.Add(1, _
             ThisApplication.TransientGeometry.CreateUnitVector( _
                                                                                   0, 0, 1))
        Dim oColorSet As GraphicsColorSet
        Set oColorSet = oGraphicsData.CreateColorSet(2)
        Call oColorSet.Add(1, 255, 255, 0)
        ' Create some client graphics.
        Dim oClientGraphics As ClientGraphics
        Set oClientGraphics = _
           invCFD.ClientGraphicsCollection.Add("Client Feature Test")
        Dim oNode As GraphicsNode
        Set oNode = oClientGraphics.AddNode(2)
        ' Create a triangle.
        Dim oTriangle As TriangleGraphics
        Set oTriangle = oNode.AddTriangleGraphics
        oTriangle.CoordinateSet = oCoordSet
        oTriangle.NormalSet = oNormalSet
        oTriangle.ColorSet = oColorSet
        oTriangle.DepthPriority = 1
        oNode.Selectable = True
        ThisApplication.ActiveView.Update
    End If
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