Hide Table of Contents

Create Frequency Study with Solid Mesh Example (VB.NET)

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 primary interop assembly as
'    a reference (in the IDE's Project Explorer, right-click
'    the project name, select Add Reference, click the Browse tab,
'    navigate to the install_dir\api\redist\CLR2 folder and
'    select SolidWorks.Interop.cosworks.dll).
' 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.
' -----------------------------------------------------------------

Imports SolidWorks.Interop.sldworks

Imports SolidWorks.Interop.swconst

Imports SolidWorks.Interop.cosworks

Imports System


Partial Class SolidWorksMacro


    Public Sub main()


        Dim COSMOSWORKS As Object

        Dim COSMOSObject As CwAddincallback

        Dim ActDoc As CWModelDoc

        Dim StudyMngr As CWStudyManager

        Dim Study As CWStudy

        Dim SolidMgr As CWSolidManager

        Dim SolidComponent As CWSolidComponent

        Dim SolidBody As CWSolidBody

        Dim CwMesh As CwMesh

        Dim CWResult As CWResults

        Dim FrequencyOptions As CWFrequencyStudyOptions

        Dim longstatus As Long, longwarnings As Long

        Dim errCode As Long, errorCode 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 Object

        Dim MassPart As Object

        Dim el As Double, tl As Double


        ' Get SolidWorks Simulation object

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

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


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


        ' Get active document

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

        ActDoc = COSMOSWORKS.ActiveDoc()

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


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

        ' Create new frequency study

        StudyMngr = ActDoc.StudyManager()

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

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

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


        ' Get number of solid components

        SolidMgr = Study.SolidManager

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

        CompCount = SolidMgr.ComponentCount


        ' Apply SolidWorks material to rest of components

        SolidBody = Nothing

        SolidComponent = Nothing

        For j = 0 To (CompCount - 1)

            SolidComponent = SolidMgr.GetComponentAt(j, errorCode)

            BodyCount = SolidComponent.SolidBodyCount

            For i = 0 To (BodyCount - 1)

                SolidBody = SolidComponent.GetSolidBodyAt(i, errCode)

                If errCode <> 0 Then ErrorMsg("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("No material applied.", True)

                SolidBody = Nothing

            Next i

        Next j


        ' Set meshing

        CwMesh = Study.Mesh

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

        CwMesh.Quality = 1

        Call CwMesh.GetDefaultElementSizeAndTolerance(0, el, tl)

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

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


        ' Set solver type as automatic

        FrequencyOptions = Study.FrequencyStudyOptions

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

        FrequencyOptions.SolverType = 1

        FrequencyOptions.NoOfFrequencies = 8


        ' Run analysis

        errCode = Study.RunAnalysis

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

        ' Get results: frequencies and mass participation factors

        CWResult = Study.Results

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

        Freq = CWResult.GetResonantFrequencies(errCode)

        MassPart = CWResult.GetMassParticipation(errCode)

    End Sub


    ' Error routine

    Private Sub ErrorMsg(ByVal Message As String, ByVal EndTest As Boolean)

        swApp.SendMsgToUser2(Message, 0, 0)

        swApp.RecordLine("'*** WARNING - General")

        swApp.RecordLine("'*** " & Message)


    End Sub


    ''' <summary>

    ''' The SldWorks swApp variable is pre-assigned for you.

    ''' </summary>

    Public swApp As SldWorks

End Class

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 Example (VB.NET)
*   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.