Files
PepApi.Core/PepLib.Core/Nest.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

132 lines
2.9 KiB
C#

using PepLib.Codes;
using PepLib.IO;
namespace PepLib
{
public class Nest
{
public Nest()
{
Report = new Report();
Loops = new List<Loop>();
Plates = new List<Plate>();
Drawings = new List<NestDrawing>();
}
public Report Report { get; set; }
public List<Loop> Loops { get; set; }
public List<Plate> Plates { get; set; }
public List<NestDrawing> Drawings { get; set; }
public void ResolveLoops()
{
for (int i = 0; i < Loops.Count; ++i)
{
var loop = Loops[i];
ResolveLoops(loop);
}
}
private void ResolveLoops(Program pgm)
{
for (int i = 0; i < pgm.Count; ++i)
{
var code = pgm[i];
if (code.CodeType() != CodeType.SubProgramCall)
continue;
var subpgmcall = (SubProgramCall)code;
var loop = GetLoop(subpgmcall.LoopId);
if (loop == null)
throw new Exception("Loop not found");
subpgmcall.Loop = loop;
}
}
public int GetQtyNested(string drawing)
{
int qty = 0;
foreach (var plate in Plates)
qty += plate.GetQtyNested(drawing);
return qty;
}
private Loop GetLoop(string name)
{
for (int i = 0; i < Loops.Count; ++i)
{
if (Loops[i].Name == name)
return Loops[i];
}
return null;
}
public Loop GetLoop(int id)
{
var ext = $".loop-{id.ToString().PadLeft(3, '0')}";
for (int i = 0; i < Loops.Count; ++i)
{
if (Loops[i].Name.EndsWith(ext))
return Loops[i];
}
return null;
}
public static Nest Load(string nestfile)
{
var reader = new NestReader();
reader.Read(nestfile);
return reader.Nest;
}
public static Nest Load(Stream stream)
{
var reader = new NestReader();
reader.Read(stream);
return reader.Nest;
}
public static bool TryLoad(string nestfile, out Nest nest)
{
try
{
nest = Load(nestfile);
}
catch (Exception)
{
nest = null;
return false;
}
return true;
}
public static bool TryLoad(Stream stream, out Nest nest)
{
try
{
nest = Load(stream);
}
catch (Exception)
{
nest = null;
return false;
}
return true;
}
}
}