draftwright 0.6.0-alpha.1

Automated technical drawings from STEP B-rep: a Rust port of draftwright.
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Spec-authored quantities use the ordinary Want -> lanes -> placement -> ink pipeline.
use super::{Measurement, Shape, Want, fmt};
use crate::model::dimension_geometry::{Reference, dot, sub, unit};
use crate::model::{Analysis, Invalid, Problem, Spec};

fn invalid(message: impl Into<String>) -> Invalid {
    Invalid {
        path: None,
        also: Vec::new(),
        message: message.into(),
        choices: vec![],
    }
}

pub(crate) fn check(s: &Spec) -> Result<(), Invalid> {
    for (id, d) in &s.dimensions {
        let authored = d.between.is_some() || d.kind.is_some() || d.along.is_some();
        if !authored {
            if id.starts_with("d:authored:") {
                return Err(invalid(format!(
                    "{id} needs between and kind (linear, diameter, radius or angle); remove the entry to delete it."
                )));
            }
            continue;
        }
        if !id.strip_prefix("d:authored:").is_some_and(|suffix| {
            !suffix.is_empty()
                && suffix
                    .bytes()
                    .all(|c| c.is_ascii_alphanumeric() || b"_-".contains(&c))
        }) {
            return Err(invalid(format!(
                "{id}: authored dimensions need a stable d:authored:<name> ID (letters, digits, _ or -)."
            )));
        }
        let kind = d.kind.as_deref().unwrap_or("linear");
        let count = match kind {
            "linear" | "angle" => 2,
            "diameter" | "radius" => 1,
            _ => {
                return Err(invalid(format!(
                    "{id}: kind must be linear, diameter, radius or angle."
                )));
            }
        };
        if !d
            .between
            .as_ref()
            .is_some_and(|refs| refs.len() == count && refs.iter().all(|r| !r.is_empty()))
        {
            return Err(invalid(format!(
                "{id}: {kind} needs {count} reference(s) in between; copy keys from Analysis.edge_ink.references."
            )));
        }
        if d.along
            .as_ref()
            .is_some_and(|a| kind != "linear" || !["x", "y", "z", "true"].contains(&a.as_str()))
        {
            return Err(invalid(format!(
                "{id}: along is x, y, z or true, and applies only to linear dimensions."
            )));
        }
        if d.fit.is_some() && !["linear", "diameter"].contains(&kind) {
            return Err(invalid(format!(
                "{id}: a fit qualifies a linear size or a diameter; give this radius or angle an explicit tolerance."
            )));
        }
    }
    Ok(())
}

