You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自制笔式绘图仪GCODE后处理多余空格问题排查求助

Fixing Unwanted Spaces in Your GCODE Post-Processor

Let's break down exactly what's causing those problematic spaces and how to fix them:

1. The Direct Cause: Explicit Space in Your Replacement Template

The most obvious issue is in your round_coordinates function's regex replacement line:

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)

Look at the replacement string: r"\1 %0..." — there's a hardcoded space right after \1 (which captures the axis letter like X/Y/Z). This forces your GCODE to output X 10.0 instead of X10.0, which is likely why your plotter rejects the code.

2. A Critical Indentation Bug

You also have an indentation error that's silently breaking your output:

for s in self.gcode.split("\n"):
    # ... axis processing logic ...
# This line is OUTSIDE the for s loop!
gcode += s + "\n"

Right now, you're only adding the last line of your original GCODE to the final output, not all processed lines. This needs to be indented one level to sit inside the for s loop.

3. Fixed Version of the Function

Here's the cleaned-up, corrected code with explanations:

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")
        return  # Exit early to avoid further issues
    gcode = ""
    for s in self.gcode.split("\n"):
        for a in "xyzijkaf":
            # Use re.compile for efficiency and re.escape to avoid regex issues
            pattern = re.compile(r"(?i)({})(\s*-?\s*\d*\.?\d*)".format(re.escape(a)))
            match = pattern.search(s)
            if match:
                coord_str = match.group(2).strip()
                if coord_str:
                    coord_val = float(coord_str)
                    rounded_val = round(coord_val, round_) if round_ > 0 else int(round(coord_val))
                    # Remove the space after \1 to keep axis letter and value joined
                    if round_ > 0:
                        replacement = r"\1{:.{}f}".format(rounded_val, round_)
                    else:
                        replacement = r"\1{}".format(rounded_val)
                    s = pattern.sub(replacement, s)
        # Now inside the loop: add every processed line to the output
        gcode += s + "\n"
    self.gcode = gcode

4. Check the Second Round Function

You mentioned a related round function at line 5440. Make sure that function doesn't have the same issue — double-check for any explicit spaces in its regex replacement templates that might be adding spaces between axis letters and coordinates.

After applying these fixes, your GCODE should have properly formatted coordinates (like X10.0 instead of X 10.0) and retain all lines of your original code.

内容的提问来源于stack exchange,提问作者Marinus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:03:28