Skip to content

Commit

Permalink
feat: add Program::context method
Browse files Browse the repository at this point in the history
Adds a Program::context method that returns a tool context that allows
for extending the program. This method will eventually replace the
deprecated Program::extend method.
  • Loading branch information
tirithen committed Oct 31, 2023
1 parent 2d4ed46 commit 613fabc
Show file tree
Hide file tree
Showing 14 changed files with 399 additions and 257 deletions.
28 changes: 13 additions & 15 deletions examples/planing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@ fn main() -> Result<()> {
5000.0, // Max feed rate/speed that the cutter will travel with (mm/min)
);

// Extend the program with the planing cuts
program.extend(&tool, |context| {
// 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,
));
// Get the tool context to extend the program
let mut context = program.context(tool);

Ok(())
})?;
// 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
Expand Down
51 changes: 23 additions & 28 deletions src/camotics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ impl Camotics {

#[cfg(test)]
mod tests {
use anyhow::Result;
use serde_json::Value;

use super::*;
Expand Down Expand Up @@ -267,7 +266,7 @@ mod tests {
}

#[test]
fn test_camotics_from_program() -> Result<()> {
fn test_camotics_from_program() {
let mut program = Program::new(Units::Metric, 10.0, 50.0);

let tool = Tool::cylindrical(
Expand All @@ -279,30 +278,28 @@ mod tests {
400.0,
);

program.extend(&tool, |context| {
context.append_cut(Cut::path(
Vector3::new(0.0, 0.0, 3.0),
vec![Segment::line(
Vector2::default(),
Vector2::new(-28.0, -30.0),
)],
-0.1,
1.0,
));

context.append_cut(Cut::path(
Vector3::new(0.0, 0.0, 3.0),
vec![
Segment::line(Vector2::new(23.0, 12.0), Vector2::new(5.0, 10.0)),
Segment::line(Vector2::new(5.0, 10.0), Vector2::new(67.0, 102.0)),
Segment::line(Vector2::new(67.0, 102.0), Vector2::new(23.0, 12.0)),
],
-0.1,
1.0,
));

Ok(())
})?;
let mut context = program.context(tool);

context.append_cut(Cut::path(
Vector3::new(0.0, 0.0, 3.0),
vec![Segment::line(
Vector2::default(),
Vector2::new(-28.0, -30.0),
)],
-0.1,
1.0,
));

context.append_cut(Cut::path(
Vector3::new(0.0, 0.0, 3.0),
vec![
Segment::line(Vector2::new(23.0, 12.0), Vector2::new(5.0, 10.0)),
Segment::line(Vector2::new(5.0, 10.0), Vector2::new(67.0, 102.0)),
Segment::line(Vector2::new(67.0, 102.0), Vector2::new(23.0, 12.0)),
],
-0.1,
1.0,
));

let camotics = Camotics::from_program("test-project", &program, 1.0);

Expand All @@ -325,7 +322,5 @@ mod tests {
files: vec!["test-project.gcode".to_string()]
}
);

Ok(())
}
}
2 changes: 1 addition & 1 deletion src/cuts/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Arc {

/// Converts arc to G-code instructions, will return error if the distance between
/// center -> from does not equal center -> to.
pub fn to_instructions(&self, context: Context) -> Result<Vec<Instruction>> {
pub fn to_instructions(&self, context: InnerContext) -> Result<Vec<Instruction>> {
let distance_from = self.from.distance_to(self.center);
let distance_to = self.to.distance_to(self.center);

Expand Down
2 changes: 1 addition & 1 deletion src/cuts/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Area {
}

/// Converts the struct to G-code instructions.
pub fn to_instructions(&self, context: Context) -> Result<Vec<Instruction>> {
pub fn to_instructions(&self, context: InnerContext) -> Result<Vec<Instruction>> {
let tool_radius = context.tool().radius();
let tool_diameter = context.tool().diameter();
let tool_units = context.tool().units();
Expand Down
2 changes: 1 addition & 1 deletion src/cuts/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Circle {
}

/// Converts the struct to G-code instructions.
pub fn to_instructions(&self, context: Context) -> Result<Vec<Instruction>> {
pub fn to_instructions(&self, context: InnerContext) -> Result<Vec<Instruction>> {
let mut instructions = vec![];

let tool_radius = context.tool().radius();
Expand Down
2 changes: 1 addition & 1 deletion src/cuts/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Frame {
}

/// Converts the struct to G-code instructions.
pub fn to_instructions(&self, context: Context) -> Result<Vec<Instruction>> {
pub fn to_instructions(&self, context: InnerContext) -> Result<Vec<Instruction>> {
let tool_radius = context.tool().radius();
let tool_diameter = context.tool().diameter();
let tool_units = context.tool().units();
Expand Down
2 changes: 1 addition & 1 deletion src/cuts/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Line {
}

/// Converts the struct to G-code instructions.
pub fn to_instructions(&self, context: Context) -> Result<Vec<Instruction>> {
pub fn to_instructions(&self, context: InnerContext) -> Result<Vec<Instruction>> {
let mut instructions = vec![];

instructions.append(&mut vec![
Expand Down
2 changes: 1 addition & 1 deletion src/cuts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Cut {
}

/// Converts the cuts to a list of G-code instructions
pub fn to_instructions(&self, context: Context) -> Result<Vec<Instruction>> {
pub fn to_instructions(&self, context: InnerContext) -> Result<Vec<Instruction>> {
match self {
Self::Arc(c) => c.to_instructions(context),
Self::Circle(c) => c.to_instructions(context),
Expand Down
2 changes: 1 addition & 1 deletion src/cuts/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl Path {
}

/// Converts the struct to G-code instructions.
pub fn to_instructions(&self, context: Context) -> Result<Vec<Instruction>> {
pub fn to_instructions(&self, context: InnerContext) -> Result<Vec<Instruction>> {
let mut instructions = vec![];

if self.segments.is_empty() {
Expand Down
62 changes: 29 additions & 33 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ use crate::{camotics::*, program::*};
/// 5000.0
/// );
///
/// program.extend(&tool, |context| {
/// context.append_cut(Cut::plane(
/// Vector3::new(0.0, 0.0, 3.0),
/// Vector2::new(100.0, 100.0),
/// 0.0,
/// 1.0,
/// ));
/// let mut context = program.context(tool);
///
/// Ok(())
/// })?;
/// context.append_cut(Cut::plane(
/// Vector3::new(0.0, 0.0, 3.0),
/// Vector2::new(100.0, 100.0),
/// 0.0,
/// 1.0,
/// ));
///
/// write_project("planing", &program, 0.5)?;
///
Expand Down Expand Up @@ -84,30 +82,28 @@ mod tests {
400.0,
);

program.extend(&tool, |context| {
context.append_cut(Cut::path(
Vector3::new(0.0, 0.0, 3.0),
vec![Segment::line(
Vector2::default(),
Vector2::new(-28.0, -30.0),
)],
-0.1,
1.0,
));

context.append_cut(Cut::path(
Vector3::new(0.0, 0.0, 3.0),
vec![
Segment::line(Vector2::new(23.0, 12.0), Vector2::new(5.0, 10.0)),
Segment::line(Vector2::new(5.0, 10.0), Vector2::new(67.0, 102.0)),
Segment::line(Vector2::new(67.0, 102.0), Vector2::new(23.0, 12.0)),
],
-0.1,
1.0,
));

Ok(())
})?;
let mut context = program.context(tool);

context.append_cut(Cut::path(
Vector3::new(0.0, 0.0, 3.0),
vec![Segment::line(
Vector2::default(),
Vector2::new(-28.0, -30.0),
)],
-0.1,
1.0,
));

context.append_cut(Cut::path(
Vector3::new(0.0, 0.0, 3.0),
vec![
Segment::line(Vector2::new(23.0, 12.0), Vector2::new(5.0, 10.0)),
Segment::line(Vector2::new(5.0, 10.0), Vector2::new(67.0, 102.0)),
Segment::line(Vector2::new(67.0, 102.0), Vector2::new(23.0, 12.0)),
],
-0.1,
1.0,
));

write_project("test-temp", &program, 0.5)?;

Expand Down
28 changes: 13 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,20 @@
//! 5000.0, // Max feed rate/speed that the cutter will travel with (mm/min)
//! );
//!
//! // Extend the program with the planing cuts
//! program.extend(&tool, |context| {
//! // 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,
//! ));
//! // Get the tool context to extend the program
//! let mut context = program.context(tool);
//!
//! Ok(())
//! })?;
//! // 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
Expand Down
Loading

0 comments on commit 613fabc

Please sign in to comment.