0

我正在尝试将 Inventor API 与 Python 一起使用。我已经设法打开新文档并修改现有零件,但现在我正试图将组件放入组件中,但它不起作用。

到目前为止,这是我的代码的简化版本:

import win32com.client as win32
project_folder = "C:\\Users\\User_1\\210608_project\\"

#initialization
inv = win32.gencache.EnsureDispatch('Inventor.Application')
inv.Visible = True

#Open a new assembly
inv.Documents.Add(win32.constants.kAssemblyDocumentObject, "", True)
invActDoc = inv.ActiveDocument
invAssDoc = win32.CastTo(invActDoc, 'AssemblyDocument')
print('invAssdoc', type(invAssDoc))


#Create the transient matrix
oTG = inv.TransientGeometry
print(type(oTG), oTG)
oMatrix = oTG.CreateMatrix
print(type(oMatrix))

#Add component to assembly
invAssDocDef = invAssDoc.ComponentDefinition
invAssOcc = invAssDocDef.Occurrences
invAssOcc.Add(project_folder + 'generic_part.ipt', oMatrix)

Inventor 和一个新的装配打开得很好,用于放置组件的瞬态矩阵已创建,但放置本身不起作用:

"C:\Program Files\Python37\python.exe"
C:/Users/User_1/210608_project/how_to_put_components_in_assemblies.py
invAssdoc <class 'win32com.gen_py.D98A091D-3A0F-4C3E-B36E-61F62068D488x0x1x0.AssemblyDocument.AssemblyDocument'>
<class 'win32com.gen_py.D98A091D-3A0F-4C3E-B36E-61F62068D488x0x1x0.TransientGeometry.TransientGeometry'> <win32com.gen_py.Autodesk Inventor Object Library.TransientGeometry instance at 0x1383360710136>
<class 'method'>
Traceback (most recent call last):
  File "C:/Users/User_1/210608_project/how_to_put_components_in_assemblies.py", line 24, in <module>
    invAssOcc.Add(project_folder + 'generic_part.ipt', oMatrix)
  File "C:\Users\User_1\AppData\Local\Temp\gen_py\3.7\D98A091D-3A0F-4C3E-B36E-61F62068D488x0x1x0\ComponentOccurrences.py", line 37, in Add
    , Position)
TypeError: The Python instance can not be converted to a COM object

Process finished with exit code 1

我认为我的错误在于我使用 win32com 的方式,但我真的不确定,因为我对编程还是很陌生。如果我尝试在 VBA 中似乎是等效的(https://forums.autodesk.com/t5/inventor-customization/vba-code-to-place-a-part-in-an-assembly/mp/6379498# M64612)它工作得很好,但我想使用 Python(部分只是为了它,部分是因为它是一个更大项目的一部分)。

4

1 回答 1

0

我不熟悉 python,但它看起来你错过了这一行的括号 oMatrix = oTG.CreateMatrix,你将 oMatrix 设置为方法的引用,而不是它的结果。试试这个oMatrix = oTG.CreateMatrix()

于 2021-06-24T07:26:26.763 回答