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 opens all selected components in the active assembly in their own windows in the same position as they appear in the original SOLIDWORKS assembly.
This macro emulates the Open Part In Position command in SOLIDWORKS toolbar, but allows to open multiple selected components at the same time.
Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Set swAssy = swApp.ActiveDoc
try:
OnErrorGoTocatchIfNot swAssy IsNothingThenDim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = swAssy.SelectionManager
Dim i AsIntegerDim hasCompSel AsBoolean
hasCompSel = FalseFor i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
Dim swComp As SldWorks.Component2
Set swComp = swSelMgr.GetSelectedObjectsComponent3(i, -1)
IfNot swComp IsNothingThen
hasCompSel = TrueDim swCompTransform As SldWorks.MathTransform
Dim swViewTransform As SldWorks.MathTransform
Dim swTotalTransform As SldWorks.MathTransform
Set swCompTransform = swComp.Transform2
Set swViewTransform = swAssy.ActiveView.Orientation3
Set swTotalTransform = swCompTransform.Multiply(swViewTransform)
OpenComponentWithTransforms swComp, swTotalTransform
EndIfNextIfNot hasCompSel Then
Err.Raise vbError, , "No components selected"EndIfElse
Err.Raise vbError, , "Please open assembly"EndIfGoTofinallycatch:
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
finally:
EndSubSub OpenComponentWithTransforms(comp As SldWorks.Component2, transform As SldWorks.MathTransform)
Dim swRefModel As SldWorks.ModelDoc2
Dim swDocSpec As SldWorks.DocumentSpecification
Set swDocSpec = swApp.GetOpenDocSpec(comp.GetPathName())
swDocSpec.Silent = TrueSet swRefModel = swApp.OpenDoc7(swDocSpec)
Dim errs AsLongDim warns AsLongIfNot swRefModel IsNothingThenIfNot swApp.ActiveDoc Is swRefModel ThenSet swRefModel = swApp.ActivateDoc3(swRefModel.GetTitle(), False, swRebuildOnActivation_e.swUserDecision, errs)
If swRefModel IsNothingThen
Err.Raise vbError, , "Cannot activate the referenced document. Error code:" & errs
EndIfEndIfDim swView As SldWorks.ModelView
Set swView = swRefModel.ActiveView
swView.Orientation3 = transform
swRefModel.ViewZoomtofit2
Else
Err.Raise vbError, , "Cannot open the referenced document. Error code:" & swDocSpec.ErrorEndIfEndSub