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.Preferences import Preferences
  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.Settings.ExtrudersModel import ExtrudersModel
  22. catalog = i18nCatalog("cura")
  23. ## Return a 4-tuple with floats 0-1 representing the html color code
  24. #
  25. # \param color_code html color code, i.e. "#FF0000" -> red
  26. def colorCodeToRGBA(color_code):
  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. ## Aborts the processing of layers.
  44. #
  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. def abort(self):
  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. new_node = CuraSceneNode()
  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 = list(manager.getMachineExtruders(global_container_stack.getId()))
  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(Preferences.getInstance().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()