Fixed double ";" when using putValue on a line with a comment

This commit is contained in:
Louis Wouters 2020-06-19 11:38:23 +02:00
parent ea0c8ff9bc
commit 3b4833a7e6

View file

@ -141,52 +141,45 @@ class Script:
All other keyword parameters are put in the result in g-code's format. All other keyword parameters are put in the result in g-code's format.
For instance, if you put ``G=1`` in the parameters, it will output For instance, if you put ``G=1`` in the parameters, it will output
``G1``. If you put ``G=1, X=100`` in the parameters, it will output ``G1``. If you put ``G=1, X=100`` in the parameters, it will output
``G1 X100``. The parameters G and M will always be put first. The ``G1 X100``. The parameters will be added in order G M T S F X Y Z E.
parameters T and S will be put second (or first if there is no G or M). Any other parameters will be added in arbitrary order.
The rest of the parameters will be put in arbitrary order.
:param line: The original g-code line that must be modified. If not :param line: The original g-code line that must be modified. If not
provided, an entirely new g-code line will be produced. provided, an entirely new g-code line will be produced.
:return: A line of g-code with the desired parameters filled in. :return: A line of g-code with the desired parameters filled in.
""" """
# Strip the comment.
#Strip the comment.
comment = ""
if ";" in line: if ";" in line:
comment = line[line.find(";"):] comment = line[line.find(";"):]
line = line[:line.find(";")] #Strip the comment. line = line[:line.find(";")]
else:
comment = ""
#Parse the original g-code line. # Parse the original g-code line and add them to kwargs.
for part in line.split(" "): for part in line.split(" "):
if part == "": if part == "":
continue continue
parameter = part[0] parameter = part[0]
if parameter in kwargs: if parameter not in kwargs:
continue #Skip this one. The user-provided parameter overwrites the one in the line.
value = part[1:] value = part[1:]
kwargs[parameter] = value kwargs[parameter] = value
#Write the new g-code line. # Start writing the new g-code line.
result = "" line_parts = list()
priority_parameters = ["G", "M", "T", "S", "F", "X", "Y", "Z", "E"] #First some parameters that get priority. In order of priority! # First add these parameters in order
for priority_key in priority_parameters: for parameter in ["G", "M", "T", "S", "F", "X", "Y", "Z", "E"]:
if priority_key in kwargs: if parameter in kwargs:
if result != "": line_parts.append(parameter + str(kwargs.pop(parameter)))
result += " " # Then add the rest of the parameters
result += priority_key + str(kwargs[priority_key]) for parameter, value in kwargs.items():
del kwargs[priority_key] line_parts.append(parameter + str(value))
for key, value in kwargs.items():
if result != "":
result += " "
result += key + str(value)
#Put the comment back in. # If there was a comment, put it at the end.
if comment != "": if comment != "":
if result != "": line_parts.append(comment)
result += " "
result += ";" + comment
return result # Add spaces and return the new line
return " ".join(line_parts)
def execute(self, data: List[str]) -> List[str]: def execute(self, data: List[str]) -> List[str]:
"""This is called when the script is executed. """This is called when the script is executed.