使用Python从Autodesk Inventor中提取参数

时间:2017-11-22 20:44:48

标签: python-3.x autodesk-inventor

我正在尝试使用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

(这是我在此处找到的代码段的一部分:Using python to automate Autodesk Inventor

我收到以下错误消息: ...'对象没有属性'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 Object model

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

我一直试图解决这个问题已经有一段时间了,任何帮助都会受到高度赞赏!

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

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案,发明人帮助台发给我了:

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