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
use dyn_clone::DynClone;
use geo::{Coordinate, MultiPolygon};
use std::fmt;
mod array;
mod circle;
mod mechanical_solder_point;
mod pos;
mod r_mount;
mod rect;
pub mod repeating;
mod screw_hole;
mod smiley;
mod triangle;
mod unit;
pub use array::Column;
pub use circle::Circle;
pub use mechanical_solder_point::MechanicalSolderPoint;
pub use pos::{AtPos, Positioning};
pub use r_mount::RMount;
pub use rect::Rect;
pub use screw_hole::ScrewHole;
pub use smiley::Smiley;
pub use triangle::Triangle;
pub use unit::Unit;
pub trait InnerFeature: fmt::Display + DynClone + fmt::Debug {
fn name(&self) -> &'static str;
fn translate(&mut self, v: Coordinate<f64>);
fn atoms(&self) -> Vec<InnerAtom>;
}
dyn_clone::clone_trait_object!(InnerFeature);
impl<'a> InnerFeature for Box<dyn InnerFeature + 'a> {
fn name(&self) -> &'static str {
self.as_ref().name()
}
fn translate(&mut self, v: Coordinate<f64>) {
self.as_mut().translate(v)
}
fn atoms(&self) -> Vec<InnerAtom> {
self.as_ref().atoms()
}
}
pub trait Feature: fmt::Display + DynClone + fmt::Debug {
fn name(&self) -> &'static str;
fn translate(&mut self, v: Coordinate<f64>);
fn edge_union(&self) -> Option<MultiPolygon<f64>>;
fn interior(&self) -> Vec<InnerAtom>;
fn edge_subtract(&self) -> Option<MultiPolygon<f64>> {
None
}
}
dyn_clone::clone_trait_object!(Feature);
impl<'a> Feature for Box<dyn Feature + 'a> {
fn name(&self) -> &'static str {
self.as_ref().name()
}
fn translate(&mut self, v: Coordinate<f64>) {
self.as_mut().translate(v)
}
fn edge_union(&self) -> Option<MultiPolygon<f64>> {
self.as_ref().edge_union()
}
fn interior(&self) -> Vec<InnerAtom> {
self.as_ref().interior()
}
fn edge_subtract(&self) -> Option<MultiPolygon<f64>> {
self.as_ref().edge_subtract()
}
}
#[derive(Debug, Clone)]
pub enum InnerAtom {
Drill {
center: Coordinate<f64>,
radius: f64,
plated: bool,
},
Circle {
center: Coordinate<f64>,
radius: f64,
layer: super::Layer,
},
Rect {
rect: geo::Rect<f64>,
layer: super::Layer,
},
VScoreH(f64),
VScoreV(f64),
}
impl InnerAtom {
pub fn stroke(&self) -> Option<usvg::Stroke> {
match self {
InnerAtom::VScoreH(_) | InnerAtom::VScoreV(_) => Some(usvg::Stroke {
paint: usvg::Paint::Color(usvg::Color::new(0x6, 0x6, 0x6)),
width: usvg::StrokeWidth::new(0.1),
opacity: usvg::Opacity::new(0.5),
dasharray: Some(vec![0.8, 0.8]),
..usvg::Stroke::default()
}),
_ => None,
}
}
pub fn fill(&self) -> Option<usvg::Fill> {
match self {
InnerAtom::Drill { .. } => Some(usvg::Fill {
paint: usvg::Paint::Color(usvg::Color::new(0x25, 0x25, 0x25)),
..usvg::Fill::default()
}),
InnerAtom::Circle { layer, .. } => Some(usvg::Fill {
paint: usvg::Paint::Color(layer.color()),
..usvg::Fill::default()
}),
InnerAtom::Rect { layer, .. } => Some(usvg::Fill {
paint: usvg::Paint::Color(layer.color()),
..usvg::Fill::default()
}),
InnerAtom::VScoreH(_) | InnerAtom::VScoreV(_) => None,
}
}
pub fn bounds(&self) -> Option<geo::Rect<f64>> {
match self {
InnerAtom::Drill { center, radius, .. } => Some(geo::Rect::new(
Coordinate {
x: center.x - radius,
y: center.y - radius,
},
Coordinate {
x: center.x + radius,
y: center.y + radius,
},
)),
InnerAtom::Circle { center, radius, .. } => Some(geo::Rect::new(
Coordinate {
x: center.x - radius,
y: center.y - radius,
},
Coordinate {
x: center.x + radius,
y: center.y + radius,
},
)),
InnerAtom::Rect { rect, .. } => Some(rect.clone()),
InnerAtom::VScoreH(_) | InnerAtom::VScoreV(_) => None,
}
}
pub fn translate(&mut self, x: f64, y: f64) {
match self {
InnerAtom::Drill { ref mut center, .. } => {
*center = *center + Coordinate { x, y };
}
InnerAtom::Circle { center, .. } => {
*center = *center + Coordinate { x, y };
}
InnerAtom::Rect { rect, .. } => {
use geo::algorithm::translate::Translate;
rect.translate_inplace(x, y);
}
InnerAtom::VScoreH(ref mut y2) => {
*y2 = *y2 + y;
}
InnerAtom::VScoreV(ref mut x2) => {
*x2 = *x2 + x;
}
}
}
}