Added Files

This commit is contained in:
2025-10-27 18:48:23 -04:00
commit ab916dc82a
89 changed files with 7575 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
using System;
using System.IO;
using System.Text;
namespace PepLib.IO
{
public sealed class DrawingInfoReader
{
public DrawingInfo Info { get; private set; }
public DrawingInfoReader()
{
Info = new DrawingInfo();
}
public DrawingInfoReader(DrawingInfo info)
{
Info = info;
}
public void Read(Stream stream)
{
Info.Name = ReadString(0xC8, ref stream);
Info.Revision = ReadString(0x20, ref stream);
Info.CreationDate = DateTime.Parse(ReadString(0xA, ref stream));
Info.LastModifiedDate = DateTime.Parse(ReadString(0xA, ref stream));
Info.LastReferenceDate = DateTime.Parse(ReadString(0xA, ref stream));
Info.Description = ReadString(0xC8, ref stream);
Info.Customer = ReadString(0x40, ref stream);
Info.Comment = ReadString(0x40, ref stream);
Info.Notes = ReadString(0x400, ref stream);
Info.Grain = (GrainType)ReadByte(ref stream);
stream.Seek(0x9, SeekOrigin.Current);
Info.MaterialNumber = int.Parse(ReadString(0x40, ref stream));
Info.MaterialGrade = ReadString(0x10, ref stream);
Info.ProgrammedBy = ReadString(0x40, ref stream);
Info.CreatedBy = ReadString(0x40, ref stream);
Info.Type = (DrawingType)ReadByte(ref stream);
stream.Seek(0x4, SeekOrigin.Current);
Info.Errors = ReadString(0x64, ref stream);
Info.Hardness = ReadString(0x20, ref stream);
Info.Specification = ReadString(0x40, ref stream);
stream.Seek(0x2, SeekOrigin.Current);
Info.UserDefined1 = ReadString(0x20, ref stream);
Info.UserDefined2 = ReadString(0x20, ref stream);
Info.UserDefined3 = ReadString(0x20, ref stream);
Info.UserDefined4 = ReadString(0x40, ref stream);
Info.UserDefined5 = ReadString(0x40, ref stream);
Info.UserDefined6 = ReadString(0x40, ref stream);
Info.MachineNumber = ReadByte(ref stream);
stream.Seek(0x1, SeekOrigin.Current);
Info.Application = (ApplicationType)ReadByte(ref stream);
}
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;
string name;
try
{
ZipHelper.ExtractByExtension(nestFile, ".dir", out name, out stream);
Read(stream);
}
finally
{
if (stream != null)
stream.Close();
}
}
private static string ReadString(int length, ref Stream stream)
{
var buffer = new byte[length];
stream.Read(buffer, 0, length);
return Encoding.Default.GetString(buffer).Trim();
}
private static byte ReadByte(ref Stream stream)
{
var buffer = new byte[0x1];
stream.Read(buffer, 0, 1);
return buffer[0];
}
}
}

View File

@@ -0,0 +1,113 @@
using Ionic.Zip;
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace PepLib.IO
{
public sealed class DrawingReader
{
public Drawing Drawing { get; private set; }
public DrawingReader()
{
Drawing = new Drawing();
}
public DrawingReader(Drawing drawing)
{
Drawing = drawing;
}
public void Read(Stream stream)
{
var zipStream = new ZipInputStream(stream);
ZipEntry theEntry;
while ((theEntry = zipStream.GetNextEntry()) != null)
{
var size = 2048;
var data = new byte[size];
var memstream = new MemoryStream();
while (true)
{
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
{
memstream.Write(data, 0, size);
memstream.Flush();
}
else break;
}
memstream.Seek(0, SeekOrigin.Begin);
var extension = Path.GetExtension(theEntry.FileName);
switch (extension)
{
case ".dir":
LoadInfo(memstream);
memstream.Close();
continue;
}
if (Regex.IsMatch(extension, "loop-\\d\\d\\d"))
Drawing.Loops.Add(ReadLoop(theEntry.FileName, memstream));
memstream.Close();
}
zipStream.Close();
Drawing.ResolveLoops();
}
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 LoadInfo(Stream stream)
{
try
{
Drawing.Info = DrawingInfo.Load(stream);
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
Debug.WriteLine(exception.StackTrace);
}
}
private Loop ReadLoop(string name, Stream stream)
{
var reader = new LoopReader();
reader.Read(name, stream);
return reader.Loop;
}
}
}

