Parsing the Component2.Name2

Here is a little Function I wrote to use in Assemblies thatallows one to use the Component.Name2 raw string to build thecorrect SelectByID2 string to be assigned to a variable and calledwhen needed. As you can see it could have many uses. Let me knowwhat you think. I hope this might be helpful.

Get your RAW string (example, Adding a Part to an Assembly):

Set swComp = swAssy.AddComponent4(PartFileName, "", ItemPoint(0),ItemPoint(1), ItemPoint(2))
Set swDoc = swApp.ActivateDoc2(AssyFileName, True, swErrors) ' Goback to Assy
ItemStatusB = swComp.Select(False) ' Select Part
'Grab the Front Plane of that Part
AssyItemPart = "Front Plane" + "@" +ItemSelectID((swComp.Name2))
' Select the Part Plane for the mate
ItemStatusB = swModelDocExt.SelectByID2(AssyItemPart, "PLANE", 0,0, 0, True, 0, Nothing, 0)


'------- START FUNCTION CODE --------

' Parse String into correct format
Private Function ItemSelectID(AssyItemLoc As String) As String
On Error Resume Next
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim lDocType As Long, Offset As Long
Dim AssyCount As Long, AssyTotal As Long, swErrors As Long
Dim AssyRoot As String, AssyActive As String
Dim AssyItemName As String, AssyItemPrefix As String
Dim AssyActiveShort As String, AssyActiveShortSub As String
Dim AssyItemSplit As Variant

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swAssy = swDoc

lDocType = swDoc.GetType

' Checks to see if the current document is an Assembly
If lDocType = 0 Then ' No file open
MsgBox "Open or create a new Assembly.", vbCritical, "No AssemblyDocument Open"
Exit Function
ElseIf lDocType = 2 Then ' Assembly file
' All is good
Else ' Part and Drawing file - Not allowed
MsgBox "This macro only works on Assemblies", vbCritical,"Incorrect Document Type"
Exit Function
End If

AssyRoot = swDoc.GetTitle ' Get Assy document name
If AssyRoot Like "*.SLDASM" Then ' Strip off the AssyRootextension
AssyRoot = Left\\\$(AssyRoot, Len(AssyRoot) - 7)
End If

' Breakdown Assy Item location and name
AssyItemSplit = Split(AssyItemLoc, "/")
AssyTotal = UBound(AssyItemSplit)
AssyActive = AssyItemSplit(AssyTotal - 1)
AssyItemName = AssyItemSplit(AssyTotal)

' Create short AssyActive name
If Not AssyActive = AssyRoot Then
Offset = InStrRev(AssyActive, Chr\\\$(45))
AssyActiveShort = Left\\\$(AssyActive, Offset - 1)
Else
AssyActiveShort = AssyRoot
End If

AssyCount = 0 ' Start counter at 0
' Start parsing for Path ID
Do While AssyCount <= AssyTotal
AssyItemPrefix = AssyItemSplit(0) + "@" + AssyRoot
If AssyTotal = 0 Then ' Item located at 1st level Assy
ItemSelectID = AssyItemPrefix
Debug.Print "New String: " + ItemSelectID
Exit Function
ElseIf AssyTotal = 1 And AssyCount = 0 Then ' Item located at 2ndlevel Assy
' Nothing to do already covered
ElseIf AssyTotal > 1 And AssyCount < (AssyTotal - 1) Then 'Item located at 3rd level Assy and on
Offset = InStrRev(AssyItemSplit(AssyCount), Chr\\\$(45))
AssyActiveShortSub = Left\\\$(AssyItemSplit(AssyCount), Offset - 1)
ItemSelectID = ItemSelectID + "/" + AssyItemSplit(AssyCount + 1) +"@" + AssyActiveShortSub
ElseIf AssyTotal = AssyCount Then
ItemSelectID = AssyItemPrefix + ItemSelectID + "/" + AssyItemName
Exit Do
End If
AssyCount = AssyCount + 1
Loop
ItemSelectID = ItemSelectID + "@" + AssyActiveShort
Debug.Print "New String: " + ItemSelectID
End Function

'------- END FUNCTION CODE --------SolidworksApi macros