Exemple of C# application using CM2 MeshTools.


Get the source-code of this example.

See also the Visual Basic version, the Borland Delphi version, the native (direct) C++ version.

// Very simple C# program to mesh a square with triangles or quadrangles.
// Uses CM2 TriaMesh and CM2 QuadMesh through the COM interface.

// Import the necessary CM2 MeshTools TLB components.
using System;
using CCM2MATH1Lib;
using meshtools   = CCM2MESHTOOLSLib;
using meshtools1D = CCM2MESHTOOLS1DLib;
using triamesh    = CCM2TRIAMESHLib;
using quadmesh    = CCM2QUADMESHLib;

namespace example_Csharp
{
   class Class1
   {
      [STAThread]
      static void Main(string[] args)
      {
         try
         {
            CDoubleMat                   pos            = new DoubleMat();                // To store the coordinates matrix.
            CUIntVec                     indices        = new UIntVec();                  // To store the indices of the boundary nodes.
            CUIntMat                     connectB       = new UIntMat();                  // To store the connectivity matrix of the boundary edges.
            CUIntMat                     connectM       = new UIntMat();                  // To store the connectivity of the 2-D mesh.

            meshtools.MeshTools          MT             = new meshtools.MeshTools();      // To use the procedures in the meshtools lib.
            meshtools1D.MeshTools1D      MT1D           = new meshtools1D.MeshTools1D();  // To use the procedures in the meshtools1D lib.

            // The mesh generators.
            triamesh.mesher              mesherT3       = new triamesh.mesher();          // Instance of triangle mesher.
            quadmesh.mesher              mesherQ4       = new quadmesh.mesher();          // Instance of quadrangle mesher.

            triamesh.data_type           dataT3         = new triamesh.data_type();       // Data record for a triangle mesher.
            quadmesh.data_type           dataQ4         = new quadmesh.data_type();       // Data record for a quadrangle mesher.

            // DLL REGISTRATION. REPLACE WITH YOUR COMPANY'S KEYS.
            mesherT3.registration ("Licensed to DEMO.", "B36C4016CA0B");
            mesherQ4.registration ("Licensed to DEMO.", "52E503F92365");

            // THE FOUR VERTICES OF THE SQUARE.
            pos.push_back2 (0.0, 4.0);
            pos.push_back2 (4.0, 4.0);
            pos.push_back2 (4.0, 4.0);
            pos.push_back2 (4.0, 4.0);

            // MESH THE FOUR SEGMENTS (10 ELEMENTS ON EACH SEGMENT).
            MT1D.mesh_straight1 (pos, 0, 1, 10, indices);  indices.pop_back();
            MT1D.mesh_straight1 (pos, 1, 2, 10, indices);  indices.pop_back();
            MT1D.mesh_straight1 (pos, 2, 3, 10, indices);  indices.pop_back();
            MT1D.mesh_straight1 (pos, 3, 0, 10, indices);
            MT1D.indices_to_connectE2 (indices, connectB);

            // DO THE TRIANGLE MESH.
            dataT3.pos = pos;                                        // The coordinates.
            dataT3.connectB = connectB;                              // The boundary.
            mesherT3.run (dataT3);                                   // Run the mesher.
            if (dataT3.error_code != 0)          
            {
               string msg1 = dataT3.msg1;
               throw new Exception ("CM2 Exception: " + msg1);       // Error reporting (license error, boundary error...)
            }
            pos = dataT3.pos;                                        // The coordinate matrix has been extended with new nodes.
            connectM = dataT3.connectM;                              // The triangle connectivity matrix.
            dataT3.print_info();                                     // Print some info on console about the generated mesh.

            // MEDIT OUTPUT OF THE TRIANGLE MESH.
            MT.medit_output ("T3.mesh", pos, connectM, meshtools.Celement_type.CM2_FACET3);


            // DO THE SAME WITH THE QUAD MESHER.
            dataQ4.pos = pos;
            dataQ4.connectB = connectB;
            mesherQ4.run (dataQ4);
            if (dataQ4.error_code != 0)       // Catch errors.
            {
               string msg1 = dataQ4.msg1;
               throw new Exception ("CM2 Exception: " + msg1);   
            }
            pos = dataQ4.pos;
            connectM = dataQ4.connectM;
            dataQ4.print_info();

            MT.medit_output ("Q4.mesh", pos, connectM, meshtools.Celement_type.CM2_FACEQ4);
         }

         catch (Exception ex)    
         {
            Console.WriteLine (ex.ToString());
         }
      }
   }
}