View File

@@ -0,0 +1,134 @@
using System;
using System.IO;
using PepLib.Codes;
namespace PepLib.IO
{
internal sealed class LoopReader
{
public Loop Loop { get; private set; }
public LoopReader()
{
Loop = new Loop();
}
public LoopReader(Loop loop)
{
Loop = loop;
}
public void Read(string name, Stream stream)
{
var pgm = Program.Load(stream);
Loop.Name = name;
Loop.AddRange(pgm);
LoadInfo();
}
private void LoadInfo()
{
for (int i = Loop.Count - 1; i >= 0; --i)
{
var code = Loop[i];
if (code.CodeType() != CodeType.Comment)
continue;
var comment = (Comment)code;
if (LoadInfo(comment.Value))
Loop.RemoveAt(i);
}
}
private bool LoadInfo(string value)
{
if (value.StartsWith("REF"))
{
ParseReferenceData(value);
return true;
}
if (value.StartsWith("DRAWING"))
{
ParseDrawingData(value);
return true;
}
if (value.StartsWith("DXF"))
{
ParseDxfData(value);
return true;
}
return false;
}
private void ParseReferenceData(string data)
{
var parts = data.Split(',');
if (parts.Length != 3)
return;
int xindex = parts[0].IndexOf('X');
parts[0] = parts[0].Remove(0, xindex);
double x = 0;
double y = 0;
var xsplit = parts[0].Split('=');
if (xsplit.Length == 2)
x = ReadDouble(xsplit[1]);
var ysplit = parts[1].Split('=');
if (ysplit.Length == 2)
y = ReadDouble(ysplit[1]);
var datesplit = parts[2].Split('=');
if (datesplit.Length == 2)
{
DateTime date;
DateTime.TryParse(datesplit[1], out date);
Loop.LastReferenceDate = date;
}
Loop.ReferencePoint = new Vector(x, y);
}
private void ParseDrawingData(string data)
{
var index = data.IndexOf('=');
if (index == -1)
Loop.DrawingName = string.Empty;
Loop.DrawingName = data.Remove(0, index + 1).Trim();
}
private void ParseDxfData(string data)
{
var index = data.IndexOf('=');
if (index == -1)
Loop.DxfPath = string.Empty;
Loop.DxfPath = data.Remove(0, index + 1).Trim();
}
private static double ReadDouble(string s, double defaultValue = 0.0)
{
double f;
if (!double.TryParse(s, out f))
return defaultValue;
return f;
}
}
}

View File

@@ -0,0 +1,90 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace PepLib.IO
{
public class MaterialDataReader
{
public List<MaterialData> Materials { get; set; }
public MaterialDataReader()
{
Materials = new List<MaterialData>();
}
public void Read(Stream stream)
{
const int dataLength = 2000;
var count = stream.Length / dataLength;
var binreader = new BinaryReader(stream);
for (int i = 0; i < count; i++)
{
var data = new MaterialData();
int id;
int.TryParse(ReadString(64, ref stream), out id);
data.Number = id;
data.Grade = ReadString(16, ref stream);
data.Name = ReadString(200, ref stream);
data.Density = binreader.ReadDouble();
stream.Seek(8, SeekOrigin.Current);
data.Thickness = binreader.ReadDouble();
Materials.Add(data);
stream.Position = i * dataLength;
}
}
public void Read(string file)
{
if (!File.Exists(file))
{
var msg = string.Format("File Not Found: {0}", file);
throw new FileNotFoundException(msg);
}
Stream stream = null;
try
{
stream = File.OpenRead(file);
Read(stream);
}
finally
{
if (stream != null)
stream.Close();
}
}
private static string ReadString(int length, ref Stream stream)
{
var buffer = new byte[length];
stream.Read(buffer, 0, length);
return Encoding.Default.GetString(buffer).Trim();
}
private static byte ReadByte(ref Stream stream)
{
var buffer = new byte[0x1];
stream.Read(buffer, 0, 1);
return buffer[0];
}
}
public class MaterialData
{
public int Number { get; set; }
public string Name { get; set; }
public string Grade { get; set; }
public double Density { get; set; }
public double Thickness { get; set; }
}
}

