The second mechanism is attributes. Attributes allow you to attach
information to a specific object. Attributes can be associated with the Document object so you can use this to store general information associated with the document.
Attributes has been widely introduced in various articles. I’d recommend:
- API help reference >> [Attributes]
- Introduction to Attributes on Brian’s blog
http://modthemachine.typepad.com/my_weblog/2009/07/introduction-to-attributes.html
In short:
Creating AttributeSet and Attributes
- From AttributeSets Collection add a new AttributeSet :
Public Function Add(
ByVal AttributeSetName As String,
Optional ByVal CopyWithOwner As Boolean = False )
As AttributeSet
- From AttributeSet Collection add a new Attribute:
Public Function Add(
ByVal AttributeName As String,
ByVal ValueType As ValueTypeEnum,
ByVal Value As Variant ) As Attribute
ValueTypeEnum:
kIntegerType
kDoubleType
kStringType
kByteArrayType
kBooleanType
Querying Attributes
AttributeManager supports different methods to query for attributes.
Can query based on attribute set name, attribute name, and/or attribute value.
FindAttributes
FindAttributeSets
FindObjects
e.g.
Public Function FindObjects(
Optional ByVal AttributeSetName As String = "*",
Optional ByVal AttributeName As String = "*",
Optional ByVal AttributeValue As Variant ) As ObjectCollection
Similar to the first post, In this post, we write a small demo to add a custom attribute set with some attributes. It also shows how to read the information.
VBA
Sub AddCustomAttribute()
' open a document invisible
Dim oDoc As Document
Set oDoc = ThisApplication.Documents.Open("c:\testpart.ipt", False)
' name of new attribute set
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' new attribute set
Dim oNewPS As AttributeSet
If oDoc.AttributeManager.FindAttributeSets(oNameOfNewPS).Count > 0 Then
' if the set exists aleady
Set oNewPS = oDoc.AttributeSets(oNameOfNewPS)
' you can clean up the existing attributes
Else
' add a new one
Set oNewPS = oDoc.AttributeSets.Add(oNameOfNewPS)
End If
'the values of the attributes
Dim oIntProV As Integer
oIntProV = 100
Dim oByteProV() As Byte
oByteProV = StrConv("ABCDEFG", vbFromUnicode)
Dim oDoubleProV As Double
oDoubleProV = 3.1415926
Dim oDateProV As String
oDateProV = "2013-3-1 15:25"
' add these attributes with the meaningful name
' assume they do not exist in the attribute set
Call oNewPS.Add("ModelCount", kIntegerType, oIntProV)
Call oNewPS.Add("ModelByte", kByteArrayType, oByteProV)
Call oNewPS.Add("ModelBasicLength", kDoubleType, oDoubleProV)
Call oNewPS.Add("ModelUpdateDate", kStringType, oDateProV)
oDoc.Save
oDoc.Close
End Sub
Sub ReadCustomAttribute()
' open a document invisible
Dim oDoc As Document
Set oDoc = ThisApplication.Documents.Open("c:\testpart.ipt", False)
' name of new attribute set
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' new attribute set
Dim oNewPS As AttributeSet
If oDoc.AttributeManager.FindAttributeSets(oNameOfNewPS).Count > 0 Then
' if the set exists aleady
Set oNewPS = oDoc.AttributeSets(oNameOfNewPS)
' you can clean up the existing attributes
Else
MsgBox "no attribute set named myNewSet"
Exit Sub
End If
Dim oShowStr As String
oShowStr = ""
'iterate the attributes
Dim oEachAtt As Attribute
For Each oEachAtt In oNewPS
oShowStr = oShowStr & " [Attribute Name] " & oEachAtt.Name
If oEachAtt.Name = "ModelByte" Then
oShowStr = oShowStr & " [Attribute Value] " & StrConv(oEachAtt.Value, vbUnicode) & vbCr
Else
oShowStr = oShowStr & " [Attribute Value] " & oEachAtt.Value & vbCr
End If
Next
MsgBox oShowStr
oDoc.Close
End Sub
VB.NET
Sub AddCustomAttribute()
Dim m_inventorApp As Inventor.Application = Nothing
m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
' open a document invisible
Dim oDoc As Document
oDoc = m_inventorApp.Documents.Open("c:\testpart.ipt", False)
' name of new attribute set
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' new attribute set
Dim oNewPS As AttributeSet
If oDoc.AttributeManager.FindAttributeSets(oNameOfNewPS).Count > 0 Then
' if the set exists aleady
oNewPS = oDoc.AttributeSets(oNameOfNewPS)
' you can clean up the existing attributes
Else
' add a new one
oNewPS = oDoc.AttributeSets.Add(oNameOfNewPS)
End If
'the values of the attributes
Dim oIntProV As Integer
oIntProV = 100
Dim oByteProV() As Byte
oByteProV = System.Text.Encoding.Default.GetBytes("ABCDEFG")
Dim oDoubleProV As Double
oDoubleProV = 3.1415926
Dim oDateProV As String
oDateProV = "2013-3-1 15:25"
' add these attributes with the meaningful name
' assume they do not exist in the attribute set
Call oNewPS.Add("ModelCount", ValueTypeEnum.kIntegerType, oIntProV)
Call oNewPS.Add("ModelByte", ValueTypeEnum.kByteArrayType, oByteProV)
Call oNewPS.Add("ModelBasicLength", ValueTypeEnum.kDoubleType, oDoubleProV)
Call oNewPS.Add("ModelUpdateDate", ValueTypeEnum.kStringType, oDateProV)
oDoc.Save()
oDoc.Close()
End Sub
Sub ReadCustomAttribute()
Dim m_inventorApp As Inventor.Application = Nothing
m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
' open a document invisible
Dim oDoc As Document
oDoc = m_inventorApp.Documents.Open("c:\testpart.ipt", False)
' name of new attribute set
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' new attribute set
Dim oNewPS As AttributeSet
If oDoc.AttributeManager.FindAttributeSets(oNameOfNewPS).Count > 0 Then
' if the set exists aleady
oNewPS = oDoc.AttributeSets(oNameOfNewPS)
' you can clean up the existing attributes
Else
MsgBox("no attribute set named myNewSet")
Exit Sub
End If
Dim oShowStr As String
oShowStr = ""
'iterate the attributes
Dim oEachAtt As Attribute
For Each oEachAtt In oNewPS
oShowStr = oShowStr & " [Attribute Name] " & oEachAtt.Name
If oEachAtt.Name = "ModelByte" Then
oShowStr = oShowStr & " [Attribute Value] " & System.Text.Encoding.Default.GetString(oEachAtt.Value) & vbCr
Else
oShowStr = oShowStr & " [Attribute Value] " & oEachAtt.Value & vbCr
End If
Next
MessageBox.Show(oShowStr)
oDoc.Close()
End Sub
Sub AddCustomAttribute_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 attribute set
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' new attribute set
Dim oNewPS As AttributeSet
If oDoc.AttributeManager.FindAttributeSets(oNameOfNewPS).Count > 0 Then
' if the set exists aleady
oNewPS = oDoc.AttributeSets(oNameOfNewPS)
' you can clean up the existing attributes
Else
' add a new one
oNewPS = oDoc.AttributeSets.Add(oNameOfNewPS)
End If
'the values of the attributes
Dim oIntProV As Integer
oIntProV = 100
Dim oByteProV() As Byte
oByteProV = System.Text.Encoding.Default.GetBytes("ABCDEFG")
Dim oDoubleProV As Double
oDoubleProV = 3.1415926
Dim oDateProV As String
oDateProV = "2013-3-1 15:25"
' add these attributes with the meaningful name
' assume they do not exist in the attribute set
Call oNewPS.Add("ModelCount", ValueTypeEnum.kIntegerType, oIntProV)
Call oNewPS.Add("ModelByte", ValueTypeEnum.kByteArrayType, oByteProV)
Call oNewPS.Add("ModelBasicLength", ValueTypeEnum.kDoubleType, oDoubleProV)
Call oNewPS.Add("ModelUpdateDate", ValueTypeEnum.kStringType, oDateProV)
m_ApprenticeApp.FileSaveAs.AddFileToSave(oDoc, oDoc.FullDocumentName)
m_ApprenticeApp.FileSaveAs.ExecuteSave()
End Sub
Sub ReadCustomAttribute_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 attribute set
Dim oNameOfNewPS As String
oNameOfNewPS = "myNewSet"
' new attribute set
Dim oNewPS As AttributeSet
If oDoc.AttributeManager.FindAttributeSets(oNameOfNewPS).Count > 0 Then
' if the set exists aleady
oNewPS = oDoc.AttributeSets(oNameOfNewPS)
' you can clean up the existing attributes
Else
MsgBox("no attribute set named myNewSet")
Exit Sub
End If
Dim oShowStr As String
oShowStr = ""
'iterate the attributes
Dim oEachAtt As Attribute
For Each oEachAtt In oNewPS
oShowStr = oShowStr & " [Attribute Name] " & oEachAtt.Name
If oEachAtt.Name = "ModelByte" Then
oShowStr = oShowStr & " [Attribute Value] " & System.Text.Encoding.Default.GetString(oEachAtt.Value) & vbCr
Else
oShowStr = oShowStr & " [Attribute Value] " & oEachAtt.Value & vbCr
End If
Next
MessageBox.Show(oShowStr)
oDoc.Close()
End Sub