MACRO LIBRARY

Get the number of entities in selected sketch

Free public access. No account or subscription required.

Files

Associated model(s) Get the number of entities in selected sketch

Code

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSketch As SldWorks.Sketch
Dim swSketchMgr As SldWorks.SketchManager
Dim vSketchSegments As Variant
Dim swSketchSeg As SldWorks.SketchSegment
Dim swSelMgr As SldWorks.SelectionMgr
Dim intSketchEntCount As Integer
Dim i As Integer
    
Sub Main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    
    'make sure one sketch is selected
    If swSelMgr.GetSelectedObjectType3(1, -1) <> 9 Or swSelMgr.GetSelectedObjectCount2(-1) > 1 Then
        swModel.Extension.ShowSmartMessage "Please select a single sketch from the FeatureManager tree.", 3000, False, True
        End
    End If
    
    'Get the Sketch Manager
    Set swSketchMgr = swModel.SketchManager
    
    'Open the selected sketch
    swSketchMgr.InsertSketch False
    
    'Get the sketch
    Set swSketch = swSketchMgr.ActiveSketch
    
    'Get the sketch segments
    intSketchEntCount = 0
    vSketchSegments = swSketch.GetSketchSegments
    For i = 0 To UBound(vSketchSegments)
        Set swSketchSeg = vSketchSegments(i)
        'for some reason this won't recognize True...
        If swSketchSeg.ConstructionGeometry = 0 Then _
            intSketchEntCount = intSketchEntCount + 1
    Next i
    
    'Close the sketch
    swSketchMgr.InsertSketch True
    
    'Display the results
    swModel.Extension.ShowSmartMessage "Sketch entities: " & intSketchEntCount, 3000, False, True
End Sub