Exemple of Delphi application using CM2 MeshTools.


Get the source-code of this example.

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

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

program Project1;

{$APPTYPE CONSOLE}

// Import the necessary CM2 MeshTools TLB components.
uses
  SysUtils, 
  ActiveX,
  ComObj,
  CCM2MATH1Lib_TLB       in 'c:\program files\borland\bds\3.0\Imports\CCM2MATH1Lib_TLB.pas',
  CCM2MESHTOOLSLib_TLB   in 'c:\program files\borland\bds\3.0\Imports\CCM2MESHTOOLSLib_TLB.pas',
  CCM2MESHTOOLS1DLib_TLB in 'c:\program files\borland\bds\3.0\Imports\CCM2MESHTOOLS1DLib_TLB.pas',
  CCM2TRIAMESHLib_TLB    in 'c:\program files\borland\bds\3.0\Imports\CCM2TRIAMESHLib_TLB.pas',
  CCM2QUADMESHLib_TLB    in 'c:\program files\borland\bds\3.0\Imports\CCM2QUADMESHLib_TLB.pas';

var
  pos: DoubleMat;                          // To store the coordinates matrix.
  indices: UIntVec;                        // To store the indices of the boundary nodes.
  connectB: UIntMat;                       // To store the connectivity matrix of the boundary edges.
  connectM: UIntMat;                       // To store the connectivity of the 2-D mesh.
  MT: MeshTools;                           // To use the procedures in the meshtools lib.
  MT1D: MeshTools1D;                       // To use the procedures in the meshtools1D lib.
  mesherT3: CCM2TRIAMESHLib_TLB.mesher;    // Instance of triangle mesher.
  mesherQ4: CCM2QUADMESHLib_TLB.mesher;    // Instance of quadrangle mesher.
  dataT3: CCM2TRIAMESHLib_TLB.data_type;   // Data record for a triangle mesher.
  dataQ4: CCM2QUADMESHLib_TLB.data_type;   // Data record for a quadrangle mesher.

begin
  ActiveX.CoInitialize (nil);
  try
     // Creation of the interfaces.
     pos := CoCDoubleMat.Create();
     indices := CoCUIntVec.Create();
     connectB := CoCUIntMat.Create();
     connectM := CoCUIntMat.Create();
     MT := CoMeshTools.Create();
     MT1D := CoMeshTools1D.Create();
     mesherT3 := CCM2TRIAMESHLib_TLB.Comesher.Create();
     mesherQ4 := CCM2QUADMESHLib_TLB.Comesher.Create();
     dataT3 := CCM2TRIAMESHLib_TLB.Codata_type.Create();
     dataQ4 := CCM2QUADMESHLib_TLB.Codata_type.Create();

     // 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, 0.0);
     pos.push_back2 (4.0, 0.0);
     pos.push_back2 (4.0, 4.0);
     pos.push_back2 (0.0, 4.0);

     // MESH THE FOUR SEGMENTS (10 ELEMENTS ON EACH SEGMENT).
     MT1D.mesh_straight1 (pos, 0, 1, 10, indices); indices.pop_back();   // 10 nodes between node #0 and node #1.
     MT1D.mesh_straight1 (pos, 1, 2, 10, indices); indices.pop_back();   // 10 nodes between node #1 and node #2.
     MT1D.mesh_straight1 (pos, 2, 3, 10, indices); indices.pop_back();   // 10 nodes between node #2 and node #3.
     MT1D.mesh_straight1 (pos, 3, 0, 10, indices);                       // 10 nodes between node #3 and node #0.
     MT1D.indices_to_connectE2 (indices, connectB);                      // Create edges from the set of consecutive nodes in 'indices'.

     // 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 then
        raise Exception.Create ('CM2 Exception: ' + dataT3.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, CM2_FACET3);

     // DO THE SAME WITH THE QUAD MESHER.
     dataQ4.pos := pos;
     dataQ4.connectB := connectB;
     mesherQ4.run (dataQ4);
     if dataQ4.error_code <> 0 then
        raise Exception.Create ('CM2 Exception: ' + dataQ4.msg1); 
     pos := dataQ4.pos;
     connectM := dataQ4.connectM;
     dataQ4.print_info();
     MT.medit_output('Q4.mesh', pos, connectM, CM2_FACEQ4);

  except
      on E: Exception do WriteLn(E.Message);
end.