|
| 1 | +/* |
| 2 | +---- Copyright Start ---- |
| 3 | +
|
| 4 | +This file is part of the OpenVectorFormatTools collection. This collection provides tools to facilitate the usage of the OpenVectorFormat. |
| 5 | +
|
| 6 | +Copyright (C) 2025 Digital-Production-Aachen |
| 7 | +
|
| 8 | +This library is free software; you can redistribute it and/or |
| 9 | +modify it under the terms of the GNU Lesser General Public |
| 10 | +License as published by the Free Software Foundation; either |
| 11 | +version 2.1 of the License, or (at your option) any later version. |
| 12 | +
|
| 13 | +This library is distributed in the hope that it will be useful, |
| 14 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | +Lesser General Public License for more details. |
| 17 | +
|
| 18 | +You should have received a copy of the GNU Lesser General Public |
| 19 | +License along with this library; if not, write to the Free Software |
| 20 | +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | +
|
| 22 | +---- Copyright End ---- |
| 23 | +*/ |
| 24 | + |
| 25 | +using System; |
| 26 | +using System.Collections; |
| 27 | +using System.Collections.Generic; |
| 28 | +using System.Linq; |
| 29 | +using System.Numerics; |
| 30 | +using System.Runtime.CompilerServices; |
| 31 | +using System.Security.Cryptography.X509Certificates; |
| 32 | +using System.Text; |
| 33 | +using System.Text.RegularExpressions; |
| 34 | +using System.Globalization; |
| 35 | +using OpenVectorFormat.Utils; |
| 36 | +using System.Net.Http.Headers; |
| 37 | + |
| 38 | +namespace GCodeReaderWriter.Commands |
| 39 | +{ |
| 40 | + // Possible preparatory function codes |
| 41 | + public enum PrepCode |
| 42 | + { |
| 43 | + G, |
| 44 | + M, |
| 45 | + T, |
| 46 | + Comment |
| 47 | + } |
| 48 | + |
| 49 | + // Represents a basic GCode with a preparatory function code and a code number |
| 50 | + public readonly struct GCode |
| 51 | + { |
| 52 | + public readonly PrepCode preparatoryFunctionCode; |
| 53 | + public readonly int codeNumber; |
| 54 | + |
| 55 | + public GCode(PrepCode preparatoryFunctionCode, int codeNumber) |
| 56 | + { |
| 57 | + this.preparatoryFunctionCode = preparatoryFunctionCode; |
| 58 | + this.codeNumber = codeNumber; |
| 59 | + } |
| 60 | + |
| 61 | + public override string ToString() |
| 62 | + { |
| 63 | + return $"{preparatoryFunctionCode}{codeNumber}"; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public class ToolParams |
| 68 | + { |
| 69 | + // Number of the currently equipped tool |
| 70 | + int toolNumber; |
| 71 | + } |
| 72 | + |
| 73 | + public abstract class GCodeCommand |
| 74 | + { |
| 75 | + // The GCode of the command |
| 76 | + public readonly GCode gCode; |
| 77 | + |
| 78 | + public readonly string comment; |
| 79 | + |
| 80 | + // Dictionary of all unassignable parameters in a GCode line |
| 81 | + public Dictionary<char, float> miscParams; |
| 82 | + |
| 83 | + // List of all recorded parameters in a GCode line |
| 84 | + public readonly List<char> recordedParams; |
| 85 | + |
| 86 | + // Dicionary mapping parameter characters to their respective class variables |
| 87 | + protected static Dictionary<char, Action<float>> parameterMap; |
| 88 | + |
| 89 | + public GCodeCommand(GCode gCode, Dictionary<char, float> commandParams = null, string comment = null) |
| 90 | + { |
| 91 | + miscParams = new Dictionary<char, float>(); |
| 92 | + recordedParams = new List<char>(); |
| 93 | + parameterMap = new Dictionary<char, Action<float>>(); |
| 94 | + this.gCode = gCode; |
| 95 | + this.comment = comment; |
| 96 | + } |
| 97 | + |
| 98 | + public GCodeCommand(PrepCode prepCode, int codeNumber, Dictionary<char, float> commandParams = null, string comment = null) |
| 99 | + { |
| 100 | + miscParams = new Dictionary<char, float>(); |
| 101 | + recordedParams = new List<char>(); |
| 102 | + parameterMap = new Dictionary<char, Action<float>>(); |
| 103 | + gCode = new GCode(prepCode, codeNumber); |
| 104 | + this.comment = comment; |
| 105 | + } |
| 106 | + |
| 107 | + // Is used by child classes to initialize their parameter map |
| 108 | + protected static void InitParameterMap() |
| 109 | + { |
| 110 | + } |
| 111 | + |
| 112 | + // Iterates through all given parameter characters and assigns the according values to the respective variables |
| 113 | + protected void ParseParams(Dictionary<char, float> commandParams) |
| 114 | + { |
| 115 | + foreach (var commandParam in commandParams) |
| 116 | + { |
| 117 | + if (parameterMap.ContainsKey(commandParam.Key)) |
| 118 | + { |
| 119 | + parameterMap[commandParam.Key](commandParam.Value); |
| 120 | + recordedParams.Add(commandParam.Key); |
| 121 | + commandParams.Remove(commandParam.Key); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // If there are still unknown parameters left, they are added to the miscParams dictionary |
| 126 | + miscParams = commandParams; |
| 127 | + } |
| 128 | + |
| 129 | + public override string ToString() |
| 130 | + { |
| 131 | + return gCode.ToString(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public class GCodeConverter |
| 136 | + { |
| 137 | + private readonly Dictionary<int, Type> _gCodeTranslations = new Dictionary<int, Type> |
| 138 | + { |
| 139 | + {0, typeof(LinearInterpolationCmd)}, |
| 140 | + {1, typeof(LinearInterpolationCmd)}, |
| 141 | + {2, typeof(CircularInterpolationCmd)}, |
| 142 | + {3, typeof(CircularInterpolationCmd)}, |
| 143 | + {4, typeof(PauseCommand)}, |
| 144 | + }; |
| 145 | + |
| 146 | + private Dictionary<int, Type> _mCodeTranslations = new Dictionary<int, Type>(); |
| 147 | + |
| 148 | + private Dictionary<int, Type> _tCodeTranslations = new Dictionary<int, Type>(); |
| 149 | + |
| 150 | + public GCodeConverter() |
| 151 | + { |
| 152 | + // Set culture info, so that floats are parsed correctly |
| 153 | + CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture; |
| 154 | + CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture; |
| 155 | + } |
| 156 | + |
| 157 | + public GCodeCommand ParseLine(string serializedCmdLine) |
| 158 | + { |
| 159 | + string[] commentSplit = serializedCmdLine.Split(';'); |
| 160 | + string commandString = commentSplit[0].Trim(); |
| 161 | + string commentString = commentSplit.Length > 1 ? commentSplit[1].Trim() : null; |
| 162 | + |
| 163 | + if (string.IsNullOrEmpty(commandString)) |
| 164 | + { |
| 165 | + if (string.IsNullOrEmpty(commentString)) |
| 166 | + { |
| 167 | + return null; |
| 168 | + } |
| 169 | + return Activator.CreateInstance(typeof(MiscCommand), new object[] { PrepCode.Comment, 0, null, commentString }) as GCodeCommand; |
| 170 | + } |
| 171 | + |
| 172 | + string[] commandArr = commandString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); |
| 173 | + char commandChar; |
| 174 | + |
| 175 | + commandChar = char.ToUpper(commandArr[0][0]); |
| 176 | + |
| 177 | + if (!Enum.TryParse(commandChar.ToString(), out PrepCode prepCode)) |
| 178 | + throw new ArgumentException($"Invalid preparatory function code: {commandChar} in line '{serializedCmdLine}'"); |
| 179 | + |
| 180 | + string commandNumber = commandArr[0].Substring(1); |
| 181 | + if (!int.TryParse(commandNumber, out int codeNumber)) |
| 182 | + throw new ArgumentException($"Invalid number format: {commandNumber} in line '{serializedCmdLine}'"); |
| 183 | + |
| 184 | + Dictionary<char, float> commandParams = new Dictionary<char, float>(); |
| 185 | + |
| 186 | + foreach (var commandParam in commandArr.Skip(1)) |
| 187 | + { |
| 188 | + if (float.TryParse(commandParam.Substring(1), out float paramValue)) |
| 189 | + { |
| 190 | + commandParams[commandParam[0]] = paramValue; |
| 191 | + } |
| 192 | + else if (commandParam.Length == 1) |
| 193 | + { |
| 194 | + commandParams[commandParam[0]] = 0; |
| 195 | + } |
| 196 | + else |
| 197 | + { |
| 198 | + throw new ArgumentException($"Invalid command parameter format: {commandParam} in line '{serializedCmdLine}'. Command parameters must be of format <char><float>"); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if (_gCodeTranslations.TryGetValue(codeNumber, out Type gCodeClassType)) |
| 203 | + { |
| 204 | + return Activator.CreateInstance(gCodeClassType, new object[] { prepCode, codeNumber, commandParams, commentString }) as GCodeCommand; |
| 205 | + } |
| 206 | + |
| 207 | + return Activator.CreateInstance(typeof(MiscCommand), new object[] { prepCode, codeNumber, commandParams, commentString }) as GCodeCommand; |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + public class GCodeCommandList : List<GCodeCommand> |
| 212 | + { |
| 213 | + private readonly Dictionary<int, Type> _gCodeTranslations = new Dictionary<int, Type> |
| 214 | + { |
| 215 | + {0, typeof(LinearInterpolationCmd)}, |
| 216 | + {1, typeof(LinearInterpolationCmd)}, |
| 217 | + {2, typeof(CircularInterpolationCmd)}, |
| 218 | + {3, typeof(CircularInterpolationCmd)}, |
| 219 | + {4, typeof(PauseCommand)}, |
| 220 | + }; |
| 221 | + |
| 222 | + private Dictionary<int, Type> _mCodeTranslations = new Dictionary<int, Type>(); |
| 223 | + |
| 224 | + private Dictionary<int, Type> _tCodeTranslations = new Dictionary<int, Type>(); |
| 225 | + |
| 226 | + public GCodeCommandList(string[] commandLines) |
| 227 | + { |
| 228 | + // Set culture info, so that floats are parsed correctly |
| 229 | + CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture; |
| 230 | + CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture; |
| 231 | + |
| 232 | + // Try to parse the given command lines to GCodeCommand objects |
| 233 | + TryParse(commandLines); |
| 234 | + } |
| 235 | + |
| 236 | + public void TryParse(string[] commandLines) |
| 237 | + { |
| 238 | + foreach (string line in commandLines) |
| 239 | + { |
| 240 | + try |
| 241 | + { |
| 242 | + GCodeCommand parseCommand = ParseLine(line); |
| 243 | + if (parseCommand != null) |
| 244 | + { |
| 245 | + Add(parseCommand); |
| 246 | + } |
| 247 | + else |
| 248 | + { |
| 249 | + continue; |
| 250 | + } |
| 251 | + } |
| 252 | + catch (ArgumentException e) |
| 253 | + { |
| 254 | + Console.WriteLine(e.Message); |
| 255 | + continue; |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + public GCodeCommand ParseLine(string serializedCmdLine) |
| 261 | + { |
| 262 | + string[] commentSplit = serializedCmdLine.Split(';'); |
| 263 | + string commandString = commentSplit[0].Trim(); |
| 264 | + string commentString = commentSplit.Length > 1 ? commentSplit[1].Trim() : null; |
| 265 | + |
| 266 | + if (string.IsNullOrEmpty(commandString)) |
| 267 | + { |
| 268 | + if (string.IsNullOrEmpty(commentString)) |
| 269 | + { |
| 270 | + return null; |
| 271 | + } |
| 272 | + return Activator.CreateInstance(typeof(MiscCommand), new object[] { PrepCode.Comment, 0, null, commentString }) as GCodeCommand; |
| 273 | + } |
| 274 | + |
| 275 | + string[] commandArr = commandString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); |
| 276 | + char commandChar; |
| 277 | + |
| 278 | + commandChar = char.ToUpper(commandArr[0][0]); |
| 279 | + |
| 280 | + if (!Enum.TryParse(commandChar.ToString(), out PrepCode prepCode)) |
| 281 | + throw new ArgumentException($"Invalid preparatory function code: {commandChar} in line '{serializedCmdLine}'"); |
| 282 | + |
| 283 | + string commandNumber = commandArr[0].Substring(1); |
| 284 | + if (!int.TryParse(commandNumber, out int codeNumber)) |
| 285 | + throw new ArgumentException($"Invalid number format: {commandNumber} in line '{serializedCmdLine}'"); |
| 286 | + |
| 287 | + Dictionary<char, float> commandParams = new Dictionary<char, float>(); |
| 288 | + |
| 289 | + foreach (var commandParam in commandArr.Skip(1)) |
| 290 | + { |
| 291 | + if (float.TryParse(commandParam.Substring(1), out float paramValue)) |
| 292 | + { |
| 293 | + commandParams[commandParam[0]] = paramValue; |
| 294 | + } |
| 295 | + else if (commandParam.Length == 1) |
| 296 | + { |
| 297 | + commandParams[commandParam[0]] = 0; |
| 298 | + } |
| 299 | + else |
| 300 | + { |
| 301 | + throw new ArgumentException($"Invalid command parameter format: {commandParam} in line '{serializedCmdLine}'. Command parameters must be of format <char><float>"); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + if (_gCodeTranslations.TryGetValue(codeNumber, out Type gCodeClassType)) |
| 306 | + { |
| 307 | + return Activator.CreateInstance(gCodeClassType, new object[] { prepCode, codeNumber, commandParams, commentString }) as GCodeCommand; |
| 308 | + } |
| 309 | + |
| 310 | + return Activator.CreateInstance(typeof(MiscCommand), new object[] { prepCode, codeNumber, commandParams, commentString }) as GCodeCommand; |
| 311 | + } |
| 312 | + } |
| 313 | +} |
0 commit comments