View File

@@ -0,0 +1,124 @@
using System;
using System.IO;
using System.Text;
namespace PepLib.IO
{
internal sealed class NestInfoReader
{
public NestInfo Info { get; private set; }
public NestInfoReader()
{
Info = new NestInfo();
}
public NestInfoReader(NestInfo info)
{
Info = info;
}
public void Read(Stream stream)
{
var binReader = new BinaryReader(stream);
Info.Name = ReadString(0xC8, ref stream);
Info.DateCreated = DateTime.Parse(ReadString(0xA, ref stream));
Info.DateLastModified = DateTime.Parse(ReadString(0xA, ref stream));
Info.LoopCount = binReader.ReadInt16();
Info.ProgramCount = binReader.ReadInt16();
Info.Customer = ReadString(0x40, ref stream);
Info.ProgrammedBy = ReadString(0x40, ref stream);
Info.Comments = ReadString(0x40, ref stream);
// skip 2 bytes
stream.Seek(0x2, SeekOrigin.Current);
Info.MaterialNumber = int.Parse(ReadString(0x40, ref stream));
Info.MaterialGrade = ReadString(0x10, ref stream);
// skip 2 bytes
stream.Seek(0x2, SeekOrigin.Current);
Info.Notes = ReadString(0x400, ref stream);
Info.PostedAs = ReadString(0x64, ref stream);
Info.Errors = ReadString(0x64, ref stream);
Info.UserDefined1 = ReadString(0x20, ref stream);
Info.UserDefined2 = ReadString(0x20, ref stream);
Info.UserDefined3 = ReadString(0x20, ref stream);
Info.UserDefined4 = ReadString(0x40, ref stream);
Info.UserDefined5 = ReadString(0x40, ref stream);
Info.UserDefined6 = ReadString(0x40, ref stream);
Info.DefaultPlateSize = ReadString(0x1E, ref stream);
Info.Kerf = ReadString(0x3, ref stream);
// skip 4 bytes
stream.Seek(0x4, SeekOrigin.Current);
switch (ReadByte(ref stream))
{
case 0:
Info.Status = StatusType.ToBeCut;
break;
case 1:
Info.Status = StatusType.Quote;
break;
case 2:
Info.Status = StatusType.HasBeenCut;
break;
case 3:
Info.Status = StatusType.Temp;
break;
default:
Info.Status = StatusType.ToBeCut;
break;
}
// skip 16 bytes
stream.Seek(16, SeekOrigin.Current);
Info.PlateCount = binReader.ReadInt16();
}
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;
string name;
try
{
ZipHelper.ExtractByExtension(nestFile, ".dir", out name, out stream);
Read(stream);
}
finally
{
if (stream != null)
stream.Close();
}
}
private static string ReadString(int length, ref Stream stream)
{
var buffer = new byte[length];
stream.Read(buffer, 0, length);
return Encoding.Default.GetString(buffer).Trim();
}
private static byte ReadByte(ref Stream stream)
{
var buffer = new byte[0x1];
stream.Read(buffer, 0, 1);
return buffer[0];
}
}
}

View File

@@ -0,0 +1,165 @@
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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";
var zipStream = new ZipInputStream(stream);
ZipEntry theEntry;
while ((theEntry = zipStream.GetNextEntry()) != null)
{
var size = 2048;
var data = new byte[size];
var memstream = new MemoryStream();
while (true)
{
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
{
memstream.Write(data, 0, size);
memstream.Flush();
}
else break;
}
memstream.Seek(0, SeekOrigin.Begin);
var extension = Path.GetExtension(theEntry.FileName);
switch (extension)
{
case ".report":
LoadReport(memstream);
memstream.Close();
continue;
case ".dwg-info":
LoadDrawingInfo(memstream);
memstream.Close();
continue;
default:
Debug.WriteLine("Unknown file: " + theEntry.FileName);
break;
}
if (Regex.IsMatch(extension, loopExtensionPattern))
loops.Add(theEntry.FileName, memstream);
else if (Regex.IsMatch(extension, plateExtensionPattern))
plates.Add(theEntry.FileName, memstream);
}
zipStream.Close();
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;
}
}
}

