5axes / Calibration-Shapes

A Cura plugin that adds simple shapes (cube, cylinder, tube) and also 24 Calibration and test parts + 7 Postprocessing scripts
GNU Affero General Public License v3.0
330 stars 69 forks source link

Misc improvements #124

Closed kartchnb closed 1 year ago

kartchnb commented 1 year ago

I so much appreciate your help and advice and I wanted to share some changes I've found useful when developing my plugin. Feel free to ignore this if it's not useful for you.

Consider using Python's enumerate method to iterate through the gcode.

For example, you can simplify this code (taken from your RetractTower.py file):

for layer in data:
    layer_index = data.index(layer)

    lines = layer.split("\n")
    for line in lines:                  
        line_index = lines.index(line)

To this:

for layer_index, layer in enumerate(data):

    lines = layer.split("\n")
    for line_index, line in enumerate(lines):

I worry that the original code could fail if there are duplicate lines in a layer. If this were to happen, the line_index=lines.index(line) command would return the index of the first line, which may not be correct. The enumerate function doesn't have this problem. It returns the correct line_index AND the line itself as it iterates.

Ignore the user-specified "end" code

As you know, Cura lets the user provide their own "start" and "end" code that are automatically inserted into the gcode. I ran into a problem when my post-processing included the "end" code during post-processing, so now I'm ignoring it.

You're already ignoring the user's "start" code by skipping the first two layers in the gcode, but you may want to also ignore the last two layers. It looks like the second-to-last layer is the user's "end" code and the last layer just contains comments inserted by Cura.

5axes commented 1 year ago

Rules Number One : if it's not broken don't fix it. For most of the scripts as the position of the line refers to a layer number and as these lines are unique for a layer or as we have most of the times only one Fan or Temp instruction by layer, I think it is not necessary to change the original code.

But for some of my scripts your coding is much more interesting and I will use your method in the future.

Thanks for these suggestions.

kartchnb commented 1 year ago

That's a really good rule... I would avoid a LOT of problems if I remembered that in the future.

Cheers!