D.S

adndevblog.typepad.com

Save extra data in Inventor file 2 - Manufacturing DevBlog

Save extra data in Inventor file 2 - Manufacturing DevBlog Manufacturing DevBlog The resource for software developers working with Design, Lifecycle and Simulation technologies from Autodesk. Home Subscribe Follow Useful Links Autodesk About this Blog « Save extra data in Inventor file 1 | Main | Manipulate rows and columns of iPart - 2 » 03/03/2013 Save extra data in Inventor file – 2 By Xiaodong Liang 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.AttributeMana