Hide Table of Contents

Apply Mesh Control (C#)

This example shows how to get and set beams and joints, create a mesh, and apply a mesh control.

//---------------------------------------------------------------------------
// Preconditions:
// 1. Add the SolidWorks Simulation as an add-in
//    (in SolidWorks, click Tools > Add-ins > SolidWorks Simulation).
// 2. Add a reference to the SolidWorks Simulation primary
//    interop assembly (in the IDE, right-click the name of the
//    project, select Add Reference, click the Browse tab,
//    navigate to <SolidWorks_install_dir>\api\redist\CLR2,
//    and select SolidWorks.Interop.cosworks.dll.
// 3. Modify the path to the part document and solidworks materials.sldmat
//    as needed.
// 4. Open the Immediate window.  
//
// Postconditions:
// 1. A static study, frame, is created.
// 2. Beam information is shown in the Immediate window.
// 3. Plain Carbon Steel material is applied to all beams.
// 4. Joints are calculated for all beams, and
//    a neutral axis is shown for each beam. The pinball tolerance
//    value and unit are shown in the Immediate window.
// 5. A mixed mesh is created, and its type and state are shown
//    in the Immediate window.
//
//    NOTE: Step 5 can take several minutes to complete.
//
// 6. A mesh control is applied, and its various values are shown
//    in the Immediate window.
//
// NOTES:
// * Beam elements are created by default for parts with
//   structural members.
// * Because the part document is used elsewhere,
//   do not save any  changes when closing it.
//-------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.cosworks;
using System;
using System.Diagnostics;

namespace Macro1.csproj
{
    
partial class SolidWorksMacro
    {
        
public void Main()
        {
            ModelDoc2 swModel =
default(ModelDoc2);
            ModelDocExtension swModelDocExt =
default(ModelDocExtension);
            SelectionMgr swSelMgr =
default(SelectionMgr);
            CosmosWorks COSMOSWORKS =
default(CosmosWorks);
            CwAddincallback COSMOSObject =
default(CwAddincallback);
            CWModelDoc ActDoc =
default(CWModelDoc);
            CWStudyManager StudyMngr =
default(CWStudyManager);
            CWStudy Study =
default(CWStudy);
            CWBeamManager BeamMgr =
default(CWBeamManager);
            CWBeamBody BeamBody =
default(CWBeamBody);
            CWJoints Joints =
default(CWJoints);
            CWMesh Mesh =
default(CWMesh);
            CWMeshControl MeshControl =
default(CWMeshControl);
            
int nbrBeamBodies = 0;
            
int beamBodyType = 0;
            
double ElementSize = 0;
            
double Tolerance = 0;
            
int errors = 0;
            
int warnings = 0;
            
int errCode = 0;
            
int j = 0;
            
bool keepJointUpdates = true;
            
bool status = true;
            
Object selEntity1 = null;
            
Object selEntity2 = null;
            
Object selEntity3 = null;
            
Object selEntity4 = null;
            
Object selEntity5 = null;
            
Object selEntity6 = null;
            
Object selEntity7 = null;
            
Object selEntity8 = null;
            
Object selEntity9 = null;

            
// Get the SolidWorks Simulation object
            COSMOSObject = (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");
            
if (COSMOSObject == null) ErrorMsg("COSMOSObject object not found.", true);
            COSMOSWORKS = COSMOSObject.CosmosWorks;
            
if (COSMOSWORKS == null) ErrorMsg("COSMOSWORKS object not found.", true);

            
//Open and get the active document
            swModel = (ModelDoc2)swApp.OpenDoc6("c:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\weldments\\weldment_box2.sldprt", (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            swModelDocExt = (ModelDocExtension)swModel.Extension;
            swSelMgr = (SelectionMgr)swModel.SelectionManager;
            ActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;
            
if (ActDoc == null) ErrorMsg("No active document.", true);

            
//Create new static study named frame
            StudyMngr = (CWStudyManager)ActDoc.StudyManager;
            
if (StudyMngr == null) ErrorMsg("StudyMngr object not there.", true);
            Study = (CWStudy)StudyMngr.CreateNewStudy(
"frame", (int)swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, (int)swsMeshType_e.swsMeshTypeMixed, out errCode);
            
if (Study == null) ErrorMsg("Study not created.", true);

            
// Get and set beam info
            BeamMgr = (CWBeamManager)Study.BeamManager;
            nbrBeamBodies = BeamMgr.BeamCount;
            
Debug.Print("Beams...");
            
Debug.Print(" Number of beams: " + nbrBeamBodies);
            BeamBody =
null;
            
for (j = 0; j <= (nbrBeamBodies - 1); j++)
            {
                BeamBody = (CWBeamBody)BeamMgr.GetBeamBodyAt(j,
out errCode);
                
if (errCode != 0) ErrorMsg("No beam body.", true);
                
Debug.Print(" Name of beam body: " + BeamBody.BeamBodyName);
                beamBodyType = BeamBody.BeamType;
                
if (beamBodyType == 0)
                {
                    
Debug.Print(" Type of beam body: beam");
                }
                
else
                {
                    
Debug.Print(" Type of beam body: truss");
                }
                errCode = BeamBody.SetLibraryMaterial(
"C:\\Program Files\\SolidWorks Corp\\SolidWorks\\lang\\english\\sldmaterials\\solidworks materials.sldmat", "Plain Carbon Steel");
                
if (errCode == 0) ErrorMsg("No material applied.", true);
                BeamBody =
null;
            }

            
// Calculate joints
            Joints = (CWJoints)BeamMgr.GetJointGroup(out errCode);
            
Debug.Print(" ");
            
Debug.Print("Joints...");
            
if (errCode != 0) ErrorMsg("No joint group.", true);
            Joints.JointsBeginEdit();
            Joints.IncludeAllSelectedBeam =
true;
            Joints.IncludeDisplayNeutralAxis =
true;
            Joints.CalculateJoints();
            Joints.JointsEndEdit();
            keepJointUpdates = Joints.IncludeKeepModifiedJointOnUpdate;
            
if (keepJointUpdates == true)
            {
                
Debug.Print(" Keep joint updates: yes");
            }
            
else
                Debug.Print(" Keep joint udpates: no");
            
Debug.Print(" Overwrite pinball value: " + Joints.IncludeTreatAsJointForClearanceLessThan);
            
Debug.Print(" Pinball radius: " + Joints.PinBallRadius * 0.001);
            
switch (Joints.PinBallRadiusUnit)
            {
                
case 0:
                    
Debug.Print(" Pinball radius unit: mm");
                    
break;
                
case 1:
                    
Debug.Print(" Pinball radius unit: cm");
                    
break;
                
case 2:
                    
Debug.Print(" Pinball radius unit: m");
                    
break;
                
case 3:
                    
Debug.Print(" Pinball radius unit: in");
                    
break;
                
case 4:
                    
Debug.Print(" Pinball radius unit: ft");
                    
break;
                
case 5:
                    
Debug.Print(" Pinball radius unit: ft-in");
                    
break;
                
case 6:
                    
Debug.Print(" Pinball radius unit: am");
                    
break;
                
case 7:
                    
Debug.Print(" Pinball radius unit: nm");
                    
break;
                
case 8:
                    
Debug.Print(" Pinball radius unit: micron");
                    
break;
                
case 9:
                    
Debug.Print(" Pinball radius unit: mil");
                    
break;
                
case 10:
                    
Debug.Print(" Pinball radius unit: microin");
                    
break;
            }

            
// Mesh the part
            Debug.Print(" ");
            
Debug.Print("*** Creating a mesh next, which can take several minutes. Please wait. ***");

            Mesh = Study.Mesh;
            
if (Mesh == null) ErrorMsg("No mesh object.", false);
            Mesh.Quality = (
int)swsMeshQuality_e.swsMeshQualityHigh;
            Mesh.GetDefaultElementSizeAndTolerance((
int)swsLinearUnit_e.swsLinearUnitMillimeters, out ElementSize, out Tolerance);
            errCode = Study.CreateMesh((
int)swsLinearUnit_e.swsLinearUnitMillimeters, ElementSize, Tolerance);
            
if (errCode != 0) ErrorMsg("Mesh failed.", true);
            
Debug.Print(" ");
            
Debug.Print("Mesh...");
            
Debug.Print(" Time to create mesh (hh:mm:ss): " + Mesh.TimeToCompleteMesh);
            
switch (Mesh.MeshType)
            {
                
case 0:
                    
Debug.Print(" Mesh type: solid");
                    
break;
                
case 1:
                    
Debug.Print(" Mesh type: midsurface");
                    
break;
                
case 2:
                    
Debug.Print(" Mesh type: surface");
                    
break;
                
case 3:
                    
Debug.Print(" Mesh type: mixed");
                    
break;
                
case 4:
                    
Debug.Print(" Mesh type: beam");
                    
break;
            }

            
Debug.Print(" Number of mesh controls: " + Mesh.MeshControlCount);

            
switch (Mesh.MeshState)
            {
                
case 0:
                    
Debug.Print(" Mesh state: no mesh");
                    
break;
                
case 1:
                    
Debug.Print(" Mesh state: exists and is current");
                    
break;
                
case 2:
                    
Debug.Print(" Mesh state: exists and is not current");
                    
break;
                
case 3:
                    
Debug.Print(" Mesh state: failed");
                    
break;
                
case 4:
                    
Debug.Print(" Mesh state: interrupted");
                    
break;
            }

            
// Select structural members for mesh control
            Debug.Print("");
            
Debug.Print("Mesh control...");

            status = swModelDocExt.SelectByID2(
"Structural Member1[4]", "SOLIDBODY", 0, 0, 0, false, 0, null, 0);
            selEntity1 = (
Object)swSelMgr.GetSelectedObject6(2, -1);

            status = swModelDocExt.SelectByID2(
"Structural Member3[2]", "SOLIDBODY", 0, 0, 0, false, 0, null, 0);
            selEntity2 = (
Object)swSelMgr.GetSelectedObject6(3, -1);

            status = swModelDocExt.SelectByID2(
"Structural Member3[3]", "SOLIDBODY", 0, 0, 0, false, 0, null, 0);
            selEntity3 = (
Object)swSelMgr.GetSelectedObject6(4, -1);

            status = swModelDocExt.SelectByID2(
"Structural Member3[4]", "SOLIDBODY", 0, 0, 0, false, 0, null, 0);
            selEntity4 = (
Object)swSelMgr.GetSelectedObject6(6, -1);

            status = swModelDocExt.SelectByID2(
"Structural Member3[1]", "SOLIDBODY", 0, 0, 0, false, 0, null, 0);
            selEntity5 = (
Object)swSelMgr.GetSelectedObject6(8, -1);

            status = swModelDocExt.SelectByID2(
"Structural Member1[1]", "SOLIDBODY", 0, 0, 0, false, 0, null, 0);
            selEntity6 = (
Object)swSelMgr.GetSelectedObject6(12, -1);

            status = swModelDocExt.SelectByID2(
"Structural Member5[1]", "SOLIDBODY", 0, 0, 0, false, 0, null, 0);
            selEntity7 = (
Object)swSelMgr.GetSelectedObject6(14, -1);

            status = swModelDocExt.SelectByID2(
"Structural Member5[2]", "SOLIDBODY", 0, 0, 0, false, 0, null, 0);
            selEntity8 = (
Object)swSelMgr.GetSelectedObject6(15, -1);

            status = swModelDocExt.SelectByID2(
"Structural Member1[3]", "SOLIDBODY", 0, 0, 0, false, 0, null, 0);
            selEntity9 = (
Object)swSelMgr.GetSelectedObject6(16, -1);

            
object[] selEntities = { selEntity1, selEntity2, selEntity3, selEntity4, selEntity5, selEntity6, selEntity7, selEntity8, selEntity9 };


            MeshControl = (CWMeshControl)Mesh.ApplyMeshControl(selEntities,
out errCode);
            
Debug.Print("  Name of mesh control: " + MeshControl.Name);
            
Debug.Print("  Element size for mesh control: " + MeshControl.ElementSize);
            
Debug.Print("  Number of entities in mesh control: " + MeshControl.EntityCount);
            
Debug.Print("  Number of elements on a beam: " + MeshControl.NumofElementsforBeams);
            
Debug.Print("  State of mesh control (0 = suppressed; 1 = not suppressed): " + MeshControl.State);


        }


        
//Error routine
        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:   Apply Mesh Control (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.