cnccoder/
lib.rs

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