mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-09 07:56:22 -06:00
Fix settings export in gcode
This commit is contained in:
parent
f2512a92a7
commit
440a56b7fa
1 changed files with 38 additions and 25 deletions
|
@ -1,17 +1,17 @@
|
||||||
# Copyright (c) 2017 Ultimaker B.V.
|
# Copyright (c) 2017 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.
|
||||||
|
|
||||||
|
import re # For escaping characters in the settings.
|
||||||
|
import json
|
||||||
|
import copy
|
||||||
|
|
||||||
from UM.Mesh.MeshWriter import MeshWriter
|
from UM.Mesh.MeshWriter import MeshWriter
|
||||||
from UM.Logger import Logger
|
from UM.Logger import Logger
|
||||||
from UM.Application import Application
|
from UM.Application import Application
|
||||||
from UM.Settings.InstanceContainer import InstanceContainer
|
from UM.Settings.InstanceContainer import InstanceContainer
|
||||||
from UM.Util import parseBool
|
|
||||||
|
|
||||||
from cura.Settings.ExtruderManager import ExtruderManager
|
from cura.Machines.QualityManager import getMachineDefinitionIDForQualitySearch
|
||||||
|
|
||||||
import re #For escaping characters in the settings.
|
|
||||||
import json
|
|
||||||
import copy
|
|
||||||
|
|
||||||
## Writes g-code to a file.
|
## Writes g-code to a file.
|
||||||
#
|
#
|
||||||
|
@ -45,6 +45,8 @@ class GCodeWriter(MeshWriter):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
self._application = Application.getInstance()
|
||||||
|
|
||||||
## Writes the g-code for the entire scene to a stream.
|
## Writes the g-code for the entire scene to a stream.
|
||||||
#
|
#
|
||||||
# Note that even though the function accepts a collection of nodes, the
|
# Note that even though the function accepts a collection of nodes, the
|
||||||
|
@ -94,7 +96,6 @@ class GCodeWriter(MeshWriter):
|
||||||
|
|
||||||
return flat_container
|
return flat_container
|
||||||
|
|
||||||
|
|
||||||
## Serialises a container stack to prepare it for writing at the end of the
|
## Serialises a container stack to prepare it for writing at the end of the
|
||||||
# g-code.
|
# g-code.
|
||||||
#
|
#
|
||||||
|
@ -104,15 +105,21 @@ class GCodeWriter(MeshWriter):
|
||||||
# \param settings A container stack to serialise.
|
# \param settings A container stack to serialise.
|
||||||
# \return A serialised string of the settings.
|
# \return A serialised string of the settings.
|
||||||
def _serialiseSettings(self, stack):
|
def _serialiseSettings(self, stack):
|
||||||
|
container_registry = self._application.getContainerRegistry()
|
||||||
|
quality_manager = self._application.getQualityManager()
|
||||||
|
|
||||||
prefix = ";SETTING_" + str(GCodeWriter.version) + " " # The prefix to put before each line.
|
prefix = ";SETTING_" + str(GCodeWriter.version) + " " # The prefix to put before each line.
|
||||||
prefix_length = len(prefix)
|
prefix_length = len(prefix)
|
||||||
|
|
||||||
|
quality_name = stack.qualityChanges.getName()
|
||||||
|
quality_type = stack.quality.getMetaDataEntry("quality_type")
|
||||||
container_with_profile = stack.qualityChanges
|
container_with_profile = stack.qualityChanges
|
||||||
if container_with_profile.getId() == "empty_quality_changes":
|
if container_with_profile.getId() == "empty_quality_changes":
|
||||||
Logger.log("e", "No valid quality profile found, not writing settings to g-code!")
|
# If the global quality changes is empty, create a new one
|
||||||
return ""
|
quality_name = container_registry.uniqueName(stack.quality.getName())
|
||||||
|
container_with_profile = quality_manager._createQualityChanges(quality_type, quality_name, stack, None)
|
||||||
|
|
||||||
flat_global_container = self._createFlattenedContainerInstance(stack.getTop(), container_with_profile)
|
flat_global_container = self._createFlattenedContainerInstance(stack.userChanges, container_with_profile)
|
||||||
# If the quality changes is not set, we need to set type manually
|
# If the quality changes is not set, we need to set type manually
|
||||||
if flat_global_container.getMetaDataEntry("type", None) is None:
|
if flat_global_container.getMetaDataEntry("type", None) is None:
|
||||||
flat_global_container.addMetaDataEntry("type", "quality_changes")
|
flat_global_container.addMetaDataEntry("type", "quality_changes")
|
||||||
|
@ -121,41 +128,47 @@ class GCodeWriter(MeshWriter):
|
||||||
if flat_global_container.getMetaDataEntry("quality_type", None) is None:
|
if flat_global_container.getMetaDataEntry("quality_type", None) is None:
|
||||||
flat_global_container.addMetaDataEntry("quality_type", stack.quality.getMetaDataEntry("quality_type", "normal"))
|
flat_global_container.addMetaDataEntry("quality_type", stack.quality.getMetaDataEntry("quality_type", "normal"))
|
||||||
|
|
||||||
# Change the default defintion
|
# Get the machine definition ID for quality profiles
|
||||||
default_machine_definition = "fdmprinter"
|
machine_definition_id_for_quality = getMachineDefinitionIDForQualitySearch(stack.definition)
|
||||||
if parseBool(stack.getMetaDataEntry("has_machine_quality", "False")):
|
flat_global_container.setMetaDataEntry("definition", machine_definition_id_for_quality)
|
||||||
default_machine_definition = stack.getMetaDataEntry("quality_definition")
|
|
||||||
if not default_machine_definition:
|
|
||||||
default_machine_definition = stack.definition.getId()
|
|
||||||
flat_global_container.setMetaDataEntry("definition", default_machine_definition)
|
|
||||||
|
|
||||||
serialized = flat_global_container.serialize()
|
serialized = flat_global_container.serialize()
|
||||||
data = {"global_quality": serialized}
|
data = {"global_quality": serialized}
|
||||||
|
|
||||||
for extruder in sorted(stack.extruders.values(), key = lambda k: k.getMetaDataEntry("position")):
|
all_setting_keys = set(flat_global_container.getAllKeys())
|
||||||
|
for extruder in sorted(stack.extruders.values(), key = lambda k: int(k.getMetaDataEntry("position"))):
|
||||||
extruder_quality = extruder.qualityChanges
|
extruder_quality = extruder.qualityChanges
|
||||||
if extruder_quality.getId() == "empty_quality_changes":
|
if extruder_quality.getId() == "empty_quality_changes":
|
||||||
Logger.log("w", "No extruder quality profile found, not writing quality for extruder %s to file!", extruder.getId())
|
# Same story, if quality changes is empty, create a new one
|
||||||
continue
|
quality_name = container_registry.uniqueName(stack.quality.getName())
|
||||||
flat_extruder_quality = self._createFlattenedContainerInstance(extruder.getTop(), extruder_quality)
|
extruder_quality = quality_manager._createQualityChanges(quality_type, quality_name, stack, None)
|
||||||
|
|
||||||
|
flat_extruder_quality = self._createFlattenedContainerInstance(extruder.userChanges, extruder_quality)
|
||||||
# If the quality changes is not set, we need to set type manually
|
# If the quality changes is not set, we need to set type manually
|
||||||
if flat_extruder_quality.getMetaDataEntry("type", None) is None:
|
if flat_extruder_quality.getMetaDataEntry("type", None) is None:
|
||||||
flat_extruder_quality.addMetaDataEntry("type", "quality_changes")
|
flat_extruder_quality.addMetaDataEntry("type", "quality_changes")
|
||||||
|
|
||||||
# Ensure that extruder is set. (Can happen if we have empty quality changes).
|
# Ensure that extruder is set. (Can happen if we have empty quality changes).
|
||||||
if flat_extruder_quality.getMetaDataEntry("extruder", None) is None:
|
if flat_extruder_quality.getMetaDataEntry("position", None) is None:
|
||||||
flat_extruder_quality.addMetaDataEntry("extruder", extruder.getBottom().getId())
|
flat_extruder_quality.addMetaDataEntry("position", extruder.getMetaDataEntry("position"))
|
||||||
|
|
||||||
# Ensure that quality_type is set. (Can happen if we have empty quality changes).
|
# Ensure that quality_type is set. (Can happen if we have empty quality changes).
|
||||||
if flat_extruder_quality.getMetaDataEntry("quality_type", None) is None:
|
if flat_extruder_quality.getMetaDataEntry("quality_type", None) is None:
|
||||||
flat_extruder_quality.addMetaDataEntry("quality_type", extruder.quality.getMetaDataEntry("quality_type", "normal"))
|
flat_extruder_quality.addMetaDataEntry("quality_type", extruder.quality.getMetaDataEntry("quality_type", "normal"))
|
||||||
|
|
||||||
# Change the default defintion
|
# Change the default definition
|
||||||
flat_extruder_quality.setMetaDataEntry("definition", default_machine_definition)
|
flat_extruder_quality.setMetaDataEntry("definition", machine_definition_id_for_quality)
|
||||||
|
|
||||||
extruder_serialized = flat_extruder_quality.serialize()
|
extruder_serialized = flat_extruder_quality.serialize()
|
||||||
data.setdefault("extruder_quality", []).append(extruder_serialized)
|
data.setdefault("extruder_quality", []).append(extruder_serialized)
|
||||||
|
|
||||||
|
all_setting_keys.update(set(flat_extruder_quality.getAllKeys()))
|
||||||
|
|
||||||
|
# Check if there is any profiles
|
||||||
|
if not all_setting_keys:
|
||||||
|
Logger.log("i", "No custom settings found, not writing settings to g-code.")
|
||||||
|
return ""
|
||||||
|
|
||||||
json_string = json.dumps(data)
|
json_string = json.dumps(data)
|
||||||
|
|
||||||
# Escape characters that have a special meaning in g-code comments.
|
# Escape characters that have a special meaning in g-code comments.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue