ProcessSlicedLayersJob.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import gc
  4. from UM.Job import Job
  5. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  6. from UM.Scene.SceneNode import SceneNode
  7. from UM.Application import Application
  8. from UM.Mesh.MeshData import MeshData
  9. from UM.Preferences import Preferences
  10. from UM.View.GL.OpenGLContext import OpenGLContext
  11. from UM.Message import Message
  12. from UM.i18n import i18nCatalog
  13. from UM.Logger import Logger
  14. from UM.Math.Vector import Vector
  15. from cura.Settings.ExtruderManager import ExtruderManager
  16. from cura import LayerDataBuilder
  17. from cura import LayerDataDecorator
  18. from cura import LayerPolygon
  19. import numpy
  20. from time import time
  21. catalog = i18nCatalog("cura")
  22. ## Return a 4-tuple with floats 0-1 representing the html color code
  23. #
  24. # \param color_code html color code, i.e. "#FF0000" -> red
  25. def colorCodeToRGBA(color_code):
  26. return [
  27. int(color_code[1:3], 16) / 255,
  28. int(color_code[3:5], 16) / 255,
  29. int(color_code[5:7], 16) / 255,
  30. 1.0]
  31. class ProcessSlicedLayersJob(Job):
  32. def __init__(self, layers):
  33. super().__init__()
  34. self._layers = layers
  35. self._scene = Application.getInstance().getController().getScene()
  36. self._progress = None
  37. self._abort_requested = False
  38. ## Aborts the processing of layers.
  39. #
  40. # This abort is made on a best-effort basis, meaning that the actual
  41. # job thread will check once in a while to see whether an abort is
  42. # requested and then stop processing by itself. There is no guarantee
  43. # that the abort will stop the job any time soon or even at all.
  44. def abort(self):
  45. self._abort_requested = True
  46. def run(self):
  47. start_time = time()
  48. if Application.getInstance().getController().getActiveView().getPluginId() == "LayerView":
  49. self._progress = Message(catalog.i18nc("@info:status", "Processing Layers"), 0, False, -1)
  50. self._progress.show()
  51. Job.yieldThread()
  52. if self._abort_requested:
  53. if self._progress:
  54. self._progress.hide()
  55. return
  56. Application.getInstance().getController().activeViewChanged.connect(self._onActiveViewChanged)
  57. new_node = SceneNode()
  58. ## Remove old layer data (if any)
  59. for node in DepthFirstIterator(self._scene.getRoot()):
  60. if node.callDecoration("getLayerData"):
  61. node.getParent().removeChild(node)
  62. break
  63. if self._abort_requested:
  64. if self._progress:
  65. self._progress.hide()
  66. return
  67. # Force garbage collection.
  68. # For some reason, Python has a tendency to keep the layer data
  69. # in memory longer than needed. Forcing the GC to run here makes
  70. # sure any old layer data is really cleaned up before adding new.
  71. gc.collect()
  72. mesh = MeshData()
  73. layer_data = LayerDataBuilder.LayerDataBuilder()
  74. layer_count = len(self._layers)
  75. # Find the minimum layer number
  76. # When using a raft, the raft layers are sent as layers < 0. Instead of allowing layers < 0, we
  77. # instead simply offset all other layers so the lowest layer is always 0.
  78. min_layer_number = 0
  79. for layer in self._layers:
  80. if layer.id < min_layer_number:
  81. min_layer_number = layer.id
  82. current_layer = 0
  83. for layer in self._layers:
  84. abs_layer_number = layer.id + abs(min_layer_number)
  85. layer_data.addLayer(abs_layer_number)
  86. this_layer = layer_data.getLayer(abs_layer_number)
  87. layer_data.setLayerHeight(abs_layer_number, layer.height)
  88. for p in range(layer.repeatedMessageCount("path_segment")):
  89. polygon = layer.getRepeatedMessage("path_segment", p)
  90. extruder = polygon.extruder
  91. line_types = numpy.fromstring(polygon.line_type, dtype="u1") # Convert bytearray to numpy array
  92. line_types = line_types.reshape((-1,1))
  93. points = numpy.fromstring(polygon.points, dtype="f4") # Convert bytearray to numpy array
  94. if polygon.point_type == 0: # Point2D
  95. points = points.reshape((-1,2)) # We get a linear list of pairs that make up the points, so make numpy interpret them correctly.
  96. else: # Point3D
  97. points = points.reshape((-1,3))
  98. line_widths = numpy.fromstring(polygon.line_width, dtype="f4") # Convert bytearray to numpy array
  99. line_widths = line_widths.reshape((-1,1)) # We get a linear list of pairs that make up the points, so make numpy interpret them correctly.
  100. # In the future, line_thicknesses should be given by CuraEngine as well.
  101. # Currently the infill layer thickness also translates to line width
  102. line_thicknesses = numpy.zeros(line_widths.shape, dtype="f4")
  103. line_thicknesses[:] = layer.thickness / 1000 # from micrometer to millimeter
  104. # Create a new 3D-array, copy the 2D points over and insert the right height.
  105. # This uses manual array creation + copy rather than numpy.insert since this is
  106. # faster.
  107. new_points = numpy.empty((len(points), 3), numpy.float32)
  108. if polygon.point_type == 0: # Point2D
  109. new_points[:, 0] = points[:, 0]
  110. new_points[:, 1] = layer.height / 1000 # layer height value is in backend representation
  111. new_points[:, 2] = -points[:, 1]
  112. else: # Point3D
  113. new_points[:, 0] = points[:, 0]
  114. new_points[:, 1] = points[:, 2]
  115. new_points[:, 2] = -points[:, 1]
  116. this_poly = LayerPolygon.LayerPolygon(extruder, line_types, new_points, line_widths, line_thicknesses)
  117. this_poly.buildCache()
  118. this_layer.polygons.append(this_poly)
  119. Job.yieldThread()
  120. Job.yieldThread()
  121. current_layer += 1
  122. progress = (current_layer / layer_count) * 99
  123. # TODO: Rebuild the layer data mesh once the layer has been processed.
  124. # This needs some work in LayerData so we can add the new layers instead of recreating the entire mesh.
  125. if self._abort_requested:
  126. if self._progress:
  127. self._progress.hide()
  128. return
  129. if self._progress:
  130. self._progress.setProgress(progress)
  131. # We are done processing all the layers we got from the engine, now create a mesh out of the data
  132. # Find out colors per extruder
  133. global_container_stack = Application.getInstance().getGlobalContainerStack()
  134. manager = ExtruderManager.getInstance()
  135. extruders = list(manager.getMachineExtruders(global_container_stack.getId()))
  136. if extruders:
  137. material_color_map = numpy.zeros((len(extruders), 4), dtype=numpy.float32)
  138. for extruder in extruders:
  139. material = extruder.findContainer({"type": "material"})
  140. position = int(extruder.getMetaDataEntry("position", default="0")) # Get the position
  141. color_code = material.getMetaDataEntry("color_code")
  142. color = colorCodeToRGBA(color_code)
  143. material_color_map[position, :] = color
  144. else:
  145. # Single extruder via global stack.
  146. material_color_map = numpy.zeros((1, 4), dtype=numpy.float32)
  147. material = global_container_stack.findContainer({"type": "material"})
  148. color_code = material.getMetaDataEntry("color_code")
  149. if color_code is None: # not all stacks have a material color
  150. color_code = "#e0e000"
  151. color = colorCodeToRGBA(color_code)
  152. material_color_map[0, :] = color
  153. # We have to scale the colors for compatibility mode
  154. if OpenGLContext.isLegacyOpenGL() or bool(Preferences.getInstance().getValue("view/force_layer_view_compatibility_mode")):
  155. line_type_brightness = 0.5 # for compatibility mode
  156. else:
  157. line_type_brightness = 1.0
  158. layer_mesh = layer_data.build(material_color_map, line_type_brightness)
  159. if self._abort_requested:
  160. if self._progress:
  161. self._progress.hide()
  162. return
  163. # Add LayerDataDecorator to scene node to indicate that the node has layer data
  164. decorator = LayerDataDecorator.LayerDataDecorator()
  165. decorator.setLayerData(layer_mesh)
  166. new_node.addDecorator(decorator)
  167. new_node.setMeshData(mesh)
  168. # Set build volume as parent, the build volume can move as a result of raft settings.
  169. # It makes sense to set the build volume as parent: the print is actually printed on it.
  170. new_node_parent = Application.getInstance().getBuildVolume()
  171. new_node.setParent(new_node_parent) # Note: After this we can no longer abort!
  172. settings = Application.getInstance().getGlobalContainerStack()
  173. if not settings.getProperty("machine_center_is_zero", "value"):
  174. new_node.setPosition(Vector(-settings.getProperty("machine_width", "value") / 2, 0.0, settings.getProperty("machine_depth", "value") / 2))
  175. if self._progress:
  176. self._progress.setProgress(100)
  177. view = Application.getInstance().getController().getActiveView()
  178. if view.getPluginId() == "LayerView":
  179. view.resetLayerData()
  180. if self._progress:
  181. self._progress.hide()
  182. # Clear the unparsed layers. This saves us a bunch of memory if the Job does not get destroyed.
  183. self._layers = None
  184. Logger.log("d", "Processing layers took %s seconds", time() - start_time)
  185. def _onActiveViewChanged(self):
  186. if self.isRunning():
  187. if Application.getInstance().getController().getActiveView().getPluginId() == "LayerView":
  188. if not self._progress:
  189. self._progress = Message(catalog.i18nc("@info:status", "Processing Layers"), 0, False, 0)
  190. if self._progress.getProgress() != 100:
  191. self._progress.show()
  192. else:
  193. if self._progress:
  194. self._progress.hide()