Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/target
Cargo.lock
simple.gcode
test_transformed.gcode
test.gcode
test_transformed.gcode
16 changes: 14 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,20 @@ The application is programmed in rust with the [nannou library](https://nannou.c
It furthermore supports a subcommand `transform`, which
allows you to transform a file by translation and dilation.

## Keyboard commands and editing features

In the graphical app, a few keyboard commands are enabled. To increase a value corresponding to a <kbd>key</kbd>, just press <kbd>key</kbd> and to decrease press <kbd>shift</kbd> + <kbd>key</kbd>. For bigger steps combine these combination with a further <kbd>ctrl</kbd>.

Furthermore, I introduced in Version 0.3.0 the ability to extend existing g-code files. Simply press
- <kbd>1</kbd> for `G1` mode
- <kbd>2</kbd> for `G2` mode
- <kbd>3</kbd> for `G3` mode
- <kbd>0</kbd> to exit the modes

and choose the coordinate with a left mouse click. One also can now undo and redo these added commands with <kbd>Z</kbd> and <kbd>Y</kbd> and save these changes to a new file with <kbd>S</kbd>. <kbd>P</kbd> changes the penmode and <kbd>H</kbd> returns to to home.

Last but not least, right-clicking prints the mouse coordinates to console.

## Room to improve

While the app can be used for many purposes, it is in a early development phase. Tests are missing and not everything is programmed the clever way. If you want to improve it feel welcome to contribute.
Expand All @@ -39,5 +51,5 @@ TODO:
- [ ] better loop management for hot reloading
- [ ] more file maniplulation features
- [ ] move in grid support
- [ ] draw mode?
- [ ] more debug options, e.g. show coordinates
- [X] draw mode?
- [ ] more debug options, e.g. show coordinates
272 changes: 260 additions & 12 deletions src/main.rs

Large diffs are not rendered by default.

105 changes: 0 additions & 105 deletions src/old_parse.rs

This file was deleted.

27 changes: 27 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum GCodeExpr<'a> {

/// without Comments, for faster and more memory efficient usecases
#[allow(non_snake_case)]
#[derive(Copy, Clone)]
pub enum CommentlessGCodeExpr {
Home,
Move { X: f32, Y: f32 },
Expand Down Expand Up @@ -47,6 +48,21 @@ impl GCodeExpr<'_> {
}
}

impl CommentlessGCodeExpr {
pub fn as_str(&self) -> String {
match self {
CommentlessGCodeExpr::Home => "G28".to_string(),
CommentlessGCodeExpr::Move{X: x, Y: y} => format!("G1 X{} Y{}", x, y),
CommentlessGCodeExpr::Arc{CLKW: clkw, X: x, Y: y, I: i, J: j} => if *clkw {
format!("G2 X{} Y{} I{} J{}", x, y, i, j)
} else {
format!("G3 X{} Y{} I{} J{}", x, y, i, j)
},
CommentlessGCodeExpr::Pen(down) => if *down { "M280 P0 S50".to_string() } else { "M280 P0 S0".to_string() },
}
}
}

#[derive(Parser)]
#[grammar = "gcode.pest"]
struct GCodeParser;
Expand Down Expand Up @@ -138,4 +154,15 @@ pub fn save(filename: &str, commands: Vec<(usize, GCodeExpr)>) {
})
//collect::<Vec<String>>().join("\n")
).expect("Unable to save the gcode.");
}

/// saves new commands on tope
pub fn resave(filename: &str, commands: &Vec<CommentlessGCodeExpr>) {
let oldfile = std::fs::read_to_string(filename).expect("unable to open the file.");
std::fs::write(
format!("{}_added.gcode", filename.strip_suffix(".gcode").expect("Expected gcode file")),
format!("{}\n; added by gcodeplot\n{}", oldfile,
commands.iter().map(|cmd| cmd.as_str()).collect::<Vec<String>>().join("\n")
)
).expect("Unable to save the gcode.");
}
21 changes: 21 additions & 0 deletions test.gcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
G28
G1 X10 Y10
G1 X20 Y28
M280 P0 S50
G3 X18 Y30 I-2 J0
G1 X14 Y30
G3 X10 Y26 I0 J-4
G1 X10 Y21
G3 X11 Y20 I1 J0
G1 X15 Y20
G3 X17 Y22 I0 J2
G1 X17 Y18
G3 X15 Y20 I-2 J0
M280 P0 S0
G1 X11 Y20
M280 P0 S50
G3 X10 Y19 I0 J-1
G1 X10 Y14
G3 X14 Y10 I4 J0
G1 X18 Y10
G3 X20 Y12 I0 J2
26 changes: 26 additions & 0 deletions test_added.gcode
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
G28
G1 X10 Y10
G1 X20 Y28
M280 P0 S50
G3 X18 Y30 I-2 J0
G1 X14 Y30
G3 X10 Y26 I0 J-4
G1 X10 Y21
G3 X11 Y20 I1 J0
G1 X15 Y20
G3 X17 Y22 I0 J2
G1 X17 Y18
G3 X15 Y20 I-2 J0
M280 P0 S0
G1 X11 Y20
M280 P0 S50
G3 X10 Y19 I0 J-1
G1 X10 Y14
G3 X14 Y10 I4 J0
G1 X18 Y10
G3 X20 Y12 I0 J2
; added by gcodeplot
G1 X25 Y15
G1 X30 Y10
G2 X35 Y15 I5 J0
G2 X30 Y20 I0 J5