cnccoder 0.2.0

A library for generating gcode operations targeted for GRBL controled cnc machines, and also generates camotics projects for simulation
Documentation
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#![cfg_attr(feature = "doc-images",
cfg_attr(all(),
doc = ::embed_doc_image::embed_image!("ballnose-tool", "doc-assets/ballnose-tool.webp"),
doc = ::embed_doc_image::embed_image!("conical-tool", "doc-assets/conical-tool.webp"),
doc = ::embed_doc_image::embed_image!("cylindrical-tool", "doc-assets/cylindrical-tool.webp"),
))]
#![cfg_attr(
    not(feature = "doc-images"),
    doc = "**Doc images not enabled**. Compile with feature `doc-images` and Rust version >= 1.54 \
           to enable."
)]

//! Module containing tool configurations for ballnose, conical, and cylindrical cutting tools.
//!
//! |Tool type |Example image |Uses |
//! |--- |---  |--- |
//! |Ballnose|![Ballnose router tool][ballnose-tool]|The ballnose tool can be useful for 3D carving to achieve a smooth surface result.|
//! |Conical|![90° Conical router tool][conical-tool]|The conical tool can be useful for v carving when engraving images, inlays or text.|
//! |Cylindrical|![Cylindrical router tool][cylindrical-tool]|The cylindrical tool is a great general purpose tool when cutting contours, pockets, holes, and planing.|
//!
//! Creating a tool using the `Tool::cylindrical` helper:
//! ```
//! use cnccoder::prelude::*;
//!
//! // 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
//!     20000.0, // Spindle speed (rpm)
//!     5000.0, // Max feed rate/speed that the cutter will travel with (mm/min)
//! );
//! ```

use std::{
    collections::HashMap,
    fmt,
    hash::{Hash, Hasher},
};

use serde::{Deserialize, Serialize};

use crate::types::*;
use crate::utils::*;

/// Represents a tool configuration.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone, Copy)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum Tool {
    /// Ballnose tool configuration.
    Ballnose(Ballnose),
    /// Conical tool configuration.
    Conical(Conical),
    /// Cylindrical tool configuration.
    Cylindrical(Cylindrical),
}

impl Tool {
    /// Helper for creating a ballnose tool configuration.
    #[must_use]
    pub fn ballnose(
        units: Units,
        length: f64,
        diameter: f64,
        direction: Direction,
        spindle_speed: f64,
        feed_rate: f64,
    ) -> Tool {
        Tool::Ballnose(Ballnose::new(
            units,
            length,
            diameter,
            direction,
            spindle_speed,
            feed_rate,
        ))
    }

    /// Helper for creating a conical tool configuration.
    #[must_use]
    pub fn conical(
        units: Units,
        angle: f64,
        diameter: f64,
        direction: Direction,
        spindle_speed: f64,
        feed_rate: f64,
    ) -> Tool {
        Tool::Conical(Conical::new(
            units,
            angle,
            diameter,
            direction,
            spindle_speed,
            feed_rate,
        ))
    }

    /// Helper for creating a cylindrical tool configuration.
    #[must_use]
    pub fn cylindrical(
        units: Units,
        length: f64,
        diameter: f64,
        direction: Direction,
        spindle_speed: f64,
        feed_rate: f64,
    ) -> Tool {
        Tool::Cylindrical(Cylindrical::new(
            units,
            length,
            diameter,
            direction,
            spindle_speed,
            feed_rate,
        ))
    }

    /// Returns the units used for the tool measurements (mm for metric, and inches for imperial).
    #[must_use]
    pub fn units(&self) -> Units {
        match self {
            Self::Cylindrical(t) => t.units,
            Self::Ballnose(t) => t.units,
            Self::Conical(t) => t.units,
        }
    }

    /// Returns the diameter of the tool cutter.
    #[must_use]
    pub fn diameter(&self) -> f64 {
        match self {
            Self::Cylindrical(t) => t.diameter,
            Self::Ballnose(t) => t.diameter,
            Self::Conical(t) => t.diameter,
        }
    }

    /// Returns the radius of the tool cutter.
    #[must_use]
    pub fn radius(&self) -> f64 {
        match self {
            Self::Cylindrical(t) => t.diameter / 2.0,
            Self::Ballnose(t) => t.diameter / 2.0,
            Self::Conical(t) => t.diameter / 2.0,
        }
    }

    /// Returns the spin direction for the tool.
    #[must_use]
    pub fn direction(&self) -> Direction {
        match self {
            Self::Cylindrical(t) => t.direction,
            Self::Ballnose(t) => t.direction,
            Self::Conical(t) => t.direction,
        }
    }

