Catch new feature creation event from SOLIDWORKS API notification

Edit ArticleEdit Article

This example listens for feature added event of the active part document using SOLIDWORKS API.

Once the new feature creation notification is caught, macro displays the message box to the user.

The listener is detached as soon as active part is closed.

Macro Module

Dim swApp As SldWorks.SldWorks
Dim swEventListener As EventListener

Sub main()

    Set swApp = Application.SldWorks
    Set swEventListener = New EventListener
    Dim swPart As SldWorks.PartDoc
    Set swPart = swApp.ActiveDoc
    swEventListener.SetPart swPart
    While swApp.ActiveDoc Is swPart
End Sub

EventListener Class

Dim WithEvents swPart As SldWorks.PartDoc

Private Function swPart_AddItemNotify(ByVal EntityType As Long, ByVal itemName As String) As Long

    If EntityType = swNotifyEntityType_e.swNotifyFeature Then
        MsgBox itemName & " feature is added"
    End If
End Function

Sub SetPart(part As SldWorks.PartDoc)
    Set swPart = part
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