This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
This VBA macro selects all features in the active model (part, assembly or drawing) using SOLIDWORKS API. For drawings and assemblies features in the children components are also selected.
Use this macro in conjunction with Get Features Type Name to get the required feature type name for filtering.
Configuration
Modify the constants in the beginning of the macro
Const APPEND_SELECTION AsBoolean = False'True to append selection False to clear existing selectionConst TYPE_NAME AsString = ""'Refer 'Get Features Type Name' macro to get the type name from the feature
This macro can be useful with other macros which require features to be preselected. It can be also used with SOLIDWORKS batch operations (such as delete or suppress).
Const APPEND_SELECTION AsBoolean = FalseConst TYPE_NAME AsString = "3DProfileFeature"'3DSketchDim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
IfNot swModel IsNothingThenDim vFeats AsVariant
vFeats = GetAllFeaturesByType(swModel, TYPE_NAME)
swModel.Extension.MultiSelect2 vFeats, False, Nothing'If swModel.Extension.MultiSelect2(vFeats, False, Nothing) = UBound(vFeats) + 1 Then'Err.Raise vbError, "", "Failed to select features"'End IfElse
MsgBox "Please open model"EndIfEndSubFunction GetAllFeaturesByType(model As SldWorks.ModelDoc2, typeName AsString) AsVariantDim swFeatMgr As SldWorks.FeatureManager
Set swFeatMgr = model.FeatureManager
Dim swRootFeatNode As SldWorks.TreeControlItem
Set swRootFeatNode = swFeatMgr.GetFeatureTreeRootItem2(swFeatMgrPane_e.swFeatMgrPaneBottom)
IfNot swRootFeatNode IsNothingThenDim swFeatsColl As Collection
Set swFeatsColl = New Collection
TraverseFeatureNode swRootFeatNode, typeName, swFeatsColl
Else
Err.Raise vbError, "", "Failed to get the root node"EndIfIf swFeatsColl.Count() > 0 ThenDim swFeats() As SldWorks.Feature
ReDim swFeats(swFeatsColl.Count() - 1)
Dim i AsIntegerFor i = 0 To UBound(swFeats)
Set swFeats(i) = swFeatsColl.item(i + 1)
Next
GetAllFeaturesByType = swFeats
Else
GetAllFeaturesByType = Empty
EndIfEndFunctionSub TraverseFeatureNode(featNode As SldWorks.TreeControlItem, typeName AsString, feats As Collection)
If featNode.ObjectType = swTreeControlItemType_e.swFeatureManagerItem_Feature ThenDim swFeat As SldWorks.Feature
Set swFeat = featNode.Object
If swFeat.GetTypeName2() = "HistoryFolder"ThenExitSubEndIfIf LCase(swFeat.GetTypeName2) = LCase(typeName) ThenIfNot Contains(feats, swFeat) Then'swFeat.Select2 True, -1
feats.Add swFeat
EndIfEndIfEndIfDim swChildFeatNode As SldWorks.TreeControlItem
Set swChildFeatNode = featNode.GetFirstChild()
WhileNot swChildFeatNode IsNothing
TraverseFeatureNode swChildFeatNode, typeName, feats
Set swChildFeatNode = swChildFeatNode.GetNext
Wend
EndSubFunction Contains(coll As Collection, item AsObject) AsBooleanDim i AsIntegerFor i = 1 To coll.CountIf coll.item(i) Is item Then
Contains = TrueExitFunctionEndIfNext
Contains = FalseEndFunction
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more