Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to set AttributeSets in Assembly based on AttributeSet of Part?

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
roger.fankhauser
901 Views, 6 Replies

How to set AttributeSets in Assembly based on AttributeSet of Part?

Hello Inventor Community,

 

I have following situation: 

 

Assembly1:

  •  2 x Part1 --> Has AttributeSet with Name = "ANSYS_NS_COLLECTION"
    • Item1.Name = "S_" ,  2 Faces
    • Item2.Name = "W_",  2 Faces
  • 2 x Part2 --> Has AttributeSet with Name = "ANSYS_NS_COLLECTION" 
    • Item1.Name = "S_",  3 Faces 
    • Item2.Name = "W_", 3 Faces

 

I added the attributes over the Ansys Addin (named selection manager) 

 

Part 1: 2 faces for "S_" and 2 faces for "W_"

rogerfankhauser_2-1624977786195.png

 

Part 2: 3 faces for "S_" and 3 faces for "W_"

rogerfankhauser_0-1624977682779.png

 

 

 

In the Assembly I want to check if there are parts with AttributeSet Name = "ANSYS_NS_COLLECTION".

If so, create new attribute on assembly level with the same faces as in part level but the item name must be renamed to "S_count" respectively "W_count". ("S_1", W_1"). The AttributeSet Name should remain "ANSYS_NS_COLLECTION".

 

In the end the Assembly should have 8 attributesets:

  • Attribute Set Name: "ANSYS_NS_COLLECTION"
    • Item 1 Name: "S_1" , 2 Faces 
    • Item 2 Name: "W_1", 2 Faces
    • Item 3 Name: "S_2", 2 Faces 
    • ...
    • Item 7 Name: "S_7", 3 Faces
    • Item 8 Name: "W_8, 3 Faces

Attached is the sample CAD File of the assembly as described here. 

 

 

What I have so far is to get all the Attributes from the parts. But I dont know how to create new one in the Assembly? 

 

Public Sub AssemblyAttribute()


    'check that the active document is an assembly file
    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
        MsgBox ("Please run this rule from the assembly document file.")
        Exit Sub
    End If
    
    ' Get the active assembly document
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    Debug.Print "Active Document: " + oDoc.DisplayName

    With oDoc
        For i = 1 To .ComponentDefinition.Occurrences.Count
            Dim oOcc As ComponentOccurrence
            Set oOcc = .ComponentDefinition.Occurrences(i)
            Debug.Print "NS for Part: " + oOcc.Name
            
            Dim oAttMgr As AttributeManager
            Dim oAttsetsEnum As AttributeSetsEnumerator
            Set oAttMgr = oOcc.Definition.Document.AttributeManager
            Set oAttsetsEnum = oAttMgr.FindAttributeSets("ANSYS_NS_COLLECTION")

            Dim oAttSet As AttributeSet
            Dim oAtt As Inventor.Attribute
            
            For Each oAttSet In oAttsetsEnum
                For Each oAtt In oAttSet
                    If InStr(oAtt.Name, "S_") Or InStr(oAtt.Name, "W_") Then
                        Debug.Print oAtt.Name
                    End If
                Next
            Next
        Next
    End With
    
End Sub

 

Produces following output:

Active Document: Assembly1.iam
NS for Part: Part2:1
W_
W_
W_
S_
S_
S_
NS for Part: Part2:2
W_
W_
W_
S_
S_
S_
NS for Part: Part1:1
W_
W_
S_
S_
NS for Part: Part1:2
W_
W_
S_
S_
Labels (5)
6 REPLIES 6
Message 2 of 7

Why don't you create iProperties on part level and then write AttributeSet in the assembly ?

 

to create an attributeSet in an assembly, you need to access it from the occurrence object

 

ComponentOccurrence.AttributeSets

Message 3 of 7

Hello

 

A first draw. Suppressed files and virtual components must be ignored.

Public Sub AssemblyAttribute()


    Dim W_Count As Integer
    Dim S_Count As Integer
    
    'check that the active document is an assembly file
    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
        MsgBox ("Please run this rule from the assembly document file.")
        Exit Sub
    End If
    
    ' Get the active assembly document
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    Debug.Print "Active Document: " + oDoc.DisplayName

    With oDoc
        Call DeleteAllAttributes(oDoc)
        Dim oAssAttrSet As AttributeSet
        Set oAssAttrSet = GetAttributeSet(oDoc)
        
        For i = 1 To .ComponentDefinition.Occurrences.Count
            S_Count = 0
            W_Count = 0
            Dim oOcc As ComponentOccurrence
            Set oOcc = .ComponentDefinition.Occurrences(i)
            Debug.Print "NS for Part: " + oOcc.Name
            
            If oOcc.Suppressed = False Then
                If Not oOcc.Definition.Type = kVirtualComponentDefinitionObject Then
                    Dim oAttMgr As AttributeManager
                    Dim oAttsetsEnum As AttributeSetsEnumerator
                    Set oAttMgr = oOcc.Definition.Document.AttributeManager
                    Set oAttsetsEnum = oAttMgr.FindAttributeSets("ANSYS_NS_COLLECTION")
        
                    Dim oAttSet As AttributeSet
                    Dim oAtt As Inventor.Attribute
                    
                    For Each oAttSet In oAttsetsEnum
                        For Each oAtt In oAttSet
                            If InStr(oAtt.Name, "S_") Then
                                S_Count = S_Count + 1
                            ElseIf InStr(oAtt.Name, "W_") Then
                                W_Count = W_Count + 1
                            End If
                        Next
                    Next
                    Call AddAttribute(oDoc, "S_" & i, S_Count)
                    Call AddAttribute(oDoc, "W_" & i, W_Count)
                End If
            End If
        Next
    End With
    
    
End Sub

Private Sub DeleteAllAttributes(ByVal oDoc As AssemblyDocument)

Dim oAttrEnum As AttributesEnumerator
Dim oAttr As Inventor.Attribute
Dim oAttrSet As AttributeSet

For Each oAttrSet In oDoc.AttributeSets
    If oAttrSet.Name = "ANSYS_NS_COLLECTION" Then
        Set oAttrEnum = oDoc.AttributeManager.FindAttributes("ANSYS_NS_COLLECTION")
    End If
Next

If oAttrEnum Is Nothing Then Exit Sub

For Each oAttr In oAttrEnum
    oAttr.Delete
Next

End Sub

Private Sub AddAttribute(ByVal oDoc As AssemblyDocument, ByVal sAttrName As String, ByVal iAttrValue As Integer)

Dim oAttrSetEnum As AttributeSetsEnumerator
Set oAttrSetEnum = oDoc.AttributeManager.FindAttributeSets("ANSYS_NS_COLLECTION")

Dim oAttrSet As AttributeSet
Set oAttrSet = oAttrSetEnum(1)

Dim oAttr As Inventor.Attribute
Set oAttr = oAttrSet.Add(sAttrName, kIntegerType, iAttrValue)

End Sub

Private Function GetAttributeSet(ByVal oDoc As AssemblyDocument) As AttributeSet

Dim oAttrSetEnum As AttributeSetsEnumerator
Set oAttrSetEnum = oDoc.AttributeManager.FindAttributeSets("ANSYS_NS_COLLECTION")

Dim oAttrSet As AttributeSet

If oAttrSetEnum.Count = 0 Then
    Set GetAttributeSet = oDoc.AttributeSets.Add("ANSYS_NS_COLLECTION")
Else
    Set GetAttributeSet = oAttrSetEnum(1)
End If

End Function

 


R. Krieg
RKW Solutions GmbH
www.rkw-solutions.com
Message 4 of 7

As far as I know it's not possible to store faces in Iproperties. I need to know the location of the face which is defined in the part document. I had some issues trying this. Becase the face had already an attribute. 

Message 5 of 7

Thank you! How can I add the Face? The code will create attributes but whitout the face. 

 

 The attributes in the part document are set to the face. 

rogerfankhauser_0-1625035160997.png

 

In the assembly it created assembly attributes but not on the faces. What I need is to create the FaceProxy attributes on the same location as in the part document. 

rogerfankhauser_1-1625035253028.png

 

Message 6 of 7

Hello

 

On one hand you write you need attributes in the assembly with the count (or sum) of faces of type S_ and W_ in each occurrence. On other hand you write that you need attributes on every face proxy. Or do you mean a mix of both, an attribute on every face proxy with the count or sum of type S_x and W_x for each occurrence as value? If so, we need to modify code a bit.

You can "copy" every face attribute to it's face proxy in assembly space wih code below.

 

'Option Explicit

Public Sub AssemblyAttribute()


    Dim W_Count As Integer
    Dim S_Count As Integer
    S_Count = 0
    W_Count = 0
            
    'check that the active document is an assembly file
    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
        MsgBox ("Please run this rule from the assembly document file.")
        Exit Sub
    End If
    
    ' Get the active assembly document
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    Debug.Print "Active Document: " + oDoc.DisplayName

    With oDoc
        Call CleanAttrSets(oDoc)
        Dim oAssAttrSet As AttributeSet
        Set oAssAttrSet = GetAttributeSet(oDoc)
        
        For i = 1 To .ComponentDefinition.Occurrences.Count
            Dim oOcc As ComponentOccurrence
            Set oOcc = .ComponentDefinition.Occurrences(i)
            Debug.Print "NS for Part: " + oOcc.Name
            
            If oOcc.Suppressed = False Then
                If Not oOcc.Definition.Type = kVirtualComponentDefinitionObject Then
                    S_Count = S_Count + 1
                    W_Count = W_Count + 1
                    
                    Dim oAttMgr As AttributeManager
                    Set oAttMgr = oOcc.Definition.Document.AttributeManager
        
                    Dim oAttSet As AttributeSet
                    Dim oAtt As Inventor.Attribute
                    
                    'get all faces with attribute
                    Dim oFaceColl As ObjectCollection
                    Set oFaceColl = oAttMgr.FindObjects("ANSYS_NS_COLLECTION")
                    
                    Dim oFace As Face
                    Dim oFaceProxy As FaceProxy
                    
                    For Each oFace In oFaceColl
                        Call oOcc.CreateGeometryProxy(oFace, oFaceProxy)
                    
                        Set oAttSet = oFace.AttributeSets("ANSYS_NS_COLLECTION")
                        For Each oAtt In oAttSet
                            If InStr(oAtt.Name, "S_") Then
                                Call AddAttribute(oFaceProxy, "S_" & S_Count, 1)
                            ElseIf InStr(oAtt.Name, "W_") Then
                                Call AddAttribute(oFaceProxy, "W_" & W_Count, 1)
                            End If
                        Next
                    Next
                End If
            End If
        Next
    End With
    
    
End Sub

Private Sub AddAttribute(ByVal oFaceProxy As FaceProxy, ByVal sAttrName As String, ByVal iAttrValue As Integer)

    Dim oAttrSet As AttributeSet
    For Each oAttrSet In oFaceProxy.AttributeSets
        If oAttrSet.Name = "ANSYS_NS_COLLECTION" Then Exit For
    Next
    
    If oAttrSet Is Nothing Then
        Set oAttrSet = oFaceProxy.AttributeSets.Add("ANSYS_NS_COLLECTION")
    End If
        
    Dim oAttr As Inventor.Attribute
    For Each oAttr In oAttrSet
        If oAttr.Name = sAttrName Then Exit For
    Next
        
    If oAttr Is Nothing Then
        Set oAttr = oAttrSet.Add(sAttrName, kIntegerType, iAttrValue)
    Else
        oAttr.Value = oAttr.Value + iAttrValue
    End If

End Sub

Private Function GetAttributeSet(ByVal oDoc As AssemblyDocument) As AttributeSet

    Dim oAttrSetEnum As AttributeSetsEnumerator
    Set oAttrSetEnum = oDoc.AttributeManager.FindAttributeSets("ANSYS_NS_COLLECTION")
    
    Dim oAttrSet As AttributeSet
    
    If oAttrSetEnum.Count = 0 Then
        Set GetAttributeSet = oDoc.AttributeSets.Add("ANSYS_NS_COLLECTION")
    Else
        Set GetAttributeSet = oAttrSetEnum(1)
    End If

End Function

Private Sub CleanAttrSets(ByVal oDoc As AssemblyDocument)

    Dim oAttrMgr As AttributeManager
    Set oAttrMgr = oDoc.AttributeManager
    
    Dim oAttrSetsEnum As AttributeSetsEnumerator
    Dim oAttrEnum As AttributesEnumerator
    
    Dim oAttrSet As AttributeSet
    Dim oAttr As Inventor.Attribute
    
    Set oAttrEnum = oAttrMgr.FindAttributes("ANSYS_NS_COLLECTION", "*")
    For Each oAttr In oAttrEnum
        Call oAttr.Delete
    Next
    
    Set oAttrSetsEnum = oAttrMgr.FindAttributeSets("ANSYS_NS_COLLECTION")
    For Each oAttrSet In oAttrSetsEnum
        Call oAttrSet.Delete
    Next

End Sub

 

 


R. Krieg
RKW Solutions GmbH
www.rkw-solutions.com
Message 7 of 7

Perfect! Exactly what I needed🙂 

 

In the meanwhile I tried it by myself but did not get the solution. The error occured when I tried to add the attribute to the attirbute set. Could you explain me what I did wrong? 

 

Public Sub AssemblyAttribute()


    Dim W_Count As Integer
    Dim S_Count As Integer
    
    'check that the active document is an assembly file
    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
        MsgBox ("Please run this rule from the assembly document file.")
        Exit Sub
    End If
    
    ' Get the active assembly document
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    Debug.Print "Active Document: " + oDoc.DisplayName
    
    S_Count = 0
    W_Count = 0
    
    
    SetName = "ANSYS_NS_COLLECTION"
    
    
    With oDoc
        'Call DeleteAllAttributes(oDoc)
        Dim oAssAttrSet As AttributeSet
        Set oAssAttrSet = GetAttributeSet(oDoc)

        For i = 1 To .ComponentDefinition.Occurrences.Count
            
            Dim oOcc As ComponentOccurrence
            Set oOcc = .ComponentDefinition.Occurrences(i)
            Debug.Print "NS for Part: " + oOcc.Name
            
            Dim oFace As face
            Dim oFaceProxy As FaceProxy
            Dim oAttribSets As AttributeSets
            Dim oAttribSet As Inventor.AttributeSet
            Dim oAttrib As Inventor.Attribute
            
            If oOcc.Suppressed = False Then
                If Not oOcc.Definition.Type = kVirtualComponentDefinitionObject Then
                    Dim oAttMgr As AttributeManager
                    Set oAttMgr = oOcc.Definition.Document.AttributeManager
                    
                    ' Get collection of attributes with SetName "ANSYS_NS_COLLECTION" and Name "S_" or "W_"
                    Dim oAttCollection_W As Inventor.ObjectCollection
                    Dim oAttCollection_S As Inventor.ObjectCollection
                    Set oAttCollection_W = oAttMgr.FindObjects(SetName, "S_")
                    Set oAttCollection_S = oAttMgr.FindObjects(SetName, "W_")
                    
                    ' Check if there are faces with "W_"
                    If oAttCollection_W.Count > 0 Then
                        Debug.Print "W_faces: " + Str(oAttCollection_W.Count)
                        W_Count = W_Count + 1
                        Name = "W_" + Str(W_Count)

                        ' Iterate over each Face in Collection
                        For Each oFace In oAttCollection_W
                            ' Create Face Proxy
                            Set oFaceProxy = Nothing
                            Call oOcc.CreateGeometryProxy(oFace, oFaceProxy)
                            ' get the face proxy attribute sets
                            Set oAttribSets = oFaceProxy.AttributeSets
                            ' Create attribute set
                            Set oAttribSet = oFaceProxy.AttributeSets.Add(SetName)
                            Set oAttrib = oAttribSet.Add(Name, kIntegerType, 1) ' This line did not work ?
                            
                        Next
                    End If
                End If
            End If
        Next
    End With

        
End Sub

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report