ParentOccurrence of selected ComponentOccurrence

ParentOccurrence of selected ComponentOccurrence

Anonymous
Not applicable
577 Views
2 Replies
Message 1 of 3

ParentOccurrence of selected ComponentOccurrence

Anonymous
Not applicable
Hello all,
I'm writing a macro that runs through all components in an assembly and sorts them into collections by their material. The collections store a unique string that corresponds to the components name as follows:
ComponentName + SubComponentName + SubSubComponentName + etc

To do this, I have this function:

{code}Private Function OccurrenceName(Comp As ComponentOccurrence)
Dim Name As String
Dim Parent As ComponentOccurrence
Err.Clear
On Error Resume Next
'see if there is a parent...
Set Parent = Comp.ParentOccurrence

'if there isn't, then the name is the components name
If Err.Number <> 0 Then
Name = Comp.Name

'If there is, then recursively call OccurrenceName to create a unique name for each component
Else
Name = OccurrenceName(Parent) + Comp.Name
End If
OccurrenceName = Name
End Function{code}

There are two ways that this function is called. First is by a sub that automatically runs through all components and subcomponents in the assembly. The function is called for each component and subcomponent individually. Each component's unique name is just the
{code}Component.Name{code}
and each subcomponent's name is
{code}Component.Name + SubComponent.Name{code}
Then these names are stored in a collection.

The second is by a sub that runs through all components in a SelectedSet. The SelectedSet technically contains an OccurrenceProxy for each selected component. So, in this case the function is called using the OccurrenceProxy.NativeObject as the argument. Sometimes the NativeObject is a subcomponent. But for some reason the function isn't able to find a .ParentOccurrence for the NativeObject. Consequently, the subcomponents unique name is not
{code}Component.Name + SubComponent.Name{code}
but rather, it comes out as just
{code}SubComponent.Name{code}

I'm not sure why this is happening. I don't see why the function can tell a difference between a ComponentOccurrence object that was found automatically and one that was found through a SelectedSet. Am I missing something here?
0 Likes
578 Views
2 Replies
Replies (2)
Message 2 of 3

alewer
Advocate
Advocate
Try ComponentOccurrenceProxy.ParentOccurrence instead of ComponentOccurrenceProxy.NativeObject.ParentOccurrence.
0 Likes
Message 3 of 3

Anonymous
Not applicable
Good idea!
For some reason, when I send ComponentOccurrenceProxy.ParentOccurrence to the OccurrenceName function it compiles the name right, but when i send ComponentOccurrenceProxy.NativeObject to the OccurrenceName function, it does not recognize any parents.
wierd. but i got it to work:

After my SelectedSet is selected, the following code is run. The first set is clipped from the sub that is activate when the user says they're done selecting. The next two subs run recursively to access the lowest level component in each selection. And the final two subs give each of those components a name, depending on if they're a ComponentOccurrence or a ComponentOccurrenceProxy. Then finally, these names are sent to AddToCollection(Name), whose code is not shown here:

{code}'----------------------------------------------------------------------------------------------------
For Each obj In oSelection
If obj.Type = kComponentOccurrenceObject Then
Set oPart = obj
If oPart.SubOccurrences.Count <> 0 Then
Call SelectSubAssy(oPart)
Else
Name = OccurrenceName(oPart)
Call AddToCollection(Name)
End If
ElseIf obj.Type = kComponentOccurrenceProxyObject Then
Name = ProxyName(obj)
If obj.SubOccurrences.Count <> 0 Then
Call SelectProxySubAssy(obj)
Else
Call AddToCollection(Name)
End If
End If
Next ob
'----------------------------------------------------------------------------------------------------
Private Sub SelectSubAssy(Comp As ComponentOccurrence)
'This sub goes though each component of the subassembly found in Selection(),
'if the component is another subassemply, then the sub is run recursively to sort through the entire selection.
Dim SubComp As ComponentOccurrence
Dim Name As String

For Each SubComp In Comp.SubOccurrences
If SubComp.Suppressed = False Then
If SubComp.SubOccurrences.Count <> 0 Then
Call SelectSubAssy(SubComp)
Else
Name = OccurrenceName(SubComp)
Call AddToCollection(Name)
End If
End If
Next SubComp
End Sub
'----------------------------------------------------------------------------------------------------
Private Sub SelectProxySubAssy(Proxy As ComponentOccurrenceProxy)
'This sub goes though each component of the subassembly found in Selection(),
'if the component is another subassemply, then the sub is run recursively to sort through the entire selection.
Dim SubProxy As ComponentOccurrenceProxy
Dim Com As ComponentOccurrence
Dim Name As String
Set Comp = Proxy.NativeObject

For Each SubProxy In Proxy.SubOccurrences
If SubProxy.NativeObject.Suppressed = False Then
If SubProxy.SubOccurrences.Count <> 0 Then
Call SelectSubAssy(SubProxy)
Else
Name = ProxyName(SubProxy)
Call AddToCollection(Name)
End If
End If
Next SubComp
End Subj
'----------------------------------------------------------------------------------------------------
Private Function OccurrenceName(Comp As ComponentOccurrence)
Dim Name As String
Dim Parent As ComponentOccurrence
Err.Clear
On Error Resume Next
'see if there is a parent...
Set Parent = Comp.ParentOccurrence

'if there isn't, then the name is the components name
If Err.Number <> 0 Then
Name = Comp.Name

'If there is, then recursively call OccurrenceName to create a unique name for each component
Else
Name = OccurrenceName(Parent) + Comp.Name
End If
OccurrenceName = Name
End Function
'----------------------------------------------------------------------------------------------------
Private Function ProxyName(Proxy As ComponentOccurrenceProxy)
Dim Name As String
Dim Parent As ComponentOccurrence
Err.Clear
On Error Resume Next
'see if there is a parent...
Set Parent = Proxy.ParentOccurrence

'if there isn't, then the name is the components name
If Err.Number <> 0 Then
Name = Proxy.NativeObject.Name

'if there is, then recursively call ProxyName to create a unique name for each component
Else
Name = OccurrenceName(Parent) + Proxy.NativeObject.Name
End If
ProxyName = Name
End Function{code}
0 Likes