Is there a way to automate editing of gcode files? March 29, 2016 05:10AM |
Registered: 8 years ago Posts: 622 |
Re: Is there a way to automate editing of gcode files? March 29, 2016 03:12PM |
Registered: 10 years ago Posts: 21 |
Re: Is there a way to automate editing of gcode files? March 29, 2016 04:19PM |
Registered: 11 years ago Posts: 14,685 |
Re: Is there a way to automate editing of gcode files? March 29, 2016 05:18PM |
Registered: 10 years ago Posts: 978 |
G0 X56.102 Y61.759 Z10.100
G0 X56.102 Y61.759 Z10.100 M567 P4 E1
#!/usr/bin/perl -i use strict; use warnings; # Declare some local variables my $maxZ = 100; # The height of the object being printed # Ramp colour A from 100% down to 0 over the height of the object # Ramp colour B from 0 to 75% over the height of the object # When A+B < 1, colour C is used to "top up" to 1. This means that colour C starts appearing at 67mm height, and ramps up to 25% at the maxZ my $startA = 1; # Proportion of colour A at the first layer my $endA = 0; # Proportion of colour A at maxZ my $startB = 0; my $endB = 0.75; # Go through the input file, line by line while ( <> ) { # Print the existing line to the output print; if (my $match = m/Z(\d+\.?\d*)/) { # If the line contains a Z followed by a number, then there's a height change, so insert a colour change M567 command # The number is extracted and stored in the Perl variable $1 # Calculate the colour values # This is a simple linear interpolation so that colours A & B ramp from their start to end values from 0 to the height of the object ($maxZ) my $colourA = $startA + ( $endA-$startA )*$1/$maxZ; my $colourB = $startB + ( $endB-$startB )*$1/$maxZ; my $totalColour = $colourA + $colourB ; my $colourC = 0; # Ensure that the total of the colours is always equal to 1 if ($totalColour >= 1) { $colourA /= $totalColour; $colourB /= $totalColour; } else { $colourC = 1 - ( $colourA + $colourB ); } # Output the M567 line. Each colour proportion is output with 2 decimal places printf "M567 P4 E%4.2f:%4.2f:%4.2f\n", $colourA, $colourB, $colourC; } }
Re: Is there a way to automate editing of gcode files? March 30, 2016 05:03AM |
Registered: 8 years ago Posts: 622 |
Re: Is there a way to automate editing of gcode files? March 30, 2016 05:07PM |
Registered: 10 years ago Posts: 978 |
Re: Is there a way to automate editing of gcode files? March 31, 2016 04:49AM |
Registered: 8 years ago Posts: 622 |
Re: Is there a way to automate editing of gcode files? March 31, 2016 07:17PM |
Registered: 10 years ago Posts: 978 |
python35 [script].py [data].gcodeReplace [script] by the script filename, replace [data] by the name of your sample gcode file. If you're on Windows, the installer installs Python 3.5.1 with the executable named "python", not "python35".
#!/usr/bin/python import sys import re import os # Declare some variables maxZ = 100 # The height of the object being printed # Ramp colour A from 100% down to 0 over the height of the object # Ramp colour B from 0 to 75% over the height of the object # When A+B < 1, colour C is used to "top up" to 1. This means that colour C starts appearing at 67mm height, and ramps up to 25% at the maxZ startA = 1 # Proportion of colour A at the first layer endA = 0 # Proportion of colour A at maxZ startB = 0 endB = 0.75 # Write metadata to a log file log = open(sys.argv[1]+".log","w") log.write(str(len(sys.argv)) + ' arguments.\n') for v in sys.argv : log.write(v + "\n") for v in os.environ : log.write(v + ": " + os.getenv(v) + "\n") infile = open(sys.argv[1]) outfile = open(sys.argv[1]+".postproc","w") # Read the input file for line in infile : # Print the existing line to the output, without adding another newline outfile.write(line) match = re.search(r"Z(\d+\.?\d*)", line) if match : # If the line contains a Z followed by a number, then there's a height change, so insert a colour change M567 command # The number extracted is stored in match.group(1) height = float(match.group(1)) # Calculate the colour values # Calculate proportions of colours A & B based on their linear ramps # This is a simple linear interpolation so that colours A & B ramp from their start to end values from 0 to the height of the object ($maxZ) colourA = startA + ( endA-startA )*height/maxZ colourB = startB + ( endB-startB )*height/maxZ totalColour = colourA + colourB # Ensure that the total of the colours is always equal to 1 colourC = 0 if totalColour >= 1 : colourA /= totalColour colourB /= totalColour else : colourC = 1 - ( colourA + colourB ) # Output the M567 line. Each colour proportion is output with 2 decimal places outfile.write("M567 P4 E{0:.2f}:{1:.2f}:{2:.2f}\n".format(colourA, colourB, colourC)) # Close the files so that the OS can do things with them outfile.close() infile.close() # Delete the original input file os.remove(sys.argv[1]) # Rename the output file to be what the input file was called os.rename(sys.argv[1]+".postproc", sys.argv[1])
Re: Is there a way to automate editing of gcode files? April 01, 2016 07:04AM |
Registered: 8 years ago Posts: 622 |
startfile = open("D:\RepRap\G Code Files Diamond hot end\Python test file.gcode",'r') endfile = open("D:\RepRap\G Code Files Diamond hot end\Python output.txt", 'w') FilA=1.00 FilB=0.00 FilC=0.00 Iteration =1 LayerChange="G1 Z" numLines=sum(1 for line in (startfile)) startfile.seek(0) # make sure we start at the beginning of the file # use the following to limit part of file?? Check the file length (number of lines, and set x to this at start for x in range (0,numLines): #for line in startfile: # if I use the above, it misses every other line but for x in range works ok. LineText=(startfile.readline()) # uncomment the following line with caution - it really slows things down. # print (LineText) endfile.write(LineText) if LayerChange in LineText: TextToWrite =("M567 P3 E"+'{0:.2f}'.format(FilA)+":"+'{0:.2f}'.format(Fil+":"+'{0:.2f}'.format(FilC)+"\n") endfile.write (TextToWrite) print (TextToWrite) if Iteration == 1: FilA=FilA-0.01 FilB=FilB+0.01 FilC=0.00 if FilA<0: FilA=0.00 FilB=1.00 Iteration =2 if Iteration==2: FilA=0.00 FilB=FilB-0.01 FilC=FilC+0.01 if FilB<0: FilB=0.00 FilC=1.00 Iteration=3 if Iteration==3: FilC=FilC-0.01 FilA=FilA+0.01 FilB=0.00 if FilC<0: FilC=0.00 FilA=1.00 Iteration =1 startfile.close() endfile.close()
Re: Is there a way to automate editing of gcode files? April 01, 2016 07:07AM |
Registered: 8 years ago Posts: 622 |
Re: Is there a way to automate editing of gcode files? April 01, 2016 03:56PM |
Registered: 10 years ago Posts: 978 |
import os.path endname = "D:\RepRap\G Code Files Diamond hot end\Python output.txt" if os.path.isfile(endname) : print("File " + endname + " already exists" quit() endfile = open(endname, 'w')
for line in startfile: LineText=(startfile.readline())
for LineText in startfile:
Re: Is there a way to automate editing of gcode files? April 01, 2016 05:23PM |
Registered: 8 years ago Posts: 622 |
Re: Is there a way to automate editing of gcode files? April 14, 2016 10:42PM |
Registered: 8 years ago Posts: 37 |
Re: Is there a way to automate editing of gcode files? April 15, 2016 01:30PM |
Registered: 8 years ago Posts: 622 |
Re: Is there a way to automate editing of gcode files? July 20, 2016 07:38PM |
Registered: 14 years ago Posts: 198 |