@@ -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 >
0 commit comments