From 2bef75f548bd7626a8ee6eb8240f6931bffa12f0 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Wed, 18 Feb 2026 06:22:46 -0500 Subject: [PATCH] feat: port CincyLib PressBrake parser to FabWorks.Core (net8.0) Co-Authored-By: Claude Opus 4.6 --- FabWorks.Core/PressBrake/Extensions.cs | 171 ++++++++++++++++++++++ FabWorks.Core/PressBrake/MatType.cs | 11 ++ FabWorks.Core/PressBrake/Program.cs | 59 ++++++++ FabWorks.Core/PressBrake/ProgramReader.cs | 149 +++++++++++++++++++ FabWorks.Core/PressBrake/SegEntry.cs | 7 + FabWorks.Core/PressBrake/Step.cs | 36 +++++ FabWorks.Core/PressBrake/ToolSetup.cs | 24 +++ 7 files changed, 457 insertions(+) create mode 100644 FabWorks.Core/PressBrake/Extensions.cs create mode 100644 FabWorks.Core/PressBrake/MatType.cs create mode 100644 FabWorks.Core/PressBrake/Program.cs create mode 100644 FabWorks.Core/PressBrake/ProgramReader.cs create mode 100644 FabWorks.Core/PressBrake/SegEntry.cs create mode 100644 FabWorks.Core/PressBrake/Step.cs create mode 100644 FabWorks.Core/PressBrake/ToolSetup.cs diff --git a/FabWorks.Core/PressBrake/Extensions.cs b/FabWorks.Core/PressBrake/Extensions.cs new file mode 100644 index 0000000..d86ce26 --- /dev/null +++ b/FabWorks.Core/PressBrake/Extensions.cs @@ -0,0 +1,171 @@ +using System; +using System.Xml.Linq; + +namespace FabWorks.Core.PressBrake +{ + internal static class Extensions + { + private static bool? ToBool(this string s) + { + if (string.IsNullOrWhiteSpace(s)) + return null; + + int intValue; + + if (!int.TryParse(s, out intValue)) + return null; + + return Convert.ToBoolean(intValue); + } + + public static bool ToBool(this XAttribute a, bool defaultValue = false) + { + if (a == null) + return defaultValue; + + var b = a.Value.ToBool(); + + return b != null ? b.Value : defaultValue; + } + + public static bool? ToBoolOrNull(this XAttribute a) + { + if (a == null) + return null; + + return a.Value.ToBool(); + } + + private static int? ToInt(this string s) + { + if (string.IsNullOrWhiteSpace(s)) + return null; + + int intValue; + + if (!int.TryParse(s, out intValue)) + return null; + + return intValue; + } + + public static int ToInt(this XAttribute a, int defaultValue = 0) + { + if (a == null) + return defaultValue; + + var b = a.Value.ToInt(); + + return b != null ? b.Value : defaultValue; + } + + public static int? ToIntOrNull(this XAttribute a) + { + if (a == null) + return null; + + return a.Value.ToInt(); + } + + public static int ToInt(this XElement a, int defaultValue = 0) + { + if (a == null) + return defaultValue; + + var b = a.Value.ToInt(); + + return b != null ? b.Value : defaultValue; + } + + public static int? ToIntOrNull(this XElement a) + { + if (a == null) + return null; + + return a.Value.ToInt(); + } + + private static double? ToDouble(this string s) + { + if (string.IsNullOrWhiteSpace(s)) + return null; + + double d; + + if (!double.TryParse(s, out d)) + return null; + + return d; + } + + public static double ToDouble(this XAttribute a, double defaultValue = 0) + { + if (a == null) + return defaultValue; + + var b = a.Value.ToDouble(); + + return b != null ? b.Value : defaultValue; + } + + public static double? ToDoubleOrNull(this XAttribute a) + { + if (a == null) + return null; + + return a.Value.ToDouble(); + } + + public static double ToDouble(this XElement a, double defaultValue = 0) + { + if (a == null) + return defaultValue; + + var b = a.Value.ToDouble(); + + return b != null ? b.Value : defaultValue; + } + + public static double? ToDoubleOrNull(this XElement a) + { + if (a == null) + return null; + + return a.Value.ToDouble(); + } + + public static DateTime? ToDateTime(this XAttribute a) + { + if (a == null || string.IsNullOrWhiteSpace(a.Value)) + return null; + + DateTime d; + + if (!DateTime.TryParse(a.Value, out d)) + return null; + + return d; + } + + public static TimeSpan? ToTimeSpan(this XElement e) + { + if (e == null || string.IsNullOrWhiteSpace(e.Value)) + return null; + + TimeSpan d; + + if (!TimeSpan.TryParse(e.Value, out d)) + return null; + + return d; + } + + public static DateTime RoundDown(this DateTime dt, TimeSpan d) + { + var modTicks = dt.Ticks % d.Ticks; + var delta = -modTicks; + + return new DateTime(dt.Ticks + delta, dt.Kind); + } + } +} diff --git a/FabWorks.Core/PressBrake/MatType.cs b/FabWorks.Core/PressBrake/MatType.cs new file mode 100644 index 0000000..c2716f8 --- /dev/null +++ b/FabWorks.Core/PressBrake/MatType.cs @@ -0,0 +1,11 @@ +namespace FabWorks.Core.PressBrake +{ + public enum MatType + { + MildSteel, + HighStrengthSteel, + Stainless, + SoftAluminum, + HardAluminum + } +} diff --git a/FabWorks.Core/PressBrake/Program.cs b/FabWorks.Core/PressBrake/Program.cs new file mode 100644 index 0000000..7b36a87 --- /dev/null +++ b/FabWorks.Core/PressBrake/Program.cs @@ -0,0 +1,59 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace FabWorks.Core.PressBrake +{ + public class Program + { + public Program() + { + UpperToolSets = new List(); + LowerToolSets = new List(); + Steps = new List(); + } + + public int Version { get; set; } + + public string ProgName { get; set; } + + public string FilePath { get; set; } + + public double MatThick { get; set; } + + public MatType MatType { get; set; } + + public double KFactor { get; set; } + + public string TeachName { get; set; } + + public string PartName { get; set; } + + public string SetupNotes { get; set; } + + public string ProgNotes { get; set; } + + public bool RZEnabled { get; set; } + + public List UpperToolSets { get; set; } + + public List LowerToolSets { get; set; } + + public List Steps { get; set; } + + public static Program Load(string file) + { + var reader = new ProgramReader(); + reader.Read(file); + return reader.Program; + } + + public static Program Load(Stream stream) + { + var reader = new ProgramReader(); + reader.Read(stream); + return reader.Program; + } + } +} diff --git a/FabWorks.Core/PressBrake/ProgramReader.cs b/FabWorks.Core/PressBrake/ProgramReader.cs new file mode 100644 index 0000000..0267e79 --- /dev/null +++ b/FabWorks.Core/PressBrake/ProgramReader.cs @@ -0,0 +1,149 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml.Linq; + +namespace FabWorks.Core.PressBrake +{ + public class ProgramReader + { + public Program Program { get; set; } + + public ProgramReader() + { + Program = new Program(); + } + + public void Read(string file) + { + var xml = XDocument.Load(file); + Program.FilePath = file; + Read(xml); + } + + public void Read(Stream stream) + { + var xml = XDocument.Load(stream); + Read(xml); + } + + private void Read(XDocument doc) + { + var data = doc.Root.Element("PressBrakeProgram"); + + Program.Version = data.Attribute("Version").ToInt(); + Program.ProgName = data.Attribute("ProgName")?.Value; + 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); + step.UpperTool = Program.UpperToolSets.FirstOrDefault(t => t.Id == step.UpperID); + step.LowerTool = Program.LowerToolSets.FirstOrDefault(t => t.Id == step.LowerID); + + Program.Steps.Add(step); + } + } + + 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.Tilt = x.Attribute("Tilt").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/FabWorks.Core/PressBrake/SegEntry.cs b/FabWorks.Core/PressBrake/SegEntry.cs new file mode 100644 index 0000000..e6ed33d --- /dev/null +++ b/FabWorks.Core/PressBrake/SegEntry.cs @@ -0,0 +1,7 @@ +namespace FabWorks.Core.PressBrake +{ + public class SegEntry + { + public double SegValue { get; set; } + } +} diff --git a/FabWorks.Core/PressBrake/Step.cs b/FabWorks.Core/PressBrake/Step.cs new file mode 100644 index 0000000..3520a17 --- /dev/null +++ b/FabWorks.Core/PressBrake/Step.cs @@ -0,0 +1,36 @@ +namespace FabWorks.Core.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 double Tilt { 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; } + + public ToolSetup UpperTool { get; set; } + public ToolSetup LowerTool { get; set; } + } +} diff --git a/FabWorks.Core/PressBrake/ToolSetup.cs b/FabWorks.Core/PressBrake/ToolSetup.cs new file mode 100644 index 0000000..c71d4b9 --- /dev/null +++ b/FabWorks.Core/PressBrake/ToolSetup.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace FabWorks.Core.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; } + } +}