1

我正在尝试使用以下代码用 Python 提取 Inventor 零件 (.ipt) 的参数:

#Open Inventor
invApp = win32com.client.Dispatch("Inventor.Application")

#Make inventor visible
invApp.Visible = True

#Set file names of template
Part_FileName_BaseIPT = 'C:\\Base.ipt'

#Open the base model
oPartDoc=invApp.Documents.Open(Part_FileName_BaseIPT)

#Collect parameters
oParams = oPartDoc.ComponentDefinition.Parameters

(这是我在这里找到的代码片段的一部分:使用 python 自动化 Autodesk Inventor

我收到以下错误消息:...' object has no attribute 'ComponentDefinition'

有什么想法有什么问题吗?

是不是我必须以某种方式告诉 Python oPartDoc 与零件文档(而不是装配文档)相关。在 VBA 中检索零件的参数如下所示:

Dim partDoc As PartDocument
Set partDoc = ThisApplication.ActiveDocument

Dim param As Parameter
Set param = partDoc.ComponentDefinition.Parameters

我想到目前为止,Python 代码中缺少 VBA 第一行中给出的信息。

这是 Inventor API 对象模型表的一部分,可能对解决方案有帮助: API 对象模型

不幸的是,使用 Python 使用 Inventor API 的文档非常少,Autodesk 论坛中的一篇帖子也没有带来任何解决方案。但由于 Python 是我所知道的唯一编程语言,我不得不依赖它。

我已经尝试解决这个问题已经有一段时间了,任何帮助将不胜感激!

(我使用 Inventor 2018、Python 3.6.2 (Anaconda) 和 Windows 10。)

4

1 回答 1

1

我终于找到了解决方案,Inventor 服务台发给了我:

import win32com.client
from win32com.client import gencache, Dispatch, constants, DispatchEx

oApp = win32com.client.Dispatch('Inventor.Application')
oApp.Visible = True
mod = gencache.EnsureModule('{D98A091D-3A0F-4C3E-B36E-61F62068D488}', 0, 1, 0)
oApp = mod.Application(oApp)
# oApp.SilentOperation = True
oDoc = oApp.ActiveDocument
oDoc = mod.PartDocument(oDoc)
#in case of an assembly use the following line instead
#oDoc = mod.AssemblyDocument(oDoc)
prop = oApp.ActiveDocument.PropertySets.Item("Design Tracking Properties")

# getting description and designer from iproperties
Descrip = prop('Description').Value
Designer = prop('Designer').Value
print("Description: ",Descrip)
print("Designer: ",Designer)

# getting mass and area
MassProps = oDoc.ComponentDefinition.MassProperties
#area of part
dArea = MassProps.Area
print("area: ",dArea)
#mass
mass = MassProps.Mass
print("mass: ",mass)

#getting  parameters
oParams = oDoc.ComponentDefinition.Parameters
lNum = oParams.Count
print("number of Parameters: ",lNum)
# make sure the parameter names exist in the Inventor model
param_d0 = oParams.Item("d0").Value
print("Parameter d0: ",param_d0)
param_d1 = oParams.Item("d1").Value
print("Parameter d1: ",param_d1)
param_d2 = oParams.Item("d2").Value
print("Parameter d2: ",param_d2)
param_d0_exp = oParams.Item("d0").Expression
print("Parameter d0_exp: ",param_d0_exp)
param_d1_exp = oParams.Item("d1").Expression
print("Parameter d1_exp: ",param_d1_exp)
param_d2_exp = oParams.Item("d2").Expression
print("Parameter d2_exp: ",param_d2_exp)

Autodesk 社区网页上的原始帖子:

https://forums.autodesk.com/t5/inventor-customization/how-to-get-parameters-and-mass-of-a-part-with-python/td-p/7553056

于 2018-03-02T11:22:50.077 回答