pub(super) fn append(
    a: &Analysis,
    s: &Spec,
    wants: &mut Vec<Want>,
) -> Result<Vec<Problem>, Invalid> {
    if !s.dimensions.keys().any(|id| id.starts_with("d:authored:"))
        && !s
            .dimensions
            .values()
            .any(|d| d.between.is_some() || d.kind.is_some() || d.along.is_some())
    {
        return Ok(super::schemes::check_authored(s, wants));
    }
    let mut problems = Vec::new();
    check(s)?;
    for (id, d) in s.dimensions.iter().filter(|(_, d)| d.between.is_some()) {
        // Invariant: This loop selects authored dimensions with between references.
        let refs = d.between.as_ref().unwrap();
        for reference in refs {
            let candidates = reference_features(a, reference);
            if candidates.len() > 1 {
                return Err(Invalid {
                    message: format!(
                        "dimensions/{id}: reference {reference} is ambiguous between features {}; name an explicit feature axis.",
                        candidates.iter().cloned().collect::<Vec<_>>().join(", ")
                    ),
                    choices: candidates.iter().map(|f| format!("axis:{f}")).collect(),
                    ..invalid("")
                });
            }
        }
        if s.binding
            .as_ref()
            .is_some_and(|b| b.source_sha256 != a.binding.source_sha256)
            && refs.iter().any(|r| r.starts_with("axis:f:"))
        {
            return Err(invalid(format!(
                "{id}: the Spec is bound to another STEP revision, so its feature-axis ID is unsafe; replace axis:f:<id> with the matching axis:face:v1:<fingerprint> from the current Analysis, or explicitly rebind the Spec."
            )));
        }
        let kind = d.kind.as_deref().unwrap_or("linear");
        let along = d.along.as_deref();
        let m = a
            .measure_references(refs, kind, along)
            .map_err(|why| invalid(format!("dimensions/{id}: {why}")))?;
        let (shape, views) = match kind {
            "linear" => {
                // Invariant: resolve returns witnesses for a successfully resolved linear measurement.
                let [p, q] = m.witnesses.unwrap();
                if let Some(k) = ["x", "y", "z"].iter().position(|k| Some(*k) == along) {
                    (
                        Shape::Linear {
                            a: p,
                            b: q,
                            axis: k,
                            a_edge: false,
                            b_edge: false,
                            order: 0,
                        },
                        super::envelope::profile_views(k),
                    )
                } else {
                    let views = super::TOWARD
                        .iter()
                        .filter(|(_, t)| dot(sub(q, p), *t).abs() < 1e-6)
                        .map(|(v, _)| *v)
                        .collect();
                    (Shape::Aligned { a: p, b: q }, views)
                }
            }
            "angle" => {
                // Invariant: resolve returns an angle for a successfully resolved angular measurement.
                let angle = m.angle.unwrap();
                let views = angle
                    .principal()
                    .map(|k| vec![super::end_on(k)])
                    .unwrap_or_default();
                (
                    Shape::Angular {
                        angle,
                        alternates: Vec::new(),
                    },
                    views,
                )
            }
            _ => {
                if m.sites.is_empty() {
                    return Err(invalid(format!(
                        "{id}: the face has no proven leader attachment; choose another face."
                    )));
                }
                // Exact surface sites, including oblique cylinders: a size leader need not be end-on.
                let views = match a.dimension_reference(&refs[0]).map_err(invalid)? {
                    Reference::Cylinder { axis, .. } => {
                        let k = (0..3).find(|&k| axis[k].abs() > 1.0 - 1e-8);
                        k.map(|k| vec![super::end_on(k)])
                            .unwrap_or_else(|| super::NAMES.to_vec())
                    }
                    _ => super::NAMES.to_vec(),
                };
                (
                    Shape::Callout {
                        arcs: Vec::new(),
                        sites: m.sites.iter().take(32).map(|&p| (p, 0.0)).collect(),
                    },
                    views,
                )
            }
        };
        if views.is_empty() {
            return Err(invalid(format!(
                "{id}: no principal view shows this quantity true; choose references lying in a principal plane, or along x, y or z."
            )));
        }
        let parameter = if kind == "linear" {
            format!("linear.{}", along.unwrap_or("true"))
        } else {
            kind.to_string()
        };
        let feature = format!("geometry:{}", refs.join("|"));
        let label = match kind {
            "diameter" => format!("⌀{}", fmt(m.value)),
            "radius" => format!("R{}", fmt(m.value)),
            "angle" => format!("{}°", fmt(m.value)),
            _ => fmt(m.value),
        };
        let mut w = Want {
            id: id.clone(),
            views,
            value: m.value,
            terms: vec![label],
            measurements: vec![Measurement {
                feature,
                parameter,
                value: m.value,
            }],
            basis: format!(
                "spec-authored: {kind} from {}; measured from exact model references",
                refs.join(" and ")
            ),
            shape,
            turning: None,
        };
        // A feature-to-feature span is relative: it does not anchor either feature.
        // Only attach an absolute location when the other reference is a datum or
        // non-feature station. Shared automatic claims are transferred by ownership.
        if let Some((axis, ..)) = linear_span(&w.shape)
            && refs
                .iter()
                .filter(|r| !reference_features(a, r).is_empty())
                .count()
                == 1
        {
            for feature in referenced_features(a, refs) {
                w.measurements.push(Measurement {
                    feature,
                    parameter: format!("location.length.{}", ["x", "y", "z"][axis]),
                    value: m.value,
                });
            }
        }
        // Only compare physical supports, never scalar equality alone.
        for existing in wants.iter().filter(|w| {
            !d.omit
                && !s.dimensions.get(&w.id).is_some_and(|d| d.omit)
                && (s.dimension_source == crate::model::DimensionSource::Auto
                    || s.dimensions.contains_key(&w.id))
        }) {
            if duplicate(a, &w, existing, refs, kind)
                && (kind != "linear" || same_features(a, refs, existing))
            {
                if !super::tolerance::same_requirement(
                    a,
                    s,
                    &w,
                    existing,
                    &referenced_features(a, refs),
                ) {
                    return Err(invalid(format!(
                        "{id} contradicts the requirement on {}; repeated measurements must have the compatible tolerances and fits.",
                        existing.id
                    )));
                }
                problems.push(Problem {
                    id: format!("p:duplicate_measurement:{id}:{}", existing.id),
                    code: "duplicate_measurement".into(), severity: "warning".into(),
                    message: format!("{} repeats a measurement already stated by {}; both truthful statements are retained.", super::wording_of(&w), super::wording_of(existing)),
                    subjects: vec![id.clone(), existing.id.clone()], ..Default::default()
                });
            }
        }
        wants.push(w);
    }
    problems.extend(super::schemes::check_authored(s, wants));
    Ok(problems)
}

