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 allows moving the selected components into the new folder in the feature manager tree using SOLIDWORKS API.
Components (or any of their entities) can be selected in the graphics area. For example only face or edge of the component(s) can be selected for macro to work.
#If VBA7 ThenPrivateDeclare PtrSafe Function SendMessage Lib"User32"Alias"SendMessageA" (ByVal hWnd AsLong, ByVal wMsg AsLong, ByVal wParam AsLong, lParam AsAny) AsLong#ElsePrivateDeclareFunction SendMessage Lib"User32"Alias"SendMessageA" (ByVal hWnd AsLong, ByVal wMsg AsLong, ByVal wParam AsLong, lParam AsAny) AsLong#End IfDim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
IfNot swModel IsNothingThen
SelectComponentsFromCurrentSelection swModel
AddSelectedComponentsToNewFolder ""Else
MsgBox "Please open assembly"EndIfEndSubSub SelectComponentsFromCurrentSelection(model As SldWorks.ModelDoc2)
Dim swComps() As SldWorks.Component2
Dim isArrInit AsBoolean
isArrInit = FalseDim i AsIntegerDim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = model.SelectionManager
For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
Dim swComp As SldWorks.Component2
Set swComp = swSelMgr.GetSelectedObjectsComponent4(i, -1)
IfNot swComp IsNothingThenDim unique AsBoolean
unique = FalseIfFalse = isArrInit Then
isArrInit = TrueReDim swComps(0)
unique = TrueElse
unique = Not Contains(swComps, swComp)
IfTrue = unique ThenReDimPreserve swComps(UBound(swComps) + 1)
EndIfEndIfIfTrue = unique ThenSet swComps(UBound(swComps)) = swComp
EndIfEndIfNextIfTrue = isArrInit ThenIf UBound(swComps) + 1 <> model.Extension.MultiSelect2(swComps, False, Nothing) Then
Err.Raise vbError, , "Failed to select components"EndIfEndIfEndSubFunction Contains(vArr AsVariant, item AsObject) AsBooleanDim i AsIntegerFor i = 0 To UBound(vArr)
If vArr(i) Is item Then
Contains = TrueExitFunctionEndIfNext
Contains = FalseEndFunctionSub AddSelectedComponentsToNewFolder(dummy)
Const WM_COMMAND AsLong = &H111
Const CMD_ADD_TO_NEW_FOLDER AsLong = 37922
Dim swFrame As SldWorks.Frame
Set swFrame = swApp.Frame
SendMessage swFrame.GetHWnd(), WM_COMMAND, CMD_ADD_TO_NEW_FOLDER, 0
EndSub
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