diff --git a/PepLib/Drawing.cs b/PepLib/Drawing.cs index 7dc816b..82a640b 100644 --- a/PepLib/Drawing.cs +++ b/PepLib/Drawing.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using PepLib.Codes; using PepLib.IO; namespace PepLib @@ -30,6 +31,58 @@ namespace PepLib return reader.Drawing; } + 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 Loop GetLoop(int id) + { + string name = GetLoopName(id); + return GetLoop(name); + } + + + private Loop GetLoop(string name) + { + for (int i = 0; i < Loops.Count; ++i) + { + if (Loops[i].Name == name) + return Loops[i]; + } + + return null; + } + + private string GetLoopName(int loopId) + { + return string.Format("{0}.loop-{1}", Info.Name, loopId.ToString().PadLeft(3, '0')); + } + public static bool TryLoad(string nestfile, out Drawing drawing) { try diff --git a/PepLib/IO/DrawingReader.cs b/PepLib/IO/DrawingReader.cs index b9396c0..153eda0 100644 --- a/PepLib/IO/DrawingReader.cs +++ b/PepLib/IO/DrawingReader.cs @@ -22,7 +22,6 @@ namespace PepLib.IO public void Read(Stream stream) { - var drawing = new Drawing(); var zipStream = new ZipInputStream(stream); ZipEntry theEntry; @@ -58,12 +57,14 @@ namespace PepLib.IO } if (Regex.IsMatch(extension, "loop-\\d\\d\\d")) - drawing.Loops.Add(ReadLoop(theEntry.FileName, memstream)); + Drawing.Loops.Add(ReadLoop(theEntry.FileName, memstream)); memstream.Close(); } zipStream.Close(); + + Drawing.ResolveLoops(); } public void Read(string nestFile) diff --git a/PepLib/PepLib.csproj b/PepLib/PepLib.csproj index e093a63..719ed4a 100644 --- a/PepLib/PepLib.csproj +++ b/PepLib/PepLib.csproj @@ -33,8 +33,8 @@ false - - ..\packages\DotNetZip.1.12.0\lib\net20\DotNetZip.dll + + ..\..\packages\DotNetZip.1.13.3\lib\net40\DotNetZip.dll diff --git a/PepLib/packages.config b/PepLib/packages.config index fa0019f..6438d2c 100644 --- a/PepLib/packages.config +++ b/PepLib/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file