Hide Table of Contents

Create Frequency Study with Solid Mesh (VBA)

This example shows how to create a frequency study with solid mesh.

' 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. Modify the path to solidworks materials.sldmat if needed.
' 4. Run the macro.
' Postconditions:
' 1.  Assembly opened.
' 2.  Default frequency study results plots are specified.
' 3.  Frequency study with solid mesh created.
' 4.  Same material applied to all bodies.
' 5.  Mesh created with default global size and tolerance parameters.
' 6.  Solver set to automatic.
' 7.  Number of frequencies set.
' 8.  Analysis run.
' 9.  Results returned: frequencies and mass participation factors.
' 10. Inspect the frequency study results plots.
' -----------------------------------------------------------------

Option Explicit


Sub main()


   Dim swApp As SldWorks.SldWorks

   Dim COSMOSWORKS As Object

   Dim COSMOSObject As CosmosWorksLib.CwAddincallback

   Dim ActDoc As CosmosWorksLib.CWModelDoc

   Dim StudyMngr As CosmosWorksLib.CWStudyManager

   Dim Study As CosmosWorksLib.CWStudy

   Dim SolidMgr As CosmosWorksLib.CWSolidManager

   Dim SolidComponent As CosmosWorksLib.CWSolidComponent

   Dim SolidBody As CosmosWorksLib.CWSolidBody

   Dim CwMesh As CosmosWorksLib.CwMesh

   Dim CWResult As CosmosWorksLib.CWResults

   Dim FrequencyOptions As CosmosWorksLib.CWFrequencyStudyOptions

   Dim CWMat As CosmosWorksLib.CWMaterial

   Dim longstatus As Long, longwarnings As Long

   Dim errCode As Long, errorCode As Long, pValue As Long

   Dim lStatus As Long

   Dim CompCount As Integer

   Dim BodyCount As Integer

   Dim j As Integer

   Dim i As Integer

   Dim bApp As Boolean

   Dim Freq As Variant

   Dim MassPart As Variant

   Dim el As Double, tl As Double


    ' Connect to SolidWork software

    If swApp Is Nothing Then Set swApp = Application.SldWorks


    ' Get SolidWorks Simulation object

    Set COSMOSObject = swApp.GetAddInObject("SldWorks.Simulation")

    If COSMOSObject Is Nothing Then ErrorMsg swApp, "COSMOSObject object not found", True


    If COSMOSWORKS Is Nothing Then ErrorMsg swApp, "COSMOSWORKS object not found", True


    ' Get active document

    swApp.OpenDoc6 "C:\Program Files\SolidWorks Corp\SolidWorks\Simulation\Examples\shaft.sldasm", swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_Silent, "", longstatus, longwarnings

    Set ActDoc = COSMOSWORKS.ActiveDoc()

    If ActDoc Is Nothing Then ErrorMsg swApp, "No active document.", True

    ' Add default frequency study results plots of resultant amplitudes for all mode shapes
    errCode = ActDoc.AddDefaultFrequencyOrBucklingStudyPlot(True, 0, True, swsFrequencyBucklingDisplacement_URES)

    ' Create new frequency study

    Set StudyMngr = ActDoc.StudyManager()

    If StudyMngr Is Nothing Then ErrorMsg swApp, "No StudyMngr object.", True

    Set Study = StudyMngr.CreateNewStudy3("Frequency", swsAnalysisStudyTypeFrequency, swsMeshTypeSolid, errCode)

    If Study Is Nothing Then ErrorMsg swApp, "Study not created.", True


    ' Get number of solid components

    Set SolidMgr = Study.SolidManager

    If SolidMgr Is Nothing Then ErrorMsg swApp, "SolidManager object not created.", True

    CompCount = SolidMgr.ComponentCount


    ' Apply SolidWorks material to rest of components

    Set SolidBody = Nothing

    Set SolidComponent = Nothing

        For j = 0 To (CompCount - 1)

            Set SolidComponent = SolidMgr.GetComponentAt(j, errorCode)

                BodyCount = SolidComponent.SolidBodyCount

          For i = 0 To (BodyCount - 1)

            Set SolidBody = SolidComponent.GetSolidBodyAt(i, errCode)

            If errCode <> 0 Then ErrorMsg swApp, "No solid body", True

            bApp = SolidBody.SetLibraryMaterial("c:\Program Files\SolidWorks Corp\SolidWorks\lang\english\sldmaterials\solidworks materials.sldmat", "Ductile Iron (SN)")

            If bApp = False Then ErrorMsg swApp, "No material applied.", True

            Set SolidBody = Nothing

          Next i

        Next j


    ' Set meshing

    Set CwMesh = Study.Mesh

    If CwMesh Is Nothing Then ErrorMsg swApp, "No mesh object.", False

    CwMesh.Quality = 1

    Call CwMesh.GetDefaultElementSizeAndTolerance(0, el, tl)

    errCode = Study.CreateMesh(0, el, tl)

    If errCode <> 0 Then ErrorMsg swApp, "Mesh failed.", True


    ' Set solver type as automatic

    Set FrequencyOptions = Study.FrequencyStudyOptions

    If FrequencyOptions Is Nothing Then ErrorMsg swApp, "No FrequencyOptions object.", False

    FrequencyOptions.SolverType = 1

    FrequencyOptions.NoOfFrequencies = 8


    ' Run analysis

    errCode = Study.RunAnalysis

    If errCode <> 0 Then ErrorMsg swApp, "Analysis failed", True

    ' Get results: frequencies and mass participation factors

    Set CWResult = Study.Results

    If CWResult Is Nothing Then ErrorMsg swApp, "No result object.", False

    Freq = CWResult.GetResonantFrequencies(errCode)

    MassPart = CWResult.GetMassParticipation(errCode)

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

End Function

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:   Create Frequency Study with Solid Mesh (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) 2014 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.