Files
PepApi.Core/PepLib.Core/IO/NestReader.cs
AJ 7c5b4ded5f chore(PepLib.Core): remove unused using directives and clean up formatting
Remove unnecessary System, System.Collections.Generic, System.IO, and
System.Linq using directives that were flagged by IDE analyzers. Also
includes minor whitespace and code style normalization.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-14 07:52:17 -05:00

148 lines
4.2 KiB
C#

using System.Diagnostics;
using System.IO.Compression;
using System.Text;
using System.Text.RegularExpressions;
namespace PepLib.IO
{
public sealed class NestReader
{
public Nest Nest { get; private set; }
private readonly Dictionary<string, Stream> plates;
private readonly Dictionary<string, Stream> loops;
public NestReader()
: this(new Nest())
{
}
public NestReader(Nest nest)
{
Nest = nest;
plates = new Dictionary<string, Stream>();
loops = new Dictionary<string, Stream>();
}
public void Read(Stream stream)
{
const string plateExtensionPattern = "plate-\\d\\d\\d";
const string loopExtensionPattern = "loop-\\d\\d\\d";
using (var zip = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true))
{
foreach (var entry in zip.Entries)
{
using var entryStream = entry.Open();
var memstream = new MemoryStream();
entryStream.CopyTo(memstream);
memstream.Seek(0, SeekOrigin.Begin);
var extension = Path.GetExtension(entry.FullName);
switch (extension)
{
case ".report":
LoadReport(memstream);
memstream.Close();
continue;
case ".dwg-info":
LoadDrawingInfo(memstream);
memstream.Close();
continue;
default:
Debug.WriteLine("Unknown file: " + entry.FullName);
break;
}
if (Regex.IsMatch(extension, loopExtensionPattern))
loops.Add(entry.FullName, memstream);
else if (Regex.IsMatch(extension, plateExtensionPattern))
plates.Add(entry.FullName, memstream);
}
}
foreach (var loop in loops)
Nest.Loops.Add(ReadLoop(loop.Key, loop.Value));
Nest.ResolveLoops();
foreach (var plate in plates)
Nest.Plates.Add(ReadPlate(plate.Key, plate.Value));
}
public void Read(string nestFile)
{
if (!File.Exists(nestFile))
{
var msg = string.Format("File Not Found: {0}", nestFile);
throw new FileNotFoundException(msg);
}
Stream stream = null;
try
{
stream = new FileStream(nestFile, FileMode.Open);
Read(stream);
}
finally
{
if (stream != null)
stream.Close();
}
}
private void LoadReport(Stream stream)
{
try
{
Nest.Report = Report.Load(stream);
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
Debug.WriteLine(exception.StackTrace);
}
}
private void LoadDrawingInfo(Stream stream)
{
var buffer = new byte[2000];
while (stream.Read(buffer, 0, buffer.Length) > 0)
{
var name = Encoding.Default.GetString(buffer, 200, 200).Trim();
var qty = BitConverter.ToInt32(buffer, 432);
var drawing = new NestDrawing
{
Name = name,
QtyRequired = qty
};
Nest.Drawings.Add(drawing);
}
}
private Loop ReadLoop(string name, Stream stream)
{
var reader = new LoopReader();
reader.Read(name, stream);
return reader.Loop;
}
private Plate ReadPlate(string name, Stream stream)
{
var reader = new PlateReader();
reader.Read(name, stream, Nest);
return reader.Plate;
}
}
}