As an alternative to VBA and regular Python, IronPython is Python with .NET binding already built in. Visual Studio can be used as a development environment (Visual Studio Shell is free from Microsoft).
Each IronPython script needs a certain amount of initialization code to interact with Inventor. The code below is what I use as a template for each script I write so far:
import clr import System clr.AddReferenceByPartialName("System.Windows.Forms") from System.Windows.Forms import * from System.Runtime.InteropServices import Marshal ThisApplication = Marshal.GetActiveObject('Inventor.Application') clr.AddReference("Autodesk.Inventor.Interop") from Inventor import *
For this first example, I will first show the sample VBA code provided by Autodesk, and then the IronPython script which accomplishes the same thing (minus comments). You will see that it is simpler in the respect that you don't need to define your variables first.
Example 1: Add Sketch
Public Sub AddSketch()
' Set a reference to the part component definition.
' This assumes that a part document is active.
Dim oCompDef As PartComponentDefinition
Set oCompDef = ThisApplication.ActiveDocument.ComponentDefinition
' Get the first face of the model. This sample assumes a simple
' model where at least the first face is a plane. (A box is a good
' test case.)
Dim oFace As Face
Set oFace = oCompDef.SurfaceBodies.Item(1).Faces.Item(1)
' Create a new sketch. The second argument specifies to include
' the edges of the face in the sketch.
Dim oSketch As PlanarSketch
Set oSketch = oCompDef.Sketches.Add(oFace, True)
' Change the name.
oSketch.Name = "My New Sketch"
Now, here is the equivalent IronPython script (minus the required init statements above and minus comments:
oCompDef = ThisApplication.ActiveDocument.ComponentDefinition oFace = oCompDef.SurfaceBodies.Item(1).Faces.Item(1) oSketch = oCompDef.Sketches.Add(oFace, True) oSketch.Name = "My New Sketch"