- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I have inventor working properly, and now I want to access TranslatorAddIn Object which is derived from ApplicationAddIn Object. Please let me know what is the best way to do it. End function is to export as PDF from IDW file using python.
VBA is not feasible cause this is the small part on inventor cause I'm doing other PDF work after that using python.
import win32com.client
mod = win32com.client.gencache.EnsureModule('{D98A091D-3A0F-4C3E-B36E-61F62068D488}', 0, 1, 0)
app = win32com.client.gencache.EnsureDispatch('Inventor.Application')
app = mod.Application(app)
app.SilentOperation = True
app.Visible = True
if app.Ready:
print("inventor ready")
print(app.ActiveDocument)
oDocument = app.ActiveDocument
PDFAddIn = app.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = app.TransientObjects.CreateTranslationContext()
oContext.Type = 13059
oOptions = app.TransientObjects.CreateNameValueMap
oDataMedium = app.TransientObjects.CreateDataMedium
for key in dir(PDFAddIn):
print('{}'.format(key))
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm not pythonist. But you need to call the same API methods in the same order as described in VBA sample. Also your variable PDFAddIn can be cast to TranslatorAddIn Object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
At the end of your code should be something like this. Sorry if the syntax is not correct.
Parameters oOptions and oContext is passed by its reference. I don't know how it is declared in python.
if PDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions):
# Options for drawings...
oOptions.Value("All_Color_AS_Black") = 0
# oOptions.Value("Remove_Line_Weights") = 0
# oOptions.Value("Vector_Resolution") = 400
# oOptions.Value("Sheet_Range") = kPrintAllSheets
# oOptions.Value("Custom_Begin_Sheet") = 2
# oOptions.Value("Custom_End_Sheet") = 4
# Set the destination file name
oDataMedium.FileName = "c:\temp\test.pdf"
# Publish document.
PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
PDFAddIn = app.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
PDFAddIn = win32com.client.CastTo(PDFAddIn, "TranslatorAddIn")
oContext = app.TransientObjects.CreateTranslationContext()
oContext = win32com.client.CastTo(oContext, "TranslationContext")
oContext.Type = 13059
oOptions = app.TransientObjects.CreateNameValueMap()
oOptions = win32com.client.CastTo(oOptions, "NameValueMap")
oDataMedium = app.TransientObjects.CreateDataMedium()
oDataMedium = win32com.client.CastTo(oDataMedium, "DataMedium")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
oOptions.Value("Sheet_Range") = kPrintAllSheets
It consider as function and throws an error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
This is just PrintRangeEnum value. You can use one of the possible values as integer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
After searching for a while I get the idea from one of the blog about how to get oOptions NamevaluMap object to work.
Basically in python it won't let you do like VBA:
oOptions.Value("Sheet_Range") = kPrintAllSheets
Instead you have to write like this:
oOptions.Remove(oCount)
oOptions.Insert("Sheet_Range", 14082)
where oCount has to be the index number of Sheet_range that hold inside the NamevalueMap.
In python first you have to remove it, cause it hold the default value then Insert it with new value.
Here is the full working function code in python:
import win32com.client
mod = win32com.client.gencache.EnsureModule('{D98A091D-3A0F-4C3E-B36E-61F62068D488}', 0, 1, 0)
app = win32com.client.gencache.EnsureDispatch('Inventor.Application')
app = mod.Application(app)
inputfile="C:/Users/adm/Desktop/Cad/Assembly1.idw"
exportfile= inputfile[:-3] +"pdf"
app.Documents.Open(inputfile)
app.SilentOperation = True
app.Visible = True
oDocument = app.ActiveDocument
PDFAddIn = app.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
PDFAddIn = win32com.client.CastTo(PDFAddIn, "TranslatorAddIn")
oContext = app.TransientObjects.CreateTranslationContext()
oContext = win32com.client.CastTo(oContext, "TranslationContext")
oContext.Type = 13059
oOptions = app.TransientObjects.CreateNameValueMap()
oOptions = win32com.client.CastTo(oOptions, "NameValueMap")
oDataMedium = app.TransientObjects.CreateDataMedium()
oDataMedium = win32com.client.CastTo(oDataMedium, "DataMedium")
if PDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions):
for i in range(1, oOptions.Count):
if oOptions.Name(i) == "Sheet_Range":
oCount = i
oOptions.Remove(oCount)
oOptions.Insert("Sheet_Range", 14082)
oDataMedium.FileName = exportfile
PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
oDocument.Close(SkipSave=True)