View File

@@ -0,0 +1,338 @@
using System.IO;
using PepLib.Codes;
namespace PepLib.IO
{
internal sealed class PlateReader
{
public Plate Plate { get; private set; }
public PlateReader()
{
Plate = new Plate();
Plate.Duplicates = 1;
}
public PlateReader(Plate plate)
{
Plate = plate;
}
public void Read(string name, Stream stream, Nest nest)
{
var pos = new Vector(0, 0);
var pgm = Program.Load(stream);
Plate.Name = name;
for (int i = 0; i < pgm.Count; i++)
{
var block = pgm[i];
switch (block.CodeType())
{
case CodeType.CircularMove:
{
var arc = (CircularMove)block;
pos = arc.EndPoint;
break;
}
case CodeType.LinearMove:
{
var line = (LinearMove)block;
pos = line.EndPoint;
break;
}
case CodeType.RapidMove:
{
var rapid = (RapidMove)block;
pos = rapid.EndPoint;
break;
}
case CodeType.Comment:
{
var comment = (Comment)block;
LoadInfo(comment.Value);
break;
}
case CodeType.SubProgramCall:
{
var subpgm = (SubProgramCall)block;
var loop = nest.GetLoop(subpgm.LoopId);
var part = Part.Create(loop, pos, AngleConverter.ToRadians(subpgm.Rotation));
var nextBlock = pgm[i + 1];
if (nextBlock.CodeType() == CodeType.Comment)
{
var comment = nextBlock as Comment;
if (comment.Value == "DISPLAY ONLY")
{
part.IsDisplayOnly = true;
i++;
}
}
Plate.Parts.Add(part);
break;
}
}
}
}
private void LoadInfo(string value)
{
if (value.StartsWith("POSTED FILES"))
ParsePostedFiles(value);
else if (value.StartsWith("HEAT LOT"))
ParseHeatLot(value);
else if (value.StartsWith("SPACING"))
ParseSpacing(value);
else if (value.StartsWith("CUT A TOTAL OF "))
ParseNumberOfDuplicates(value);
else if (value.StartsWith("EDGES,"))
ParseEdgeSpacing(value);
else if (value.StartsWith("PLATE SCALING"))
ParsePlateSize(value);
else if (value.StartsWith("MACHINE"))
ParseMachine(value);
else if (value.StartsWith("MATERIAL"))
ParseMaterial(value);
else if (value.StartsWith("GRADE"))
ParseGrade(value);
else if (value.StartsWith("DESCRIPTION"))
ParseDescription(value);
else if (value.StartsWith("PLATE THICKNESS"))
ParseThickness(value);
else if (value.StartsWith("DENSITY"))
ParseDensity(value);
else if (value.StartsWith("TORCHES"))
ParseTorchCount(value);
}
private void ParseNumberOfDuplicates(string data)
{
var parts = data.Split(' ');
if (parts.Length != 7)
return;
int dup;
int.TryParse(parts[4], out dup);
Plate.Duplicates = dup;
}
private void ParsePostedFiles(string data)
{
if (data.Length < 14)
return;
Plate.PostedFiles = data.Remove(0, 14).Trim();
}
private void ParseHeatLot(string data)
{
if (data.Length < 9)
return;
Plate.HeatLot = data.Remove(0, 9).Trim();
}
private void ParseSpacing(string data)
{
var parts = data.Split('=');
if (parts.Length != 2)
return;
double spacing;
double.TryParse(parts[1], out spacing);
Plate.PartSpacing = spacing;
}
private void ParseEdgeSpacing(string data)
{
var parts = data.Split(',');
if (parts.Length != 5)
return;
var leftSplit = parts[1].Split('=');
if (leftSplit.Length == 2)
{
double x;
double.TryParse(leftSplit[1], out x);
Plate.EdgeSpacing.Left = x;
}
var bottomSplit = parts[2].Split('=');
if (bottomSplit.Length == 2)
{
double x;
double.TryParse(bottomSplit[1], out x);
Plate.EdgeSpacing.Bottom = x;
}
var rightSplit = parts[3].Split('=');
if (rightSplit.Length == 2)
{
double x;
double.TryParse(rightSplit[1], out x);
Plate.EdgeSpacing.Right = x;
}
var topSplit = parts[4].Split('=');
if (topSplit.Length == 2)
{
double x;
double.TryParse(topSplit[1], out x);
Plate.EdgeSpacing.Top = x;
}
}
private void ParsePlateSize(string data)
{
var quadrantIndex = data.IndexOf("QUADRANT");
if (quadrantIndex != -1)
{
var plateData = data.Remove(quadrantIndex);
var plateDataSplit = plateData.Split('=');
if (plateDataSplit.Length == 2)
{
Size plateSize;
Size.TryParse(plateDataSplit[1], out plateSize);
Plate.Size = plateSize;
}
var quadrantData = data.Remove(0, quadrantIndex);
var quadrantDataSplit = quadrantData.Split('=');
if (quadrantDataSplit.Length == 2)
{
int quadrant;
int.TryParse(quadrantDataSplit[1], out quadrant);
Plate.Quadrant = quadrant;
}
}
else
{
var plateDataSplit = data.Split('=');
if (plateDataSplit.Length == 2)
{
Size plateSize;
Size.TryParse(plateDataSplit[1], out plateSize);
Plate.Size = plateSize;
}
}
}
private void ParseMachine(string data)
{
var parts = data.Split(',');
if (parts.Length != 2)
return;
var machineSplit = parts[0].Split('=');
if (machineSplit.Length == 2)
{
int num;
int.TryParse(machineSplit[1], out num);
Plate.Machine.Id = num;
}
Plate.Machine.Name = parts[1].Trim();
}
private void ParseMaterial(string data)
{
var parts = data.Split('=');
if (parts.Length != 2)
return;
int material;
int.TryParse(parts[1], out material);
Plate.Material.Id = material;
}
private void ParseGrade(string data)
{
var parts = data.Split('=');
if (parts.Length != 2)
return;
Plate.Material.Grade = parts[1].Trim();
}
private void ParseDescription(string data)
{
var parts = data.Split('=');
if (parts.Length != 2)
return;
Plate.Description = parts[1].Trim();
}
private void ParseThickness(string data)
{
var parts = data.Split('=');
if (parts.Length != 2)
return;
double thickness;
double.TryParse(parts[1], out thickness);
Plate.Thickness = thickness;
}
private void ParseDensity(string data)
{
var parts = data.Split('=');
if (parts.Length != 2)
return;
double density;
double.TryParse(parts[1], out density);
Plate.Material.Density = density;
}
private void ParseTorchCount(string data)
{
var parts = data.Split('=');
if (parts.Length != 2)
return;
int torchCount;
int.TryParse(parts[1], out torchCount);
Plate.TorchCount = torchCount;
}
}
}

