At the time, I was working on my "next" python script conversion, and was digging into the gen_py file created from the Inventor COM tlb file. Attached is my latest python script, a "translation" of the AddOccurrence sample from the Inventor API docs (with a few additions to it). This python sample shows how to "get" an AssemblyDocument object (as opposed to the generic Inventor Document type), and I added some sample messagebox calls to show the activedocument type. Below is the text of the script (possibly wrapped wrong?)
# Inventor test script for pythonwin COM access
# AddOccurrence API Docs Sample
# converted from VBA code by Bob Schader
import win32con
import win32ui
import win32com.client
oApp=win32com.client.Dispatch("Inventor.Application")
doctype = oApp.ActiveDocumentType
if doctype == win32com.client.constants.kPartDocumentObject :
win32ui.MessageBox("Active Document Type: Part Document","Add Occurence")
elif doctype == win32com.client.constants.kAssemblyDocumentObject :
win32ui.MessageBox("Active Document Type: Assembly Document","Add Occurence")
elif doctype == win32com.client.constants.kDrawingDocumentObject :
win32ui.MessageBox("Active Document Type: Drawing Document","Add Occurence")
elif doctype == win32com.client.constants.kPresentationDocumentObject :
win32ui.MessageBox("Active Document Type: Presentation Document","Add Occurence")
elif doctype == win32com.client.constants.kDesignElementDocumentObject :
win32ui.MessageBox("Active Document Type: Design Element Document","Add Occurence")
elif doctype == win32com.client.constants.kForeignModelDocumentObject :
win32ui.MessageBox("Active Document Type: Foriegn Model Document","Add Occurence")
elif doctype == win32com.client.constants.kSATFileDocumentObject :
win32ui.MessageBox("Active Document Type: SAT File Document","Add Occurence")
else:
win32ui.MessageBox("Active Document Type: Error","Add Occurence")
if doctype == win32com.client.constants.kAssemblyDocumentObject :
oDoc=oApp.ActiveDocument
oAsmDoc = win32com.client.CastTo(oDoc, "AssemblyDocument")
oAsmCompDef = oAsmDoc.ComponentDefinition
oTG = oApp.TransientGeometry
oMatrix = oTG.CreateMatrix()
oMatrix.SetToRotation(3.14159265358979/4, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1))
oOcc = oAsmCompDef.Occurrences.Add("D:\\Temp\\Part1.ipt", oMatrix)
win32ui.MessageBox("Occurence Added Complete","Add Occurence")
This sucks! Anyone know how to force the web version of the discussion group to insert preformatted or indented text? I tried the preformat tags but they just disappear.
BTW, don't forget to edit the location of your part to add (near the end of the code).