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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
use str_error;
use footprint;
use Sexp;
use layout::{Adjust, Bound, BoundingBox};
use std::{fmt, result};
use KicadError;
#[derive(Debug)]
pub struct Layout {
pub version: i64,
pub host: Host,
pub general: General,
pub page: String,
pub setup: Setup,
pub layers: Vec<Layer>,
pub elements: Vec<Element>,
}
#[derive(Clone, Debug)]
pub enum Element {
Module(footprint::Module),
Net(Net),
NetClass(NetClass),
GrText(GrText),
GrLine(GrLine),
GrArc(GrArc),
GrCircle(GrCircle),
Dimension(Dimension),
Segment(Segment),
Via(Via),
Zone(Zone),
Other(Sexp),
}
#[derive(Clone, Debug)]
pub struct Zone {
pub net: i64,
pub net_name: NetName,
pub layer: footprint::Layer,
pub tstamp: String,
pub hatch: Hatch,
pub priority: u64,
pub connect_pads: ConnectPads,
pub min_thickness: f64,
pub keepout: Option<Keepout>,
pub fill: Fill,
pub polygons: Vec<footprint::Pts>,
pub filled_polygons: Vec<footprint::Pts>,
pub fill_segments: Option<footprint::Pts>,
pub other: Vec<Sexp>,
}
impl Adjust for Zone {
fn adjust(&mut self, x: f64, y: f64) {
for p in &mut self.polygons {
p.adjust(x, y)
}
for p in &mut self.filled_polygons {
p.adjust(x, y)
}
for p in &mut self.fill_segments {
p.adjust(x, y)
}
}
}
impl BoundingBox for Zone {
fn bounding_box(&self) -> Bound {
let mut b = Bound::default();
for p in &self.polygons {
b.update(&p.bounding_box());
}
for p in &self.filled_polygons {
b.update(&p.bounding_box());
}
for p in &self.fill_segments {
b.update(&p.bounding_box());
}
b.swap_if_needed();
b
}
}
#[derive(Clone, Debug, Default)]
pub struct Hatch {
pub style: String,
pub pitch: f64,
}
#[derive(Clone, Debug, Default)]
pub struct ConnectPads {
pub connection: Option<String>,
pub clearance: f64,
}
#[derive(Clone, Debug, Default)]
pub struct Keepout {
pub tracks: bool,
pub vias: bool,
pub copperpour: bool,
}
#[derive(Clone, Debug, Default)]
pub struct Fill {
pub filled: bool,
pub segment: bool,
pub arc_segments: i64,
pub thermal_gap: f64,
pub thermal_bridge_width: f64,
pub smoothing: Option<String>,
pub corner_radius: f64,
}
#[derive(Clone, Debug)]
pub struct Host {
pub tool: String,
pub build: String,
}
#[derive(Clone, Debug)]
pub struct General {
pub links: i64,
pub no_connects: i64,
pub area: Area,
pub thickness: f64,
pub drawings: i64,
pub tracks: i64,
pub zones: i64,
pub modules: i64,
pub nets: i64,
}
#[derive(Clone, Debug, Default)]
pub struct Area {
pub x1: f64,
pub y1: f64,
pub x2: f64,
pub y2: f64,
}
#[derive(Clone, Debug)]
pub struct Layer {
pub num: i64,
pub layer: footprint::Layer,
pub layer_type: LayerType,
pub hide: bool,
}
#[derive(Clone, Debug)]
pub enum LayerType {
Signal,
Power,
Mixed,
Jumper,
User,
}
#[derive(Clone, Debug, Default)]
pub struct Setup {
pub elements: Vec<SetupElement>,
pub pcbplotparams: Vec<SetupElement>,
}
#[derive(Clone, Debug)]
pub struct SetupElement {
pub name: String,
pub value1: String,
pub value2: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct NetName(pub String);
impl<'a> From<&'a str> for NetName {
fn from(s: &'a str) -> NetName {
NetName(s.into())
}
}
impl From<String> for NetName {
fn from(s: String) -> NetName {
NetName(s)
}
}
impl NetName {
pub fn replace_block(&mut self, from: &str, to: &str) {
let from_pat = format!("/{}/", from);
let to_pat = format!("/{}/", to);
let n = self.0.replace(&from_pat, &to_pat);
self.0 = n;
}
pub fn rename(&mut self, from: &str, to: &str) {
if self.0 == from {
self.0 = to.into();
}
}
}
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Net {
pub num: i64,
pub name: NetName,
}
#[derive(Clone, PartialEq, Debug)]
pub struct NetClass {
pub name: String,
pub desc: String,
pub clearance: f64,
pub trace_width: f64,
pub via_dia: f64,
pub via_drill: f64,
pub uvia_dia: f64,
pub uvia_drill: f64,
pub diff_pair_gap: Option<f64>,
pub diff_pair_width: Option<f64>,
pub nets: Vec<NetName>,
}
#[derive(Clone, Debug, Default)]
pub struct GrText {
pub value: String,
pub at: footprint::At,
pub layer: footprint::Layer,
pub effects: footprint::Effects,
pub tstamp: Option<String>,
}
impl Adjust for GrText {
fn adjust(&mut self, x: f64, y: f64) {
self.at.adjust(x, y)
}
}
impl BoundingBox for GrText {
fn bounding_box(&self) -> Bound {
debug!("poor bounding box for GrText");
Bound::new(self.at.x, self.at.y, self.at.x, self.at.y)
}
}
#[derive(Clone, Debug, Default)]
pub struct GrLine {
pub start: footprint::Xy,
pub end: footprint::Xy,
pub angle: f64,
pub layer: footprint::Layer,
pub width: f64,
pub tstamp: Option<String>,
}
impl Adjust for GrLine {
fn adjust(&mut self, x: f64, y: f64) {
self.start.adjust(x, y);
self.end.adjust(x, y)
}
}
impl BoundingBox for GrLine {
fn bounding_box(&self) -> Bound {
let x1 = self.start.x.min(self.end.x);
let y1 = self.start.y.min(self.end.y);
let x2 = self.start.x.max(self.end.x);
let y2 = self.start.y.max(self.end.y);
Bound::new(x1, y1, x2, y2)
}
}
#[derive(Clone, Debug, Default)]
pub struct GrArc {
pub start: footprint::Xy,
pub end: footprint::Xy,
pub angle: f64,
pub layer: footprint::Layer,
pub width: f64,
pub tstamp: Option<String>,
}
impl Adjust for GrArc {
fn adjust(&mut self, x: f64, y: f64) {
self.start.adjust(x, y);
self.end.adjust(x, y);
}
}
impl BoundingBox for GrArc {
fn bounding_box(&self) -> Bound {
let x1 = self.start.x.min(self.end.x);
let y1 = self.start.y.min(self.end.y);
let x2 = self.start.x.max(self.end.x);
let y2 = self.start.y.max(self.end.y);
Bound::new(x1, y1, x2, y2)
}
}
#[derive(Clone, Debug, Default)]
pub struct GrCircle {
pub center: footprint::Xy,
pub end: footprint::Xy,
pub layer: footprint::Layer,
pub width: f64,
pub tstamp: Option<String>,
}
impl Adjust for GrCircle {
fn adjust(&mut self, x: f64, y: f64) {
self.center.x += x;
self.center.y += y;
self.end.x += x;
self.end.y += y;
}
}
impl BoundingBox for GrCircle {
fn bounding_box(&self) -> Bound {
let r = (self.center.x - self.end.x) * (self.center.x - self.end.x)
+ (self.center.y - self.end.y) * (self.center.y - self.end.y);
let r = r.sqrt();
Bound::new(
self.center.x - r,
self.center.y - r,
self.center.x + r,
self.center.y + r,
)
}
}
#[derive(Clone, Debug, Default)]
pub struct Dimension {
pub name: String,
pub width: f64,
pub layer: footprint::Layer,
pub tstamp: Option<String>,
pub text: GrText,
pub feature1: footprint::Pts,
pub feature2: footprint::Pts,
pub crossbar: footprint::Pts,
pub arrow1a: footprint::Pts,
pub arrow1b: footprint::Pts,
pub arrow2a: footprint::Pts,
pub arrow2b: footprint::Pts,
}
impl Adjust for Dimension {
fn adjust(&mut self, x: f64, y: f64) {
self.text.adjust(x, y);
self.feature1.adjust(x, y);
self.feature2.adjust(x, y);
self.crossbar.adjust(x, y);
self.arrow1a.adjust(x, y);
self.arrow1b.adjust(x, y);
self.arrow2a.adjust(x, y);
self.arrow2b.adjust(x, y)
}
}
impl BoundingBox for Dimension {
fn bounding_box(&self) -> Bound {
let mut b = Bound::default();
b.update(&self.text.bounding_box());
b.update(&self.feature1.bounding_box());
b.update(&self.feature2.bounding_box());
b.update(&self.crossbar.bounding_box());
b.update(&self.arrow1a.bounding_box());
b.update(&self.arrow1b.bounding_box());
b.update(&self.arrow2a.bounding_box());
b.update(&self.arrow2b.bounding_box());
b
}
}
#[derive(Clone, Debug, Default)]
pub struct Segment {
pub start: footprint::Xy,
pub end: footprint::Xy,
pub width: f64,
pub layer: footprint::Layer,
pub net: i64,
pub tstamp: Option<String>,
pub status: Option<String>,
}
impl Adjust for Segment {
fn adjust(&mut self, x: f64, y: f64) {
self.start.adjust(x, y);
self.end.adjust(x, y)
}
}
impl BoundingBox for Segment {
fn bounding_box(&self) -> Bound {
let x1 = self.start.x.min(self.end.x);
let y1 = self.start.y.min(self.end.y);
let x2 = self.start.x.max(self.end.x);
let y2 = self.start.y.max(self.end.y);
Bound::new(x1, y1, x2, y2)
}
}
#[derive(Clone, Debug, Default)]
pub struct Via {
pub blind: bool,
pub micro: bool,
pub at: footprint::At,
pub size: f64,
pub drill: f64,
pub layers: footprint::Layers,
pub net: i64,
}
impl Adjust for Via {
fn adjust(&mut self, x: f64, y: f64) {
self.at.adjust(x, y);
}
}
impl BoundingBox for Via {
fn bounding_box(&self) -> Bound {
let x1 = self.at.x - self.size;
let y1 = self.at.y - self.size;
let x2 = self.at.x + self.size;
let y2 = self.at.y + self.size;
Bound::new(x1, y1, x2, y2)
}
}
impl Default for Layout {
fn default() -> Layout {
Layout {
version: 4,
host: Host::default(),
elements: vec![],
setup: Setup::default(),
general: General::default(),
page: String::from("A4"),
layers: vec![],
}
}
}
impl Adjust for Layout {
fn adjust(&mut self, x: f64, y: f64) {
for e in &mut self.elements {
e.adjust(x, y)
}
}
}
impl BoundingBox for Layout {
fn bounding_box(&self) -> Bound {
let mut bound = Bound::default();
for e in &self.elements {
bound.update(&e.bounding_box());
}
bound.swap_if_needed();
bound
}
}
impl Layout {
pub fn nets(&self) -> Vec<&Net> {
let mut v = vec![];
for element in &self.elements {
if let Element::Net(ref net) = *element {
v.push(net)
}
}
v
}
pub fn change_net_name(&mut self, old_name: &str, new_name: &str) {
let update = |name: &mut NetName| {
name.rename(old_name, new_name);
Ok(())
};
self.update_net_names(update).unwrap();
}
pub fn update_net_names<F>(&mut self, update: F) -> Result<(), KicadError>
where
F: Fn(&mut NetName) -> Result<(), KicadError>,
{
for element in &mut self.elements {
match *element {
Element::Net(ref mut net) => {
update(&mut net.name)?;
}
Element::NetClass(ref mut nc) => for name in &mut nc.nets {
update(name)?;
},
Element::Module(ref mut module) => for m_e in &mut module.elements {
if let footprint::Element::Pad(ref mut pad) = *m_e {
if let Some(ref mut net) = pad.net {
update(&mut net.name)?;
}
}
},
Element::Zone(ref mut zone) => {
update(&mut zone.net_name)?;
}
_ => (),
}
}
Ok(())
}
pub fn netclasses(&self) -> Vec<&NetClass> {
let mut v = vec![];
for element in &self.elements {
if let Element::NetClass(ref net_class) = *element {
v.push(net_class)
}
}
v
}
pub fn get_module(&self, reference: &str) -> Option<&footprint::Module> {
for x in &self.elements[..] {
match *x {
Element::Module(ref m) => if m.is_reference_with_name(reference) {
return Some(m);
},
Element::Via(_) |
Element::Segment(_) |
Element::Net(_) |
Element::NetClass(_) |
Element::GrText(_) |
Element::GrLine(_) |
Element::GrArc(_) |
Element::GrCircle(_) |
Element::Dimension(_) |
Element::Zone(_) |
Element::Other(_) => (),
}
}
None
}
pub fn get_modules(&self) -> Vec<&footprint::Module> {
let mut v = vec![];
for e in &self.elements {
if let Element::Module(ref m) = *e {
v.push(m);
}
}
v
}
pub fn modify_module<F>(&mut self, reference: &str, fun: F) -> Result<(), KicadError>
where
F: Fn(&mut footprint::Module) -> (),
{
for x in &mut self.elements {
match *x {
Element::Module(ref mut m) => if m.is_reference_with_name(reference) {
return Ok(fun(m));
},
Element::Via(_) |
Element::Segment(_) |
Element::Net(_) |
Element::NetClass(_) |
Element::GrText(_) |
Element::GrLine(_) |
Element::GrArc(_) |
Element::GrCircle(_) |
Element::Dimension(_) |
Element::Zone(_) |
Element::Other(_) => (),
}
}
str_error(format!("did not find module with reference {}", reference))
}
pub fn add_net(&mut self, num: i64, name: &'static str) {
self.elements.push(Element::Net(Net {
num: num,
name: name.into(),
}));
}
pub fn add_netclass(
&mut self,
name: &'static str,
desc: &'static str,
clearance: f64,
trace_width: f64,
via_dia: f64,
via_drill: f64,
uvia_dia: f64,
uvia_drill: f64,
diff_pair_gap: Option<f64>,
diff_pair_width: Option<f64>,
nets: Vec<String>,
) {
self.elements.push(Element::NetClass(NetClass {
name: String::from(name),
desc: String::from(desc),
clearance: clearance,
trace_width: trace_width,
via_dia: via_dia,
via_drill: via_drill,
uvia_dia: uvia_dia,
uvia_drill: uvia_drill,
diff_pair_gap: diff_pair_gap,
diff_pair_width: diff_pair_width,
nets: nets.into_iter().map(|x| x.into()).collect(),
}));
}
}
impl Default for Host {
fn default() -> Host {
Host {
tool: String::from("pcbnew"),
build: String::from("custom"),
}
}
}
impl Default for General {
fn default() -> General {
General {
links: 0,
no_connects: 0,
area: Area::default(),
thickness: 1.6,
drawings: 0,
tracks: 0,
zones: 0,
modules: 0,
nets: 0,
}
}
}
impl NetClass {
pub fn equal_no_net(&self, other: &NetClass) -> bool {
let mut s1 = self.clone();
s1.nets = vec![];
let mut s2 = other.clone();
s2.nets = vec![];
s1 == s2
}
pub fn has_net(&self, name: &'static str) -> bool {
for net in &self.nets {
if &net.0[..] == name {
return true;
}
}
false
}
}
impl Setup {
pub fn get(&self, s: &str) -> Option<&String> {
for element in &self.elements {
if element.name[..] == s[..] {
return Some(&element.value1);
}
}
None
}
pub fn update_element(&mut self, name: &'static str, value: String) {
for element in &mut self.elements {
if &element.name[..] == name {
element.value1 = value;
return;
}
}
self.elements.push(SetupElement {
name: String::from(name),
value1: value,
value2: None,
})
}
}
impl Adjust for Element {
fn adjust(&mut self, x: f64, y: f64) {
match *self {
Element::Module(ref mut e) => e.adjust(x, y),
Element::GrText(ref mut e) => e.adjust(x, y),
Element::GrLine(ref mut e) => e.adjust(x, y),
Element::GrArc(ref mut e) => e.adjust(x, y),
Element::GrCircle(ref mut e) => e.adjust(x, y),
Element::Dimension(ref mut e) => e.adjust(x, y),
Element::Segment(ref mut e) => e.adjust(x, y),
Element::Via(ref mut e) => e.adjust(x, y),
Element::Zone(ref mut e) => e.adjust(x, y),
Element::Net(_) | Element::NetClass(_) | Element::Other(_) => (),
}
}
}
impl BoundingBox for Element {
fn bounding_box(&self) -> Bound {
match *self {
Element::Module(ref e) => e.bounding_box(),
Element::GrText(ref e) => e.bounding_box(),
Element::GrLine(ref e) => e.bounding_box(),
Element::GrArc(ref e) => e.bounding_box(),
Element::GrCircle(ref e) => e.bounding_box(),
Element::Dimension(ref e) => e.bounding_box(),
Element::Segment(ref e) => e.bounding_box(),
Element::Via(ref e) => e.bounding_box(),
Element::Zone(ref e) => e.bounding_box(),
Element::Net(_) | Element::NetClass(_) | Element::Other(_) => Bound::default(),
}
}
}
impl NetName {
pub fn is_unnamed_net(&self) -> Option<(String, String)> {
if self.0.starts_with("Net-(") {
let v: Vec<&str> = self.0.split(|c| c == '-' || c == '(' || c == ')').collect();
if v.len() != 5 {
None
} else {
Some((v[2].into(), v[3].into()))
}
} else {
None
}
}
pub fn set_unnamed_net(&mut self, new_name: &str) -> Result<(), KicadError> {
if let Some((_, pad)) = self.is_unnamed_net() {
self.0 = format!("Net-({}-{})", new_name, pad);
Ok(())
} else {
Err(format!("net is not an unnamed net: {}", self.0).into())
}
}
}
impl fmt::Display for NetName {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
write!(f, "{}", self.0)
}
}
#[test]
fn test_is_unnamed_net() {
let n = Net {
num: 1,
name: "HELLO".into(),
};
assert_eq!(n.name.is_unnamed_net(), None);
let n = Net {
num: 1,
name: "Net-(L1-Pad1)".into(),
};
assert_eq!(n.name.is_unnamed_net(), Some(("L1".into(), "Pad1".into())));
}
#[test]
fn test_unnamed_rename() {
let mut n = Net {
num: 1,
name: "Net-(L1-Pad1)".into(),
};
n.name.set_unnamed_net("L101").unwrap();
assert_eq!(
n.name.is_unnamed_net(),
Some(("L101".into(), "Pad1".into()))
);
}