draftwright 0.6.0-alpha.3

Dimensioned engineering drawings from STEP, with SVG, PDF and editable DXF output.
Documentation
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
//! Hole-pattern location and spacing, from physical members and printed measurement claims.
//! A proven grid is located at its centre; other arrays need one physical member anchor.

use super::{LintIssue, Mark, holes};
use crate::bridge::holes::{self as geometry, frame, point, to_part};
use crate::model::Analysis;

const TOL: f64 = 0.01;
// Model/recognition coordinates are rounded to 0.001 mm.
const CENTRE_TOL: f64 = 1e-3;

/// A shared centre is evidence only in a true-shape view that actually draws the
/// round outline, the pattern's pitch circle and their common centre mark.
pub(crate) fn concentric_on_drawing(
    a: &Analysis,
    drawing: &crate::output::Drawing,
) -> std::collections::BTreeMap<String, std::collections::BTreeSet<&'static str>> {
    use crate::output::{Curve, Kind};
    let mut located = std::collections::BTreeMap::<_, std::collections::BTreeSet<_>>::new();
    let Some(ink) = &a.edge_ink else {
        return located;
    };
    for p in a.recognition.iter().flat_map(|r| &r.patterns) {
        if p.arrangement != "bolt_circle" {
            continue;
        }
        let Some(c) = point(p.parameters.get("center")).map(|c| to_part(&frame(a), c)) else {
            continue;
        };
        let Some(diameter) = p.parameters.get("diameter").and_then(|v| v.as_f64()) else {
            continue;
        };
        for (name, f) in &drawing.sheet.frames {
            let Some(axis) = (0..3).find(|&k| f.direction[k].abs() > 1. - 1e-8) else {
                continue;
            };
            let across: Vec<_> = (0..3).filter(|&k| k != axis).collect();
            // Recognition rounds model coordinates to 0.001 mm.
            // Both extrema must be tangents of this cylinder within that tolerance. A round boss
            // elsewhere, or mere coincidence with the bounding-box centre, proves nothing.
            let radius = ink
                .curved
                .iter()
                .filter_map(|s| s.cylinder.as_ref())
                .find_map(|s| {
                    (s.axis[axis].abs() > 1. - 1e-8
                        && across.iter().all(|&k| {
                            (s.origin[k] - c[k]).abs() <= CENTRE_TOL
                                && (s.origin[k] - s.radius - a.envelope[0][k]).abs() <= CENTRE_TOL
                                && (s.origin[k] + s.radius - a.envelope[1][k]).abs() <= CENTRE_TOL
                        }))
                    .then_some(s.radius * f.scale)
                });
            let Some(radius) = radius else { continue };
            let centre = f.at;
            // The printed pitch circle also uses the rounded recognition centre.
            let paper_tol = (CENTRE_TOL * f.scale.abs()).max(1e-6);
            let near = |p: [f64; 2], q: [f64; 2]| (p[0] - q[0]).hypot(p[1] - q[1]) <= paper_tol;
            let outline = drawing
                .sheet
                .views
                .iter()
                .filter(|v| v.name == format!("v:{name}"))
                .flat_map(|v| &v.edges)
                .any(|edge| {
                    matches!(edge,
                    Curve::Conic { centre: c, radii, t0, t1, .. }
                        if near(*c, centre) && radii.iter().all(|r| (*r - radius).abs() <= paper_tol)
                            && ((t1 - t0).abs() - std::f64::consts::TAU).abs() < 1e-6)
                });
            let marked = drawing
                .sheet
                .centre_marks
                .get(name)
                .is_some_and(|marks| marks.iter().any(|c| near(*c, centre)));
            let pitch = diameter * f.scale / 2.;
            let ring = drawing.sheet.items.iter().any(|item| {
                if item.kind != Kind::Centreline
                    || item.name != format!("ink:v:{name}:{}", p.id)
                    || item.lines.len() < 3
                {
                    return false;
                }
                let on_circle = item.lines.iter().flatten().all(|q| {
                    ((q[0] - centre[0]).hypot(q[1] - centre[1]) - pitch).abs() <= paper_tol
                });
                let closed = item
                    .lines
                    .iter()
                    .zip(item.lines.iter().cycle().skip(1))
                    .all(|(a, b)| (a[1][0] - b[0][0]).hypot(a[1][1] - b[0][1]) < 1e-6);
                let sweep: f64 = item
                    .lines
                    .iter()
                    .map(|[a, b]| {
                        let a = [a[0] - centre[0], a[1] - centre[1]];
                        let b = [b[0] - centre[0], b[1] - centre[1]];
                        (a[0] * b[1] - a[1] * b[0])
                            .atan2(a[0] * b[0] + a[1] * b[1])
                            .abs()
                    })
                    .sum();
                on_circle && closed && (sweep - std::f64::consts::TAU).abs() < 1e-6
            });
            if outline && marked && ring {
                for k in &across {
                    located.entry(p.id.clone()).or_default().insert(
                        [
                            "location.length.x",
                            "location.length.y",
                            "location.length.z",
                        ][*k],
                    );
                }
            }
        }
    }
    located
}

