Skip to content

Commit 30fc92f

Browse files
committed
Add readme and demos
1 parent 28eba16 commit 30fc92f

6 files changed

Lines changed: 220 additions & 0 deletions

File tree

orx-g-code/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# orx-g-code
2+
3+
Utilities for generating *g-code* from compositions.
4+
5+
>*Make sure to verify compatibility to your hardware and that the
6+
commands do not exceed the machines limits before running any output of this.*
7+
8+
## Generator
9+
10+
A [Generator](src/commonMain/kotlin/Generator.kt) generates g-code for the following operations:
11+
- `setup`: Setup code at the beginning of a file.
12+
- `moveTo`: A move operation to the given location.
13+
- `preDraw`: Start drawing sequence. Pen down, laser on, etc.
14+
- `drawTo`: A draw operation to the given location.
15+
- `postDraw`: End draw sequence. Lift pen, turn laser off, etc.
16+
- `end`: End of file sequence.
17+
- `comment`: Insert a comment.
18+
19+
These are used by the `toCommands()` extensions in [shape.kt](src/commonMain/kotlin/shape.kt) to convert a composition
20+
to a set of Commands.
21+
22+
It only supports absolute moves, so `G90 absolute positioning` should be included in the setup.
23+
24+
`basicGrblSetup` defines an example that outputs g-code, which *should be*
25+
compatible with [grbl v1.1](https://github.com/grbl/grbl).
26+
27+
See [DemoGcodeGenerator.kt](src/demo/kotlin/DemoGcodeGenerator.kt) for an example of how to use.
28+
29+
## Plot (jvm only)
30+
31+
The [Plot Extension](src/jvmMain/kotlin/Plot.kt) provides a quick setup for drawing, rendering and exporting files for
32+
a pen plotter.
33+
See [DemoSimplePlot.kt](src/demo/kotlin/DemoSimplePlot.kt) for an example of how to use.
34+
35+
36+
<!-- __demos__ -->
37+
## Demos
38+
### DemoInteractivePlot
39+
[source code](src/demo/kotlin/DemoInteractivePlot.kt)
40+
41+
![DemoInteractivePlotKt](https://raw.githubusercontent.com/openrndr/orx/media/orx-g-code/images/DemoInteractivePlotKt.png)
42+
43+
### DemoSimplePlot
44+
[source code](src/demo/kotlin/DemoSimplePlot.kt)
45+
46+
![DemoSimplePlotKt](https://raw.githubusercontent.com/openrndr/orx/media/orx-g-code/images/DemoSimplePlotKt.png)
128 KB
Loading
249 KB
Loading
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import org.openrndr.application
2+
import org.openrndr.drawComposition
3+
import org.openrndr.extra.gcode.asCommands
4+
import org.openrndr.extra.gcode.basicGrblSetup
5+
import org.openrndr.extra.gcode.extensions.toCommands
6+
import org.openrndr.extra.gcode.toGcode
7+
import org.openrndr.extra.gcode.withoutDuplicates
8+
9+
fun main() = application {
10+
configure {
11+
width = 800
12+
height = 800
13+
}
14+
15+
program {
16+
17+
val generator = basicGrblSetup(drawRate = 500.0).copy(
18+
preDraw = (40..80 step 5).flatMap { listOf("M3 S$it", "G4 P0.08") },
19+
postDraw = "M3 S40".asCommands(),
20+
)
21+
22+
val comp = drawComposition {
23+
lineSegment(10.0, 10.0, 20.0, 20.0)
24+
}
25+
26+
// Convert composition to g-code, print to console and exit
27+
val commands = generator.setup + comp.toCommands(generator, 0.05) + generator.end
28+
val gCode = commands.withoutDuplicates().toGcode()
29+
println(gCode)
30+
application.exit()
31+
32+
/* Output:
33+
G21
34+
G90
35+
;begin composition
36+
;begin shape: 0
37+
G0 X10.0 Y10.0
38+
M3 S40
39+
G4 P0.08
40+
M3 S45
41+
G4 P0.08
42+
M3 S50
43+
G4 P0.08
44+
M3 S55
45+
G4 P0.08
46+
M3 S60
47+
G4 P0.08
48+
M3 S65
49+
G4 P0.08
50+
M3 S70
51+
G4 P0.08
52+
M3 S75
53+
G4 P0.08
54+
M3 S80
55+
G4 P0.08
56+
G1 X20.0 Y20.0 F500.0
57+
M3 S40
58+
;end shape: 0
59+
;end composition
60+
G0 X0 Y0
61+
G90
62+
*/
63+
}
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import org.openrndr.MouseEvent
2+
import org.openrndr.application
3+
import org.openrndr.extensions.Screenshots
4+
import org.openrndr.extra.gcode.Plot
5+
import org.openrndr.extra.gcode.basicGrblSetup
6+
import org.openrndr.math.Vector2
7+
import org.openrndr.shape.ContourBuilder
8+
9+
fun main() = application {
10+
configure {
11+
width = 600
12+
height = 800
13+
}
14+
15+
program {
16+
extend(Screenshots())
17+
18+
val plot = Plot(dimensions = Vector2(210.0, 297.0), manualRedraw = false)
19+
extend(plot) {
20+
generator = basicGrblSetup()
21+
22+
// Set output files to be exported to tmp
23+
// "g" to export g-code.
24+
folder = "/tmp"
25+
}
26+
27+
val drawingArea = plot.docBounds.offsetEdges(-10.0)
28+
29+
// Converts mouse events position to document space
30+
fun MouseEvent.documentPosition(): Vector2 {
31+
val s = 1 / plot.scale()
32+
return Vector2(position.x * s, plot.docBounds.height - position.y * s)
33+
}
34+
35+
36+
val cb = ContourBuilder(true)
37+
38+
// Handle mouse events and restrict drawing to drawing area
39+
mouse.buttonDown.listen {
40+
val p = it.documentPosition()
41+
if (drawingArea.contains(p)) {
42+
cb.moveTo(p)
43+
} else {
44+
cb.moveTo(drawingArea.contour.nearest(p).position)
45+
}
46+
}
47+
mouse.dragged.listen {
48+
val p = it.documentPosition()
49+
if (drawingArea.contains(p)) {
50+
cb.moveOrLineTo(p)
51+
} else {
52+
cb.moveOrLineTo(drawingArea.contour.nearest(p).position)
53+
}
54+
}
55+
56+
// Draw contours of contour builder on every frame
57+
extend {
58+
plot.layer("drawing") {
59+
strokeWeight = .5
60+
contours(cb.result)
61+
}
62+
}
63+
}
64+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import org.openrndr.application
2+
import org.openrndr.color.ColorRGBa
3+
4+
import org.openrndr.extra.gcode.LayerMode
5+
import org.openrndr.extra.gcode.Plot
6+
7+
import org.openrndr.extra.gcode.basicGrblSetup
8+
9+
import org.openrndr.math.Vector2
10+
11+
fun main() = application {
12+
configure {
13+
width = 800
14+
height = 800
15+
}
16+
17+
program {
18+
19+
20+
// A4 Portrait
21+
extend(Plot(dimensions = Vector2(210.0, 297.0))) {
22+
generator = basicGrblSetup()
23+
24+
// Export each layer to separate file
25+
layerMode = LayerMode.MULTI_FILE
26+
27+
// Set output files to be exported to tmp
28+
// "g" to export g-code.
29+
folder = "/tmp"
30+
31+
draw {
32+
// Rectangle in default layer
33+
rectangle(docBounds.offsetEdges(-10.0))
34+
}
35+
36+
layer("circles") {
37+
// Stroke changes do not affect g-code
38+
stroke = ColorRGBa.PINK
39+
strokeWeight = .5
40+
(10..90 step 10).forEach { r ->
41+
circle(docBounds.center, r.toDouble())
42+
}
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)