1

I'm attempting to use Autodesk Inventor's COM API to create a python script that will generate PDFs of a selection on Inventor Drawings, these PDFs will then be processed in particular ways that aren't important to my question. I'm using pywin32 to access the COM API, but I'm not particularly familiar with how COM APIs are used, and the pywin32 module.

This is the extent of the documentation for Inventor's API that I have been able to find (diagram of API Object Model Reference Document), and I have not been able to find documentation for the individual objects listed. As such, I'm basing my understanding of the use of these objects on what I can find from examples online (all in VB or iLogic - Inventor's own simple built-in language).

A big issue I'm coming up against is in creating the objects I'd like to use. Simplified example below:

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 = ??? # How can I create this object
# I've tried:
# print_manager = Dispatch("Inventor.Application.Documents.DrawingDocument.DrawingPrintManager") #"Invalid class string"
# print_manager = drawing.DrawingPrintManager() #"object has no attribute 'DrawingPrintManger'
# print_manager = drawing.DrawingPrintManager   # same as above
# print_manager = drawing.PrintManger # worked in the end
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()

So I can't figure out how to create a DrawingPrintManager to use! You can see I've avoided this issue when creating my DrawingDocument object, as I just happened to know that there is an ActiveDocument attribute that I can get from the application itself.

I also:

  • don't know what the full list of attributes and methods for DrawingPrintManager are, so I don't know how to set a save location
  • don't know for sure that the two Enums I'm trying to use are actually defined within DrawingPrintManager, but I can figure that out once I actually have a DrawingPrintManager to work with

If anyone with more experience in using COM APIs or pywin32 can help me out, I'd be really appreciative. And the same if anyone can point me towards any actual documentation of Inventor's API Objects, which would make things a lot easier.

Thanks

Edit: After posting I've almost immediately found that I can get a PrintManager (can't tell if a PrintManager or DrawingPrintManager) by accessing drawing.PrintManager rather than drawing.DrawingPrintManager.

This is a workaround however as it doesn't answer my question of how to create objects within pywin32.

My problem moving forward is finding where I can access the PrintScaleModeEnum and PrintSizeEnum objects, and finding how to set the save location of the printed PDF (which I think will be a a separate question, as it's probably unrelated to the COM API).

1 Answer 1

1

I'm not familiar with python and pywin32, but I try to answer your questions.

Documentation of Inventor API is available in local installation "C:\Users\Public\Documents\Autodesk\Inventor 2020\Local Help" or online https://help.autodesk.com/view/INVNTOR/2020/ENU/

Generaly you are not able to create new instances of Inventor API objects. You must obtain them as a result of appropriate method or property value.

For example:

You CAN'T do this

doc = new Inventor.Document()

You MUST do this

doc = app.Documents.Add(...)

With print manager is this

print_manager = drawing.PrintManger 
# this returns object of type Inventor.DrawingPrintManager 
# when drawing is of type Inventor.DrawingDocument

See this for more details

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much for your response, and the links to documentation. I'm not sure why I couldn't find the API stuff within the online help before. I understand now that I can't create objects out of thin air, in the example of a document it doesn't make sense to have an open document that doesn't belong to the Inventor application. How would this apply to Enums, I'm not sure where I could add them from? Is it safe to simply use their actual value as a workaround?
Usually I'm using C# and I have referenced .NET assembly Autodesk.Inventor.Interop.dll where are definitions of this enums. When I test some enums in VBA, I don't find difference between use of enum member and its int/long value. This both looks the same: ThisApplication.ActiveView.DisplayMode = 8713 and ThisApplication.ActiveView.DisplayMode = kMonochromeRendering
Thanks, I'm managing to use int/long values for now. If I ever need to use the API in depth, I'm sure I'll find the pythonic way to get all the definitions from pywin32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.