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
//! Apply manufacturing presentation before routing and measuring any dimension demand.
use super::{Shape, Want};
use crate::model::{Analysis, Spec};
pub(super) fn apply(analysis: &Analysis, spec: &Spec, wants: &mut [Want]) {
let raw = crate::pmi_lowering::manufacturing(analysis);
let m = crate::pmi_lowering::manufacturing_for(analysis, spec);
for w in wants {
let mut owned: Vec<_> = m
.owned
.iter()
.filter(|o| {
w.measurements
.iter()
.any(|v| v.feature == o.id && v.parameter == o.aspect)
})
.collect();
// A shared requirement can own several holes. Prefer this callout's
// physical owner and state each requirement once, not once per owner.
owned.sort_by_key(|o| !w.measurements.iter().any(|v| v.feature == o.owner));
let mut stated = std::collections::BTreeSet::new();
owned.retain(|o| stated.insert((&o.id, o.aspect)));
if !owned.is_empty() {
let count = w.terms.first().filter(|s| s.ends_with('×')).cloned();
let original = w.terms.join(" ");
let raw_terms = raw.terms(&owned[0].owner).0.join(" ");
let mut riders = String::new();
let mut qualified_bore = None;
let mut physical_diameter = None;
if let Some((physical, suffix)) = original.split_once(&raw_terms) {
if owned.iter().any(|o| o.knurl_values().is_some()) {
// The knurl's note moves manufacturing wording only. The size's
// own PMI band and qualifier must stay beside its diameter.
physical_diameter = Some(
physical
.trim()
.strip_prefix(count.as_deref().unwrap_or(""))
.unwrap_or(physical.trim())
.trim()
.to_string(),
);
}
if owned.iter().any(|o| o.thread().is_some_and(|t| t.internal)) {
let bore = physical
.trim()
.strip_prefix(count.as_deref().unwrap_or(""))
.unwrap_or(physical.trim())
.trim();
let end = bore.find(['⌴', '⌵']).unwrap_or(bore.len());
let bore = bore[..end].trim();
let qualified = bore.split_whitespace().any(|word| {
let value = word.trim_start_matches(['ø', '⌀', 'Ø']);
!matches!(word, "↧" | "THRU") && value.parse::<f64>().is_err()
});
let specified = spec
.features
.get(&owned[0].owner)
.is_some_and(|f| f.fit.is_some())
|| spec
.dimensions
.get(&w.id)
.is_some_and(|d| d.fit.is_some() || d.tolerance.is_some());
let precise = spec.precision.contains_key(&w.id)
|| w.measurements.iter().any(|m| {
matches!(m.parameter.as_str(), "bore.diameter" | "bore.depth")
&& spec
.precision
.contains_key(&format!("d:{}/{}", m.feature, m.parameter))
});
if qualified || specified || precise {
qualified_bore = Some(bore.to_string());
}
}
// Counterbores, spotfaces and countersinks remain independent physical
// requirements. Pattern shape/pitch/face riders follow the manufacturing text.
if let Some(start) = physical.find(['⌴', '⌵']) {
riders += physical[start..].trim();
}
if !suffix.trim().is_empty() {
if !riders.is_empty() {
riders.push(' ');
}
riders += suffix.trim();
}
}
let mut text = owned
.iter()
.map(|o| o.suffix.as_str())
.collect::<Vec<_>>()
.join(" \n ");
if let Some(diameter) = physical_diameter
&& let Some((identity, rest)) = text.split_once(' ')
&& identity.starts_with(['ø', '⌀', 'Ø'])
{
// Keep the manufacturing identity's source precision (e.g. 6.25,
// where the generic geometry label rounds to 6.2). Only a size
// stated as two explicit limits replaces that nominal identity.
let diameter = match diameter.split_once(' ') {
Some((_, band)) if band.starts_with("- ") => diameter,
Some((_, band)) => format!("{identity} {band}"),
None => identity.to_string(),
};
// A common MAX already on the size also qualifies MAX AFTER KNURL.
let rest = if diameter.ends_with(" MAX") {
rest.strip_prefix("MAX ").unwrap_or(rest)
} else {
rest
};
text = format!("{diameter} {rest}");
}
w.terms = text
.split(' ')
.filter(|s| !s.is_empty())
.map(str::to_string)
.collect();
if let Some(bore) = qualified_bore {
if crate::callouts::style(spec, &owned[0].owner) == crate::callouts::Style::Full {
let suffix = raw
.on(&owned[0].owner)
.iter()
.map(|o| o.suffix.as_str())
.collect::<Vec<_>>()
.join("; ");
w.terms = format!("{bore} {suffix}")
.split_whitespace()
.map(str::to_string)
.collect();
} else {
w.terms.truncate(
w.terms
.iter()
.position(|s| s == crate::callouts::BREAK)
.unwrap_or(w.terms.len()),
);
w.terms.push(crate::callouts::BREAK.into());
w.terms.extend(bore.split_whitespace().map(str::to_string));
}
w.basis += "; retains the source tapping-drill fit on its own drill line";
}
if !riders.is_empty() {
w.terms
.extend(riders.split_whitespace().map(str::to_string));
}
if let Some(count) = count {
w.terms.insert(0, count);
}
// Manufacturing requirements always read horizontally, including a lone knurl
// on an overall diameter. Its measured diameter remains claimed by this leader.
if let Shape::Linear { a, b, .. } = w.shape {
let frame = crate::bridge::holes::frame(analysis);
let sites: Vec<_> = owned
.iter()
.filter_map(|o| analysis.features.iter().find(|f| f.id == o.owner))
.filter_map(|f| crate::lint::turned::band(&frame, f).ok())
.flat_map(|band| {
let mut mid = band.on;
mid[band.axis] = (band.lo + band.hi) / 2.0;
(0..3).filter(move |k| *k != band.axis).flat_map(move |k| {
[-0.5 * band.diameter, 0.5 * band.diameter].map(|r| {
let mut p = mid;
p[k] += r;
(p, 0.0)
})
})
})
.collect();
w.shape = Shape::Callout {
arcs: Vec::new(),
sites: if sites.is_empty() {
vec![(a, 0.0), (b, 0.0)]
} else {
sites
},
};
}
for o in owned {
let style = crate::callouts::style(spec, &o.owner);
w.basis += &format!(
"; callouts/style {style:?}: {} {}",
o.source,
o.presentation_reason()
);
}
}
if matches!(w.shape, Shape::Callout { .. }) {
w.terms = crate::callouts::wrap(&w.terms);
}
}
}