Suppress or Unsuppress Feature Example (VBA)
This example shows how to pass in two strings
and either suppress or unsuppress features in a part. The first input
parameter, SearchStr, allows you to pass in a feature name or a portion
of a feature name. This routine cycles through all the features in the
part and selects all features that contain the specified search string.
For example, if you pass the string "Hole", this
routine selects any feature with a name containing the word "Hole".
The second parameter passed, Action, determines whether the selected features
are suppressed or unsuppressed.
This example shows how to:
Cycle through features
in a part
Get a feature name
Suppress and unsuppress
features
Perform a string comparison
using Visual Basic
'---------------------------------------------
Sub FeatureSuppression(SearchStr,
Action)
' Variable used to hold the SldWorks object
Dim swApp As Object
' Variable used to hold the ModelDoc object
Dim Model As Object
' Variable used to hold the current Feature object
Dim feat As Object
Dim featureName As String
' These definitions are consistent with type names
Const swDocPART = 1
' Constant enumerators
Const swDocASSEMBLY = 2
Const swDocDRAWING = 3
Set swApp = CreateObject("SldWorks.Application")
' Attach to the active document
Set Model = swApp.ActiveDoc
' Exit if no model is active
If Model Is Nothing Then
Exit Sub
End If
' Do not allow drawings or assemblies
If (Model.GetType
<> swDocPART) Then
' Define message
Msg = "Only
Allowed on Parts"
' OK Button only
Style = vbOKOnly
' Define title
Title = "Error"
' Display error message
Call MsgBox(Msg,
Style, Title)
' Exit this program
Exit Sub
End If
' Get the 1st feature in part
Set feat = Model.FirstFeature
' While we have a valid feature
Do While Not feat Is Nothing
' Get the name of the feature
Let featureName = feat.Name
' See if the feature name contains our search string
If InStr(1, featureName, SearchStr,
1) Then
' Select the feature
res = Model.SelectByID(featureName, "BODYFEATURE",
0, 0, 0)
If (Action = "Suppress")
Then ' User chose to suppress
res = Model.EditSuppress() ' Suppress the
feature
ElseIf (Action =
"Unsuppress") Then ' User chose to unsuppress
res = Model.EditUnSuppress() ' Unsuppress
the feature
End If
End If
Set feat = feat.GetNextFeature()
' Get the next feature
Loop ' Continue until
no more features exist
End Sub