Esempio n. 1
0
    def _read(self, file_name: str) -> "CuraSceneNode":
        # Open the file
        archive = VirtualFile()
        archive.open(file_name)
        # Get the gcode data from the file
        gcode_data = archive.getData("/3D/model.gcode")
        # Convert the bytes stream to string
        gcode_stream = gcode_data["/3D/model.gcode"].decode("utf-8")

        # Open the GCodeReader to parse the data
        gcode_reader = PluginRegistry.getInstance().getPluginObject("GCodeReader")  # type: ignore
        gcode_reader.preReadFromStream(gcode_stream)  # type: ignore
        return gcode_reader.readFromStream(gcode_stream, file_name)  # type: ignore
Esempio n. 2
0
    def _read(self, file_name: str) -> "CuraSceneNode":
        # Open the file
        archive = VirtualFile()
        archive.open(file_name)
        # Get the gcode data from the file
        gcode_data = archive.getData("/3D/model.gcode")
        # Convert the bytes stream to string
        gcode_stream = gcode_data["/3D/model.gcode"].decode("utf-8")

        # Open the GCodeReader to parse the data
        gcode_reader = PluginRegistry.getInstance().getPluginObject("GCodeReader")  # type: ignore
        gcode_reader.preReadFromStream(gcode_stream)  # type: ignore
        return gcode_reader.readFromStream(gcode_stream)  # type: ignore
Esempio n. 3
0
def test_GCodeReader():
    f = VirtualFile()
    f.open(os.path.join(os.path.dirname(__file__), "resources", "um3.gcode"))
    assert f.getData(
        "/metadata")["/metadata/toolpath/default/flavor"] == "Griffin"
    assert b"M104" in f.getStream("/toolpath").read()
    f.close()
Esempio n. 4
0
    def write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode):
        archive = VirtualFile()
        archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)

        #Store the g-code from the scene.
        archive.addContentType(extension = "gcode", mime_type = "text/x-gcode")
        gcode_textio = StringIO() #We have to convert the g-code into bytes.
        gcode_writer = cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
        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.
            self.setInformation(gcode_writer.getInformation())
            return False
        gcode = archive.getStream("/3D/model.gcode")
        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")

        self._createSnapshot()

        #Store the thumbnail.
        if self._snapshot:
            archive.addContentType(extension = "png", mime_type = "image/png")
            thumbnail = archive.getStream("/Metadata/thumbnail.png")

            thumbnail_buffer = QBuffer()
            thumbnail_buffer.open(QBuffer.ReadWrite)
            thumbnail_image = self._snapshot
            thumbnail_image.save(thumbnail_buffer, "PNG")

            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")
        else:
            Logger.log("d", "Thumbnail not created, cannot save it")

        # Store the material.
        application = Application.getInstance()
        machine_manager = application.getMachineManager()
        material_manager = application.getMaterialManager()
        global_stack = machine_manager.activeMachine

        material_extension = "xml.fdm_material"
        material_mime_type = "application/x-ultimaker-material-profile"

        try:
            archive.addContentType(extension = material_extension, mime_type = material_mime_type)
        except:
            Logger.log("w", "The material extension: %s was already added", material_extension)

        added_materials = []
        for extruder_stack in global_stack.extruders.values():
            material = extruder_stack.material
            try:
                material_file_name = material.getMetaData()["base_file"] + ".xml.fdm_material"
            except KeyError:
                Logger.log("w", "Unable to get base_file for the material %s", material.getId())
                continue
            material_file_name = "/Materials/" + material_file_name

            # The same material should not be added again.
            if material_file_name in added_materials:
                continue

            material_root_id = material.getMetaDataEntry("base_file")
            material_group = material_manager.getMaterialGroup(material_root_id)
            if material_group is None:
                Logger.log("e", "Cannot find material container with root id [%s]", material_root_id)
                return False

            material_container = material_group.root_material_node.getContainer()
            try:
                serialized_material = material_container.serialize()
            except NotImplementedError:
                Logger.log("e", "Unable serialize material container with root id: %s", material_root_id)
                return False

            material_file = archive.getStream(material_file_name)
            material_file.write(serialized_material.encode("UTF-8"))
            archive.addRelation(virtual_path = material_file_name,
                                relation_type = "http://schemas.ultimaker.org/package/2018/relationships/material",
                                origin = "/3D/model.gcode")

            added_materials.append(material_file_name)

        archive.close()
        return True
