@@ -5,7 +5,6 @@ namespace PrusaMK4S.Handlers;
55
66public class GCodeHandler : IGcodeHandler
77{
8- private const string _fileNameBase = "IterationPrint" ;
98 private const int _printBedHeight = 210 ;
109 private const int _printBedWidth = 190 ;
1110 private static string [ ] _movementCommands = [ "G0" , "G1" , "G2" , "G3" ] ;
@@ -18,7 +17,6 @@ public GCodeHandler(byte[] original_data)
1817 public async Task Init ( )
1918 {
2019 ModificationStartIndex = - 1 ;
21- ModificationStopIndex = - 1 ;
2220 await CalculateObjectSize ( ) ;
2321 AdjustedFileData = await GenerateFirstPrintData ( ) ;
2422 }
@@ -37,25 +35,11 @@ public async Task<byte[]> ApplyPlanningParameters(int bedTemperature,
3735
3836 // The G-Code associated with the user defined main object
3937 List < string > main_print_gcode ;
40- // G-Code that comes at the end of a print, after the main object finishes, remains unmodified
41- List < string > unmodified_end_gcode = new List < string > ( ) ;
4238 // Start G-Code contains the nozzle and bed temperature, which we might need to edit sometimes
4339 List < string > start_gcode = data . Take ( ModificationStartIndex + 1 ) . ToList ( ) ;
4440
45-
46- if ( ModificationStopIndex != - 1 )
47- {
48- var lengthOfMainPrint = ModificationStopIndex - ModificationStartIndex + 1 ;
49- main_print_gcode = data . Skip ( ModificationStartIndex + 1 ) . Take ( lengthOfMainPrint ) . ToList ( ) ;
50- unmodified_end_gcode = data . Skip ( ModificationStopIndex + 1 ) . ToList ( ) ;
51- }
52-
53- else
54- {
55- main_print_gcode = data . Skip ( ModificationStartIndex + 1 ) . ToList ( ) ;
56- }
57-
58-
41+ main_print_gcode = data . Skip ( ModificationStartIndex + 1 ) . ToList ( ) ;
42+
5943 if ( nozzleTemperature > 0 )
6044 start_gcode = UpdateNozzleTemperature ( nozzleTemperature , start_gcode ) ;
6145
@@ -77,7 +61,7 @@ public async Task<byte[]> ApplyPlanningParameters(int bedTemperature,
7761 if ( fanSpeedMod > 0 )
7862 main_print_gcode = UpdateFanSpeed ( fanSpeedMod , main_print_gcode ) ;
7963
80- var updated_gcode = ConvertGCodeToBytes ( start_gcode , main_print_gcode , unmodified_end_gcode ) ;
64+ var updated_gcode = ConvertGCodeToBytes ( start_gcode , main_print_gcode ) ;
8165 return updated_gcode ;
8266 }
8367
@@ -324,21 +308,16 @@ private bool MatchesNozzleTempCommand(string entry)
324308
325309 private List < string > UpdateBedTemperature ( int desiredTemp , List < string > gcode )
326310 {
327- var updatedData = new List < string > ( ) ;
328- var bedTempMatchOne = $ "M140 S{ OriginalBedTemp } ";
329- var bedTempMatchTwo = $ "M190 S{ OriginalBedTemp } ";
330-
331- foreach ( var entry in gcode )
311+ var updatedData = gcode . Select ( entry =>
332312 {
333- if ( entry . StartsWith ( bedTempMatchOne ) )
334- updatedData . Add ( $ "M140 S{ desiredTemp } ") ;
313+ if ( entry . StartsWith ( "M140 S" ) )
314+ return $ "M140 S{ desiredTemp } ";
335315
336- else if ( entry . StartsWith ( bedTempMatchTwo ) )
337- updatedData . Add ( $ "M190 S{ desiredTemp } ") ;
316+ if ( entry . StartsWith ( "M190 S" ) )
317+ return $ "M190 S{ desiredTemp } ";
338318
339- else
340- updatedData . Add ( entry ) ;
341- }
319+ return entry ;
320+ } ) . ToList ( ) ;
342321
343322 return updatedData ;
344323 }
@@ -418,7 +397,7 @@ public async Task<byte[]> CreatePrintIteration(int iteration)
418397 var modifiedData = startup_gcode
419398 . Concat ( stringData
420399 . Skip ( ModificationStartIndex )
421- . Select ( line => UpdateLine ( line , xShift , yShift , 0 ) ) ) ;
400+ . Select ( line => ApplyOffsetToLine ( line , xShift , yShift , 0 ) ) ) ;
422401
423402 var modifiedStringData = string . Join ( "\n " , modifiedData ) ;
424403 var data = Encoding . UTF8 . GetBytes ( modifiedStringData ) ;
@@ -447,32 +426,8 @@ private double CalculateYShift(int itemIndex)
447426 return ( - ItemHeight - 10 ) * itemIndex ;
448427 }
449428
450- private string ? UpdateLine ( string line , double xOffset , double yOffset , int iteration )
429+ private string ? ApplyOffsetToLine ( string line , double xOffset , double yOffset , int iteration )
451430 {
452- //Determine original desired nozzle temperature
453- if ( line . StartsWith ( "; temperature = " ) )
454- {
455- var temp = line . Substring ( 16 ) ;
456- var parsed = int . TryParse ( temp , out var intTemp ) ;
457-
458- if ( parsed )
459- OriginalNozzleTemp = intTemp ;
460-
461- return line ;
462- }
463-
464- //Determine original desired bed temperature
465- if ( iteration == 0 && line . StartsWith ( "; bed_temperature = " ) )
466- {
467- var temp = line . Substring ( 20 ) ;
468- var parsed = int . TryParse ( temp , out var intTemp ) ;
469-
470- if ( parsed )
471- OriginalBedTemp = intTemp ;
472-
473- return line ;
474- }
475-
476431 if ( string . IsNullOrWhiteSpace ( line ) || line . StartsWith ( ";" ) )
477432 return line ;
478433
@@ -574,12 +529,11 @@ private async Task<byte[]> GenerateFirstPrintData()
574529
575530 var startup_gcode = stringData . Take ( ModificationStartIndex ) . ToList ( ) ;
576531 var shifted_startup = ShiftInitialPurgeLine ( startup_gcode ) ;
577- //shifted_startup = await AddFullMeshBedLeveling(shifted_startup);
578532
579533 var modifiedData = shifted_startup
580534 . Concat ( stringData
581535 . Skip ( ModificationStartIndex )
582- . Select ( line => UpdateLine ( line , XMinimumOffset , YMaximumOffset , 0 ) ) ) ;
536+ . Select ( line => ApplyOffsetToLine ( line , XMinimumOffset , YMaximumOffset , 0 ) ) ) ;
583537
584538 var modifiedStringData = string . Join ( "\n " , modifiedData ) ;
585539 var bytes = Encoding . UTF8 . GetBytes ( modifiedStringData ) ;
@@ -726,10 +680,9 @@ private async Task<List<string>> ConvertGCodeToStrings(byte[] gcode)
726680 return data ;
727681 }
728682
729- private byte [ ] ConvertGCodeToBytes ( List < string > start_gcode , List < string > updated_gcode , List < string > end_gcode )
683+ private byte [ ] ConvertGCodeToBytes ( List < string > start_gcode , List < string > updated_gcode )
730684 {
731685 updated_gcode . ForEach ( start_gcode . Add ) ;
732- end_gcode . ForEach ( start_gcode . Add ) ;
733686 var modifiedStringData = string . Join ( "\n " , start_gcode ) ;
734687 return Encoding . UTF8 . GetBytes ( modifiedStringData ) ;
735688 }
@@ -749,6 +702,7 @@ private void DetermineModificationIndex(List<string> gcode)
749702
750703 public int GetPrintBedHeight ( ) => _printBedHeight ;
751704 public int GetPrintBedWidth ( ) => _printBedWidth ;
705+
752706 public ValueTask DisposeAsync ( )
753707 {
754708 return ValueTask . CompletedTask ;
@@ -764,11 +718,8 @@ public ValueTask DisposeAsync()
764718 public double ItemWidth { get ; set ; }
765719 public double XMinimumOffset { get ; set ; }
766720 public double YMaximumOffset { get ; set ; }
767- public int OriginalNozzleTemp { get ; set ; }
768- public int OriginalBedTemp { get ; set ; }
769721 public bool SearchForMinAndMax { get ; set ; }
770722 public double LatestXShift { get ; set ; }
771723 public double LatestYShift { get ; set ; }
772724 public int ModificationStartIndex { get ; set ; } = - 1 ;
773- public int ModificationStopIndex { get ; set ; } = - 1 ;
774725}
0 commit comments