0

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.

1 Answer 1

0

Right. After all last night and most of today investigating this problem, I believe I've got to the bottom of it and felt it may be useful to record the answer here for anyone else (and my own future reference), in case they might encounter the same problem.

TLDR: It appears the code is/was correct as shown above, but that the solve state of the top-level model file is pretty crucial.

The key difference between my 'test' and 'production' models that was throwing up the problem, was that I had initially solved my 'test' model outside of Load Case Manager, using the Right-click study name => Run command. So there was a 'default' result set sitting there in the 'CWStudy.Results' slot.

In my 'production' model, I hadn't first solved via the same route. I'd just gone straight to Load Case Manager (LCM) and set that running. So in that case, even though there were results sets stored in the Load Case Manager, there was nothing in the 'default' result slot.

It appears that the API's CWStudy.Results property doesn't successfully initialise if there isn't at least a set of solved 'default' study results. So even though there are LCM result sets available, and even though the command LoadCaseManager.LoadResultsOfPrimaryLoadCase(x) apparently loads said results and returns a boolean true assuring that, when you try and access CWStudy.Results it is just 'Nothing'.

Sign up to request clarification or add additional context in comments.

1 Comment

Note: This critical nuance / quirk of SW Simulation API doesn't appear to be documented anywhere, either online, in the API reference manual, or indeed within the knowledgebase of my SW vendors' technical support team. Very frustrating!?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.