Select all sketch elements using SOLIDWORKS API

Edit ArticleEdit Article

Selected sketch elements in the active sketch
Selected sketch elements in the active sketch

This example demonstrates how to select all sketch segments and sketch points in the active sketch using the direct ::Select method in SOLIDWORKS API.

Dim swApp As SldWorks.SldWorks

Sub main()

    Set swApp = Application.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Set swModel = swApp.ActiveDoc
    If Not swModel Is Nothing Then
        Dim swSketch As SldWorks.sketch
        Set swSketch = swModel.SketchManager.ActiveSketch
        If Not swSketch Is Nothing Then
            swModel.ClearSelection2 True
            SelectAllSketchSegments swSketch
            SelectAllSketchPoints swSketch
            MsgBox "Please open sketch"
        End If
        MsgBox "Please open part or assembly"
    End If
End Sub

Sub SelectAllSketchSegments(sketch As SldWorks.sketch)
    Dim vSegs As Variant
    vSegs = sketch.GetSketchSegments
    Dim i As Integer
    For i = 0 To UBound(vSegs)
        Dim swSkSeg As SldWorks.SketchSegment
        Set swSkSeg = vSegs(i)
        swSkSeg.Select4 True, Nothing
End Sub

Sub SelectAllSketchPoints(sketch As SldWorks.sketch)
    Dim vPoints As Variant
    vPoints = sketch.GetSketchPoints2
    Dim i As Integer
    For i = 0 To UBound(vPoints)
        Dim swSkPt As SldWorks.SketchPoint
        Set swSkPt = vPoints(i)
        swSkPt.Select4 True, Nothing
End Sub


All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.

Product of Xarial Product of Xarial