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 remove all mates from the active assembly and fixes all the top level components.
Macro allows to configure the actions to perform on the assembly by changing the values of the constants
Const FIX_COMPONENTS AsBoolean = True'True to fix components, False to keep components as isConst REMOVE_MATES AsBoolean = True'True to remove mates, False to keep mates
Macro will fix all top level components, excluding all components which are instances of the pattern
This allows to significantly improve the performance of the assembly.
Const FIX_COMPONENTS AsBoolean = TrueConst REMOVE_MATES AsBoolean = TrueDim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
IfNot swModel IsNothingThenIf swModel.GetType() <> swDocumentTypes_e.swDocASSEMBLY Then
Err.Raise vbError, "Only assembly document is supported"EndIfDim swAssy As SldWorks.AssemblyDoc
Set swAssy = swModel
If REMOVE_MATES ThenDim vMates AsVariant
vMates = GetAllMates(swAssy)
IfNot IsEmpty(vMates) ThenIf swModel.Extension.MultiSelect2(vMates, False, Nothing) = UBound(vMates) + 1 ThenIfFalse = swModel.Extension.DeleteSelection2(swDeleteSelectionOptions_e.swDelete_Absorbed) Then
Err.Raise vbError, "", "Failed to delete mates"EndIfElse
Err.Raise vbError, "", "Failed to select mates for deletion"EndIfEndIfEndIfIf FIX_COMPONENTS ThenDim vComps AsVariant
vComps = GetAllComponents(swAssy)
IfNot IsEmpty(vComps) ThenIf swAssy.Extension.MultiSelect2(vComps, False, Nothing) = UBound(vComps) + 1 Then
swAssy.FixComponent
Else
Err.Raise vbError, "", "Failed to select components"EndIfEndIfEndIfElse
Err.Raise vbError, "", "Please open assemby document"EndIfEndSubFunction GetAllMates(assm As SldWorks.AssemblyDoc) AsVariantDim swMates() As SldWorks.Feature
Dim isInit AsBoolean
isInit = FalseDim swModel As SldWorks.ModelDoc2
Set swModel = assm
Dim swMateGroupFeat As SldWorks.Feature
Dim featIndex AsInteger
featIndex = 0
DoSet swMateGroupFeat = swModel.FeatureByPositionReverse(featIndex)
featIndex = featIndex + 1
LoopWhile swMateGroupFeat.GetTypeName2() <> "MateGroup"Dim swMateFeat As SldWorks.Feature
Set swMateFeat = swMateGroupFeat.GetFirstSubFeature
WhileNot swMateFeat IsNothingIfTypeOf swMateFeat.GetSpecificFeature2() Is SldWorks.Mate2 ThenIf isInit ThenReDimPreserve swMates(UBound(swMates) + 1)
ElseReDim swMates(0)
isInit = TrueEndIfSet swMates(UBound(swMates)) = swMateFeat
EndIfSet swMateFeat = swMateFeat.GetNextSubFeature
Wend
If isInit Then
GetAllMates = swMates
Else
GetAllMates = Empty
EndIfEndFunctionFunction GetAllComponents(assm As SldWorks.AssemblyDoc) AsVariantDim swComps() As SldWorks.Component2
Dim isInit AsBoolean
isInit = FalseDim vComps AsVariant
vComps = assm.GetComponents(True)
Dim i AsIntegerFor i = 0 To UBound(vComps)
Dim swComp As SldWorks.Component2
Set swComp = vComps(i)
IfFalse = swComp.IsPatternInstance ThenIfNot isInit Then
isInit = TrueReDim swComps(0)
ElseReDimPreserve swComps(UBound(swComps) + 1)
EndIfSet swComps(UBound(swComps)) = swComp
EndIfNextIf isInit Then
GetAllComponents = swComps
Else
GetAllComponents = Empty
EndIfEndFunction
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