Inventor文件中保存自定义数据 - 2

第二种方式是Attribute。我们叫做属性,和iPRoperties以示区别。很多Inventor对象都提供了添加属性的功能,包括文档。属性的操作也有大量文章,本文推荐两个材料:
第一是帮助文档。专门有个章节讲解:  

image 

另外就是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

image

 

上一篇文章类似,以下代码添加了自定义属性集和属性,并演示如何访问。

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值