This code is getting error, I am not able to search the desired name of the occurrence. Please any one help in this.
Sub Main()
RotateCoil("StdParts\Bv06Coil_N:S1")
End Sub
Sub RotateCoil(pointValue As String)
'Dim Doc As XmlDocument
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
'Search for occurence name
Dim oOccurence As ComponentOccurrence= FindOccByName(oAsmDoc.ComponentDefinition.Occurrences, pointValue)
' For Each oOcc As ComponentOccurrence In oAsmDoc.ComponentDefinition.Occurrences
' If oOcc.Name=("StdParts\Bv06Coil_N:S2" )Then
' oOccurence = oOcc
' Exit For
' End If
' Next
'Check if Occurrence was not found
If oOccurence Is Nothing Then
MessageBox.Show("Occurrence with the given name was not found")
Exit Sub
End If
'Count the number of sections
Dim sectionCount As Integer = CountSections(oOccurence)
'Define the rotation matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
oMatrix.SetToRotation(Math.PI, ThisApplication.TransientGeometry.CreateVector(0,0,1),ThisApplication.TransientGeometry.CreatePoint(0,0,0))
'Perform the rotation based on the section
If sectionCount > 3 Then
'Rotate 2 scetion Coil
Dim oPart2Occ As ComponentOccurrence = FindOccByName(oOccurence.Definition.Occurrences, "StdParts\Bv06Coil_N:S2")
' For Each comp As ComponentOccurrence In oOccurence.Defination.Occurrences
' If comp.Name ="StdParts\Bv06Coil_N:S2" Then
' oPart2Occ = comp
' Exit For
' End If
' Next
If Not oPart2Occ Is Nothing Then
oPart2Occ.Transformation = oMatrix
Else
MessageBox.Show("BV06Coil:S2 not found within the occurrence")
End If
Else
'Rotate the 1 section coil
Dim oPart1Occ As ComponentOccurrence = FindOccByName(oOccurence.SubOccurrences, "StdParts\Bv06Coil_N:S1")
' For Each comp As ComponentOccurrence In oOccurence.Defination.Occurrences
' If comp.Name ="StdParts\Bv06Coil_N:S2" Then
' oPart1Occ = comp
' Exit For
' End If
' Next
If Not oPart1Occ Is Nothing Then
oPart1Occ.Transformation = oMatrix
Else
MessageBox.Show("BV06Coil:S1 not found within the occurrence")
End If
End If
End Sub
Function FindOccByName(occurrences As ComponentOccurrences, name As String) As ComponentOccurrence
For Each occ As ComponentOccurrence In occurrences
If occ.Name = name Then
Return occ
End If
'Recursively search innested Occurrences
Dim foundOcc As ComponentOccurrence = FindOccByName(occ.SubOccurrences, name)
If Not foundOcc Is Nothing Then
Return foundOcc
End If
Next
Return Nothing
End Function
Function CountSections(occurrence As ComponentOccurrence) As Integer
Dim count As Integer = 0
For Each comp As ComponentOccurrence In occurrence.SubOccurrences
If comp.Name.Contains("Bodies") Then
count += 1
End If
Next
Return count
End Function