Ultimaker / Cura

3D printer / slicing GUI built on top of the Uranium framework
GNU Lesser General Public License v3.0
6.17k stars 2.08k forks source link

[3.4.1] Problems with writing own post processing script #5231

Closed RedPetroleum closed 5 years ago

RedPetroleum commented 5 years ago

Application Version 3.4.1

Platform MacOS 10.13.6

I would like to make my own post processing script to implement the suggested Sloped Z Hop: #2761
My approach was to just delete the gcode line (line 2) after a retraction. Since the next line already has the z hight (line 3): G1 F2940 E43.94278 G1 F300 Z0.4 G0 F3600 X100.763 Y104.582 Z0.4 To filter a retraction I set the retraction speed to the specific value of 49 mm/s which translates to F2940 (line 1) while the prime speed is 50mm/s.

Here is the function that I replaced in the preinstalled FilamentChange script: def execute(self, data):

    for layer in data:
        lines = layer.split("\n")
        for i, line in enumerate(lines): #enumerate() returns a sequence of pairs: (42, "G1 X21.662 Y22.442"), (43, "G10 S1"), etc...
            if self.getValue(line, "F") == 2940: #Search for F2940
                j=i+1   #select next line
                lines[j] = ";DELETED" #delete line of gcode by overwriting it
    return data

Unfortunately this doesn't do anything. I don't know much about coding and I would be really happy if someone could help my out with that.

RedPetroleum commented 5 years ago

This solves it.

`def getSettingDataString(self):

    return """{
        "name":"Sloped Z hop",
        "key": "SlopedZhop",
        "metadata": {},
        "version": 2,
        "settings":
        {
            "my_retraction_speed":
            {
                "label": "Retraction speed",
                "description": "Must be different to prime speed!",
                "unit": "mm",
                "type": "float",
                "default_value": 42.0
            }
        }
    }"""

def execute(self, data: list):
    speed = self.getSettingValueByKey("my_retraction_speed")
    myspeed = speed * 60
    for index, layer in enumerate(data):
        mylayer = ""
        retract = 0
        lines = layer.split("\n")
        # Scroll each line of each layer in the G-code
        for line in lines:
            if mylayer is not "": #first line of layer
                mylayer += "\n"
            if retract == 1 and self.getValue(line, "G") == 1: 
                mylayer += ";Z LIFT DELETED"
                retract = 0
            elif self.getValue(line, "F") == myspeed:
                retract = 1 #Z lift will be deleted next line
                mylayer += line
            else:
                mylayer += line
        data[index] = mylayer
    return data`