ProcessSlicedLayersJob.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Job import Job
  4. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  5. from UM.Scene.SceneNode import SceneNode
  6. from UM.Application import Application
  7. from UM.Mesh.MeshData import MeshData
  8. from UM.Message import Message
  9. from UM.i18n import i18nCatalog
  10. from UM.Math.Vector import Vector
  11. from cura import LayerData
  12. from cura import LayerDataDecorator
  13. import numpy
  14. catalog = i18nCatalog("cura")
  15. class ProcessSlicedLayersJob(Job):
  16. def __init__(self, layers):
  17. super().__init__()
  18. self._layers = layers
  19. self._scene = Application.getInstance().getController().getScene()
  20. self._progress = None
  21. self._abort_requested = False
  22. ## Aborts the processing of layers.
  23. #
  24. # This abort is made on a best-effort basis, meaning that the actual
  25. # job thread will check once in a while to see whether an abort is
  26. # requested and then stop processing by itself. There is no guarantee
  27. # that the abort will stop the job any time soon or even at all.
  28. def abort(self):
  29. self._abort_requested = True
  30. def run(self):
  31. if Application.getInstance().getController().getActiveView().getPluginId() == "LayerView":
  32. self._progress = Message(catalog.i18nc("@info:status", "Processing Layers"), 0, False, -1)
  33. self._progress.show()
  34. Job.yieldThread()
  35. if self._abort_requested:
  36. if self._progress:
  37. self._progress.hide()
  38. return
  39. Application.getInstance().getController().activeViewChanged.connect(self._onActiveViewChanged)
  40. object_id_map = {}
  41. new_node = SceneNode()
  42. ## Remove old layer data (if any)
  43. for node in DepthFirstIterator(self._scene.getRoot()):
  44. if type(node) is SceneNode and node.getMeshData():
  45. if node.callDecoration("getLayerData"):
  46. self._scene.getRoot().removeChild(node)
  47. Job.yieldThread()
  48. if self._abort_requested:
  49. if self._progress:
  50. self._progress.hide()
  51. return
  52. settings = Application.getInstance().getMachineManager().getWorkingProfile()
  53. mesh = MeshData()
  54. layer_data = LayerData.LayerData()
  55. layer_count = len(self._layers)
  56. current_layer = 0
  57. for layer in self._layers:
  58. layer_data.addLayer(layer.id)
  59. layer_data.setLayerHeight(layer.id, layer.height)
  60. layer_data.setLayerThickness(layer.id, layer.thickness)
  61. for p in range(layer.repeatedMessageCount("polygons")):
  62. polygon = layer.getRepeatedMessage("polygons", p)
  63. points = numpy.fromstring(polygon.points, dtype="i8") # Convert bytearray to numpy array
  64. points = points.reshape((-1,2)) # We get a linear list of pairs that make up the points, so make numpy interpret them correctly.
  65. # Create a new 3D-array, copy the 2D points over and insert the right height.
  66. # This uses manual array creation + copy rather than numpy.insert since this is
  67. # faster.
  68. new_points = numpy.empty((len(points), 3), numpy.float32)
  69. new_points[:,0] = points[:,0]
  70. new_points[:,1] = layer.height
  71. new_points[:,2] = -points[:,1]
  72. new_points /= 1000
  73. layer_data.addPolygon(layer.id, polygon.type, new_points, polygon.line_width)
  74. Job.yieldThread()
  75. Job.yieldThread()
  76. current_layer += 1
  77. progress = (current_layer / layer_count) * 100
  78. # TODO: Rebuild the layer data mesh once the layer has been processed.
  79. # This needs some work in LayerData so we can add the new layers instead of recreating the entire mesh.
  80. if self._abort_requested:
  81. if self._progress:
  82. self._progress.hide()
  83. return
  84. if self._progress:
  85. self._progress.setProgress(progress)
  86. # We are done processing all the layers we got from the engine, now create a mesh out of the data
  87. layer_data.build()
  88. if self._abort_requested:
  89. if self._progress:
  90. self._progress.hide()
  91. return
  92. #Add layerdata decorator to scene node to indicate that the node has layerdata
  93. decorator = LayerDataDecorator.LayerDataDecorator()
  94. decorator.setLayerData(layer_data)
  95. new_node.addDecorator(decorator)
  96. new_node.setMeshData(mesh)
  97. new_node.setParent(self._scene.getRoot()) #Note: After this we can no longer abort!
  98. if not settings.getSettingValue("machine_center_is_zero"):
  99. new_node.setPosition(Vector(-settings.getSettingValue("machine_width") / 2, 0.0, settings.getSettingValue("machine_depth") / 2))
  100. if self._progress:
  101. self._progress.setProgress(100)
  102. view = Application.getInstance().getController().getActiveView()
  103. if view.getPluginId() == "LayerView":
  104. view.resetLayerData()
  105. if self._progress:
  106. self._progress.hide()
  107. def _onActiveViewChanged(self):
  108. if self.isRunning():
  109. if Application.getInstance().getController().getActiveView().getPluginId() == "LayerView":
  110. if not self._progress:
  111. self._progress = Message(catalog.i18nc("@info:status", "Processing Layers"), 0, False, 0)
  112. if self._progress.getProgress() != 100:
  113. self._progress.show()
  114. else:
  115. if self._progress:
  116. self._progress.hide()