Esempio n. 5
0
    def write(self, stream, nodes, mode=MeshWriter.OutputMode.BinaryMode):
        archive = VirtualFile()
        archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)

        #Store the g-code from the scene.
        archive.addContentType(extension="gcode", mime_type="text/x-gcode")
        gcode_textio = StringIO()  #We have to convert the g-code into bytes.
        gcode_writer = cast(
            MeshWriter,
            PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
        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.
            self.setInformation(gcode_writer.getInformation())
            return False
        gcode = archive.getStream("/3D/model.gcode")
        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")

        #Store the thumbnail.
        if self._snapshot:
            archive.addContentType(extension="png", mime_type="image/png")
            thumbnail = archive.getStream("/Metadata/thumbnail.png")

            thumbnail_buffer = QBuffer()
            thumbnail_buffer.open(QBuffer.ReadWrite)
            thumbnail_image = self._snapshot
            thumbnail_image.save(thumbnail_buffer, "PNG")

            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")
        else:
            Logger.log("d", "Thumbnail not created, cannot save it")

        archive.close()
        return True
Esempio n. 6
0
    def write(self, stream, nodes, mode=MeshWriter.OutputMode.BinaryMode):
        archive = VirtualFile()
        archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)

        #Store the g-code from the scene.
        archive.addContentType(extension="gcode", mime_type="text/x-gcode")
        gcode_textio = StringIO()  #We have to convert the g-code into bytes.
        gcode_writer = cast(
            MeshWriter,
            PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
        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.
            self.setInformation(gcode_writer.getInformation())
            return False
        gcode = archive.getStream("/3D/model.gcode")
        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")

        self._createSnapshot()

        #Store the thumbnail.
        if self._snapshot:
            archive.addContentType(extension="png", mime_type="image/png")
            thumbnail = archive.getStream("/Metadata/thumbnail.png")

            thumbnail_buffer = QBuffer()
            thumbnail_buffer.open(QBuffer.ReadWrite)
            thumbnail_image = self._snapshot
            thumbnail_image.save(thumbnail_buffer, "PNG")

            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")
        else:
            Logger.log("d", "Thumbnail not created, cannot save it")

        # Store the material.
        application = Application.getInstance()
        machine_manager = application.getMachineManager()
        material_manager = application.getMaterialManager()
        global_stack = machine_manager.activeMachine

        material_extension = "xml.fdm_material"
        material_mime_type = "application/x-ultimaker-material-profile"

        try:
            archive.addContentType(extension=material_extension,
                                   mime_type=material_mime_type)
        except:
            Logger.log("w", "The material extension: %s was already added",
                       material_extension)

        added_materials = []
        for extruder_stack in global_stack.extruders.values():
            material = extruder_stack.material
            try:
                material_file_name = material.getMetaData(
                )["base_file"] + ".xml.fdm_material"
            except KeyError:
                Logger.log("w", "Unable to get base_file for the material %s",
                           material.getId())
                continue
            material_file_name = "/Materials/" + material_file_name

            # The same material should not be added again.
            if material_file_name in added_materials:
                continue

            material_root_id = material.getMetaDataEntry("base_file")
            material_group = material_manager.getMaterialGroup(
                material_root_id)
            if material_group is None:
                Logger.log("e",
                           "Cannot find material container with root id [%s]",
                           material_root_id)
                return False

            material_container = material_group.root_material_node.getContainer(
            )
            try:
                serialized_material = material_container.serialize()
            except NotImplementedError:
                Logger.log(
                    "e",
                    "Unable serialize material container with root id: %s",
                    material_root_id)
                return False

            material_file = archive.getStream(material_file_name)
            material_file.write(serialized_material.encode("UTF-8"))
            archive.addRelation(
                virtual_path=material_file_name,
                relation_type=
                "http://schemas.ultimaker.org/package/2018/relationships/material",
                origin="/3D/model.gcode")

            added_materials.append(material_file_name)

        archive.close()
        return True
Esempio n. 7
0
    def write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode):
        archive = VirtualFile()
        archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)

        #Store the g-code from the scene.
        archive.addContentType(extension = "gcode", mime_type = "text/x-gcode")
        gcode_textio = StringIO() #We have to convert the g-code into bytes.
        PluginRegistry.getInstance().getPluginObject("GCodeWriter").write(gcode_textio, None)
        gcode = archive.getStream("/3D/model.gcode")
        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")

        #Store the thumbnail.
        if self._snapshot:
            archive.addContentType(extension = "png", mime_type = "image/png")
            thumbnail = archive.getStream("/Metadata/thumbnail.png")

            thumbnail_buffer = QBuffer()
            thumbnail_buffer.open(QBuffer.ReadWrite)
            thumbnail_image = self._snapshot
            thumbnail_image.save(thumbnail_buffer, "PNG")

            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")
        else:
            Logger.log("d", "Thumbnail not created, cannot save it")

        archive.close()
        return True
