Inventor TranslatorAddIn access using python

Inventor TranslatorAddIn access using python

cpadmani95
Explorer Explorer
894 Views
6 Replies
Message 1 of 7

Inventor TranslatorAddIn access using python

cpadmani95
Explorer
Explorer

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

 

0 Likes
Accepted solutions (2)
895 Views
6 Replies
Replies (6)
Message 2 of 7

Michael.Navara
Advisor
Advisor
Accepted solution

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

 

 

 

 

Message 3 of 7

Michael.Navara
Advisor
Advisor

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)

 

 

Message 4 of 7

cpadmani95
Explorer
Explorer
I was able to see all the object inside once I did Cast.
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")
0 Likes
Message 5 of 7

cpadmani95
Explorer
Explorer
This is the tricky one, I don't know how to set this one in python:
oOptions.Value("Sheet_Range") = kPrintAllSheets

It consider as function and throws an error.
0 Likes
Message 6 of 7

Michael.Navara
Advisor
Advisor

This is just PrintRangeEnum value. You can use one of the possible values as integer.

0 Likes
Message 7 of 7

cpadmani95
Explorer
Explorer
Accepted solution

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)

 

0 Likes