Get index of selected part or sub assembly

Get index of selected part or sub assembly

Darrell.johnson
Enthusiast Enthusiast
782 Views
2 Replies
Message 1 of 3

Get index of selected part or sub assembly

Darrell.johnson
Enthusiast
Enthusiast
Dim oOccurrence As ComponentOccurrence
Dim Prop As String

Try
  oOccurrence = ThisDoc.Document.SelectSet.Item(1)
Catch
  MessageBox.Show("Please select a part / subAssembly!", "Get Index of Part / SubAssembly", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
  Return
End Try

'active assembly s
Dim oMainAsm As AssemblyDocument = ThisDoc.Document
Dim oAsmDef As AssemblyComponentDefinition = oMainAsm.ComponentDefinition


index = oOccurrence."INDEX OF THE SELECTED PART / SUBASSEMBLY" ???

' get the occurrence by its position in the occurrences collection
Dim oOcc As ComponentOccurrence = oAsmDef.Occurrences.Item(index)

Is it possible to get the index from oOccurrence as shown in the Code above?

0 Likes
Accepted solutions (1)
783 Views
2 Replies
Replies (2)
Message 2 of 3

tobias.orlow
Alumni
Alumni
Accepted solution

Hi @Darrell.johnson ,

 

you can do so by iterating through the Occurrences collection with a 'for'-loop as seen in the code below.

Note however that this will only go through your top level assemblies' occurrences. If you have more nested assemblies and e.g. a part within a sub-assembly gets selected, you'll have to add additional logic.

 

Dim oOccurrence As ComponentOccurrence
Dim Prop As String

Try
  oOccurrence = ThisDoc.Document.SelectSet.Item(1)
Catch
  MessageBox.Show("Please select a part / subAssembly!", "Get Index of Part / SubAssembly", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
  Return
End Try

'active assembly s
Dim oMainAsm As AssemblyDocument = ThisDoc.Document
Dim oAsmDef As AssemblyComponentDefinition = oMainAsm.ComponentDefinition
Dim index As Integer For i = 1 To oAsmDef.Occurrences.Count If oAsmDef.Occurrences.Item(i).Name = oOccurrence.Name Then index = i Exit For End If Next ' get the occurrence by its position in the occurrences collection Dim oOcc As ComponentOccurrence = oAsmDef.Occurrences.Item(index) MessageBox.Show("You selected: " & oOcc.Name)

 

Best regards,

Tobias

Tobias Orlow
Designated Support Specialist
Customer Success Organization
Linkedin: www.linkedin.com/in/tobiasorlow/
0 Likes
Message 3 of 3

Darrell.johnson
Enthusiast
Enthusiast

It works perfectly, thank you very much!

0 Likes