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
//! Components which compose a panel.

use dyn_clone::DynClone;
use geo::{Coordinate, MultiPolygon};
use std::fmt;

mod array;
mod circle;
mod pos;
mod rect;
pub mod repeating;
mod screw_hole;
mod unit;
pub use array::Column;
pub use circle::Circle;
pub use pos::{AtPos, Positioning};
pub use rect::Rect;
pub use screw_hole::ScrewHole;
pub use unit::Unit;

/// Specifies geometry interior to the bounds of the panel.
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()
    }
}

/// A top-level unit that makes up the geometry of the panel.
pub trait Feature: fmt::Display + DynClone + fmt::Debug {
    /// Human-readable name describing the construction.
    fn name(&self) -> &'static str;
    /// Adjust all coordinates by the specified amount. Should
    /// affect all geometries returned from [`Feature::edge_union`],
    /// [`Feature::edge_subtract`], and [`Feature::interior`].
    fn translate(&mut self, v: Coordinate<f64>);

    /// Returns the outer geometry describing the boundaries of the
    /// panel, which should be unioned with the outer geometry of all
    /// other features.
    fn edge_union(&self) -> Option<MultiPolygon<f64>>;

    /// Returns the inner geometry describing features on the panel,
    /// within the bounds of the computed edge geometry.
    fn interior(&self) -> Vec<InnerAtom>;

    /// Returns the outer geometry describing the boundaries of the
    /// panel, which should be subtracted from the outer geometry of all
    /// other features.
    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()
    }
}

/// The smallest geometries from which inner features are composed.
#[derive(Debug, Clone)]
pub enum InnerAtom {
    Drill {
        center: Coordinate<f64>,
        radius: f64,
        plated: bool,
    },
    Circle {
        center: Coordinate<f64>,
        radius: f64,
        layer: super::Layer,
    },
}

impl InnerAtom {
    pub fn stroke(&self) -> Option<usvg::Stroke> {
        match self {
            // InnerAtom::Circle { layer, .. } => Some(usvg::Stroke {
            //     paint: usvg::Paint::Color(layer.color()),
            //     width: usvg::StrokeWidth::new(0.1),
            //     opacity: usvg::Opacity::new(0.5),
            //     ..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()
            }),
        }
    }

    pub fn bounds(&self) -> geo::Rect<f64> {
        match self {
            InnerAtom::Drill { center, radius, .. } => 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, .. } => geo::Rect::new(
                Coordinate {
                    x: center.x - radius,
                    y: center.y - radius,
                },
                Coordinate {
                    x: center.x + radius,
                    y: center.y + radius,
                },
            ),
        }
    }

    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 };
            }
        }
    }
}