From 2efa05777b159f1318567ae38ef4b61675d3eb36 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Tue, 30 Oct 2018 18:23:58 -0700 Subject: [PATCH] Improved press brake program reading --- CincyLib/CincyLib.csproj | 6 + CincyLib/PressBrake/Extensions.cs | 47 +++++ CincyLib/PressBrake/MatType.cs | 11 + CincyLib/PressBrake/Program.cs | 290 +-------------------------- CincyLib/PressBrake/ProgramReader.cs | 158 +++++++++++++++ CincyLib/PressBrake/SegEntry.cs | 7 + CincyLib/PressBrake/Step.cs | 32 +++ CincyLib/PressBrake/ToolSetup.cs | 24 +++ 8 files changed, 289 insertions(+), 286 deletions(-) create mode 100644 CincyLib/PressBrake/Extensions.cs create mode 100644 CincyLib/PressBrake/MatType.cs create mode 100644 CincyLib/PressBrake/ProgramReader.cs create mode 100644 CincyLib/PressBrake/SegEntry.cs create mode 100644 CincyLib/PressBrake/Step.cs create mode 100644 CincyLib/PressBrake/ToolSetup.cs diff --git a/CincyLib/CincyLib.csproj b/CincyLib/CincyLib.csproj index 2dd89b8..54b6749 100644 --- a/CincyLib/CincyLib.csproj +++ b/CincyLib/CincyLib.csproj @@ -38,7 +38,13 @@ + + + + + + diff --git a/CincyLib/PressBrake/Extensions.cs b/CincyLib/PressBrake/Extensions.cs new file mode 100644 index 0000000..91dfa12 --- /dev/null +++ b/CincyLib/PressBrake/Extensions.cs @@ -0,0 +1,47 @@ +using System; +using System.Xml.Linq; + +namespace CincyLib.PressBrake +{ + static class Extensions + { + public static bool ToBool(this XAttribute a, bool defaultValue = false) + { + if (a == null || string.IsNullOrWhiteSpace(a.Value)) + return defaultValue; + + int intValue; + + if (!int.TryParse(a.Value, out intValue)) + return defaultValue; + + return Convert.ToBoolean(intValue); + } + + public static int ToInt(this XAttribute a, int defaultValue = 0) + { + if (a == null || string.IsNullOrWhiteSpace(a.Value)) + return defaultValue; + + int intValue; + + if (!int.TryParse(a.Value, out intValue)) + return defaultValue; + + return intValue; + } + + public static double ToDouble(this XAttribute a, double defaultValue = 0) + { + if (a == null || string.IsNullOrWhiteSpace(a.Value)) + return defaultValue; + + double d; + + if (!double.TryParse(a.Value, out d)) + return defaultValue; + + return d; + } + } +} diff --git a/CincyLib/PressBrake/MatType.cs b/CincyLib/PressBrake/MatType.cs new file mode 100644 index 0000000..2a80fd2 --- /dev/null +++ b/CincyLib/PressBrake/MatType.cs @@ -0,0 +1,11 @@ +namespace CincyLib.PressBrake +{ + public enum MatType + { + MildSteel, + HighStrengthSteel, + Stainless, + SoftAluminum, + HardAluminum + } +} diff --git a/CincyLib/PressBrake/Program.cs b/CincyLib/PressBrake/Program.cs index 6b1c6b0..7900624 100644 --- a/CincyLib/PressBrake/Program.cs +++ b/CincyLib/PressBrake/Program.cs @@ -1,10 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; -using System.Xml.Linq; namespace CincyLib.PressBrake { @@ -19,7 +16,9 @@ namespace CincyLib.PressBrake public int Version { get; set; } - public string Name { get; set; } + public string ProgName { get; set; } + + public string FilePath { get; set; } public double MatThick { get; set; } @@ -57,285 +56,4 @@ namespace CincyLib.PressBrake return reader.Program; } } - - public enum MatType - { - MildSteel, - HighStrengthSteel, - Stainless, - SoftAluminum, - HardAluminum - } - - public class ProgramReader - { - public Program Program { get; set; } - - public ProgramReader() - { - Program = new Program(); - } - - public bool Read(string file) - { - Stream stream = null; - this.Program.Name = Path.GetFileNameWithoutExtension(file); - - var success = false; - - try - { - stream = File.OpenRead(file); - success = Read(stream); - } - catch (SystemException ex) - { - Debug.WriteLine(ex.Message); - } - finally - { - if (stream != null) - stream.Close(); - } - - return success; - } - - public bool Read(Stream stream) - { - var xml = XDocument.Load(stream); - - var data = xml.Root.Element("PressBrakeProgram"); - - var version = data.Attribute("Version")?.Value; - var matthick = data.Attribute("MatThick")?.Value; - Program.MatType = GetMaterialType(data.Attribute("MatType")?.Value); - var kfactor = data.Attribute("KFactor")?.Value; - - if (version != null) - { - int v; - - if (int.TryParse(version, out v)) - Program.Version = v; - } - - if (matthick != null) - { - double t; - - if (double.TryParse(matthick, out t)) - Program.MatThick = t; - } - - if (kfactor != null) - { - double k; - - if (double.TryParse(kfactor, out k)) - Program.KFactor = k; - } - - Program.TeachName = data.Attribute("TeachName")?.Value; - Program.PartName = data.Attribute("PartName")?.Value; - Program.SetupNotes = data.Attribute("SetupNotes")?.Value; - Program.ProgNotes = data.Attribute("ProgNotes")?.Value; - var RZEnabled = int.Parse(data.Attribute("RZEnabled").Value); - Program.RZEnabled = Convert.ToBoolean(RZEnabled); - - foreach (var item in data.Element("UpperToolSets").Descendants("ToolSetup")) - { - var setup = ReadToolSetup(item); - Program.UpperToolSets.Add(setup); - } - - foreach (var item in data.Element("LowerToolSets").Descendants("ToolSetup")) - { - var setup = ReadToolSetup(item); - Program.LowerToolSets.Add(setup); - } - - foreach (var item in data.Element("StepData").Descendants("Step")) - { - var step = ReadStep(item); - Program.Steps.Add(step); - } - - return true; - } - - private ToolSetup ReadToolSetup(XElement x) - { - var setup = new ToolSetup(); - - setup.Name = x.Attribute("Name").Value; - setup.Id = x.Attribute("ID").ToInt(); - setup.Length = x.Attribute("Length").ToDouble(); - setup.StackedHolderType = x.Attribute("StackedHolderType").ToInt(); - setup.HolderHeight = x.Attribute("HolderHeight").ToDouble(); - - foreach (var item in x.Descendants("SegEntry")) - { - var entry = new SegEntry(); - entry.SegValue = item.Attribute("SegValue").ToDouble(); - setup.Segments.Add(entry); - } - - return setup; - } - - private Step ReadStep(XElement x) - { - var step = new Step(); - - step.RevMode = x.Attribute("RevMode").ToInt(); - step.RevTons = x.Attribute("RevTons").ToDouble(); - step.MaxTons = x.Attribute("MaxTons").ToDouble(); - step.RevAbsPos = x.Attribute("RevAbsPos").ToDouble(); - step.ActualAng = x.Attribute("ActualAng").ToDouble(); - step.BendLen = x.Attribute("BendLen").ToDouble(); - step.StrokeLen = x.Attribute("StrokeLen").ToDouble(); - step.UpperID = x.Attribute("UpperID").ToInt(); - step.LowerID = x.Attribute("LowerID").ToInt(); - step.SpdChgDwn = x.Attribute("SpdChgDwn").ToDouble(); - step.SpdChgUp = x.Attribute("SpdChgUp").ToDouble(); - step.FormSpeed = x.Attribute("FormSpeed").ToDouble(); - step.XLeft = x.Attribute("XLeft").ToDouble(); - step.XRight = x.Attribute("XRight").ToDouble(); - step.RLeft = x.Attribute("RLeft").ToDouble(); - step.RRight = x.Attribute("RRight").ToDouble(); - step.ZLeft = x.Attribute("ZLeft").ToDouble(); - step.ZRight = x.Attribute("ZRight").ToDouble(); - step.FLeft = x.Attribute("FLeft").ToDouble(); - step.FRight = x.Attribute("FRight").ToDouble(); - step.SSLeft = x.Attribute("SSLeft").ToDouble(); - step.SSRight = x.Attribute("SSRight").ToDouble(); - step.ReturnSpd = x.Attribute("ReturnSpd").ToDouble(); - step.SideFlgHeight = x.Attribute("SideFlgHeight").ToDouble(); - return step; - } - - private MatType GetMaterialType(string value) - { - if (value == null) - return MatType.MildSteel; - - int i; - - if (!int.TryParse(value, out i)) - return MatType.MildSteel; - - switch (i) - { - case 0: - return MatType.MildSteel; - case 1: - return MatType.HighStrengthSteel; - case 2: - return MatType.Stainless; - case 3: - return MatType.SoftAluminum; - case 4: - return MatType.HardAluminum; - } - - return MatType.MildSteel; - } - } - - static class Extensions - { - public static bool ToBool(this XAttribute a, bool defaultValue = false) - { - if (a == null || string.IsNullOrWhiteSpace(a.Value)) - return defaultValue; - - int intValue; - - if (!int.TryParse(a.Value, out intValue)) - return defaultValue; - - return Convert.ToBoolean(intValue); - } - - public static int ToInt(this XAttribute a, int defaultValue = 0) - { - if (a == null || string.IsNullOrWhiteSpace(a.Value)) - return defaultValue; - - int intValue; - - if (!int.TryParse(a.Value, out intValue)) - return defaultValue; - - return intValue; - } - - public static double ToDouble(this XAttribute a, double defaultValue = 0) - { - if (a == null || string.IsNullOrWhiteSpace(a.Value)) - return defaultValue; - - double d; - - if (!double.TryParse(a.Value, out d)) - return defaultValue; - - return d; - } - } - - public class ToolSetup - { - public ToolSetup() - { - Segments = new List(); - } - - public string Name { get; set; } - - public int Id { get; set; } - - public double Length { get; set; } - - public int StackedHolderType { get; set; } - - public double HolderHeight { get; set; } - - public List Segments { get; set; } - } - - public class SegEntry - { - public double SegValue { get; set; } - } - - public class Step - { - public int RevMode { get; set; } - public double RevTons { get; set; } - public double MaxTons { get; set; } - public double RevAbsPos { get; set; } - public double ActualAng { get; set; } - public double BendLen { get; set; } - public double StrokeLen { get; set; } - public int UpperID { get; set; } - public int LowerID { get; set; } - public double SpdChgDwn { get; set; } - public double SpdChgUp { get; set; } - public double FormSpeed { get; set; } - public double XLeft { get; set; } - public double XRight { get; set; } - public double RLeft { get; set; } - public double RRight { get; set; } - public double ZLeft { get; set; } - public double ZRight { get; set; } - public double FLeft { get; set; } - public double FRight { get; set; } - - public double SSLeft { get; set; } - public double SSRight { get; set; } - public double ReturnSpd { get; set; } - public double SideFlgHeight { get; set; } - } } diff --git a/CincyLib/PressBrake/ProgramReader.cs b/CincyLib/PressBrake/ProgramReader.cs new file mode 100644 index 0000000..0ab217d --- /dev/null +++ b/CincyLib/PressBrake/ProgramReader.cs @@ -0,0 +1,158 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Xml.Linq; + +namespace CincyLib.PressBrake +{ + public class ProgramReader + { + public Program Program { get; set; } + + public ProgramReader() + { + Program = new Program(); + } + + public bool Read(string file) + { + Stream stream = null; + Program.FilePath = file; + + var success = false; + + try + { + stream = File.OpenRead(file); + success = Read(stream); + } + catch (SystemException ex) + { + Debug.WriteLine(ex.Message); + } + finally + { + if (stream != null) + stream.Close(); + } + + return success; + } + + public bool Read(Stream stream) + { + var xml = XDocument.Load(stream); + + var data = xml.Root.Element("PressBrakeProgram"); + + Program.Version = data.Attribute("Version").ToInt(); + Program.MatThick = data.Attribute("MatThick").ToDouble(); + Program.MatType = GetMaterialType(data.Attribute("MatType")?.Value); + Program.KFactor = data.Attribute("KFactor").ToDouble(); + Program.TeachName = data.Attribute("TeachName")?.Value; + Program.PartName = data.Attribute("PartName")?.Value; + Program.SetupNotes = data.Attribute("SetupNotes")?.Value; + Program.ProgNotes = data.Attribute("ProgNotes")?.Value; + Program.RZEnabled = Convert.ToBoolean(data.Attribute("RZEnabled").ToInt()); + + foreach (var item in data.Element("UpperToolSets").Descendants("ToolSetup")) + { + var setup = ReadToolSetup(item); + Program.UpperToolSets.Add(setup); + } + + foreach (var item in data.Element("LowerToolSets").Descendants("ToolSetup")) + { + var setup = ReadToolSetup(item); + Program.LowerToolSets.Add(setup); + } + + foreach (var item in data.Element("StepData").Descendants("Step")) + { + var step = ReadStep(item); + Program.Steps.Add(step); + } + + return true; + } + + private ToolSetup ReadToolSetup(XElement x) + { + var setup = new ToolSetup(); + + setup.Name = x.Attribute("Name").Value; + setup.Id = x.Attribute("ID").ToInt(); + setup.Length = x.Attribute("Length").ToDouble(); + setup.StackedHolderType = x.Attribute("StackedHolderType").ToInt(); + setup.HolderHeight = x.Attribute("HolderHeight").ToDouble(); + + foreach (var item in x.Descendants("SegEntry")) + { + var entry = new SegEntry(); + entry.SegValue = item.Attribute("SegValue").ToDouble(); + setup.Segments.Add(entry); + } + + return setup; + } + + private Step ReadStep(XElement x) + { + var step = new Step(); + + step.RevMode = x.Attribute("RevMode").ToInt(); + step.RevTons = x.Attribute("RevTons").ToDouble(); + step.MaxTons = x.Attribute("MaxTons").ToDouble(); + step.RevAbsPos = x.Attribute("RevAbsPos").ToDouble(); + step.ActualAng = x.Attribute("ActualAng").ToDouble(); + step.AngleAdj = x.Attribute("AngleAdj").ToDouble(); + step.BendLen = x.Attribute("BendLen").ToDouble(); + step.StrokeLen = x.Attribute("StrokeLen").ToDouble(); + step.UpperID = x.Attribute("UpperID").ToInt(); + step.LowerID = x.Attribute("LowerID").ToInt(); + step.SpdChgDwn = x.Attribute("SpdChgDwn").ToDouble(); + step.SpdChgUp = x.Attribute("SpdChgUp").ToDouble(); + step.FormSpeed = x.Attribute("FormSpeed").ToDouble(); + step.XLeft = x.Attribute("XLeft").ToDouble(); + step.XRight = x.Attribute("XRight").ToDouble(); + step.RLeft = x.Attribute("RLeft").ToDouble(); + step.RRight = x.Attribute("RRight").ToDouble(); + step.ZLeft = x.Attribute("ZLeft").ToDouble(); + step.ZRight = x.Attribute("ZRight").ToDouble(); + step.FLeft = x.Attribute("FLeft").ToDouble(); + step.FRight = x.Attribute("FRight").ToDouble(); + step.SSLeft = x.Attribute("SSLeft").ToDouble(); + step.SSRight = x.Attribute("SSRight").ToDouble(); + step.ReturnSpd = x.Attribute("ReturnSpd").ToDouble(); + step.SideFlgHeight = x.Attribute("SideFlgHeight").ToDouble(); + return step; + } + + private MatType GetMaterialType(string value) + { + if (value == null) + return MatType.MildSteel; + + int i; + + if (!int.TryParse(value, out i)) + return MatType.MildSteel; + + switch (i) + { + case 0: + return MatType.MildSteel; + case 1: + return MatType.HighStrengthSteel; + case 2: + return MatType.Stainless; + case 3: + return MatType.SoftAluminum; + case 4: + return MatType.HardAluminum; + } + + return MatType.MildSteel; + } + } +} diff --git a/CincyLib/PressBrake/SegEntry.cs b/CincyLib/PressBrake/SegEntry.cs new file mode 100644 index 0000000..375a243 --- /dev/null +++ b/CincyLib/PressBrake/SegEntry.cs @@ -0,0 +1,7 @@ +namespace CincyLib.PressBrake +{ + public class SegEntry + { + public double SegValue { get; set; } + } +} diff --git a/CincyLib/PressBrake/Step.cs b/CincyLib/PressBrake/Step.cs new file mode 100644 index 0000000..f4c7f33 --- /dev/null +++ b/CincyLib/PressBrake/Step.cs @@ -0,0 +1,32 @@ +namespace CincyLib.PressBrake +{ + public class Step + { + public int RevMode { get; set; } + public double RevTons { get; set; } + public double MaxTons { get; set; } + public double RevAbsPos { get; set; } + public double ActualAng { get; set; } + public double AngleAdj { get; set; } + public double BendLen { get; set; } + public double StrokeLen { get; set; } + public int UpperID { get; set; } + public int LowerID { get; set; } + public double SpdChgDwn { get; set; } + public double SpdChgUp { get; set; } + public double FormSpeed { get; set; } + public double XLeft { get; set; } + public double XRight { get; set; } + public double RLeft { get; set; } + public double RRight { get; set; } + public double ZLeft { get; set; } + public double ZRight { get; set; } + public double FLeft { get; set; } + public double FRight { get; set; } + + public double SSLeft { get; set; } + public double SSRight { get; set; } + public double ReturnSpd { get; set; } + public double SideFlgHeight { get; set; } + } +} diff --git a/CincyLib/PressBrake/ToolSetup.cs b/CincyLib/PressBrake/ToolSetup.cs new file mode 100644 index 0000000..6368d7b --- /dev/null +++ b/CincyLib/PressBrake/ToolSetup.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace CincyLib.PressBrake +{ + public class ToolSetup + { + public ToolSetup() + { + Segments = new List(); + } + + public string Name { get; set; } + + public int Id { get; set; } + + public double Length { get; set; } + + public int StackedHolderType { get; set; } + + public double HolderHeight { get; set; } + + public List Segments { get; set; } + } +}