Get Normal Contact/Friction Force (VBA)
This example shows how to get the normal contact/friction x, y, z, and
resultant force for the selected entities.
'--------------------------------------------
'
' Preconditions:
' 1. Add the SolidWorks Simulation as an add-in
' (in SolidWorks, click Tools > Add-ins > SolidWorks Simulation).
' 2. Add the SolidWorks Simulation type library as a reference
' (in the IDE, click Tools > References > SolidWorks
' Simulation version type library).
' 3. Open <SolidWorks_install_dir>\Simulation\Examples\Contact\slider_locker_mechanism.sldasm.
' 4. Open the Immediate window.
' 5. Select the Ready Simulation study tab.
' 6. Right-click Ready in the Simulation study tree and
' select Run. The study may take several minutes to solve.
' Click Yes if the message box informing you
' of excessive displacements is displayed.
' 7. Run the macro.
'
' Postconditions: The x, y, z, and resultant
' force are printed to the Immediate window.
'
' NOTE: Because this assembly document is used by
' SolidWorks Simulation tutorial, do not save any
' changes when closing the document.
'
'-------------------------------
Option Explicit
Sub main()
Dim SwApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim COSMOSObject As CosmosWorksLib.CwAddincallback
Dim COSMOSWORKS As Object
Dim ActDoc As CosmosWorksLib.CWModelDoc
Dim StudyMngr As CosmosWorksLib.CWStudyManager
Dim Study As CosmosWorksLib.CWStudy
Dim Results As CosmosWorksLib.CWResults
Dim errors As Long
Dim selection1 As String
Dim selection2 As String
Dim sel1 As Variant
Dim sel2 As Variant
Dim selObject As Object
Dim state As Long
Dim arraySelections As Variant
Dim Forces As Variant
Dim i As Long
' Connect to SolidWorks
If SwApp Is Nothing Then Set SwApp = Application.SldWorks
Set swModel = SwApp.ActiveDoc
Set swModelDocExt = swModel.Extension
' Get the SolidWorks Simulation object
Set COSMOSObject = SwApp.GetAddInObject("CosmosWorks.CosmosWorks")
If COSMOSObject Is Nothing Then ErrorMsg SwApp, "COSMOSObject object not found.", True
Set COSMOSWORKS = COSMOSObject.COSMOSWORKS
If COSMOSWORKS Is Nothing Then ErrorMsg SwApp, "COSMOSWORKS object not found.", True
' Get the active document
Set ActDoc = COSMOSWORKS.ActiveDoc()
If ActDoc Is Nothing Then ErrorMsg SwApp, "No active document.", True
' Get the Ready study
Set StudyMngr = ActDoc.StudyManager()
If StudyMngr Is Nothing Then ErrorMsg SwApp, "StudyMngr object not there.", True
StudyMngr.ActiveStudy = 0
Set Study = StudyMngr.GetStudy(0)
If Study Is Nothing Then ErrorMsg SwApp, "Study not created.", True
' Get the results
Debug.Print "Active study: " & Study.Name
Set Results = Study.Results
If Results Is Nothing Then ErrorMsg SwApp, "No Results object.", True
' Get the PIDs of the faces to select
selection1 = "48,17,0,0,3,0,0,0,255,254,255,32,76,0,111,0,99,0,107,0,101,0,114,0,45,0,49,0,64,0,115,0,108,0,105,0,100,0,101,0,114,0,95,0,108,0,111,0,99,0,107,0,101,0,114,0,95,0,109,0,101,0,99,0,104,0,97,0,110,0,105,0,115,0,109,0,4,0,0,0,16,0,0,0,1,0,0,0,1,0,0,0,11,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,57,67,0,58,0,92,0,67,0,79,0,83,0,77,0,79,0,83,0,68,0,111,0,99,0,115,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0,115,0,95,0,102,0,111,0,114,0,67,0,117,0,115,0,116,0,111,0,109,0,101,0,114,0,115,0,92,0,67,0,111,0,110,0,116,0,97,0,99,0,116,0,92,0,76,0,111,0,99,0,107,0,101,0,11"
selection1 = selection1 & "4,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,9,128,255,254,255,6,76,0,111,0,99,0,107,0,101,0,114,0,2,0,0,9,255,26,58,0,166,20,28,65,1,0,0,0,0,0,0,0,3,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,166,20,28,65,33,0,0,0,63,0,27,58,12,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,33,0,0,0,63,0,27,58,0,0,0,0,255,255,255,255,3,128,0,0,5,128,8,0,33,0,0,0,63,0,27,58,19,0,0,0,12,128,0,0,5,128,8,0,33,0,0,0,63,0,27,58,1,0,0,0,255,255,255,255,3,128,0,0,5,128,8,0,33,0,0,0,63,0,27,58,13,0,0,0,0,0,0,0,0,0,0,0,0,0"
StringtoArray selection1, sel1
Set selObject1 = swModelDocExt.GetObjectByPersistReference3((sel1), state)
selection2 = "48,17,0,0,3,0,0,0,255,254,255,32,83,0,108,0,105,0,100,0,101,0,114,0,45,0,49,0,64,0,115,0,108,0,105,0,100,0,101,0,114,0,95,0,108,0,111,0,99,0,107,0,101,0,114,0,95,0,109,0,101,0,99,0,104,0,97,0,110,0,105,0,115,0,109,0,4,0,0,0,16,0,0,0,1,0,0,0,1,0,0,0,16,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,57,67,0,58,0,92,0,67,0,79,0,83,0,77,0,79,0,83,0,68,0,111,0,99,0,115,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0,115,0,95,0,102,0,111,0,114,0,67,0,117,0,115,0,116,0,111,0,109,0,101,0,114,0,115,0,92,0,67,0,111,0,110,0,116,0,97,0,99,0,116,0,92,0,83,0,108,0,105,0,100,0,101,0,"
selection2 = selection2 & "114,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,9,128,255,254,255,6,83,0,108,0,105,0,100,0,101,0,114,0,2,0,0,90,0,27,58,0,9,21,28,65,1,0,0,0,0,0,0,0,2,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,9,21,28,65,16,0,0,0,131,0,27,58,2,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,16,0,0,0,131,0,27,58,0,0,0,0,255,255,255,255,3,128,0,0,5,128,8,0,16,0,0,0,131,0,27,58,9,0,0,0,12,128,0,0,5,128,8,0,16,0,0,0,131,0,27,58,1,0,0,0,255,255,255,255,3,128,0,0,5,128,8,0,16,0,0,0,131,0,27,58,3,0,0,0,0,0,0,0,0,0,0,0,0,0"
StringtoArray selection2, sel2
Set selObject2 = swModelDocExt.GetObjectByPersistReference3((sel2), state)
arraySelections = Array(selObject1, selObject2)
' Get the normal contact/friction force for the selected entities
Forces = Results.GetContactForcesAndFriction(0, Nothing, (arraySelections), 0, swsUnitSI, errors)
Debug.Print (" x, y, z, and Resultant:")
For i = 0 To UBound(Forces)
Debug.Print (" " & Forces(i))
Next
End Sub
' Error function
Function ErrorMsg(SwApp As Object, Message As String, EndTest As Boolean)
SwApp.SendMsgToUser2 Message, 0, 0
SwApp.RecordLine "'*** WARNING - General"
SwApp.RecordLine "'*** " & Message
SwApp.RecordLine ""
If EndTest Then
End If
End Function
' Parse string into an array
Function StringtoArray(inputSTR As String, varEntity As Variant)
Dim PID() As Byte
Dim i As Integer
varEntity = Split(inputSTR, ",")
ReDim PID(UBound(varEntity))
For i = 0 To (UBound(varEntity) - 1)
PID(i) = varEntity(i)
Next i
varEntity = PID
End Function