第二种方式是Attribute。我们叫做属性,和iPRoperties以示区别。很多Inventor对象都提供了添加属性的功能,包括文档。属性的操作也有大量文章,本文推荐两个材料:
第一是帮助文档。专门有个章节讲解:
另外就是Brian的博客文章。我还没来得及翻译。
http://modthemachine.typepad.com/my_weblog/2009/07/introduction-to-attributes.html
简单讲:
创建属性集(AttributeSet)和 属性(Attributes)需要用的方法:
- 通过对象的AttributeSets集合添加属性集
Public Function Add(
ByVal AttributeSetName As String,
Optional ByVal CopyWithOwner As Boolean = False )
As AttributeSet
- 通过属性集添加属性:
Public Function Add(
ByVal AttributeName As String,
ByVal ValueType As ValueTypeEnum,
ByVal Value As Variant ) As Attribute
目前属性可以添加以下类型:
ValueTypeEnum:
kIntegerType 整形
kDoubleType 实数
kStringType 字串
kByteArrayType 字节
kBooleanType 布尔值
查询属性
每个文档都有一个属性管理对象AttributeManager。它支持多种灵活的方式查询。而且比起遍历快很多。可以基于属性集名,属性名或属性值查找。
FindAttributes
FindAttributeSets
FindObjects
Public Function FindObjects(
Optional ByVal AttributeSetName As String = "*",
Optional ByVal AttributeName As String = "*",
Optional ByVal AttributeValue As Variant ) As ObjectCollection
和上一篇文章类似,以下代码添加了自定义属性集和属性,并演示如何访问。
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