Add a LayerData class and a Job class to create it from a Protobuf message

This commit is contained in:
Arjen Hiemstra 2015-01-28 11:57:47 +01:00
parent 4b3e2a3f8c
commit ecd8a8d2a3
2 changed files with 82 additions and 0 deletions

45
LayerData.py Normal file
View file

@ -0,0 +1,45 @@
from UM.Mesh.MeshData import MeshData
import numpy
import math
class LayerData(MeshData):
def __init__(self):
super().__init__()
self._layers = {}
def addPolygon(self, layer, type, data):
if layer not in self._layers:
self._layers[layer] = []
self._layers[layer].append(Polygon(self, type, data))
def getLayers(self):
return self._layers
class Polygon():
NoneType = 0
Inset0Type = 1
InsetXType = 2
SkinType = 3
SupportType = 4
SkirtType = 5
def __init__(self, mesh, type, data):
super().__init__()
self._type = type
self._begin = mesh._vertex_count
mesh.addVertices(data)
indices = [self._begin + i for i in range(len(data))]
mesh.addIndices(numpy.array(indices, dtype=numpy.int32))
self._end = mesh._vertex_count
@property
def type(self):
return self._type
@property
def data(self):
return self._data

View file

@ -0,0 +1,37 @@
from UM.Job import Job
from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
from UM.Scene.SceneNode import SceneNode
from UM.Application import Application
from UM.Mesh.MeshData import MeshData
from . import LayerData
import numpy
import struct
class ProcessSlicedObjectListJob(Job):
def __init__(self, message):
super().__init__(description = 'Processing sliced object', visible = True)
self._message = message
self._scene = Application.getInstance().getController().getScene()
def run(self):
objectIdMap = {}
for node in DepthFirstIterator(self._scene.getRoot()):
if type(node) is SceneNode and node.getMeshData():
objectIdMap[id(node)] = node
for object in self._message.objects:
mesh = objectIdMap[object.id].getMeshData()
layerData = LayerData.LayerData()
for layer in object.layers:
for polygon in layer.polygons:
points = numpy.fromstring(polygon.points, dtype='i8') # Convert bytearray to numpy array
points = points.reshape((-1,2)) # We get a linear list of pairs that make up the points, so make numpy interpret them correctly.
points = numpy.asarray(points, dtype=numpy.float32)
points /= 1000
points = numpy.insert(points, 1, layer.id / 10, axis = 1)
layerData.addPolygon(layer.id, polygon.type, points)
mesh.layerData = layerData