fn linear_span(s: &Shape) -> Option<(usize, f64, f64)> {
    match s {
        Shape::Linear { a, b, axis, .. } => {
            Some((*axis, a[*axis].min(b[*axis]), a[*axis].max(b[*axis])))
        }
        Shape::Aligned { a, b } => {
            let delta = sub(*b, *a);
            let k = (0..3).find(|&k| (0..3).all(|j| j == k || delta[j].abs() < 1e-6))?;
            Some((k, a[k].min(b[k]), a[k].max(b[k])))
        }
        _ => None,
    }
}

fn duplicate(a: &Analysis, w: &Want, o: &Want, refs: &[String], kind: &str) -> bool {
    let close = |a: f64, b: f64| (a - b).abs() < 1e-5;
    if let (Some((k, p, q)), Some((j, r, s))) = (linear_span(&w.shape), linear_span(&o.shape)) {
        return k == j && close(p, r) && close(q, s);
    }
    if let (Shape::Aligned { a: p, b: q }, Shape::Aligned { a: r, b: s }) = (&w.shape, &o.shape) {
        let eq = |p: &[f64; 3], q: &[f64; 3]| (0..3).all(|k| close(p[k], q[k]));
        if (eq(p, r) && eq(q, s)) || (eq(p, s) && eq(q, r)) {
            return true;
        }
    }
    if o.measurements.iter().any(|m| {
        m.feature.starts_with("geometry:") && {
            let other: Vec<_> = m
                .feature
                .trim_start_matches("geometry:")
                .split('|')
                .collect();
            let mut r: Vec<_> = refs.iter().map(String::as_str).collect();
            r.sort();
            let mut other = other;
            other.sort();
            r == other
                && ((kind == "diameter" || kind == "radius")
                    && (m.parameter == "diameter" || m.parameter == "radius")
                    || m.parameter == w.measurements[0].parameter)
        }
    }) {
        return true;
    }
    if kind == "radius" || kind == "diameter" {
        let Ok(r) = a.dimension_reference(&refs[0]) else {
            return false;
        };
        let (bounds, radius) = match &r {
            Reference::Cylinder { bounds, radius, .. }
            | Reference::Sphere { bounds, radius, .. } => (*bounds, *radius),
            _ => return false,
        };
        let states_size = |m: &Measurement| {
            (m.parameter.contains("diameter") && close(m.value, 2.0 * radius))
                || (m.parameter.contains("radius") && close(m.value, radius))
        };
        if let (Reference::Cylinder { origin, axis, .. }, Some(k)) = (&r, o.turning)
            && axis[k].abs() > 1.0 - 1e-8
            && (0..3)
                .all(|j| j == k || close(origin[j], 0.5 * (a.envelope[0][j] + a.envelope[1][j])))
            && o.measurements
                .iter()
                .any(|m| m.feature == "envelope" && m.parameter == "od.diameter" && states_size(m))
        {
            return true;
        }
        let faces: Vec<_> = a
            .face_attachments
            .iter()
            .enumerate()
            .filter_map(|(i, f)| {
                f.bounds
                    .filter(|b| {
                        (0..3).all(|k| close(bounds[0][k], b[k]) && close(bounds[1][k], b[k + 3]))
                    })
                    .map(|_| i)
            })
            .collect();
        return o.measurements.iter().any(|m| {
            states_size(m)
                && a.features
                    .iter()
                    .find(|f| f.id == m.feature)
                    .is_some_and(|f| f.faces.iter().any(|i| faces.contains(i)))
        });
    }
    if let (Shape::Angular { angle: p, .. }, Shape::Angular { angle: q, .. }) = (&w.shape, &o.shape)
    {
        return close(w.value, o.value)
            && p.principal() == q.principal()
            && (0..3).all(|k| Some(k) == p.principal() || close(p.vertex[k], q.vertex[k]))
            && p.rays().zip(q.rays()).is_some_and(|(p, q)| {
                p.iter().all(|p| {
                    q.iter().any(|q| {
                        unit(*q)
                            .zip(unit(*p))
                            .is_some_and(|(p, q)| dot(p, q).abs() > 1.0 - 1e-6)
                    })
                })
            });
    }
    false
}

