mirror of
https://git.mirrors.martin98.com/https://github.com/Ultimaker/Cura
synced 2025-06-04 11:14:21 +08:00
3mf reader now uses libSavitar for loading
This greatly decreases (~factor 10) the time required to load 3mf files CURA-3215
This commit is contained in:
parent
425dbf1ad8
commit
a3326a8313
@ -18,6 +18,9 @@ from cura.Settings.ExtruderManager import ExtruderManager
|
|||||||
from cura.QualityManager import QualityManager
|
from cura.QualityManager import QualityManager
|
||||||
from UM.Scene.SceneNode import SceneNode
|
from UM.Scene.SceneNode import SceneNode
|
||||||
|
|
||||||
|
import Savitar
|
||||||
|
import numpy
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import xml.etree.cElementTree as ET
|
import xml.etree.cElementTree as ET
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -129,6 +132,9 @@ class ThreeMFReader(MeshReader):
|
|||||||
return node
|
return node
|
||||||
|
|
||||||
def _createMatrixFromTransformationString(self, transformation):
|
def _createMatrixFromTransformationString(self, transformation):
|
||||||
|
if transformation == "":
|
||||||
|
return Matrix()
|
||||||
|
|
||||||
splitted_transformation = transformation.split()
|
splitted_transformation = transformation.split()
|
||||||
## Transformation is saved as:
|
## Transformation is saved as:
|
||||||
## M00 M01 M02 0.0
|
## M00 M01 M02 0.0
|
||||||
@ -155,51 +161,92 @@ class ThreeMFReader(MeshReader):
|
|||||||
|
|
||||||
return temp_mat
|
return temp_mat
|
||||||
|
|
||||||
|
def _convertSavitarNodeToUMNode(self, savitar_node):
|
||||||
|
um_node = SceneNode()
|
||||||
|
transformation = self._createMatrixFromTransformationString(savitar_node.getTransformation())
|
||||||
|
um_node.setTransformation(transformation)
|
||||||
|
mesh_builder = MeshBuilder()
|
||||||
|
|
||||||
|
data = numpy.fromstring(savitar_node.getMeshData().getFlatVerticesAsBytes(), dtype=numpy.float32)
|
||||||
|
|
||||||
|
vertices = numpy.resize(data, (int(data.size / 3), 3))
|
||||||
|
mesh_builder.setVertices(vertices)
|
||||||
|
mesh_builder.calculateNormals(fast=True)
|
||||||
|
mesh_data = mesh_builder.build()
|
||||||
|
|
||||||
|
if len(mesh_data.getVertices()):
|
||||||
|
um_node.setMeshData(mesh_data)
|
||||||
|
|
||||||
|
for child in savitar_node.getChildren():
|
||||||
|
um_node.addChild(self._convertSavitarNodeToUMNode(child))
|
||||||
|
settings = savitar_node.getSettings()
|
||||||
|
|
||||||
|
# Add the setting override decorator, so we can add settings to this node.
|
||||||
|
if settings:
|
||||||
|
um_node.addDecorator(SettingOverrideDecorator())
|
||||||
|
|
||||||
|
global_container_stack = Application.getInstance().getGlobalContainerStack()
|
||||||
|
# Ensure the correct next container for the SettingOverride decorator is set.
|
||||||
|
if global_container_stack:
|
||||||
|
multi_extrusion = global_container_stack.getProperty("machine_extruder_count", "value") > 1
|
||||||
|
|
||||||
|
# Ensure that all extruder data is reset
|
||||||
|
if not multi_extrusion:
|
||||||
|
default_stack_id = global_container_stack.getId()
|
||||||
|
else:
|
||||||
|
default_stack = ExtruderManager.getInstance().getExtruderStack(0)
|
||||||
|
if default_stack:
|
||||||
|
default_stack_id = default_stack.getId()
|
||||||
|
else:
|
||||||
|
default_stack_id = global_container_stack.getId()
|
||||||
|
um_node.callDecoration("setActiveExtruder", default_stack_id)
|
||||||
|
|
||||||
|
# Get the definition & set it
|
||||||
|
definition = QualityManager.getInstance().getParentMachineDefinition(global_container_stack.getBottom())
|
||||||
|
um_node.callDecoration("getStack").getTop().setDefinition(definition)
|
||||||
|
|
||||||
|
setting_container = um_node.callDecoration("getStack").getTop()
|
||||||
|
|
||||||
|
for key in settings:
|
||||||
|
setting_value = settings[key]
|
||||||
|
|
||||||
|
# Extruder_nr is a special case.
|
||||||
|
if key == "extruder_nr":
|
||||||
|
extruder_stack = ExtruderManager.getInstance().getExtruderStack(int(setting_value))
|
||||||
|
if extruder_stack:
|
||||||
|
um_node.callDecoration("setActiveExtruder", extruder_stack.getId())
|
||||||
|
else:
|
||||||
|
Logger.log("w", "Unable to find extruder in position %s", setting_value)
|
||||||
|
continue
|
||||||
|
setting_container.setProperty(key,"value", setting_value)
|
||||||
|
|
||||||
|
if len(um_node.getChildren()) > 0:
|
||||||
|
group_decorator = GroupDecorator()
|
||||||
|
um_node.addDecorator(group_decorator)
|
||||||
|
um_node.setSelectable(True)
|
||||||
|
return um_node
|
||||||
|
|
||||||
def read(self, file_name):
|
def read(self, file_name):
|
||||||
result = []
|
result = []
|
||||||
# The base object of 3mf is a zipped archive.
|
# The base object of 3mf is a zipped archive.
|
||||||
|
try:
|
||||||
archive = zipfile.ZipFile(file_name, "r")
|
archive = zipfile.ZipFile(file_name, "r")
|
||||||
self._base_name = os.path.basename(file_name)
|
self._base_name = os.path.basename(file_name)
|
||||||
try:
|
parser = Savitar.ThreeMFParser()
|
||||||
self._root = ET.parse(archive.open("3D/3dmodel.model"))
|
scene_3mf = parser.parse(archive.open("3D/3dmodel.model").read())
|
||||||
self._unit = self._root.getroot().get("unit")
|
self._unit = scene_3mf.getUnit()
|
||||||
|
for node in scene_3mf.getSceneNodes():
|
||||||
build_items = self._root.findall("./3mf:build/3mf:item", self._namespaces)
|
um_node = self._convertSavitarNodeToUMNode(node)
|
||||||
|
|
||||||
for build_item in build_items:
|
|
||||||
id = build_item.get("objectid")
|
|
||||||
object = self._root.find("./3mf:resources/3mf:object[@id='{0}']".format(id), self._namespaces)
|
|
||||||
if "type" in object.attrib:
|
|
||||||
if object.attrib["type"] == "support" or object.attrib["type"] == "other":
|
|
||||||
# Ignore support objects, as cura does not support these.
|
|
||||||
# We can't guarantee that they wont be made solid.
|
|
||||||
# We also ignore "other", as I have no idea what to do with them.
|
|
||||||
Logger.log("w", "3MF file contained an object of type %s which is not supported by Cura", object.attrib["type"])
|
|
||||||
continue
|
|
||||||
elif object.attrib["type"] == "solidsupport" or object.attrib["type"] == "model":
|
|
||||||
pass # Load these as normal
|
|
||||||
else:
|
|
||||||
# We should technically fail at this point because it's an invalid 3MF, but try to continue anyway.
|
|
||||||
Logger.log("e", "3MF file contained an object of type %s which is not supported by the 3mf spec",
|
|
||||||
object.attrib["type"])
|
|
||||||
continue
|
|
||||||
|
|
||||||
build_item_node = self._createNodeFromObject(object, self._base_name + "_" + str(id))
|
|
||||||
|
|
||||||
# compensate for original center position, if object(s) is/are not around its zero position
|
# compensate for original center position, if object(s) is/are not around its zero position
|
||||||
|
|
||||||
transform_matrix = Matrix()
|
transform_matrix = Matrix()
|
||||||
mesh_data = build_item_node.getMeshData()
|
mesh_data = um_node.getMeshData()
|
||||||
if mesh_data is not None:
|
if mesh_data is not None:
|
||||||
extents = mesh_data.getExtents()
|
extents = mesh_data.getExtents()
|
||||||
center_vector = Vector(extents.center.x, extents.center.y, extents.center.z)
|
center_vector = Vector(extents.center.x, extents.center.y, extents.center.z)
|
||||||
transform_matrix.setByTranslation(center_vector)
|
transform_matrix.setByTranslation(center_vector)
|
||||||
|
transform_matrix.multiply(um_node.getLocalTransformation())
|
||||||
# offset with transform from 3mf
|
um_node.setTransformation(transform_matrix)
|
||||||
transform = build_item.get("transform")
|
|
||||||
if transform is not None:
|
|
||||||
transform_matrix.multiply(self._createMatrixFromTransformationString(transform))
|
|
||||||
|
|
||||||
build_item_node.setTransformation(transform_matrix)
|
|
||||||
|
|
||||||
global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
|
global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
|
||||||
|
|
||||||
@ -227,12 +274,13 @@ class ThreeMFReader(MeshReader):
|
|||||||
transformation_matrix.multiply(scale_matrix)
|
transformation_matrix.multiply(scale_matrix)
|
||||||
|
|
||||||
# Pre multiply the transformation with the loaded transformation, so the data is handled correctly.
|
# Pre multiply the transformation with the loaded transformation, so the data is handled correctly.
|
||||||
build_item_node.setTransformation(build_item_node.getLocalTransformation().preMultiply(transformation_matrix))
|
um_node.setTransformation(um_node.getLocalTransformation().preMultiply(transformation_matrix))
|
||||||
|
|
||||||
result.append(build_item_node)
|
result.append(um_node)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception:
|
||||||
Logger.log("e", "An exception occurred in 3mf reader: %s", e)
|
Logger.logException("e", "An exception occurred in 3mf reader.")
|
||||||
|
return []
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user