vbasolidworkssolidworksapi

Why is Solidworks API method SelectByID2 not selecting assembly component features?


I am rewriting a macro for Solidworks in VBA so that it is easier to read as well as adding additional features and error handling. The rewritten macro works as intended except that for some reason the SelectByID2 method is not functioning like it was in the original macro I wrote. I looked into this previously posted question:

SolidWorks API How to use SelectByID2 method

but it did not provide me an answer to my specific use case. After trial and error I have determined that instead of selecting the assembly component features the macro is selecting the top level assembly features and I don't understand why.

I assumed the issue was in my new code so I recorded a macro making the same selections that I want the new macro to make assuming the string I was passing for the named selection was incorrect. The recorded macro code looks like this:


Dim Part As ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Top Plane@3660623_DSBC-40-100-D3-PPVA-N3_BODY-1@3660623_DSBC-40-100-D3-PPVA-N3", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
End Sub

What this should do is first select the top plane of the active assembly then select the top plane of the specified assembly component with the intention of creating a mate with the previous 2 selections. The first selection works but when it runs the second selection it returns an empty selection list. I checked to make sure I had args for appending selections set to True and this did not yield a different result. So then on a whim I thought to see what would happen if I made the second selection a different plane than the first selection and the result I get is that instead of selecting the front plane of the component or returning an empty selection like before it selects the front plane of the assembly document appended to the previous selection rather than the specified feature of the assembly component.

I have heard that SelectByID2 is not always the best method for selecting features so I tried selecting the component features through the Feature interface and this did not yield a different result. For reference this is the code that I wrote trying to access the selections through the Feature object.

'    Set swComp = New Collection
'    For Each swComp In FeatureColl
'        For Each Feature In Component
'            ' Cast the feature to an entity
'            Set swEntity = Feature
'            Set swComp = swEntity.GetComponent
'
'            If swComp.Name2 Like PartNameArg Then
'                If Feature.Name = "Top Plane" Then
'                    Bool = Feature.Select4(True, Data)
'                End If
'            End If
'        Next
'    Next
'
'    For Each Feature In AssemblyFeatures
'        Set swEntity = Feature
'        Set swComp = swEntity.GetComponent
'
'        If Feature.Name = "Top Plane" Then
'            Bool = Feature.Select4(True, Data)
'            Debug.Print Feature.Name & " | " & Bool
'        End If
'    Next

So I'm back to looking at the SelectByID2 method and this morning I recorded another macro like the one I recorded before and inexplicably that recording worked for a moment. The code in the macro I recorded this morning is exactly the same as the code from the earlier recording.

Unsure of what was different I made some edits to my new macro's code (shown below) and this failed to make the selections still.

    swModel.ClearSelection2 True
    SelectRightPlane = swModel.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
    SelectRightPlane = swModel.Extension.SelectByID2("Right Plane@" & PartSelect, "PLANE", 0, 0, 0, True, 0, Nothing, 0)

So I went back to my recorded macro that had briefly worked this morning to see if it still worked and again it is not making the correct selections. At this point I'm at a loss for what I am doing wrong so any help with this would be greatly appreciated.


Solution

  • I made up the code, which only works if you select planes, otherwise you will have to modify it for different types of selection.

    
    Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swDoc As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swEntity As SldWorks.Entity
    Dim swSelFeatures() As SldWorks.Feature
    Dim swSelFtrString() As String
    
    Dim i As Integer
    Dim numSelObjs As Integer
    Dim selObjs() As Object
    Dim swSelMark As Long
    Dim selAppend As Boolean
    Dim objMark As Long: objMark = 0
    Dim objCallout As Callout
    Dim selOption As Long: selOption = 0
    Dim selBool As Boolean
    Dim planeEnum As swSelectType_e: planeEnum = SwConst.swSelectType_e.swSelDATUMPLANES
    swSelMark = -1
    
    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
    Set swSelMgr = swDoc.SelectionManager
    
    
    'Allocating memory
    numSelObjs = swSelMgr.GetSelectedObjectCount2(swSelMark)
    
    'Obtain names of selection to use by SelectByID2 method
    If (numSelObjs = 0) Then
        MsgBox "No features selected, stopping macro"
        End
    Else
        ReDim Preserve selObjs(numSelObjs - 1)
        ReDim Preserve swSelFeatures(numSelObjs - 1)
        ReDim Preserve swSelFtrString(numSelObjs - 1)
        For i = 1 To numSelObjs
            Set selObjs(i - 1) = swSelMgr.GetSelectedObject6(i, swSelMark)
            Set swSelFeatures(i - 1) = selObjs(i - 1)
            swSelFtrString(i - 1) = swSelFeatures(i - 1).GetNameForSelection(CInt(planeEnum))
        Next
    End If
    
    'Selecting features with SelectByID2 method
    swDoc.ClearSelection2 (True)
    For i = 1 To numSelObjs
        If (i = 1) Then
            selAppend = False
        Else
            selAppend = True
        End If
        selBool = swDoc.Extension.SelectByID2(swSelFtrString(i - 1), "PLANE", 0, 0, 0, selAppend, objMark, objCallout, selOption)
        
        If (selBool) Then
            Debug.Print ("Feature " + swSelFtrString(i - 1) + " selected")
        End If
    Next
    
    End Sub