// Copyright (C) Stichting Deltares 2024. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System.IO;
using System.Text.Json;
using Deltares.DamEngine.Data.Geometry;
namespace Deltares.DamEngine.Data.GeometryExport;
///
/// Export the geometry to a file
///
public static class GeometryExporter
{
///
/// Next constants are used for debugging purposes
///
public const string VisualizationFolder = @"C:\Dam\DamTools\GeometryVisualizer\"; //#John, for debugging purposes
//public const string VisualizationFolder = @"D:\src\dam\DamTools\GeometryVisualizer\"; //#Tom
public const string ExportJasonFile = "GeometryAndSurfaceLinePlot.json"; //for debugging purposes
///
/// Export the geometry to a json file
///
///
///
///
public static void ExportWithSurfaceLineToJsonFile(string fileName, GeometryData geometry, GeometryPointString surfaceLine = null)
{
var jsonSerializerOptions = new JsonSerializerOptions
{
WriteIndented = true
};
PersistableGeometry persistableGeometry = GeometryExporterHelper.CreateFromGeometryData(geometry);
PersistableSurfaceLine persistableSurfaceLine = GeometryExporterHelper.CreateFromGeometryPointString(surfaceLine);
PersistableGeometryWithSurfaceLine persistableGeometryWithSurfaceLine = new PersistableGeometryWithSurfaceLine
{
Geometry = persistableGeometry,
SurfaceLine = persistableSurfaceLine
};
string output = JsonSerializer.Serialize(persistableGeometryWithSurfaceLine, jsonSerializerOptions);
// Save output to text file
File.WriteAllText(fileName, output);
}
///
/// Export the geometry to a text file
///
///
///
public static void ExportToFile(GeometryData geometry, string fileName)
{
using (var writer = new StreamWriter(fileName))
{
// Export points
writer.WriteLine("[BEGIN POINTS]");
foreach (Point2D point in geometry.Points)
{
writer.WriteLine($"{point.X}, {point.Z}");
}
writer.WriteLine("[END POINTS]");
// Export curves
writer.WriteLine("[BEGIN CURVES]");
foreach (GeometryCurve curve in geometry.Curves)
{
writer.WriteLine($"Curve: ({curve.HeadPoint.X}, {curve.HeadPoint.Z}) - ({curve.EndPoint.X}, {curve.EndPoint.Z})");
}
writer.WriteLine("[END CURVES]");
// Export surfaces
writer.WriteLine("[BEGIN SURFACES]");
foreach (GeometrySurface surface in geometry.Surfaces)
{
// Outerloop
writer.WriteLine($"Surface: {surface.Name}");
writer.WriteLine($"OuterLoop: {surface.OuterLoop.Name}");
foreach (GeometryCurve curve in surface.OuterLoop.CurveList)
{
writer.WriteLine($"({curve.HeadPoint.X}, {curve.HeadPoint.Z}) - ({curve.EndPoint.X}, {curve.EndPoint.Z})");
}
writer.WriteLine("");
// Innerloops
foreach (GeometryLoop innerLoop in surface.InnerLoops)
{
writer.WriteLine($"InnerLoop: {innerLoop.Name}");
foreach (GeometryCurve curve in innerLoop.CurveList)
{
writer.WriteLine($"Curve: ({curve.HeadPoint.X}, {curve.HeadPoint.Z}) - ({curve.EndPoint.X}, {curve.EndPoint.Z})");
}
writer.WriteLine("");
}
}
writer.WriteLine("[END SURFACES]");
}
}
public static void ExportSurfacelineToFile(GeometryPointString surfaceLine, string fileName)
{
using (var writer = new StreamWriter(fileName))
{
// Export calc points
writer.WriteLine("[BEGIN CALC POINTS] " + surfaceLine.Points.Count);
foreach (Point2D point in surfaceLine.Points)
{
writer.WriteLine($"{point.X}, {point.Z}");
}
writer.WriteLine("[END CALC POINTS]");
}
}
///
/// Export all geometry items to a file including newly created points and curves and inner loops
///
///
///
public static void ExportFullGeometryToFile(GeometryData geometry, string fileName)
{
using (var writer = new StreamWriter(fileName))
{
// Export newly effected points
writer.WriteLine("[BEGIN NEWLY EFFECTED POINTS]");
foreach (Point2D point in geometry.NewlyEffectedPoints)
{
writer.WriteLine($"{point.X}, {point.Z}");
}
writer.WriteLine("[END NEWLY EFFECTED POINTS]");
// Export newly effected curves
writer.WriteLine("[BEGIN NEWLY EFFECTED CURVES]");
foreach (GeometryCurve curve in geometry.NewlyEffectedCurves)
{
writer.WriteLine($"Curve: ({curve.HeadPoint.X}, {curve.HeadPoint.Z}) - ({curve.EndPoint.X}, {curve.EndPoint.Z})");
}
writer.WriteLine("[END NEWLY EFFECTED CURVES]");
// Export points
writer.WriteLine("[BEGIN POINTS]");
foreach (Point2D point in geometry.Points)
{
writer.WriteLine($"{point.X}, {point.Z}");
}
writer.WriteLine("[END POINTS]");
// Export curves
writer.WriteLine("[BEGIN CURVES]");
foreach (GeometryCurve curve in geometry.Curves)
{
writer.WriteLine($"Curve: ({curve.HeadPoint.X}, {curve.HeadPoint.Z}) - ({curve.EndPoint.X}, {curve.EndPoint.Z})");
}
writer.WriteLine("[END CURVES]");
// Export surfaces
writer.WriteLine("[BEGIN SURFACES]");
foreach (GeometrySurface surface in geometry.Surfaces)
{
// Outerloop
writer.WriteLine($"Surface: {surface.Name}");
writer.WriteLine($"OuterLoop: {surface.OuterLoop.Name}");
foreach (GeometryCurve curve in surface.OuterLoop.CurveList)
{
writer.WriteLine($"({curve.HeadPoint.X}, {curve.HeadPoint.Z}) - ({curve.EndPoint.X}, {curve.EndPoint.Z})");
}
writer.WriteLine("");
// Innerloops
foreach (GeometryLoop innerLoop in surface.InnerLoops)
{
writer.WriteLine($"InnerLoop: {innerLoop.Name}");
foreach (GeometryCurve curve in innerLoop.CurveList)
{
writer.WriteLine($"Curve: ({curve.HeadPoint.X}, {curve.HeadPoint.Z}) - ({curve.EndPoint.X}, {curve.EndPoint.Z})");
}
writer.WriteLine("");
}
}
writer.WriteLine("[END SURFACES]");
}
}
}