Inventor Customization

Reply
New Member
frankfralick
Posts: 1
Registered: ‎12-13-2012
Message 1 of 5 (398 Views)
Accepted Solution

Example of embedding IronPython into a C# addin

398 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

ADN Support Specialist
ekinsb
Posts: 300
Registered: ‎07-21-2006
Message 2 of 5 (393 Views)

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
Developer Technical Services
Autodesk Developer Network
Autodesk, Inc.
Distinguished Contributor
rschader
Posts: 249
Registered: ‎12-15-2003
Message 3 of 5 (369 Views)

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!

Distinguished Contributor
rschader
Posts: 249
Registered: ‎12-15-2003
Message 4 of 5 (365 Views)

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

Distinguished Contributor
rschader
Posts: 249
Registered: ‎12-15-2003
Message 5 of 5 (363 Views)

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!

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community


Inventor Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for Autodesk Inventor helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Inventor

Twitter

Facebook

Blogs

Pinterest

Youtube