在一些工作流中,需要对文件本身附加一些数据,对最终用户不可见,便于下游的使用。Inventor API提供了三种方式:
1. iProperties
2. Attribute
3. IStorage & IStream
首先最容易的就是iProperties 。这个基本内容有太多文章介绍。我就不赘述了。直接看几段代码,看看如何添加一些数据,并如何取出。为方便不同用户,例子用VBA, .NET。Inventor API 或学徒服务器。
VBA
Sub AddCustomProperty()
隐式打开文档
Dim oDoc As Document
Set oDoc = ThisApplication.Documents.Open("c:\testpart.ipt", False)
' 需要添加的特性集合名
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' 新建特性集合
Dim oNewPS As PropertySet
If oDoc.PropertySets.PropertySetExists("myNewSet") Then
' 如果已经存在
Set oNewPS = oDoc.PropertySets("myNewSet")
' 可考虑清理掉原有数据
Else
'添加新的特性集合,记得给定一个GUID
Set oNewPS = oDoc.PropertySets.Add("myNewSet",
"{3FDB7763-80DB-4269-83E4-7F43BC8E9EA7}")
End If
'各种特性值
Dim oIntProV As Integer
oIntProV = 100
Dim oBoolProV As Boolean
oBoolProV = False
Dim oDoubleProV As Double
oDoubleProV = 3.1415926
Dim oDateProV As Date
oDateProV = "2013-3-1 15:25"
' 添加特性,假定它们还不存在
' assume they do not exist in the property set
Call oNewPS.Add(oIntProV, "ModelCount")
Call oNewPS.Add(oBoolProV, "ModelUpdateToDate")
Call oNewPS.Add(oDoubleProV, "ModelBasicLength")
Call oNewPS.Add(oDateProV, "ModelUpdateDate")
oDoc.Save
oDoc.Close
End Sub
Sub ReadCustomProperty()
' 隐式打开文档
Dim oDoc As Document
Set oDoc = ThisApplication.Documents.Open("c:\testpart.ipt", False)
' 需要添加的特性集合名
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' 新建特性集合
Dim oNewPS As PropertySet
If oDoc.PropertySets.PropertySetExists("myNewSet") Then
' 如果已经存在
Set oNewPS = oDoc.PropertySets("myNewSet")
Else
MsgBox "no property named myNewSet"
Exit Sub
End If
Dim oShowStr As String
oShowStr = ""
‘ 遍历特性
Dim oEachP As Property
For Each oEachP In oNewPS
oShowStr = oShowStr & " [Property Name] " & oEachP.Name
oShowStr = oShowStr & " [Property Value] " & oEachP.Value & vbCr
Next
MsgBox oShowStr
oDoc.Close
End Sub
VB.NET
Sub AddCustomProperty()
Dim m_inventorApp As Inventor.Application = Nothing
m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
' open a document invisble
Dim oDoc As Document
oDoc = m_inventorApp.Documents.Open("c:\testpart.ipt",False)
' name of new property set
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' new property set
Dim oNewPS As PropertySet
If oDoc.PropertySets.PropertySetExists("myNewSet") Then
' if the set exists aleady
oNewPS = oDoc.PropertySets("myNewSet")
' you can clean up the existing properties
Else
' add a new one with a GUID
oNewPS = oDoc.PropertySets.Add("myNewSet",
"{3FDB7763-80DB-4269-83E4-7F43BC8E9EA7}")
End If
'the values of the properties
Dim oIntProV As Integer
oIntProV = 100
Dim oBoolProV As Boolean
oBoolProV = False
Dim oDoubleProV As Double
oDoubleProV = 3.1415926000000001
Dim oDateProV As Date
oDateProV = "2013-3-1 15:25"
' add these properties with the meaningful name
' assume they do not exist in the property set
oNewPS.Add(oIntProV, "ModelCount")
oNewPS.Add(oBoolProV, "ModelUpdateToDate")
oNewPS.Add(oDoubleProV, "ModelBasicLength")
oNewPS.Add(oDateProV, "ModelUpdateDate")
oDoc.Save()
oDoc.Close()
End Sub
Sub ReadCustomProperty()
Dim m_inventorApp As
Inventor.Application = Nothing
m_inventorApp = System.Runtime.InteropServices.Marshal.
GetActiveObject("Inventor.Application")
' open a document invisble
Dim oDoc As Document
oDoc = m_inventorApp.Documents.Open("c:\testpart.ipt",False)
' name of new property set
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' new property set
Dim oNewPS As PropertySet = Nothing
If oDoc.PropertySets.PropertySetExists("myNewSet") Then
' if the set exists aleady
oNewPS = oDoc.PropertySets("myNewSet")
' you can clean up the existing properties
Else
MsgBox("no property named myNewSet")
Exit Sub
End If
Dim oShowStr As String
oShowStr = ""
'iterate the properties
Dim oEachP As [Property]
For Each oEachP In oNewPS
oShowStr &= " [Property Name] " &
oEachP.Name
oShowStr &= " [Property Value] " &
oEachP.Value & vbCr
Next
MessageBox.Show(oShowStr)
oDoc.Close()
End Sub
Apprentice
Sub AddCustomProperty_Apprentice()
Dim m_ApprenticeApp As Inventor.ApprenticeServerComponent=
New Inventor.ApprenticeServerComponent()
' open a document
Dim oDoc As ApprenticeServerDocument
oDoc = m_ApprenticeApp.Open("c:\testpart.ipt")
' name of new property set
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' new property set
Dim oNewPS As PropertySet
If oDoc.PropertySets.PropertySetExists("myNewSet") Then
' if the set exists aleady
oNewPS = oDoc.PropertySets("myNewSet")
' you can clean up the existing properties
Else
' add a new one with a GUID
oNewPS = oDoc.PropertySets.Add("myNewSet",
"{3FDB7763-80DB-4269-83E4-7F43BC8E9EA7}")
End If
'the values of the properties
Dim oIntProV As Integer
oIntProV = 100
Dim oBoolProV As Boolean
oBoolProV = False
Dim oDoubleProV As Double
oDoubleProV = 3.1415926000000001
Dim oDateProV As Date
oDateProV = "2013-3-1 15:25"
' add these properties with the meaningful name
' assume they do not exist in the property set
oNewPS.Add(oIntProV, "ModelCount")
oNewPS.Add(oBoolProV, "ModelUpdateToDate")
oNewPS.Add(oDoubleProV, "ModelBasicLength")
oNewPS.Add(oDateProV, "ModelUpdateDate")
' flush the modification. Apprentice requires
oDoc.PropertySets.FlushToFile()
m_ApprenticeApp.FileSaveAs.AddFileToSave(oDoc, oDoc.FullDocumentName)
m_ApprenticeApp.FileSaveAs.ExecuteSave()
End Sub
Sub ReadCustomProperty_Apprentice()
Dim m_ApprenticeApp As Inventor.ApprenticeServerComponent=
New Inventor.ApprenticeServerComponent()
' open a document invisble
' open a document
Dim oDoc As ApprenticeServerDocument
oDoc = m_ApprenticeApp.Open("c:\testpart.ipt")
' name of new property set
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' new property set
Dim oNewPS As PropertySet = Nothing
If oDoc.PropertySets.PropertySetExists("myNewSet") Then
' if the set exists aleady
oNewPS = oDoc.PropertySets("myNewSet")
' you can clean up the existing properties
Else
MsgBox("no property named myNewSet")
Exit Sub
End If
Dim oShowStr As String
oShowStr = ""
'iterate the properties
Dim oEachP As [Property]
For Each oEachP In oNewPS
oShowStr &= " [Property Name] " &
oEachP.Name
oShowStr &= " [Property Value] " &
oEachP.Value & vbCr
Next
MessageBox.Show(oShowStr)
oDoc.Close()
End Sub