Read and render a gcode file
-
Hi guys!
I need read and render a gcode file.
the gcode file looks like:; generated by Slic3r 1.1.7 on 2015-06-13 at 16:29:20
; perimeters extrusion width = 0.50mm
; infill extrusion width = 0.52mm
; solid infill extrusion width = 0.52mm
; top infill extrusion width = 0.52mmG21 ; set units to millimeters
M107
M104 S205 ; set temperature
G28 ; home all axes
G1 Z5 F5000 ; lift nozzleM109 S205 ; wait for temperature to be reached
G90 ; use absolute coordinates
G92 E0
M82 ; use absolute distances for extrusion
G1 F1800.000 E-1.00000
G92 E0
G1 Z0.500 F7800.000
G1 X68.325 Y68.049 F7800.000
G1 E1.00000 F1800.000
G1 X70.211 Y66.283 E1.16316 F1080.000
G1 X72.173 Y64.643 E1.32463
G1 X74.270 Y63.088 E1.48952
G1 X76.303 Y61.747 E1.64326
G1 X78.457 Y60.493 E1.80071
G1 X80.665 Y59.367 E1.95715
G1 X82.779 Y58.426 E2.10329
G1 X84.233 Y57.852 E2.20197
G1 X85.695 Y57.335 E2.29996
G1 X86.455 Y57.089 E2.35037
G1 X87.862 Y56.671 E2.44303
G1 X88.759 Y56.429 E2.50174
G1 X89.675 Y56.203 E2.56129So, i need read this points and render. But i dont know nothing about graphics. Any tips? How to start?
thanks! -
Hi, I guess you already googled it, but due to your post I was curious about gcode and found some open source viewer here:
http://gcode.ws
http://jherrm.github.com/gcode-viewer
Maybe the links can give you some insights. -
In some ways executing gcode is similar to the old "turtle graphics" from some time ago.
in turtle graphics you imagined you had a turtle that you could tell to move to various places. Generally turtle graphics was 2D so you gave it X/Y coordinates. As the turtle moved it left a colored line behind it of the size and color you specify.
You could tell the turtle to move without leaving the line to get to another starting point.
gcode is much the same. The initial "G" values are instructions. There are definitions all over the web on what the various instructions mean.
To write code to parse a gcode file is not trivial but can be a fun learning process. You are going to need a few things:
-
Code that can read in the text file. So learn file IO in c++. Qt has some routines to help.
-
You are going to need something that can look at a line read from your gcode file and decide what must be done. Generally there are several parts to this code. First is something that can break the line into tokens. In gcode things are space separated so you could for your first pass use the "split" functionality of QString. Be aware that you probably want to delete comments so anything from a ; on is a comment. So read in a line, remove comments, perform a split. Now you have tokens in your split output. You evaluate each one.
For example the line:
G1 X84.233 Y57.852 E2.20197
When split on spaces would end up with tokens:
G1
X84.233
Y57.852
E2.20197So the first token tells you what to do but you can't really do it until you evaluate the remaining tokens. The X is where to move to in X, Y is for Y position etc. Once you have extracted the data you are ready to execute the G1 command for this line.
You execute it (in your case painting a line perhaps on a display) then move on to the next line,
Hope this helps you get started.
-