public GCodeCommand(string line) { _parts = GCodeCommandPart.ParseStringToParts(line).ToArray(); var firstPart = _parts.FirstOrDefault(part => part.Type == GCodeCommandPartType.CharacterAndNumber); if (firstPart != null) { CommandType = firstPart.Character; CommandNumber = (int)firstPart.Number; Command = CommandCache.Get(CommandType, CommandNumber); } }
public static IEnumerable <GCodeCommandPart> ParseStringToParts(string line) { bool isFirstPart = true; int index = 0; while (index < line.Length) { if (line[index] == ';') { yield return (new GCodeCommandPart() { Type = GCodeCommandPartType.Comment, Text = line.Substring(index + 1), }); yield break; } if (char.IsWhiteSpace(line[index])) { int count = 1; index++; while ((index < line.Length) && char.IsWhiteSpace(line, index)) { count++; index++; } yield return (new GCodeCommandPart() { Type = GCodeCommandPartType.Space, Number = count, }); } else { GCodeCommandPart part = new GCodeCommandPart(); part.Type = GCodeCommandPartType.CharacterAndNumber; part.Character = line[index]; index++; int numberStart = index; while ((index < line.Length) && !char.IsWhiteSpace(line, index)) { index++; } part.Number = decimal.Parse(line.Substring(numberStart, index - numberStart)); yield return(part); bool wasFirstPart = isFirstPart; isFirstPart = false; if (wasFirstPart && (part.Character == 'M') && ((part.Number == 117m) || (part.Number == 118m)) && (index < line.Length)) { // Return remainder of line as a single text block int count = 1; index++; while ((index < line.Length) && char.IsWhiteSpace(line, index)) { count++; index++; } yield return (new GCodeCommandPart() { Type = GCodeCommandPartType.Space, Number = count, }); if (index < line.Length) { yield return (new GCodeCommandPart() { Type = GCodeCommandPartType.Text, Text = line.Substring(index), }); } yield break; } } } }