/// Feature-axis identities, including the stable face spelling of the same axis.
fn referenced_features(a: &Analysis, refs: &[String]) -> std::collections::BTreeSet<String> {
    refs.iter().flat_map(|r| reference_features(a, r)).collect()
}

fn reference_features(a: &Analysis, reference: &str) -> std::collections::BTreeSet<String> {
    let mut out = std::collections::BTreeSet::new();
    if let Some(key) = reference.strip_prefix("axis:") {
        if a.features.iter().any(|f| f.id == key) {
            out.insert(key.to_string());
        } else if let Ok(Reference::Cylinder { bounds, .. }) = a.dimension_reference(key) {
            for f in &a.features {
                // A compound can contain this face without defining one axis of its own.
                if f.faces.iter().any(|&i| {
                    a.face_attachments.get(i).is_some_and(|face| {
                        face.bounds.is_some_and(|b| {
                            (0..3).all(|k| {
                                (bounds[0][k] - b[k]).abs() < 1e-6
                                    && (bounds[1][k] - b[k + 3]).abs() < 1e-6
                            })
                        })
                    })
                }) && a
                    .dimension_reference(&format!("axis:{}", f.id))
                    .is_ok_and(|r| matches!(r, Reference::Edge { .. }))
                {
                    out.insert(f.id.clone());
                }
            }
        }
    }
    out
}

fn same_features(a: &Analysis, refs: &[String], other: &Want) -> bool {
    let named = referenced_features(a, refs);
    if named.is_empty() {
        // Untyped face/vertex spans keep their existing physical-support check.
        return true;
    }
    let Some(owner) = other.measurements.first() else {
        return false;
    };
    if let Some(refs) = owner.feature.strip_prefix("geometry:") {
        named == referenced_features(a, &refs.split('|').map(String::from).collect::<Vec<_>>())
    } else {
        // A shared automatic owner states every one of its measurements, not
        // just its first feature's role.
        named
            .iter()
            .all(|f| other.measurements.iter().any(|m| m.feature == *f))
    }
}

/// Hand only the numerically stated roles to a new owner. Compound riders remain required.
#[derive(Default)]
pub(super) struct Ownership {
    pub claims: std::collections::BTreeMap<String, Vec<Measurement>>,
    pub covered: std::collections::BTreeMap<String, String>,
}

