From 9e1b10d1b87864a70a8ea4c89e4faec0f0527cb7 Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Thu, 16 Jun 2016 20:02:21 +0200 Subject: [PATCH 01/12] CURA-1615: Updating GCodeProfileReader I got the plugin most of all working. At least the "successfully" imported profile XY" dialog appears. But sadly the profile does not appear in the list of profiles after that. I can only guess something is blocking here. Additionally it should be noted that G-Code exported from Cura 2.1.x does not work here anymore on Cura 2.2.x. --- .../GCodeProfileReader/GCodeProfileReader.py | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/plugins/GCodeProfileReader/GCodeProfileReader.py b/plugins/GCodeProfileReader/GCodeProfileReader.py index 11cc249657..4e1604fdc0 100644 --- a/plugins/GCodeProfileReader/GCodeProfileReader.py +++ b/plugins/GCodeProfileReader/GCodeProfileReader.py @@ -1,19 +1,21 @@ # Copyright (c) 2015 Ultimaker B.V. # Cura is released under the terms of the AGPLv3 or higher. -from UM.Application import Application #To get the machine manager to create the new profile in. -from UM.Settings.Profile import Profile -from UM.Settings.ProfileReader import ProfileReader -from UM.Logger import Logger +import os import re #Regular expressions for parsing escape characters in the settings. +from UM.Application import Application #To get the machine manager to create the new profile in. +from UM.Settings.InstanceContainer import InstanceContainer +from UM.Logger import Logger + +from cura.ProfileReader import ProfileReader ## A class that reads profile data from g-code files. # # It reads the profile data from g-code files and stores it in a new profile. # This class currently does not process the rest of the g-code in any way. class GCodeProfileReader(ProfileReader): - ## The file format version of the serialised g-code. + ## The file format version of the serialized g-code. # # It can only read settings with the same version as the version it was # written with. If the file format is changed in a way that breaks reverse @@ -51,31 +53,32 @@ class GCodeProfileReader(ProfileReader): # Loading all settings from the file. # They are all at the end, but Python has no reverse seek any more since Python3. # TODO: Consider moving settings to the start? - serialised = "" # Will be filled with the serialised profile. + serialized = "" # Will be filled with the serialized profile. try: with open(file_name) as f: for line in f: if line.startswith(prefix): # Remove the prefix and the newline from the line and add it to the rest. - serialised += line[prefix_length : -1] + serialized += line[prefix_length : -1] except IOError as e: Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e)) return None - - # Un-escape the serialised profile. + + # Un-escape the serialized profile. pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys())) # Perform the replacement with a regular expression. - serialised = pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], serialised) + serialized = pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], serialized) + Logger.log("i", "Serialized the following from %s: %s" %(file_name, repr(serialized))) - # Apply the changes to the current profile. - profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False) + # Create an empty profile with the name of the G-code file + profile = InstanceContainer(os.path.basename(os.path.splitext(file_name)[0])) + profile.addMetaDataEntry("type", "quality") try: - profile.unserialise(serialised) - profile.setType(None) # Force type to none so it's correctly added. + profile.deserialize(serialized) profile.setReadOnly(False) - profile.setDirty(True) except Exception as e: # Not a valid g-code file. Logger.log("e", "Unable to serialise the profile: %s", str(e)) return None + return profile \ No newline at end of file From 42a860535c80b91c9c858f412c02ef0e7be51cc2 Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Thu, 16 Jun 2016 20:19:03 +0200 Subject: [PATCH 02/12] Updating API to 3 --- plugins/GCodeProfileReader/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/GCodeProfileReader/__init__.py b/plugins/GCodeProfileReader/__init__.py index 1f4ced2ae6..690ef69376 100644 --- a/plugins/GCodeProfileReader/__init__.py +++ b/plugins/GCodeProfileReader/__init__.py @@ -13,7 +13,7 @@ def getMetaData(): "author": "Ultimaker", "version": "1.0", "description": catalog.i18nc("@info:whatsthis", "Provides support for importing profiles from g-code files."), - "api": 2 + "api": 3 }, "profile_reader": [ { From 29f7c240c42830cef787102b087c17a55ac1b00f Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Thu, 16 Jun 2016 20:37:32 +0200 Subject: [PATCH 03/12] CURA-1615: Updating SlicingInfoPlugin Just bumped the API version to 3 and added a message that is sent to the log that data was sent to the given URL. As slicing is broken here, because of "Arcus Error (5, native 32): Could not send message size" this also needs testing here. --- plugins/SliceInfoPlugin/SliceInfo.py | 6 +++++- plugins/SliceInfoPlugin/__init__.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/SliceInfoPlugin/SliceInfo.py b/plugins/SliceInfoPlugin/SliceInfo.py index c2e8b9a147..6a9e821b10 100644 --- a/plugins/SliceInfoPlugin/SliceInfo.py +++ b/plugins/SliceInfoPlugin/SliceInfo.py @@ -8,6 +8,7 @@ from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator from UM.Scene.SceneNode import SceneNode from UM.Message import Message from UM.i18n import i18nCatalog +from UM.Logger import Logger import collections import json @@ -25,6 +26,8 @@ catalog = i18nCatalog("cura") # The data is only sent when the user in question gave permission to do so. All data is anonymous and # no model files are being sent (Just a SHA256 hash of the model). class SliceInfo(Extension): + info_url = "https://stats.youmagine.com/curastats/slice" + def __init__(self): super().__init__() Application.getInstance().getOutputDeviceManager().writeStarted.connect(self._onWriteStarted) @@ -114,7 +117,8 @@ class SliceInfo(Extension): # Submit data try: - f = urllib.request.urlopen("https://stats.youmagine.com/curastats/slice", data = binary_data, timeout = 1) + f = urllib.request.urlopen(self.info_url, data = binary_data, timeout = 1) + Logger.log("i", "Sent anonymous slice info to %s", self.info_url) except Exception as e: print("Exception occured", e) diff --git a/plugins/SliceInfoPlugin/__init__.py b/plugins/SliceInfoPlugin/__init__.py index da9111b2e4..f6e77fbf22 100644 --- a/plugins/SliceInfoPlugin/__init__.py +++ b/plugins/SliceInfoPlugin/__init__.py @@ -11,7 +11,7 @@ def getMetaData(): "author": "Ultimaker", "version": "1.0", "description": catalog.i18nc("@info:whatsthis", "Submits anonymous slice info. Can be disabled through preferences."), - "api": 2 + "api": 3 } } From 65f2afeebdd06248715fbb9bf155934bcd3bb102 Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Fri, 17 Jun 2016 11:36:39 +0200 Subject: [PATCH 04/12] Use the same id as set when exporting Nothing special. Doesn't fix anything.. --- plugins/GCodeProfileReader/GCodeProfileReader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/GCodeProfileReader/GCodeProfileReader.py b/plugins/GCodeProfileReader/GCodeProfileReader.py index 4e1604fdc0..a9d59e9d96 100644 --- a/plugins/GCodeProfileReader/GCodeProfileReader.py +++ b/plugins/GCodeProfileReader/GCodeProfileReader.py @@ -72,7 +72,7 @@ class GCodeProfileReader(ProfileReader): Logger.log("i", "Serialized the following from %s: %s" %(file_name, repr(serialized))) # Create an empty profile with the name of the G-code file - profile = InstanceContainer(os.path.basename(os.path.splitext(file_name)[0])) + profile = InstanceContainer("G-code-imported-profile") profile.addMetaDataEntry("type", "quality") try: profile.deserialize(serialized) From 200529e8c93aed813b211d394775fe0907d6d6dd Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Fri, 17 Jun 2016 11:43:23 +0200 Subject: [PATCH 05/12] Removing setReadOnly(False) The profile is writable by default. --- plugins/GCodeProfileReader/GCodeProfileReader.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/GCodeProfileReader/GCodeProfileReader.py b/plugins/GCodeProfileReader/GCodeProfileReader.py index a9d59e9d96..3cff7167b5 100644 --- a/plugins/GCodeProfileReader/GCodeProfileReader.py +++ b/plugins/GCodeProfileReader/GCodeProfileReader.py @@ -76,7 +76,6 @@ class GCodeProfileReader(ProfileReader): profile.addMetaDataEntry("type", "quality") try: profile.deserialize(serialized) - profile.setReadOnly(False) except Exception as e: # Not a valid g-code file. Logger.log("e", "Unable to serialise the profile: %s", str(e)) return None From 95116926388674371feb95ac93e15a4481b642ce Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Fri, 17 Jun 2016 12:10:34 +0200 Subject: [PATCH 06/12] Replace old MachineManager with GlobalContainerStack Also removing unused variable. --- plugins/SliceInfoPlugin/SliceInfo.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugins/SliceInfoPlugin/SliceInfo.py b/plugins/SliceInfoPlugin/SliceInfo.py index 6a9e821b10..a31c25cb06 100644 --- a/plugins/SliceInfoPlugin/SliceInfo.py +++ b/plugins/SliceInfoPlugin/SliceInfo.py @@ -47,7 +47,6 @@ class SliceInfo(Extension): def _onWriteStarted(self, output_device): if not Preferences.getInstance().getValue("info/send_slice_info"): return # Do nothing, user does not want to send data - settings = Application.getInstance().getMachineManager().getWorkingProfile() # Load all machine definitions and put them in machine_settings dict #setting_file_name = Application.getInstance().getActiveMachineInstance().getMachineSettings()._json_file @@ -69,11 +68,11 @@ class SliceInfo(Extension): break - profile_values = settings.getChangedSettings() # TODO: @UnusedVariable + settings = Application.getInstance().getGlobalContainerStack() # Get total material used (in mm^3) print_information = Application.getInstance().getPrintInformation() - material_radius = 0.5 * settings.getSettingValue("material_diameter") + material_radius = 0.5 * settings.getProperty("material_diameter", "value") material_used = math.pi * material_radius * material_radius * print_information.materialAmount #Volume of material used # Get model information (bounding boxes, hashes and transformation matrix) From 4ca247cf3753cedd77919778187bc0e4e43c88e9 Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Fri, 17 Jun 2016 12:41:24 +0200 Subject: [PATCH 07/12] Changing the profile name to something unique. Imported profiles will be now called "Custom profile ()" --- plugins/GCodeProfileReader/GCodeProfileReader.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugins/GCodeProfileReader/GCodeProfileReader.py b/plugins/GCodeProfileReader/GCodeProfileReader.py index 3cff7167b5..8b8809df57 100644 --- a/plugins/GCodeProfileReader/GCodeProfileReader.py +++ b/plugins/GCodeProfileReader/GCodeProfileReader.py @@ -7,6 +7,8 @@ import re #Regular expressions for parsing escape characters in the settings. from UM.Application import Application #To get the machine manager to create the new profile in. from UM.Settings.InstanceContainer import InstanceContainer from UM.Logger import Logger +from UM.i18n import i18nCatalog +catalog = i18nCatalog("cura") from cura.ProfileReader import ProfileReader @@ -71,7 +73,7 @@ class GCodeProfileReader(ProfileReader): serialized = pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], serialized) Logger.log("i", "Serialized the following from %s: %s" %(file_name, repr(serialized))) - # Create an empty profile with the name of the G-code file + # Create an empty profile - the id will be changed later profile = InstanceContainer("G-code-imported-profile") profile.addMetaDataEntry("type", "quality") try: @@ -80,4 +82,9 @@ class GCodeProfileReader(ProfileReader): Logger.log("e", "Unable to serialise the profile: %s", str(e)) return None + #Creating a unique name using the filename of the GCode + new_name = catalog.i18nc("@label", "Custom profile (%s)") %(os.path.splitext(os.path.basename(file_name))[0]) + profile.setName(new_name) + profile._id = new_name + return profile \ No newline at end of file From cbb8eebf7212a24114b49cd45ee81b9123e6620d Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Fri, 17 Jun 2016 12:52:14 +0200 Subject: [PATCH 08/12] Correcting and adding different things to SliceInfo * Replace regular print() with Logger.logException() * Adding log message that reporting is turned off * Rename variable "settings" to "global_container_stack". Should be less misleading. --- plugins/SliceInfoPlugin/SliceInfo.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/SliceInfoPlugin/SliceInfo.py b/plugins/SliceInfoPlugin/SliceInfo.py index a31c25cb06..62a8f422c1 100644 --- a/plugins/SliceInfoPlugin/SliceInfo.py +++ b/plugins/SliceInfoPlugin/SliceInfo.py @@ -46,6 +46,7 @@ class SliceInfo(Extension): def _onWriteStarted(self, output_device): if not Preferences.getInstance().getValue("info/send_slice_info"): + Logger.log("d", "'info/send_slice_info' is turned off.") return # Do nothing, user does not want to send data # Load all machine definitions and put them in machine_settings dict @@ -68,11 +69,11 @@ class SliceInfo(Extension): break - settings = Application.getInstance().getGlobalContainerStack() + global_container_stack = Application.getInstance().getGlobalContainerStack() # Get total material used (in mm^3) print_information = Application.getInstance().getPrintInformation() - material_radius = 0.5 * settings.getProperty("material_diameter", "value") + material_radius = 0.5 * global_container_stack.getProperty("material_diameter", "value") material_used = math.pi * material_radius * material_radius * print_information.materialAmount #Volume of material used # Get model information (bounding boxes, hashes and transformation matrix) @@ -119,6 +120,6 @@ class SliceInfo(Extension): f = urllib.request.urlopen(self.info_url, data = binary_data, timeout = 1) Logger.log("i", "Sent anonymous slice info to %s", self.info_url) except Exception as e: - print("Exception occured", e) + Logger.logException("e", e) f.close() From 8f450d0d2fe71441a796618c5784f3e1d14b2368 Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Sat, 18 Jun 2016 14:05:32 +0200 Subject: [PATCH 09/12] Sending serialized global_settings Containers currently don't have a function to return their content as JSON or dict --- plugins/SliceInfoPlugin/SliceInfo.py | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/plugins/SliceInfoPlugin/SliceInfo.py b/plugins/SliceInfoPlugin/SliceInfo.py index 62a8f422c1..c97e436e06 100644 --- a/plugins/SliceInfoPlugin/SliceInfo.py +++ b/plugins/SliceInfoPlugin/SliceInfo.py @@ -49,26 +49,6 @@ class SliceInfo(Extension): Logger.log("d", "'info/send_slice_info' is turned off.") return # Do nothing, user does not want to send data - # Load all machine definitions and put them in machine_settings dict - #setting_file_name = Application.getInstance().getActiveMachineInstance().getMachineSettings()._json_file - machine_settings = {} - #with open(setting_file_name, "rt", -1, "utf-8") as f: - # data = json.load(f, object_pairs_hook = collections.OrderedDict) - #machine_settings[os.path.basename(setting_file_name)] = copy.deepcopy(data) - active_machine_definition= Application.getInstance().getMachineManager().getActiveMachineInstance().getMachineDefinition() - data = active_machine_definition._json_data - # Loop through inherited json files - setting_file_name = active_machine_definition._path - while True: - if "inherits" in data: - inherited_setting_file_name = os.path.dirname(setting_file_name) + "/" + data["inherits"] - with open(inherited_setting_file_name, "rt", -1, "utf-8") as f: - data = json.load(f, object_pairs_hook = collections.OrderedDict) - machine_settings[os.path.basename(inherited_setting_file_name)] = copy.deepcopy(data) - else: - break - - global_container_stack = Application.getInstance().getGlobalContainerStack() # Get total material used (in mm^3) @@ -102,7 +82,7 @@ class SliceInfo(Extension): "processor": platform.processor(), "machine": platform.machine(), "platform": platform.platform(), - "machine_settings": json.dumps(machine_settings), + "global_settings": global_container_stack.serialize(), "version": Application.getInstance().getVersion(), "modelhash": "None", "printtime": str(print_information.currentPrintTime), From eb6abdf773def2a1282cc0b8584e56ee8101d404 Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Sat, 18 Jun 2016 14:07:14 +0200 Subject: [PATCH 10/12] Move f.close() into try: In case urlopen() fails, e.g. because of a missing internet connection, f will be indefined. --- plugins/SliceInfoPlugin/SliceInfo.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/SliceInfoPlugin/SliceInfo.py b/plugins/SliceInfoPlugin/SliceInfo.py index c97e436e06..7ca9e0241e 100644 --- a/plugins/SliceInfoPlugin/SliceInfo.py +++ b/plugins/SliceInfoPlugin/SliceInfo.py @@ -99,7 +99,6 @@ class SliceInfo(Extension): try: f = urllib.request.urlopen(self.info_url, data = binary_data, timeout = 1) Logger.log("i", "Sent anonymous slice info to %s", self.info_url) + f.close() except Exception as e: Logger.logException("e", e) - - f.close() From 1dc29fb984d3106695e078ffc1736cd31863dfef Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Wed, 22 Jun 2016 14:02:11 +0200 Subject: [PATCH 11/12] SliceInfo: Correctly output the print time --- plugins/SliceInfoPlugin/SliceInfo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/SliceInfoPlugin/SliceInfo.py b/plugins/SliceInfoPlugin/SliceInfo.py index 7ca9e0241e..9af90c02e0 100644 --- a/plugins/SliceInfoPlugin/SliceInfo.py +++ b/plugins/SliceInfoPlugin/SliceInfo.py @@ -85,7 +85,7 @@ class SliceInfo(Extension): "global_settings": global_container_stack.serialize(), "version": Application.getInstance().getVersion(), "modelhash": "None", - "printtime": str(print_information.currentPrintTime), + "printtime": print_information.currentPrintTime.getDisplayString(), "filament": material_used, "language": Preferences.getInstance().getValue("general/language"), "materials_profiles ": {} From 688accfab7d4fff9ab15a57114445d89ae69c230 Mon Sep 17 00:00:00 2001 From: Thomas Karl Pietrowski Date: Wed, 22 Jun 2016 23:25:04 +0200 Subject: [PATCH 12/12] GCodeProfileReader: Removing useless containername --- plugins/GCodeProfileReader/GCodeProfileReader.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/GCodeProfileReader/GCodeProfileReader.py b/plugins/GCodeProfileReader/GCodeProfileReader.py index 8b8809df57..89046bcecf 100644 --- a/plugins/GCodeProfileReader/GCodeProfileReader.py +++ b/plugins/GCodeProfileReader/GCodeProfileReader.py @@ -74,7 +74,7 @@ class GCodeProfileReader(ProfileReader): Logger.log("i", "Serialized the following from %s: %s" %(file_name, repr(serialized))) # Create an empty profile - the id will be changed later - profile = InstanceContainer("G-code-imported-profile") + profile = InstanceContainer("") profile.addMetaDataEntry("type", "quality") try: profile.deserialize(serialized) @@ -87,4 +87,4 @@ class GCodeProfileReader(ProfileReader): profile.setName(new_name) profile._id = new_name - return profile \ No newline at end of file + return profile