1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! cnccoder is a crate for writing cutting instructions and converting them to
//! [G-code](https://en.wikipedia.org/wiki/G-code) for use on 3 axis
//! [CNC machines](https://en.wikipedia.org/wiki/Numerical_control).
//!
//! Rather than generating cuts from 3D models as FreeCAD and similar software,
//! it allows the user to precisely write the instructions for how the CNC machine
//! should cut and which [tools](tools/index.html) to use.
//!
//! By providing several helper [cutting functions](cuts/index.html) and
//! [programs](programs/), the crate can then compile these higher level
//! instructions down to G-code.
//!
//! The crate can also generate project files for [Camotics](https://camotics.org/)
//! that can be used to simulate the G-code so that it can be verified before ran
//! on an actual machine, reducing the risk of damaging the CNC machine and injury.
//!
//! G-code does come in several flavors, but so far the project is only targeting
//! CNC machines using the [Grbl](https://github.com/gnea/grbl) controller.
//!
//! Example of a simple planing program (from `examples/planing.rs`):
//! ```
//! use anyhow::Result;
//! use cnccoder::prelude::*;
//!
//! fn main() -> Result<()> {
//! // Create a program with metric measurements where the tool can travel freely at 10 mm
//! // height, and move to 50 mm height for manual tool change.
//! let mut program = Program::new(Units::Metric, 10.0, 50.0);
//!
//! // Create a cylindrical tool
//! let tool = Tool::cylindrical(
//! Units::Metric, // Unit for tool measurements
//! 20.0, // Cutter length
//! 10.0, // Cutter diameter
//! Direction::Clockwise, // Spindle rotation direction
//! 10000.0, // Spindle speed (rpm)
//! 3000.0, // Max feed rate/speed that the cutter will travel with (mm/min)
//! );
//!
//! // Get the tool context to extend the program
//! let mut context = program.context(tool);
//!
//! // Append the planing cuts to the cylindrical tool context
//! context.append_cut(Cut::plane(
//! // Start at the x 0 mm, y 0 mm, z 3 mm coordinates
//! Vector3::new(0.0, 0.0, 3.0),
//! // Plane a 100 x 100 mm area
//! Vector2::new(100.0, 100.0),
//! // Plane down to 0 mm height (from 3 mm)
//! 0.0,
//! // Cut at the most 1 mm per pass
//! 1.0,
//! ));
//!
//! // Write the G-code (for CNC) `planing.gcode` and Camotics project file
//! // `planing.camotics` (for simulation) to disk using a resolution value
//! // of 0.5 for the Camotics simulation.
//! write_project(&program, 0.5)?;
//!
//! Ok(())
//! }
//! ```
//!
//! To run the G-code simulation in Camotics (if installed), simply run:
//! ```bash
//! $ cargo run --example planing && camotics planing.camotics
//! ```
//!
//! The `cargo run --example planing` command generates the G-code file
//! `planing.gcode` and the Camotics project file `planing.camotics` in the
//! current directory.
//!
//! The files can then be opened in Camotics by running `camotics planing.camotics`.
//!
//! In this way you can easily simulate your projects.
/// A prelude module to simplify imports in projects using this crate.
///
/// All public components can be imported in a project with:
/// ```
/// use cnccoder::prelude::*;
///
/// let program = Program::new(Units::Metric, 5.0, 50.0);
/// // The rest of your CNC program...
/// ```