本文是「Inventor API 进阶」系列第 1 篇。iProperties 是 Inventor 文件的核心元数据,掌握它的 API 操作是做任何二次开发的基础。
为什么 iProperties 这么重要?
四个内置属性集
属性集 |
显示名称 |
内部名称 |
说明 |
设计跟踪 |
Design Tracking Properties |
|
零件号、材料、质量等设计属性 |
摘要信息 |
Summary Information |
|
标题、主题、作者、公司等 |
文档摘要 |
Document Summary Information |
|
类别、公司、管理者等 |
用户自定义 |
User Defined Properties |
|
自定义属性,可增删 |
基本操作
获取属性集
Sub GetPropertySets()Dim doc As DocumentSet doc = ThisApplication.ActiveDocumentDim ps As PropertySetFor Each ps In doc.PropertySetsDebug.Print "名称: " & ps.Name & " (内部名: " & ps.InternalName & ")"NextEnd Sub
读取属性值
Sub ReadProperties()Dim doc As DocumentSet doc = ThisApplication.ActiveDocument' 获取"摘要信息"属性集Dim summaryPropSet As PropertySetSet summaryPropSet = doc.PropertySets.Item("Summary Information")' 读取"公司"属性Dim companyProp As PropertySet companyProp = summaryPropSet.Item("Company")MsgBox "公司: " & companyProp.ValueEnd Sub
修改属性值
Sub ModifyProperty()Dim doc As DocumentSet doc = ThisApplication.ActiveDocumentDim summaryPropSet As PropertySetSet summaryPropSet = doc.PropertySets.Item("Summary Information")' 修改"公司"属性summaryPropSet.Item("Company").Value = "我的公司"' 保存文档以持久化修改doc.SaveEnd Sub
自定义属性
添加自定义属性
Sub AddCustomProperty()Dim doc As DocumentSet doc = ThisApplication.ActiveDocument' 获取用户自定义属性集Dim userPropSet As PropertySetSet userPropSet = doc.PropertySets.Item("User Defined Properties")' 添加一个字符串类型的自定义属性' 参数:名称、值、类型(可选)On Error Resume NextuserPropSet.Add "审批人", "张三", ValueTypeEnum.kStringTypeOn Error GoTo 0End Sub
注意:如果属性已存在,Add 方法会报错。可以先检查或用 On Error 处理。
iProperty 的数据类型
ValueTypeEnum |
说明 |
kStringType |
字符串 |
kNumberType |
数值 |
kDateType |
日期 |
kYesNoType |
是/否 |
kTextType |
文本(RTF 格式) |
日期属性的特殊行为
Sub SetDateProperty()Dim doc As DocumentSet doc = ThisApplication.ActiveDocumentDim designPropSet As PropertySetSet designPropSet = doc.PropertySets.Item("Design Tracking Properties")Dim createDateProp As PropertySet createDateProp = designPropSet.Item("Date Created")' 设置日期值createDateProp.Value = #2026/09/01#End Sub
iProperty 表达式
Sub SetExpression()Dim doc As DocumentSet doc = ThisApplication.ActiveDocumentDim designPropSet As PropertySetSet designPropSet = doc.PropertySets.Item("Design Tracking Properties")' 设置表达式(以 = 开头表示表达式)designPropSet.Item("Description").Expression = "=<" & "项目编号" & ">-<" & "零件号" & ">"End Sub
参数也可以作为 iProperties
不用 Inventor 也能操作 iProperties:Apprentice
-
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();
三种存储自定义数据的机制对比
机制 |
用户可见 |
适用场景 |
性能 |
iProperties |
是(自定义选项卡) |
需要用户查看/编辑的数据 |
中等 |
Attributes |
否 |
程序内部标记,如关联组件分组 |
高 |
IStorage/IStream |
否 |
任意二进制数据 |
最高 |
性能建议
-
处理大量文件的 iProperties 时:用 Apprentice 而非 Inventor——不需要完整 Inventor 功能时 -
用 FlushToFile() 而非 Save()——只保存 iProperty 变更 -
批量操作时先收集再写入——减少文件 I/O 次数
本篇小结
要点 |
说明 |
属性集 |
四个内置集:设计跟踪、摘要、文档摘要、用户自定义 |
读写 |
|
自定义属性 |
在"User Defined Properties"集中 Add |
Apprentice |
无需 Inventor 即可操作 iProperties,用 FlushToFile 高效保存 |
三种存储机制 |
iProperties(可见)/ Attributes(隐藏)/ IStorage(任意) |


