BuildVolume.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.View.Renderer import Renderer
  4. from UM.Scene.SceneNode import SceneNode
  5. from UM.Application import Application
  6. from UM.Resources import Resources
  7. from UM.Mesh.MeshData import MeshData
  8. from UM.Mesh.MeshBuilder import MeshBuilder
  9. from UM.Math.Vector import Vector
  10. from UM.Math.Color import Color
  11. from UM.Math.AxisAlignedBox import AxisAlignedBox
  12. import numpy
  13. class BuildVolume(SceneNode):
  14. VolumeOutlineColor = Color(12, 169, 227, 255)
  15. def __init__(self, parent = None):
  16. super().__init__(parent)
  17. self._width = 0
  18. self._height = 0
  19. self._depth = 0
  20. self._material = None
  21. self._grid_mesh = None
  22. self._grid_material = None
  23. self._disallowed_areas = []
  24. self._disallowed_area_mesh = None
  25. self.setCalculateBoundingBox(False)
  26. def setWidth(self, width):
  27. self._width = width
  28. def setHeight(self, height):
  29. self._height = height
  30. def setDepth(self, depth):
  31. self._depth = depth
  32. def setDisallowedAreas(self, areas):
  33. self._disallowed_areas = areas
  34. def render(self, renderer):
  35. if not self.getMeshData():
  36. return True
  37. if not self._material:
  38. self._material = renderer.createMaterial(
  39. Resources.getPath(Resources.ShadersLocation, "basic.vert"),
  40. Resources.getPath(Resources.ShadersLocation, "vertexcolor.frag")
  41. )
  42. self._grid_material = renderer.createMaterial(
  43. Resources.getPath(Resources.ShadersLocation, "basic.vert"),
  44. Resources.getPath(Resources.ShadersLocation, "grid.frag")
  45. )
  46. self._grid_material.setUniformValue("u_gridColor0", Color(245, 245, 245, 255))
  47. self._grid_material.setUniformValue("u_gridColor1", Color(205, 202, 201, 255))
  48. renderer.queueNode(self, material = self._material, mode = Renderer.RenderLines)
  49. renderer.queueNode(self, mesh = self._grid_mesh, material = self._grid_material)
  50. if self._disallowed_area_mesh:
  51. renderer.queueNode(self, mesh = self._disallowed_area_mesh, material = self._material)
  52. return True
  53. def rebuild(self):
  54. if self._width == 0 or self._height == 0 or self._depth == 0:
  55. return
  56. minW = -self._width / 2
  57. maxW = self._width / 2
  58. minH = 0.0
  59. maxH = self._height
  60. minD = -self._depth / 2
  61. maxD = self._depth / 2
  62. mb = MeshBuilder()
  63. mb.addLine(Vector(minW, minH, minD), Vector(maxW, minH, minD), color = self.VolumeOutlineColor)
  64. mb.addLine(Vector(minW, minH, minD), Vector(minW, maxH, minD), color = self.VolumeOutlineColor)
  65. mb.addLine(Vector(minW, maxH, minD), Vector(maxW, maxH, minD), color = self.VolumeOutlineColor)
  66. mb.addLine(Vector(maxW, minH, minD), Vector(maxW, maxH, minD), color = self.VolumeOutlineColor)
  67. mb.addLine(Vector(minW, minH, maxD), Vector(maxW, minH, maxD), color = self.VolumeOutlineColor)
  68. mb.addLine(Vector(minW, minH, maxD), Vector(minW, maxH, maxD), color = self.VolumeOutlineColor)
  69. mb.addLine(Vector(minW, maxH, maxD), Vector(maxW, maxH, maxD), color = self.VolumeOutlineColor)
  70. mb.addLine(Vector(maxW, minH, maxD), Vector(maxW, maxH, maxD), color = self.VolumeOutlineColor)
  71. mb.addLine(Vector(minW, minH, minD), Vector(minW, minH, maxD), color = self.VolumeOutlineColor)
  72. mb.addLine(Vector(maxW, minH, minD), Vector(maxW, minH, maxD), color = self.VolumeOutlineColor)
  73. mb.addLine(Vector(minW, maxH, minD), Vector(minW, maxH, maxD), color = self.VolumeOutlineColor)
  74. mb.addLine(Vector(maxW, maxH, minD), Vector(maxW, maxH, maxD), color = self.VolumeOutlineColor)
  75. self.setMeshData(mb.getData())
  76. mb = MeshBuilder()
  77. mb.addQuad(
  78. Vector(minW, minH, maxD),
  79. Vector(maxW, minH, maxD),
  80. Vector(maxW, minH, minD),
  81. Vector(minW, minH, minD)
  82. )
  83. self._grid_mesh = mb.getData()
  84. for n in range(0, 6):
  85. v = self._grid_mesh.getVertex(n)
  86. self._grid_mesh.setVertexUVCoordinates(n, v[0], v[2])
  87. if self._disallowed_areas:
  88. mb = MeshBuilder()
  89. for area in self._disallowed_areas:
  90. mb.addQuad(
  91. area[0],
  92. area[1],
  93. area[2],
  94. area[3],
  95. color = Color(174, 174, 174, 255)
  96. )
  97. self._disallowed_area_mesh = mb.getData()
  98. else:
  99. self._disallowed_area_mesh = None
  100. self._aabb = AxisAlignedBox(minimum = Vector(minW, minH - 1.0, minD), maximum = Vector(maxW, maxH, maxD))