diff --git a/libraries/AA.xml b/libraries/AA.xml index 2cf6ee8..28dab95 100644 --- a/libraries/AA.xml +++ b/libraries/AA.xml @@ -3,96 +3,96 @@ - + - - - - - - + + + + + + - - - - + + + + - + - - - - + + + + - + - + - - - - - - + + + + + + - - - - + + + + - - + + - + - - - - - - + + + + + + - + - + - + - + - - - - - - + + + + + + - - - - + + + + - + \ No newline at end of file diff --git a/libraries/AAU_UR10.xml b/libraries/AAU_UR10.xml index 584333a..77ff034 100644 --- a/libraries/AAU_UR10.xml +++ b/libraries/AAU_UR10.xml @@ -3,23 +3,23 @@ - + - - - - - - + + + + + + - - - - + + + + - + \ No newline at end of file diff --git a/libraries/AarhusSchoolOfArchitecture.xml b/libraries/AarhusSchoolOfArchitecture.xml index f3963d4..3d1e79b 100644 --- a/libraries/AarhusSchoolOfArchitecture.xml +++ b/libraries/AarhusSchoolOfArchitecture.xml @@ -3,43 +3,43 @@ - + - - - - - - + + + + + + - - + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - + + + + - - + + \ No newline at end of file diff --git a/libraries/Automata.xml b/libraries/Automata.xml index 3dfaeb0..5c680c1 100644 --- a/libraries/Automata.xml +++ b/libraries/Automata.xml @@ -1,23 +1,23 @@ - - + + - + - - - - - - + + + + + + - - - - + + + + diff --git a/libraries/Bigli.xml b/libraries/Bigli.xml index 0f33d99..5cc1232 100644 --- a/libraries/Bigli.xml +++ b/libraries/Bigli.xml @@ -1,23 +1,23 @@  - + - + - - - - - - + + + + + + - - - - + + + + \ No newline at end of file diff --git a/libraries/CRR.xml b/libraries/CRR.xml index bf77866..d5978b6 100644 --- a/libraries/CRR.xml +++ b/libraries/CRR.xml @@ -3,91 +3,90 @@ - + - - - - - - + + + + + + - - + + - + - + - - - - - - + + + + + + - - - + + - + - - + + - + - - - - - - + + + + + + - - + + - - + + - - + + - - - - - - + + + + + + - - + - - + + \ No newline at end of file diff --git a/libraries/Carnegie.xml b/libraries/Carnegie.xml index 00903b6..ce7305d 100644 --- a/libraries/Carnegie.xml +++ b/libraries/Carnegie.xml @@ -3,46 +3,46 @@ - - - - - - - - - + + + + + + + + + - - - - + + + + - - - - - - - - - + + + + + + + + + - - - - + + + + - + \ No newline at end of file diff --git a/libraries/ETH.xml b/libraries/ETH.xml index a7efb88..3d79e30 100644 --- a/libraries/ETH.xml +++ b/libraries/ETH.xml @@ -3,29 +3,29 @@ - + - - - - - - + + + + + + - + - + - - - + + + - - + + - + \ No newline at end of file diff --git a/libraries/IRB6700.xml b/libraries/IRB6700.xml index db58a4d..f77293b 100644 --- a/libraries/IRB6700.xml +++ b/libraries/IRB6700.xml @@ -3,21 +3,21 @@ - + - - - - - - + + + + + + - - + + - + \ No newline at end of file diff --git a/libraries/Innsbruck.xml b/libraries/Innsbruck.xml index 5f8894f..3822992 100644 --- a/libraries/Innsbruck.xml +++ b/libraries/Innsbruck.xml @@ -1,77 +1,76 @@  - + - + - - - - - - + + + + + + - + - + - + - - + + - + - - - - - - + + + + + + - + - - - - - - + + + + + + - + - - - - - - + + + + + + - - + + - - - + + \ No newline at end of file diff --git a/libraries/KULeuven.xml b/libraries/KULeuven.xml index 6a904c5..9d47f1d 100644 --- a/libraries/KULeuven.xml +++ b/libraries/KULeuven.xml @@ -3,64 +3,64 @@ - + - - - - - - + + + + + + - - + + - + - + - - - - - - + + + + + + - - - - + + + + - + - - - - - - + + + + + + - - - - + + + + diff --git a/libraries/MIT_sim.xml b/libraries/MIT_sim.xml index f3a0ff6..d9de5ca 100644 --- a/libraries/MIT_sim.xml +++ b/libraries/MIT_sim.xml @@ -2,33 +2,33 @@ - - + + - + - - - - - - + + + + + + - - + + - - + + - - - - + + + + diff --git a/libraries/PennState.xml b/libraries/PennState.xml index 19f963a..da7b860 100644 --- a/libraries/PennState.xml +++ b/libraries/PennState.xml @@ -3,22 +3,22 @@ - - - - - - - - - + + + + + + + + + - - - - + + + + diff --git a/libraries/Stuttgart.xml b/libraries/Stuttgart.xml index 6e473a3..79cb1f0 100644 --- a/libraries/Stuttgart.xml +++ b/libraries/Stuttgart.xml @@ -1,69 +1,69 @@  - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + - + - + - - - - - - + + + + + + - - + + - - + + - + \ No newline at end of file diff --git a/libraries/Taiwan.xml b/libraries/Taiwan.xml index e065e30..f269488 100644 --- a/libraries/Taiwan.xml +++ b/libraries/Taiwan.xml @@ -3,65 +3,65 @@ - + - - - - - - + + + + + + - - + + - + - - - - - - + + + + + + - - - - + + + + - + - + - - - - - - + + + + + + - - - - + + + + - + \ No newline at end of file diff --git a/libraries/TaiwanTech.xml b/libraries/TaiwanTech.xml index 5ec2b15..3462214 100644 --- a/libraries/TaiwanTech.xml +++ b/libraries/TaiwanTech.xml @@ -1,36 +1,66 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - + + + + + + - - + + - + - + - - - - - - + + + + + + @@ -39,45 +69,45 @@ - + - - - - - - + + + + + + - + - - + + - - + + - + - + - + - + - + - - - + + + diff --git a/libraries/TaiwanTechTools.xml b/libraries/TaiwanTechTools.xml deleted file mode 100644 index e3ca09c..0000000 --- a/libraries/TaiwanTechTools.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - 0.2 - - - - - 0.2 - - - - - 0.3 - - - - - 1.2 - - - - - 20 - - - - - 7 - - - \ No newline at end of file diff --git a/libraries/bartlett.xml b/libraries/bartlett.xml index 4edfef0..b07c072 100644 --- a/libraries/bartlett.xml +++ b/libraries/bartlett.xml @@ -3,78 +3,78 @@ - + - - - - - - + + + + + + - - + + - + - - - - - - + + + + + + - - - - + + + + - + - - - - - - + + + + + + - - - - + + + + - + - - - - - - + + + + + + - + - - + + - - - - + + + + - + - - - - - - + + + + + + - + - - + + - - - - + + + + - + - - - - - - + + + + + + - - - - + + + + - + - - - - - - + + + + + + - + - + - - - - + + + + - + - - - - - - + + + + + + - - - - + + + + @@ -216,111 +216,111 @@ - + - - - - - - + + + + + + - + - - + + - - - - + + + + - + - - - - - - + + + + + + - - + + - + - - - - - - + + + + + + - - + + - + - - - - - - + + + + + + - - + + - + - - - - - - + + + + + + - - + + - + \ No newline at end of file diff --git a/libraries/cita.xml b/libraries/cita.xml index 44bca37..23d2e73 100644 --- a/libraries/cita.xml +++ b/libraries/cita.xml @@ -2,132 +2,132 @@ - + - - - - - - + + + + + + - + - - + + - - - - + + + + - + - - - - - - + + + + + + - + - + - - - - - - + + + + + + - - - - + + + + - + - - - - - - + + + + + + - - - - + + + + - + - - - - - - + + + + + + - - - - + + + + - + - - - - - - + + + + + + - - - - + + + + - + \ No newline at end of file diff --git a/libraries/grymsdyke.xml b/libraries/grymsdyke.xml index f830fc9..ec558ed 100644 --- a/libraries/grymsdyke.xml +++ b/libraries/grymsdyke.xml @@ -3,43 +3,43 @@ - + - - - - - - + + + + + + - - + + - + - - - - - - + + + + + + - - - - + + + + - + \ No newline at end of file diff --git a/libraries/iaac.xml b/libraries/iaac.xml index 2358f70..832a86e 100644 --- a/libraries/iaac.xml +++ b/libraries/iaac.xml @@ -3,63 +3,63 @@ - + - - - - - - + + + + + + - - - - + + + + - + - + - - - - - - + + + + + + - - + + - + - + - - - - - - + + + + + + - - + + - + \ No newline at end of file diff --git a/libraries/nagami.xml b/libraries/nagami.xml index 677ef58..79b3174 100644 --- a/libraries/nagami.xml +++ b/libraries/nagami.xml @@ -3,120 +3,120 @@ - + - - - - - - + + + + + + - + - + - + - - - - - - + + + + + + - - + + - + - - - - - - + + + + + + - + - + - - + + - + - - - - - - + + + + + + - + - + - - - - + + + + - + - - - - - - + + + + + + - + - + - - - - + + + + - + \ No newline at end of file diff --git a/libraries/rmit.xml b/libraries/rmit.xml index 56c5cc3..44ad34b 100644 --- a/libraries/rmit.xml +++ b/libraries/rmit.xml @@ -3,22 +3,22 @@ - + - - - - - - + + + + + + - - - - + + + + diff --git a/src/Robots.Grasshopper/Goos/GH_Command.cs b/src/Robots.Grasshopper/Goos/GH_Command.cs index 873e1fb..4be3f84 100644 --- a/src/Robots.Grasshopper/Goos/GH_Command.cs +++ b/src/Robots.Grasshopper/Goos/GH_Command.cs @@ -12,7 +12,6 @@ public class GH_Command : GH_Goo public override string TypeName => "Command"; public override string TypeDescription => "Command"; public override string ToString() => Value.ToString(); - public override object ScriptVariable() => Value; public override bool CastFrom(object source) { diff --git a/src/Robots.Grasshopper/Goos/GH_Frame.cs b/src/Robots.Grasshopper/Goos/GH_Frame.cs index 4914f05..4cc0521 100644 --- a/src/Robots.Grasshopper/Goos/GH_Frame.cs +++ b/src/Robots.Grasshopper/Goos/GH_Frame.cs @@ -13,7 +13,6 @@ public class GH_Frame : GH_Goo public override string TypeName => "Frame"; public override string TypeDescription => "Frame"; public override string ToString() => Value.ToString(); - public override object ScriptVariable() => Value; public override bool CastFrom(object source) { diff --git a/src/Robots.Grasshopper/Goos/GH_Program.cs b/src/Robots.Grasshopper/Goos/GH_Program.cs index 28eef69..439e6e8 100644 --- a/src/Robots.Grasshopper/Goos/GH_Program.cs +++ b/src/Robots.Grasshopper/Goos/GH_Program.cs @@ -12,7 +12,6 @@ public GH_Program() { } public override string TypeName => "Program"; public override string TypeDescription => "Program"; public override string ToString() => Value.ToString(); - public override object? ScriptVariable() => Value; public override bool CastFrom(object source) { diff --git a/src/Robots.Grasshopper/Goos/GH_RobotSystem.cs b/src/Robots.Grasshopper/Goos/GH_RobotSystem.cs index 6ba95e8..6d2ab04 100644 --- a/src/Robots.Grasshopper/Goos/GH_RobotSystem.cs +++ b/src/Robots.Grasshopper/Goos/GH_RobotSystem.cs @@ -14,7 +14,6 @@ public GH_RobotSystem() { } public override string TypeName => "RobotSystem"; public override string TypeDescription => "RobotSystem"; public override string ToString() => Value.ToString(); - public override object ScriptVariable() => Value; public override bool CastFrom(object source) { diff --git a/src/Robots.Grasshopper/Goos/GH_Speed.cs b/src/Robots.Grasshopper/Goos/GH_Speed.cs index 114c336..5119801 100644 --- a/src/Robots.Grasshopper/Goos/GH_Speed.cs +++ b/src/Robots.Grasshopper/Goos/GH_Speed.cs @@ -13,7 +13,6 @@ public class GH_Speed : GH_Goo public override string TypeName => "Speed"; public override string TypeDescription => "Speed"; public override string ToString() => Value.ToString(); - public override object ScriptVariable() => Value; public override bool CastFrom(object source) { diff --git a/src/Robots.Grasshopper/Goos/GH_Target.cs b/src/Robots.Grasshopper/Goos/GH_Target.cs index 2a4767e..e4cc169 100644 --- a/src/Robots.Grasshopper/Goos/GH_Target.cs +++ b/src/Robots.Grasshopper/Goos/GH_Target.cs @@ -14,7 +14,6 @@ public class GH_Target : GH_Goo public override string TypeName => "Target"; public override string TypeDescription => "Target"; public override string ToString() => Value.ToString(); - public override object ScriptVariable() => Value; public override bool CastFrom(object source) { diff --git a/src/Robots.Grasshopper/Goos/GH_Tool.cs b/src/Robots.Grasshopper/Goos/GH_Tool.cs index fd82cb7..d0fec3b 100644 --- a/src/Robots.Grasshopper/Goos/GH_Tool.cs +++ b/src/Robots.Grasshopper/Goos/GH_Tool.cs @@ -12,7 +12,6 @@ public class GH_Tool : GH_Goo public override string TypeName => "Tool"; public override string TypeDescription => "Tool"; public override string ToString() => Value.ToString(); - public override object ScriptVariable() => Value; public override bool CastFrom(object source) { switch (source) diff --git a/src/Robots.Grasshopper/Goos/GH_Toolpath.cs b/src/Robots.Grasshopper/Goos/GH_Toolpath.cs index 2526060..7fddf98 100644 --- a/src/Robots.Grasshopper/Goos/GH_Toolpath.cs +++ b/src/Robots.Grasshopper/Goos/GH_Toolpath.cs @@ -19,7 +19,6 @@ public override string ToString() _ => "Toolpath", }; } - public override object ScriptVariable() => Value; public override bool CastFrom(object source) { diff --git a/src/Robots.Grasshopper/Goos/GH_Zone.cs b/src/Robots.Grasshopper/Goos/GH_Zone.cs index 14d7fa4..a6dd217 100644 --- a/src/Robots.Grasshopper/Goos/GH_Zone.cs +++ b/src/Robots.Grasshopper/Goos/GH_Zone.cs @@ -13,7 +13,6 @@ public class GH_Zone : GH_Goo public override string TypeName => "Zone"; public override string TypeDescription => "Zone"; public override string ToString() => Value.ToString(); - public override object ScriptVariable() => Value; public override bool CastFrom(object source) { diff --git a/src/Robots.Grasshopper/Target/CreateFrame.cs b/src/Robots.Grasshopper/Target/CreateFrame.cs index 2902965..7a537c0 100644 --- a/src/Robots.Grasshopper/Target/CreateFrame.cs +++ b/src/Robots.Grasshopper/Target/CreateFrame.cs @@ -38,7 +38,14 @@ protected override void SolveInstance(IGH_DataAccess DA) if (!DA.GetData(2, ref coupledMechanism)) { return; } DA.GetData(3, ref name); - var frame = new Frame(plane.Value, coupledMechanism, coupledGroup, name); - DA.SetData(0, new GH_Frame(frame)); + try + { + var frame = new Frame(plane.Value, coupledMechanism, coupledGroup, name); + DA.SetData(0, new GH_Frame(frame)); + } + catch (ArgumentException e) + { + AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message); + } } -} +} \ No newline at end of file diff --git a/src/Robots.Grasshopper/Target/CreateSpeed.cs b/src/Robots.Grasshopper/Target/CreateSpeed.cs index db76ca7..80c51ac 100644 --- a/src/Robots.Grasshopper/Target/CreateSpeed.cs +++ b/src/Robots.Grasshopper/Target/CreateSpeed.cs @@ -36,4 +36,4 @@ protected override void SolveInstance(IGH_DataAccess DA) var speed = new Speed(translationSpeed, rotationSpeed, translationExternal, rotationExternal); DA.SetData(0, new GH_Speed(speed)); } -} +} \ No newline at end of file diff --git a/src/Robots.Grasshopper/Target/CreateTool.cs b/src/Robots.Grasshopper/Target/CreateTool.cs index e0220eb..cfc8981 100644 --- a/src/Robots.Grasshopper/Target/CreateTool.cs +++ b/src/Robots.Grasshopper/Target/CreateTool.cs @@ -38,7 +38,7 @@ protected override void SolveInstance(IGH_DataAccess DA) double weight = 0; GH_Mesh? mesh = null; GH_Point? centroid = null; - var planes = new List(); + var planes = new List(); if (!DA.GetData(0, ref name) || name is null) { return; } if (!DA.GetData(1, ref tcp) || tcp is null) { return; } @@ -47,17 +47,16 @@ protected override void SolveInstance(IGH_DataAccess DA) DA.GetData(4, ref centroid); DA.GetData(5, ref mesh); - var tool = new Tool(tcp.Value, name, weight, centroid?.Value, mesh?.Value); + try + { + var tool = new Tool(tcp.Value, name, weight, centroid?.Value, mesh?.Value, planes); - if (planes.Count > 0) + DA.SetData(0, new GH_Tool(tool)); + DA.SetData(1, tool.Tcp); + } + catch (ArgumentException e) { - if (planes.Count != 4) - AddRuntimeMessage(GH_RuntimeMessageLevel.Error, " Calibration input must be 4 planes"); - else - tool.FourPointCalibration(planes[0].Value, planes[1].Value, planes[2].Value, planes[3].Value); + AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message); } - - DA.SetData(0, new GH_Tool(tool)); - DA.SetData(1, tool.Tcp); } } \ No newline at end of file diff --git a/src/Robots/Commands/Command.cs b/src/Robots/Commands/Command.cs index 85e5c9f..a78f677 100644 --- a/src/Robots/Commands/Command.cs +++ b/src/Robots/Commands/Command.cs @@ -7,6 +7,8 @@ public abstract class Command : TargetAttribute protected Dictionary> _declarations = new(4); protected Dictionary> _commands = new(4); + protected Command(string? name = null) : base(name) { } + protected virtual void ErrorChecking(RobotSystem robotSystem) { } protected virtual void Populate() { } public bool RunBefore { get; set; } = false; diff --git a/src/Robots/Commands/Custom.cs b/src/Robots/Commands/Custom.cs index 1efb537..1e49929 100644 --- a/src/Robots/Commands/Custom.cs +++ b/src/Robots/Commands/Custom.cs @@ -5,9 +5,8 @@ public class Custom : Command readonly Dictionary _customCommands = new(); readonly Dictionary _customDeclarations = new(); - public Custom(string name = "Custom command", Manufacturers manufacturer = Manufacturers.All, string? command = null, string? declaration = null) + public Custom(string name = "CustomCommand", Manufacturers manufacturer = Manufacturers.All, string? command = null, string? declaration = null) : base(name) { - Name = name; AddCommand(manufacturer, command, declaration); } diff --git a/src/Robots/Commands/Group.cs b/src/Robots/Commands/Group.cs index ea8e7c1..da9e4e8 100644 --- a/src/Robots/Commands/Group.cs +++ b/src/Robots/Commands/Group.cs @@ -6,13 +6,10 @@ public class Group : Command, IList { readonly List _commandList = new(); - public Group() - { - } + public Group() { } - public Group(IEnumerable commands) + public Group(IEnumerable commands) : base("GroupCommand") { - Name = "Group command"; _commandList.AddRange(commands); } diff --git a/src/Robots/FileIO.cs b/src/Robots/FileIO.cs index 6b71341..0c96617 100644 --- a/src/Robots/FileIO.cs +++ b/src/Robots/FileIO.cs @@ -1,4 +1,5 @@ -using System.Xml; +using System.Text.RegularExpressions; +using System.Xml; using System.Xml.Linq; using Rhino.FileIO; using Rhino.Geometry; @@ -322,6 +323,38 @@ static Mesh GetToolMesh(string name) // Extensions + public static bool IsValidName(this string name, out string error) + { + if (name.Length == 0) + { + error = "name is empty."; + return false; + } + + var excess = name.Length - 32; + + if (excess > 0) + { + error = $"name is {excess} character(s) too long."; + return false; + } + + if (!char.IsLetter(name[0])) + { + error = "name must start with a letter."; + return false; + } + + if (!Regex.IsMatch(name, @"^[A-Z0-9_]+$", RegexOptions.IgnoreCase)) + { + error = "name can only contain letters, digits, and underscores (_)."; + return false; + } + + error = ""; + return true; + } + static XElement GetElement(this XElement element, string name) { return element.Element(XName.Get(name)) diff --git a/src/Robots/Program/CheckProgram.cs b/src/Robots/Program/CheckProgram.cs index 7e337f1..9efeec7 100644 --- a/src/Robots/Program/CheckProgram.cs +++ b/src/Robots/Program/CheckProgram.cs @@ -17,13 +17,35 @@ internal CheckProgram(Program program, List cellTargets, double step { _robotSystem = program.RobotSystem; _program = program; - // groupCount = cellTargets[0].ProgramTargets.Count; + CheckName(); FixFirstTarget(cellTargets[0]); FixTargetAttributes(cellTargets); IndexError = FixTargetMotions(cellTargets, stepSize); } + void CheckName() + { + var name = _program.Name; + + if (_robotSystem is RobotCell cell) + { + var group = cell.MechanicalGroups.MaxBy(g => g.Name.Length).Name; + name = $"{name}_{group}_{000}"; + } + + if (!name.IsValidName(out var error)) + _program.Errors.Add("Program " + error); + + if (_robotSystem is RobotCellKuka) + { + var excess = name.Length - 24; + + if (excess > 0) + _program.Warnings.Add($"If using an older KRC2 or KRC3 controller, make the program name {excess} character(s) shorter."); + } + } + void FixFirstTarget(CellTarget firstTarget) { var fix = firstTarget.ProgramTargets.Where(x => !x.IsJointTarget); @@ -133,9 +155,10 @@ void FixTargetAttributes(List cellTargets) // Name attributes with no name { var types = new List(); + foreach (var attribute in _program.Attributes.ToList()) { - if (attribute.Name is null) + if (!attribute.HasName) { var type = attribute.GetType(); types.Add(type); @@ -148,11 +171,16 @@ void FixTargetAttributes(List cellTargets) // Rename attributes with duplicate names { - var duplicates = _program.Attributes.GroupBy(x => x.Name).Where(x => x.Count() > 1); + var duplicates = _program.Attributes.GroupBy(a => a.Name); + foreach (var group in duplicates) { + if (!group.Skip(1).Any()) + continue; + _program.Warnings.Add($"Multiple target attributes named \"{group.Key}\" found"); int i = 0; + foreach (var attribute in group) { string name = $"{attribute.Name}{i++:000}"; diff --git a/src/Robots/Program/Program.cs b/src/Robots/Program/Program.cs index 3d1b348..6bfd115 100644 --- a/src/Robots/Program/Program.cs +++ b/src/Robots/Program/Program.cs @@ -34,7 +34,7 @@ public Program(string name, RobotSystem robotSystem, IEnumerable tool var targets = toolpaths.Select(t => t.Targets); if (!targets.SelectMany(x => x).Any()) - throw new Exception(" The program has to contain at least 1 target."); + throw new ArgumentException(" The program has to contain at least 1 target."); int targetCount = targets.First().Count(); diff --git a/src/Robots/Program/ProgramTarget.cs b/src/Robots/Program/ProgramTarget.cs index 3bfa5ab..0c79931 100644 --- a/src/Robots/Program/ProgramTarget.cs +++ b/src/Robots/Program/ProgramTarget.cs @@ -165,5 +165,4 @@ internal void SetTargetKinematics(KinematicSolution kinematics, List err else ChangesConfiguration = false; } - -} +} \ No newline at end of file diff --git a/src/Robots/TargetAttributes/Frame.cs b/src/Robots/TargetAttributes/Frame.cs index 1a2d4c2..fab8243 100644 --- a/src/Robots/TargetAttributes/Frame.cs +++ b/src/Robots/TargetAttributes/Frame.cs @@ -17,9 +17,8 @@ public class Frame : TargetAttribute internal int CoupledPlaneIndex { get; set; } public bool IsCoupled => (CoupledMechanicalGroup != -1); - public Frame(Plane plane, int coupledMechanism = -1, int coupledMechanicalGroup = -1, string? name = null) + public Frame(Plane plane, int coupledMechanism = -1, int coupledMechanicalGroup = -1, string? name = null) : base(name) { - Name = name; Plane = plane; CoupledMechanism = coupledMechanism; CoupledMechanicalGroup = coupledMechanicalGroup; diff --git a/src/Robots/TargetAttributes/Speed.cs b/src/Robots/TargetAttributes/Speed.cs index c9b5bbc..e8f6900 100644 --- a/src/Robots/TargetAttributes/Speed.cs +++ b/src/Robots/TargetAttributes/Speed.cs @@ -36,9 +36,8 @@ public class Speed : TargetAttribute /// public double Time { get; set; } = 0; - public Speed(double translation = 100, double rotationSpeed = PI, double translationExternal = 5000, double rotationExternal = PI * 6, string? name = null) + public Speed(double translation = 100, double rotationSpeed = PI, double translationExternal = 5000, double rotationExternal = PI * 6, string? name = null) : base(name) { - Name = name; TranslationSpeed = translation; RotationSpeed = rotationSpeed; TranslationExternal = translationExternal; diff --git a/src/Robots/TargetAttributes/TargetAttribute.cs b/src/Robots/TargetAttributes/TargetAttribute.cs index 6243ee5..9c901e6 100644 --- a/src/Robots/TargetAttributes/TargetAttribute.cs +++ b/src/Robots/TargetAttributes/TargetAttribute.cs @@ -2,10 +2,30 @@ public abstract class TargetAttribute { + string? _name; + + protected TargetAttribute(string? name) + { + if (name is not null) + Name = name; + } + /// /// Name of the attribute /// - public string? Name { get; internal set; } + public string Name + { + get => _name.NotNull(); + private set + { + if (!value.IsValidName(out var error)) + throw new ArgumentException($" {GetType().Name} {error}"); + + _name = value; + } + } + + public bool HasName => _name is not null; public T CloneWithName(string name) where T : TargetAttribute { @@ -13,4 +33,4 @@ public T CloneWithName(string name) where T : TargetAttribute attribute.Name = name; return attribute; } -} +} \ No newline at end of file diff --git a/src/Robots/TargetAttributes/Tool.cs b/src/Robots/TargetAttributes/Tool.cs index ae543fb..da9739a 100644 --- a/src/Robots/TargetAttributes/Tool.cs +++ b/src/Robots/TargetAttributes/Tool.cs @@ -6,33 +6,45 @@ public class Tool : TargetAttribute { public static Tool Default { get; } = new Tool(Plane.WorldXY, "DefaultTool"); - public Plane Tcp { get; private set; } + public Plane Tcp { get; } public double Weight { get; } public Point3d Centroid { get; } public Mesh Mesh { get; } - public Tool(Plane tcp, string? name = null, double weight = 0, Point3d? centroid = null, Mesh? mesh = null) + public Tool(Plane tcp, string? name = null, double weight = 0, Point3d? centroid = null, Mesh? mesh = null, IList? calibrationPlanes = null) : base(name) { - Name = name; - Tcp = tcp; Weight = weight; Centroid = (centroid is null) ? tcp.Origin : (Point3d)centroid; Mesh = mesh ?? new Mesh(); + + if (calibrationPlanes is null || !calibrationPlanes.Any()) + { + Tcp = tcp; + } + else + { + if (calibrationPlanes.Count != 4) + throw new ArgumentException(" Calibration requires 4 planes."); + + var origin = FourPointCalibration(calibrationPlanes); + Tcp = new Plane(origin, tcp.XAxis, tcp.YAxis); + } } - public void FourPointCalibration(Plane a, Plane b, Plane c, Plane d) + Point3d FourPointCalibration(IList calibrationPlanes) { - var calibrate = new Geometry.CircumcentreSolver(a.Origin, b.Origin, c.Origin, d.Origin); + var p = calibrationPlanes; + var calibrate = new Geometry.CircumcentreSolver(p[0].Origin, p[1].Origin, p[2].Origin, p[3].Origin); Point3d tcpOrigin = Point3d.Origin; - foreach (Plane plane in new Plane[] { a, b, c, d }) + foreach (Plane plane in calibrationPlanes) { plane.RemapToPlaneSpace(calibrate.Center, out Point3d remappedPoint); tcpOrigin += remappedPoint; } - tcpOrigin /= 4; - Tcp = new Plane(tcpOrigin, Tcp.XAxis, Tcp.YAxis); + tcpOrigin /= 4.0; + return tcpOrigin; } public override string ToString() => $"Tool ({Name})"; diff --git a/src/Robots/TargetAttributes/Zone.cs b/src/Robots/TargetAttributes/Zone.cs index 5a92b65..4222ac7 100644 --- a/src/Robots/TargetAttributes/Zone.cs +++ b/src/Robots/TargetAttributes/Zone.cs @@ -29,9 +29,8 @@ public class Zone : TargetAttribute // RotationExternal = Rotation; //} - public Zone(double distance, double? rotation = null, double? rotationExternal = null, string? name = null) + public Zone(double distance, double? rotation = null, double? rotationExternal = null, string? name = null) : base(name) { - Name = name; Distance = distance; if (rotation.HasValue) diff --git a/src/Robots/Util.cs b/src/Robots/Util.cs index 6c97762..85b1cc8 100644 --- a/src/Robots/Util.cs +++ b/src/Robots/Util.cs @@ -13,7 +13,7 @@ static class Util public const double UnitTol = 0.000001; public const double SingularityTol = 0.0001; public const double HalfPI = PI * 0.5; - public const double PI2 = PI * 2.0; + public const double PI2 = PI * 2.0; // File @@ -84,6 +84,28 @@ public static T[] RangeSubset(this T[] array, int startIndex, int length) return subset; } + public static T MaxBy(this IEnumerable list, Func comparable) where K : IComparable + { + if (!list.Any()) + throw new ArgumentException("List can't be empty."); + + T maxItem = list.First(); + K maxValue = comparable(maxItem); + + foreach (var item in list.Skip(1)) + { + var val = comparable(item); + + if (val.CompareTo(maxValue) > 0) + { + maxValue = val; + maxItem = item; + } + } + + return maxItem; + } + public static IEnumerable> Transpose(this IEnumerable> source) { var enumerators = source.Select(e => e.GetEnumerator()).ToArray();