Previewing G-Code using OpenSCAD

Unfortunately, this is not directly possible, though there are a number of tools which examine this space:

The problem is OpenSCAD does not have the facility for processing text, so can’t directly use G-Code as is, so it will be necessary to have two outputs in parallel:

  • G-Code
  • OpenSCAD code where each command matches a G-Code command

This will require a tool which exports the two different files, or a tool which converts G-Code into this OpenSCAD format.

The OpenSCAD end will be made up of:

  • code for previewing Endmills
  • code for making each sort of movement:
    • straight-line moves
    • arcs
1 Like

Start with endmill previews/simulations:

gcp endmill square.zip (976 Bytes)

gcode to openscad looks entirely possible as relatively simple tool…
… but would people use it over, say, camotics?

Ball endmills require a bit of translation:

gcp endmill ball.zip (1.3 KB)

Because Camotics can’t preview some endmill geometries.

I guess one would model the stock and then basically subtract each toolpath from it, so that you see what’s left afterwards. Assuming openscad does subtraction well enough

Yes, that’s pretty much it — we’ll have to see how things sort out.

Vee endmills are pretty straight-forward:

gcp endmill v.zip (1.1 KB)

hmm maybe but wouldn’t you want it not a pure V but more the path of a V… so more or less
2 V cylinders (begin and end) and then some triangle-bar shape between them?

It’s of course possible to just do a lot of these little cones in small steps from eachother to emulate a path… it is not always a bad idea to do things that way.

OpenSCAD has a Hull operation which allows combining two instances of an endmill for most sorts — that rather naturally brings us to the next sort of endmill, one which Camotics does not handle, cove radius endmills — it’s not workable to merely hull two instances, they have to be handled as a series of stacked angled cylinders.

So, we need to define a module which includes the coordinates for the beginning and end of the cut, as well as an input for how detailed it will be modeled as, in addition to the actual characteristics of the endmill itself.

Worked up a module for the radius cut in OpenSCAD previously, so no real need to do it in BlockSCAD:

//!OpenSCAD

Endmill_Diameter = 6.35;

Detail = 100;

$fn = Detail * 1;

tool_radius_tip = 0.79375;
tool_radius_width = 3.175;

feedrate = 600;
plungerate = 150;
safeheight = 8;

module radiuscut(bx, by, bz, ex, ey, ez) {
n = Detail;
step = 360/n;
endmillradius = Endmill_Diameter / 2;

hull(){
translate([bx,by,bz])
cylinder(step,tool_radius_tip,tool_radius_tip);
translate([ex,ey,ez])
cylinder(step,tool_radius_tip,tool_radius_tip);
}

hull(){
translate([bx,by,bz+tool_radius_width])

cylinder(tool_radius_width*2,tool_radius_tip+tool_radius_width,tool_radius_tip+tool_radius_width);

translate([ex,ey,ez+tool_radius_width])

cylinder(tool_radius_width*2,tool_radius_tip+tool_radius_width,tool_radius_tip+tool_radius_wi dth);
}

for (i=[0:step:90]) {
    angle = i;
    dx = tool_radius_width*cos(angle);
    dxx = tool_radius_width*cos(angle+step);
    dzz = tool_radius_width*sin(angle);
    dz = tool_radius_width*sin(angle+step);
    dh = dz-dzz;
    hull(){
    translate([bx,by,bz+dz])
       cylinder(dh,tool_radius_tip+tool_radius_width-dx,tool_radius_tip+tool_radius_width-dxx);
    translate([ex,ey,ez+dz])
       cylinder(dh,tool_radius_tip+tool_radius_width-dx,tool_radius_tip+tool_radius_width-dxx);
        }
}
}

radiuscut(0, 0, 0, 20, 20, 0);

We put the commands for cutting into a .scad file and import it as a library and we can then have a LiveCode (or other) program which writes out the two sorts of files:

We then configure OpenSCAD for an external file and open the .scad file in it:

It’s then just a matter of creating the code to create the codes:

if you want any help converting the gcode into something else let me know be happy to help

(I have a pretty complete gcode parsing code base at this point, including one that deals with G2/G3 arcs correctly)

Neat! Thanks!

I’m hoping to just code the OpenSCAD end of things so that it’s orthogonal to G-Code — that’s not quite doable, since G-Code implements a given command based on current machine state, but I will hopefully manage something reasonably close.

the gcode parsing I normally do ends up converting gcode into a series of do_something(X1,Y1,Z1,X2,Y2,Z2) which seems to match the building blocks you’ve been building.
(even G2/G3 gets made into a series of small linear segments)

Some gcode (like Carbide Create) can also tell you the size of the stock and some properties of the tool (like diameter and ball radius)

1 Like

Yeah, I’ll be writing out suitable comments and so forth, once I get that far along w/ things.

Next up is encapsulating the G-code previews so that they will cut (will have to separate out the radius cut module since it’s not in BlockSCAD), which turns out to be quite simple:

but obviously it needs a module to select the correct module and parameters based on the tool # — if OpenSCAD had a facility for processing text we could import a .csv, but in the absence of that, we brute force it.

a tool that reads gcode and outputs this, can also do the CSV thing on that level

1 Like

Agreed… something like Convert to SVG [cnc-apps.com] ?

Once I’ve got the OpenSCAD end of things finalized, I’ll put it up on Github and anyone who find it of interest/use can make use of it — I’d love to see a tool which would accept G-Code and output a .scad file for this.

I’ll be happy to contribute that part of things…

do you have a preferred language, like javascript or python?
(this sort of thing is really not where C/C++ and co shine)