# Did anybody develop a G-code (RS-274) Import Format?

**URL:** <https://community.wolfram.com/t/did-anybody-develop-a-g-code-rs-274-import-format/11717>\
**Category:** Questions\
**Tags:** data-science, engineering, mathematica, curated-data, geometry, graphics-and-visualization, import-and-export, modeling, packages-and-paclets\
**Created:** [December 22, 2017, 3:41pm UTC](https://community.wolfram.com/t/did-anybody-develop-a-g-code-rs-274-import-format/11717 "2017-12-22T15:41:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![markussondereggerboss](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@markussondereggerboss](https://community.wolfram.com/u/markussondereggerboss)\
**Post date:** [December 22, 2017, 3:41pm UTC](https://community.wolfram.com/t/did-anybody-develop-a-g-code-rs-274-import-format/11717/1 "2017-12-22T15:41:17Z")

</div>

For a student project, we plan to create out of the G-code a Geometric Region, which models the path the tool (of the mill, lathe) has executed in 3D space.  
It could be e.g. a List of Lines or a List of Hexahedrons (if we look at what was milled away by the tool).

The final goal is to Import the workpiece modeled in its CAM Software in STL Format into Mathematica and then try to map each workpiece mesh to its corresponding G-Code(s) statements which milled it.

Any thoughts are much appreciated.  
Regards  
Markus

---

<div class="post-metadata">

**Author:** ![carlson](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/carlson/32/6475_2.png) [@carlson](https://community.wolfram.com/u/carlson)\
**Post date:** [December 22, 2017, 4:11pm UTC](https://community.wolfram.com/t/did-anybody-develop-a-g-code-rs-274-import-format/11717/2 "2017-12-22T16:11:50Z")

</div>

Sorry, no advice to give. But a very interesting project!

---

<div class="post-metadata">

**Author:** ![christopherpurcell](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/christopherpurcell/32/1616_2.png) [@christopherpurcell](https://community.wolfram.com/u/christopherpurcell)\
**Post date:** [January 7, 2018, 5:54pm UTC](https://community.wolfram.com/t/did-anybody-develop-a-g-code-rs-274-import-format/11717/3 "2018-01-07T17:54:01Z")

</div>

[G-Code](http://www.linuxcnc.org/docs/html/gcode.html) contains no information about the part dimensions, and very little about the shape of the cutting tool. Essentially it just refers to a tool table where it retrieves the tool radius and axial offset (for mills). For end mills and ball nose cutters this is enough, but for more complicated tools, their shapes must be stored somewhere else. A bigger challenge is that G-Code is a full programming language, so you can’t just translate arbitrary G-Code into _Mathematica_ **Graphics3D** commands. You would in the general case have to write a G-Code interpreter that would run the code. Since G-Code can invoke arbitrary code (like unix scripts) handling all that could be tricky. So a full G-Code interpreter would be a challenging student project - maybe requiring a small team. If the G-Code contains only G0 or G1 moves, and no fancy stuff, then a simple parse of the G-Code will extract the tool path, and then you would be on your way to getting an STL file. Here is a trivial example, probably of how NOT to do this, since the result is rather rough.

Here an end mill makes a single pass through a rectangular part. The tool path is generated here, not read from a G-Code file. This example shows that **RegionPlot3D** is a possible starting point for building the STL file. Here we are, in effect doing “plunge milling”, so the cut surface is rough. I think this overall approach is called CSG (constructive solid geometry).

```
Clear[part, path, tool, swarf];

n = 100; (* the number of plot points used by RegionPlot3D in all 3 \
directions *)
stepsize = .01; (* The tool moves in a finite # of \
steps, and this is the distance per step *)

part[{a_, b_, c_}, {c1_, c2_, 
   c3_}] := ((0 < #1 - c1 < a) && (0 < #2 - c2 < b) && (0 < #3 - c3 < 
      c)) &; (* the geometry of the part, a brick of sides {a,b,c} \
centered at {c1,c2,c3} *)  

path = Table[{x, x, 0.1}, {x, 0, 1, 
   stepsize}]; (* the path of the center of the bottom of the tool *)
\

tool[{radius_, length_}, {c1_, c2_, 
   c3_}] := ((0 < (#1 - c1)^2 + (#2 - c2)^2 < 
      radius^2) && (0 < #3 - c3 < length)) &;
(* The cutting tool is an end mill - a cylinder aligned with the z \
axis, the center is located at {c1,c2,c3}. *)

swarf = Not[
      tool[{.05, .5}, #][u, v, w] ] &[First[path]]; 
len = Length[path];
i = 0;
While[i <= len - 1, i++; swarf = swarf && Not[
       tool[{.05, .5}, #][u, v, w] ] & [
   path[[i]]]]; (* the region removed by the tool *)

Show[Graphics3D[{Thickness[.01], Blue, Line[path]}], RegionPlot3D[ 
  tool[{.05, .5}, {0, 0, .1}][u, v, w] 
   ||
    part[{.5, .5, .2}, {.2, .2, 0}][u, v, w], {u, -.2, 
   1}, {v, -.2, 1},
  {w, 0, 1}, PlotPoints -> {n, n, n},
  Mesh -> None, 
  BoxRatios -> 
   Automatic] ](* show the tool, the part and tool path *)

img = RegionPlot3D[ 
   part[{.5, .5, .2}, {.2, .2, 0}][u, v, w]
   &&
   swarf , {u, -.2, 1}, {v, -.2, 1},
  {w, 0, 1}, PlotPoints -> {n, n, n},
  Mesh -> None, Boxed -> False, Axes -> False, 
  BoxRatios -> 
   Automatic]; (* produce the graphic of the milled part *)

Export["test.STL", img];
Import["test.STL"] (* show the STL file of the milled part *)

```

 ![setup.jpeg](https://global.discourse-cdn.com/wolfram/original/3X/5/9/59a4533dfd3ae368c16267d5633cdb23b6439e69.jpeg)

Fig 1 shows a cylinder representing the end mill, the uncut part, and the blue line is the tool path.

 ![final.jpg](https://global.discourse-cdn.com/wolfram/original/2X/e/ef007df7d04595b8ca95501d9b50595e31fa6e61.jpeg)

Fig 2 shows we can use **Export** to convert the Graphics of the milled part from **RegionPlot3D** into STL.

The result can be improved by increasing the **PlotPoints** option in **RegionPlot3D** , but fundamentally this approach is poor since we are not modelling a continuous cut. A possible next level of refinement might be to wrap something like **Tube** around the path and intersect that with the part, where the **Tube** would be tweaked to represent the tool cross section.
