How to find all sketch based features using a CATScript

One of the tutorials in the expanded edition of VB Scripting for CATIA V5 teaches you how to write a macro to count the number of sketched based shapes contained within a part file. I’ll share some of the script here. Sketch based features are, you guessed it, features which are constructed from a sketch (as opposed to 3D geometry like planes, lines, and points).

We begin with Sub CATMain as usual:

Sub CATMain()

In order to run this macro a single catpart must be the active document, so we’ll insert some error handling into our code to make sure the active document is in fact a part file:

On Error Resume Next

Dim partDoc1 As PartDocument
Set partDoc1 = CATIA.ActiveDocument

If Err.Number <> 0 Then

    MsgBox “Active document is not a part document!“”

End If

Next, we need to point to the first geometrical set contained within our part document (for a more through explanation please consider purchasing the ebook manual).
Dim body1 As Body
Set body1 = partDoc1.Part.Bodies.Item(1)

Dim shapes1 As Shapes
Set shapes1 = body1.Shapes

shapecount = shapes1.count

Dim shape1 As Shape

We want to count the number of sketch based features so we use SketchBasedShape and loop through all the shapes. If the shape is sketch based it will add it to the count.

Dim sketchShape As SketchBasedShape
Dim count1 As Integer
count1 = 0

For i = 1 To shapecount

    Set sketchShape = shapes1.Item(i)
    If Err.Number = 0 Then
        count1 = count1 + 1
    End If


Finally, we’ll display a message box with the number of sketch based shapes.

MsgBox “Number of sketch based shapes is: ” & count1

End Sub

Go back to macro tutorials for CATIA.