using System; using System.Collections.Generic; using System.IO; using PepLib.Codes; using PepLib.IO; namespace PepLib { public class Nest { public Nest() { Report = new Report(); Loops = new List(); Plates = new List(); Drawings = new List(); } public Report Report { get; set; } public List Loops { get; set; } public List Plates { get; set; } public List 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; } } }