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.
Solved! Go to Solution.
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.
Inventor API Product Designer
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!
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?
from System.Windows.Forms import *
from System.Runtime.InteropServices import Marshal
InventorApp = Marshal.GetActiveObject('Inventor.Application')
from Inventor import *
doctype = InventorApp.ActiveDocumentType
print oDoc.DisplayName, doctype
if doctype == DocumentTypeEnum.kPartDocumentObject.value__ :
oPartDoc = clr.Convert(oDoc, PartDocument)
oPartDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure.value__
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!