Merge pull request #8023 from Ultimaker/CURA-6915_identify_objects_on_buildplate

CURA-6915_remaining_issues
This commit is contained in:
Konstantinos Karmas 2020-07-06 11:44:54 +02:00 committed by GitHub
commit f0456526b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,18 +1,20 @@
#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.
from io import StringIO #For converting g-code to bytes. from io import StringIO # For converting g-code to bytes.
from UM.Logger import Logger from UM.Logger import Logger
from UM.Mesh.MeshWriter import MeshWriter #The writer we need to implement. from UM.Mesh.MeshWriter import MeshWriter # The writer we need to implement.
from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType 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.Iterator.DepthFirstIterator import DepthFirstIterator
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
@ -58,12 +60,12 @@ class UFPWriter(MeshWriter):
self._writeObjectList(archive) self._writeObjectList(archive)
#Store the g-code from the scene. # Store the g-code from the scene.
archive.addContentType(extension = "gcode", mime_type = "text/x-gcode") archive.addContentType(extension = "gcode", mime_type = "text/x-gcode")
gcode_textio = StringIO() #We have to convert the g-code into bytes. gcode_textio = StringIO() # We have to convert the g-code into bytes.
gcode_writer = cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("GCodeWriter")) gcode_writer = cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
success = gcode_writer.write(gcode_textio, None) success = gcode_writer.write(gcode_textio, None)
if not success: #Writing the g-code failed. Then I can also not write the gzipped g-code. if not success: # Writing the g-code failed. Then I can also not write the gzipped g-code.
self.setInformation(gcode_writer.getInformation()) self.setInformation(gcode_writer.getInformation())
return False return False
gcode = archive.getStream("/3D/model.gcode") gcode = archive.getStream("/3D/model.gcode")
@ -72,7 +74,7 @@ class UFPWriter(MeshWriter):
self._createSnapshot() self._createSnapshot()
#Store the thumbnail. # Store the thumbnail.
if self._snapshot: if self._snapshot:
archive.addContentType(extension = "png", mime_type = "image/png") archive.addContentType(extension = "png", mime_type = "image/png")
thumbnail = archive.getStream("/Metadata/thumbnail.png") thumbnail = archive.getStream("/Metadata/thumbnail.png")
@ -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")
@ -151,8 +155,25 @@ 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"]} for item in objects_model.items] object_metas = []
for item in objects_model.items:
object_metas.extend(UFPWriter._getObjectMetadata(item["node"]))
data = {METADATA_OBJECTS_PATH: object_metas} data = {METADATA_OBJECTS_PATH: object_metas}
archive.setMetadata(data) archive.setMetadata(data)
@staticmethod
def _getObjectMetadata(node: SceneNode) -> List[Dict[str, str]]:
"""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
"""
return [{"name": item.getName()}
for item in DepthFirstIterator(node)
if item.getMeshData() is not None and not item.callDecoration("isNonPrintingMesh")]