Opinions

Autodesk Inventor API – Part 2

By Allen Gager

Previous parts of this series: Introduction and Part 1.

Welcome back. Hopefully you have had an opportunity to stop by the Mod the Machine blog by Brian Ekins and read up a bit on Inventor and Programming.

We are going to use Inventor’s integrated Visual Basic Editor for this example of loading Materials information from an Excel document into the Inventor Style and Standard Editor. You will want to visit the Inventor Programming Help. There is some great information for new users and I would encourage you to start at the beginning.  Figure 1 was captured from Autodesk Inventor 2010 while using the Ribbon Interface.

Figure 1

Start a new Part file and exit the Sketch. In this case, we are going to create the code within the Part document, thus creating a VBA Document Project. Now there are some very good reasons not to use a Document Project and only one reason you may want to. Fortunately, this example fits that one reason. We are going to create a short program to do one purpose. Follow this link to find out more about VBA Document Projects.

The next thing to do is locate the Index to cut-and-paste sample code located in the Programming Help. See Figure 2.

Figure 2

Select Material Create located under the Miscellaneous heading. The following code is taken directly from the Inventor Programming Help file.

“Material Create Example”

This sample demonstrates how to create a new material in a part document. Before running the sample you must have a part document open.

Public Sub CreateMaterial()

‘ Set a reference to the part document.
‘ This assumes a part document is active.
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument

‘ Create a new material.
Dim oNewMaterial As Material
Set oNewMaterial = oPartDoc.Materials.Add(“My Material”, 11.37)

‘ Define the other properties of the material
oNewMaterial.LinearExpansion = 5.73
oNewMaterial.PoissonsRatio = 0.38
‘ Arbitrarily assigns the first render style in the render styles collection.
oNewMaterial.RenderStyle = oPartDoc.RenderStyles.Item(1)
oNewMaterial.SpecificHeat = 124.3
oNewMaterial.ThermalConductivity = 310.2
oNewMaterial.UltimateTensileStrength = 210.45
oNewMaterial.YieldStrength = 185.5
oNewMaterial.YoungsModulus = 76.37
End Sub

From the Tools tab, select VBA Editor located on the Options panel. See Figure 3.

Figure 3

In the Project panel expand DocumentProject(Part.1) and then expand the folder Autodesk Inventor 2010 Objects, activate ThisDocument by double clicking it.  Paste the code from the Help file.   We have just created a Document Project. See Figure 4.

Figure 4

Select the Play button from the tool bar, or select Run from the Menu then select Run, or simply press the F5 key on your keyboard.

The code will execute and create a new Material based on the values you provided in the code.  Select the Manage tab and then select Styles Editor from the Styles and Standards panel.  Locate your new material named “My Material” in the Materials category.

You have just written your first VBA program for Inventor.

Take a moment to change the name and the other values and create a few more Materials. If you remember back in the first part of this series, I said that often most of the pieces are already done. It is just a matter of fitting them together. As you can see, we have the code to create a new Material and we didn’t have to really do anything.

In the next part of this series we access the contents of the Excel document containing our new Materials definitions.

Go to Part 3.