ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
MSMark Soldan02/01/2021

I am having trouble writing my first VBA macro that mates a selected group of parts. To figure out the code, I am starting with a simple example where I am selecting two parts in an assembly and trying to apply a coincident mate to their top planes.

The file names involve are:

AssyA.SLDASM
PartA.SLDPRT
PartB.SLDPRT

I select the two parts in the assembly, either in the feature tree or in the assembly window, and run my macro.  The following code:

FirstSelection = "Top@" & swSelMgr.GetSelectedObjectsComponent2(1).Name & "@" & AssemblyName

SecondSelection = "Top@" & swSelMgr.GetSelectedObjectsComponent2(2).Name & "@" & AssemblyName

    Debug.Print "First: " & FirstSelection

    Debug.Print "Second: " & SecondSelection

swModel.ClearSelection2 (True)

Set swDocExt = swModel.Extension

BoolStatus = swDocExt.SelectByID2(FirstSelection, "PLANE", 0, 0, 0, True, 1, Nothing, swSelectOptionDefault)

    Debug.Print "Selection 1: " & BoolStatus

BoolStatus = swDocExt.SelectByID2(SecondSelection, "PLANE", 0, 0, 0, True, 1, Nothing, wSelectOptionDefault)

    Debug.Print "Selection 2: " & BoolStatus

Set MateFeature = swModel.AddMate5(swMateCOINCIDENT, swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False, False, 0, MateError)

    Debug.Print "MateError: " & MateError

Results in the following output:

First: Top@PartA-1@AssyA
Second: Top@PartB-1@AssyA
Selection 1: False
Selection 2: False
MateError: 4

My SelectByID2 statements are failing and I cannot figure out why.

Thank you for your help.