Files
Toggle component lightweight / suppressed state
Code
Module: mMain
'Demonstrates how to load suppressed and lightweight models into memory in order _
to access their underlying IModelDoc2 pointers and then return them to their _
original state afterward.
'Preconditions: assembly is open with lightweight and/or suppressed components.
'Results: Path of each underlying component is printed to immediate window using _
IModelDoc2::GetPathName.
'ModifyComponentState arguments:
'swComp - the component for which the state will be modified, if necessary
'blnLoad - true to unsuppress or resolve the component if it isn't already _
resolved, false to re-suppress or make lightweight the component if it _
was previously.
'intState - out / ByRef argument that will record the original state if _
blnLoad = true and will be used to reset the original state if blnLoad = false.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim vComps As Variant
Dim i As Integer, intState As Integer
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
vComps = swAssy.GetComponents(False)
If IsEmpty(vComps) Then Exit Sub
For i = 0 To UBound(vComps)
Set swComp = vComps(i)
Debug.Print "Processing: " & swComp.Name2
If ModifyComponentState(swComp, True, intState) = False Then Stop
Set swCompModel = swComp.GetModelDoc2
Debug.Print vbTab & swCompModel.GetPathName
If ModifyComponentState(swComp, False, intState) = False Then Stop
Next i
End Sub
Function ModifyComponentState(swComp As SldWorks.Component2, _
blnLoad As Boolean, ByRef intState As Integer)
On Error GoTo ErrHandler
If blnLoad Then
Debug.Print vbTab & "Ensuring component is resolved"
intState = swComp.GetSuppression
swComp.SetSuppression2 swComponentResolved
Else
Debug.Print vbTab & "Returning component to original suppression state"
swComp.SetSuppression2 intState
End If
ModifyComponentState = True
Exit Function
ErrHandler:
MsgBox "ERROR: Failed to get or set component state" & _
vbCrLf & "Error number: " & Err.Number _
& vbCrLf & "Description: " & Err.Description
ModifyComponentState = False
End Function