Often we need to access attributes and attribute sets in an iPart Child component. Unfortunately, attributes that you create in the iPart Factory do not carry over to the children.
Thankfully, we can still get to those attributes through automation. This involves using the Inventor API but this code can be ran in iLogic just as if it was using the iLogic Snippets.
This code is only looking at Faces and Workplanes but that can be expanded upon to look at other entities.
The full code is listed at the bottom of this post. I'll break down what it's doing:
The main sub gets the iPart Child occurrence that is used in your assembly and passes that to the CopyAttributes subroutine.
The CopyAttributes subroutine:
- Gets the Factory document from the occurrence
- Loops through all the faces/workplanes in the Factory document
- If it finds an attribute set, then it creates the same attribute set on the corresponding entity of the Child occurrence.
These attributes can be used for finding entities (think of the times that you select a face or a workplace while in an assembly). One of the more common reasons I use them is for creating constraints.
Sub Main() ' Get occurrence Dim oOcc as ComponentOccurrence oOcc = Component.InventorComponent("BrowserName") ' Copy attributes CopyAttributes(oOcc) End Sub Public Sub CopyAttributes(oOcc As ComponentOccurrence) ' Declarations Dim oOccFaces As Faces oOccFaces = oOcc.SurfaceBodies.Item(1).Faces Dim oOccWorkPlanes as Workplanes oOccWorkPlanes = oOcc.Definition.Workplanes Dim oFactoryDoc As PartDocument oFactoryDoc = oOcc.Definition.iPartMember.ParentFactory.Parent Dim oNativeFaces As Faces oNativeFaces = oFactoryDoc.ComponentDefinition.SurfaceBodies.Item(1).Faces Dim oNativeWorkPlanes As Workplanes oNativeWorkPlanes = oFactoryDoc.ComponentDefinition.Workplanes ' Loop thru faces in factory and copy Dim i As Long For i = 1 To oNativeFaces.Count Dim oFaceProxy As Face oFaceProxy = oOccFaces.Item(i) Dim oAttributeSet As AttributeSet For Each oAttributeSet In oNativeFaces(i).AttributeSets ' Delete if existing If oFaceProxy.AttributeSets.NameIsUsed(oAttributeSet.Name) Then oFaceProxy.AttributeSets(oAttributeSet.Name).Delete End If Dim oNewAttSet As AttributeSet oNewAttSet = oFaceProxy.AttributeSets.Add(oAttributeSet.Name) Next Next For i = 1 To oNativeWorkPlanes.count Dim oWPProxy As WorkPlane oWPProxy = oOccWorkPlanes.item(i) Dim oAttributeSet As AttributeSet For Each oAttributeSet In oNativeWorkPlanes(i).AttributeSets ' Delete if existing If oWPProxy.AttributeSets.NameIsUsed(oAttributeSet.name) Then oWPProxy.AttributeSets(oAttributeSet.name).Delete End If Dim oNewAttSet As AttributeSet oNewAttSet = oWPProxy.AttributeSets.Add(oAttributeSet.name) Next Next End Sub
About the Author
Follow on Linkedin More Content by Randall Mabery