    /// Returns the configured spindle/tool rotation speed (rpm).
    #[must_use]
    pub fn spindle_speed(&self) -> f64 {
        match self {
            Self::Cylindrical(t) => t.spindle_speed,
            Self::Ballnose(t) => t.spindle_speed,
            Self::Conical(t) => t.spindle_speed,
        }
    }

    /// Returns the configured feed rate (mm/min for metric and inches/min for imperial) for the tool.
    #[must_use]
    pub fn feed_rate(&self) -> f64 {
        match self {
            Self::Cylindrical(t) => t.feed_rate,
            Self::Ballnose(t) => t.feed_rate,
            Self::Conical(t) => t.feed_rate,
        }
    }
}

impl Default for Tool {
    fn default() -> Self {
        Self::Cylindrical(Cylindrical::default())
    }
}

impl fmt::Display for Tool {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let description = match self {
            Self::Cylindrical(t) => t.to_string(),
            Self::Ballnose(t) => t.to_string(),
            Self::Conical(t) => t.to_string(),
        };

        write!(formatter, "{}", description)
    }
}

/// Ballnose tool configuration.
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Ballnose {
    /// The units used for the tool measurements (mm for metric, and inches for imperial).
    pub units: Units,
    /// The length of the tool cutter.
    pub length: f64,
    /// The diameter of the tool cutter.
    pub diameter: f64,
    /// The spin direction for the tool.
    pub direction: Direction,
    /// The selected spindle/tool rotation speed (rpm) for this tool.
    pub spindle_speed: f64,
    /// The selected feed rate (mm/min for metric and inches/min for imperial) for this tool.
    pub feed_rate: f64,
}

impl Ballnose {
    /// Creates a new `Ballnose` tool struct
    #[must_use]
    pub fn new(
        units: Units,
        length: f64,
        diameter: f64,
        direction: Direction,
        spindle_speed: f64,
        feed_rate: f64,
    ) -> Ballnose {
        Ballnose {
            units,
            length,
            diameter,
            direction,
            spindle_speed,
            feed_rate,
        }
    }

    /// Returns the radius of the tool cutter.
    #[must_use]
    pub fn radius(&self) -> f64 {
        self.diameter / 2.0
    }
}

impl Default for Ballnose {
    fn default() -> Self {
        Self {
            units: Units::Metric,
            length: 5.0,
            diameter: 2.0,
            direction: Direction::Clockwise,
            spindle_speed: 10000.0,
            feed_rate: 500.0,
        }
    }
}

impl fmt::Display for Ballnose {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let units = match self.units {
            Units::Imperial => self.units.to_string(),
            Units::Metric => format!(" {}", self.units),
        };

        write!(
            formatter,
            "type = Ballnose, diameter = {}{}, length = {}{}, direction = {}, spindle_speed = {} rpm, feed_rate = {}{}/min",
            round_precision(self.diameter),
            units,
            round_precision(self.length),
            units,
            self.direction,
            round_precision(self.spindle_speed),
            round_precision(self.feed_rate),
            units,
        )
    }
}

impl PartialEq for Ballnose {
    fn eq(&self, other: &Ballnose) -> bool {
        self.units == other.units
            && self.length == other.length
            && self.diameter == other.diameter
            && self.direction == other.direction
            && self.spindle_speed == other.spindle_speed
            && self.feed_rate == other.feed_rate
    }
}

impl Eq for Ballnose {}

impl Hash for Ballnose {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.units.hash(state);
        self.length.to_bits().hash(state);
        self.diameter.to_bits().hash(state);
        self.direction.hash(state);
        self.spindle_speed.to_bits().hash(state);
        self.feed_rate.to_bits().hash(state);
    }
}

/// Conical tool configuration.
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Conical {
    /// The units used for the tool measurements (mm for metric, and inches for imperial).
    pub units: Units,
    /// The length of the tool cutter.
    pub length: f64,
    /// The angle of the tool cutter.
    pub angle: f64,
    /// The diameter of the tool cutter.
    pub diameter: f64,
    /// The spin direction for the tool.
    pub direction: Direction,
    /// The selected spindle/tool rotation speed (rpm) for this tool.
    pub spindle_speed: f64,
    /// The selected feed rate (mm/min for metric and inches/min for imperial) for this tool.
    pub feed_rate: f64,
}

impl Conical {
    /// Creates a new `Conical` tool struct
    #[must_use]
    pub fn new(
        units: Units,
        angle: f64,
        diameter: f64,
        direction: Direction,
        spindle_speed: f64,
        feed_rate: f64,
    ) -> Conical {
        Conical {
            units,
            length: (diameter / 2.0) / (angle / 2.0).to_radians().tan(),
            angle,
            diameter,
            direction,
            spindle_speed,
            feed_rate,
        }
    }

    /// Returns the radius of the tool cutter.
    #[must_use]
    pub fn radius(&self) -> f64 {
        self.diameter / 2.0
    }
}

