mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-23 14:44:13 -06:00
Introduced a LayerDataBuilder. Made LayerData immutable just like its superclass. Fixed the layer view which broke.
Contributes to CURA-1504
This commit is contained in:
parent
d1f68143a4
commit
0b858f3878
4 changed files with 89 additions and 58 deletions
|
@ -1,66 +1,25 @@
|
||||||
# Copyright (c) 2015 Ultimaker B.V.
|
# Copyright (c) 2015 Ultimaker B.V.
|
||||||
# Cura is released under the terms of the AGPLv3 or higher.
|
# Cura is released under the terms of the AGPLv3 or higher.
|
||||||
from .Layer import Layer
|
|
||||||
from .LayerPolygon import LayerPolygon
|
|
||||||
from UM.Mesh.MeshData import MeshData
|
from UM.Mesh.MeshData import MeshData
|
||||||
|
|
||||||
import numpy
|
## Class to holds the layer mesh and information about the layers.
|
||||||
|
# Immutable, use LayerDataBuilder to create one of these.
|
||||||
|
|
||||||
class LayerData(MeshData):
|
class LayerData(MeshData):
|
||||||
def __init__(self):
|
def __init__(self, vertices = None, normals = None, indices = None, colors = None, uvs = None, file_name = None,
|
||||||
super().__init__()
|
center_position = None, layers=None, element_counts=None):
|
||||||
self._layers = {}
|
super().__init__(vertices=vertices, normals=normals, indices=indices, colors=colors, uvs=uvs,
|
||||||
self._element_counts = {}
|
file_name=file_name, center_position=center_position)
|
||||||
|
self._layers = layers
|
||||||
def addLayer(self, layer):
|
self._element_counts = element_counts
|
||||||
if layer not in self._layers:
|
|
||||||
self._layers[layer] = Layer(layer)
|
|
||||||
|
|
||||||
def addPolygon(self, layer, polygon_type, data, line_width):
|
|
||||||
if layer not in self._layers:
|
|
||||||
self.addLayer(layer)
|
|
||||||
|
|
||||||
p = LayerPolygon(self, polygon_type, data, line_width)
|
|
||||||
self._layers[layer].polygons.append(p)
|
|
||||||
|
|
||||||
def getLayer(self, layer):
|
def getLayer(self, layer):
|
||||||
if layer in self._layers:
|
if layer in self._layers:
|
||||||
return self._layers[layer]
|
return self._layers[layer]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def getLayers(self):
|
def getLayers(self):
|
||||||
return self._layers
|
return self._layers
|
||||||
|
|
||||||
def getElementCounts(self):
|
def getElementCounts(self):
|
||||||
return self._element_counts
|
return self._element_counts
|
||||||
|
|
||||||
def setLayerHeight(self, layer, height):
|
|
||||||
if layer not in self._layers:
|
|
||||||
self.addLayer(layer)
|
|
||||||
|
|
||||||
self._layers[layer].setHeight(height)
|
|
||||||
|
|
||||||
def setLayerThickness(self, layer, thickness):
|
|
||||||
if layer not in self._layers:
|
|
||||||
self.addLayer(layer)
|
|
||||||
|
|
||||||
self._layers[layer].setThickness(thickness)
|
|
||||||
|
|
||||||
def build(self):
|
|
||||||
vertex_count = 0
|
|
||||||
for layer, data in self._layers.items():
|
|
||||||
vertex_count += data.vertexCount()
|
|
||||||
|
|
||||||
vertices = numpy.empty((vertex_count, 3), numpy.float32)
|
|
||||||
colors = numpy.empty((vertex_count, 4), numpy.float32)
|
|
||||||
indices = numpy.empty((vertex_count, 2), numpy.int32)
|
|
||||||
|
|
||||||
offset = 0
|
|
||||||
for layer, data in self._layers.items():
|
|
||||||
offset = data.build(offset, vertices, colors, indices)
|
|
||||||
self._element_counts[layer] = data.elementCount
|
|
||||||
|
|
||||||
self.clear()
|
|
||||||
self.addVertices(vertices)
|
|
||||||
self.addColors(colors)
|
|
||||||
self.addIndices(indices.flatten())
|
|
||||||
|
|
72
cura/LayerDataBuilder.py
Normal file
72
cura/LayerDataBuilder.py
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
# Copyright (c) 2015 Ultimaker B.V.
|
||||||
|
# Cura is released under the terms of the AGPLv3 or higher.
|
||||||
|
|
||||||
|
from .Layer import Layer
|
||||||
|
from .LayerPolygon import LayerPolygon
|
||||||
|
from UM.Mesh.MeshBuilder import MeshBuilder
|
||||||
|
from .LayerData import LayerData
|
||||||
|
|
||||||
|
import numpy
|
||||||
|
|
||||||
|
## Builder class for constructing a LayerData object
|
||||||
|
class LayerDataBuilder(MeshBuilder):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self._layers = {}
|
||||||
|
self._element_counts = {}
|
||||||
|
|
||||||
|
def addLayer(self, layer):
|
||||||
|
if layer not in self._layers:
|
||||||
|
self._layers[layer] = Layer(layer)
|
||||||
|
|
||||||
|
def addPolygon(self, layer, polygon_type, data, line_width):
|
||||||
|
if layer not in self._layers:
|
||||||
|
self.addLayer(layer)
|
||||||
|
|
||||||
|
p = LayerPolygon(self, polygon_type, data, line_width)
|
||||||
|
self._layers[layer].polygons.append(p)
|
||||||
|
|
||||||
|
def getLayer(self, layer):
|
||||||
|
if layer in self._layers:
|
||||||
|
return self._layers[layer]
|
||||||
|
|
||||||
|
def getLayers(self):
|
||||||
|
return self._layers
|
||||||
|
|
||||||
|
def getElementCounts(self):
|
||||||
|
return self._element_counts
|
||||||
|
|
||||||
|
def setLayerHeight(self, layer, height):
|
||||||
|
if layer not in self._layers:
|
||||||
|
self.addLayer(layer)
|
||||||
|
|
||||||
|
self._layers[layer].setHeight(height)
|
||||||
|
|
||||||
|
def setLayerThickness(self, layer, thickness):
|
||||||
|
if layer not in self._layers:
|
||||||
|
self.addLayer(layer)
|
||||||
|
|
||||||
|
self._layers[layer].setThickness(thickness)
|
||||||
|
|
||||||
|
def build(self):
|
||||||
|
vertex_count = 0
|
||||||
|
for layer, data in self._layers.items():
|
||||||
|
vertex_count += data.vertexCount()
|
||||||
|
|
||||||
|
vertices = numpy.empty((vertex_count, 3), numpy.float32)
|
||||||
|
colors = numpy.empty((vertex_count, 4), numpy.float32)
|
||||||
|
indices = numpy.empty((vertex_count, 2), numpy.int32)
|
||||||
|
|
||||||
|
offset = 0
|
||||||
|
for layer, data in self._layers.items():
|
||||||
|
offset = data.build(offset, vertices, colors, indices)
|
||||||
|
self._element_counts[layer] = data.elementCount
|
||||||
|
|
||||||
|
self.addVertices(vertices)
|
||||||
|
self.addColors(colors)
|
||||||
|
self.addIndices(indices.flatten())
|
||||||
|
|
||||||
|
return LayerData(vertices=self.getVertices(), normals=self.getNormals(), indices=self.getIndices(),
|
||||||
|
colors=self.getColors(), uvs=self.getUVCoordinates(), file_name=self.getFileName(),
|
||||||
|
center_position=self.getCenterPosition(), layers=self._layers,
|
||||||
|
element_counts=self._element_counts)
|
|
@ -12,7 +12,7 @@ from UM.i18n import i18nCatalog
|
||||||
|
|
||||||
from UM.Math.Vector import Vector
|
from UM.Math.Vector import Vector
|
||||||
|
|
||||||
from cura import LayerData
|
from cura import LayerDataBuilder
|
||||||
from cura import LayerDataDecorator
|
from cura import LayerDataDecorator
|
||||||
|
|
||||||
import numpy
|
import numpy
|
||||||
|
@ -65,7 +65,7 @@ class ProcessSlicedLayersJob(Job):
|
||||||
settings = Application.getInstance().getMachineManager().getWorkingProfile()
|
settings = Application.getInstance().getMachineManager().getWorkingProfile()
|
||||||
|
|
||||||
mesh = MeshData()
|
mesh = MeshData()
|
||||||
layer_data = LayerData.LayerData()
|
layer_data = LayerDataBuilder.LayerDataBuilder()
|
||||||
layer_count = len(self._layers)
|
layer_count = len(self._layers)
|
||||||
|
|
||||||
# Find the minimum layer number
|
# Find the minimum layer number
|
||||||
|
@ -117,7 +117,7 @@ class ProcessSlicedLayersJob(Job):
|
||||||
self._progress.setProgress(progress)
|
self._progress.setProgress(progress)
|
||||||
|
|
||||||
# We are done processing all the layers we got from the engine, now create a mesh out of the data
|
# We are done processing all the layers we got from the engine, now create a mesh out of the data
|
||||||
layer_data.build()
|
layer_mesh = layer_data.build()
|
||||||
|
|
||||||
if self._abort_requested:
|
if self._abort_requested:
|
||||||
if self._progress:
|
if self._progress:
|
||||||
|
@ -126,7 +126,7 @@ class ProcessSlicedLayersJob(Job):
|
||||||
|
|
||||||
# Add LayerDataDecorator to scene node to indicate that the node has layer data
|
# Add LayerDataDecorator to scene node to indicate that the node has layer data
|
||||||
decorator = LayerDataDecorator.LayerDataDecorator()
|
decorator = LayerDataDecorator.LayerDataDecorator()
|
||||||
decorator.setLayerData(layer_data)
|
decorator.setLayerData(layer_mesh)
|
||||||
new_node.addDecorator(decorator)
|
new_node.addDecorator(decorator)
|
||||||
|
|
||||||
new_node.setMeshData(mesh)
|
new_node.setMeshData(mesh)
|
||||||
|
|
|
@ -8,7 +8,7 @@ from UM.Event import Event, KeyEvent
|
||||||
from UM.Signal import Signal
|
from UM.Signal import Signal
|
||||||
from UM.Scene.Selection import Selection
|
from UM.Scene.Selection import Selection
|
||||||
from UM.Math.Color import Color
|
from UM.Math.Color import Color
|
||||||
from UM.Mesh.MeshData import MeshData
|
from UM.Mesh.MeshBuilder import MeshBuilder
|
||||||
from UM.Job import Job
|
from UM.Job import Job
|
||||||
|
|
||||||
from UM.View.RenderBatch import RenderBatch
|
from UM.View.RenderBatch import RenderBatch
|
||||||
|
@ -228,7 +228,7 @@ class _CreateTopLayersJob(Job):
|
||||||
if self._cancel or not layer_data:
|
if self._cancel or not layer_data:
|
||||||
return
|
return
|
||||||
|
|
||||||
layer_mesh = MeshData()
|
layer_mesh = MeshBuilder()
|
||||||
for i in range(self._solid_layers):
|
for i in range(self._solid_layers):
|
||||||
layer_number = self._layer_number - i
|
layer_number = self._layer_number - i
|
||||||
if layer_number < 0:
|
if layer_number < 0:
|
||||||
|
@ -263,7 +263,7 @@ class _CreateTopLayersJob(Job):
|
||||||
if not jump_mesh or jump_mesh.getVertices() is None:
|
if not jump_mesh or jump_mesh.getVertices() is None:
|
||||||
jump_mesh = None
|
jump_mesh = None
|
||||||
|
|
||||||
self.setResult({ "layers": layer_mesh, "jumps": jump_mesh })
|
self.setResult({ "layers": layer_mesh.build(), "jumps": jump_mesh })
|
||||||
|
|
||||||
def cancel(self):
|
def cancel(self):
|
||||||
self._cancel = True
|
self._cancel = True
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue