diff --git a/CincyLib/PressBrake/LowerTool.cs b/CincyLib/PressBrake/LowerTool.cs index 6d5384d..bea9551 100644 --- a/CincyLib/PressBrake/LowerTool.cs +++ b/CincyLib/PressBrake/LowerTool.cs @@ -1,108 +1,87 @@ using System; -using System.Diagnostics; +using System.Collections.Generic; using System.IO; -using System.Xml; +using System.Linq; +using System.Xml.Linq; namespace CincyLib.PressBrake { public class LowerTool { - /// - /// The name of the file - /// - public string Name; + public LowerTool() + { + Segments = new List(); + } - public string ToolName; - public int Type; - public double Length; - public double Height; - public double Clearance; - public double MaxLoad; - public double Angle; - public double Radius; - public double VOpening; - public double Offset; + public string FilePath { get; set; } + public List Segments { get; set; } + public int Version { get; set; } + public string ToolName { get; set; } + public int Type { get; set; } + public double Length { get; set; } + public double Height { get; set; } + public double Clearance { get; set; } + public double MaxLoad { get; set; } + public double Angle { get; set; } + public double Radius { get; set; } + public double VOpening { get; set; } + public double Offset { get; set; } - public double BendRadius() + public double DevelopedRadius() { return VOpening * 0.15625; } + public bool IsSegmented + { + get { return Segments.Count > 0; } + } + public static LowerTool Load(string xmlpath) { - var lowerTool = new LowerTool(); - var reader = XmlReader.Create(xmlpath); + var stream = File.OpenRead(xmlpath); + var tool = Load(stream); + tool.FilePath = xmlpath; + return tool; + } - try + public static LowerTool Load(Stream stream) + { + var t = new LowerTool(); + + var xml = XDocument.Load(stream); + var data = xml.Root.Element("LowerTool"); + + t.Version = data.Attribute("Version").ToInt(); + t.ToolName = data.Attribute("ToolName")?.Value; + t.Type = data.Attribute("Type").ToInt(); + t.Height = data.Attribute("Height").ToDouble(); + t.Clearance = data.Attribute("Clearance").ToDouble(); + t.MaxLoad = data.Attribute("MaxLoad").ToDouble(); + t.Angle = data.Attribute("Angle").ToDouble(); + t.Radius = data.Attribute("Radius").ToDouble(); + t.VOpening = data.Attribute("VeeOpening").ToDouble(); + + foreach (var item in data.Element("SegmentList").Descendants("ToolSeg")) { - while (reader.Read()) - { - if (reader.IsStartElement()) - { - switch (reader.Name) - { - case "LowerTool": - lowerTool.Name = Path.GetFileNameWithoutExtension(xmlpath); - lowerTool.ToolName = reader.GetAttribute("ToolName"); + var seg = new ToolSegment(); + seg.Length = item.Attribute("Length").ToDouble(); + seg.Quantity = item.Attribute("Quantity").ToInt(1); - int type; - int.TryParse(reader.GetAttribute("Type"), out type); - lowerTool.Type = type; - - double length; - double.TryParse(reader.GetAttribute("Length"), out length); - lowerTool.Length = length; - - double height; - double.TryParse(reader.GetAttribute("Height"), out height); - lowerTool.Height = height; - - double clearance; - double.TryParse(reader.GetAttribute("Clearance"), out clearance); - lowerTool.Clearance = clearance; - - double maxload; - double.TryParse(reader.GetAttribute("MaxLoad"), out maxload); - lowerTool.MaxLoad = maxload; - - double angle; - double.TryParse(reader.GetAttribute("Angle"), out angle); - lowerTool.Angle = angle; - - double radius; - double.TryParse(reader.GetAttribute("Radius"), out radius); - lowerTool.Radius = radius; - - double vopening; - double.TryParse(reader.GetAttribute("VeeOpening"), out vopening); - lowerTool.VOpening = vopening; - - double offset; - double.TryParse(reader.GetAttribute("Offset"), out offset); - lowerTool.Offset = offset; - - break; - } - } - } - } - catch (SystemException ex) - { - Debug.WriteLine("Error loading: " + xmlpath); - Debug.WriteLine(ex.Message); - } - finally - { - if (reader != null) - reader.Close(); + t.Segments.Add(seg); } - return lowerTool; + var totalSegLength = t.Segments.Sum(i => i.Length * i.Quantity); + + if (totalSegLength > 0) + t.Length = totalSegLength; + + return t; } public void Print() { - Console.WriteLine(Name); + Console.WriteLine(ToolName); Console.WriteLine(" Length: {0}", Length); Console.WriteLine(" Angle: {0}", Angle); Console.WriteLine(" Radius: {0}", Radius); @@ -112,7 +91,7 @@ namespace CincyLib.PressBrake Console.WriteLine(" MaxLoad: {0}", MaxLoad); Console.WriteLine(" Offset: {0}", Offset); Console.WriteLine(" Type: {0}", Type); - Console.WriteLine(" Bend Radius: {0}", BendRadius()); + Console.WriteLine(" Bend Radius: {0}", DevelopedRadius()); } } } diff --git a/CincyLib/PressBrake/ToolSet.cs b/CincyLib/PressBrake/ToolSet.cs index c50a85a..9166126 100644 --- a/CincyLib/PressBrake/ToolSet.cs +++ b/CincyLib/PressBrake/ToolSet.cs @@ -23,7 +23,7 @@ namespace CincyLib.PressBrake public double BendRadius() { - double r1 = lowerTool.BendRadius(); + double r1 = lowerTool.DevelopedRadius(); double r2 = upperTool.Radius; return r1 > r2 ? r1 : r2; } @@ -48,7 +48,7 @@ namespace CincyLib.PressBrake public void Print() { - Console.WriteLine("{0} / {1}", lowerTool.Name, upperTool.Name); + Console.WriteLine("{0} / {1}", lowerTool.ToolName, upperTool.ToolName); Console.WriteLine(" Generates an inside radius of: {0}", BendRadius().ToString("n3") + "\""); Console.WriteLine(" Usable on materials less than or equal to: {0}", BendRadius().ToString("n3") + "\""); Console.WriteLine(" Capable of bends greater than or equal to: {0}", lowerTool.Angle.ToString("n0") + " degrees"); diff --git a/CincyLib/PressBrake/UpperTool.cs b/CincyLib/PressBrake/UpperTool.cs index 6026dc6..e2ae9fb 100644 --- a/CincyLib/PressBrake/UpperTool.cs +++ b/CincyLib/PressBrake/UpperTool.cs @@ -1,99 +1,83 @@ using System; -using System.Diagnostics; +using System.Collections.Generic; using System.IO; -using System.Xml; +using System.Linq; +using System.Xml.Linq; namespace CincyLib.PressBrake { public class UpperTool { - /// - /// The name of the file - /// - public string Name; + public UpperTool() + { + Segments = new List(); + } - public string ToolName; - public double Length; - public double Angle; - public double Radius; - public double Height; - public double Clearance; - public double MaxLoad; - public double Offset; - public int Type; + public List Segments { get; set; } + public string FilePath { get; set; } + public int Version { get; set; } + public string ToolName { get; set; } + public double Length { get; set; } + public double Angle { get; set; } + public double Radius { get; set; } + public double Height { get; set; } + public double Clearance { get; set; } + public double MaxLoad { get; set; } + public double Offset { get; set; } + public int Type { get; set; } + + public bool IsSegmented + { + get { return Segments.Count > 0; } + } public static UpperTool Load(string xmlpath) + { + var stream = File.OpenRead(xmlpath); + var tool = Load(stream); + tool.FilePath = xmlpath; + return tool; + } + + public static UpperTool Load(Stream stream) { var upperTool = new UpperTool(); - var reader = XmlReader.Create(xmlpath); - try + var xml = XDocument.Load(stream); + var data = xml.Root.Element("UpperTool"); + + upperTool.Version = data.Attribute("Version").ToInt(); + upperTool.ToolName = data.Attribute("ToolName")?.Value; + upperTool.Type = data.Attribute("Type").ToInt(); + upperTool.Length = data.Attribute("Length").ToDouble(); + upperTool.Height = data.Attribute("Height").ToDouble(); + upperTool.Clearance = data.Attribute("Clearance").ToDouble(); + upperTool.MaxLoad = data.Attribute("MaxLoad").ToDouble(); + upperTool.Angle = data.Attribute("Angle").ToDouble(); + upperTool.Radius = data.Attribute("Radius").ToDouble(); + upperTool.Offset = data.Attribute("Radius").ToDouble(); + + foreach (var item in data.Element("SegmentList").Descendants("ToolSeg")) { - while (reader.Read()) - { - if (reader.IsStartElement()) - { - switch (reader.Name) - { - case "UpperTool": - upperTool.Name = Path.GetFileNameWithoutExtension(xmlpath); - upperTool.ToolName = reader.GetAttribute("ToolName"); + var seg = new ToolSegment(); + seg.Length = item.Attribute("Length").ToDouble(); + seg.Quantity = item.Attribute("Quantity").ToInt(1); - int type; - int.TryParse(reader.GetAttribute("Type"), out type); - upperTool.Type = type; - - double length; - double.TryParse(reader.GetAttribute("Length"), out length); - upperTool.Length = length; - - double height; - double.TryParse(reader.GetAttribute("Height"), out height); - upperTool.Height = height; - - double clearance; - double.TryParse(reader.GetAttribute("Clearance"), out clearance); - upperTool.Clearance = clearance; - - double maxload; - double.TryParse(reader.GetAttribute("MaxLoad"), out maxload); - upperTool.MaxLoad = maxload; - - double angle; - double.TryParse(reader.GetAttribute("Angle"), out angle); - upperTool.Angle = angle; - - double radius; - double.TryParse(reader.GetAttribute("Radius"), out radius); - upperTool.Radius = radius; - - double offset; - double.TryParse(reader.GetAttribute("Offset"), out offset); - upperTool.Offset = offset; - - break; - } - } - } - } - catch (SystemException ex) - { - Debug.WriteLine("Error loading: " + xmlpath); - Debug.WriteLine(ex.Message); - } - finally - { - if (reader != null) - reader.Close(); + upperTool.Segments.Add(seg); } + var totalSegLength = upperTool.Segments.Sum(i => i.Length * i.Quantity); + + if (totalSegLength > 0) + upperTool.Length = totalSegLength; + return upperTool; } public void Print() { - Console.WriteLine(Name); + Console.WriteLine(ToolName); Console.WriteLine(" Length: {0}", Length); Console.WriteLine(" Angle: {0}", Angle); Console.WriteLine(" Radius: {0}", Radius); @@ -104,4 +88,10 @@ namespace CincyLib.PressBrake Console.WriteLine(" Type: {0}", Type); } } + + public class ToolSegment + { + public double Length { get; set; } + public int Quantity { get; set; } + } }