Skip to content

Commit 416b92a

Browse files
author
Sven Erb
committed
Updated to streamreader usage
1 parent 245e045 commit 416b92a

File tree

2 files changed

+117
-31
lines changed

2 files changed

+117
-31
lines changed

ReaderWriter/3rdPartyFormatAdapters/GCODE/GCodeReaderWriter/GCodeCommand.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,82 @@ public override string ToString()
535535
}
536536
}
537537

538+
public class GCodeConverter
539+
{
540+
private readonly Dictionary<int, System.Type> _gCodeTranslations = new Dictionary<int, System.Type>
541+
{
542+
{0, typeof(LinearInterpolationCmd)},
543+
{1, typeof(LinearInterpolationCmd)},
544+
{2, typeof(CircularInterpolationCmd)},
545+
{3, typeof(CircularInterpolationCmd)},
546+
{4, typeof(PauseCommand)},
547+
};
548+
549+
private Dictionary<int, System.Type> _mCodeTranslations = new Dictionary<int, System.Type>();
550+
551+
private Dictionary<int, System.Type> _tCodeTranslations = new Dictionary<int, System.Type>();
552+
553+
public GCodeConverter()
554+
{
555+
// Set culture info, so that floats are parsed correctly
556+
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
557+
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
558+
}
559+
560+
public GCodeCommand ParseLine(string serializedCmdLine)
561+
{
562+
string[] commentSplit = serializedCmdLine.Split(';');
563+
string commandString = commentSplit[0].Trim();
564+
string commentString = (commentSplit.Length > 1) ? commentSplit[1].Trim() : null;
565+
566+
if (string.IsNullOrEmpty(commandString))
567+
{
568+
if (string.IsNullOrEmpty(commentString))
569+
{
570+
return null;
571+
}
572+
return Activator.CreateInstance(typeof(MiscCommand), new Object[] { PrepCode.Comment, 0, null, commentString }) as GCodeCommand;
573+
}
574+
575+
string[] commandArr = commandString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
576+
char commandChar;
577+
578+
commandChar = char.ToUpper(commandArr[0][0]);
579+
580+
if (!System.Enum.TryParse(commandChar.ToString(), out PrepCode prepCode))
581+
throw new ArgumentException($"Invalid preparatory function code: {commandChar} in line '{serializedCmdLine}'");
582+
583+
string commandNumber = commandArr[0].Substring(1);
584+
if (!int.TryParse(commandNumber, out int codeNumber))
585+
throw new ArgumentException($"Invalid number format: {commandNumber} in line '{serializedCmdLine}'");
586+
587+
Dictionary<char, float> commandParams = new Dictionary<char, float>();
588+
589+
foreach (var commandParam in commandArr.Skip(1))
590+
{
591+
if (float.TryParse(commandParam.Substring(1), out float paramValue))
592+
{
593+
commandParams[commandParam[0]] = paramValue;
594+
}
595+
else if (commandParam.Length == 1)
596+
{
597+
commandParams[commandParam[0]] = 0;
598+
}
599+
else
600+
{
601+
throw new ArgumentException($"Invalid command parameter format: {commandParam} in line '{serializedCmdLine}'. Command parameters must be of format <char><float>");
602+
}
603+
}
604+
605+
if (_gCodeTranslations.TryGetValue(codeNumber, out System.Type gCodeClassType))
606+
{
607+
return Activator.CreateInstance(gCodeClassType, new Object[] { prepCode, codeNumber, commandParams, commentString }) as GCodeCommand;
608+
}
609+
610+
return Activator.CreateInstance(typeof(MiscCommand), new Object[] { prepCode, codeNumber, commandParams, commentString }) as GCodeCommand;
611+
}
612+
}
613+
538614
public class GCodeCommandList : List<GCodeCommand>
539615
{
540616
private readonly Dictionary<int, System.Type> _gCodeTranslations = new Dictionary<int, System.Type>

ReaderWriter/3rdPartyFormatAdapters/GCODE/GCodeReaderWriter/GCodeReader.cs

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -197,43 +197,53 @@ public void ParseGCodeFile(IFileReaderWriterProgress progress = null)
197197
}
198198
};
199199

200-
int gCodeCommandCount = gCodeCommands.Count;
200+
//int gCodeCommandCount = gCodeCommands.Count;
201201
int percentComplete = 0;
202202

203-
for (int i = 0; i < gCodeCommandCount; i++)
203+
using (StreamReader sr = new StreamReader(_filename))
204204
{
205-
//commandChanges = commandStateTracker.UpdateState(gCodeCommands[i]);
206-
switch (gCodeCommands[i])
205+
string line;
206+
GCodeConverter gCodeConverter = new GCodeConverter();
207+
208+
while ((line = sr.ReadLine()) != null)
207209
{
208-
// Process command according to the command-type
209-
case MovementCommand movementCmd:
210-
ProcessMovementCmd(movementCmd);
211-
break;
212-
case PauseCommand pauseCmd:
213-
ProcessPauseCmd(pauseCmd);
214-
break;
215-
case ToolChangeCommand toolChangeCmd:
216-
ProcessToolChangeCmd(toolChangeCmd);
217-
break;
218-
case MonitoringCommand monitoringCmd:
219-
ProcessMonitoringCmd(monitoringCmd);
220-
break;
221-
case ProgramLogicsCommand programLogicsCmd:
222-
ProcessProgramLogicsCmd(programLogicsCmd);
223-
break;
224-
case MiscCommand miscCmd:
225-
ProcessMiscCmd(miscCmd);
226-
break;
227-
}
210+
object command = gCodeConverter.ParseLine(line);
228211

229-
// Reset locking state for new vector blocks
230-
VBlocked = false;
212+
//commandChanges = commandStateTracker.UpdateState(gCodeCommands[i]);
213+
switch (command)
214+
{
215+
// Process command according to the command-type
216+
case MovementCommand movementCmd:
217+
ProcessMovementCmd(movementCmd);
218+
break;
219+
case PauseCommand pauseCmd:
220+
ProcessPauseCmd(pauseCmd);
221+
break;
222+
case ToolChangeCommand toolChangeCmd:
223+
ProcessToolChangeCmd(toolChangeCmd);
224+
break;
225+
case MonitoringCommand monitoringCmd:
226+
ProcessMonitoringCmd(monitoringCmd);
227+
break;
228+
case ProgramLogicsCommand programLogicsCmd:
229+
ProcessProgramLogicsCmd(programLogicsCmd);
230+
break;
231+
case MiscCommand miscCmd:
232+
ProcessMiscCmd(miscCmd);
233+
break;
234+
}
231235

232-
// Update progress
233-
if (percentComplete != (int) (i + 1) * 100 / gCodeCommandCount)
234-
{
235-
percentComplete = (i + 1) * 100 / gCodeCommandCount;
236-
progress?.Update("Command " + i + " of " + gCodeCommandCount, percentComplete);
236+
// Reset locking state for new vector blocks
237+
VBlocked = false;
238+
239+
// Update progress
240+
/*
241+
if (percentComplete != (int)(i + 1) * 100 / gCodeCommandCount)
242+
{
243+
percentComplete = (i + 1) * 100 / gCodeCommandCount;
244+
progress?.Update("Command " + i + " of " + gCodeCommandCount, percentComplete);
245+
}
246+
*/
237247
}
238248
}
239249

0 commit comments

Comments
 (0)