Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRisenPhoenix committed Dec 7, 2021
0 parents commit 27bc341
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# IntelliJ related
*.iml
*.ipr
*.iws
.idea/
bin/
obj/
16 changes: 16 additions & 0 deletions MPS2ASC.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mps2asc", "mps2asc\mps2asc.csproj", "{FE61F52C-A24F-425A-9598-98A03D708CA9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FE61F52C-A24F-425A-9598-98A03D708CA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE61F52C-A24F-425A-9598-98A03D708CA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE61F52C-A24F-425A-9598-98A03D708CA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE61F52C-A24F-425A-9598-98A03D708CA9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "6.0",
"rollForward": "latestMajor",
"allowPrerelease": true
}
}
47 changes: 47 additions & 0 deletions mps2asc/MpsParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;

namespace mps2asc
{
public static class MpsParser
{
public static List<Point> ParseXml(string path)
{
XmlDocument doc = new();
doc.Load(path);

var points = new List<Point>();

var nodes = doc.DocumentElement?.SelectSingleNode("/point_set_file/point_set/time_series")?.SelectNodes("point");
if (nodes != null)
{
foreach (XmlNode n in nodes)
{
var xNode = n.SelectSingleNode("x");
var yNode = n.SelectSingleNode("y");
var zNode = n.SelectSingleNode("z");

if (xNode != null && yNode != null && zNode != null)
{
var x = double.Parse(xNode.InnerText, CultureInfo.InvariantCulture);
var y = double.Parse(yNode.InnerText, CultureInfo.InvariantCulture);
var z = double.Parse(zNode.InnerText, CultureInfo.InvariantCulture);
points.Add(new Point(x,y,z));
}
else
{
Console.WriteLine("Point not matching expected structure...");
}
}
}
else
{
Console.WriteLine("Document not matching expected structure...");
}

return points;
}
}
}
23 changes: 23 additions & 0 deletions mps2asc/Point.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Globalization;

namespace mps2asc
{
public class Point
{
public Point(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}

public double X { get; }
public double Y { get; }
public double Z { get; }

public override string ToString()
{
return $"{X.ToString(CultureInfo.InvariantCulture)} {Y.ToString(CultureInfo.InvariantCulture)} {Z.ToString(CultureInfo.InvariantCulture)}";
}
}
}
39 changes: 39 additions & 0 deletions mps2asc/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.IO;
using System.Linq;
using mps2asc;

class Program
{
static void Main()
{
var args = Environment.GetCommandLineArgs();
string path = "";
if (args.Length >= 2)
{
path = args[1];
}

while (!File.Exists(path))
{
Console.WriteLine("Couldn't find the file. Please enter the path to the file (e.g. 'C:\\Users\\X\\Desktop\\file.mps')");
path = Console.ReadLine() ?? string.Empty;
}
var outputFile = Path.ChangeExtension(path, "asc");

var points = MpsParser.ParseXml(path);

if (points.Count == 0)
{
Console.WriteLine("Apparently, something went wrong parsing...");
Console.Read();
}
else
{
var stringifyPoints = points.Select(p => p.ToString());
File.WriteAllLines(outputFile, stringifyPoints);

Console.WriteLine($"{points.Count} points written to {outputFile}");
}
}
}
10 changes: 10 additions & 0 deletions mps2asc/mps2asc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

0 comments on commit 27bc341

Please sign in to comment.