Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Example of embedding IronPython into a C# addin

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
frankfralick
1226 Views, 4 Replies

Example of embedding IronPython into a C# addin

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

4 REPLIES 4
Message 2 of 5
ekinsb
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 and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 5
rschader
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!

Message 4 of 5
rschader
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()

Message 5 of 5
rschader
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!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report