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 macro counts all unique components selected in the assembly using SOLIDWORKS API. Components can be either selected in the features manager tree or in the graphics area.
Macro will also count component if only entity of the component is selected (e.g. face or edge) using ISelectionMgr SOLIDWORKS API Interface..
Dim swApp As SldWorks.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Sub main()
Set swApp = Application.SldWorks
Set swAssy = swApp.ActiveDoc
IfNot swAssy IsNothingThenDim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = swAssy.SelectionManager
Dim swCompsColl As Collection
Set swCompsColl = New Collection
Dim i AsIntegerFor i = 0 To swSelMgr.GetSelectedObjectCount2(-1)
Dim swComp As SldWorks.Component2
Set swComp = swSelMgr.GetSelectedObjectsComponent2(i)
IfNot swComp IsNothingThenIfNot Contains(swCompsColl, swComp) Then'get only unique components
swCompsColl.Add swComp
EndIfEndIfNextDim swFrame As SldWorks.Frame
Set swFrame = swApp.Frame
swFrame.SetStatusBarText "Selected " & swCompsColl.Count() & " component(s)"Else
MsgBox "Please open assembly"EndIfEndSubFunction 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