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 finds all parent components of the selected component instances (Where Used) in the active assembly using SOLIDWORKS API and displays the list for review.
All references can be selected in the form and corresponding component will be highlighted in the Feature Manager Tree.
Configuration
Macro can be configured by changing the constant parameters at the beginning of the macro as shown below:
Const CONSIDER_CONFIG AsBoolean = False'True to only find the component which have the same referenced configuration, False to find by model path onlyConst INCLUDE_SUPPRESSED AsBoolean = False'True to include suppressed components in the search, False to not include
Const CONSIDER_CONFIG AsBoolean = FalseConst INCLUDE_SUPPRESSED AsBoolean = FalseDim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Set swAssy = swApp.ActiveDoc
IfNot swAssy IsNothingThenDim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = swAssy.SelectionManager
Dim swComp As SldWorks.Component2
Set swComp = swSelMgr.GetSelectedObjectsComponent3(1, -1)
IfNot swComp IsNothingThenDim vComps AsVariant
vComps = GetAllComponentInstances(swAssy, swComp, CONSIDER_CONFIG, INCLUDE_SUPPRESSED)
IfNot IsEmpty(vComps) ThenDim vParents AsVariant
vParents = GetParents(vComps)
WhereUsedForm.Components = vParents
Set WhereUsedForm.Assembly = swAssy
WhereUsedForm.Show vbModeless
Else
MsgBox "Failed to find component instances"EndIfElse
MsgBox "Please select component"EndIfElse
MsgBox "Please open assembly"EndIfEndSubFunction GetAllComponentInstances(assy As SldWorks.AssemblyDoc, targComp As SldWorks.Component2, considerConfig AsBoolean, includeSuppressed AsBoolean)
Dim swCompInst() As SldWorks.Component2
Dim isInit AsBooleanDim vComps AsVariant
vComps = assy.GetComponents(False)
Dim i AsIntegerFor i = 0 To UBound(vComps)
Dim swComp As SldWorks.Component2
Set swComp = vComps(i)
If UCase(swComp.GetPathName()) = UCase(targComp.GetPathName()) ThenIfNot considerConfig Or UCase(swComp.ReferencedConfiguration) = UCase(targComp.ReferencedConfiguration) ThenIf includeSuppressed OrFalse = swComp.IsSuppressed() ThenIf isInit ThenReDimPreserve swCompInst(UBound(swCompInst()) + 1)
ElseReDim swCompInst(0)
isInit = TrueEndIfSet swCompInst(UBound(swCompInst())) = swComp
EndIfEndIfEndIfNextIf isInit Then
GetAllComponentInstances = swCompInst
Else
GetAllComponentInstances = Empty
EndIfEndFunctionFunction GetParents(comps AsVariant) AsVariantDim swParents() As SldWorks.Component2
Dim isInit AsVariantDim i AsIntegerFor i = 0 To UBound(comps)
Dim swComp As SldWorks.Component2
Set swComp = comps(i)
Dim swParentComp As SldWorks.Component2
Set swParentComp = swComp.GetParent
Dim addParent AsBoolean
addParent = TrueIfNot isInit Then
isInit = TrueReDim swParents(0)
ElseIfNot Contains(swParents, swParentComp) ThenReDimPreserve swParents(UBound(swParents) + 1)
Else
addParent = FalseEndIfEndIfIf addParent ThenSet swParents(UBound(swParents)) = swParentComp
EndIfNext
GetParents = swParents
EndFunctionFunction Contains(vArr AsVariant, item AsObject) AsBooleanDim i AsIntegerFor i = 0 To UBound(vArr)
If vArr(i) Is item Then
Contains = TrueExitFunctionEndIfNext
Contains = FalseEndFunction
WhereUsedForm
Dim swComps AsVariantPublicAssemblyAs SldWorks.AssemblyDoc
PropertyLet Components(val AsVariant)
swComps = val
Dim i AsIntegerFor i = 0 To UBound(swComps)
Dim swComp As SldWorks.Component2
Set swComp = swComps(i)
Dim name AsStringIf swComp IsNothingThen
name = "<root>"Else
name = swComp.Name2
EndIf
ReferencesList.AddItem name
NextEndPropertyPrivateSub ReferencesList_Change()
Dim swComp As SldWorks.Component2
Set swComp = swComps(ReferencesList.ListIndex)
IfNot swComp IsNothingThen
swComp.Select4 False, Nothing, FalseElseAssembly.ClearSelection2 FalseEndIfEndSub