Jump to content
UltiMaker Community of 3D Printing Experts

Easiest way to turn on/off my laser for G0 moves then turn it back on for G1 draws using Cura???


Recommended Posts

Posted · Easiest way to turn on/off my laser for G0 moves then turn it back on for G1 draws using Cura???

I am using code to turn my laser on and off but need a way to get Cura to insert the code at the appropriate times.

I have been hand editing the g-code and inserting the code by hand which is getting tedious!

Each time I want the laser to turn off I put this code in:

G4 P0050 ; Pauses for 50 ms. This makes sure that the last move command executes before the laser turns off.

M104 S0 T2 ; Sets the #3 extruder heater output to off.

G0 Xnnn Ynnn ; General command for a non-printing move.

Each time I want the laser back on:

G4 P0050 ; pause

M104 S50 T2 ; Sets temp above threashold so it activates Laser to turn on.

G4 P0500 ; pauses a short 50ms so Laser can get fully turned on




  • Link to post
    Share on other sites

    Posted · Easiest way to turn on/off my laser for G0 moves then turn it back on for G1 draws using Cura???


    You could use a plugin in Cura, don't know if this worked, but an intern tried this a year ago:

    other option is to use regular expressions in a better text editor:


    G0 (.*)



    G0 /1

    [btw, i am not a programmer, but i really like google and copy/paste...]

    cheers \ joris

    #Name: Powder Printing

    #Info: Switch off fan power during travel moves

    #Help: PowderPrinting

    #Depend: GCode

    #Type: postprocess

    #Param: fanpowermin(int:) Fan Power Min(0-255)

    #Param: fanpowermax(int:) Fan Power Max(0-255)

    ## Written by Tom Kerckhaert, based on TweakAtZ by Steven Morlock

    ## Special Thanks to Joris van Tubergen

    ## This script is licensed under the Creative Commons - Attribution - Share Alike (CC BY-SA) terms

    # Uses - Switch off fan power during travel moves

    # TODO:

    # Get rid of the unnecessary fan on / off between two G moves

    # Fan power variables do not work yet

    import re

    def getValue(line, key, default = None):

    if not key in line or (';' in line and line.find(key) > line.find(';')):

    return default

    subPart = line[line.find(key) + 1:]

    m = re.search('^[0-9]+\.?[0-9]*', subPart)

    if m == None:

    return default


    return float(m.group(0))


    return default

    with open(filename, "r") as f:

    lines = f.readlines()

    with open(filename, "w") as f:

    for line in lines:

    if getValue(line, 'G', None) == 0:

    f.write("; Powder Plugin\n")

    f.write("M106 S0\n")


    f.write("M106 S255\n")

    f.write("; Plugin: End\n")


    if getValue(line, 'G', None) == 1:

    x = getValue(line, 'X', dx)

    y = getValue(line, 'Y', dy)

    z = getValue(line, 'Z', dz)

    e = getValue(line, 'E', e)

    f = getValue(line, 'F', f)

    line = 'G1 '

    if dx != x:

    line += 'X%f ' % x

    if dy != y:

    line += 'Y%f ' % y

    if dz != z:

    line += 'Z%f ' % z

    if df != f:

    line += 'F%f ' % f

    line += '\n'


    # if comment is the extruder movements before and after the print, remove that line


  • Link to post
    Share on other sites

    Posted · Easiest way to turn on/off my laser for G0 moves then turn it back on for G1 draws using Cura???

    Yes, what Joris said. Write a plugin. It will be awesome. And please share it! Note also that the newest versions of Cura (15.06.* and newer) have a totally different way of doing plugins and I don't know if anyone knows how to do that yet so I would go for the older version e.g. 15.04 and I'm sure all the work you did for the plugin will still be useful - I suspect converting plugins from 15.04 to 15.06 will be pretty simple once someone documents the process.

  • Link to post
    Share on other sites

    • 2 weeks later...
    Posted · Easiest way to turn on/off my laser for G0 moves then turn it back on for G1 draws using Cura???

    Regular edits with search and replace has sped up the process considerably. Thank you for taking the time to send me your suggestion. I am considering writing a plug in to eliminate the editing for all future projects, but I don't have the time to go down that road right now.

  • Link to post
    Share on other sites

    Posted · Easiest way to turn on/off my laser for G0 moves then turn it back on for G1 draws using Cura???

    I wrote a post process plugin to solve this problem

    # Copyright (c) 2020 Belin Fieldson
    # LaserCode is released under the terms of the AGPLv3 or higher.
    from ..Script import Script
    class LaserCode(Script):
        version = "1.01"
        def __init__(self):
        def getSettingDataString(self):
            return """{
                "name":"Laser Code v.(""" + self.version + """)",
                "metadata": {},
                "version": 2,
                        "label": "Laser Activate",
                        "description": "The code to use to activate the laser",
                        "type": "str",
                        "default_value": "M3"
                        "label": "Laser Deactivate",
                        "description": "The code to use to deactivate the laser",
                        "type": "str",
                        "default_value": "M5"
                        "label": "Active GCODE",
                        "description": "The gcode that the activate code should be prefixed to",
                        "type": "str",
                        "default_value": "G1"
                        "label": "Inactive GCODE",
                        "description": "The gcode that the deactivate code should be prefixed to",
                        "type": "str",
                        "default_value": "G0"
        def execute(self, data):
            activation = self.getSettingValueByKey("_activation");
            deactivation = self.getSettingValueByKey("_deactivation");
            searchActivation = self.getSettingValueByKey("_prefixActivation");
            searchDeactivation = self.getSettingValueByKey("_prefixDeactivation");
            activated = False;
            # Declare output gcode
            for layer in data:
                index = data.index(layer)
                lines = layer.split("\n")
                newLayer = ""
                for line in lines:
                    if (searchActivation in line and not activated):
                        newLayer += activation + "\n"
                        activated = True
                    if (searchDeactivation in line and activated):
                        newLayer += deactivation + "\n"
                        activated = False
                    newLayer += line + "\n"
                data[index] = newLayer
            return data


  • Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    • Our picks

      • Here it is. The new UltiMaker S7
        The UltiMaker S7 is built on the success of the UltiMaker S5 and its design decisions were heavily based on feedback from customers.
        So what’s new?
        The obvious change is the S7’s height. It now includes an integrated Air Manager. This filters the exhaust air of every print and also improves build temperature stability. To further enclose the build chamber the S7 only has one magnetically latched door.
        The build stack has also been completely redesigned. A PEI-coated flexible steel build plate makes a big difference to productivity. Not only do you not need tools to pop a printed part off. But we also don’t recommend using or adhesion structures for UltiMaker materials (except PC, because...it’s PC). Along with that, 4 pins and 25 magnets make it easy to replace the flex plate perfectly – even with one hand.
        The re-engineered print head has an inductive sensor which reduces noise when probing the build plate. This effectively makes it much harder to not achieve a perfect first layer, improving overall print success. We also reversed the front fan direction (fewer plastic hairs, less maintenance), made the print core door magnets stronger, and add a sensor that helps avoid flooding.

        The UltiMaker S7 also includes quality of life improvements:
        Reliable bed tilt compensation (no more thumbscrews) 2.4 and 5 GHz Wi-Fi A 1080p camera (mounted higher for a better view) Compatibility with 280+ Marketplace materials Compatibility with S5 project files (no reslicing needed) And a whole lot more  
        Curious to see the S7 in action?
        We’re hosting a free tech demo on February 7.
        It will be live and you can ask any questions to our CTO, Miguel Calvo.
        Register here for the Webinar
        • 10 replies
      • UltiMaker Cura 5.3.0-Alpha 🎄 Tree Support Spotlight 🎄
        Are you a fan of tree support, but dislike the removal process and the amount of filament it uses? Then we would like to invite you to try this special release of UltiMaker Cura. Brought to you by our special community contributor @thomasrahm
        We generated a special version of Cura 5.2 called 5.3.0 Alpha + Xmas. The only changes we introduced compared to UltiMaker Cura 5.2.1 are those which are needed for the new supports. So keep in mind, this is not a sneak peek for Cura 5.3 (there are some really cool new features coming up) but a spotlight release highlighting this new version of tree supports.  
        • 16 replies
      • New here? Get ahead with a free onboarding course
        Often getting started is the most difficult part of any process. A good start sets you up for success and saves you time and energy that could be spent elsewhere. That is why we have a onboarding course ready for
        Ultimaker S5 Pro Bundle, Ultimaker S5, Ultimaker S3 Ultimaker 2+ Connect.   
        They're ready for you on the Ultimaker Academy platform. All you need to do to gain access is to register your product to gain free access. 
        Ready? Register your product here in just 60 seconds.
        • 14 replies
    • Create New...