Macro to change force value and run static simulation (SW BUG?)

Hi Folks,

I am trying to generate a spring curve for a nonlinear spring Running SW Premium 2013 SP3.0. using a VBA macro.

The intent ultimately is to change the value of the force and run the simulation in a loop incrementing the force and collecting a single sensor reading.

Currently it attempts to simply change the value from the original value to a different one and run.

I have set up a simple static study (attached.)  The macro is also attached.

The macro runs and acts like it is doing the correct thing, but the actual simulation does not use new force values nor does the force dialog update in the application window.

<p>Sub main()</p><p>   Dim SwApp As SldWorks.SldWorks</p><p>   Dim COSMOSWORKS As CosmosWorksLib.COSMOSWORKS</p><p>   Dim COSMOSObject As CosmosWorksLib.CWAddinCallBack</p><p>   Dim ActDoc As CosmosWorksLib.CWModelDoc</p><p>   Dim StudyMngr As CosmosWorksLib.CWStudyManager</p><p>   Dim Study As CosmosWorksLib.CWStudy</p><p>   Dim BeamMgr As CosmosWorksLib.CWBeamManager</p><p>   Dim BeamBody As CosmosWorksLib.CWBeamBody</p><p>   Dim SolidMgr As CosmosWorksLib.CWSolidManager</p><p>   Dim SolidComponent As CosmosWorksLib.CWSolidComponent</p><p>   Dim swModelDocExt As SldWorks.ModelDocExtension</p><p>   Dim SolidBody As CosmosWorksLib.CWSolidBody</p><p>   Dim nbrBeamBodies As Long</p><p>   Dim beamBodyType As Long</p><p>   Dim errors As Long, warnings As Long</p><p>   Dim errCode As Long</p><p>   Dim j As Long</p><p>   Dim nbrSolidComponents As Long</p><p>   Dim nbrSolidBodies As Long</p><p>   Dim k As Long</p><p>' Connect to SolidWorks</p><p>    If SwApp Is Nothing Then Set SwApp = Application.SldWorks</p><p>       Set swModel = SwApp.ActiveDoc()</p><p>   Set swModelDocExt = swModel.Extension</p><p>' Get the SolidWorks Simulation object</p><p>    Set COSMOSObject = SwApp.GetAddInObject("SldWorks.Simulation")</p><p>    If COSMOSObject Is Nothing Then ErrorMsg SwApp, "COSMOSObject object not found.", True</p><p>    Set COSMOSWORKS = COSMOSObject.COSMOSWORKS</p><p>    If COSMOSWORKS Is Nothing Then ErrorMsg SwApp, "COSMOSWORKS object not found.", True</p><p>'Open and get the active document</p><p>'SwApp.OpenDoc6 "C:\\Program Files\\SolidWorks Corp\\SolidWorks\\Simulation\\Examples\\Beams\\Beam_Truss.sldprt", swDocPART, swOpenDocOptions_Silent, "", errors, warnings</p><p>    Set ActDoc = COSMOSWORKS.ActiveDoc()</p><p>    If ActDoc Is Nothing Then ErrorMsg SwApp, "No active document.", True</p><p>'Get study Manager</p><p>    Set StudyMngr = ActDoc.StudyManager()</p><p>    If StudyMngr Is Nothing Then ErrorMsg SwApp, "StudyMngr object not there.", True</p><p>    StudyMngr.ActiveStudy = 0</p><p>'Get study</p><p>    Set Study = StudyMngr.GetStudy(0)</p><p>    If Study Is Nothing Then ErrorMsg SwApp, "No study.", True</p><p>Dim LBCMgr          As CosmosWorksLib.CWLoadsAndRestraintsManager</p><p>'get LoadAndRestraintsManager</p><p>    Set LBCMgr = Study.LoadsAndRestraintsManager</p><p>Dim LandRValue As CWLoadsAndRestraints</p><p>Dim LRErrors As Long</p><p></p><p>Dim MyCWForce          As CosmosWorksLib.ICWForce</p><p>    Set MyCWForce = LBCMgr.GetLoadsAndRestraints(1, LRErrors)</p><p>'MsgBox MyCWForce.Name</p><p>Dim B1 As Long</p><p>Dim B2 As Long</p><p>Dim B3 As Long</p><p>Dim D1 As Double</p><p>Dim D2 As Double</p><p>Dim D3 As Double</p><p>Dim value As Integer</p><p>Dim PIDCollection   As New Collection</p><p>    </p><p>Dim var1            As Variant</p><p>Dim var2            As Variant</p><p>Dim pDisp1          As Object</p><p>Dim pDisp2          As Object</p><p>Dim DispArray1      As Variant</p><p>Dim DispArray2      As Variant</p><p>Dim selection1 As String</p><p>Dim oSelect1 As Object</p><p>Dim varArray1 As Variant</p><p>Dim status As Long</p><p>    'Constants</p><p>    selection1 = "112,23,0,0,3,0,0,0,255,254,255,0,0,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,6,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,63,92,0,92,0,72,0,84,0,92,0,72,0,84,0,101,0,99,0,104,0,92,0,80,0,114,0,111,0,106,0,101,0,99,0,116,0,115,0,92,0,80,0,114,0,105,0,110,0,116,0,32,0,84,0,101,0,99,0,104,0,92,0,84,0,101,0,110,0,110,0,105,0,115,0,92,0,83,0,112,0,114,0,105,0,110,0,103,0,45,0,78,0,111,0,110,0,76,0,105,0,110,0,101,0,97,0,114,0,32,0,49,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,9,128,255,254,255,18,83,0,112,0,114,0,105,0,110,0,103,0,45,0,78,0,111,0,110,0,76,0,105,0,110,0,101,0,97,0,114,0,32,0,49,0,2,0,0,184,104,237,81,0"</p><p>    selection1 = selection1 & ",211,110,168,48,0,0,0,0,0,0,0,0,0,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,72,17,95,99,42,0,0,0,68,105,237,81,3,0,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,42,0,0,0,68,105,237,81,0,0,0,0,0,0,0,0,255,255,1,0,19,0,109,111,70,105,108,108,101,116,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,45,0,0,0,73,221,237,81,2,0,0,0,12,128,0,0,5,128,8,0,42,0,0,0,68,105,237,81,1,0,0,0,0,0,0,0,3,128,0,0,5,128,8,0,42,0,0,0,68,105,237,81,6,0,0,0,0,0,0,0,0,0,0,0,0,0"</p><p>    selection1 = selection1 & ",Type=1"</p><p>    StringtoArray selection1, var1</p><p>    Set oSelect1 = swModelDocExt.GetObjectByPersistReference3((var1), status)</p><p>    varArray1 = Array(oSelect1)</p><p>'when not commented out this sucessfully adds a force, but its value does not change either.</p><p>'Set MyCWForce = LBCMgr.AddForce2(swsForceTypeNormal, 0, (varArray1), Nothing, errCode)</p><p>   'If errCode <> 0 Then ErrorMsg SwApp, "No force applied.", True</p><p>    MyCWForce.GetForceComponentValues B1, B2, B3, D1, D2, D3</p><p>    MsgBox B1 & "  " & B2 & "  " & B3 & "  " & D1 & "  " & D2 & "  " & D3</p><p>'correct force reported</p><p>    MyCWForce.ForceBeginEdit</p><p>    MyCWForce.SetForceComponentValues 0, 0, 1, 0, 0, 7</p><p>    value = MyCWForce.ForceEndEdit()</p><p>    MsgBox value</p><p>'No error reported</p><p>    MyCWForce.GetForceComponentValues B1, B2, B3, D1, D2, D3</p><p>    MsgBox B1 & "  " & B2 & "  " & B3 & "  " & D1 & "  " & D2 & "  " & D3</p><p>'force values show to be changed  Values in dialog box in study do not change.</p><p>    errCode = Study.RunAnalysis</p><p>    If errCode <> 0 Then ErrorMsg SwApp, "Analysis failed with error code " & errCode & " - " & ProcErrCode(errCode), False</p><p>'Simulation does not use new values but otherwise runs correctly.</p><p>End Sub</p><p></p><p>Function ErrorMsg(SwApp As Object, Message As String, EndTest As Boolean)</p><p>   SwApp.SendMsgToUser2 Message, 0, 0</p><p>   SwApp.RecordLine "'*** WARNING - General"</p><p>   SwApp.RecordLine "'*** " & Message</p><p>   SwApp.RecordLine ""</p><p>   If EndTest Then</p><p>   End If</p><p>End Function</p><p></p><p> Function StringtoArray(inputSTR As String, varEntity As Variant)</p><p>   Dim PID() As Byte</p><p>   Dim i As Integer</p><p>   varEntity = Split(inputSTR, ",")</p><p>   ReDim PID(UBound(varEntity))</p><p>   For i = 0 To (UBound(varEntity) - 1)</p><p>   PID(i) = varEntity(i)</p><p>   Next i</p><p>   varEntity = PID</p><p>End Function</p><p></p><p></p>
SolidworksApi macros