pub(super) fn ownership(a: &Analysis, s: &Spec, wants: &[Want]) -> Ownership {
    let mut out = Ownership::default();
    if s.dimension_source != crate::model::DimensionSource::Authored {
        return out;
    }
    for w in wants.iter().filter(|w| w.id.starts_with("d:authored:")) {
        let Some(d) = s.dimensions.get(&w.id) else {
            continue;
        };
        let Some(refs) = &d.between else { continue };
        let kind = d.kind.as_deref().unwrap_or("linear");
        for original in wants
            .iter()
            .filter(|o| !o.id.starts_with("d:authored:") && !s.dimensions.contains_key(&o.id))
        {
            if !duplicate(a, w, original, refs, kind) {
                continue;
            }
            let stated: Vec<_> = original
                .measurements
                .iter()
                .filter(|m| {
                    (m.value - w.value).abs() < 1e-6
                        && match kind {
                            "linear" => matches!(
                                original.shape,
                                Shape::Linear { .. } | Shape::Aligned { .. }
                            ),
                            "angle" => m.parameter.contains("angle"),
                            "diameter" => m.parameter.contains("diameter"),
                            "radius" => m.parameter.contains("radius"),
                            _ => false,
                        }
                })
                .cloned()
                .collect();
            if !stated.is_empty() && stated.len() == original.measurements.len() {
                out.covered.insert(original.id.clone(), w.id.clone());
            }
            let claims = out.claims.entry(w.id.clone()).or_default();
            for m in stated {
                // Preserve shared claims unless another authored witness already
                // explicitly locates that feature. Coincidence alone does not
                // make two distinct features' explicit locations duplicates.
                if wants.iter().any(|other| {
                    other.id != w.id
                        && other.id.starts_with("d:authored:")
                        && other
                            .measurements
                            .iter()
                            .any(|c| c.feature == m.feature && c.parameter == m.parameter)
                }) {
                    continue;
                }
                if !w
                    .measurements
                    .iter()
                    .chain(claims.iter())
                    .any(|c| c.feature == m.feature && c.parameter == m.parameter)
                {
                    claims.push(m);
                }
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    #[test]
    fn a_feature_owns_only_the_surface_sizes_its_label_actually_states() {
        let (_, a) = crate::model::analyse_native(
            &std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("tests/fixtures/parts/mounting_plate.step"),
        )
        .unwrap();
        let mut s = crate::model::Spec::default();
        let (mut wants, ..) = super::super::wants(&a);
        let (key, radius, owner) = a
            .dimension_references()
            .iter()
            .find_map(|(key, rs)| {
                let [crate::model::dimension_geometry::Reference::Cylinder { radius, .. }] =
                    rs.as_slice()
                else {
                    return None;
                };
                let d: crate::model::DimensionSpec =
                    serde_json::from_value(serde_json::json!({"kind":"diameter","between":[key]}))
                        .unwrap();
                s.dimensions.insert("d:authored:test".into(), d);
                super::append(&a, &s, &mut wants.clone())
                    .unwrap()
                    .into_iter()
                    .find(|p| p.code == "duplicate_measurement")
                    .and_then(|p| p.subjects.get(1).cloned())
                    .map(|id| (key.clone(), *radius, id))
            })
            .unwrap();
        let plan = crate::model::plan_recovered(&a, &s).unwrap();
        assert!(
            plan.problems
                .iter()
                .any(|p| p.code == "duplicate_measurement")
        );
        assert!(
            plan.dimensions
                .iter()
                .any(|d| d.id == "d:authored:test" && d.placed.is_some())
        );
        s.dimension_source = crate::model::DimensionSource::Authored;
        super::append(&a, &s, &mut wants).unwrap();
        let authored = wants.iter().find(|w| w.id == "d:authored:test").unwrap();
        let mut automatic = wants.iter().find(|w| w.id == owner).unwrap().clone();
        assert!(super::duplicate(
            &a,
            authored,
            &automatic,
            std::slice::from_ref(&key),
            "diameter"
        ));
        for m in &mut automatic.measurements {
            if m.parameter.contains("diameter") || m.parameter.contains("radius") {
                m.value = radius * 7.0;
            }
        }
        assert!(!super::duplicate(
            &a,
            authored,
            &automatic,
            &[key],
            "diameter"
        ));
    }
}