Write group content metadata for ufp export

CURA-6915
This commit is contained in:
Nino van Hooff 2020-07-03 09:56:28 +02:00
parent a56489b885
commit ead0594c56

View file

@ -1,7 +1,7 @@
# Copyright (c) 2020 Ultimaker B.V. # Copyright (c) 2020 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from typing import cast from typing import cast, List, Dict
from Charon.VirtualFile import VirtualFile # To open UFP files. from Charon.VirtualFile import VirtualFile # To open UFP files.
from Charon.OpenMode import OpenMode # To indicate that we want to write to UFP files. from Charon.OpenMode import OpenMode # To indicate that we want to write to UFP files.
@ -13,6 +13,7 @@ from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType
from UM.PluginRegistry import PluginRegistry # To get the g-code writer. from UM.PluginRegistry import PluginRegistry # To get the g-code writer.
from PyQt5.QtCore import QBuffer from PyQt5.QtCore import QBuffer
from UM.Scene.SceneNode import SceneNode
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
from cura.Snapshot import Snapshot from cura.Snapshot import Snapshot
from cura.Utils.Threading import call_on_qt_thread from cura.Utils.Threading import call_on_qt_thread
@ -68,7 +69,8 @@ class UFPWriter(MeshWriter):
return False return False
gcode = archive.getStream("/3D/model.gcode") gcode = archive.getStream("/3D/model.gcode")
gcode.write(gcode_textio.getvalue().encode("UTF-8")) gcode.write(gcode_textio.getvalue().encode("UTF-8"))
archive.addRelation(virtual_path = "/3D/model.gcode", relation_type = "http://schemas.ultimaker.org/package/2018/relationships/gcode") archive.addRelation(virtual_path="/3D/model.gcode",
relation_type="http://schemas.ultimaker.org/package/2018/relationships/gcode")
self._createSnapshot() self._createSnapshot()
@ -83,7 +85,9 @@ class UFPWriter(MeshWriter):
thumbnail_image.save(thumbnail_buffer, "PNG") thumbnail_image.save(thumbnail_buffer, "PNG")
thumbnail.write(thumbnail_buffer.data()) thumbnail.write(thumbnail_buffer.data())
archive.addRelation(virtual_path = "/Metadata/thumbnail.png", relation_type = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail", origin = "/3D/model.gcode") archive.addRelation(virtual_path="/Metadata/thumbnail.png",
relation_type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail",
origin="/3D/model.gcode")
else: else:
Logger.log("d", "Thumbnail not created, cannot save it") Logger.log("d", "Thumbnail not created, cannot save it")
@ -118,7 +122,8 @@ class UFPWriter(MeshWriter):
material_root_id = material.getMetaDataEntry("base_file") material_root_id = material.getMetaDataEntry("base_file")
material_root_query = container_registry.findContainers(id=material_root_id) material_root_query = container_registry.findContainers(id=material_root_id)
if not material_root_query: if not material_root_query:
Logger.log("e", "Cannot find material container with root id {root_id}".format(root_id = material_root_id)) Logger.log("e",
"Cannot find material container with root id {root_id}".format(root_id=material_root_id))
return False return False
material_container = material_root_query[0] material_container = material_root_query[0]
@ -151,11 +156,30 @@ class UFPWriter(MeshWriter):
To retrieve, use: `archive.getMetadata(METADATA_OBJECTS_PATH)` To retrieve, use: `archive.getMetadata(METADATA_OBJECTS_PATH)`
""" """
objects_model = CuraApplication.getInstance().getObjectsModel() objects_model = CuraApplication.getInstance().getObjectsModel()
object_metas = [{"name": item["name"]} object_metas = []
for item in objects_model.items
if item["node"].getMeshData() is not None and not item["node"].callDecoration("isNonPrintingMesh") for item in objects_model.items:
] object_metas = object_metas + UFPWriter._getObjectMetas(item["node"])
data = {METADATA_OBJECTS_PATH: object_metas} data = {METADATA_OBJECTS_PATH: object_metas}
archive.setMetadata(data) archive.setMetadata(data)
@staticmethod
def _getObjectMetas(node: SceneNode) -> List[Dict]:
"""Get object metadata to write for a Node.
:return: List of object metadata dictionaries.
Might contain > 1 element in case of a group node.
Might be empty in case of nonPrintingMesh
"""
nodes = [node]
if node.callDecoration("isGroup"):
nodes = nodes + node.getAllChildren() # all descendants
return [{"name": item.getName()}
for item in nodes
if item.getMeshData() is not None and not item.callDecoration("isNonPrintingMesh")
]