• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    Autodesk Inventor Customization

    Reply
    New Member
    Posts: 1
    Registered: ‎12-13-2012
    Accepted Solution

    Example of embedding IronPython into a C# addin

    154 Views, 4 Replies
    12-13-2012 09:29 AM

    Below is a link to a github repo that has a code example showing how to embed an IronPython engine inside a C# addin.  It is the same as the "SimpleAddIn" that comes with the SDK, only with the logic part of the addin factored out into IronPython.  I could never find anything on using IronPython with Inventor so I'm posting this for any others that might be interested. 

     

    The main reason for doing this is to speed up development time.  Edits related to the python code do not require the cycle of shutting down Inventor, compiling, restarting etc.  More detail is in the readme.

     

    https://github.com/frankfralick/InventorAddIns

     

    @frankfralick

    Please use plain text.
    Employee
    Posts: 152
    Registered: ‎07-21-2006

    Re: Example of embedding IronPython into a C# addin

    12-13-2012 11:42 AM in reply to: frankfralick

    I don't mean to derail Frank's suggestion of using Iron Python, but there is another method of developing an add-in that doesn't require the shutting down and starting up each time you make a change or learning another language.  It's something that I often use.  Instead of beginning with an add-in I'll start with an simple Windows Forms Application and will develop and debug the logic portion of my add-in there.  Once I have the code working there I can copy it over into my add-in project.



    Brian Ekins
    Inventor API Product Designer
    Manufacturing Solutions
    Autodesk, Inc.
    Please use plain text.
    Distinguished Contributor
    Posts: 248
    Registered: ‎12-15-2003

    Re: Example of embedding IronPython into a C# addin

    01-17-2013 10:10 AM in reply to: frankfralick

    I have created my own simple addin for Inventor that just brings up a file requester allowing me to choose and and execute python scripts directly from Inventor, but I can just as well edit and execute them using Visual Studio Shell IDE. In either case, they execute just fine. At some point when I have enough python scripts developed, I wouldn't mind trying to expand the addin to allow adding ribbons and buttons that are tied to python scripts, but I find just getting to know how to interact with Inventor using IronPython quite enough for now!

    Please use plain text.
    Distinguished Contributor
    Posts: 248
    Registered: ‎12-15-2003

    Re: Example of embedding IronPython into a C# addin

    01-17-2013 10:25 AM in reply to: rschader

    This is an IronPython script I just wrote today, I use it to set the active document's BOMStructure to Reference:

     

    # Set BOM value of active document to Reference
    # may need differing sections for handling components vs. assemblies?
    import clr
    import System
    clr.AddReferenceByPartialName("System.Windows.Forms")

    from System.Windows.Forms import *
    from System.Runtime.InteropServices import Marshal
    InventorApp = Marshal.GetActiveObject('Inventor.Application')
    clr.AddReference("Autodesk.Inventor.Interop")
    from Inventor import *

    oDoc=InventorApp.ActiveDocument
    doctype = InventorApp.ActiveDocumentType
    print oDoc.DisplayName, doctype

    if doctype == DocumentTypeEnum.kPartDocumentObject.value__ :
        oPartDoc = clr.Convert(oDoc, PartDocument)
        print oPartDoc.ComponentDefinition.BOMStructure
        oPartDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure.value__
        oPartDoc.Save()

    Please use plain text.
    Distinguished Contributor
    Posts: 248
    Registered: ‎12-15-2003

    Re: Example of embedding IronPython into a C# addin

    01-17-2013 10:32 AM in reply to: rschader

    One may ask, why write a macro or script for such a minor task, but I do this task alot, and using this script, I only need to click once on my addin button, double click the script, and I am done, even saving the file. Otherwise, I have to select the Tools ribbon, click on Document Proerties, then select the Bill of Materials tab, select the dropdown for setting Reference, then click Close, and then save. So I feel I'm saving myself quite a bit of repetitive strain!

    Please use plain text.