Files
PepApi.Core/PepLib.Core/IO/NestReader.cs
AJ Isaacs 61866df17e refactor(pep-lib): replace DotNetZip with System.IO.Compression and refactor readers
- Remove DotNetZip package from PepLib.Core.csproj
- Update DrawingReader, NestReader, and ZipHelper to use System.IO.Compression.ZipArchive
- Simplify stream handling and improve resource disposal
- Keep behavior consistent for loop/plate detection and extraction
2025-10-29 11:07:22 -04:00

151 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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;
}
}
}