大数跨境

Inventor API 进阶1--iProperties 深度解析

Inventor API 进阶1--iProperties 深度解析 锐和软件资讯服务
2026-09-14
2
导读:Inventor API 进阶--iProperties 深度解析


文是「Inventor API 进阶」系列第 1 篇。iProperties 是 Inventor 文件的核心元数据,掌握它的 API 操作是做任何二次开发的基础。

为什么 iProperties 这么重要?

每个 Inventor 文件都有一组属性(iProperties),用于跟踪和管理文件信息——零件号、描述、材料、质量、创建日期、公司名称等。这些属性不仅是文件管理的基石,也是 BOM 表、标题栏、PDM 系统的数据来源。
如果你做过以下任何一件事,你就在用 iProperties:
在标题栏里显示零件号和材料
在 BOM 表中列出每个零件的重量
用 Vault 管理文件的状态和版本
批量修改零件的公司名称
这些场景的自动化,都离不开 iProperties API。

四个内置属性集

Inventor 的 iProperties 按属性集(PropertySet)组织。每个 PropertySet 有显示名称和内部名称。有四个公开的内置属性集:

属性集

显示名称

内部名称

说明

设计跟踪

Design Tracking Properties

{32853F0F-3444-11D1-9E93-0060B03A1A61}

零件号、材料、质量等设计属性

摘要信息

Summary Information

{F29F85E0-4FF9-1068-BB07-00A0C913CA3}

标题、主题、作者、公司等

文档摘要

Document Summary Information

{0A0A0000-0029-10D1-9E7E-00A0B0A5A3E}

类别、公司、管理者等

用户自定义

User Defined Properties

{90388D0D-E0AB-11D1-9E7E-00A0B0A5A3E}

自定义属性,可增删

前三个属性集包含固定数量的属性,用户自定义集允许你添加任意属性。

基本操作

获取属性集

Sub GetPropertySets()    Dim doc As Document    Set doc = ThisApplication.ActiveDocument
    Dim ps As PropertySet    For Each ps In doc.PropertySets        Debug.Print "名称: " & ps.Name & " (内部名: " & ps.InternalName & ")"    NextEnd Sub

读取属性值

Sub ReadProperties()    Dim doc As Document    Set doc = ThisApplication.ActiveDocument    ' 获取"摘要信息"属性集    Dim summaryPropSet As PropertySet    Set summaryPropSet = doc.PropertySets.Item("Summary Information")    ' 读取"公司"属性    Dim companyProp As Property    Set companyProp = summaryPropSet.Item("Company")    MsgBox "公司: " & companyProp.ValueEnd Sub

修改属性值

Sub ModifyProperty()    Dim doc As Document    Set doc = ThisApplication.ActiveDocument    Dim summaryPropSet As PropertySet    Set summaryPropSet = doc.PropertySets.Item("Summary Information")    ' 修改"公司"属性    summaryPropSet.Item("Company").Value = "我的公司"    ' 保存文档以持久化修改    doc.SaveEnd Sub

自定义属性

用户自定义属性是最灵活的部分。在 UI 中通过 iProperties 对话框的"自定义"选项卡管理。

添加自定义属性

Sub AddCustomProperty()    Dim doc As Document    Set doc = ThisApplication.ActiveDocument    ' 获取用户自定义属性集    Dim userPropSet As PropertySet    Set userPropSet = doc.PropertySets.Item("User Defined Properties")    ' 添加一个字符串类型的自定义属性    ' 参数:名称、值、类型(可选)    On Error Resume Next    userPropSet.Add "审批人""张三", ValueTypeEnum.kStringType    On Error GoTo 0End Sub

注意:如果属性已存在,Add 方法会报错。可以先检查或用 On Error 处理。

iProperty 的数据类型

iProperties 支持多种数据类型:

ValueTypeEnum

说明

kStringType

字符串

kNumberType

数值

kDateType

日期

kYesNoType

是/否

kTextType

文本(RTF 格式)

日期属性的特殊行为

日期类型属性有一个特殊行为:每个日期属性旁边有个复选框。当未勾选时,Expression属性不是有效日期。修改时需要注意:
Sub SetDateProperty()    Dim doc As Document    Set doc = ThisApplication.ActiveDocument    Dim designPropSet As PropertySet    Set designPropSet = doc.PropertySets.Item("Design Tracking Properties")    Dim createDateProp As Property    Set createDateProp = designPropSet.Item("Date Created")    ' 设置日期值    createDateProp.Value = #2026/09/01#End Sub

