Attributes of ComponentOccurrence vs ComponentOccurrenceProxy

<?xml encoding=”UTF-8″>By Adam Nagy

Attributes are context sensitive because they are saved in the document which is hosting the object that has the attribute.

Let’s say we have the following assembly structure:

Asm1
  + Asm2:1
    + Part1:1
    + Part2:1

If you open up Asm2.iam and add an attribute to Part1:1 then it will be stored in Asm2.iam, so if now you open up Asm1.iam and check the attributes on Asm2:1 >> Part1:1, the attribute will not be there. However, you can easily move to the corresponding object in Asm2.iam using the NativeObject property of the ComponentOccurrenceProxy object. 

Some info on Proxies and Contexts: http://adndevblog.typepad.com/manufacturing/2013/07/occurrences-contexts-definitions-proxies.html

You can play with attributes using the following VBA code:
– SetAttribute adds an attribute to the selected occurrence with a value that also contains the display name of the assembly document that hosts the occurrence 
– GetAttribute gets back the attribute of the selected occurrence – if useNativeObject = True then it will look for the attribute inside the document that hosts the occurrence:

Sub SetAttribute()
Dim doc As Document
Set doc = ThisApplication.ActiveDocument
Dim ent As Object
Set ent = doc.SelectSet(1)
Dim attSets As AttributeSets
Set attSets = ent.AttributeSets
Dim attSet As AttributeSet
If attSets.NameIsUsed("MyAttSet") Then
Set attSet = attSets("MyAttSet")
Else
Set attSet = attSets.Add("MyAttSet")
End If
Dim att As Attribute
If attSet.NameIsUsed("MyAtt") Then
Set att = attSet("MyAtt")
Else
Set att = attSet.Add("MyAtt", kStringType, _
"MyAttValue in " + doc.DisplayName)
End If
End Sub
Sub GetAttribute()
Dim doc As Document
Set doc = ThisApplication.ActiveDocument
Dim ent As Object
Set ent = doc.SelectSet(1)
' You can set this
Dim useNativeObject As Boolean
useNativeObject = False
If TypeOf ent Is ComponentOccurrenceProxy _
And useNativeObject Then
Set ent = ent.NativeObject
End If
Dim attSets As AttributeSets
Set attSets = ent.AttributeSets
Dim attSet As AttributeSet
If attSets.NameIsUsed("MyAttSet") Then
Set attSet = attSets("MyAttSet")
Dim att As Attribute
If attSet.NameIsUsed("MyAtt") Then
Set att = attSet("MyAtt")
End If
Call MsgBox(att.Value)
End If
End Sub

Here is a pic showing what you get back using GetAttribute if both in Asm1 and Asm2 you added attributes to Part2:1 by selecting it and calling SetAttribute:

Proxyattributes

 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading