MACRO LIBRARY

Sketch contour selection

Free public access. No account or subscription required.

Files

Sketch contour selection Associated model

Code

'Demonstrates how to select sketch contours when creating features.
    
'Preconditions: contour selection.SLDPRT is open.
    
'Run either main() or main2() to see different ways you can work with contours. Note that _
    each method has its advantages and disadvantages. The first method does not allow for _
    selection of contours formed by intersecting sketch entities. It does, however, allow _
    for selection of (some) contours without using IModelDocExtension::SelectByID2.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swFeat As SldWorks.Feature
Dim swSketch As SldWorks.Sketch
Dim swSkCont As SldWorks.SketchContour
Dim swSelData As SldWorks.SelectData
Dim vSkCont As Variant
Dim i As Integer
    
'Select contours using ISketch::GetSketchContours
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    swModel.SelectionManager.EnableContourSelection = True
    swModel.Extension.SelectByID2 "Sketch1", "SKETCHREGION", 0, 0, 0, True, 4, Nothing, 0
    swModel.FeatureManager.FeatureExtrusion2 True, False, False, 0, 0, 0.01, 0.01, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False
    swModel.SelectionManager.EnableContourSelection = False
    swModel.ClearSelection2 True
End Sub
    
'Select contours using ISelectionMgr::EnableContourSelection
Sub main2()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swFeat = swPart.FeatureByName("Sketch1")
    Set swSketch = swFeat.GetSpecificFeature2
    Set swSelData = swSelMgr.CreateSelectData
    swModel.ClearSelection2 True
    vSkCont = swSketch.GetSketchContours
    For i = 0 To UBound(vSkCont)
        Set swSkCont = vSkCont(i)
        
        'include code here that specifies which contour you want
        
        Debug.Print swSkCont.Select2(False, swSelData)  'fails if 2nd arg is Nothing
    Next i
    swModel.FeatureManager.FeatureExtrusion2 True, False, False, 0, 0, 0.01, 0.01, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False
End Sub