我正在尝试将示例 VBA 代码从 Inventor 转换为 python。
大多数事情,比如修改参数,效果都很好。
但不幸的是,导出到 STL 失败。
看来,应该有的“STL Exporter Plugin”的方法只是缺失了。
就我而言:
HasSaveCopyAsOptions
和SaveCopyAs
。
(这甚至似乎发生在其他插件上..)
错误信息:
AttributeError: '<win32com.gen_py.Autodesk Inventor Object Library.ApplicationAddIn instance at 0x1940552744240>' object has no attribute 'HasSaveCopyAsOptions'
Library.ApplicationAddIn
,所以我只能看到默认方法?我已经在调用
SaveCopyAs
之前设置了一个断点并尝试 dir(oSTLTranslator)
。
对于我的期望来说,结果列表看起来很空。
# -*- coding: utf-8 -*-
import win32com.client
from win32com.client import gencache, Dispatch, constants, DispatchEx
import os
# !!MINIFIED VERSION OF CLASS!!
class InventorAPI:
def loadInventorApp(self):
self.oApp = win32com.client.Dispatch('Inventor.Application')
self.oApp.Visible = True
self.mod = gencache.EnsureModule('{D98A091D-3A0F-4C3E-B36E-61F62068D488}', 0, 1, 0)
self.oApp = self.mod.Application(self.oApp)
# self.oApp.SilentOperation = True
def loadInventorDoc(self):
oDoc = self.oApp.ActiveDocument
self.oDoc = self.mod.PartDocument(oDoc)
def listAllPlugins(self):
no = self.oApp.ApplicationAddIns.Count
print(f"Listing {no} plugins:")
i=0
while (i<no):
i+=1 #Increments first, as index is 1-based
name = self.oApp.ApplicationAddIns.Item(i).ShortDisplayName
id = self.oApp.ApplicationAddIns.Item(i).ClientId
print(f"{i:02}: {id} - {name}")
def saveStl(self, Name):
oSTLTranslator = self.oApp.ApplicationAddIns.ItemById("{533E9A98-FC3B-11D4-8E7E-0010B541CD80}")
if oSTLTranslator is None:
raise( "Could not access STL translator." )
oContext = self.oApp.TransientObjects.CreateTranslationContext
oOptions = self.oApp.TransientObjects.CreateNameValueMap
breakpoint()
# <=== NEXT CALL WILL FAIL
if oSTLTranslator.HasSaveCopyAsOptions(self.oApp.ActiveDocument, oContext, oOptions):
#Set accuracy.
# 2 = High
# 1 = Medium
# 0 = Low
oOptions["Resolution"] = 1
oContext.Type = kFileBrowseIOMechanism
oData = self.oApp.TransientObjects.CreateDataMedium
oData.FileName = Name
oSTLTranslator.SaveCopyAs(self.oApp.ActiveDocument, oContext, oOptions, oData)
inv = InventorAPI()
inv.loadInventorApp()
#inv.listAllPlugins()
#exit()
inv.loadInventorDoc()
inv.saveStl("Mount_75mm.stl")
exit()
更新:
我检查了oSTLTranslator的属性和方法:
dir(oSTLTranslator): ['Activate', 'Activated', 'AddInType', 'Application', 'Automation', 'CLSID', 'ClassIdString', '
ClientId', 'DataVersion', 'Deactivate', 'Description', 'DisplayName', 'Hidden', 'LicenseStatus'
, 'LoadAutomatically', 'LoadBehavior', 'Location', 'Parent', 'ProgId', 'ShortDisplayName', 'Sta
rtUpEnabled', 'Type', 'UserInterfaceVersion', 'UserUnloadable', 'Version', '_ApplyTypes_', '__c
lass__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__
getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter_
_', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__re
pr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_good_ob
ject_', '_get_good_single_object_', '_oleobj_', '_prop_map_get_', '_prop_map_put_', 'coclass_cl
sid']
.CLSID {A0481EEB-2031-11D3-B78D-0060B0F159EF}
.ClassIdString {533E9A98-FC3B-11D4-8E7E-0010B541CD80}
.ClientId {533E9A98-FC3B-11D4-8E7E-0010B541CD80}
.Description Autodesk Internal STL Export Translator
.DisplayName Translator: STL Export
.Location C:\Program Files\Autodesk\Inventor 2024\Bin\STLTrans.dll
.Type 50343936
所以,SaveAs 功能不存在,甚至不区分大小写。 但它似乎是一个额外的DLL文件。 但它在 genpy 文件夹中没有专门的条目。 只有主要应用程序的文件:
D98A091D-3A0F-4C3E-B36E-61F62068D488x0x1x0.py
但该文件也不包含 STL 翻译的任何字符串或 ID。
win32com.client.dynamic.Dispatch('Inventor.Application')
,但这似乎会导致代码早期出现大量其他问题......抱歉我对python不熟悉。但问题出在使用过的类型上。 方法 ApplicationAddIns.ItemById 返回类型为 ApplicationAddIn 的对象。但翻译器的类型是TranslatorAddIn,它派生自ApplicationAddIn。
尝试将变量
oSTLTranslator
重新输入到 TranslatorAddIn,我希望它会起作用。