Hide Table of Contents

Suppress or Unsuppress Feature Example (VBA)

This example shows how to suppress a feature in a part.

This example also shows how to perform a string comparison using Visual Basic. The first input parameter, searchStr, allows you to pass in a feature name or a portion of a feature name.

This macro cycles through all the features in the part and selects all features that contain the specified search string and suppresses those features.

'------------------------------------------------------
' Preconditions: Verify that the part to open exists.
'
' Postconditions:
' 1. Opens the part.
' 2. Suppresses the specified feature.
' 3. Examine the FeatureManager design tree and
'    graphics area.
'
' NOTE: Because the part is used elsewhere, do not save
' changes.
'-------------------------------------------------------
Option Explicit
Sub main()
 
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swModelDocExt As SldWorks.ModelDocExtension
    Dim swPart As SldWorks.PartDoc
    Dim swFeature As SldWorks.Feature
    Dim featureName As String
    Dim action As String
    Dim fileName As String
    Dim searchStr As String
    Dim errors As Long
    Dim warnings As Long
    Dim msg As String
    Dim style As Long
    Dim title As String
    Dim status As Boolean    
    Set swApp = CreateObject("SldWorks.Application")    
    ' Attach to the active document
    fileName = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\api\bolt.sldprt"
    Set swModel = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
    searchStr = "chamf"
    action = "Suppress"    'Change to "Unsuppress" to unsuppress supressed features
    Set swModelDocExt = swModel.Extension
    Set swPart = swModel    
    ' Exit if no model is active
    If swModel Is Nothing Then
        Exit Sub
    End If    
    ' Do not allow drawings or assemblies
    If (swModel.GetType <> swDocPART) Then
        ' Define message
        msg = "Macro Only valid for parts"
        ' OK button only
        style = VbMsgBoxStyle.vbOKOnly
        ' Define title
        title = "Error"
        ' Display error message
        Call MsgBox(msg, style, title)
        ' Exit this program
        Exit Sub
    End If    
    ' Get the first feature in part
    Set swFeature = swPart.FirstFeature    
    ' While a valid feature
    Do While Not swFeature Is Nothing
        ' Get the name of the feature
        Let featureName = swFeature.Name
        ' See if the feature name contains search string
        If InStr(1, featureName, searchStr, 1) Then
            ' Select the feature
            status = swModelDocExt.SelectByID2(featureName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
            If (action = "Suppress") Then
                    status = swModel.EditSuppress2() ' Suppress the feature
                ElseIf (action = "Unsuppress") Then '
                    status = swModel.EditUnsuppress2() ' Unsuppress the feature
            End If
        End If
        Set swFeature = swFeature.GetNextFeature() ' Get the next feature
    Loop ' Continue looping until no more features     
End Sub
 
 


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Suppress or Unsuppress Feature Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2018 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.