iProperty 表达式

iProperties 支持表达式,可以把多个属性的值组合到一个属性中。比如在"描述"属性中写 =项目编号 & "-" & 零件号,描述就会自动拼接。
Sub SetExpression()    Dim doc As Document    Set doc = ThisApplication.ActiveDocument    Dim designPropSet As PropertySet    Set designPropSet = doc.PropertySets.Item("Design Tracking Properties")    ' 设置表达式(以 = 开头表示表达式)    designPropSet.Item("Description").Expression = "=<" & "项目编号" & ">-<" & "零件号" & ">"End Sub

参数也可以作为 iProperties

Inventor 参数可以在 iProperties 中引用。通过设置参数的"导出"属性,参数值可以出现在 BOM 和 iProperties 中。

不用 Inventor 也能操作 iProperties:Apprentice

这是 iProperties 最强大的能力之一——你不需要启动 Inventor 就能读写 iProperties
Apprentice 是 Inventor 的一个无界面编程组件,随 Inventor 和免费的 Inventor View 安装。它支持的功能子集包括:
  • iProperties(读写)
  • 装配体结构
  • 文件引用
  • BOM
  • 属性(Attributes)
  • 几何体(只读)

Apprentice 的限制

  • 不能和 Inventor 在同一进程使用:所以不能用 Inventor 内置 VBA 或 Add-In 中的 Apprentice 
  • 不能迁移文件:如果文件版本较旧,不能编辑和保存
  • 功能子集:不支持特征、草图等编辑

Apprentice 代码示例(C#)

// 创建 Apprentice 组件ApprenticeServerComponent apprentice = new ApprenticeServerComponent();// 打开文件ApprenticeServerDocument appDoc = apprentice.Open(@"C:\Temp\Part1.ipt");// 读取并修改 iPropertyPropertySet summarySet = appDoc.PropertySets.Item["Summary Information"];summarySet.Item["Company"].Value = "新公司名";// 只保存 iProperty 变更(不重写整个文件,非常高效)appDoc.PropertySets.FlushToFile();
FlushToFile() 方法是 Apprentice 的核心优势——它只写入 iProperty 变更部分,不需要保存整个文档,速度极快。批量处理数千个文件的 iProperties 时,Apprentice 比 Inventor 快几个数量级。

三种存储自定义数据的机制对比

机制

用户可见

适用场景

性能

iProperties

是(自定义选项卡)

需要用户查看/编辑的数据

中等

Attributes

程序内部标记,如关联组件分组

IStorage/IStream

任意二进制数据

最高

iProperties 适合存储用户需要查看的元数据;Attributes 适合程序内部使用(如标记组件分组);IStorage/IStream 适合存储任意复杂的私有数据。

性能建议

  • 处理大量文件的 iProperties 时:用 Apprentice 而非 Inventor——不需要完整 Inventor 功能时
  • 用 FlushToFile() 而非 Save()——只保存 iProperty 变更
  • 批量操作时先收集再写入——减少文件 I/O 次数

本篇小结

要点

说明

属性集

四个内置集:设计跟踪、摘要、文档摘要、用户自定义

读写

PropertySets.Item("名称").Item("属性名").Value

自定义属性

在"User Defined Properties"集中 Add

Apprentice

无需 Inventor 即可操作 iProperties,用 FlushToFile 高效保存

三种存储机制

iProperties(可见)/ Attributes(隐藏)/ IStorage(任意)


下篇预告:《装配体中 Occurrence 与 Proxy 的关系》——Inventor 装配体 API 中最容易让人困惑的概念。
本文素材参考自 Autodesk Developer Blog「Mod The Machine」系列文章,作者 Brian Ekins / Adam Nagy / Augusto Goncalves。

【声明】内容源于网络
0
0
锐和软件资讯服务
专注制造业设计服务20年,Autodesk产品金牌经销商,为您提供专业的技术解答,及时的信息资讯,全新的产品动态。各种好看、好玩、实用的设计咨询,惊爆您的眼球
内容 99
粉丝 0
锐和软件资讯服务 专注制造业设计服务20年,Autodesk产品金牌经销商,为您提供专业的技术解答,及时的信息资讯,全新的产品动态。各种好看、好玩、实用的设计咨询,惊爆您的眼球
总阅读768
粉丝0
内容99