View File

@@ -0,0 +1,404 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using PepLib.Codes;
namespace PepLib.IO
{
internal sealed class ProgramReader
{
private const int BufferSize = 200;
private int codeIndex;
private CodeBlock block;
private CodeSection section;
public Program Program { get; private set; }
public ProgramReader()
{
Program = new Program();
}
public ProgramReader(Program program)
{
Program = program;
}
public void Read(Stream stream)
{
foreach (var line in GetLines(stream))
{
block = ParseBlock(line);
ProcessCurrentBlock();
}
}
private IEnumerable<string> GetLines(Stream stream)
{
var buffer = new byte[BufferSize];
while (stream.Read(buffer, 0, BufferSize) > 0)
{
yield return Encoding.ASCII.GetString(buffer);
}
}
private CodeBlock ParseBlock(string line)
{
var block = new CodeBlock();
Code code = null;
for (int i = 0; i < line.Length; ++i)
{
var c = line[i];
if (char.IsLetter(c))
block.Add((code = new Code(c)));
else if (c == ':')
{
block.Add((new Code(c, line.Remove(0, i + 1).Trim())));
break;
}
else if (code != null)
code.Value += c;
}
return block;
}
private void ProcessCurrentBlock()
{
var code = GetFirstCode();
while (code != null)
{
switch (code.Id)
{
case ':':
Program.Add(new Comment(code.Value));
code = GetNextCode();
break;
case 'G':
int value = int.Parse(code.Value);
switch (value)
{
case 0:
case 1:
section = CodeSection.Line;
ReadLine(value == 0);
code = GetCurrentCode();
break;
case 2:
case 3:
section = CodeSection.Arc;
ReadArc(value == 2 ? RotationType.CW : RotationType.CCW);
code = GetCurrentCode();
break;
case 92:
section = CodeSection.SubProgram;
ReadSubProgram();
code = GetCurrentCode();
break;
case 40:
Program.Add(new SetKerf() { Kerf = KerfType.None });
code = GetNextCode();
break;
case 41:
Program.Add(new SetKerf() { Kerf = KerfType.Left });
code = GetNextCode();
break;
case 42:
Program.Add(new SetKerf() { Kerf = KerfType.Right });
code = GetNextCode();
break;
default:
code = GetNextCode();
break;
}
break;
case 'F':
Program.Add(new SetFeedrate() { Value = double.Parse(code.Value) });
code = GetNextCode();
break;
default:
code = GetNextCode();
break;
}
}
}
private void ReadLine(bool isRapid)
{
double x = 0;
double y = 0;
var type = EntityType.Cut;
while (section == CodeSection.Line)
{
var code = GetNextCode();
if (code == null)
{
section = CodeSection.Unknown;
break;
}
switch (code.Id)
{
case 'X':
x = double.Parse(code.Value);
break;
case 'Y':
y = double.Parse(code.Value);
break;
case ':':
{
var value = code.Value.Trim().ToUpper();
switch (value)
{
case "EXTERNAL LEAD-IN":
type = EntityType.ExternalLeadin;
break;
case "EXTERNAL LEAD-OUT":
type = EntityType.ExternalLeadout;
break;
case "INTERNAL LEAD-IN":
type = EntityType.InternalLeadin;
break;
case "INTERNAL LEAD-OUT":
type = EntityType.InternalLeadout;
break;
case "DISPLAY":
type = EntityType.Display;
break;
}
break;
}
default:
section = CodeSection.Unknown;
break;
}
}
if (isRapid)
Program.Add(new RapidMove(x, y));
else
Program.Add(new LinearMove(x, y) { Type = type });
}
private void ReadArc(RotationType rotation)
{
double x = 0;
double y = 0;
double i = 0;
double j = 0;
var type = EntityType.Cut;
while (section == CodeSection.Arc)
{
var code = GetNextCode();
if (code == null)
{
section = CodeSection.Unknown;
break;
}
switch (code.Id)
{
case 'X':
x = double.Parse(code.Value);
break;
case 'Y':
y = double.Parse(code.Value);
break;
case 'I':
i = double.Parse(code.Value);
break;
case 'J':
j = double.Parse(code.Value);
break;
case ':':
{
var value = code.Value.Trim().ToUpper();
switch (value)
{
case "EXTERNAL LEAD-IN":
type = EntityType.ExternalLeadin;
break;
case "EXTERNAL LEAD-OUT":
type = EntityType.ExternalLeadout;
break;
case "INTERNAL LEAD-IN":
type = EntityType.InternalLeadin;
break;
case "INTERNAL LEAD-OUT":
type = EntityType.InternalLeadout;
break;
case "DISPLAY":
type = EntityType.Display;
break;
}
break;
}
default:
section = CodeSection.Unknown;
break;
}
}
Program.Add(new CircularMove()
{
EndPoint = new Vector(x, y),
CenterPoint = new Vector(i, j),
Rotation = rotation,
Type = type
});
}
private void ReadSubProgram()
{
int l = 0;
int r = 0;
double p = 0;
while (section == CodeSection.SubProgram)
{
var code = GetNextCode();
if (code == null)
{
section = CodeSection.Unknown;
break;
}
switch (code.Id)
{
case 'L':
l = int.Parse(code.Value);
break;
case 'R':
r = int.Parse(code.Value);
break;
case 'P':
p = double.Parse(code.Value);
break;
default:
section = CodeSection.Unknown;
break;
}
}
Program.Add(new SubProgramCall() { LoopId = l, RepeatCount = r, Rotation = p });
}
private Code GetNextCode()
{
codeIndex++;
if (codeIndex >= block.Count)
return null;
return block[codeIndex];
}
private Code GetCurrentCode()
{
if (codeIndex >= block.Count)
return null;
return block[codeIndex];
}
private Code GetFirstCode()
{
if (block.Count == 0)
return null;
codeIndex = 0;
return block[codeIndex];
}
private class Code
{
public Code(char id)
{
Id = id;
Value = string.Empty;
}
public Code(char id, string value)
{
Id = id;
Value = value;
}
public char Id { get; private set; }
public string Value { get; set; }
public override string ToString()
{
return Id + Value;
}
}
private class CodeBlock : List<Code>
{
public void Add(char id, string value)
{
Add(new Code(id, value));
}
public override string ToString()
{
var builder = new StringBuilder();
foreach (var code in this)
builder.Append(code.ToString() + " ");
return builder.ToString();
}
}
private enum CodeSection
{
Unknown,
Arc,
Line,
SubProgram
}
}
}

