BuildVolume.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 getDisallowedAreas(self):
  33. return self._disallowed_areas
  34. def setDisallowedAreas(self, areas):
  35. self._disallowed_areas = areas
  36. def render(self, renderer):
  37. if not self.getMeshData():
  38. return True
  39. if not self._material:
  40. self._material = renderer.createMaterial(
  41. Resources.getPath(Resources.ShadersLocation, "basic.vert"),
  42. Resources.getPath(Resources.ShadersLocation, "vertexcolor.frag")
  43. )
  44. self._grid_material = renderer.createMaterial(
  45. Resources.getPath(Resources.ShadersLocation, "basic.vert"),
  46. Resources.getPath(Resources.ShadersLocation, "grid.frag")
  47. )
  48. self._grid_material.setUniformValue("u_gridColor0", Color(245, 245, 245, 255))
  49. self._grid_material.setUniformValue("u_gridColor1", Color(205, 202, 201, 255))
  50. renderer.queueNode(self, material = self._material, mode = Renderer.RenderLines)
  51. renderer.queueNode(self, mesh = self._grid_mesh, material = self._grid_material, force_single_sided = True)
  52. if self._disallowed_area_mesh:
  53. renderer.queueNode(self, mesh = self._disallowed_area_mesh, material = self._material)
  54. return True
  55. def rebuild(self):
  56. if self._width == 0 or self._height == 0 or self._depth == 0:
  57. return
  58. minW = -self._width / 2
  59. maxW = self._width / 2
  60. minH = 0.0
  61. maxH = self._height
  62. minD = -self._depth / 2
  63. maxD = self._depth / 2
  64. mb = MeshBuilder()
  65. mb.addLine(Vector(minW, minH, minD), Vector(maxW, minH, minD), color = self.VolumeOutlineColor)
  66. mb.addLine(Vector(minW, minH, minD), Vector(minW, maxH, minD), color = self.VolumeOutlineColor)
  67. mb.addLine(Vector(minW, maxH, minD), Vector(maxW, maxH, minD), color = self.VolumeOutlineColor)
  68. mb.addLine(Vector(maxW, minH, minD), Vector(maxW, maxH, minD), color = self.VolumeOutlineColor)
  69. mb.addLine(Vector(minW, minH, maxD), Vector(maxW, minH, maxD), color = self.VolumeOutlineColor)
  70. mb.addLine(Vector(minW, minH, maxD), Vector(minW, maxH, maxD), color = self.VolumeOutlineColor)
  71. mb.addLine(Vector(minW, maxH, maxD), Vector(maxW, maxH, maxD), color = self.VolumeOutlineColor)
  72. mb.addLine(Vector(maxW, minH, maxD), Vector(maxW, maxH, maxD), color = self.VolumeOutlineColor)
  73. mb.addLine(Vector(minW, minH, minD), Vector(minW, minH, maxD), color = self.VolumeOutlineColor)
  74. mb.addLine(Vector(maxW, minH, minD), Vector(maxW, minH, maxD), color = self.VolumeOutlineColor)
  75. mb.addLine(Vector(minW, maxH, minD), Vector(minW, maxH, maxD), color = self.VolumeOutlineColor)
  76. mb.addLine(Vector(maxW, maxH, minD), Vector(maxW, maxH, maxD), color = self.VolumeOutlineColor)
  77. self.setMeshData(mb.getData())
  78. mb = MeshBuilder()
  79. mb.addQuad(
  80. Vector(minW, minH, minD),
  81. Vector(maxW, minH, minD),
  82. Vector(maxW, minH, maxD),
  83. Vector(minW, minH, maxD)
  84. )
  85. self._grid_mesh = mb.getData()
  86. for n in range(0, 6):
  87. v = self._grid_mesh.getVertex(n)
  88. self._grid_mesh.setVertexUVCoordinates(n, v[0], v[2])
  89. disallowed_area_size = 0
  90. if self._disallowed_areas:
  91. mb = MeshBuilder()
  92. for polygon in self._disallowed_areas:
  93. points = polygon.getPoints()
  94. mb.addQuad(
  95. Vector(points[0, 0], 0.1, points[0, 1]),
  96. Vector(points[1, 0], 0.1, points[1, 1]),
  97. Vector(points[2, 0], 0.1, points[2, 1]),
  98. Vector(points[3, 0], 0.1, points[3, 1]),
  99. color = Color(174, 174, 174, 255)
  100. )
  101. # Find the largest disallowed area to exclude it from the maximum scale bounds
  102. size = abs(numpy.max(points[:, 1]) - numpy.min(points[:, 1]))
  103. disallowed_area_size = max(size, disallowed_area_size)
  104. self._disallowed_area_mesh = mb.getData()
  105. else:
  106. self._disallowed_area_mesh = None
  107. self._aabb = AxisAlignedBox(minimum = Vector(minW, minH - 1.0, minD), maximum = Vector(maxW, maxH, maxD))
  108. settings = Application.getInstance().getActiveMachine()
  109. skirt_size = 0.0
  110. if settings.getSettingValueByKey("adhesion_type") == "None":
  111. skirt_size = settings.getSettingValueByKey("skirt_line_count") * settings.getSettingValueByKey("skirt_line_width") + settings.getSettingValueByKey("skirt_gap")
  112. elif settings.getSettingValueByKey("adhesion_type") == "Brim":
  113. skirt_size = settings.getSettingValueByKey("brim_line_count") * settings.getSettingValueByKey("skirt_line_width")
  114. else:
  115. skirt_size = settings.getSettingValueByKey("skirt_line_width")
  116. skirt_size += settings.getSettingValueByKey("skirt_line_width")
  117. scale_to_max_bounds = AxisAlignedBox(
  118. minimum = Vector(minW + skirt_size, minH, minD + skirt_size + disallowed_area_size),
  119. maximum = Vector(maxW - skirt_size, maxH, maxD - skirt_size - disallowed_area_size)
  120. )
  121. Application.getInstance().getController().getScene()._maximum_bounds = scale_to_max_bounds