impl Default for Conical {
    fn default() -> Self {
        Self {
            units: Units::Metric,
            length: 8.0,
            angle: 90.0,
            diameter: 16.0,
            direction: Direction::Clockwise,
            spindle_speed: 10000.0,
            feed_rate: 500.0,
        }
    }
}

impl fmt::Display for Conical {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let units = match self.units {
            Units::Imperial => self.units.to_string(),
            Units::Metric => format!(" {}", self.units),
        };

        write!(
            formatter,
            "type = Conical, angle = {}°, diameter = {}{}, length = {}{}, direction = {}, spindle_speed = {} rpm, feed_rate = {}{}/min",
            round_precision(self.angle),
            round_precision(self.diameter),
            units,
            round_precision(self.length),
            units,
            self.direction,
            round_precision(self.spindle_speed),
            round_precision(self.feed_rate),
            units,
        )
    }
}

impl PartialEq for Conical {
    fn eq(&self, other: &Conical) -> bool {
        self.units == other.units
            && self.angle == other.angle
            && self.length == other.length
            && self.diameter == other.diameter
            && self.direction == other.direction
            && self.spindle_speed == other.spindle_speed
            && self.feed_rate == other.feed_rate
    }
}

impl Eq for Conical {}

impl Hash for Conical {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.units.hash(state);
        self.angle.to_bits().hash(state);
        self.length.to_bits().hash(state);
        self.diameter.to_bits().hash(state);
        self.direction.hash(state);
        self.spindle_speed.to_bits().hash(state);
        self.feed_rate.to_bits().hash(state);
    }
}

/// Cylindrical tool configuration.
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Cylindrical {
    /// The units used for the tool measurements (mm for metric, and inches for imperial).
    pub units: Units,
    /// The length of the tool cutter.
    pub length: f64,
    /// The diameter of the tool cutter.
    pub diameter: f64,
    /// The spin direction for the tool.
    pub direction: Direction,
    /// The selected spindle/tool rotation speed (rpm) for this tool.
    pub spindle_speed: f64,
    /// The selected feed rate (mm/min for metric and inches/min for imperial) for this tool.
    pub feed_rate: f64,
}

impl Cylindrical {
    /// Creates a new `Cylindrical` tool struct
    #[must_use]
    pub fn new(
        units: Units,
        length: f64,
        diameter: f64,
        direction: Direction,
        spindle_speed: f64,
        feed_rate: f64,
    ) -> Cylindrical {
        Cylindrical {
            units,
            length,
            diameter,
            direction,
            spindle_speed,
            feed_rate,
        }
    }

    /// Returns the radius of the tool cutter.
    #[must_use]
    pub fn radius(&self) -> f64 {
        self.diameter / 2.0
    }
}

impl Default for Cylindrical {
    fn default() -> Self {
        Self {
            units: Units::Metric,
            length: 30.0,
            diameter: 6.0,
            direction: Direction::Clockwise,
            spindle_speed: 10000.0,
            feed_rate: 500.0,
        }
    }
}

impl fmt::Display for Cylindrical {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let units = match self.units {
            Units::Imperial => self.units.to_string(),
            Units::Metric => format!(" {}", self.units),
        };

        write!(
            formatter,
            "type = Cylindrical, diameter = {}{}, length = {}{}, direction = {}, spindle_speed = {} rpm, feed_rate = {}{}/min",
            round_precision(self.diameter),
            units,
            round_precision(self.length),
            units,
            self.direction,
            round_precision(self.spindle_speed),
            round_precision(self.feed_rate),
            units,
        )
    }
}

impl PartialEq for Cylindrical {
    fn eq(&self, other: &Cylindrical) -> bool {
        self.units == other.units
            && self.length == other.length
            && self.diameter == other.diameter
            && self.direction == other.direction
            && self.spindle_speed == other.spindle_speed
            && self.feed_rate == other.feed_rate
    }
}

impl Eq for Cylindrical {}

impl Hash for Cylindrical {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.units.hash(state);
        self.length.to_bits().hash(state);
        self.diameter.to_bits().hash(state);
        self.direction.hash(state);
        self.spindle_speed.to_bits().hash(state);
        self.feed_rate.to_bits().hash(state);
    }
}

/// Keeps a list of tools and their order. It also allows for manipulating the order of the tools to ease choosing which cuts that should be made first.
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct ToolOrdering {
    tools: Vec<Tool>,
    ordering: HashMap<Tool, u8>,
    explicit_ordering: HashMap<Tool, u8>,
}

impl ToolOrdering {
    fn next_auto_order(&self) -> u8 {
        let mut next_order = 1;

        while self
            .ordering
            .values()
            .chain(self.explicit_ordering.values())
            .any(|&order| order == next_order)
        {
            next_order += 1;
        }

        next_order
    }

