我尝试使用以下代码使用 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
不幸的是,Inventor API 与 Python 的使用记录非常少,Autodesk 论坛上的帖子也没有带来任何解决方案。但由于 Python 是我唯一知道的编程语言,所以我必须依赖它。
我已经尝试解决这个问题很长一段时间了,任何帮助将不胜感激!
(我使用 Inventor 2018、Python 3.6.2 (Anaconda) 和 Windows 10。)
最佳答案
我终于找到了解决方案,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 社区网页上的原始帖子:
关于python-3.x - 使用 Python 从 Autodesk Inventor 中提取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47443621/