【问题标题】:How to create and use objects from the Inventor COM API in python (pywin32)如何在 python (pywin32) 中从 Inventor COM API 创建和使用对象
【发布时间】:2020-07-27 00:31:02
【问题描述】:

我正在尝试使用 Autodesk Inventor 的 COM API 创建一个 Python 脚本,该脚本将在 Inventor 工程图中生成所选内容的 PDF,然后以对我的问题不重要的特定方式处理这些 PDF。我正在使用 pywin32 访问 COM API,但我对 COM API 的使用方式和 pywin32 模块不是特别熟悉。

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),我无法找到列出的各个对象的文档。因此,我对这些对象的使用的理解基于我可以从在线示例中找到的内容(全部使用 VB 或 iLogic - Inventor 自己的简单内置语言)。

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

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 对象的任何实际文档,那也是一样,这将使事情变得容易得多。

谢谢

编辑:发布后我几乎立即发现我可以通过访问drawing.PrintManager 而不是drawing.DrawingPrintManager 来获得一个PrintManager(不知道是PrintManager 还是DrawingPrintManager)。

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

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

【问题讨论】:

    标签: python com pywin32 autodesk-inventor


    【解决方案1】:

    我不熟悉python和pywin32,但我试着回答你的问题。

    Inventor API 文档可在本地安装“C:\Users\Public\Documents\Autodesk\Inventor 2020\Local Help”或在线https://help.autodesk.com/view/INVNTOR/2020/ENU/中找到

    一般来说,您无法创建 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
    

    See this了解更多详情

    【讨论】:

    • 非常感谢您的回复以及文档链接。我不知道为什么我以前在在线帮助中找不到 API 的东西。我现在明白了,我不能凭空创建对象,在文档示例中,打开不属于 Inventor 应用程序的文档是没有意义的。这将如何应用于枚举,我不确定我可以从哪里添加它们?简单地使用它们的实际值作为解决方法是否安全?
    • 通常我使用的是 C#,并且我已经引用了 .NET 程序集 Autodesk.Inventor.Interop.dll,这些枚举的定义在哪里。当我在 VBA 中测试一些枚举时,我发现枚举成员的使用与其 int/long 值之间没有区别。这两个看起来一样:ThisApplication.ActiveView.DisplayMode = 8713ThisApplication.ActiveView.DisplayMode = kMonochromeRendering
    • 谢谢,我现在正在设法使用 int/long 值。如果我需要深入使用 API,我相信我会找到从 pywin32 获取所有定义的 Python 方式
    猜你喜欢
    • 2021-01-04
    • 2020-06-17
    • 2012-03-03
    • 2013-10-19
    • 2018-03-13
    • 2018-10-23
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多