InsertAtLayerChange.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. }
  32. }"""
  33. def execute(self, data):
  34. gcode_to_add = self.getSettingValueByKey("gcode_to_add") + "\n"
  35. for layer in data:
  36. # Check that a layer is being printed
  37. lines = layer.split("\n")
  38. for line in lines:
  39. if ";LAYER:" in line:
  40. index = data.index(layer)
  41. if self.getSettingValueByKey("insert_location") == "before":
  42. layer = gcode_to_add + layer
  43. else:
  44. layer = layer + gcode_to_add
  45. data[index] = layer
  46. break
  47. return data