ProcessSlicedLayersJob.py 11 KB

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