SolidView.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os.path
  4. from UM.View.View import View
  5. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  6. from UM.Scene.Selection import Selection
  7. from UM.Resources import Resources
  8. from PyQt6.QtGui import QOpenGLContext, QDesktopServices, QImage
  9. from PyQt6.QtCore import QSize, QUrl
  10. import numpy as np
  11. import time
  12. from UM.Application import Application
  13. from UM.Logger import Logger
  14. from UM.Message import Message
  15. from UM.Math.Color import Color
  16. from UM.Event import Event
  17. from UM.View.RenderBatch import RenderBatch
  18. from UM.View.GL.OpenGL import OpenGL
  19. from UM.i18n import i18nCatalog
  20. from cura.Settings.ExtruderManager import ExtruderManager
  21. from cura import XRayPass
  22. import math
  23. catalog = i18nCatalog("cura")
  24. class SolidView(View):
  25. """Standard view for mesh models."""
  26. _show_xray_warning_preference = "view/show_xray_warning"
  27. def __init__(self):
  28. super().__init__()
  29. application = Application.getInstance()
  30. application.getPreferences().addPreference("view/show_overhang", True)
  31. application.globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  32. self._enabled_shader = None
  33. self._disabled_shader = None
  34. self._non_printing_shader = None
  35. self._support_mesh_shader = None
  36. self._xray_shader = None
  37. self._xray_pass = None
  38. self._xray_composite_shader = None
  39. self._composite_pass = None
  40. self._extruders_model = None
  41. self._theme = None
  42. self._support_angle = self._retrieveSupportAngle()
  43. self._lowest_printable_height = self._retrieveLowestPrintHeight()
  44. self._global_stack = None
  45. self._old_composite_shader = None
  46. self._old_layer_bindings = None
  47. self._next_xray_checking_time = time.time()
  48. self._xray_checking_update_time = 30.0 # seconds
  49. self._xray_warning_cooldown = 60 * 10 # reshow Model error message every 10 minutes
  50. self._xray_warning_message = Message(
  51. catalog.i18nc("@info:status", "The highlighted areas indicate either missing or extraneous surfaces. Fix your model and open it again into Cura."),
  52. lifetime = 60 * 5, # leave message for 5 minutes
  53. title = catalog.i18nc("@info:title", "Model Errors"),
  54. option_text = catalog.i18nc("@info:option_text", "Do not show this message again"),
  55. option_state = False,
  56. message_type=Message.MessageType.WARNING
  57. )
  58. self._xray_warning_message.optionToggled.connect(self._onDontAskMeAgain)
  59. application.getPreferences().addPreference(self._show_xray_warning_preference, True)
  60. self._xray_warning_message.addAction("manifold", catalog.i18nc("@action:button", "Learn more"), "[no_icon]", "[no_description]",
  61. button_style = Message.ActionButtonStyle.LINK,
  62. button_align = Message.ActionButtonAlignment.ALIGN_LEFT)
  63. self._xray_warning_message.actionTriggered.connect(self._onNonManifoldLearnMoreClicked)
  64. application.engineCreatedSignal.connect(self._onGlobalContainerChanged)
  65. def _onDontAskMeAgain(self, checked: bool) -> None:
  66. Application.getInstance().getPreferences().setValue(self._show_xray_warning_preference, not checked)
  67. def _onNonManifoldLearnMoreClicked(self, action, message) -> None:
  68. QDesktopServices.openUrl(QUrl("https://support.ultimaker.com/hc/en-us/articles/360014055959"))
  69. def _onGlobalContainerChanged(self) -> None:
  70. if self._global_stack:
  71. try:
  72. self._global_stack.propertyChanged.disconnect(self._onPropertyChanged)
  73. except TypeError:
  74. pass
  75. for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
  76. extruder_stack.propertyChanged.disconnect(self._onPropertyChanged)
  77. self._global_stack = Application.getInstance().getGlobalContainerStack()
  78. if self._global_stack:
  79. self._global_stack.propertyChanged.connect(self._onPropertyChanged)
  80. for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
  81. extruder_stack.propertyChanged.connect(self._onPropertyChanged)
  82. # Force re-evaluation:
  83. self._support_angle = self._retrieveSupportAngle()
  84. self._lowest_printable_height = self._retrieveLowestPrintHeight()
  85. def _onPropertyChanged(self, key: str, property_name: str) -> None:
  86. if property_name != "value":
  87. return
  88. # As the rendering is called a *lot* we really, dont want to re-evaluate the property every time. So we store em!
  89. if key == "support_angle":
  90. self._support_angle = self._retrieveSupportAngle()
  91. elif key == "layer_height_0" or key == "slicing_tolerance":
  92. self._lowest_printable_height = self._retrieveLowestPrintHeight()
  93. def _retrieveSupportAngle(self) -> float:
  94. global_container_stack = Application.getInstance().getGlobalContainerStack()
  95. if global_container_stack:
  96. support_extruder_nr = int(global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))
  97. try:
  98. support_angle_stack = global_container_stack.extruderList[support_extruder_nr]
  99. except IndexError:
  100. pass
  101. else:
  102. angle = support_angle_stack.getProperty("support_angle", "value")
  103. if angle is not None:
  104. return angle
  105. return 90.0
  106. def _retrieveLowestPrintHeight(self) -> float:
  107. min_height = 0.0
  108. for extruder in Application.getInstance().getExtruderManager().getActiveExtruderStacks():
  109. init_layer_height = extruder.getProperty("layer_height_0", "value")
  110. tolerance_setting = extruder.getProperty("slicing_tolerance", "value")
  111. if tolerance_setting == "middle":
  112. init_layer_height /= 2.0
  113. min_height = max(min_height, init_layer_height)
  114. return min_height
  115. def _checkSetup(self):
  116. if not self._extruders_model:
  117. self._extruders_model = Application.getInstance().getExtrudersModel()
  118. if not self._theme:
  119. self._theme = Application.getInstance().getTheme()
  120. if not self._enabled_shader:
  121. self._enabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
  122. self._enabled_shader.setUniformValue("u_overhangColor", Color(*self._theme.getColor("model_overhang").getRgb()))
  123. self._enabled_shader.setUniformValue("u_renderError", 0.0)
  124. if not self._disabled_shader:
  125. self._disabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  126. self._disabled_shader.setUniformValue("u_diffuseColor1", Color(*self._theme.getColor("model_unslicable").getRgb()))
  127. self._disabled_shader.setUniformValue("u_diffuseColor2", Color(*self._theme.getColor("model_unslicable_alt").getRgb()))
  128. self._disabled_shader.setUniformValue("u_width", 50.0)
  129. if not self._non_printing_shader:
  130. self._non_printing_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "transparent_object.shader"))
  131. self._non_printing_shader.setUniformValue("u_diffuseColor", Color(*self._theme.getColor("model_non_printing").getRgb()))
  132. self._non_printing_shader.setUniformValue("u_opacity", 0.6)
  133. if not self._support_mesh_shader:
  134. self._support_mesh_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  135. self._support_mesh_shader.setUniformValue("u_vertical_stripes", True)
  136. self._support_mesh_shader.setUniformValue("u_width", 5.0)
  137. if not Application.getInstance().getPreferences().getValue(self._show_xray_warning_preference):
  138. self._xray_shader = None
  139. self._xray_composite_shader = None
  140. if self._composite_pass and 'xray' in self._composite_pass.getLayerBindings():
  141. self._composite_pass.setLayerBindings(self._old_layer_bindings)
  142. self._composite_pass.setCompositeShader(self._old_composite_shader)
  143. self._old_layer_bindings = None
  144. self._old_composite_shader = None
  145. self._enabled_shader.setUniformValue("u_renderError", 0.0) # We don't want any error markers!.
  146. self._xray_warning_message.hide()
  147. else:
  148. if not self._xray_shader:
  149. self._xray_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "xray.shader"))
  150. if not self._xray_composite_shader:
  151. self._xray_composite_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "xray_composite.shader"))
  152. theme = Application.getInstance().getTheme()
  153. self._xray_composite_shader.setUniformValue("u_background_color", Color(*theme.getColor("viewport_background").getRgb()))
  154. self._xray_composite_shader.setUniformValue("u_outline_color", Color(*theme.getColor("model_selection_outline").getRgb()))
  155. self._xray_composite_shader.setUniformValue("u_flat_error_color_mix", 0.) # Don't show flat error color in solid-view.
  156. renderer = self.getRenderer()
  157. if not self._composite_pass or not 'xray' in self._composite_pass.getLayerBindings():
  158. # Currently the RenderPass constructor requires a size > 0
  159. # This should be fixed in RenderPass's constructor.
  160. self._xray_pass = XRayPass.XRayPass(1, 1)
  161. self._enabled_shader.setUniformValue("u_renderError", 1.0) # We don't want any error markers!.
  162. renderer.addRenderPass(self._xray_pass)
  163. if not self._composite_pass:
  164. self._composite_pass = self.getRenderer().getRenderPass("composite")
  165. self._old_layer_bindings = self._composite_pass.getLayerBindings()
  166. self._composite_pass.setLayerBindings(["default", "selection", "xray"])
  167. self._old_composite_shader = self._composite_pass.getCompositeShader()
  168. self._composite_pass.setCompositeShader(self._xray_composite_shader)
  169. def beginRendering(self):
  170. scene = self.getController().getScene()
  171. renderer = self.getRenderer()
  172. self._checkSetup()
  173. global_container_stack = Application.getInstance().getGlobalContainerStack()
  174. if global_container_stack:
  175. if Application.getInstance().getPreferences().getValue("view/show_overhang"):
  176. # Make sure the overhang angle is valid before passing it to the shader
  177. if self._support_angle >= 0 and self._support_angle <= 90:
  178. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(90 - self._support_angle)))
  179. else:
  180. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0))) #Overhang angle of 0 causes no area at all to be marked as overhang.
  181. else:
  182. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0)))
  183. self._enabled_shader.setUniformValue("u_lowestPrintableHeight", self._lowest_printable_height)
  184. disabled_batch = renderer.createRenderBatch(shader = self._disabled_shader)
  185. normal_object_batch = renderer.createRenderBatch(shader = self._enabled_shader)
  186. renderer.addRenderBatch(disabled_batch)
  187. renderer.addRenderBatch(normal_object_batch)
  188. for node in DepthFirstIterator(scene.getRoot()):
  189. if node.render(renderer):
  190. continue
  191. if node.getMeshData() and node.isVisible():
  192. uniforms = {}
  193. shade_factor = 1.0
  194. per_mesh_stack = node.callDecoration("getStack")
  195. extruder_index = node.callDecoration("getActiveExtruderPosition")
  196. if extruder_index is None:
  197. extruder_index = "0"
  198. extruder_index = int(extruder_index)
  199. try:
  200. material_color = self._extruders_model.getItem(extruder_index)["color"]
  201. except KeyError:
  202. material_color = self._extruders_model.defaultColors[0]
  203. if extruder_index != ExtruderManager.getInstance().activeExtruderIndex:
  204. # Shade objects that are printed with the non-active extruder 25% darker
  205. shade_factor = 0.6
  206. try:
  207. # Colors are passed as rgb hex strings (eg "#ffffff"), and the shader needs
  208. # an rgba list of floats (eg [1.0, 1.0, 1.0, 1.0])
  209. uniforms["diffuse_color"] = [
  210. shade_factor * int(material_color[1:3], 16) / 255,
  211. shade_factor * int(material_color[3:5], 16) / 255,
  212. shade_factor * int(material_color[5:7], 16) / 255,
  213. 1.0
  214. ]
  215. # Color the currently selected face-id. (Disable for now.)
  216. #face = Selection.getHoverFace()
  217. uniforms["hover_face"] = -1 #if not face or node != face[0] else face[1]
  218. except ValueError:
  219. pass
  220. if node.callDecoration("isNonPrintingMesh"):
  221. if per_mesh_stack and (node.callDecoration("isInfillMesh") or node.callDecoration("isCuttingMesh")):
  222. renderer.queueNode(node, shader = self._non_printing_shader, uniforms = uniforms, transparent = True)
  223. else:
  224. renderer.queueNode(node, shader = self._non_printing_shader, transparent = True)
  225. elif getattr(node, "_outside_buildarea", False):
  226. disabled_batch.addItem(node.getWorldTransformation(copy = False), node.getMeshData(), normal_transformation = node.getCachedNormalMatrix())
  227. elif per_mesh_stack and node.callDecoration("isSupportMesh"):
  228. # Render support meshes with a vertical stripe that is darker
  229. shade_factor = 0.6
  230. uniforms["diffuse_color_2"] = [
  231. uniforms["diffuse_color"][0] * shade_factor,
  232. uniforms["diffuse_color"][1] * shade_factor,
  233. uniforms["diffuse_color"][2] * shade_factor,
  234. 1.0
  235. ]
  236. renderer.queueNode(node, shader = self._support_mesh_shader, uniforms = uniforms)
  237. else:
  238. normal_object_batch.addItem(node.getWorldTransformation(copy=False), node.getMeshData(), uniforms=uniforms, normal_transformation = node.getCachedNormalMatrix())
  239. if node.callDecoration("isGroup") and Selection.isSelected(node):
  240. renderer.queueNode(scene.getRoot(), mesh = node.getBoundingBoxMesh(), mode = RenderBatch.RenderMode.LineLoop)
  241. def endRendering(self):
  242. # check whether the xray overlay is showing badness
  243. if time.time() > self._next_xray_checking_time\
  244. and Application.getInstance().getPreferences().getValue(self._show_xray_warning_preference):
  245. self._next_xray_checking_time = time.time() + self._xray_checking_update_time
  246. xray_img = self._xray_pass.getOutput()
  247. xray_img = xray_img.convertToFormat(QImage.Format.Format_RGB888)
  248. # We can't just read the image since the pixels are aligned to internal memory positions.
  249. # xray_img.byteCount() != xray_img.width() * xray_img.height() * 3
  250. # The byte count is a little higher sometimes. We need to check the data per line, but fast using Numpy.
  251. # See https://stackoverflow.com/questions/5810970/get-raw-data-from-qimage for a description of the problem.
  252. # We can't use that solution though, since it doesn't perform well in Python.
  253. class QImageArrayView:
  254. """
  255. Class that ducktypes to be a Numpy ndarray.
  256. """
  257. def __init__(self, qimage):
  258. bits_pointer = qimage.bits()
  259. if bits_pointer is None: # If this happens before there is a window.
  260. self.__array_interface__ = {
  261. "shape": (0, 0),
  262. "typestr": "|u4",
  263. "data": (0, False),
  264. "strides": (1, 3),
  265. "version": 3
  266. }
  267. else:
  268. self.__array_interface__ = {
  269. "shape": (qimage.height(), qimage.width()),
  270. "typestr": "|u4", # Use 4 bytes per pixel rather than 3, since Numpy doesn't support 3.
  271. "data": (int(bits_pointer), False),
  272. "strides": (qimage.bytesPerLine(), 3), # This does the magic: For each line, skip the correct number of bytes. Bytes per pixel is always 3 due to QImage.Format.Format_RGB888.
  273. "version": 3
  274. }
  275. array = np.asarray(QImageArrayView(xray_img)).view(np.dtype({
  276. "r": (np.uint8, 0, "red"),
  277. "g": (np.uint8, 1, "green"),
  278. "b": (np.uint8, 2, "blue"),
  279. "a": (np.uint8, 3, "alpha") # Never filled since QImage was reformatted to RGB888.
  280. }), np.recarray)
  281. if np.any(np.mod(array.r, 2)):
  282. self._next_xray_checking_time = time.time() + self._xray_warning_cooldown
  283. self._xray_warning_message.show()
  284. Logger.log("i", "X-Ray overlay found non-manifold pixels.")
  285. def event(self, event):
  286. if event.type == Event.ViewDeactivateEvent:
  287. if self._composite_pass and 'xray' in self._composite_pass.getLayerBindings():
  288. self.getRenderer().removeRenderPass(self._xray_pass)
  289. self._composite_pass.setLayerBindings(self._old_layer_bindings)
  290. self._composite_pass.setCompositeShader(self._old_composite_shader)
  291. self._xray_warning_message.hide()