@@ -7,6 +7,7 @@ public class GCodeHandler : IGcodeHandler
77{
88 private const int _printBedHeight = 210 ;
99 private const int _printBedWidth = 190 ;
10+ private const int _distanceBetweenPurgeLines = 5 ;
1011 private static string [ ] _movementCommands = [ "G0" , "G1" , "G2" , "G3" ] ;
1112
1213 public GCodeHandler ( byte [ ] original_data )
@@ -31,6 +32,7 @@ public async Task<byte[]> ApplyPlanningParameters(int bedTemperature,
3132 byte [ ] gcode )
3233 {
3334 var data = await ConvertGCodeToStrings ( gcode ) ;
35+ //data = UpdateBedLeveling(data, 0);
3436 DetermineModificationIndex ( data ) ;
3537
3638 // The G-Code associated with the user defined main object
@@ -52,7 +54,7 @@ public async Task<byte[]> ApplyPlanningParameters(int bedTemperature,
5254 if ( speedMod > 0 )
5355 main_print_gcode = UpdateMovementSpeed ( speedMod , main_print_gcode ) ;
5456
55- if ( retractionLength > - 1 )
57+ if ( retractionLength > 0 )
5658 main_print_gcode = UpdateRetractionLength ( retractionLength , main_print_gcode ) ;
5759
5860 if ( accelerationMod > 0 )
@@ -543,18 +545,21 @@ private async Task<byte[]> GenerateFirstPrintData()
543545
544546 private List < string > ShiftInitialPurgeLine ( List < string > gcode )
545547 {
546- var updatedGcode = new List < string > ( ) ;
548+ var updatedGcode = new List < string > ( ) ;
547549 var shouldEdit = false ;
548550
549551 foreach ( var line in gcode )
550552 {
551- if ( line . Contains ( "; probe near purge place" ) )
552- updatedGcode . Add ( ApplyPurgeXOffset ( line . Split ( ) , 200 ) ) ;
553+ // if(line.Contains("; probe near purge place"))
554+ // updatedGcode.Add(ApplyPurgeXOffset(line.Split(), 200));
553555
554556 if ( line . StartsWith ( "; prepare for purge" ) )
557+ {
555558 shouldEdit = true ;
559+ updatedGcode . Add ( line ) ;
560+ }
556561
557- if ( line . StartsWith ( "G" ) && shouldEdit )
562+ else if ( line . StartsWith ( "G" ) && shouldEdit )
558563 updatedGcode . Add ( ApplyPurgeXOffset ( line . Split ( ) , 200 ) ) ;
559564
560565 else
@@ -591,19 +596,22 @@ private string ApplyPurgeXOffset(string[] splitLine, int shift)
591596
592597 private List < string > ShiftIterationPurgeLine ( List < string > gcode , int iteration )
593598 {
594- var yShift = iteration * 5 ;
599+ var yShift = iteration * _distanceBetweenPurgeLines ;
595600 var updatedGcode = new List < string > ( ) ;
596601 var shouldEdit = false ;
597602
598603 foreach ( var line in gcode )
599604 {
600- if ( line . Contains ( "; probe near purge place" ) )
601- updatedGcode . Add ( ApplyPurgeYOffset ( line . Split ( ) , yShift ) ) ;
605+ // if(line.Contains("; probe near purge place"))
606+ // updatedGcode.Add(ApplyPurgeYOffset(line.Split(), yShift));
602607
603608 if ( line . StartsWith ( "; prepare for purge" ) )
609+ {
604610 shouldEdit = true ;
611+ updatedGcode . Add ( line ) ;
612+ }
605613
606- if ( line . StartsWith ( "G" ) && shouldEdit )
614+ else if ( line . StartsWith ( "G" ) && shouldEdit )
607615 updatedGcode . Add ( ApplyPurgeYOffset ( line . Split ( ) , yShift ) ) ;
608616
609617 else
@@ -616,6 +624,17 @@ private List<string> ShiftIterationPurgeLine(List<string> gcode, int iteration)
616624 private string ApplyPurgeYOffset ( string [ ] splitLine , int shift )
617625 {
618626 var y = splitLine . FirstOrDefault ( e => e . StartsWith ( "Y" ) ) ;
627+ //var height = splitLine.FirstOrDefault(e => e.StartsWith("H"));
628+ //var width = splitLine.FirstOrDefault(e => e.StartsWith("W"));
629+
630+ //if(splitLine[0] == "G29")
631+ //{
632+ //var height_index = splitLine.IndexOf(height);
633+ //var width_index = splitLine.IndexOf(width);
634+
635+ //splitLine[height_index] = "H25";
636+ //splitLine[width_index] = "W25";
637+ //}
619638
620639 if ( y is null )
621640 {
@@ -626,6 +645,7 @@ private string ApplyPurgeYOffset(string[] splitLine, int shift)
626645 else
627646 {
628647 var index = splitLine . IndexOf ( y ) ;
648+
629649 var parsed = float . TryParse ( y . Substring ( 1 ) , out var yFloat ) ;
630650
631651 if ( ! parsed )
@@ -644,26 +664,20 @@ private List<string> UpdateBedLeveling(List<string> gcode, int printIteration)
644664
645665 foreach ( var line in gcode )
646666 {
647- if ( line . StartsWith ( "M555 " ) )
667+ if ( line . StartsWith ( "G29 P1 " ) )
648668 {
649- //M555 X0 Y0 W0 H0
650- var splitLine = line . Split ( ) ;
651-
652- if ( splitLine . Length < 5 )
669+ if ( line . Contains ( "purge" ) )
653670 {
654671 updated_gcode . Add ( line ) ;
655- continue ;
656672 }
657673
658- splitLine [ 1 ] = "X0" ;
659- splitLine [ 2 ] = "Y0" ;
660- splitLine [ 3 ] = $ "W25";
661- splitLine [ 4 ] = $ "H25";
662-
663- var newLevelAreaCmd = string . Join ( " " , splitLine ) ;
664-
665- updated_gcode . Add ( newLevelAreaCmd ) ;
666- continue ;
674+ else
675+ {
676+ var x_center = LatestXShift + ( 0.5 * ItemWidth ) ;
677+ var y_center = _printBedHeight + LatestYShift - ( 0.5 * ItemHeight ) ;
678+ var newLevelCommand = $ "G29 P1 X{ x_center } Y{ y_center } W{ ItemWidth } H{ ItemHeight } ; Level the ares determined by ARES";
679+ updated_gcode . Add ( newLevelCommand ) ;
680+ }
667681 }
668682
669683 else
0 commit comments