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
use std::convert::From;
use std::io::Write;
use ::{GerberCode, GerberError, GerberResult};
#[derive(Debug)]
pub struct ApertureMacro {
pub name: String,
pub content: Vec<MacroContent>,
}
impl ApertureMacro {
pub fn new<S: Into<String>>(name: S) -> Self {
ApertureMacro {
name: name.into(),
content: Vec::new(),
}
}
pub fn add_content<C>(mut self, c: C) -> Self where C: Into<MacroContent>{
self.content.push(c.into());
self
}
pub fn add_content_mut<C>(&mut self, c: C) where C: Into<MacroContent> {
self.content.push(c.into());
}
}
impl<W: Write> GerberCode<W> for ApertureMacro {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
if self.content.is_empty() {
return Err(GerberError::MissingDataError("There must be at least 1 content element in an aperture macro".into()));
}
writeln!(writer, "AM{}*", self.name)?;
let mut first = true;
for content in &self.content {
if first {
first = false;
} else {
write!(writer, "\n")?;
}
content.to_code(writer)?;
}
Ok(())
}
}
#[derive(Debug, PartialEq)]
pub enum MacroDecimal {
Value(f64),
Variable(u32),
}
impl MacroDecimal {
fn is_negative(&self) -> bool {
match *self {
MacroDecimal::Value(v) => v < 0.0,
MacroDecimal::Variable(_) => false,
}
}
}
impl From<f32> for MacroDecimal {
fn from(val: f32) -> Self {
MacroDecimal::Value(val as f64)
}
}
impl From<f64> for MacroDecimal {
fn from(val: f64) -> Self {
MacroDecimal::Value(val)
}
}
impl<W: Write> GerberCode<W> for MacroDecimal {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
match *self {
MacroDecimal::Value(ref v) => write!(writer, "{}", v)?,
MacroDecimal::Variable(ref v) => write!(writer, "${}", v)?,
};
Ok(())
}
}
#[derive(Debug, PartialEq)]
pub enum MacroContent {
Circle(CirclePrimitive),
VectorLine(VectorLinePrimitive),
CenterLine(CenterLinePrimitive),
Outline(OutlinePrimitive),
Polygon(PolygonPrimitive),
Moire(MoirePrimitive),
Thermal(ThermalPrimitive),
VariableDefinition(VariableDefinition),
Comment(String),
}
impl<W: Write> GerberCode<W> for MacroContent {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
match *self {
MacroContent::Circle(ref c) => c.to_code(writer)?,
MacroContent::VectorLine(ref vl) => vl.to_code(writer)?,
MacroContent::CenterLine(ref cl) => cl.to_code(writer)?,
MacroContent::Outline(ref o) => o.to_code(writer)?,
MacroContent::Polygon(ref p) => p.to_code(writer)?,
MacroContent::Moire(ref m) => m.to_code(writer)?,
MacroContent::Thermal(ref t) => t.to_code(writer)?,
MacroContent::Comment(ref s) => write!(writer, "0 {}*", &s)?,
MacroContent::VariableDefinition(ref v) => v.to_code(writer)?,
};
Ok(())
}
}
macro_rules! impl_into {
($target:ty, $from:ty, $choice:expr) => {
impl From<$from> for $target {
fn from(val: $from) -> $target {
$choice(val)
}
}
}
}
impl_into!(MacroContent, CirclePrimitive, MacroContent::Circle);
impl_into!(MacroContent, VectorLinePrimitive, MacroContent::VectorLine);
impl_into!(MacroContent, CenterLinePrimitive, MacroContent::CenterLine);
impl_into!(MacroContent, OutlinePrimitive, MacroContent::Outline);
impl_into!(MacroContent, PolygonPrimitive, MacroContent::Polygon);
impl_into!(MacroContent, MoirePrimitive, MacroContent::Moire);
impl_into!(MacroContent, ThermalPrimitive, MacroContent::Thermal);
impl_into!(MacroContent, VariableDefinition, MacroContent::VariableDefinition);
impl<T: Into<String>> From<T> for MacroContent {
fn from(val: T) -> Self {
MacroContent::Comment(val.into())
}
}
#[derive(Debug, PartialEq)]
pub struct CirclePrimitive {
pub exposure: bool,
pub diameter: MacroDecimal,
pub center: (MacroDecimal, MacroDecimal),
pub angle: Option<MacroDecimal>,
}
impl<W: Write> GerberCode<W> for CirclePrimitive {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
write!(writer, "1,")?;
self.exposure.to_code(writer)?;
write!(writer, ",")?;
self.diameter.to_code(writer)?;
write!(writer, ",")?;
self.center.0.to_code(writer)?;
write!(writer, ",")?;
self.center.1.to_code(writer)?;
if let Some(ref a) = self.angle {
write!(writer, ",")?;
a.to_code(writer)?;
}
write!(writer, "*")?;
Ok(())
}
}
#[derive(Debug, PartialEq)]
pub struct VectorLinePrimitive {
pub exposure: bool,
pub width: MacroDecimal,
pub start: (MacroDecimal, MacroDecimal),
pub end: (MacroDecimal, MacroDecimal),
pub angle: MacroDecimal,
}
impl<W: Write> GerberCode<W> for VectorLinePrimitive {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
write!(writer, "20,")?;
self.exposure.to_code(writer)?;
write!(writer, ",")?;
self.width.to_code(writer)?;
write!(writer, ",")?;
self.start.0.to_code(writer)?;
write!(writer, ",")?;
self.start.1.to_code(writer)?;
write!(writer, ",")?;
self.end.0.to_code(writer)?;
write!(writer, ",")?;
self.end.1.to_code(writer)?;
write!(writer, ",")?;
self.angle.to_code(writer)?;
write!(writer, "*")?;
Ok(())
}
}
#[derive(Debug, PartialEq)]
pub struct CenterLinePrimitive {
pub exposure: bool,
pub dimensions: (MacroDecimal, MacroDecimal),
pub center: (MacroDecimal, MacroDecimal),
pub angle: MacroDecimal,
}
impl<W: Write> GerberCode<W> for CenterLinePrimitive {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
write!(writer, "21,")?;
self.exposure.to_code(writer)?;
write!(writer, ",")?;
self.dimensions.0.to_code(writer)?;
write!(writer, ",")?;
self.dimensions.1.to_code(writer)?;
write!(writer, ",")?;
self.center.0.to_code(writer)?;
write!(writer, ",")?;
self.center.1.to_code(writer)?;
write!(writer, ",")?;
self.angle.to_code(writer)?;
write!(writer, "*")?;
Ok(())
}
}
#[derive(Debug, PartialEq)]
pub struct OutlinePrimitive {
pub exposure: bool,
pub points: Vec<(MacroDecimal, MacroDecimal)>,
pub angle: MacroDecimal,
}
impl<W: Write> GerberCode<W> for OutlinePrimitive {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
if self.points.len() < 2 {
return Err(GerberError::MissingDataError("There must be at least 1 subsequent point in an outline".into()));
}
if self.points.len() > 5001 {
return Err(GerberError::RangeError("The maximum number of subsequent points in an outline is 5000".into()));
}
if self.points[0] != self.points[self.points.len() - 1] {
return Err(GerberError::RangeError("The last point must be equal to the first point".into()));
}
write!(writer, "4,")?;
self.exposure.to_code(writer)?;
writeln!(writer, ",{},", self.points.len() - 1)?;
for &(ref x, ref y) in &self.points {
x.to_code(writer)?;
write!(writer, ",")?;
y.to_code(writer)?;
writeln!(writer, ",")?;
}
self.angle.to_code(writer)?;
write!(writer, "*")?;
Ok(())
}
}
#[derive(Debug, PartialEq)]
pub struct PolygonPrimitive {
pub exposure: bool,
pub vertices: u8,
pub center: (MacroDecimal, MacroDecimal),
pub diameter: MacroDecimal,
pub angle: MacroDecimal,
}
impl<W: Write> GerberCode<W> for PolygonPrimitive {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
if self.vertices < 3 {
return Err(GerberError::MissingDataError("There must be at least 3 vertices in a polygon".into()));
}
if self.vertices > 12 {
return Err(GerberError::RangeError("The maximum number of vertices in a polygon is 12".into()));
}
if self.diameter.is_negative() {
return Err(GerberError::RangeError("The diameter must not be negative".into()));
}
write!(writer, "5,")?;
self.exposure.to_code(writer)?;
write!(writer, ",{},", self.vertices)?;
self.center.0.to_code(writer)?;
write!(writer, ",")?;
self.center.1.to_code(writer)?;
write!(writer, ",")?;
self.diameter.to_code(writer)?;
write!(writer, ",")?;
self.angle.to_code(writer)?;
write!(writer, "*")?;
Ok(())
}
}
#[derive(Debug, PartialEq)]
pub struct MoirePrimitive {
pub center: (MacroDecimal, MacroDecimal),
pub diameter: MacroDecimal,
pub ring_thickness: MacroDecimal,
pub gap: MacroDecimal,
pub max_rings: u32,
pub cross_hair_thickness: MacroDecimal,
pub cross_hair_length: MacroDecimal,
pub angle: MacroDecimal,
}
impl<W: Write> GerberCode<W> for MoirePrimitive {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
if self.diameter.is_negative() {
return Err(GerberError::RangeError("Outer diameter of a moiré may not be negative".into()));
}
if self.ring_thickness.is_negative() {
return Err(GerberError::RangeError("Ring thickness of a moiré may not be negative".into()));
}
if self.gap.is_negative() {
return Err(GerberError::RangeError("Gap of a moiré may not be negative".into()));
}
if self.cross_hair_thickness.is_negative() {
return Err(GerberError::RangeError("Cross hair thickness of a moiré may not be negative".into()));
}
if self.cross_hair_length.is_negative() {
return Err(GerberError::RangeError("Cross hair length of a moiré may not be negative".into()));
}
write!(writer, "6,")?;
self.center.0.to_code(writer)?;
write!(writer, ",")?;
self.center.1.to_code(writer)?;
write!(writer, ",")?;
self.diameter.to_code(writer)?;
write!(writer, ",")?;
self.ring_thickness.to_code(writer)?;
write!(writer, ",")?;
self.gap.to_code(writer)?;
write!(writer, ",{},", self.max_rings)?;
self.cross_hair_thickness.to_code(writer)?;
write!(writer, ",")?;
self.cross_hair_length.to_code(writer)?;
write!(writer, ",")?;
self.angle.to_code(writer)?;
write!(writer, "*")?;
Ok(())
}
}
#[derive(Debug, PartialEq)]
pub struct ThermalPrimitive {
pub center: (MacroDecimal, MacroDecimal),
pub outer_diameter: MacroDecimal,
pub inner_diameter: MacroDecimal,
pub gap: MacroDecimal,
pub angle: MacroDecimal,
}
impl<W: Write> GerberCode<W> for ThermalPrimitive {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
if self.inner_diameter.is_negative() {
return Err(GerberError::RangeError("Inner diameter of a thermal may not be negative".into()));
}
write!(writer, "7,")?;
self.center.0.to_code(writer)?;
write!(writer, ",")?;
self.center.1.to_code(writer)?;
write!(writer, ",")?;
self.outer_diameter.to_code(writer)?;
write!(writer, ",")?;
self.inner_diameter.to_code(writer)?;
write!(writer, ",")?;
self.gap.to_code(writer)?;
write!(writer, ",")?;
self.angle.to_code(writer)?;
write!(writer, "*")?;
Ok(())
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct VariableDefinition {
number: u32,
expression: String,
}
impl<W: Write> GerberCode<W> for VariableDefinition {
fn to_code(&self, writer: &mut W) -> GerberResult<()> {
write!(writer, "${}={}*", self.number, self.expression)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use std::io::BufWriter;
use super::*;
use super::MacroDecimal::{Value, Variable};
use ::GerberCode;
macro_rules! assert_code {
($obj:expr, $expected:expr) => {
let mut buf = BufWriter::new(Vec::new());
$obj.to_code(&mut buf).expect("Could not generate Gerber code");
let bytes = buf.into_inner().unwrap();
let code = String::from_utf8(bytes).unwrap();
assert_eq!(&code, $expected);
}
}
#[test]
fn test_circle_primitive_codegen() {
let with_angle = CirclePrimitive {
exposure: true,
diameter: Value(1.5),
center: (Value(0.), Value(0.)),
angle: Some(Value(0.)),
};
assert_code!(with_angle, "1,1,1.5,0,0,0*");
let no_angle = CirclePrimitive {
exposure: false,
diameter: Value(99.9),
center: (Value(1.1), Value(2.2)),
angle: None,
};
assert_code!(no_angle, "1,0,99.9,1.1,2.2*");
}
#[test]
fn test_vector_line_primitive_codegen() {
let line = VectorLinePrimitive {
exposure: true,
width: Value(0.9),
start: (Value(0.), Value(0.45)),
end: (Value(12.), Value(0.45)),
angle: Value(0.),
};
assert_code!(line, "20,1,0.9,0,0.45,12,0.45,0*");
}
#[test]
fn test_center_line_primitive_codegen() {
let line = CenterLinePrimitive {
exposure: true,
dimensions: (Value(6.8), Value(1.2)),
center: (Value(3.4), Value(0.6)),
angle: Value(30.0),
};
assert_code!(line, "21,1,6.8,1.2,3.4,0.6,30*");
}
#[test]
fn test_outline_primitive_codegen() {
let line = OutlinePrimitive {
exposure: true,
points: vec![
(Value(0.1), Value(0.1)),
(Value(0.5), Value(0.1)),
(Value(0.5), Value(0.5)),
(Value(0.1), Value(0.5)),
(Value(0.1), Value(0.1)),
],
angle: Value(0.0),
};
assert_code!(line, "4,1,4,\n0.1,0.1,\n0.5,0.1,\n0.5,0.5,\n0.1,0.5,\n0.1,0.1,\n0*");
}
#[test]
fn test_polygon_primitive_codegen() {
let line = PolygonPrimitive {
exposure: true,
vertices: 8,
center: (Value(1.5), Value(2.0)),
diameter: Value(8.0),
angle: Value(0.0),
};
assert_code!(line, "5,1,8,1.5,2,8,0*");
}
#[test]
fn test_moire_primitive_codegen() {
let line = MoirePrimitive {
center: (Value(0.0), Value(0.0)),
diameter: Value(5.0),
ring_thickness: Value(0.5),
gap: Value(0.5),
max_rings: 2,
cross_hair_thickness: Value(0.1),
cross_hair_length: Value(6.0),
angle: Value(0.0),
};
assert_code!(line, "6,0,0,5,0.5,0.5,2,0.1,6,0*");
}
#[test]
fn test_thermal_primitive_codegen() {
let line = ThermalPrimitive {
center: (Value(0.0), Value(0.0)),
outer_diameter: Value(8.0),
inner_diameter: Value(6.5),
gap: Value(1.0),
angle: Value(45.0),
};
assert_code!(line, "7,0,0,8,6.5,1,45*");
}
#[test]
fn test_aperture_macro_codegen() {
let am = ApertureMacro::new("CRAZY").add_content(
MacroContent::Thermal(
ThermalPrimitive {
center: (Value(0.0), Value(0.0)),
outer_diameter: Value(0.08),
inner_diameter: Value(0.055),
gap: Value(0.0125),
angle: Value(45.0),
}
)
).add_content(
MacroContent::Moire(
MoirePrimitive {
center: (Value(0.0), Value(0.0)),
diameter: Value(0.125),
ring_thickness: Value(0.01),
gap: Value(0.01),
max_rings: 3,
cross_hair_thickness: Value(0.003),
cross_hair_length: Value(0.150),
angle: Value(0.0),
}
)
);
assert_code!(am, "AMCRAZY*\n7,0,0,0.08,0.055,0.0125,45*\n6,0,0,0.125,0.01,0.01,3,0.003,0.15,0*");
}
#[test]
fn test_codegen_with_variable() {
let line = VectorLinePrimitive {
exposure: true,
width: Variable(0),
start: (Variable(1), 0.45.into()),
end: (Value(12.), Variable(2)),
angle: Variable(3),
};
assert_code!(line, "20,1,$0,$1,0.45,12,$2,$3*");
}
#[test]
fn test_macro_decimal_into() {
let a = Value(1.0);
let b: MacroDecimal = 1.0.into();
assert_eq!(a, b);
let c = Variable(1);
let d = Variable(1);
assert_eq!(c, d);
}
#[test]
fn test_comment_codegen() {
let comment = MacroContent::Comment("hello world".to_string());
assert_code!(comment, "0 hello world*");
}
#[test]
fn test_variable_definition_codegen() {
let var = VariableDefinition {
number: 17,
expression: "$40+2".to_string(),
};
assert_code!(var, "$17=$40+2*");
}
#[test]
fn test_macrocontent_from_into() {
let a = MacroContent::Comment("hello".into());
let b: MacroContent = "hello".to_string().into();
let c: MacroContent = "hello".into();
assert_eq!(a, b);
assert_eq!(b, c);
}
}