Help using API's SaveAs to export PDFs of drawings (Error 0x800757)

Help using API's SaveAs to export PDFs of drawings (Error 0x800757)

jrveale
Participant Participant
2,182 Views
8 Replies
Message 1 of 9

Help using API's SaveAs to export PDFs of drawings (Error 0x800757)

jrveale
Participant
Participant

Hi,

 

I'm trying to learn a little of the Inventor COM API. As a proof of concept, I'm trying to open Inventor, open a drawing file, and then save a copy of as a pdf.

 

Here's my code (Python):

 

 

import os
from win32com.client import *

print("Test")
app = Dispatch("Inventor.Application", True)
app.Visible = True
print("Inventor opened")
desktop = os.path.normpath(os.path.expanduser("~/Desktop"))
app.Documents.Open(os.path.join(desktop, "Drawing1.idw"))
print("File opened")
file = CastTo(app.ActiveDocument, 'DrawingDocument')
file.SaveAs(os.path.join(desktop, "Drawing1.pdf"), SaveCopyAs=False)
print("PDF saved")
file.Close()

 

 

The script fails at SaveAs with the following output.

 

 

Test
Inventor opened
File opened
Traceback (most recent call last):
  File "C:/Users/jamle/PycharmProjects/Tests/Test.py", line 13, in <module>
    file.SaveAs(os.path.join(desktop, "Drawing1.pdf"), SaveCopyAs=False)
  File "C:\Users\jamle\AppData\Local\Temp\gen_py\3.7\D98A091D-3A0F-4C3E-B36E-61F62068D488x0x1x0\DrawingDocument.py", line 175, in SaveAs
    , SaveCopyAs)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024809), None)

Process finished with exit code 1

 

 

Error -2147352567 (0x80020009) is the generic Exception occured COM error code. Which isn't very helpful. Error -2147024809 (0x80070057) is listed as "One or more arguments are invalid". The only change I can make to get this error to not occur is to change the new file to also be an IDW rather than a PDF.

 

Edit: If I save as a DWG, it works. It's just PDFs that don't. I note that that when I use Inventor manually and click SaveAs, my only options are IDW and DWG...

 

I was under the impression from this article that SaveAs could be used to change the format of the drawing. However, it appears not. What command should I be using instead?

 

As a bonus question, can I perform this action (saving an IDW or DWG as a PDF) without having to open inventor and the drawing? i.e. Can I find a drawing object from a filepath and call it's SaveAs (or whichever other) method without opening it?

 

Thanks for the help

0 Likes
2,183 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

I'm not familiar with 'Python' code language, but I have created both iLogic rules and VBA macros that translate and save IDW drawings to PDF's.  I know that you have to pass the IDW through a translator to do this.  Whether it's a special Printer that you can print to PDF's, or the PDF Translator Add-in, inside Inventor, you always have to create a variable, assign its value as the translator, set its options, then use it to basically SaveCopyAs.  There are many variations of code available here on the forums that translate drawings to PDF's.  Does the code need to be in Python, though?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

jrveale
Participant
Participant

Thanks for your pointers

 

I found this, which seems to explain printing and the use of translators much better than the stuff I'd previously found. If I've understood correctly that this is the right path to be taking and it looks like the right way to you, I'll have a look after the bank holiday weekend.

 

I'd like to use Python ideally, as it makes it very simple for me to create a general tool which can then use the PDFs to do other things and be integrated into other workflows. It also means that other users at my workplace will be much more likely to understand the code than if it were VBA.

 

I also honestly really don't like writing VBA, but that's just a personal preference which probably shows my age, I'd make do if I had to!

0 Likes
Message 4 of 9

WCrihfield
Mentor
Mentor

I honestly don't care for writing code in VBA either.  I prefer working with Inventor's iLogic much more than its VBA.  iLogic is Inventor's simplified / customized version of VB.NET.

