I've been writing a script to traverse through our assemblies and run a bunch of different subroutines. I've been repeatedly encountering the same frustrating issue during the traverse that I can't seem to wrap my head around and wonder if it's a bug in SolidWorks. I've attached a much simplified version of the code that shows the heart of the issue.
The line of code that fails is "Set swModel = swComp.GetModelDoc2
". For some reason during the traverse some components seem to be loaded incorrectly which makes this line of code fail. When this happens, "swComp.GetSuppression2
" reports -1, which according to the documentation shouldn't be possible. I assume that many if not all other members of swComp would also return incorrect values. Somehow it seems that the swComp variable is loaded incorrectly which makes it impossible to work with.
During debugging, if I browse to the component that is failing in the tree and select it, then it no longer returns a -1 for .GetSuppression2 and I can get the modelDoc for it. This fixes the problem. However If I am to run "bool = modelCustomPropMgr.Get6("AnyPRPName", False, val, resolvedVal, wasResolved, True)
" on an inactive configuration of that assembly, the component then enters into this bad state again making it unusable.
This only seems to happen to assembly files with multiple configurations. We've found at least 100 assemblies in our vault that have this issue in the past few weeks and constantly having to stop the code and click around in the tree (rebuild all configurations of the assembly also works) to fix the problem is not a good solution.
Long story short, it's very frustrating that the component2 is not getting set correctly. Does anyone know what is going on here and be able to help?
Option Explicit Public boolDoNothingTraverseSuccessful As Boolean Public skipRemainingChildren As Boolean Sub topLevelDoNothingTraverse() Dim swModel As ModelDoc2 If swApp Is Nothing Then Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc swModel.ResolveAllLightWeightComponents (False) 'resolve boolDoNothingTraverseSuccessful = True 'iniitialize boolean to true Debug.Print "Starting-------------------------------------------------------------------" Call doNothingTraverse2(swModel.GetActiveConfiguration.GetRootComponent3(True)) 'call do nothing traverse 'give message box of if it was successful or not If boolDoNothingTraverseSuccessful = True Then MsgBox ("Do Nothing Traverse Successfull") Else MsgBox ("Do Nothing Traverse Failed") End If End Sub Sub doNothingTraverse2(swComp As SldWorks.Component2) Dim swModel As ModelDoc2 Dim i As Integer Dim vChildComp As Variant Dim swChildComp As SldWorks.Component2 If swComp.ExcludeFromBOM = True Then GoTo endSub If swComp.GetSuppression2 = swComponentSuppressed Then GoTo endSub 'skip suppressed components If swComp.GetSuppression2 = -1 Then 'no idea why this is happening sometimes. 'Why is this failing? Can't set swmodel from an swcomp that is in this weird state 'report an issue occurred then skip out of this child/sub Debug.Print "Failed state for - " & swComp.Name2 & " - " & swComp.GetPathName boolDoNothingTraverseSuccessful = False GoTo endSub End If Set swModel = swComp.GetModelDoc2 'Now that we've checked that this component isn't in this weird state, we can confidently set the swModel 'Traverse through this model's children If swModel.GetType() = swconst.swDocASSEMBLY Then 'can't traverse if it's not an assembly vChildComp = swComp.GetChildren For i = 0 To UBound(vChildComp) Set swChildComp = vChildComp(i) Call doNothingTraverse2(swChildComp) Next i End If endSub: End Sub