Post processing script to find an replace Gcode commands
 
Avisos
Vaciar todo

Post processing script to find an replace Gcode commands  

  RSS
MaxGyver
(@maxgyver)
Active Member
Post processing script to find an replace Gcode commands

Hey all,

I am using compressed air instead of a dedicated hotend and part cooling fan on my Duet controlled printer running Reprap Firmware. The Airflow for part cooling is regulated by a ball valve that is actuated by a stepper motor. 

Since it is not possible to map a stepper motor to a fan output in Reprap Firmware, I would like to replace all M106 S commands with G1 A (A is the dedicated axis for the valve stepper) to move the stepper in the correct position. So for example M106 S255 (Fan at 100%) is replaced with G1 A255 (Valve fully open)

I have tried adjusting some scripts I found without success. So a little help would be much appreciated. 🙂

-Max

Respondido : 13/05/2021 8:45 pm
bobstro
(@bobstro)
Illustrious Member
RE: Post processing script to find an replace Gcode commands

I wrote a python Slic3r post-processing script to add a "bump" to fan speeds. It should be easy enough to modify to replace the fan commands. I haven't used it in a couple of years now, so I'm not sure how well it'll work with current versions of PrusaSlicer.

My notes and disclaimers on 3D printing

and miscellaneous other tech projects
He is intelligent, but not experienced. His pattern indicates two dimensional thinking. -- Spock in Star Trek: The Wrath of Khan

Respondido : 13/05/2021 9:31 pm
--
 --
(@)
Illustrious Member
RE: Post processing script to find an replace Gcode commands

I use the regexp search/replace feature of Notepad++ - seems to work okay; but requires playing with regexp which can be a learning experience in itself (too many flavors, imo).

For example  ^M73.*$\n   finds and selects all lines with M73 in them.

Though, this technique won't allow programmatic replacement values: it is strictly 1 to 1 find / replace.

Esta publicación ha sido modificada el hace 4 years por --
Respondido : 14/05/2021 12:06 am
MaxGyver
(@maxgyver)
Active Member
Topic starter answered:
RE: Post processing script to find an replace Gcode commands

@bobstro

I tried running your script before making any changes. But it throws an error right away.

Post-processing script C:\Python\Python39\python.exe "C:\Python\Slic3r bump fan post processing script.py" on file C:\Users\maxbo\Desktop\3D-Print\Adapter.gcode failed.
Error code: 1

-Max

Respondido : 14/05/2021 2:03 pm
bobstro
(@bobstro)
Illustrious Member
RE: Post processing script to find an replace Gcode commands

@maxgyver - Can you run that command from the command line? I believe Error code 1 indicates a permissions issue. You might try renaming the script to remove the spaces, as that can cause odd problems depending on OS. The script itself doesn't rely on any exotic libraries, but there's always the chance that something hasn't been installed. Are you able to run "hello world" level scripts?

 

My notes and disclaimers on 3D printing

and miscellaneous other tech projects
He is intelligent, but not experienced. His pattern indicates two dimensional thinking. -- Spock in Star Trek: The Wrath of Khan

Respondido : 14/05/2021 2:11 pm
MaxGyver
(@maxgyver)
Active Member
Topic starter answered:
RE: Post processing script to find an replace Gcode commands

@bobstro

The error persists after removing the spaces. The "hello world" scripts run without problems.

When running the script from console I get the following:

========================= RESTART: C:\Python\script.py =========================
Traceback (most recent call last):
File "C:\Python\script.py", line 6, in <module>
sourceFile=sys.argv[1]
IndexError: list index out of range
>>>

-Max

Respondido : 14/05/2021 2:59 pm
bobstro
(@bobstro)
Illustrious Member
RE: Post processing script to find an replace Gcode commands

Did you run it like this?

C:\Python\Python39\python.exe "C:\Python\Slic3r bump fan post processing script.py" "C:\Users\maxbo\Desktop\3D-Print\Adapter.gcode"

(fixing filenames where necessary)

It's stopping on a line before it gets into anything remotely tricky. Parsing command-line arguments is straightforward. You might try cutting it down to figure out what's going on with your python configuration:

#!/usr/bin/python
import sys
import re
import os

sourceFile=sys.argv[1]

print(sourceFile)


My notes and disclaimers on 3D printing

and miscellaneous other tech projects
He is intelligent, but not experienced. His pattern indicates two dimensional thinking. -- Spock in Star Trek: The Wrath of Khan

Respondido : 14/05/2021 4:02 pm
Matti R
(@matti-r)
Miembro
RE:

I stumbled to this same problem and found that the script had to be changed a bit to work in latest PS:

#!/usr/bin/python
import sys
import re
import os

sourceFile=sys.argv[1]

# Read the ENTIRE g-code file into memory
with open(sourceFile, "r") as f:
    lines = f.readlines()

destFile = re.sub('\.gcode$','',sourceFile)
#os.rename(sourceFile,destFile+".bumpfan.bak")
destFile = re.sub('\.gcode$','',sourceFile)
#destFile = destFile + '.gcode'

with open(destFile, "w") as of:
    for lIndex in range(len(lines)):
        oline = lines[lIndex]
        # Parse gcode line
        parts = oline.split(';', 1)
        if len(parts) > 0:
            # Parse command
            command = parts[0].strip()

            if command:
                stringMatch = re.search ('^M106 S(.*)', command)
                if stringMatch:
                    # Insert code to bump fan to max before fan speed commands
                    of.write('M106 S255\n')
            
            # Write original line       
            of.write(oline)
of.close()
f.close()

Hope this helps anybody else struggling.

Seems like PS naming is different and there was a obsolete Python command xrange. Python console warns about  "\" but it does not affect PS.

@bobstro, Check if you see anything else to update, works for me at least.

Esta publicación ha sido modificada el hace 3 months por Matti R
Respondido : 18/09/2024 3:23 pm
Matti R
(@matti-r)
Miembro
RE: Post processing script to find an replace Gcode commands

Tried to edit previous post but couldn't for some reason.

cleaned up the code:

#!/usr/bin/python
import sys
import re
import os

sourceFile=sys.argv[1]

# Read the ENTIRE g-code file into memory
with open(sourceFile, "r") as f:
    lines = f.readlines()

destFile = re.sub('\\.gcode$','',sourceFile)


with open(destFile, "w") as of:
    for lIndex in range(len(lines)):
        oline = lines[lIndex]
        # Parse gcode line
        parts = oline.split(';', 1)
        if len(parts) > 0:
            # Parse command
            command = parts[0].strip()

            if command:
                stringMatch = re.search ('^M106 S(.*)', command)
                if stringMatch:
                    # Insert code to bump fan to max before fan speed commands
                    of.write('M106 S255\n')
            
            # Write original line       
            of.write(oline)
of.close()
f.close()

 

Respondido : 25/09/2024 6:54 am
Neophyl
(@neophyl)
Illustrious Member
RE:

Editing is only possible for a short period after posting,  around 5 minutes.

Also Bob is sadly no longer with us it seems.  Which is a bloody tragedy.

Respondido : 25/09/2024 12:21 pm
Compartir: