feat: port CincyLib PressBrake parser to FabWorks.Core (net8.0)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-18 06:22:46 -05:00
parent 78a8a2197d
commit 2bef75f548
7 changed files with 457 additions and 0 deletions

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,11 @@
namespace FabWorks.Core.PressBrake
{
public enum MatType
{
MildSteel,
HighStrengthSteel,
Stainless,
SoftAluminum,
HardAluminum
}
}

View File

@@ -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<ToolSetup>();
LowerToolSets = new List<ToolSetup>();
Steps = new List<Step>();
}
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<ToolSetup> UpperToolSets { get; set; }
public List<ToolSetup> LowerToolSets { get; set; }
public List<Step> 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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,7 @@
namespace FabWorks.Core.PressBrake
{
public class SegEntry
{
public double SegValue { get; set; }
}
}

View File

@@ -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; }
}
}

View File

@@ -0,0 +1,24 @@
using System.Collections.Generic;
namespace FabWorks.Core.PressBrake
{
public class ToolSetup
{
public ToolSetup()
{
Segments = new List<SegEntry>();
}
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<SegEntry> Segments { get; set; }
}
}