How to Simulate Gcode

Hello, is there any way to take Gcode and simulate the toolpath of a machine within grasshopper (rhino 7)

Yes, but it’s probably easier to do this with some script within Grasshopper rather than with native Gh components. You could use the Anenome plugin to cycle through each line of g-code, determine if it a straight line move (G0 or G1) and draw a line from the current point to the new coordinates. You could colour the line based on whether its a rapid traverse or at the feedrate if you keep track of the feedrate. The other challenge is that G-Code doesn’t always give you X, Y and Z coordinates for example…

G1 F2000 X10 Y9.1 Z2.25
means move to (10,9.1,2.25) at 2000 units per minute.

but…

G1 Y10.2 means move only in Y to Y=10.2 at the current feedrate.

so… you have to keep track of things to process each line of G-Code because each line on it’s own does not give you all the information.

Then you have arcs too…
G3 X125.1213 Y758.9213 I-3.0 J0.0

Anenome could do this because you can pass feedrate and X,Y,Z coordinates back to the start of the loop to process the next line of G-Code but I have a feeling a script might be better for this.

Attached is a simple GH file that reads a G-Code text file and splits it into the header, main g-code and footer.

I will leave it up to someone else on here to figure out the best way of turning the g-code into colour coded lines and arcs.

Be aware too that not all g-code is the same… it can vary slightly from one machine to the next (different post processors) and if its for a 3d printer for example, there are other g-codes for moving the extruder axis etc.
G_CODE_SIM.gh (16.7 KB)

You can copy the text below into a text file if you want some example g-code and you will have to point the Path component at that file… I can’t upload .txt files here…

( Made using CamBam - http://www.cambam.co.uk )
( HINGE_DRILL_JIG 3/9/2021 1:27:33 PM )
( Post processor: Mach3 )
( T0 : 6.0 )
G21 G90 G64 G40
G91.1
G0 Z10.0
( T0 : 6.0 )
T0 M6
( Profile1 )
G17
M3 S24000
G0 X126.0 Y23.0
G0 Z1.0
G1 F300.0 Z-6.0
G1 F2500.0 Y756.8
G3 X125.1213 Y758.9213 I-3.0 J0.0
G1 X115.1213 Y768.9213
G3 X113.0 Y769.8 I-2.1213 J-2.1213
G1 X23.0
G3 X20.0 Y766.8 I0.0 J-3.0
G1 Y23.0
G3 X23.0 Y20.0 I3.0 J0.0
G1 X123.0
G3 X126.0 Y23.0 I0.0 J3.0
G1 F300.0 Z-12.0
G1 F2500.0 Y756.8
G3 X125.1213 Y758.9213 I-3.0 J0.0
G1 X115.1213 Y768.9213
G3 X113.0 Y769.8 I-2.1213 J-2.1213
G1 X23.0
G3 X20.0 Y766.8 I0.0 J-3.0
G1 Y23.0
G3 X23.0 Y20.0 I3.0 J0.0
G1 X123.0
G3 X126.0 Y23.0 I0.0 J3.0
G1 F300.0 Z-18.0
G1 F2500.0 Y226.2178
G1 Y230.2178 Z-14.0
G1 Y236.2178
G1 Y240.2178 Z-18.0
G1 Y646.6533
G1 Y650.6533 Z-14.0
G1 Y656.6533
G1 Y660.6533 Z-18.0
G1 Y756.8
G3 X125.1213 Y758.9213 I-3.0 J0.0
G1 X115.1213 Y768.9213
G3 X113.0 Y769.8 I-2.1213 J-2.1213
G1 X23.0
G3 X20.0 Y766.8 I0.0 J-3.0
G1 Y560.6533
G1 Y556.6533 Z-14.0
G1 Y550.6533
G1 Y546.6533 Z-18.0
G1 Y140.2178
G1 Y136.2178 Z-14.0
G1 Y130.2178
G1 Y126.2178 Z-18.0
G1 Y23.0
G3 X23.0 Y20.0 I3.0 J0.0
G1 X123.0
G3 X126.0 Y23.0 I0.0 J3.0
G0 Z10.0
M5
M30

1 Like

How would you go about doing the gcode simulation using the Anenome plugin? I have been attempting to get it to work, but I’m not sure where to begin.

Have you seen this?

yes but I am getting an error message at the first solid union, and my panels are displaying 0 rather then any number.

Are you referring to my file 20_12_04_worm.gh?

yes I am

OK

So this is for the first of the two Anemone loops: set the toggle on the Loop End component to False and then click the Button on the LLoop start to reset and start the loop.

This is how the result should look like:

This worm is also internalised in the definition, so is the intersection curve input for the second loop

This is what happens in the second loop: