Hide Table of Contents

Copy Mesh and Generate Report Example (C#)

This example shows how to copy the mesh from one study to another and generate a report.

//----------------------------------------------------------------------------
// 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, click Add Reference, click the Browse tab,
//    navigate to install_dir\api\redist\CLR2,
//    click SolidWorks.Interop.cosworks.dll, and click OK).

// 3. Open install_dir\Simulation\Examples\Verification\buckling2.sldprt.

//
// Postconditions:
// 1. Meshes the Quality and Draft studies.
// 2. Copies the mesh from the Quality study to the Draft study.
// 3. Click OK to close any dialog boxes.
// 4. Generates a report for the Draft study in c:\temp.
//
// NOTE: Because the model is used elsewhere, do not save changes.
// ---------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.cosworks;
using System.Runtime.InteropServices;
namespace CopyMesh_CSharp.csproj
{
    partial class SolidWorksMacro
    {
 
        public void Main()
        {
            CosmosWorks COSMOSWORKS = default(CosmosWorks);
            CwAddincallback CWAddinCallBack = default(CwAddincallback);
            CWModelDoc ActDoc = default(CWModelDoc);
            CWStudyManager StudyMngr = default(CWStudyManager);
            CWStudy Study = default(CWStudy);
            CWMesh CWMesh = default(CWMesh);
 
            int errCode = 0;
            int n = 0;
            double el = 0;
            double tl = 0;
 
            const double NoOfElements1 = 712;
            const double NoOfElements2 = 2686;
 
            CWAddinCallBack = (CwAddincallback)swApp.GetAddInObject("CosmosWorks.CosmosWorks");
            if (CWAddinCallBack == null)
                ErrorMsg(swApp, "No CWAddinCallBack object"true);
            COSMOSWORKS = CWAddinCallBack.CosmosWorks;
            if (COSMOSWORKS == null)
                ErrorMsg(swApp, "No CosmosWorks object"true);
 
            //Get active document
            ActDoc = COSMOSWORKS.ActiveDoc;
            if (ActDoc == null)
                ErrorMsg(swApp, "No active document"true);
 
            //Get the first study
            StudyMngr = ActDoc.StudyManager;
            if (StudyMngr == null)
                ErrorMsg(swApp, "No study manager object"true);
            StudyMngr.ActiveStudy = 0;
            Study = StudyMngr.GetStudy(0);
            if (Study == null)
                ErrorMsg(swApp, "No study object"true);
 
            //Mesh the first study
            CWMesh = Study.Mesh;
            if (CWMesh == null)
                ErrorMsg(swApp, "No mesh object"false);
            CWMesh.Quality = 1;
            CWMesh.GetDefaultElementSizeAndTolerance(0, out el, out tl);
            errCode = Study.CreateMesh(0, el, tl);
            if (errCode != 0)
                ErrorMsg(swApp, "Mesh failed"true);
 
            n = CWMesh.NodeCount;
 
            if ((n < 0.95 * NoOfElements2) | (n > 1.05 * NoOfElements2))
            {
                ErrorMsg(swApp, " % error = " + (((n - NoOfElements2) / NoOfElements2) * 100), false);
            }
 
            //Get the second study
            StudyMngr.ActiveStudy = 1;
            Study = StudyMngr.GetStudy(1);
            if (Study == null)
                ErrorMsg(swApp, "No study object"true);
 
            //Mesh the second study
            CWMesh = Study.Mesh;
            if (CWMesh == null)
                ErrorMsg(swApp, "No mesh object"false);
            CWMesh.Quality = 0;
            CWMesh.GetDefaultElementSizeAndTolerance(0, out el, out tl);
            errCode = Study.CreateMesh(0, el, tl);
            if (errCode != 0)
                ErrorMsg(swApp, "Mesh failed"true);
 
            n = CWMesh.NodeCount;
            if ((n < 0.95 * NoOfElements1) | (n > 1.05 * NoOfElements1))
            {
                ErrorMsg(swApp, " % error = " + (((n - NoOfElements1) / NoOfElements1) * 100), false);
            }
 
            //Copy mesh from the first study to the second study
            errCode = Study.CopyMeshFromStudy("Quality");
            if (errCode != 0)
                ErrorMsg(swApp, "Mesh failed to copy"true);
 
            n = CWMesh.NodeCount;
            if ((n < 0.95 * NoOfElements2) | (n > 1.05 * NoOfElements2))
            {
                ErrorMsg(swApp, " % error = " + (((n - NoOfElements2) / NoOfElements2) * 100), false);
            }
 
            //Generate Report
            errCode = Study.GenerateReport("C:\\temp""report"false);
            if (errCode != 1)
                ErrorMsg(swApp, "Failed to generate report"true);
 
        }
 
        public void ErrorMsg(SldWorks SwApp, string Message, bool EndTest)
        {
            SwApp.SendMsgToUser2(Message, 0, 0);
            SwApp.RecordLine("'*** WARNING - General");
            SwApp.RecordLine("'*** " + Message);
            SwApp.RecordLine("");
        }
 
        /// <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:   Copy Mesh and Generate Report 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) 2015 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.