MACRO LIBRARY

Class example using SolidWorks object

Free public access. No account or subscription required.

Files

Class example using SolidWorks object

Code

Modules: creating_a_class1

'This class determines whether a feature with a particular string in its name exists.
'Preconditions: Part is open
'Postconditions: True if text is found in the name of any feature, false if not
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeatureSearch As clsFeatureSearch
Dim strName As String
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swFeatureSearch = New clsFeatureSearch
    
    'or we could make swModel a public variable and remove "model" as an argument
    Debug.Print swFeatureSearch.Search(swModel, "Fillet")
    
End Sub
    
'If we make Search within clsFeatureSearch private, the method or data member is not found.
    
'swFeatureSearch can be declared as "New FeatureSearch" and then no Set statement in the _
    sub procedure is needed. See API Help for "New". Otherwise, when you set _
    swFeatureSearch, you must set it equal to a New FeatureSearch.
    
'From API Help on New statement: Keyword that enables implicit creation of an object. _
    If you use New when declaring the object variable, a new instance of the object is _
    created on first reference to it, so you don't have to use the Set statement to _
    assign the object reference.

Class Modules: clsFeatureSearch

Dim swFeat As SldWorks.Feature
    
Public Function Search(model As SldWorks.ModelDoc2, text As String) As Boolean
    Set swFeat = model.FirstFeature
    
    While Not swFeat Is Nothing
        If InStr(1, swFeat.Name, text) <> 0 Then Search = True
        Set swFeat = swFeat.GetNextFeature
    Wend
End Function