Pass arguments to GetReferenceKey() and BindKeyToObject() methods in a .NET

By Wayne Brill

Passing arguments to GetReferenceKey() and BindKeyToObject() methods in .NET can be a little tricky. Here are a couple of things to be aware of:

When using an array, initialize it before passing it to a function that takes it as a reference parameter. (not as ‘out’). In C# a variable of the required type must be passed as ref or out parameter.

Here is a C# and VB.NET example. To test the code have an assembly open with at least one component.
 
C#

public void referenceKeytest()            {                Inventor.Application oApp =                     System.Runtime.InteropServices.        Marshal.GetActiveObject("Inventor.Application")                                as Inventor.Application;                Inventor.AssemblyDocument oAD =       oApp.ActiveDocument as Inventor.AssemblyDocument;                     // Initilization                byte[] abRefKey = new byte[] { };              // Get the first component in the assembly                Inventor.ComponentOccurrence oOcc =                 oAD.ComponentDefinition.Occurrences[1];                     MessageBox.Show(oOcc.Name.ToString());                          // Get the reference key                 oOcc.GetReferenceKey(ref abRefKey, 0);                     oOcc = null;                // Get the Reference key manager from                // the assembly (active document)                Inventor.ReferenceKeyManager oRKM =                               oAD.ReferenceKeyManager;                     object oType;                // Get the component using the reference key               Inventor.ComponentOccurrence oOcc_2 =     oRKM.BindKeyToObject(ref abRefKey, 0, out oType);                      MessageBox.Show(oOcc_2.Name.ToString());                 }

VB.NET

Sub referenceKeytest()                 Dim oApp As Inventor.Application =          System.Runtime.InteropServices.Marshal.           GetActiveObject("Inventor.Application")                 Dim oAD As Inventor.AssemblyDocument = _                                 oApp.ActiveDocument                 ' Initilization             Dim abRefKey() As Byte = New Byte() {}                 ' Get the first component in the assembly            Dim oOcc As Inventor.ComponentOccurrence =                oAD.ComponentDefinition.Occurrences(1)                 MessageBox.Show(oOcc.Name.ToString())                 ' Get the reference key             Call oOcc.GetReferenceKey(abRefKey)         &#160
;  oOcc = Nothing                 ' Get the Reference key manager from            ' the assembly (active document)            Dim oRKM As Inventor.ReferenceKeyManager = _                oAD.ReferenceKeyManager                 ' Get the component using the reference key            Dim oOcc_2 As Inventor.ComponentOccurrence            oOcc_2 = oRKM.BindKeyToObject _                            (abRefKey, , )                 MessageBox.Show(oOcc_2.Name.ToString())        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