Hide Table of Contents

Create Frequency Study with Solid Mesh Example (C#)

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.
//
// 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.
// -----------------------------------------------------------------

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using SolidWorks.Interop.cosworks;

using System;

namespace CreateFrequencyStudySolidMeshCSharp.csproj

{

    partial class SolidWorksMacro

    {

        public void Main()

        {

            CosmosWorks COSMOSWORKS = default(CosmosWorks);

            CwAddincallback COSMOSObject = default(CwAddincallback);

            CWModelDoc ActDoc = default(CWModelDoc);

            CWStudyManager StudyMngr = default(CWStudyManager);

            CWStudy Study = default(CWStudy);

            CWSolidManager SolidMgr = default(CWSolidManager);

            CWSolidComponent SolidComponent = default(CWSolidComponent);

            CWSolidBody SolidBody = default(CWSolidBody);

            CWMesh CwMesh = default(CWMesh);

            CWResults CWResult = default(CWResults);

            CWFrequencyStudyOptions FrequencyOptions = default(CWFrequencyStudyOptions);

            int status = 0;

            int warnings = 0;

            int errCode = 0;

            int errorCode = 0;

            int CompCount = 0;

            int BodyCount = 0;

            int j = 0;

            int i = 0;

            int returnValue = 0;

            object Freq = null;

            object MassPart = null;

            double el = 0;

            double tl = 0;

 

            // Get SolidWorks Simulation object

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

            if (COSMOSObject == null) ErrorMsg("COSMOSObject object not found", true);

            COSMOSWORKS = (CosmosWorks)COSMOSObject.CosmosWorks;

            if (COSMOSWORKS == null) ErrorMsg("COSMOSWORKS object not found", true);

 

            // Get active document

            swApp.OpenDoc6("C:\\Program Files\\SolidWorks Corp\\SolidWorks\\Simulation\\Examples\\shaft.sldasm", (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref status, ref warnings);

            ActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;

            if (ActDoc == null) ErrorMsg("No active document.", true);

 

            // Add default frequency study result plot of resultant amplitude
            errCode = ActDoc.AddDefaultFrequencyOrBucklingStudyPlot(true, 0, true, (int)swsFrequencyBucklingResultDisplacementComponentTypes_e.swsFrequencyBucklingDisplacement_URES);

 

            // Create new frequency study

            StudyMngr = (CWStudyManager)ActDoc.StudyManager;

            if (StudyMngr == null) ErrorMsg("No StudyMngr object.", true);

            Study = (CWStudy)StudyMngr.CreateNewStudy3("Frequency", (int)swsAnalysisStudyType_e.swsAnalysisStudyTypeFrequency, (int)swsMeshType_e.swsMeshTypeSolid, out errCode);

            if (Study == null) ErrorMsg("Study not created.", true);

 

            // Get number of solid components

            SolidMgr = (CWSolidManager)Study.SolidManager;

            if (SolidMgr == null) ErrorMsg("SolidManager object not created.", true);

            CompCount = SolidMgr.ComponentCount;

 

            // Apply SolidWorks material to rest of components

            SolidBody = null;

            SolidComponent = null;

            for (j = 0; j <= (CompCount - 1); j++)

            {

                SolidComponent = (CWSolidComponent)SolidMgr.GetComponentAt(j, out errorCode);

                BodyCount = SolidComponent.SolidBodyCount;

                for (i = 0; i <= (BodyCount - 1); i++)

                {

                    SolidBody = (CWSolidBody)SolidComponent.GetSolidBodyAt(i, out errCode);

                    if (errCode != 0) ErrorMsg("No solid body", true);

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

                    if (returnValue != 1) ErrorMsg("No material applied.", true);

                    SolidBody = null;

                }

            }

 

            // Set meshing

            CwMesh = (CWMesh)Study.Mesh;

            if (CwMesh == null) ErrorMsg("No mesh object.", false);

            CwMesh.Quality = 1;

            CwMesh.GetDefaultElementSizeAndTolerance(0, out el, out tl);

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

            if (errCode != 0) ErrorMsg("Mesh failed.", true);

 

            // Set solver type as automatic

            FrequencyOptions = (CWFrequencyStudyOptions)Study.FrequencyStudyOptions;

            if (FrequencyOptions == null) ErrorMsg("No frequency options object.", false);

            FrequencyOptions.SolverType = 1;

            FrequencyOptions.NoOfFrequencies = 8;

 

            // Run analysis

            errCode = Study.RunAnalysis();

            if (errCode != 0) ErrorMsg("Analysis failed", true);

 

            // Get results: frequencies and mass participation factors

            CWResult = (CWResults)Study.Results;

            if (CWResult == null) ErrorMsg("No result object.", false);

            Freq = CWResult.GetResonantFrequencies(out errCode);

            MassPart = CWResult.GetMassParticipation(out errCode);

        }

 

 

        private void ErrorMsg(string Message, bool EndTest)

        {

            swApp.SendMsgToUser2(Message, 0, 0);

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

            swApp.RecordLine("'*** " + Message);

            swApp.RecordLine("");

            if (EndTest)

            {

            }

        }

 

        /// <summary>

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

        /// </summary>

        public SldWorks swApp;

    }

}



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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Create Frequency Study with Solid Mesh Example (C#)
*Comment:  
*   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:




x

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
x

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.