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

这篇博客介绍了如何在Autodesk Inventor中使用VBA和VB.NET添加、读取自定义属性。文章详细阐述了创建属性集、添加不同类型的属性(如整型、字符串型等)以及如何通过属性管理对象查询属性的方法。还提供了VBA和VB.NET的示例代码,展示如何为零件文档添加自定义属性集和属性,以及如何读取这些属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第二种方式是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

        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 > 0Then

            ' 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 > 0Then

            ' 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 

Apprentice  

    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 > 0Then

            ' 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 > 0Then

            ' 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

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值