fn stations(centres: &[[f64; 3]], axis: usize) -> Vec<f64> {
    let mut values: Vec<f64> = centres.iter().map(|c| c[axis]).collect();
    values.sort_by(f64::total_cmp);
    values.dedup_by(|a, b| (*a - *b).abs() <= TOL);
    values
}

fn even(values: &[f64]) -> bool {
    values.len() < 3
        || values
            .windows(2)
            .all(|w| ((w[1] - w[0]) - (values[1] - values[0])).abs() <= TOL)
}

/// A repeated pitch prints the number of physical intervals, never the member count.
fn pitch_states(text: &str, pitch: f64, intervals: usize) -> bool {
    match text.split_once('×') {
        Some((count, nominal)) => {
            count.trim().parse::<usize>() == Ok(intervals) && holes::states(nominal, pitch)
        }
        None => intervals == 1 && holes::states(text, pitch),
    }
}

/// Exact pattern claims or truthful physical-member ordinates prove location and spacing.
pub fn coverage(analysis: &Analysis, marks: &[Mark]) -> Vec<LintIssue> {
    let read: Vec<_> = geometry::read(analysis)
        .into_iter()
        .filter_map(|(_, h)| h.ok())
        .collect();
    let refs: Vec<_> = read.iter().collect();
    let bosses = geometry::bosses(analysis);
    let mut issues = Vec::new();
    let Some(recognition) = &analysis.recognition else {
        return issues;
    };
    for p in &recognition.patterns {
        let Ok(members) = geometry::established_with(p, &refs, &bosses) else {
            continue;
        };
        let Some((axis, _)) = read[members[0]].principal else {
            continue;
        };
        let axes: Vec<_> = (0..3).filter(|&k| k != axis).collect();
        let (u, v) = (axes[0], axes[1]);
        let centres: Vec<_> = members.iter().map(|&i| read[i].at).collect();
        let cols = stations(&centres, u);
        let rows = stations(&centres, v);
        let lattice = p.arrangement == "grid"
            && cols.len() * rows.len() == centres.len()
            && even(&cols)
            && even(&rows)
            && cols.iter().all(|x| {
                rows.iter().all(|y| {
                    centres
                        .iter()
                        .filter(|c| (c[u] - x).abs() <= TOL && (c[v] - y).abs() <= TOL)
                        .count()
                        == 1
                })
            });
        let mut anchors = centres.clone();
        let mut requirements: Vec<(String, Option<f64>)> = Vec::new();
        if p.arrangement == "bolt_circle" {
            anchors = point(p.parameters.get("center"))
                .map(|c| vec![to_part(&frame(analysis), c)])
                .unwrap_or_default();
            requirements.push((
                "bolt_circle.diameter".into(),
                p.parameters.get("diameter").and_then(|d| d.as_f64()),
            ));
        } else if lattice {
            let mut centre = centres[0];
            centre[u] = (cols[0] + cols[cols.len() - 1]) / 2.0;
            centre[v] = (rows[0] + rows[rows.len() - 1]) / 2.0;
            anchors = vec![centre];
            for (name, values) in [("col", &cols), ("row", &rows)] {
                if values.len() > 1 {
                    requirements.push((
                        format!("grid_pitch.length.{name}"),
                        Some(values[1] - values[0]),
                    ));
                }
            }
        } else if p.arrangement == "linear" {
            let pitch = if rows.len() == 1 && even(&cols) && cols.len() > 1 {
                Some(cols[1] - cols[0])
            } else if cols.len() == 1 && even(&rows) && rows.len() > 1 {
                Some(rows[1] - rows[0])
            } else {
                p.parameters.get("pitch").and_then(|x| x.as_f64())
            };
            requirements.push(("pitch.length".into(), pitch));
        } else {
            for name in ["col", "row"] {
                requirements.push((format!("grid_pitch.length.{name}"), None));
            }
        }
        let spacing_stated = |parameter: &str, text: &str, value: f64| {
            let intervals = match parameter {
                "grid_pitch.length.col" => Some(cols.len().saturating_sub(1)),
                "grid_pitch.length.row" => Some(rows.len().saturating_sub(1)),
                "pitch.length" if rows.len() == 1 => Some(cols.len().saturating_sub(1)),
                "pitch.length" if cols.len() == 1 => Some(rows.len().saturating_sub(1)),
                "pitch.length" => Some(centres.len().saturating_sub(1)),
                _ => None,
            };
            intervals.map_or_else(
                || holes::states(text, value),
                |n| pitch_states(text, value, n),
            )
        };
        let locations = |printed_only: bool| -> Vec<_> {
            members
                .iter()
                .map(|&i| {
                    [u, v].map(|k| {
                        let distance = read[i].at[k] - analysis.envelope[0][k];
                        let parameter = format!("location.length.{}", ["x", "y", "z"][k]);
                        distance.abs() <= 1e-6
                            || marks
                                .iter()
                                .filter(|m| {
                                    !printed_only || !m.name.starts_with("location_network:")
                                })
                                .any(|m| {
                                    m.claims.iter().any(|c| {
                                        c.feature == read[i].feature.id && c.parameter == parameter
                                    }) && m.states_text(&m.text, distance)
                                })
                    })
                })
                .collect()
        };
        let member_locations = locations(false);
        let printed_member_locations = locations(true);
        let all_members_located = member_locations.iter().all(|axes| axes.iter().all(|b| *b));
        let member_anchor_with_pitch = member_locations.iter().any(|axes| axes.iter().all(|b| *b))
            && requirements.iter().all(|(parameter, value)| {
                value.is_some_and(|d| {
                    marks.iter().any(|m| {
                        m.claims
                            .iter()
                            .any(|c| c.feature == p.id && c.parameter == *parameter)
                            && spacing_stated(parameter, &m.text, m.units.from_mm(d))
                    })
                })
            });
        let member_spacing = p.arrangement == "linear" || p.arrangement == "grid";
        // Stream K6 (ADR4-U1): a pitch along an axis on which every member is also located
        // states a separation the locations already fix: over-dimensioning that is neither
        // identical nor coincident. Each such pitch is reported, naming its marks.
        if member_spacing {
            for (position, k) in [u, v].into_iter().enumerate() {
                let along = if position == 0 { &cols } else { &rows };
                if along.len() < 2 || !printed_member_locations.iter().all(|axes| axes[position]) {
                    continue;
                }
                let parameter = if p.arrangement == "grid" {
                    format!("grid_pitch.length.{}", ["col", "row"][position])
                } else if (position == 0) == (rows.len() == 1) {
                    "pitch.length".to_string()
                } else {
                    continue;
                };
                let pitch = along[1] - along[0];
                let Some(mark) = marks.iter().find(|m| {
                    m.claims
                        .iter()
                        .any(|c| c.feature == p.id && c.parameter == parameter)
                        && m.states_text(&m.text, pitch)
                }) else {
                    continue;
                };
                let mut issue = super::uncovered(
                    analysis,
                    "over_dimensioned_pattern",
                    &p.id,
                    &parameter,
                    format!(
                        "{} states its pitch along {} and every member's {} location too; the locations fix the pitch, so one of them is redundant.",
                        p.id,
                        ["x", "y", "z"][k],
                        ["x", "y", "z"][k]
                    ),
                );
                issue.annotation = Some(mark.name.clone());
                issue.related = p.members.clone();
                issue.fix = Some(format!(
                    "Omit the repeated {} dimension, or locate the pattern once and keep its pitch.",
                    parameter.replace(['_', '.'], " ")
                ));
                issues.push(issue);
            }
        }
        for (position, k) in [u, v].into_iter().enumerate() {
            if member_spacing
                && (member_locations.iter().all(|axes| axes[position]) || member_anchor_with_pitch)
            {
                continue;
            }

            let parameter = format!("location.length.{}", ["x", "y", "z"][k]);
            let distances: Vec<_> = anchors
                .iter()
                .map(|c| c[k] - analysis.envelope[0][k])
                .collect();
            // A zero datum distance needs no numeric ordinate, as in the location backstop.
            if distances.iter().any(|d| d.abs() <= 1e-6) {
                continue;
            }
            let claiming: Vec<_> = marks
                .iter()
                .filter(|m| {
                    m.claims
                        .iter()
                        .any(|c| c.feature == p.id && c.parameter == parameter)
                })
                .collect();
            if claiming
                .iter()
                .any(|m| distances.iter().any(|d| m.states_text(&m.text, *d)))
            {
                continue;
            }
            let mut issue = super::uncovered(
                analysis,
                "hole_pattern_requirement_missing",
                &p.id,
                &parameter,
                format!(
                    "{} has no proven {} location of its {} on the drawing.",
                    p.id,
                    ["x", "y", "z"][k],
                    if lattice || p.arrangement == "bolt_circle" {
                        "centre"
                    } else {
                        "physical member anchor"
                    }
                ),
            );
            issue.related = p.members.clone();
            issues.push(issue);
        }
        for (parameter, value) in requirements {
            // Fully located members state their separations without a redundant pitch label.
            let pitch_axis = match parameter.as_str() {
                "grid_pitch.length.col" => Some(0),
                "grid_pitch.length.row" => Some(1),
                "pitch.length" if rows.len() == 1 => Some(0),
                "pitch.length" if cols.len() == 1 => Some(1),
                _ => None,
            };
            if member_spacing
                && (all_members_located
                    || pitch_axis
                        .is_some_and(|axis| member_locations.iter().all(|axes| axes[axis])))
            {
                continue;
            }
            let claiming: Vec<_> = marks
                .iter()
                .filter(|m| {
                    m.claims
                        .iter()
                        .any(|c| c.feature == p.id && c.parameter == parameter)
                })
                .collect();
            if value.is_some_and(|d| {
                claiming
                    .iter()
                    .any(|m| spacing_stated(&parameter, &m.text, m.units.from_mm(d)))
            }) {
                continue;
            }
            let code = if value.is_none() || !claiming.is_empty() {
                "hole_pattern_requirement_unverifiable"
            } else {
                "hole_pattern_requirement_missing"
            };
            let mut issue = super::uncovered(
                analysis,
                code,
                &p.id,
                &parameter,
                format!(
                    "{}: {parameter} is not proved by the printed drawing.",
                    p.id
                ),
            );
            issue.related = p.members.clone();
            issue.annotation = claiming.first().map(|m| m.name.clone());
            issue.evidence = value.map(|d| format!("Expected {}", super::parameters::fmt(d)));
            issues.push(issue);
        }
    }
    issues
}