ProcessSlicedLayersJob.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.Logger import Logger
  11. from UM.Math.Vector import Vector
  12. from cura import LayerDataBuilder
  13. from cura import LayerDataDecorator
  14. from cura import LayerPolygon
  15. import numpy
  16. from time import time
  17. catalog = i18nCatalog("cura")
  18. class ProcessSlicedLayersJob(Job):
  19. def __init__(self, layers):
  20. super().__init__()
  21. self._layers = layers
  22. self._scene = Application.getInstance().getController().getScene()
  23. self._progress = None
  24. self._abort_requested = False
  25. ## Aborts the processing of layers.
  26. #
  27. # This abort is made on a best-effort basis, meaning that the actual
  28. # job thread will check once in a while to see whether an abort is
  29. # requested and then stop processing by itself. There is no guarantee
  30. # that the abort will stop the job any time soon or even at all.
  31. def abort(self):
  32. self._abort_requested = True
  33. def run(self):
  34. start_time = time()
  35. if Application.getInstance().getController().getActiveView().getPluginId() == "LayerView":
  36. self._progress = Message(catalog.i18nc("@info:status", "Processing Layers"), 0, False, -1)
  37. self._progress.show()
  38. Job.yieldThread()
  39. if self._abort_requested:
  40. if self._progress:
  41. self._progress.hide()
  42. return
  43. Application.getInstance().getController().activeViewChanged.connect(self._onActiveViewChanged)
  44. new_node = SceneNode()
  45. ## Remove old layer data (if any)
  46. for node in DepthFirstIterator(self._scene.getRoot()):
  47. if node.callDecoration("getLayerData"):
  48. node.getParent().removeChild(node)
  49. break
  50. if self._abort_requested:
  51. if self._progress:
  52. self._progress.hide()
  53. return
  54. mesh = MeshData()
  55. layer_data = LayerDataBuilder.LayerDataBuilder()
  56. layer_count = len(self._layers)
  57. # Find the minimum layer number
  58. # When using a raft, the raft layers are sent as layers < 0. Instead of allowing layers < 0, we
  59. # instead simply offset all other layers so the lowest layer is always 0.
  60. min_layer_number = 0
  61. for layer in self._layers:
  62. if layer.id < min_layer_number:
  63. min_layer_number = layer.id
  64. current_layer = 0
  65. for layer in self._layers:
  66. abs_layer_number = layer.id + abs(min_layer_number)
  67. layer_data.addLayer(abs_layer_number)
  68. this_layer = layer_data.getLayer(abs_layer_number)
  69. layer_data.setLayerHeight(abs_layer_number, layer.height)
  70. layer_data.setLayerThickness(abs_layer_number, layer.thickness)
  71. for p in range(layer.repeatedMessageCount("path_segment")):
  72. polygon = layer.getRepeatedMessage("path_segment", p)
  73. extruder = polygon.extruder
  74. line_types = numpy.fromstring(polygon.line_type, dtype="u1") # Convert bytearray to numpy array
  75. line_types = line_types.reshape((-1,1))
  76. points = numpy.fromstring(polygon.points, dtype="f4") # Convert bytearray to numpy array
  77. if polygon.point_type == 0: # Point2D
  78. points = points.reshape((-1,2)) # We get a linear list of pairs that make up the points, so make numpy interpret them correctly.
  79. else: # Point3D
  80. points = points.reshape((-1,3))
  81. line_widths = numpy.fromstring(polygon.line_width, dtype="f4") # Convert bytearray to numpy array
  82. 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.
  83. # Create a new 3D-array, copy the 2D points over and insert the right height.
  84. # This uses manual array creation + copy rather than numpy.insert since this is
  85. # faster.
  86. new_points = numpy.empty((len(points), 3), numpy.float32)
  87. if polygon.point_type == 0: # Point2D
  88. new_points[:, 0] = points[:, 0]
  89. new_points[:, 1] = layer.height / 1000 # layer height value is in backend representation
  90. new_points[:, 2] = -points[:, 1]
  91. else: # Point3D
  92. new_points[:, 0] = points[:, 0]
  93. new_points[:, 1] = points[:, 2]
  94. new_points[:, 2] = -points[:, 1]
  95. this_poly = LayerPolygon.LayerPolygon(layer_data, extruder, line_types, new_points, line_widths)
  96. this_poly.buildCache()
  97. this_layer.polygons.append(this_poly)
  98. Job.yieldThread()
  99. Job.yieldThread()
  100. current_layer += 1
  101. progress = (current_layer / layer_count) * 99
  102. # TODO: Rebuild the layer data mesh once the layer has been processed.
  103. # This needs some work in LayerData so we can add the new layers instead of recreating the entire mesh.
  104. if self._abort_requested:
  105. if self._progress:
  106. self._progress.hide()
  107. return
  108. if self._progress:
  109. self._progress.setProgress(progress)
  110. # We are done processing all the layers we got from the engine, now create a mesh out of the data
  111. layer_mesh = layer_data.build()
  112. if self._abort_requested:
  113. if self._progress:
  114. self._progress.hide()
  115. return
  116. # Add LayerDataDecorator to scene node to indicate that the node has layer data
  117. decorator = LayerDataDecorator.LayerDataDecorator()
  118. decorator.setLayerData(layer_mesh)
  119. new_node.addDecorator(decorator)
  120. new_node.setMeshData(mesh)
  121. # Set build volume as parent, the build volume can move as a result of raft settings.
  122. # It makes sense to set the build volume as parent: the print is actually printed on it.
  123. new_node_parent = Application.getInstance().getBuildVolume()
  124. new_node.setParent(new_node_parent) # Note: After this we can no longer abort!
  125. settings = Application.getInstance().getGlobalContainerStack()
  126. if not settings.getProperty("machine_center_is_zero", "value"):
  127. new_node.setPosition(Vector(-settings.getProperty("machine_width", "value") / 2, 0.0, settings.getProperty("machine_depth", "value") / 2))
  128. if self._progress:
  129. self._progress.setProgress(100)
  130. view = Application.getInstance().getController().getActiveView()
  131. if view.getPluginId() == "LayerView":
  132. view.resetLayerData()
  133. if self._progress:
  134. self._progress.hide()
  135. # Clear the unparsed layers. This saves us a bunch of memory if the Job does not get destroyed.
  136. self._layers = None
  137. Logger.log("d", "Processing layers took %s seconds", time() - start_time)
  138. def _onActiveViewChanged(self):
  139. if self.isRunning():
  140. if Application.getInstance().getController().getActiveView().getPluginId() == "LayerView":
  141. if not self._progress:
  142. self._progress = Message(catalog.i18nc("@info:status", "Processing Layers"), 0, False, 0)
  143. if self._progress.getProgress() != 100:
  144. self._progress.show()
  145. else:
  146. if self._progress:
  147. self._progress.hide()