View File

@@ -0,0 +1,378 @@
using System;
using System.Diagnostics;
using System.IO;
namespace PepLib.IO
{
internal sealed class ReportReader
{
public Report Report { get; private set; }
public ReportReader()
{
Report = new Report();
}
public ReportReader(Report report)
{
Report = report;
}
public void Read(Stream stream)
{
var reader = new StreamReader(stream);
Report.Drawing dwg = null;
Report.Plate plt = null;
var section = Section.Unknown;
string line;
while ((line = reader.ReadLine()) != null)
{
var equalIndex = line.IndexOf('=');
if (equalIndex != -1)
{
var valueIndex = equalIndex + 1;
var key = line.Substring(0, equalIndex).Trim();
var value = line.Substring(valueIndex, line.Length - valueIndex).Trim();
switch (section)
{
case Section.NestHeader:
ReadNestHeaderData(key, value);
break;
case Section.NestedPlates:
ReadNestedPlatesData(key, value, plt);
break;
case Section.QuantitiesNested:
ReadQuantitiesNestedData(key, value, dwg);
break;
case Section.Unknown:
break;
}
}
else
{
var category = line.Trim();
switch (category)
{
case "Nest header":
section = Section.NestHeader;
continue;
case "Nested plates":
section = Section.NestedPlates;
continue;
case "Quantities nested":
section = Section.QuantitiesNested;
continue;
}
switch (section)
{
case Section.NestedPlates:
if (category.StartsWith("Plate"))
Report.Plates.Add((plt = new Report.Plate()));
break;
case Section.QuantitiesNested:
if (category.StartsWith("Drawing"))
Report.Drawings.Add((dwg = new Report.Drawing()));
break;
default:
Debug.WriteLine("Unknown category: " + category);
break;
}
}
}
}
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;
string name;
try
{
ZipHelper.ExtractByExtension(nestFile, ".report", out name, out stream);
Read(stream);
}
finally
{
if (stream != null)
stream.Close();
}
}
private void ReadNestHeaderData(string key, string value)
{
switch (key)
{
case "Program":
Report.Name = value;
break;
case "Customer name":
Report.Customer = value;
break;
case "Date programmed":
DateTime date;
DateTime.TryParse(value, out date);
Report.DateProgrammed = date;
break;
case "Material":
Report.Material = value;
break;
case "Programmed by":
Report.ProgrammedBy = value;
break;
case "Machine":
Report.Machine = value;
break;
case "Comments":
Report.Comments = value;
break;
case "Remarks":
Report.Remarks = value;
break;
default:
Debug.WriteLine(string.Format("Report.ReadNestHeaderData: \"{0}\" not implemented", key));
break;
}
}
private void ReadNestedPlatesData(string key, string value, Report.Plate plt)
{
switch (key)
{
case "Plate number":
plt.Name = value;
break;
case "Thickness":
plt.Thickness = ParseDouble(value);
break;
case "Plate Size":
ReadPlateSize(value, plt);
break;
case "Material":
plt.MaterialNumber = ParseInt32(value);
break;
case "Grade":
plt.MaterialGrade = value;
break;
case "Material Description":
plt.MaterialDescription = value;
break;
case "Dup plates":
plt.Quantity = int.Parse(value);
break;
case "Plate Util":
plt.PlateUtilization = ParsePercent(value);
break;
case "Material Util":
plt.MaterialUtilization = ParsePercent(value);
break;
case "Total Area1":
plt.Area1 = ParseDouble(value);
break;
case "Total Area2":
plt.Area2 = ParseDouble(value);
break;
case "Bubble pierces":
plt.BubblePierceCount = ParseInt32(value);
break;
case "Total cutting time":
ReadCuttingTime(value, Report);
break;
case "Cutting feedrate":
Report.CutFeedrate = ParseInt32(value);
break;
case "Rapid feedrate":
Report.RapidFeedrate = ParseInt32(value);
break;
default:
Debug.WriteLine(string.Format("Report.ReadNestedPlatesData: \"{0}\" not implemented", key));
break;
}
}
private void ReadQuantitiesNestedData(string key, string value, Report.Drawing dwg)
{
switch (key)
{
case "Customer Name":
dwg.Customer = value;
break;
case "Drawing Name":
dwg.Name = value;
break;
case "Revision":
dwg.Revision = value;
break;
case "Qty Req":
dwg.QtyRequired = ParseInt32(value);
break;
case "Qty Nstd":
dwg.QtyNested = ParseInt32(value);
break;
case "# of Pierces":
dwg.PierceCount = ParseInt32(value);
break;
case "Intersections":
dwg.IntersectionCount = ParseInt32(value);
break;
case "Area1*":
dwg.Area1 = ParseDouble(value);
break;
case "Area2**":
dwg.Area2 = ParseDouble(value);
break;
case "% of Material":
dwg.PercentOfMaterial = ParsePercent(value);
break;
case "% of Time":
dwg.PercentOfCutTime = ParsePercent(value);
dwg.TotalCutTime =
TimeSpan.FromTicks((long)(Report.TotalCutTime.Ticks * dwg.PercentOfCutTime / 100.0));
break;
default:
Debug.WriteLine(string.Format("Report.ReadQuantitiesNestedData: \"{0}\" not implemented", key));
break;
}
}
private void ReadPlateSize(string value, Report.Plate plt)
{
var a = value.ToUpper().Split('X');
var x = float.Parse(a[0]);
var y = float.Parse(a[1]);
if (x < y)
{
plt.Width = x;
plt.Length = y;
}
else
{
plt.Width = y;
plt.Length = x;
}
}
private void ReadCuttingTime(string value, Report report)
{
var parts = value.Split(',');
int hrs = 0, min = 0, sec = 0;
foreach (var part in parts)
{
if (part.Contains("hr"))
hrs = int.Parse(part.Remove(part.IndexOf("hr")));
else if (part.Contains("min"))
min = int.Parse(part.Remove(part.IndexOf("min")));
else if (part.Contains("sec"))
sec = int.Parse(part.Remove(part.IndexOf("sec")));
}
report.TotalCutTime = new TimeSpan(hrs, min, sec);
}
private static double ParsePercent(string s, double defaultValue = 0.0)
{
var t = s.TrimEnd('%', ' ');
double f;
if (!double.TryParse(t, out f))
{
Debug.WriteLine("Failed to convert \"" + s + "\" from percent string to double");
return defaultValue;
}
return f;
}
private static double ParseDouble(string s, double defaultValue = 0.0)
{
double f;
if (!double.TryParse(s, out f))
{
Debug.WriteLine("Failed to convert \"" + s + "\" from string to double");
return defaultValue;
}
return f;
}
private static int ParseInt32(string s, int defaultValue = 0)
{
int i;
if (!int.TryParse(s, out i))
{
Debug.WriteLine("Failed to convert \"" + s + "\" from string to int");
return defaultValue;
}
return i;
}
private enum Section
{
Unknown,
NestHeader,
NestedPlates,
QuantitiesNested,
}
}
}