@@ -33,6 +33,8 @@ You should have received a copy of the GNU Lesser General Public
3333using System . Text . RegularExpressions ;
3434using System . Globalization ;
3535using OpenVectorFormat . Utils ;
36+ using System . Net . Http . Headers ;
37+ using OpenVectorFormat . GCodeReaderWriter ;
3638
3739namespace OpenVectorFormat . GCodeReaderWriter
3840{
@@ -43,7 +45,6 @@ public enum PrepCode
4345 T
4446 }
4547
46-
4748 public struct GCode
4849 {
4950 public readonly PrepCode preparatoryFunctionCode ;
@@ -66,170 +67,6 @@ class ToolParams
6667 int toolNumber ;
6768 }
6869
69- public class GCodeState
70- {
71- private readonly Dictionary < int , Type > _gCodeTranslations = new Dictionary < int , Type >
72- {
73- { 0 , typeof ( LinearInterpolationCmd ) } ,
74- { 1 , typeof ( LinearInterpolationCmd ) } ,
75- { 2 , typeof ( CircularInterpolationCmd ) } ,
76- { 3 , typeof ( CircularInterpolationCmd ) } ,
77- { 4 , typeof ( PauseCommand ) } ,
78- } ;
79-
80- internal class GCodeProperties
81- {
82- internal Type gCodeType ;
83- internal List < char > recordedParams ;
84- internal Dictionary < char , float > miscParams ;
85- }
86-
87- internal class GCodeMarkingParams
88- {
89- // Marking parameters
90- internal float workSpeed = 0 ;
91- internal float travelSpeed = 0 ;
92- internal float acceleration = 0 ;
93-
94- // Pause parameters
95- internal float pauseDuration = 0 ;
96-
97- // Other parameters
98- internal ToolParams toolParams ;
99-
100- Dictionary < string , float > miscParameters = new Dictionary < string , float > ( ) ;
101-
102- internal GCodeMarkingParams ( )
103- {
104-
105- }
106- }
107-
108- internal class GCodePosition
109- {
110- internal Vector3 position ;
111- internal Vector2 centerRel ;
112- internal GCodePosition ( )
113- {
114-
115- }
116- }
117-
118- private Dictionary < int , Type > _mCodeTranslations = new Dictionary < int , Type > ( ) ;
119-
120- private Dictionary < int , Type > _tCodeTranslations = new Dictionary < int , Type > ( ) ;
121-
122- public GCodeCommand gCodeCommand ;
123- internal Vector3 position ;
124-
125- public GCodeState ( string serializedCmdLine )
126- {
127- CultureInfo . DefaultThreadCurrentCulture = CultureInfo . InvariantCulture ;
128- CultureInfo . DefaultThreadCurrentUICulture = CultureInfo . InvariantCulture ;
129- this . gCodeCommand = ParseToGCodeCommand ( serializedCmdLine ) ;
130- }
131-
132- public GCodeCommand ParseToGCodeCommand ( string serializedCmdLine )
133- {
134- string commandString = serializedCmdLine . Split ( ';' ) [ 0 ] . Trim ( ) ;
135- string [ ] commandArr = commandString . Split ( new [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
136-
137- char commandChar = char . ToUpper ( commandArr [ 0 ] [ 0 ] ) ;
138- if ( ! Enum . TryParse ( commandChar . ToString ( ) , out PrepCode prepCode ) )
139- throw new ArgumentException ( $ "Invalid preparatory function code: { commandChar } in line '{ serializedCmdLine } '") ;
140-
141- string commandNumber = commandArr [ 0 ] . Substring ( 1 ) ;
142- if ( ! int . TryParse ( commandNumber , out int codeNumber ) )
143- throw new ArgumentException ( $ "Invalid number format: { commandNumber } in line '{ serializedCmdLine } '") ;
144-
145- Dictionary < char , float > commandParams = new Dictionary < char , float > ( ) ;
146- Console . WriteLine ( string . Join ( Environment . NewLine , commandArr ) ) ;
147-
148- foreach ( var commandParam in commandArr . Skip ( 1 ) )
149- {
150- if ( float . TryParse ( commandParam . Substring ( 1 ) , out float paramValue ) )
151- {
152- commandParams [ commandParam [ 0 ] ] = paramValue ;
153- }
154- else if ( commandParam . Length == 1 )
155- {
156- commandParams [ commandParam [ 0 ] ] = 0 ;
157- }
158- else
159- {
160- throw new ArgumentException ( $ "Invalid command parameter format: { commandParam } in line '{ serializedCmdLine } '. Command parameters must be of format <char><float>") ;
161- }
162- }
163- Console . WriteLine ( string . Join ( Environment . NewLine , commandParams ) ) ;
164- if ( _gCodeTranslations . TryGetValue ( codeNumber , out Type gCodeClassType ) )
165- {
166- return Activator . CreateInstance ( gCodeClassType , new Object [ ] { prepCode , codeNumber , commandParams } ) as GCodeCommand ;
167- }
168-
169- return Activator . CreateInstance ( typeof ( MiscCommand ) , new Object [ ] { prepCode , codeNumber , commandParams } ) as GCodeCommand ;
170- }
171-
172- public bool [ ] Update ( string serializedCmdLine )
173- {
174- GCodeCommand previousGCodeCommand = this . gCodeCommand ;
175- GCodeCommand currentGCodeCommand = ParseToGCodeCommand ( serializedCmdLine ) ;
176-
177- bool workPlaneChanged = false ;
178- bool markingParamsChanged = false ;
179- bool vectorBlockChanged = false ;
180-
181- if ( previousGCodeCommand . GetType ( ) != currentGCodeCommand . GetType ( ) )
182- {
183- vectorBlockChanged = true ;
184- }
185- markingParamsChanged = UpdateParameters ( ) ;
186- workPlaneChanged = updatePosition ( ) ;
187-
188- bool UpdateParameters ( )
189- {
190- bool parametersChanged = false ;
191- foreach ( var property in previousGCodeCommand . GetType ( ) . GetProperties ( ) )
192- {
193- var previousValue = property . GetValue ( previousGCodeCommand ) ;
194- var currentValue = property . GetValue ( currentGCodeCommand ) ;
195-
196- if ( ! Equals ( previousValue , currentValue ) )
197- {
198- property . SetValue ( currentGCodeCommand , previousValue ) ;
199- parametersChanged = ( property . Name != "xPosition" && property . Name != "yPosition" && property . Name != "zPosition" ) ;
200- }
201- }
202-
203- return parametersChanged ;
204- }
205-
206- bool updatePosition ( )
207- {
208- bool zChange = false ;
209- if ( currentGCodeCommand is MovementCommand movementCmd )
210- {
211- try
212- {
213- if ( movementCmd . zPosition != position . Z )
214- {
215- zChange = true ;
216- }
217- position = new Vector3 ( ( float ) movementCmd . xPosition , ( float ) movementCmd . yPosition , ( float ) movementCmd . zPosition ) ;
218- }
219- catch ( Exception e )
220- {
221- throw new ArgumentException ( $ "Unable to update position in line '{ serializedCmdLine } '. Creating a Vector object from command was not possible.") ;
222- }
223- }
224- return zChange ;
225- }
226-
227- this . gCodeCommand = currentGCodeCommand ;
228-
229- return new bool [ ] { workPlaneChanged , markingParamsChanged , vectorBlockChanged } ;
230- }
231- }
232-
23370 public class GCodeCommand
23471 {
23572 public readonly GCode gCode ;
@@ -296,12 +133,15 @@ protected void ParseParams(Dictionary<char, float> commandParams)
296133
297134 public override string ToString ( )
298135 {
136+ /*
299137 string outString = gCode.ToString();
300138 foreach (var param in miscParams)
301139 {
302140 outString += $" {param.Key}{param.Value}";
303141 }
304142 return outString;
143+ */
144+ return this . gCode . ToString ( ) ;
305145 }
306146 }
307147
@@ -444,18 +284,6 @@ private void CheckDirection()
444284 // Move angle calculation below to gcode state object or to transition point of OVF-Parameters
445285 // this.angle = (float) Math.Atan2((double)(yPosition - yCenterRel),(double) (xPosition - xCenterRel)) - (float) Math.Atan2((double)(yStartPos - yCenterRel), (double) (xStartPos - xCenterRel));
446286
447-
448- /*
449- public CircularInterpolationCmd(Vector2 startPos, Vector2 targetPos, float radius, ToolParams toolParams, PrepCode prepCode, int codeNumber, float? feedRate = null, float? zHeight = null)
450- : base(startPos, toolParams, prepCode, codeNumber, feedRate, zHeight)
451- {
452- float pointDistance = (float)Math.Sqrt(Math.Pow(targetPos.X - startPos.X, 2) + Math.Pow(targetPos.Y - startPos.Y, 2));
453- float centerRelX = (float)(startPos.X + radius * Math.Cos(Math.Asin(radius / pointDistance)));
454- float centerRelY = (float)(startPos.Y + radius * Math.Sin(Math.Asin(radius / pointDistance)));
455- this.centerRel = new Vector2(centerRelX, centerRelY);
456- this.angle = (float)Math.Atan2(targetPos.Y - this.centerRel.Y, targetPos.X - this.centerRel.X) - (float)Math.Atan2(startPos.Y - this.centerRel.Y, startPos.X - this.centerRel.X);
457- }
458- */
459287 public override string ToString ( )
460288 {
461289 return base . ToString ( ) + ( xCenterRel != null ? $ " I{ xCenterRel } " : "" ) + ( yCenterRel != null ? $ " I{ yCenterRel } " : "" ) ;
@@ -617,26 +445,106 @@ public override string ToString()
617445
618446 internal class MiscCommand : GCodeCommand
619447 {
620- public readonly Dictionary < string , float > cmdParams ;
621448
622- public MiscCommand ( PrepCode prepCode , int codeNumber , Dictionary < string , float > cmdParams = null ) : base ( prepCode , codeNumber )
449+ public MiscCommand ( PrepCode prepCode , int codeNumber , Dictionary < char , float > cmdParams = null ) : base ( prepCode , codeNumber )
623450 {
624- this . cmdParams = cmdParams ;
451+ this . miscParams = cmdParams ;
625452 }
626- public MiscCommand ( GCode gCode , Dictionary < string , float > cmdParams = null ) : base ( gCode )
453+ public MiscCommand ( GCode gCode , Dictionary < char , float > cmdParams = null ) : base ( gCode )
627454 {
628- this . cmdParams = cmdParams ;
455+ this . miscParams = cmdParams ;
629456 }
630457 public override string ToString ( )
631458 {
459+ /*
632460 string outString = base.ToString();
633- if ( cmdParams != null ) {
634- for ( int i = 0 ; i < cmdParams . Count ; i ++ )
461+ if (this.miscParams != null)
462+ {
463+ for (int i = 0; i < this.miscParams.Count; i++)
635464 {
636- outString += $ " { cmdParams . Keys . ElementAt ( i ) } { cmdParams . Values . ElementAt ( i ) } ";
465+ outString += $" {this.miscParams. Keys.ElementAt(i)}{this.miscParams .Values.ElementAt(i)}";
637466 }
638467 }
639468 return outString;
469+ */
470+ return base . ToString ( ) ;
471+ }
472+ }
473+ }
474+
475+ public class GCodeCommandList : List < GCodeCommand >
476+ {
477+ private readonly Dictionary < int , Type > _gCodeTranslations = new Dictionary < int , Type >
478+ {
479+ { 0 , typeof ( LinearInterpolationCmd ) } ,
480+ { 1 , typeof ( LinearInterpolationCmd ) } ,
481+ { 2 , typeof ( CircularInterpolationCmd ) } ,
482+ { 3 , typeof ( CircularInterpolationCmd ) } ,
483+ { 4 , typeof ( PauseCommand ) } ,
484+ } ;
485+
486+ private Dictionary < int , Type > _mCodeTranslations = new Dictionary < int , Type > ( ) ;
487+
488+ private Dictionary < int , Type > _tCodeTranslations = new Dictionary < int , Type > ( ) ;
489+
490+ public GCodeCommandList ( string [ ] commandLines )
491+ {
492+ CultureInfo . DefaultThreadCurrentCulture = CultureInfo . InvariantCulture ;
493+ CultureInfo . DefaultThreadCurrentUICulture = CultureInfo . InvariantCulture ;
494+ TryParse ( commandLines ) ;
495+ }
496+
497+ public void TryParse ( string [ ] commandLines )
498+ {
499+ foreach ( string line in commandLines )
500+ {
501+ try
502+ {
503+ this . Add ( ParseLine ( line ) ) ;
504+ }
505+ catch ( ArgumentException e )
506+ {
507+ Console . WriteLine ( e . Message ) ;
508+ }
509+ }
510+ }
511+
512+ public GCodeCommand ParseLine ( string serializedCmdLine )
513+ {
514+ string commandString = serializedCmdLine . Split ( ';' ) [ 0 ] . Trim ( ) ;
515+ string [ ] commandArr = commandString . Split ( new [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
516+
517+ char commandChar = char . ToUpper ( commandArr [ 0 ] [ 0 ] ) ;
518+ if ( ! Enum . TryParse ( commandChar . ToString ( ) , out PrepCode prepCode ) )
519+ throw new ArgumentException ( $ "Invalid preparatory function code: { commandChar } in line '{ serializedCmdLine } '") ;
520+
521+ string commandNumber = commandArr [ 0 ] . Substring ( 1 ) ;
522+ if ( ! int . TryParse ( commandNumber , out int codeNumber ) )
523+ throw new ArgumentException ( $ "Invalid number format: { commandNumber } in line '{ serializedCmdLine } '") ;
524+
525+ Dictionary < char , float > commandParams = new Dictionary < char , float > ( ) ;
526+
527+ foreach ( var commandParam in commandArr . Skip ( 1 ) )
528+ {
529+ if ( float . TryParse ( commandParam . Substring ( 1 ) , out float paramValue ) )
530+ {
531+ commandParams [ commandParam [ 0 ] ] = paramValue ;
532+ }
533+ else if ( commandParam . Length == 1 )
534+ {
535+ commandParams [ commandParam [ 0 ] ] = 0 ;
536+ }
537+ else
538+ {
539+ throw new ArgumentException ( $ "Invalid command parameter format: { commandParam } in line '{ serializedCmdLine } '. Command parameters must be of format <char><float>") ;
540+ }
640541 }
542+ Console . WriteLine ( string . Join ( Environment . NewLine , commandParams ) ) ;
543+ if ( _gCodeTranslations . TryGetValue ( codeNumber , out Type gCodeClassType ) )
544+ {
545+ return Activator . CreateInstance ( gCodeClassType , new Object [ ] { prepCode , codeNumber , commandParams } ) as GCodeCommand ;
546+ }
547+
548+ return Activator . CreateInstance ( typeof ( MiscCommand ) , new Object [ ] { prepCode , codeNumber , commandParams } ) as GCodeCommand ;
641549 }
642550}
0 commit comments