    /// Adds a tool and automatically assign the first available order to the tool and its related cuts.
    pub fn auto_ordering(&mut self, tool: &Tool) {
        if self.ordering.contains_key(tool) {
            return;
        }

        self.tools.push(*tool);
        self.ordering.insert(*tool, self.next_auto_order());
    }

    /// Adds a tool and assign a specific order to the tool and its related cuts. The minimum order value is 1.
    pub fn set_ordering(&mut self, tool: &Tool, order: u8) {
        let order = if order == 0 { 1 } else { order };

        self.tools.push(*tool);
        self.explicit_ordering
            .retain(|t, o| *o != order || t == tool);
        self.explicit_ordering.insert(*tool, order);

        self.ordering.clear();

        self.explicit_ordering.iter().for_each(|(t, &o)| {
            self.ordering.insert(*t, o);
        });

        for tool in &self.tools {
            if !self.ordering.contains_key(tool) {
                self.ordering.insert(*tool, self.next_auto_order());
            }
        }
    }

    /// Returns the order of the tool and its related cuts, returns None if the tool has not been added.
    pub fn ordering(&self, tool: &Tool) -> Option<u8> {
        self.ordering.get(tool).copied()
    }

    /// Returns an ordered list of the tools added.
    pub fn tools_ordered(&self) -> Vec<Tool> {
        let mut tools = vec![];

        let tool_ordering = &self.ordering;
        let mut orderings: Vec<_> = tool_ordering.iter().collect();
        orderings.sort_by(|a, b| a.1.cmp(b.1));

        for (tool, _) in orderings {
            tools.push(*tool);
        }

        tools
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_auto_ordering() {
        let mut tool_ordering = ToolOrdering::default();

        let tool1 = Tool::conical(
            Units::Metric,
            30.0,
            6.0,
            Direction::Clockwise,
            10000.0,
            500.0,
        );
        let tool2 = Tool::cylindrical(
            Units::Metric,
            30.0,
            2.0,
            Direction::Clockwise,
            10000.0,
            500.0,
        );

        tool_ordering.auto_ordering(&tool1);
        tool_ordering.auto_ordering(&tool2);

        assert_eq!(tool_ordering.ordering(&tool1), Some(1));
        assert_eq!(tool_ordering.ordering(&tool2), Some(2));
        assert_ne!(
            tool_ordering.ordering(&tool1),
            tool_ordering.ordering(&tool2)
        );
    }

    #[test]
    fn test_set_ordering() {
        let mut tool_ordering = ToolOrdering::default();

        let tool1 = Tool::conical(
            Units::Metric,
            45.0,
            6.0,
            Direction::Clockwise,
            10000.0,
            500.0,
        );
        let tool2 = Tool::cylindrical(
            Units::Metric,
            20.0,
            4.0,
            Direction::Clockwise,
            10000.0,
            500.0,
        );

        tool_ordering.set_ordering(&tool1, 1);
        tool_ordering.set_ordering(&tool2, 2);

        assert_eq!(tool_ordering.ordering(&tool1), Some(1));
        assert_eq!(tool_ordering.ordering(&tool2), Some(2));

        tool_ordering.set_ordering(&tool1, 3);

        assert_eq!(tool_ordering.ordering(&tool1), Some(3));
        assert_eq!(tool_ordering.ordering(&tool2), Some(2));
    }

    #[test]
    fn test_mix_set_and_auto_ordering() {
        let mut tool_ordering = ToolOrdering::default();

        let tool1 = Tool::conical(
            Units::Metric,
            30.0,
            4.0,
            Direction::Clockwise,
            10000.0,
            500.0,
        );
        let tool2 = Tool::ballnose(
            Units::Metric,
            20.0,
            1.0,
            Direction::Clockwise,
            10000.0,
            500.0,
        );
        let tool3 = Tool::cylindrical(
            Units::Metric,
            32.0,
            2.0,
            Direction::Clockwise,
            10000.0,
            500.0,
        );

        tool_ordering.auto_ordering(&tool1);
        tool_ordering.set_ordering(&tool2, 1);
        tool_ordering.auto_ordering(&tool3);

        assert_eq!(tool_ordering.ordering(&tool1), Some(2));
        assert_eq!(tool_ordering.ordering(&tool2), Some(1));
        assert_eq!(tool_ordering.ordering(&tool3), Some(3));

        assert_ne!(
            tool_ordering.ordering(&tool1),
            tool_ordering.ordering(&tool2)
        );
        assert_ne!(
            tool_ordering.ordering(&tool1),
            tool_ordering.ordering(&tool3)
        );
        assert_ne!(
            tool_ordering.ordering(&tool2),
            tool_ordering.ordering(&tool3)
        );
    }
}