def _gcode_append_comment(self, line: Line, comment: str) -> None: """ Add new comment or append to existing comment. """ line.comment = "%s ; %s" % (line.comment, comment)
def processFile(infile, mods, modStartZ): m = Machine() outfile = getOutputFilename(infile, mods) print("Processing {inf} to {outf}".format(inf=infile, outf=outfile)) with open(infile, 'r') as fhIn: fhOut = open(outfile, 'w') lineNumber = 0 # Set all initial positions to 0 curE = prevX = prevY = prevZ = prevE = lastZ = float(0) for line_text in fhIn.readlines(): lineNumber = lineNumber + 1 line = Line(line_text) if (line.comment is not None): debug("{line}: {comment}".format(line=lineNumber, comment=line.comment)) if (line.block.words is not None): hasX = hasY = hasZ = hasE = False origX, origY, origZ, origE = float(0) for word in line.block.words: isMovement = False if word.letter == "G" and word.value == 1: isMovement = True if (word.letter == "G" & word.value == 92): prevE = 0 # Simplify3D resets E to 0 for each layer, so if we see a G92 we should consider the current position to be 0 hasX, origX = wordHasLetter(word, "X") hasY, origY = wordHasLetter(word, "Y") hasZ, origZ = wordHasLetter(word, "Z") hasE, origE = wordHasLetter(word, "E") if hasZ: lastZ = origZ # save last seen Z if isMovement: # start by assuming the new XYZE will be the same as in the file newX, newY, newE = origX, origY, origE if (hasZ): newZ = origZ else: # the line didn't specify Z so we have to use the last one we saw origZ = lastZ newZ = lastZ for mod in mods: adjustRoutine = mod["mod"] extrudeOnly = mod["extrudeOnly"] if (newZ >= modStartZ) and hasX and hasY and ( hasE or not extrudeOnly): newX, newY, newZ, newE = adjustRoutine( mod, modStartZ, m.abs_pos.X, m.abs_pos.Y, m.abs_pos.Z, prevE, newX, newY, newZ, newE) line = replaceOrAddLetter(line, "X", "{:.3f}".format(newX)) line = replaceOrAddLetter(line, "Y", "{:.3f}".format(newY)) line = replaceOrAddLetter(line, "Z", "{:.3f}".format(newZ)) line = replaceOrAddLetter(line, "E", "{:.4f}".format(newE)) #debug("*** X: {X} -> {nX}, Y: {Y} -> {nY}, Z: {Z} -> {nZ}, E: {E} -> {nE}".format(X = origX, Y=origY, Z= origZ, E=origE, nX = newX, nY = newY, nZ = newZ, nE = newE)) if (origZ != newZ) or (not hasZ): line.comment = "; z {oz} to {nz}".format(oz=origZ, nz=newZ) prevX, prevY, prevZ, prevE = newX, newY, newZ, newE try: m.process_block(line.block) except MachineInvalidState: #debug("here") continue finally: print(str(line), file=fhOut)