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
//! PCB net record type.
//!
//! Nets define electrical connectivity and routing properties
//! for connected copper elements on the PCB.
use crate::types::{Color, Coord, Layer, ParameterCollection};
/// PCB net record.
///
/// A net represents an electrical connection between multiple points
/// on the PCB. It stores routing properties like track widths for
/// different layers and visual display settings.
#[derive(Debug, Clone, Default)]
pub struct PcbNet {
/// Net name.
pub name: String,
/// Layer (typically TOP for default).
pub layer: Layer,
/// Whether the net is locked.
pub locked: bool,
/// Whether the net is visible.
pub visible: bool,
/// Whether primitives are locked.
pub primitive_lock: bool,
/// Whether this is a user-routed net.
pub user_routed: bool,
/// Whether this is a keepout net.
pub keepout: bool,
/// Union index (for grouping).
pub union_index: i32,
/// Display color for the net.
pub color: Color,
/// Whether to use loop removal optimization.
pub loop_removal: bool,
/// Whether to override color for drawing.
pub override_color_for_draw: bool,
/// Layer-specific minimum routing widths (indexed by layer).
/// Format: TOPLAYER_MRWIDTH, MIDLAYER1_MRWIDTH, etc.
pub layer_widths: Vec<(String, Coord)>,
/// Unique ID.
pub unique_id: String,
/// All parameters for round-tripping.
pub params: ParameterCollection,
}
impl PcbNet {
/// Parse a net from parameters.
pub fn from_params(params: &ParameterCollection) -> Self {
let mut net = Self {
name: params
.get("NAME")
.map(|v| v.as_str().to_string())
.unwrap_or_default(),
layer: params
.get("LAYER")
.map(|v| v.as_layer())
.unwrap_or_default(),
locked: params
.get("LOCKED")
.map(|v| v.as_bool_or(false))
.unwrap_or(false),
visible: params
.get("VISIBLE")
.map(|v| v.as_bool_or(true))
.unwrap_or(true),
primitive_lock: params
.get("PRIMITIVELOCK")
.map(|v| v.as_bool_or(false))
.unwrap_or(false),
user_routed: params
.get("USERROUTED")
.map(|v| v.as_bool_or(true))
.unwrap_or(true),
keepout: params
.get("KEEPOUT")
.map(|v| v.as_bool_or(false))
.unwrap_or(false),
union_index: params
.get("UNIONINDEX")
.map(|v| v.as_int_or(0))
.unwrap_or(0),
color: params
.get("COLOR")
.and_then(|v| v.as_color().ok())
.unwrap_or_default(),
loop_removal: params
.get("LOOPREMOVAL")
.map(|v| v.as_bool_or(true))
.unwrap_or(true),
override_color_for_draw: params
.get("OVERRIDECOLORFORDRAW")
.map(|v| v.as_bool_or(false))
.unwrap_or(false),
layer_widths: Vec::new(),
unique_id: params
.get("UNIQUEID")
.map(|v| v.as_str().to_string())
.unwrap_or_default(),
params: params.clone(),
};
// Parse layer-specific widths
// TOPLAYER_MRWIDTH, MIDLAYER1_MRWIDTH, ..., MIDLAYER30_MRWIDTH, BOTTOMLAYER_MRWIDTH
for (key, value) in params.iter() {
if key.ends_with("_MRWIDTH") {
if let Ok(width) = value.as_coord() {
let layer_name = key.trim_end_matches("_MRWIDTH").to_string();
net.layer_widths.push((layer_name, width));
}
}
}
net
}
/// Export to parameters.
pub fn to_params(&self) -> ParameterCollection {
let mut params = self.params.clone();
params.add("NAME", &self.name);
params.add("LAYER", &self.layer.to_string());
params.add("LOCKED", if self.locked { "TRUE" } else { "FALSE" });
params.add("VISIBLE", if self.visible { "TRUE" } else { "FALSE" });
params.add(
"PRIMITIVELOCK",
if self.primitive_lock { "TRUE" } else { "FALSE" },
);
params.add(
"USERROUTED",
if self.user_routed { "TRUE" } else { "FALSE" },
);
params.add("KEEPOUT", if self.keepout { "TRUE" } else { "FALSE" });
params.add_int("UNIONINDEX", self.union_index);
params.add_color("COLOR", self.color);
params.add(
"LOOPREMOVAL",
if self.loop_removal { "TRUE" } else { "FALSE" },
);
params.add(
"OVERRIDECOLORFORDRAW",
if self.override_color_for_draw {
"TRUE"
} else {
"FALSE"
},
);
// Write layer widths
for (layer_name, width) in &self.layer_widths {
params.add_coord(&format!("{}_MRWIDTH", layer_name), *width);
}
if !self.unique_id.is_empty() {
params.add("UNIQUEID", &self.unique_id);
}
params
}
/// Get the minimum routing width for a specific layer.
pub fn get_layer_width(&self, layer_name: &str) -> Option<Coord> {
self.layer_widths
.iter()
.find(|(name, _)| name.eq_ignore_ascii_case(layer_name))
.map(|(_, width)| *width)
}
/// Get the top layer minimum routing width.
pub fn top_layer_width(&self) -> Option<Coord> {
self.get_layer_width("TOPLAYER")
}
/// Get the bottom layer minimum routing width.
pub fn bottom_layer_width(&self) -> Option<Coord> {
self.get_layer_width("BOTTOMLAYER")
}
}