0

I am post-processing output from a python script that generates GCODE for my homebrew pen plotter.

The post-processor adds a white space in my GCODE right after a crucial bit of information (after every X and Y coordinate value), causing the GCODE to become invalid.

Example of how it should look: should

Example of how my output looks: looks

I have tried to remove the \s operators from the suspected pieces of code, I have tried using .lstrip() in various areas to remove the white spaces but to no avail. I have also removed all double spaces that were present in the code, and nothing has helped so far.

I suspect this code is doing it:

def round_coordinates(self,parameters) :
    try: 
        round_ = int(parameters)
    except :    
        self.error("Bad parameters for round. Round should be an integer! \n(Parameters: '%s')"%(parameters), "error")      
    gcode = ""
    for s in self.gcode.split("\n"):
        for a in "xyzijkaf" :
            r = re.search(r"(?i)("+a+r")\s*(-?\s*(\d*\.?\d*))", s)
            if r : 

                if r.group(2)!="":
                    s = re.sub(
                                r"(?i)("+a+r")\s*(-?)\s*(\d*\.?\d*)",
                                (r"\1 %0."+str(round_)+"f" if round_>0 else r"\1 %d")%round(float(r.group(2)),round_),
                                s)
        gcode += s + "\n"
    self.gcode = gcode

I am hoping to be able to find out where the whitespace comes from, I may not showing the right bit of code so I have linked the source file. It appears at line 2648 and at line 5440 there is also a round function present that seems to be related.

Here is a pastebin to the complete code: https://pastebin.com/s8J1H8r6

Marinus
  • 39
  • 5
  • Can you check if it's due to the space between \1 and %0 in this line: `r"\1 %0."+str(round_)+"f" if round_>0 else r"\1 %d")%round(float(r.group(2)),round_),` – prime_hit Jul 09 '19 at 11:11
  • Thanks for the hint, I indeed also tried that one but then it breaks my script.. It breaks a group reference. – Marinus Jul 09 '19 at 11:13

1 Answers1

0

I have attached an example which can be run to check this. I find this interesting but I was not able to find a way to solve it in a nice way... I will update this with better answer if I come across it.

For now, I see that we need to put some character between \1 and %d for the code to work. I would NOT use following to solve the issue, but it works at least. What I did was to put --- between the two, and then later remove it using replace function.

import re
def round_coordinates() :
    round_ = 5 

    gcode = """O1000
T1 M6
G0 G90 G40 G21 G17 G94 G80
G54 X-75 Y-25 S500 M3  
G43 Z100 H1
Z5
G1 Z-20 F100
X-50 M8            
Y0                 
X0 Y50               
X50 Y0             
X0 Y-50           
X-50 Y0      
Y25          
X-75  
G0 Z100
M30"""

    for s in gcode.split("\n"):
        for a in "xyzijkaf" :
            r = re.search(r"(?i)("+a+r")\s*(-?\s*(\d*\.?\d*))", s)
            if r : 

                if r.group(2)!="":
                    s = re.sub(
                                r"(?i)("+a+r")\s*(-?)\s*(\d*\.?\d*)",
                                (r"\1---%0."+str(round_)+"f" if round_>0 else r"\1---%d")%round(float(r.group(2)),round_),
                                s)
                    s = s.replace("---", "")
        gcode += s + "\n"
    return gcode

print(round_coordinates())

PS. I would use format function instead of % I guess...

prime_hit
  • 326
  • 1
  • 7
  • Thanks you are right, this is what I also expected. I should've just added a character in that first white space in the code to see if that was the place. Indeed adding this works. However, it now completely messes up the generated gcode. Maybe the original developer had never finished the round function to begin with. – Marinus Jul 09 '19 at 12:02
  • Can you explain why is this messing up your code? I mean, I just expect this to remove the space and nothing else... – prime_hit Jul 09 '19 at 12:04
  • Also, there is no round function in the pastebin code you posted! Oe am I missing something? – prime_hit Jul 09 '19 at 12:15
  • I'm sorry, I made a mistake, my solution somehow also removed the single minus characters '-' and therefor turned the negative numbers into positive ones. I fixed it. Now there is only one more problem with the script, but I think I will have to make a new topic for that one as it's a very different problem and needs a very clearly written description. It's about the tool changes in the gcode that are now applied for every path that is drawn while the tool change should only be applied with every layer change. – Marinus Jul 09 '19 at 12:28
  • Oh okay! Whatever you use instead of `---` just make sure it will never appear in your code that you are processing... otherwise it will just make the output wrong! – prime_hit Jul 09 '19 at 12:34
  • There is a much better way for this using function for replacement... I will update the answer with that.. hope that will help! – prime_hit Jul 09 '19 at 12:34
  • Thanks for your help, I am not new to hacking scripts but have never worked with Python before. Meanwhile I've changed about 500 lines of code in this script and I am pretty amazed I managed to get so far with improvements and additions across multiple dependent files. The replace script works well, but I'm curious what you think should be the way to do this. I will try and start working on the layer change script or at least figure out why it's not working. – Marinus Jul 09 '19 at 13:05
  • I would suggest you to post a new question with the details of what you are trying to achieve exactly. I am not actually sure what your project is about... – prime_hit Jul 09 '19 at 13:13
  • Yes, I will do that later today :) thanks for your help! :) – Marinus Jul 09 '19 at 13:21
  • The new question is in here, I do not expect you to be able to help me out, but I thought it would be at least nice to let you know about it: https://stackoverflow.com/questions/56955977/how-to-store-a-variable-until-it-changes-and-compare-the-new-value-to-the-old-on – Marinus Jul 09 '19 at 15:37