From 47b3f51a3faedecf16c56efdc9e601cadfa40a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 30 Oct 2019 18:10:52 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20Speed=20up=20the=20analyser=20a=20b?= =?UTF-8?q?it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #3318 Thanks at @jmfrank63 for the heads-up! --- src/octoprint/util/gcodeInterpreter.py | 71 ++++++++++++++++---------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/src/octoprint/util/gcodeInterpreter.py b/src/octoprint/util/gcodeInterpreter.py index 8d7834c4b..be0b2cfe2 100644 --- a/src/octoprint/util/gcodeInterpreter.py +++ b/src/octoprint/util/gcodeInterpreter.py @@ -62,36 +62,55 @@ class Vector3D(object): return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z) def __add__(self, other): - if isinstance(other, Vector3D): + try: + if len(other) == 3: + return Vector3D(self.x + other[0], + self.y + other[1], + self.z + other[2]) + except TypeError: + # doesn't look like a 3-tuple + pass + + try: return Vector3D(self.x + other.x, self.y + other.y, self.z + other.z) - elif isinstance(other, (tuple, list)) and len(other) == 3: - return Vector3D(self.x + other[0], - self.y + other[1], - self.z + other[2]) - else: - raise ValueError("other must be a Vector3D instance or a list or tuple of length 3") + except AttributeError: + # also doesn't look like a Vector3D + pass + + raise TypeError("other must be a Vector3D instance or a list or tuple of length 3") def __sub__(self, other): - if isinstance(other, Vector3D): + try: + if len(other) == 3: + return Vector3D(self.x - other[0], + self.y - other[1], + self.z - other[2]) + except TypeError: + # doesn't look like a 3-tuple + pass + + try: return Vector3D(self.x - other.x, self.y - other.y, self.z - other.z) - elif isinstance(other, (tuple, list)) and len(other) == 3: - return Vector3D(self.x - other[0], - self.y - other[1], - self.z - other[2]) - else: - raise ValueError("other must be a Vector3D instance or a list or tuple") + except AttributeError: + # also doesn't look like a Vector3D + pass + + raise TypeError("other must be a Vector3D instance or a list or tuple of length 3") def __mul__(self, other): - if isinstance(other, (int, float)): + try: return Vector3D(self.x * other, self.y * other, self.z * other) - else: - raise ValueError("other must be a float or int value") + except TypeError: + # doesn't look like a scalar + pass + + raise ValueError("other must be a float or int value") def __rmul__(self, other): return self.__mul__(other) @@ -342,11 +361,11 @@ class gcode(object): totalExtrusion[currentExtruder]) if currentExtruder == 0 and len(currentE) > 1 and duplicationMode: - # Copy first extruder length to other extruders + # Copy first extruder length to other extruders for i in range(1, len(currentE)): - totalExtrusion[i] += e - currentE[i] += e - maxExtrusion[i] = max(maxExtrusion[i], + totalExtrusion[i] += e + currentE[i] += e + maxExtrusion[i] = max(maxExtrusion[i], totalExtrusion[i]) else: e = 0.0 @@ -436,13 +455,13 @@ class gcode(object): else: fwrecoverTime = (fwretractDist + s) / f elif M == 605: #Duplication/Mirroring mode - s = getCodeInt(line, 'S') - if s in [2, 4, 5, 6]: - # Duplication / Mirroring mode selected. Printer firmware copies extrusion commands + s = getCodeInt(line, 'S') + if s in [2, 4, 5, 6]: + # Duplication / Mirroring mode selected. Printer firmware copies extrusion commands # from first extruder to all other extruders duplicationMode = True - else: - duplicationMode = False + else: + duplicationMode = False elif T is not None: if T > max_extruders: