如何通过 API 和 Python 使用 Inventor 将零件放入装配体
How to place parts into assemblys using Inventor via the API with Python
我正在尝试将 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.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/m-p/6379498#M64612) 中的等效项,它工作得很好,但我想使用 Python (部分只是为了它,部分是因为它是部分一个更大的项目)。
我对 python 不熟悉,但看起来你错过了这一行的括号
oMatrix = oTG.CreateMatrix
并且您将 oMatrix 设置为对方法的引用,而不是其结果。试试这个 oMatrix = oTG.CreateMatrix()
我正在尝试将 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.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/m-p/6379498#M64612) 中的等效项,它工作得很好,但我想使用 Python (部分只是为了它,部分是因为它是部分一个更大的项目)。
我对 python 不熟悉,但看起来你错过了这一行的括号
oMatrix = oTG.CreateMatrix
并且您将 oMatrix 设置为对方法的引用,而不是其结果。试试这个 oMatrix = oTG.CreateMatrix()