If there is a 'Printer' installed on your system (and on the systems of the other users) that processes  what you print to it into a PDF format file, then you can probably utilize that much easier than going through the Add-in route.  However, working with the settings of that PDF printer, to ensure they are correct before submitting the print, is another issue of its own.  The PrintManager in iLogic is fairly simple, and therefore has fewer options.  The DrawingPrintManager has a lot more options.  But either way, the settings they work with are fairly basic, and supposed to be 'universal', but at least one of them that I know of doesn't like all printers.  The ColorMode setting is somewhat printer specific, and doesn't have any effect on some printers, because there's nothing to pair it with on the Printer's settings side.  That's the sort of thing to watch out for when going the Printer route.

   From the Inventor side, I believe the document has to be opened, before you can use the SaveCopyAs method.  Although, you can open the document in the background (doesn't show it), which takes less time & resources, then call your Sub routine to translate it, then close the document again.  I personally don't know of a way to do this without starting up Inventor though.  But, that doesn't necessarily mean it can't be done, somehow.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 9

jrveale
Participant
Participant

Hi,

 

Thanks to your advice, I am now getting there with using the Print to PDF approach.

 

from win32com.client import *

# user chooses file paths for open and save here...drawing_filepath = ""

# Open Inventor application, and set visible (so I can tell it's opened for now)app = Dispatch('Inventor.Application')app.Visible = True

# Open the file to be saved as a pdf (returns a Document object) app.Documents.Open(drawing_filepath)
# Cast the opened Document object to a DrawingDocument object (it is guaranteed to be a drawing)drawing = CastTo(app.ActiveDocument, "DrawingDocument")

# Create and setup a print manager (so can use "Adobe PDF" printer to convert the drawings to PDF)
print_manager = drawing.PrintManger # It took me a while to find this method of getting the PrintManager
print_manager.Printer = "Adobe PDF"print_manager.NumberOfCopies = 1
print_manager.ScaleMode = print_manager.PrintScaleModeEnum.kPrintFullScale print_manager.PaperSize = print_manager.PrintSizeEnum.kPaperSizeA3 # Print PDF
print_manager.SubmitPrint()

 

I've taken a guess as to where the PrintScaleModeEnum and PrintSizeEnum can be found in the COM, but I appear to be wrong as I get an error telling me that "print_manager has no attribute PrintScaleModeEnum". Do you know where I can access these Enums from? Or can you point me to documentation for them?

 

Thanks

0 Likes
Message 6 of 9

jrveale
Participant
Participant

Ah, I spoke too soon.

 

I've been testing on a IDW, which are fine. However if I use the Print to PDF approach with a DWG file, the pdf appears to be a printscreen of the viewer (showing the drawing, with it's yellow paper, and the blue-grey background behind the drawing, at the zoom level the drawing was at when the print was called). Do you know of a way around this?

0 Likes
Message 7 of 9

WCrihfield
Mentor
Mentor

Unfortunately, I won't be much help with that situation.  I don't have a full paid version of Adobe Acrobat DC installed, which seems to be a prerequisite to using their Print To PDF service (unless I wanted to install a trial).  Also our shop saves all Inventor drawings as IDW, instead of DWG, so I have never tried to export a DWG to PDF from Inventor.  But I wouldn't assume it would be that much difference.

I know that the same Inventor Translator Add-in is used to publish both DWF, DWFx, & PDF formats.

I also know that another Inventor Translator Add-in is used to publish both DWG & DXF formats.

Those Add-ins use DLL files, within the ProgramFiles Installation directory.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 9

jrveale
Participant
Participant

Thanks for all your help nonetheless.

 

I've worked around it by making a temporary copy of the dwg as an idw, and creating the pdf from that. I then delete the temprary idw.

 

I've also worked around the Enum issues by finally finding the documentation in help.autodesk.com, under Programming Interface > Inventor API Reference Manual > Enums, and using the actual values behind those enums in their place.

 

I'll be sure post my working solution here once it's tidied.

0 Likes
Message 9 of 9

Anonymous
Not applicable

After using win32com.client.Dispatch, use gencache to generate constants cache. It stores all the constants

import win32com.client

invApp = win32com.client.Dispatch('Inventor.Application')
invApp.Visible = True
invApp = win32com.client.gencache.EnsureDispatch("Inventor.Application")

 

and you can access them from the win32com.client.constants variable, for example:

win32com.client.constants.kPaperSizeA3

 

0 Likes