CShap FEM 文件转换为 GMSH 网格文件 源代码
时间: 2023-11-03 09:59:49 浏览: 105
SOAP教程C++CSHAP
以下是将CSharp FEM文件转换为GMSH网格文件的源代码:
```csharp
using System;
using System.IO;
namespace CSharpToGMSH
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: CSharpToGMSH.exe input.fem output.msh");
return;
}
string inputPath = args[0];
string outputPath = args[1];
// Read input file
string[] inputLines = File.ReadAllLines(inputPath);
// Extract nodes, elements and element types
int numNodes = 0;
int numElements = 0;
string[] nodeLines = null;
string[] elementLines = null;
string[] elementTypeLines = null;
foreach (string line in inputLines)
{
if (line.StartsWith("Nodes:"))
{
numNodes = int.Parse(line.Substring(6));
nodeLines = new string[numNodes];
for (int i = 0; i < numNodes; i++)
{
nodeLines[i] = inputLines[i + 1];
}
}
else if (line.StartsWith("Elements:"))
{
numElements = int.Parse(line.Substring(9));
elementLines = new string[numElements];
elementTypeLines = new string[numElements];
for (int i = 0; i < numElements; i++)
{
elementLines[i] = inputLines[i + numNodes + 2];
elementTypeLines[i] = inputLines[i + numNodes + numElements + 3];
}
}
}
// Write output file
using (StreamWriter writer = new StreamWriter(outputPath))
{
// Write header
writer.WriteLine("$MeshFormat");
writer.WriteLine("2.2 0 8");
writer.WriteLine("$EndMeshFormat");
// Write nodes
writer.WriteLine("$Nodes");
writer.WriteLine(numNodes);
for (int i = 0; i < numNodes; i++)
{
string[] tokens = nodeLines[i].Split(' ');
writer.WriteLine($"{i + 1} {tokens[0]} {tokens[1]} {tokens[2]}");
}
writer.WriteLine("$EndNodes");
// Write elements
writer.WriteLine("$Elements");
writer.WriteLine(numElements);
for (int i = 0; i < numElements; i++)
{
string[] tokens = elementLines[i].Split(' ');
int numNodesPerElement = int.Parse(tokens[1]);
writer.Write($"{i + 1} {elementTypeLines[i]} 2 0 {numNodesPerElement}");
for (int j = 0; j < numNodesPerElement; j++)
{
writer.Write($" {int.Parse(tokens[j + 2])}");
}
writer.WriteLine();
}
writer.WriteLine("$EndElements");
}
}
}
}
```
阅读全文