Hide Table of Contents

Add Gravity Load Example (VBA)

This example shows how to add a gravity load to a static study.

' Preconditions:
' 1. Add the SOLIDWORKS Simulation as an add-in (in SOLIDWORKS, click
'    Tools > Add-ins > SOLIDWORKS Simulation > OK).
' 2. Add the SOLIDWORKS Simulation type library as a reference (in the IDE,
'    click Tools > References > SOLIDWORKS Simulation version type library).
' 3. Verify that the specified model document exists.
' 4. Open the Immediate window.
' Postconditions:
' 1. Opens the document.
' 2. Activates Ready - 8 plies.
' 3. Deletes two pressure loads from Ready - 8 plies.
' 4. Adds the Gravity-1 load to Ready - 8 plies.
' 5. Prints the Gravity-1 properties to the Immediate window.
' 6. Meshes the part.
' 7. Sets the solver type.
' 8. Analyzes Ready - 8 plies.
' 9. Inspect the Immediate window, the Simulation study tree,
'    and the graphics area.
' NOTE: Because this part document is used elsewhere, do not save any changes.
Option Explicit

Sub main()
    Dim SwApp           As SldWorks.SldWorks
    Dim Part            As SldWorks.ModelDoc2
    Dim fileName        As String
    Dim errors          As Long
    Dim warnings        As Long
    Dim errCode         As Long
    Dim longstatus      As Long
    Dim CWAddinCallBack As CosmosWorksLib.CWAddinCallBack
    Dim ActDoc          As CosmosWorksLib.CWModelDoc
    Dim StudyMngr       As CosmosWorksLib.CWStudyManager
    Dim Study           As CosmosWorksLib.CWStudy
    Dim mesh            As CosmosWorksLib.CWMesh
    Dim StaticOptions   As CosmosWorksLib.CWStaticStudyOptions
    Dim lrMgr           As CosmosWorksLib.CWLoadsAndRestraintsManager
    Dim Gravity         As CosmosWorksLib.CWGravity
    Dim dispEntity      As Object
    Dim str1            As String
    Dim var1            As Variant
    Dim xval As Double, yval As Double, zval As Double

    Const MeshEleSize   As Double = 1.4769
    Const MeshTol       As Double = 0.073847


    Set SwApp = Application.SldWorks

    fileName = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2017\tutorial\api\tjoint.sldprt"
    Set Part = SwApp.OpenDoc6(fileName, swDocPART, 1, "", errors, warnings)

    Set CWAddinCallBack = SwApp.GetAddInObject("SldWorks.Simulation")

    Set ActDoc = COSMOSWORKS.ActiveDoc()

    Set StudyMngr = ActDoc.StudyManager()
    StudyMngr.ActiveStudy = 1
    Set Study = StudyMngr.GetStudy(StudyMngr.ActiveStudy)


    'Select top plane for gravity's direction reference
    str1 = "64,31,0,0,1,0,0,0,255,254,255,0,0,0,0,0,3,0,0,0"
    StringtoArray str1, var1
    Set dispEntity = Part.Extension.GetObjectByPersistReference3((var1), longstatus)

    Set lrMgr = Study.LoadsAndRestraintsManager

    'Delete pressure loads
    lrMgr.DeleteLoadsAndRestraints ("Pressure-1")
    lrMgr.DeleteLoadsAndRestraints ("Pressure-2")


    'Add gravity load
    Set Gravity = lrMgr.AddGravity(dispEntity, errCode)
    Gravity.GetGravitationalAcclerationValues xval, yval, zval
    Debug.Print "Gravity acceleration:"
    Debug.Print "  Direction 1: " & xval
    Debug.Print "     Reverse? (1=yes, 0=no) " & Gravity.ReverseDirectionAlongPlaneDir1
    Debug.Print "  Direction 2: " & yval
    Debug.Print "     Reverse? (1=yes, 0=no) " & Gravity.ReverseDirectionAlongPlaneDir2
    Debug.Print "  Normal direction: " & zval
    Debug.Print "     Reverse? (1=yes, 0=no) " & Gravity.ReverseDirectionNormalToPlane
    Debug.Print "  Units as defined in swsUnitSystem_e: " & Gravity.Unit

    Set mesh = Study.Mesh
    mesh.MesherType = 0
    mesh.Quality = 0
    errCode = Study.CreateMesh(0, MeshEleSize, MeshTol)

    Set StaticOptions = Study.StaticStudyOptions
    StaticOptions.SolverType = 2

    'Run analysis
    errCode = Study.RunAnalysis

End Sub

Sub 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 Sub



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Add Gravity Load Example (VBA)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2017 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.