InsertAtLayerChange.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. # Created by Wayne Porter
  4. from ..Script import Script
  5. class InsertAtLayerChange(Script):
  6. def __init__(self):
  7. super().__init__()
  8. def getSettingDataString(self):
  9. return """{
  10. "name": "Insert at layer change",
  11. "key": "InsertAtLayerChange",
  12. "metadata": {},
  13. "version": 2,
  14. "settings":
  15. {
  16. "insert_location":
  17. {
  18. "label": "When to insert",
  19. "description": "Whether to insert code before or after layer change.",
  20. "type": "enum",
  21. "options": {"before": "Before", "after": "After"},
  22. "default_value": "before"
  23. },
  24. "gcode_to_add":
  25. {
  26. "label": "G-code to insert",
  27. "description": "G-code to add before or after layer change.",
  28. "type": "str",
  29. "default_value": ""
  30. },
  31. "skip_layers":
  32. {
  33. "label": "Skip layers",
  34. "description": "Number of layers to skip between insertions (0 for every layer).",
  35. "type": "int",
  36. "default_value": 0,
  37. "minimum_value": 0
  38. }
  39. }
  40. }"""
  41. def execute(self, data):
  42. gcode_to_add = self.getSettingValueByKey("gcode_to_add") + "\n"
  43. skip_layers = self.getSettingValueByKey("skip_layers")
  44. count = 0
  45. for layer in data:
  46. # Check that a layer is being printed
  47. lines = layer.split("\n")
  48. for line in lines:
  49. if ";LAYER:" in line:
  50. index = data.index(layer)
  51. if count == 0:
  52. if self.getSettingValueByKey("insert_location") == "before":
  53. layer = gcode_to_add + layer
  54. else:
  55. layer = layer + gcode_to_add
  56. data[index] = layer
  57. count = (count + 1) % (skip_layers + 1)
  58. break
  59. return data