In Solidworks simulation I'm having trouble making a VBA macro to automate some operations on Load Case Manager results sets. A part of the code fails at an odd point for an unknown reason, and I can't really determine why because its com code that doesn't expose much of the object info or error feedback.
In this code, all executes fine up until the line:
bSuccess = LoadCaseManager.LoadResultsOfPrimaryLoadCase(PrimaryCases(0))
This returns a boolean true, indicating that that method has executed successfully. Apparently all well & good.
Dim swApp As Object
Sub main()
Dim swModel As SldWorks.ModelDoc2
Dim COSMOSWORKS As CosmosWorksLib.COSMOSWORKS
Dim CWAddinCallBack As CosmosWorksLib.CWAddinCallBack
Dim ActDoc As CosmosWorksLib.CWModelDoc
Dim StudyMngr As CosmosWorksLib.CWStudyManager
Dim Study As CosmosWorksLib.CWStudy
Dim LoadCaseManager As CosmosWorksLib.CWLoadCaseManager
Dim CWResult As CosmosWorksLib.CWResults
Dim activeStudyIndex As Integer
Dim errorCode As Long
Dim bSuccess As Boolean
Dim PrimaryCases As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "No active model."
Exit Sub
End If
Set CWAddinCallBack = swApp.GetAddInObject("SldWorks.Simulation")
Set COSMOSWORKS = CWAddinCallBack.COSMOSWORKS
Set ActDoc = COSMOSWORKS.ActiveDoc()
Set StudyMngr = ActDoc.StudyManager()
activeStudyIndex = StudyMngr.ActiveStudy
Set Study = StudyMngr.GetStudy(activeStudyIndex)
Set LoadCaseManager = Study.LoadCaseManager
If LoadCaseManager Is Nothing Then Stop
bSuccess = LoadCaseManager.OpenLoadCaseManager
bSuccess = LoadCaseManager.ShowResultsViewTab
PrimaryCases = LoadCaseManager.GetAllPrimaryLoadCaseNames
bSuccess = False
bSuccess = LoadCaseManager.LoadResultsOfPrimaryLoadCase(PrimaryCases(0))
If bSuccess = False Then Stop
Debug.Print Study.Name
Set CWResult = Study.Results
If CWResult Is Nothing Then Stop
Debug.Print "Number of plots for these results: " & CWResult.GetPlotCount
End Sub
The next operational line unexpectedly returns "nothing":
Set CWResult = Study.Results
Its odd because that line works fine when loading 'default' results outside of Load Case Manager, in my large 'production' FE model and in the much simpler model I made for testing. And it also works fine when returning Load Case Manager primary load case results in my simple test model.
It just seems to be failing for some unknown reason in my 'production' FE model, that has lots of pre-configured primary load combinations. I can't spot any significant differences between my 'test' and 'production' models, so am a bit flummoxed as to why this is happening.
I suspect this is quite a long-shot request, but I'm not quite sure where to go with it yet seeing as the com issue means its pretty hard to debug. So all and any input advice will be much appreciated.