09-14-2022
08:15 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
09-14-2022
08:15 PM
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)