ProcessSlicedLayersJob.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #Copyright (c) 2019 Ultimaker B.V.
  2. #Cura is released under the terms of the LGPLv3 or higher.
  3. import gc
  4. import sys
  5. from UM.Job import Job
  6. from UM.Application import Application
  7. from UM.Mesh.MeshData import MeshData
  8. from UM.View.GL.OpenGLContext import OpenGLContext
  9. from UM.Message import Message
  10. from UM.i18n import i18nCatalog
  11. from UM.Logger import Logger
  12. from UM.Math.Vector import Vector
  13. from cura.Scene.BuildPlateDecorator import BuildPlateDecorator
  14. from cura.Scene.CuraSceneNode import CuraSceneNode
  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. from cura.Machines.Models.ExtrudersModel import ExtrudersModel
  22. catalog = i18nCatalog("cura")
  23. def colorCodeToRGBA(color_code):
  24. """Return a 4-tuple with floats 0-1 representing the html color code
  25. :param color_code: html color code, i.e. "#FF0000" -> red
  26. """
  27. if color_code is None:
  28. Logger.log("w", "Unable to convert color code, returning default")
  29. return [0, 0, 0, 1]
  30. return [
  31. int(color_code[1:3], 16) / 255,
  32. int(color_code[3:5], 16) / 255,
  33. int(color_code[5:7], 16) / 255,
  34. 1.0]
  35. class ProcessSlicedLayersJob(Job):
  36. def __init__(self, layers):
  37. super().__init__()
  38. self._layers = layers
  39. self._scene = Application.getInstance().getController().getScene()
  40. self._progress_message = Message(catalog.i18nc("@info:status", "Processing Layers"), 0, False, -1)
  41. self._abort_requested = False
  42. self._build_plate_number = None
  43. def abort(self):
  44. """Aborts the processing of layers.
  45. This abort is made on a best-effort basis, meaning that the actual
  46. job thread will check once in a while to see whether an abort is
  47. requested and then stop processing by itself. There is no guarantee
  48. that the abort will stop the job any time soon or even at all.
  49. """
  50. self._abort_requested = True
  51. def setBuildPlate(self, new_value):
  52. self._build_plate_number = new_value
  53. def getBuildPlate(self):
  54. return self._build_plate_number
  55. def run(self):
  56. Logger.log("d", "Processing new layer for build plate %s..." % self._build_plate_number)
  57. start_time = time()
  58. view = Application.getInstance().getController().getActiveView()
  59. if view.getPluginId() == "SimulationView":
  60. view.resetLayerData()
  61. self._progress_message.show()
  62. Job.yieldThread()
  63. if self._abort_requested:
  64. if self._progress_message:
  65. self._progress_message.hide()
  66. return
  67. Application.getInstance().getController().activeViewChanged.connect(self._onActiveViewChanged)
  68. # The no_setting_override is here because adding the SettingOverrideDecorator will trigger a reslice
  69. new_node = CuraSceneNode(no_setting_override = True)
  70. new_node.addDecorator(BuildPlateDecorator(self._build_plate_number))
  71. # Force garbage collection.
  72. # For some reason, Python has a tendency to keep the layer data
  73. # in memory longer than needed. Forcing the GC to run here makes
  74. # sure any old layer data is really cleaned up before adding new.
  75. gc.collect()
  76. mesh = MeshData()
  77. layer_data = LayerDataBuilder.LayerDataBuilder()
  78. layer_count = len(self._layers)
  79. # Find the minimum layer number
  80. # When disabling the remove empty first layers setting, the minimum layer number will be a positive
  81. # value. In that case the first empty layers will be discarded and start processing layers from the
  82. # first layer with data.
  83. # When using a raft, the raft layers are sent as layers < 0. Instead of allowing layers < 0, we
  84. # simply offset all other layers so the lowest layer is always 0. It could happens that the first
  85. # raft layer has value -8 but there are just 4 raft (negative) layers.
  86. min_layer_number = sys.maxsize
  87. negative_layers = 0
  88. for layer in self._layers:
  89. if layer.repeatedMessageCount("path_segment") > 0:
  90. if layer.id < min_layer_number:
  91. min_layer_number = layer.id
  92. if layer.id < 0:
  93. negative_layers += 1
  94. current_layer = 0
  95. for layer in self._layers:
  96. # If the layer is below the minimum, it means that there is no data, so that we don't create a layer
  97. # data. However, if there are empty layers in between, we compute them.
  98. if layer.id < min_layer_number:
  99. continue
  100. # Layers are offset by the minimum layer number. In case the raft (negative layers) is being used,
  101. # then the absolute layer number is adjusted by removing the empty layers that can be in between raft
  102. # and the model
  103. abs_layer_number = layer.id - min_layer_number
  104. if layer.id >= 0 and negative_layers != 0:
  105. abs_layer_number += (min_layer_number + negative_layers)
  106. layer_data.addLayer(abs_layer_number)
  107. this_layer = layer_data.getLayer(abs_layer_number)
  108. layer_data.setLayerHeight(abs_layer_number, layer.height)
  109. layer_data.setLayerThickness(abs_layer_number, layer.thickness)
  110. for p in range(layer.repeatedMessageCount("path_segment")):
  111. polygon = layer.getRepeatedMessage("path_segment", p)
  112. extruder = polygon.extruder
  113. line_types = numpy.fromstring(polygon.line_type, dtype = "u1") # Convert bytearray to numpy array
  114. line_types = line_types.reshape((-1,1))
  115. points = numpy.fromstring(polygon.points, dtype = "f4") # Convert bytearray to numpy array
  116. if polygon.point_type == 0: # Point2D
  117. points = points.reshape((-1,2)) # We get a linear list of pairs that make up the points, so make numpy interpret them correctly.
  118. else: # Point3D
  119. points = points.reshape((-1,3))
  120. line_widths = numpy.fromstring(polygon.line_width, dtype = "f4") # Convert bytearray to numpy array
  121. 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.
  122. line_thicknesses = numpy.fromstring(polygon.line_thickness, dtype = "f4") # Convert bytearray to numpy array
  123. line_thicknesses = line_thicknesses.reshape((-1,1)) # We get a linear list of pairs that make up the points, so make numpy interpret them correctly.
  124. line_feedrates = numpy.fromstring(polygon.line_feedrate, dtype = "f4") # Convert bytearray to numpy array
  125. line_feedrates = line_feedrates.reshape((-1,1)) # We get a linear list of pairs that make up the points, so make numpy interpret them correctly.
  126. # Create a new 3D-array, copy the 2D points over and insert the right height.
  127. # This uses manual array creation + copy rather than numpy.insert since this is
  128. # faster.
  129. new_points = numpy.empty((len(points), 3), numpy.float32)
  130. if polygon.point_type == 0: # Point2D
  131. new_points[:, 0] = points[:, 0]
  132. new_points[:, 1] = layer.height / 1000 # layer height value is in backend representation
  133. new_points[:, 2] = -points[:, 1]
  134. else: # Point3D
  135. new_points[:, 0] = points[:, 0]
  136. new_points[:, 1] = points[:, 2]
  137. new_points[:, 2] = -points[:, 1]
  138. this_poly = LayerPolygon.LayerPolygon(extruder, line_types, new_points, line_widths, line_thicknesses, line_feedrates)
  139. this_poly.buildCache()
  140. this_layer.polygons.append(this_poly)
  141. Job.yieldThread()
  142. Job.yieldThread()
  143. current_layer += 1
  144. progress = (current_layer / layer_count) * 99
  145. # TODO: Rebuild the layer data mesh once the layer has been processed.
  146. # This needs some work in LayerData so we can add the new layers instead of recreating the entire mesh.
  147. if self._abort_requested:
  148. if self._progress_message:
  149. self._progress_message.hide()
  150. return
  151. if self._progress_message:
  152. self._progress_message.setProgress(progress)
  153. # We are done processing all the layers we got from the engine, now create a mesh out of the data
  154. # Find out colors per extruder
  155. global_container_stack = Application.getInstance().getGlobalContainerStack()
  156. manager = ExtruderManager.getInstance()
  157. extruders = manager.getActiveExtruderStacks()
  158. if extruders:
  159. material_color_map = numpy.zeros((len(extruders), 4), dtype = numpy.float32)
  160. for extruder in extruders:
  161. position = int(extruder.getMetaDataEntry("position", default = "0"))
  162. try:
  163. default_color = ExtrudersModel.defaultColors[position]
  164. except IndexError:
  165. default_color = "#e0e000"
  166. color_code = extruder.material.getMetaDataEntry("color_code", default=default_color)
  167. color = colorCodeToRGBA(color_code)
  168. material_color_map[position, :] = color
  169. else:
  170. # Single extruder via global stack.
  171. material_color_map = numpy.zeros((1, 4), dtype = numpy.float32)
  172. color_code = global_container_stack.material.getMetaDataEntry("color_code", default = "#e0e000")
  173. color = colorCodeToRGBA(color_code)
  174. material_color_map[0, :] = color
  175. # We have to scale the colors for compatibility mode
  176. if OpenGLContext.isLegacyOpenGL() or bool(Application.getInstance().getPreferences().getValue("view/force_layer_view_compatibility_mode")):
  177. line_type_brightness = 0.5 # for compatibility mode
  178. else:
  179. line_type_brightness = 1.0
  180. layer_mesh = layer_data.build(material_color_map, line_type_brightness)
  181. if self._abort_requested:
  182. if self._progress_message:
  183. self._progress_message.hide()
  184. return
  185. # Add LayerDataDecorator to scene node to indicate that the node has layer data
  186. decorator = LayerDataDecorator.LayerDataDecorator()
  187. decorator.setLayerData(layer_mesh)
  188. new_node.addDecorator(decorator)
  189. new_node.setMeshData(mesh)
  190. # Set build volume as parent, the build volume can move as a result of raft settings.
  191. # It makes sense to set the build volume as parent: the print is actually printed on it.
  192. new_node_parent = Application.getInstance().getBuildVolume()
  193. new_node.setParent(new_node_parent) # Note: After this we can no longer abort!
  194. settings = Application.getInstance().getGlobalContainerStack()
  195. if not settings.getProperty("machine_center_is_zero", "value"):
  196. new_node.setPosition(Vector(-settings.getProperty("machine_width", "value") / 2, 0.0, settings.getProperty("machine_depth", "value") / 2))
  197. if self._progress_message:
  198. self._progress_message.setProgress(100)
  199. if self._progress_message:
  200. self._progress_message.hide()
  201. # Clear the unparsed layers. This saves us a bunch of memory if the Job does not get destroyed.
  202. self._layers = None
  203. Logger.log("d", "Processing layers took %s seconds", time() - start_time)
  204. def _onActiveViewChanged(self):
  205. if self.isRunning():
  206. if Application.getInstance().getController().getActiveView().getPluginId() == "SimulationView":
  207. if not self._progress_message:
  208. self._progress_message = Message(catalog.i18nc("@info:status", "Processing Layers"), 0, False, 0, catalog.i18nc("@info:title", "Information"))
  209. if self._progress_message.getProgress() != 100:
  210. self._progress_message.show()
  211. else:
  212. if self._progress_message:
  213. self._progress_message.hide()