Skip to content

Commit

Permalink
Do not swallow exceptions when sending a mesh to GLVis.
Browse files Browse the repository at this point in the history
  • Loading branch information
wo80 committed Nov 6, 2023
1 parent 813a110 commit 5e7a063
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/Triangle/IO/MfemMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ namespace TriangleNet.IO
/// <summary>
/// A simple helper class to write a mesh using MFEM mesh format and send it to GLVis.
/// </summary>
/// <remarks>
/// See https://mfem.org/mesh-format-v1.0/ and https://glvis.org/
/// </remarks>
public static class MfemMesh
{
/// <summary>
/// Send the to default GLVis socket (127.0.0.1:19916).
/// Send the mesh to default GLVis socket (127.0.0.1:19916).
/// </summary>
/// <param name="mesh">The mesh to send.</param>
/// <param name="port">The port number (default = 19916).</param>
Expand All @@ -26,35 +29,28 @@ public static async Task Send(IMesh mesh, int port = 19916)
}

/// <summary>
/// Number the vertices and write them to a .node file.
/// Send the mesh to the given GLVis socket.
/// </summary>
/// <param name="mesh">The mesh to send.</param>
/// <param name="ip">The IP address.</param>
/// <param name="port">The port number.</param>
public static async Task Send(IMesh mesh, IPAddress ip, int port)
{
var client = new TcpClient();
using var client = new TcpClient();

try
{
await client.ConnectAsync(ip, port);

var stream = client.GetStream();

using (var sw = new StreamWriter(stream) { NewLine = "\n" })
{
sw.WriteLine("mesh"); // fem2d_gf_data
sw.WriteLine();
await client.ConnectAsync(ip, port);

Write(mesh, sw);
}
var stream = client.GetStream();

client.Close();
}
catch (Exception e)
using (var sw = new StreamWriter(stream) { NewLine = "\n" })
{
Console.WriteLine(e.Message);
sw.WriteLine("mesh");
sw.WriteLine();

Write(mesh, sw);
}

client.Close();
}

/// <summary>
Expand All @@ -65,7 +61,10 @@ public static async Task Send(IMesh mesh, IPAddress ip, int port)
public static void Write(IMesh mesh, string filename)
{
using var file = File.Open(filename, FileMode.Create);
using var sw = new StreamWriter(file);
using var sw = new StreamWriter(file)
{
NewLine = "\n"
};

Write(mesh, sw);
}
Expand All @@ -77,15 +76,16 @@ public static void Write(IMesh mesh, string filename)
/// <param name="stream">The target stream.</param>
public static void Write(IMesh mesh, Stream stream)
{
using var sw = new StreamWriter(stream);
using var sw = new StreamWriter(stream)
{
NewLine = "\n"
};

Write(mesh, sw);
}

private static void Write(IMesh mesh, StreamWriter sw)
{
sw.NewLine = "\n";

mesh.Renumber();

// Header
Expand Down

0 comments on commit 5e7a063

Please sign in to comment.