How to add your knowledge

IronPython scripting examples

    IronPython scripting for Inventor

    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).

    Samples

     

    The main goal for this page is to provide a sample repository of IronPython scripts which have been converted from the VBA macros Autodesk provides in the Inventor Programmers documentation. This will give people a head start on programming Inventor using IronPython as the scripting engine. Contributor's are welcome also.
     

    Script initialization:

    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"
    End Sub

    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"
    
    Add the init code in front of this and save it as "AddSketch.py", and it can be executed directly from whichever IDE you prefer (I use Visual Studio Integrated Shell, which works quite nicely).
     

    Example #3

    Sketches -> Drawing Sketches - editing line type and color

     
    Converting this sample code gave me a few problems, but I figured it out. In the File attached, "ModifyDrawingSketchEntityProperties.py", you can see that I included the original VBA code in the python script, but commented the VBA code out and interspersed it with the python statements. Through using the interactive interpreter, I was able to determine that the call to oDrawView = oDoc.ActiveSheet.DrawingViews.AddDraftView fails in python unless I provide the Scale and Name parameters. Not sure why it works without those parameters in VBA? The script I have added in the file area also contains a few debugging type statements, mostly using print.

    Related Wiki Topics

    External Links

    1. http://IronPython.net
    2. http://pytools.codeplex.com/ (Python Tools for Visual Studio)