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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
use anyhow::{anyhow, Result};
use crate::instructions::*;
use crate::program::*;
use crate::types::*;
use crate::utils::*;
/// Cut a circle spiraling down in a helix to `end_z`.
///
/// If the circle radius equals the tool radius with `ToolPathCompensation::None` the cut will
/// instead be a drilling top/down cut. Unlike [Area](struct.Area.html), the circle cut will
/// only cut at the edge of the circle, and not cut inside the circle.
#[derive(Debug, Clone)]
pub struct Circle {
/// Start point in 3D space.
pub start: Vector3,
/// The circle radius
pub radius: f64,
/// The end depth of the cut on the z axis.
pub end_z: f64,
/// The maximum depth to cut on the z axis on each pass.
pub max_step_z: f64,
/// Indicates how a path should be compensated by the radius of the tool.
/// `ToolPathCompensation::Inner` is useful for cutting holes wider than the tool,
/// `ToolPathCompensation::Outer` is useful for cutting out round pieces, and
/// `ToolPathCompensation::Outer` is useful when drilling.
pub compensation: ToolPathCompensation,
}
impl Circle {
/// Creates a new `Circle` struct.
#[must_use]
pub fn new(
start: Vector3,
radius: f64,
end_z: f64,
max_step_z: f64,
compensation: ToolPathCompensation,
) -> Self {
Self {
start,
end_z,
radius,
max_step_z,
compensation,
}
}
/// Drill cut from start coordinate to end z depth.
#[must_use]
pub fn drill(start: Vector3, end_z: f64) -> Self {
Self {
start,
radius: 0.0,
end_z,
max_step_z: 0.0,
compensation: ToolPathCompensation::None,
}
}
/// Returns the bounds of the cut.
#[must_use]
pub fn bounds(&self) -> Bounds {
Bounds {
min: Vector3::new(
self.start.x - self.radius,
self.start.y - self.radius,
self.end_z,
),
max: Vector3::new(
self.start.x + self.radius,
self.start.y + self.radius,
self.start.z,
),
}
}
/// Converts the struct to G-code instructions.
pub fn to_instructions(&self, context: InnerContext) -> Result<Vec<Instruction>> {
let mut instructions = vec![];
let tool_radius = context.tool().radius();
let cut_radius = match self.compensation {
ToolPathCompensation::None => self.radius,
ToolPathCompensation::Inner => self.radius - tool_radius,
ToolPathCompensation::Outer => self.radius + tool_radius,
};
if (0.0..0.001).contains(&cut_radius) {
instructions.append(&mut vec![
Instruction::Empty(Empty {}),
Instruction::Comment(Comment {
text: format!(
"Drill hole at: x = {}, y = {}",
round_precision(self.start.x),
round_precision(self.start.y)
),
}),
Instruction::G0(G0 {
x: None,
y: None,
z: Some(context.z_safe()),
}),
Instruction::G0(G0 {
x: Some(self.start.x),
y: Some(self.start.y),
z: None,
}),
Instruction::G1(G1 {
x: None,
y: None,
z: Some(self.end_z),
f: Some(context.tool().feed_rate()),
}),
Instruction::G0(G0 {
x: None,
y: None,
z: Some(context.z_safe()),
}),
])
} else if cut_radius > 0.0 {
instructions.append(&mut vec![
Instruction::Empty(Empty {}),
Instruction::Comment(Comment {
text: format!(
"Cut hole at: x = {}, y = {}",
round_precision(self.start.x),
round_precision(self.start.y)
),
}),
Instruction::G0(G0 {
x: None,
y: None,
z: Some(context.z_safe()),
}),
Instruction::G0(G0 {
x: Some(self.start.x - cut_radius),
y: Some(self.start.y),
z: None,
}),
Instruction::G1(G1 {
x: None,
y: None,
z: Some(self.start.z),
f: Some(context.tool().feed_rate()),
}),
]);
let max_step_z = self.max_step_z.abs();
// TODO: add check that layer steps does not exceed cutting height if the bit
let layers = ((self.start.z - self.end_z) / max_step_z).floor() as u32;
// Cut spiraling down in steps
for index in 0..layers {
instructions.push(Instruction::G2(G2 {
x: Some(self.start.x - cut_radius),
y: None,
z: Some((self.start.z - index as f64 * max_step_z).max(self.end_z)),
i: Some(cut_radius),
j: None,
k: None,
r: None,
p: None,
f: None,
}));
}
// Extra flat circle
instructions.push(Instruction::G2(G2 {
x: Some(self.start.x - cut_radius),
y: None,
z: Some(self.end_z),
i: Some(cut_radius),
j: None,
k: None,
r: None,
p: None,
f: None,
}));
instructions.push(Instruction::G2(G2 {
x: Some(self.start.x - cut_radius),
y: None,
z: Some(self.end_z),
i: Some(cut_radius - 0.001),
j: None,
k: None,
r: None,
p: None,
f: None,
}));
instructions.push(Instruction::G0(G0 {
x: None,
y: None,
z: Some(context.z_safe()),
}));
} else {
let tool = context.tool();
let units = context.units();
// TODO: handle calculation for the case when tool and program units are different.
return Err(anyhow!(
"Unable to cut circle of diameter {:.2} {} with tool diameter {:.2} {}.",
cut_radius.abs() * 2.0,
units,
tool.diameter(),
units,
));
}
Ok(instructions)
}
}