Esempio n. 8
0
    def write(self, stream, nodes, mode=MeshWriter.OutputMode.BinaryMode):
        archive = VirtualFile()
        archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)

        self._writeObjectList(archive)

        # Store the g-code from the scene.
        archive.addContentType(extension="gcode", mime_type="text/x-gcode")
        gcode_textio = StringIO()  # We have to convert the g-code into bytes.
        gcode_writer = cast(
            MeshWriter,
            PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
        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.
            self.setInformation(gcode_writer.getInformation())
            return False
        gcode = archive.getStream("/3D/model.gcode")
        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")

        # TODO temporarily commented out, as is causes a crash whenever the UFPWriter is called outside of the main thread
        # self._createSnapshot()
        #
        # # Store the thumbnail.
        # if self._snapshot:
        #     archive.addContentType(extension = "png", mime_type = "image/png")
        #     thumbnail = archive.getStream("/Metadata/thumbnail.png")
        #
        #     thumbnail_buffer = QBuffer()
        #     thumbnail_buffer.open(QBuffer.ReadWrite)
        #     thumbnail_image = self._snapshot
        #     thumbnail_image.save(thumbnail_buffer, "PNG")
        #
        #     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")
        # else:
        #     Logger.log("d", "Thumbnail not created, cannot save it")

        # Store the material.
        application = CuraApplication.getInstance()
        machine_manager = application.getMachineManager()
        container_registry = application.getContainerRegistry()
        global_stack = machine_manager.activeMachine

        material_extension = "xml.fdm_material"
        material_mime_type = "application/x-ultimaker-material-profile"

        try:
            archive.addContentType(extension=material_extension,
                                   mime_type=material_mime_type)
        except:
            Logger.log("w", "The material extension: %s was already added",
                       material_extension)

        added_materials = []
        for extruder_stack in global_stack.extruderList:
            material = extruder_stack.material
            try:
                material_file_name = material.getMetaData(
                )["base_file"] + ".xml.fdm_material"
            except KeyError:
                Logger.log("w", "Unable to get base_file for the material %s",
                           material.getId())
                continue
            material_file_name = "/Materials/" + material_file_name

            # The same material should not be added again.
            if material_file_name in added_materials:
                continue

            material_root_id = material.getMetaDataEntry("base_file")
            material_root_query = container_registry.findContainers(
                id=material_root_id)
            if not material_root_query:
                Logger.log(
                    "e",
                    "Cannot find material container with root id {root_id}".
                    format(root_id=material_root_id))
                return False
            material_container = material_root_query[0]

            try:
                serialized_material = material_container.serialize()
            except NotImplementedError:
                Logger.log(
                    "e",
                    "Unable serialize material container with root id: %s",
                    material_root_id)
                return False

            material_file = archive.getStream(material_file_name)
            material_file.write(serialized_material.encode("UTF-8"))
            archive.addRelation(
                virtual_path=material_file_name,
                relation_type=
                "http://schemas.ultimaker.org/package/2018/relationships/material",
                origin="/3D/model.gcode")

            added_materials.append(material_file_name)

        try:
            archive.close()
        except OSError as e:
            error_msg = catalog.i18nc(
                "@info:error", "Can't write to UFP file:") + " " + str(e)
            self.setInformation(error_msg)
            Logger.error(error_msg)
            return False
        return True