vbasolidworks

SolidWorks API How to use SelectByID2 method


How to create a reference plane by using InsertRefPlane method which requires SelectByID2 method in SolidWorks API in an example?


Solution

  • The goal is to create a reference plane by only selecting three vertices.

    First we need to declare and initialize needed variables/objects. Then we obtain points from the SelectionManager from which we need to extract coordinates. These coordinates will help us since we can specify vertex coordinates to successfuly select and mark them using SelectByID2 method.

    ' Create reference plane with three vertices
    
    ' Prerequisites: Opened part with three selected vertices
    ' Result: Reference plane which lies in the plane formed by three vertices
    
    Dim swApp As SldWorks.SldWorks
    
    Sub main()
    Dim swModel As SldWorks.ModelDoc2
    Dim swPart As SldWorks.PartDoc
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swModelDocExt As SldWorks.ModelDocExtension
    Dim faceObj As SldWorks.Face2
    Dim vertexObj As SldWorks.Vertex
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swModelExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
    
    ' Declare and initialise needed variables
    Dim bool As Boolean
    Dim numSelectedObjs As Integer
    Dim selMark As Long
    Dim selObjectType() As Long
    Dim selObject() As Object
    Dim ptCtr As Integer: ptCtr = 1
    Dim ptObjArr As Variant
    Dim ptVar() As Variant
    Dim i, j As Integer
    selMark = -1
    
    ' Check selection and reallocate needed objects
    numSelectedObjs = swSelMgr.GetSelectedObjectCount2(selMark)
    If (numSelectedObjs <> 3) Then
        MsgBox "You must select three vertices to create a plane"
        End
    ElseIf (numSelectedObjs = 0) Then
        MsgBox "No vertices selected"
        End
    End If
    ReDim Preserve selObjectType(numSelectedObjs - 1)
    ReDim Preserve selObject(numSelectedObjs - 1)
    
    ' Traverse selection and obtain vertex coordinates
    For i = 1 To numSelectedObjs
        selObjectType(i - 1) = swSelMgr.GetSelectedObjectType3(i, selMark)
        If (selObjectType(i - 1) = SwConst.swSelVERTICES) Then 'Identify selection
            Set vertexObj = swSelMgr.GetSelectedObject6(i, selMark)
            Set selObject(i - 1) = vertexObj
            ptObjArr = swSelMgr.GetSelectionPoint2(i, selMark)
            ReDim Preserve ptVar(ptCtr - 1)
            ptVar(ptCtr - 1) = ptObjArr
            ptCtr = ptCtr + 1
        'ElseIf (selObjectType(i - 1) = SwConst.swSelFACES) Then
        'ElseIf (selObjectType(i - 1) = SwConst.swSelEDGES) Then
        End If
    Next
    '
    ' Clear selection and proceed with marking the points for InsertReferencePlane using SelectByID2 method
    swModel.ClearSelection2 (True)
    
    Dim selAppend As Boolean: selAppend = False
    Dim objMark As Long: objMark = 0
    Dim objCallout As Callout
    Dim selOption As swSelectOption_e: selOption = 0
    For i = 1 To numSelectedObjs
        If (i = 1) Then
            selAppend = False
        Else
            selAppend = True
        End If
        objMark = i - 1
        selBool = swModel.Extension.SelectByID2("", "", ptVar(i - 1)(0), ptVar(i - 1)(1), ptVar(i - 1)(2), selAppend, objMark, objCallout, selOption)
        Debug.Print selBool
    Next
    
    ' Reference plane
    ' Creating the reference plane
    Dim refPlaneObj As SldWorks.RefPlane
    Dim firstCon As Long: firstCon = SwConst.swRefPlaneReferenceConstraint_Coincident
    Dim firstConVal As Long: firstConVal = 0
    Dim secondCon As Long: secondCon = SwConst.swRefPlaneReferenceConstraint_Coincident
    Dim secondConVal As Long: secondConVal = 0
    Dim thirdCon As Long: thirdCon = SwConst.swRefPlaneReferenceConstraint_Coincident
    Dim thirdConVal As Long: thirdConVal = 0
    Set refPlaneObj = swModel.FeatureManager.InsertRefPlane(firstCon, firstConVal, _
                                                            secondCon, secondConVal, _
                                                            thirdCon, thirdConVal)
    End Sub