gerber_types/
function_codes.rs

1//! Function code types.
2
3use std::io::Write;
4
5use crate::coordinates::{CoordinateOffset, Coordinates};
6use crate::errors::GerberResult;
7use crate::traits::{GerberCode, PartialGerberCode};
8
9// DCode
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum DCode {
13    Operation(Operation),
14    SelectAperture(i32),
15}
16
17impl<W: Write> GerberCode<W> for DCode {
18    fn serialize(&self, writer: &mut W) -> GerberResult<()> {
19        match *self {
20            DCode::Operation(ref operation) => operation.serialize(writer)?,
21            DCode::SelectAperture(code) => writeln!(writer, "D{}*", code)?,
22        };
23        Ok(())
24    }
25}
26
27// GCode
28
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub enum GCode {
31    InterpolationMode(InterpolationMode),
32    RegionMode(bool),
33    QuadrantMode(QuadrantMode),
34    Comment(String),
35}
36
37impl<W: Write> GerberCode<W> for GCode {
38    fn serialize(&self, writer: &mut W) -> GerberResult<()> {
39        match *self {
40            GCode::InterpolationMode(ref mode) => mode.serialize(writer)?,
41            GCode::RegionMode(enabled) => {
42                if enabled {
43                    writeln!(writer, "G36*")?;
44                } else {
45                    writeln!(writer, "G37*")?;
46                }
47            }
48            GCode::QuadrantMode(ref mode) => mode.serialize(writer)?,
49            GCode::Comment(ref comment) => writeln!(writer, "G04 {}*", comment)?,
50        };
51        Ok(())
52    }
53}
54
55// MCode
56
57#[derive(Debug, Clone, PartialEq, Eq)]
58pub enum MCode {
59    EndOfFile,
60}
61
62impl<W: Write> GerberCode<W> for MCode {
63    fn serialize(&self, writer: &mut W) -> GerberResult<()> {
64        match *self {
65            MCode::EndOfFile => writeln!(writer, "M02*")?,
66        };
67        Ok(())
68    }
69}
70
71// Operation
72
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub enum Operation {
75    /// D01 Command
76    Interpolate(Coordinates, Option<CoordinateOffset>),
77    /// D02 Command
78    Move(Coordinates),
79    /// D03 Command
80    Flash(Coordinates),
81}
82
83impl<W: Write> GerberCode<W> for Operation {
84    fn serialize(&self, writer: &mut W) -> GerberResult<()> {
85        match *self {
86            Operation::Interpolate(ref coords, ref offset) => {
87                coords.serialize_partial(writer)?;
88                offset.serialize_partial(writer)?;
89                writeln!(writer, "D01*")?;
90            }
91            Operation::Move(ref coords) => {
92                coords.serialize_partial(writer)?;
93                writeln!(writer, "D02*")?;
94            }
95            Operation::Flash(ref coords) => {
96                coords.serialize_partial(writer)?;
97                writeln!(writer, "D03*")?;
98            }
99        };
100        Ok(())
101    }
102}
103
104// InterpolationMode
105
106#[derive(Debug, Clone, Copy, PartialEq, Eq)]
107pub enum InterpolationMode {
108    Linear,
109    ClockwiseCircular,
110    CounterclockwiseCircular,
111}
112
113impl<W: Write> GerberCode<W> for InterpolationMode {
114    fn serialize(&self, writer: &mut W) -> GerberResult<()> {
115        match *self {
116            InterpolationMode::Linear => writeln!(writer, "G01*")?,
117            InterpolationMode::ClockwiseCircular => writeln!(writer, "G02*")?,
118            InterpolationMode::CounterclockwiseCircular => writeln!(writer, "G03*")?,
119        };
120        Ok(())
121    }
122}
123
124// QuadrantMode
125
126#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127pub enum QuadrantMode {
128    Single,
129    Multi,
130}
131
132impl<W: Write> GerberCode<W> for QuadrantMode {
133    fn serialize(&self, writer: &mut W) -> GerberResult<()> {
134        match *self {
135            QuadrantMode::Single => writeln!(writer, "G74*")?,
136            QuadrantMode::Multi => writeln!(writer, "G75*")?,
137        };
138        Ok(())
139    }
140}
141
142#[cfg(test)]
143mod test {}