Accessing Inventor Parameters

By Augusto Goncalves

Parameters are used to define design intent while developing parts, particularly to define the size and shape of features and to control the relative positioning of components within assemblies. Equations could also be used to define the relationships between parameters and parameters could be used to relate dimensions to functional requirements. Each time a dimension or other measurement is added to a model, the value is established as a parameter for the model. Inventor parameters can be classified into three categories:

Model Parameters: Model parameters are associated and automatically generated during the modeling process. Each time a dimension or feature is added to the model, model parameters are assigned.

User Parameters: User parameters are parameters that are added by the user to convey design intent and are defined to drive dimensions and features in the model. Once they have been added, model parameters can be used to reference user parameters.

Linked Parameters: Linked parameters are parameters that are linked/embedded from an external spreadsheet. When a spreadsheet is linked, a link to the file is added, any changes to the parameters have to be done by opening the file in Excel and then saved.

The Inventor API provides access to the above set of parameters as well as functionality to modify parameter names/values. The Parameters collection object can be accessed using the ComponentDefinition object which is in turn obtained from the Document object. The following sample code shows how to access the Parameters object which represents all the parameters that are associated with the particular Inventor document, but this can in turn be used to query for the different parameter types.

The VB.NET sample below show how to access the Model parameters and displays the names and values. Parameter names and values can also be changed either by iterating through the entire collection of the particular parameter type or directly if the name of the parameter is known.

Excel spreadsheets can also be linked using the API, the ParameterTables collection represents the collection of all linked/embedded Excel spreadsheets. The file should be formatted with 2 columns: name and value.  To access the parameters that have been linked one could use the TableParameters collection object (obtained in turn from the ParameterTableFile object).

Public Shared Sub ShowParameters()

   ‘ get Inventor

   Dim m_inventorApp As Inventor.Application = _

     GetInventorApp()

   If (m_inventorApp Is Nothing) Then Exit Sub

   m_inventorApp.Visible = True

 

   ‘ get a part document

   Dim oDoc As Inventor.Document

   If (m_inventorApp.ActiveDocumentType <> _

       DocumentTypeEnum.kPartDocumentObject) Then

     oDoc = m_inventorApp.Documents.Add( _

       DocumentTypeEnum.kPartDocumentObject)

   Else

     oDoc = m_inventorApp.ActiveDocument

   End If

 

   ‘ *******************

   ‘ show all parameters

   ‘ list of parameters

   Dim allParams As Parameters = oDoc. _

     ComponentDefinition.Parameters

   If allParams.Count > 0 Then

     Dim paramValues As New System.Text.StringBuilder

     For Each param As Parameter In allParams

 

       ‘ append the param name, type and status to the string

       paramValues.AppendFormat("{0}: {1} – {2}{3}", _

                                param.Name, _

                                param.ParameterType, _

                                param.HealthStatus, _

                                System.Environment.NewLine)

     Next

     MessageBox.Show(paramValues.ToString(), "All Parameters")

   End If

 

   ‘ *********************

   ‘ show model parameters

   Dim modelParams As ModelParameters = allParams.ModelParameters

   If modelParams.Count > 0 Then

     Dim paramValues As New System.Text.StringBuilder

     For Each modelParam As ModelParameter In modelParams

 

       ‘ append the param name, value and unit to the string

       paramValues.AppendFormat("{0}: {1} ({2}){3}",
_

                                modelParam.Name, _

                                modelParam.ModelValue, _

                                modelParam.Units, _

                                System.Environment.NewLine)

 

       ‘ NOTE: the value will be show using Inventor

       ‘ internal units, e.g. cm or kilograms

     Next

     MessageBox.Show(paramValues.ToString(), "Model Parameters")

   End If

 

   ‘ select a XLS file

   ‘ using Windows.Form open file dialog, which is better when

   ‘ calling from out-of-process due change of focus

   ‘ if calling from in-process addin, try Inventor.FileDialog

   Dim selectXLSFile As New System.Windows.Forms.OpenFileDialog

   selectXLSFile.Filter = "Excel File (*.xls)|*.xls"

   If (selectXLSFile.ShowDialog() = DialogResult.OK) Then

 

     ‘ add the parameters defined on the XLS file

     Dim fileName As String = selectXLSFile.FileName

     allParams.ParameterTables.AddExcelTable(fileName, "A1", True)

   End If

 

   ‘ *********************

   ‘ show model parameters

   Dim paramTables As ParameterTables = allParams.ParameterTables

   If paramTables.Count > 0 Then

     For Each paramTable As ParameterTable In paramTables

       Dim paramValues As New System.Text.StringBuilder

       For Each tableParam As TableParameter _

         In paramTable.TableParameters

 

         ‘ append the list of parameters

         paramValues.AppendFormat("{0}: {1}{2}", _

                                tableParam.Name, _

                                tableParam.ModelValue, _

                                System.Environment.NewLine)

       Next

       MessageBox.Show(paramValues.ToString(), _

                       String.Format("Table param from file {0}", _

                                     paramTable.FileName))

     Next

   End If

 End Sub

 

 Private Shared Function GetInventorApp() As  _

   Inventor.Application

   Dim m_inventorApp As Inventor.Application = Nothing

 

   ‘ get running Inventor

   Try

  &
#160;  m_inventorApp = System.Runtime. _

       InteropServices.Marshal. _

       GetActiveObject("Inventor.Application")

   Catch ex As Exception

   End Try

 

   ‘ if not active, create a new Inventor session

   Try

 

     If m_inventorApp Is Nothing Then

       Dim inventorAppType As Type = System. _

         Type.GetTypeFromProgID("Inventor.Application")

       m_inventorApp = System.Activator. _

         CreateInstance(inventorAppType)

     End If

   Catch

   End Try

 

   Return m_inventorApp

 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