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:
Example of how my output 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
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...