如何在python(pywin32)中从Inventor COM API创建和使用对象

如何在python(pywin32)中从Inventor COM API创建和使用对象,python,com,pywin32,autodesk-inventor,Python,Com,Pywin32,Autodesk Inventor,我正在尝试使用Autodesk Inventor的COM API创建python脚本,该脚本将在Inventor图形上生成所选PDF,然后将以对我的问题不重要的特定方式处理这些PDF。我使用pywin32访问COM API,但我不太熟悉COM API的使用方式以及pywin32模块 ,并且我还无法找到列出的各个对象的文档。因此,我对这些对象的使用的理解是基于我可以从VB或iLogic(发明家自己的简单内置语言)在线示例中找到的 我遇到的一个大问题是创建我想要使用的对象。简化示例如下: from

我正在尝试使用Autodesk Inventor的COM API创建python脚本,该脚本将在Inventor图形上生成所选PDF,然后将以对我的问题不重要的特定方式处理这些PDF。我使用pywin32访问COM API,但我不太熟悉COM API的使用方式以及pywin32模块

,并且我还无法找到列出的各个对象的文档。因此,我对这些对象的使用的理解是基于我可以从VB或iLogic(发明家自己的简单内置语言)在线示例中找到的

我遇到的一个大问题是创建我想要使用的对象。简化示例如下:

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()
所以我不知道如何创建DrawingPrintManager来使用!您可以看到,我在创建DrawingDocument对象时避免了这个问题,因为我碰巧知道有一个ActiveDocument属性可以从应用程序本身获得

我还:

不知道DrawingPrintManager的完整属性和方法列表是什么,所以我不知道如何设置保存位置 我不确定我尝试使用的两个枚举是否在DrawingPrintManager中定义,但一旦我有了一个DrawingPrintManager可以使用,我就可以弄清楚这一点 如果有谁在使用COM API或pywin32方面有更多经验可以帮助我,我将非常感激。如果有人能告诉我Inventor的API对象的任何实际文档,那也是一样的,这将使事情变得更容易

谢谢

编辑:发布后,我几乎立即发现我可以得到一个PrintManager,但我无法通过访问drawing.PrintManager而不是drawing.DrawingPrintManager来判断是PrintManager还是DrawingPrintManager

但是,这是一个解决方法,因为它没有回答我关于如何在pywin32中创建对象的问题


我前进的问题是找到可以访问PrintScaleModeEnum和PrintSizeEnum对象的位置,以及找到如何设置打印PDF的保存位置,我认为这将是一个单独的问题,因为它可能与COM API无关。

我不熟悉python和pywin32,但我尝试回答您的问题

Inventor API的文档在本地安装C:\Users\Public\Documents\Autodesk\Inventor 2020\local Help或联机中提供

通常,您无法创建Inventor API对象的新实例。您必须通过适当的方法或属性值获得它们

例如:

你不能这么做

doc = new Inventor.Document()
你必须这样做

doc = app.Documents.Add(...)
这是打印管理器吗

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

有关更多详细信息

非常感谢您的回复和文档链接。我不确定为什么以前在联机帮助中找不到API内容。现在我明白了,我不能凭空创建对象,在文档示例中,打开不属于Inventor应用程序的文档是没有意义的。这将如何应用于枚举,我不确定从何处添加它们?简单地使用它们的实际值作为解决方法是否安全?通常我使用C,并且我引用了.NET程序集Autodesk.Inventor.Interop.dll,其中是此枚举的定义。当我在VBA中测试一些枚举时,我并没有发现枚举成员的使用和它的int/long值之间的区别。这两个看起来都一样:ThisApplication.ActiveView.DisplayMode=8713和ThisApplication.ActiveView.DisplayMode=kmonochromerenderingtanks,我现在正设法使用int/long值。如果我需要深入使用API,我相信我会找到pythonic方法从pywin32获取所有定义