springrep 1.0.0

An exact boundary-representation geometry kernel, in safe Rust
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
//! Reading a STEP file: the product structure gives each solid's placements, and each solid is
//! built once and copied to every placement.

use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicUsize, Ordering};

use super::{
    WRITER,
    geometry::{Geometry, Placing},
    heal::Healed,
    part21::{self, File, Param},
    topology,
};
use crate::{
    Error, Result, check,
    math::Point3,
    topo::{Brep, FaceId, FaceName, SolidId},
};

/// What a read produced: every solid that became a valid kernel solid, how far healing moved
/// each one's geometry, and why any other was rejected.
#[derive(Debug)]
pub struct Import {
    pub brep: Brep,
    /// One entry per rejected solid placement: `#id: error`.
    pub rejected: Vec<String>,
    /// One entry per placed solid whose geometry healing moved, in placement order.
    pub healed: Vec<Healing>,
    /// One entry per placed solid kept as the file has it, with local tolerances, in placement
    /// order.
    pub tolerant: Vec<Tolerant>,
    /// Every place a placed solid passes through or touches itself, in placement order. Such a
    /// solid is kept as the file has it: an imported mesh stitched with folds microns deep is still
    /// the part, and other systems keep it too.
    pub crossed: Vec<Crossed>,
    /// One entry per solid of `brep`, in its order: what the file calls it and how it draws it.
    pub bodies: Vec<Body>,
    /// The faces the file gives a colour of their own, apart from their body's.
    pub face_colours: Vec<(FaceId, [f64; 3])>,
}

/// A solid of an import as its file describes it.
#[derive(Clone, Debug, PartialEq)]
pub struct Body {
    pub solid: SolidId,
    /// The file's id of the solid it came from; an assembly places one solid many times.
    pub step_id: u64,
    /// The name of the product whose shape holds the solid, where the file has products.
    pub name: Option<String>,
    /// The uses that place it, from the root assembly down: each use's name, or its id where it
    /// has none. Empty for a solid of a root product.
    pub instance: Vec<String>,
    /// The body's surface colour, red, green and blue in 0 to 1, where the file gives one.
    pub colour: Option<[f64; 3]>,
}

/// Two faces of one placed solid that share no edge but meet.
#[derive(Clone, Debug, PartialEq)]
pub struct Crossed {
    /// The solid's position among `Import::brep`'s solids.
    pub index: usize,
    /// The solid's STEP instance.
    pub step_id: u64,
    /// The two faces, in `Import::brep`.
    pub faces: (FaceId, FaceId),
    /// Where they meet, placed as the solid is.
    pub at: Point3,
    /// How far one passes through the other, in mm: zero where they only touch.
    pub depth: f64,
}

/// A placed solid whose geometry could not be healed to meet within [`crate::tol::LINEAR`]
/// without moving it further than the file says it meets, so its edges and vertices carry the
/// tolerances it does meet within. Those are measured, and may be wider than the file states.
#[derive(Clone, Debug, PartialEq)]
pub struct Tolerant {
    /// The solid's position among `Import::brep`'s solids.
    pub index: usize,
    /// The solid's STEP instance.
    pub step_id: u64,
    /// How many of its vertices and edges carry a tolerance above the kernel's.
    pub vertices: usize,
    pub edges: usize,
    /// The largest tolerance of any of them, in mm.
    pub largest: f64,
    /// The uncertainty the file states for the solid, in mm, where it states one.
    pub stated: Option<f64>,
    /// How many of them carry a tolerance above `stated`: where the file misses its own word.
    pub past_stated: usize,
}

/// How far healing moved one placed solid's geometry to close it within the kernel tolerance.
#[derive(Clone, Debug, PartialEq)]
pub struct Healing {
    /// The solid's position among `Import::brep`'s solids.
    pub index: usize,
    /// The solid's STEP instance.
    pub step_id: u64,
    /// The largest distance any vertex, edge or surface moved, in mm.
    pub moved: f64,
    /// Where that movement was, placed as the solid is.
    pub at: Point3,
    /// What moved most, such as `vertex #12` or `the surface of face #34`.
    pub what: String,
    /// The largest distance any of its surfaces moved, which bounds how far its faces moved.
    pub surfaces: f64,
}

const SOLIDS: [&str; 4] = [
    "MANIFOLD_SOLID_BREP",
    "BREP_WITH_VOIDS",
    "FACETED_BREP",
    "TESSELLATED_SOLID",
];

/// The most solid placements one file may expand to; an assembly that reuses its parts
/// exponentially deep is refused rather than expanded.
const MAX_PLACEMENTS: usize = 1_000_000;

fn cyclic<T>(id: u64, what: &str) -> Result<T> {
    Err(Error::InvalidInput(format!("STEP #{id}: {what}")))
}

/// A solid to build, the representation context its units come from, and where it goes.
struct Placement {
    solid: u64,
    context: Option<u64>,
    at: Placing,
    /// The product definition whose shape holds the solid.
    product: Option<u64>,
    /// The assembly uses that place it, root first.
    uses: Vec<u64>,
}

impl Placement {
    /// What the solid is built from: the same solid in the same units at the same scale is built
    /// once.
    fn key(&self) -> Key {
        (self.solid, self.context, self.at.scale.to_bits())
    }
}

type Key = (u64, Option<u64>, u64);

fn params(f: &File, id: u64, kind: &str) -> Option<Vec<Param>> {
    f.get(id)?.part(kind).map(<[Param]>::to_vec)
}

fn refs(p: Option<&Param>) -> Vec<u64> {
    p.and_then(Param::as_list)
        .map(|l| l.iter().filter_map(Param::as_ref).collect())
        .unwrap_or_default()
}

/// The representation an instance is (any entity with a `REPRESENTATION` part or subtype that
/// carries `name, items, context`).
fn representation(f: &File, id: u64) -> Option<(Vec<u64>, Option<u64>)> {
    let e = f.get(id)?;
    let (_, p) = e.parts.iter().find(|(k, p)| {
        k.ends_with("REPRESENTATION") && !k.contains("RELATIONSHIP") && p.len() >= 3
    })?;
    Some((refs(p.get(1)), p.get(2).and_then(Param::as_ref)))
}

struct Assembly<'a> {
    file: &'a File,
    /// Representations joined without a transformation (a part's shape and its B-rep).
    links: HashMap<u64, Vec<u64>>,
    /// Each product definition's shape representations.
    reps: HashMap<u64, Vec<u64>>,
    /// Each product definition's children: the child, the relation that places it, and the use.
    children: HashMap<u64, Vec<(u64, u64, u64)>>,
    related: HashSet<u64>,
}

impl<'a> Assembly<'a> {
    fn new(file: &'a File) -> Assembly<'a> {
        let mut a = Assembly {
            file,
            links: HashMap::new(),
            reps: HashMap::new(),
            children: HashMap::new(),
            related: HashSet::new(),
        };
        let mut nauo_place: HashMap<u64, u64> = HashMap::new();
        for e in &file.entities {
            if let Some(p) = e.part("SHAPE_DEFINITION_REPRESENTATION") {
                let (Some(pds), Some(rep)) = (
                    p.first().and_then(Param::as_ref),
                    p.get(1).and_then(Param::as_ref),
                ) else {
                    continue;
                };
                if let Some(pd) = params(file, pds, "PRODUCT_DEFINITION_SHAPE")
                    .and_then(|q| q.get(2).and_then(Param::as_ref))
                {
                    a.reps.entry(pd).or_default().push(rep);
                }
            } else if let Some(p) = e.part("CONTEXT_DEPENDENT_SHAPE_REPRESENTATION") {
                let (Some(rel), Some(pds)) = (
                    p.first().and_then(Param::as_ref),
                    p.get(1).and_then(Param::as_ref),
                ) else {
                    continue;
                };
                if let Some(nauo) = params(file, pds, "PRODUCT_DEFINITION_SHAPE")
                    .and_then(|q| q.get(2).and_then(Param::as_ref))
                {
                    nauo_place.insert(nauo, rel);
                }
            } else if !e.has("REPRESENTATION_RELATIONSHIP_WITH_TRANSFORMATION")
                && let Some(p) = e.part("REPRESENTATION_RELATIONSHIP").or_else(|| {
                    e.part("SHAPE_REPRESENTATION_RELATIONSHIP")
                        .filter(|p| p.len() >= 4)
                })
                && let (Some(r1), Some(r2)) = (
                    p.get(2).and_then(Param::as_ref),
                    p.get(3).and_then(Param::as_ref),
                )
            {
                a.links.entry(r1).or_default().push(r2);
                a.links.entry(r2).or_default().push(r1);
            }
        }
        for (i, e) in file.entities.iter().enumerate() {
            if let Some(p) = e.part("NEXT_ASSEMBLY_USAGE_OCCURRENCE") {
                let id = file.ids[i];
                let (Some(parent), Some(child)) = (
                    p.get(3).and_then(Param::as_ref),
                    p.get(4).and_then(Param::as_ref),
                ) else {
                    continue;
                };
                if let Some(&rel) = nauo_place.get(&id) {
                    a.children.entry(parent).or_default().push((child, rel, id));
                    a.related.insert(child);
                }
            }
        }
        a
    }

    /// Every representation joined to `rep` without a transformation, `rep` included.
    fn linked(&self, rep: u64) -> Vec<u64> {
        let mut seen = vec![rep];
        let mut i = 0;
        while i < seen.len() {
            for &r in self.links.get(&seen[i]).into_iter().flatten() {
                if !seen.contains(&r) {
                    seen.push(r);
                }
            }
            i += 1;
        }
        seen
    }

    /// The solids in representation `rep` and those joined to it, at `at`. `path` holds the
    /// representations mapped into this one, so a map that contains itself is an error.
    fn rep_solids(
        &self,
        geo: &mut Geometry,
        rep: u64,
        at: &Placing,
        path: &mut Vec<u64>,
        out: &mut Vec<Placement>,
    ) -> Result<()> {
        if path.contains(&rep) {
            return cyclic(rep, "a mapped representation contains itself");
        }
        path.push(rep);
        let r = self.rep_solids_in(geo, rep, at, path, out);
        path.pop();
        r
    }

    fn rep_solids_in(
        &self,
        geo: &mut Geometry,
        rep: u64,
        at: &Placing,
        path: &mut Vec<u64>,
        out: &mut Vec<Placement>,
    ) -> Result<()> {
        for r in self.linked(rep) {
            let Some((items, context)) = representation(self.file, r) else {
                continue;
            };
            for it in items {
                let Some(e) = self.file.get(it) else { continue };
                if SOLIDS.contains(&e.kind()) {
                    if out.len() >= MAX_PLACEMENTS {
                        return cyclic(it, "the assembly places too many solids");
                    }
                    out.push(Placement {
                        solid: it,
                        context,
                        at: *at,
                        product: None,
                        uses: Vec::new(),
                    });
                } else if let Some(p) = e.part("MAPPED_ITEM") {
                    let _ = (geo.use_context(context), p);
                    if let Some(t) = self.mapped(geo, p) {
                        let (src, place) = t;
                        self.rep_solids(geo, src, &place.then(at), path, out)?;
                    }
                }
            }
        }
        Ok(())
    }

    /// A `MAPPED_ITEM`'s source representation and its placement in the target.
    fn mapped(&self, geo: &mut Geometry, p: &[Param]) -> Option<(u64, Placing)> {
        let map = params(self.file, p.get(1)?.as_ref()?, "REPRESENTATION_MAP")?;
        let origin = geo.transform_of(map.first()?.as_ref()?).ok()?;
        let src = map.get(1)?.as_ref()?;
        let target = geo.transform_of(p.get(2)?.as_ref()?).ok()?;
        Some((src, origin.inverse().then(&target)))
    }

    /// The motion that places `child`'s shape in its parent's, from the placing relation.
    fn placement(&self, geo: &mut Geometry, child: u64, rel: u64) -> Option<Placing> {
        let e = self.file.get(rel)?;
        let rr = e.part("REPRESENTATION_RELATIONSHIP")?;
        let (r1, r2) = (rr.get(2)?.as_ref()?, rr.get(3)?.as_ref()?);
        let op = e
            .part("REPRESENTATION_RELATIONSHIP_WITH_TRANSFORMATION")?
            .first()?
            .as_ref()?;
        let child_reps: Vec<u64> = self
            .reps
            .get(&child)
            .into_iter()
            .flatten()
            .flat_map(|&r| self.linked(r))
            .collect();
        let child_is_r1 = child_reps.contains(&r1) || !child_reps.contains(&r2);
        let context = |r: u64| representation(self.file, r).and_then(|x| x.1);
        let oe = self.file.get(op)?;
        if let Some(it) = oe.part("ITEM_DEFINED_TRANSFORMATION") {
            geo.use_context(context(r1)).ok()?;
            let a = geo.transform_of(it.get(2)?.as_ref()?).ok()?;
            geo.use_context(context(r2)).ok()?;
            let b = geo.transform_of(it.get(3)?.as_ref()?).ok()?;
            let (from, to) = if child_is_r1 { (a, b) } else { (b, a) };
            Some(from.inverse().then(&to))
        } else {
            geo.use_context(context(if child_is_r1 { r2 } else { r1 }))
                .ok()?;
            let t = geo.transform_of(op).ok()?;
            Some(if child_is_r1 { t } else { t.inverse() })
        }
    }

    /// Places `pd` and its children at `at`. `path` holds the products above it, so an assembly
    /// that contains itself is an error.
    fn walk(
        &self,
        geo: &mut Geometry,
        pd: u64,
        at: &Placing,
        path: &mut Vec<u64>,
        seen: &mut HashSet<u64>,
        out: &mut Vec<Placement>,
    ) -> Result<()> {
        if path.contains(&pd) {
            return cyclic(pd, "the assembly contains itself");
        }
        seen.insert(pd);
        path.push(pd);
        let own = out.len();
        for &rep in self.reps.get(&pd).into_iter().flatten() {
            self.rep_solids(geo, rep, at, &mut Vec::new(), out)?;
        }
        for p in &mut out[own..] {
            p.product = Some(pd);
        }
        for &(child, rel, used) in self.children.get(&pd).into_iter().flatten() {
            if let Some(t) = self.placement(geo, child, rel) {
                let below = out.len();
                self.walk(geo, child, &t.then(at), path, seen, out)?;
                for p in &mut out[below..] {
                    p.uses.insert(0, used);
                }
            }
        }
        path.pop();
        Ok(())
    }

    fn placements(&self, geo: &mut Geometry) -> Result<Vec<Placement>> {
        let mut out = Vec::new();
        let mut roots: Vec<u64> = self
            .reps
            .keys()
            .chain(self.children.keys())
            .copied()
            .filter(|pd| !self.related.contains(pd))
            .collect();
        roots.sort_unstable();
        roots.dedup();
        let mut seen = HashSet::new();
        for pd in roots {
            self.walk(
                geo,
                pd,
                &Placing::IDENTITY,
                &mut Vec::new(),
                &mut seen,
                &mut out,
            )?;
        }
        // A product no root reaches sits on a cycle of assembly uses.
        let mut unreached: Vec<u64> = self
            .children
            .keys()
            .copied()
            .filter(|pd| !seen.contains(pd))
            .collect();
        unreached.sort_unstable();
        match unreached.first() {
            Some(&pd) => cyclic(pd, "the assembly contains itself"),
            None => Ok(out),
        }
    }
}

/// Reads a STEP file as one part: the union of its solids, as the pieces of a solid that touches
/// itself are written one by one and join again. A file with a solid this kernel rejects is an
/// error, since the part would lack it.
pub fn read_part(text: &[u8], feature: &str) -> Result<Brep> {
    let import = read(text, feature)?;
    if !import.rejected.is_empty() {
        return Err(Error::InvalidResult(import.rejected));
    }
    let b = &import.brep;
    let pieces: Vec<Brep> = b
        .solid_ids()
        .map(|s| crate::boolean::solids_of(b, &[s]))
        .collect();
    match &pieces[..] {
        [one] => Ok(one.clone()),
        _ => crate::boolean::union_all(&pieces.iter().collect::<Vec<_>>()),
    }
}

/// Reads a STEP file into kernel solids. Faces carry the names a file this kernel wrote
/// gave them; otherwise each is `<feature>.face_<i>`, numbered in the order the solids are placed.
pub fn read(text: &[u8], feature: &str) -> Result<Import> {
    let file = part21::parse(text)?;
    let ours = file.originating_system() == Some(WRITER);
    let mut geo = Geometry::new(&file);
    let mut placements = Assembly::new(&file).placements(&mut geo)?;
    if placements.is_empty() {
        // Without a product structure, a solid takes its units and uncertainty from a
        // representation that holds it, if one does.
        let mut contexts: HashMap<u64, u64> = HashMap::new();
        for &id in &file.ids {
            if let Some((items, Some(ctx))) = representation(&file, id) {
                for it in items {
                    contexts.entry(it).or_insert(ctx);
                }
            }
        }
        for (i, e) in file.entities.iter().enumerate() {
            if SOLIDS.contains(&e.kind()) {
                placements.push(Placement {
                    solid: file.ids[i],
                    context: contexts.get(&file.ids[i]).copied(),
                    at: Placing::IDENTITY,
                    product: None,
                    uses: Vec::new(),
                });
            }
        }
    }
    if placements.is_empty() {
        return Err(Error::InvalidInput("STEP: the file holds no solids".into()));
    }
    let mut keys: Vec<Key> = placements.iter().map(Placement::key).collect();
    keys.sort_unstable();
    keys.dedup();
    let built = build_all(&file, &keys);
    let mut out = Brep::new();
    let mut rejected = Vec::new();
    let mut healed = Vec::new();
    let mut tolerant = Vec::new();
    let mut crossed = Vec::new();
    let mut crossings: HashMap<Key, Vec<check::SelfCrossing>> = HashMap::new();
    let mut counter = 0usize;
    let mut index = 0usize;
    let colours = colours(&file);
    let mut bodies = Vec::new();
    let mut face_colours = Vec::new();
    for pl in placements {
        match &built[&pl.key()] {
            Err(e) => rejected.push(format!("#{}: {e}", pl.solid)),
            Ok((b, names, heal, stated)) => {
                if let Some(h) = heal {
                    healed.push(Healing {
                        index,
                        step_id: pl.solid,
                        moved: h.moved,
                        at: pl.at.rigid.point(h.at),
                        what: h.what.clone(),
                        surfaces: h.surfaces,
                    });
                }
                if b.has_tolerances() {
                    let (vs, es) = b.tolerant();
                    let all = vs.iter().map(|x| x.1).chain(es.iter().map(|x| x.1));
                    let past = |t: &f64| stated.is_some_and(|s| *t > s);
                    tolerant.push(Tolerant {
                        index,
                        step_id: pl.solid,
                        vertices: vs.len(),
                        edges: es.len(),
                        largest: vs.iter().map(|x| x.1).fold(0.0, f64::max),
                        stated: *stated,
                        past_stated: all.filter(past).count(),
                    });
                }
                let first = out.face_count();
                let found = crossings
                    .entry(pl.key())
                    .or_insert_with(|| check::self_crossings(b))
                    .clone();
                let solid_index = index;
                index += 1;
                let mut copy = b.clone();
                copy.transform(&pl.at.rigid);
                let mut own_colours = Vec::new();
                let faces = copy.face_ids().collect::<Vec<_>>();
                for (k, (f, (step, name))) in faces.into_iter().zip(names).enumerate() {
                    if let Some(&c) = colours.get(step) {
                        own_colours.push((k, c));
                    }
                    let parsed = if ours { parse_names(name) } else { Vec::new() };
                    copy.face_mut(f).names = if parsed.is_empty() {
                        [FaceName::new(feature, format!("face_{counter}"))].into()
                    } else {
                        parsed.into_iter().collect()
                    };
                    counter += 1;
                }
                let solids = out.append(&copy);
                let ids: Vec<FaceId> = out.face_ids().skip(first).collect();
                face_colours.extend(own_colours.into_iter().map(|(k, c)| (ids[k], c)));
                bodies.extend(solids.into_iter().map(|solid| Body {
                    solid,
                    step_id: pl.solid,
                    name: pl.product.and_then(|pd| product_name(&file, pd)),
                    instance: pl.uses.iter().map(|&u| use_name(&file, u)).collect(),
                    colour: colours.get(&pl.solid).copied(),
                }));
                for x in found {
                    crossed.push(Crossed {
                        index: solid_index,
                        step_id: pl.solid,
                        faces: (ids[x.a.index()], ids[x.b.index()]),
                        at: pl.at.rigid.point(x.at),
                        depth: x.depth,
                    });
                }
            }
        }
    }
    Ok(Import {
        brep: out,
        rejected,
        healed,
        tolerant,
        crossed,
        bodies,
        face_colours,
    })
}

fn text(p: Option<&Param>) -> Option<String> {
    p.and_then(Param::as_str)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
}

/// The name of the product a product definition defines, or its id where it has no name.
fn product_name(file: &File, pd: u64) -> Option<String> {
    let formation = params(file, pd, "PRODUCT_DEFINITION")?.get(2)?.as_ref()?;
    let formed = file.get(formation)?;
    let product = formed
        .parts
        .iter()
        .find(|(k, _)| k.starts_with("PRODUCT_DEFINITION_FORMATION"))?
        .1
        .get(2)?
        .as_ref()?;
    let p = params(file, product, "PRODUCT")?;
    text(p.get(1)).or_else(|| text(p.first()))
}

/// What an assembly use is called: its name, else its id, else its number in the file.
fn use_name(file: &File, used: u64) -> String {
    params(file, used, "NEXT_ASSEMBLY_USAGE_OCCURRENCE")
        .and_then(|p| text(p.get(1)).or_else(|| text(p.first())))
        .unwrap_or_else(|| format!("#{used}"))
}

fn all_refs(p: &Param, out: &mut Vec<u64>) {
    match p {
        Param::Ref(r) => out.push(*r),
        Param::List(l) => l.iter().for_each(|x| all_refs(x, out)),
        Param::Typed(_, inner) => all_refs(inner, out),
        _ => {}
    }
}

/// The first colour reached from entity `id` within `depth` references.
fn colour_under(file: &File, id: u64, depth: usize) -> Option<[f64; 3]> {
    let e = file.get(id)?;
    if let Some(p) = e.part("COLOUR_RGB") {
        let at = |i: usize| p.get(i).and_then(Param::as_f64);
        return Some([at(1)?, at(2)?, at(3)?]);
    }
    if let Some(p) = e
        .part("DRAUGHTING_PRE_DEFINED_COLOUR")
        .or_else(|| e.part("PRE_DEFINED_COLOUR"))
    {
        return Some(match p.first().and_then(Param::as_str)? {
            "red" => [1.0, 0.0, 0.0],
            "green" => [0.0, 1.0, 0.0],
            "blue" => [0.0, 0.0, 1.0],
            "yellow" => [1.0, 1.0, 0.0],
            "magenta" => [1.0, 0.0, 1.0],
            "cyan" => [0.0, 1.0, 1.0],
            "black" => [0.0, 0.0, 0.0],
            "white" => [1.0, 1.0, 1.0],
            _ => return None,
        });
    }
    if depth == 0 {
        return None;
    }
    let mut below = Vec::new();
    for (_, ps) in &e.parts {
        ps.iter().for_each(|p| all_refs(p, &mut below));
    }
    below
        .into_iter()
        .find_map(|r| colour_under(file, r, depth - 1))
}

/// The surface colour the file's presentation styles give each solid or face they name, by its
/// id. A curve's or a point's colour is not a body's.
fn colours(file: &File) -> HashMap<u64, [f64; 3]> {
    // Usage, side style, fill area, fill style, fill colour, colour.
    const DEPTH: usize = 6;
    let mut out = HashMap::new();
    for e in &file.entities {
        let Some(p) = e
            .part("STYLED_ITEM")
            .or_else(|| e.part("OVER_RIDING_STYLED_ITEM"))
        else {
            continue;
        };
        let Some(item) = p.get(2).and_then(Param::as_ref) else {
            continue;
        };
        let mut styles = Vec::new();
        for assignment in refs(p.get(1)) {
            if let Some(a) = file.get(assignment) {
                for (_, ps) in &a.parts {
                    ps.iter().for_each(|x| all_refs(x, &mut styles));
                }
            }
        }
        let colour = styles
            .into_iter()
            .filter(|&s| file.get(s).is_some_and(|x| x.has("SURFACE_STYLE_USAGE")))
            .find_map(|s| colour_under(file, s, DEPTH));
        if let Some(c) = colour {
            out.entry(item).or_insert(c);
        }
    }
    out
}

/// A solid, its faces' ids and names, how far healing moved it, and the uncertainty its file
/// states.
type Built = std::result::Result<(Brep, Vec<(u64, String)>, Option<Healed>, Option<f64>), String>;

/// A rough measure of how long a solid takes to build: its faces, and the control points of its
/// spline surfaces, which healing fits.
fn weight(file: &File, solid: u64) -> usize {
    let refs_of = |id: u64, at: usize| -> Vec<u64> {
        let Some(e) = file.get(id) else {
            return Vec::new();
        };
        let Some(p) = e.parts.first().map(|p| &p.1) else {
            return Vec::new();
        };
        match p.get(at) {
            Some(Param::List(l)) => l.iter().filter_map(Param::as_ref).collect(),
            Some(x) => x.as_ref().into_iter().collect(),
            None => Vec::new(),
        }
    };
    let mut shells = refs_of(solid, 1);
    shells.extend(refs_of(solid, 2));
    let mut total = 0;
    for sh in shells {
        let sh = match file.get(sh).and_then(|e| e.part("ORIENTED_CLOSED_SHELL")) {
            Some(p) => p.get(2).and_then(Param::as_ref).unwrap_or(sh),
            None => sh,
        };
        for f in refs_of(sh, 1) {
            total += 1;
            let Some(face) = file.get(f) else { continue };
            let Some(s) = face.parts[0].1.get(2).and_then(Param::as_ref) else {
                continue;
            };
            let Some(e) = file.get(s) else { continue };
            // A complex instance's B_SPLINE_SURFACE part has no name before its degrees.
            let net = match (
                e.part("B_SPLINE_SURFACE"),
                e.part("B_SPLINE_SURFACE_WITH_KNOTS"),
            ) {
                (Some(p), _) => p.get(2),
                (None, Some(p)) => p.get(3),
                _ => None,
            };
            total += net.and_then(Param::as_list).map_or(0, |rows| {
                rows.iter().filter_map(Param::as_list).map(<[_]>::len).sum()
            });
        }
    }
    total
}

/// Builds each distinct solid once. Solids share nothing but the file, so they are built on as
/// many threads as the machine offers, each with its own geometry cache, heaviest first so that
/// no long build starts last. Each result is filed under its own key, whatever finished first.
fn build_all(file: &File, keys: &[Key]) -> HashMap<Key, Built> {
    let threads = std::thread::available_parallelism()
        .map_or(1, |n| n.get())
        .min(keys.len().max(1));
    let mut order: Vec<(usize, usize)> = keys
        .iter()
        .enumerate()
        .map(|(k, key)| (weight(file, key.0), k))
        .collect();
    order.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(&b.1)));
    let next = AtomicUsize::new(0);
    let results: Vec<Vec<(usize, Built)>> = std::thread::scope(|scope| {
        let workers: Vec<_> = (0..threads)
            .map(|_| {
                scope.spawn(|| {
                    let mut geo = Geometry::new(file);
                    let mut mine = Vec::new();
                    while let Some(&(_, k)) = order.get(next.fetch_add(1, Ordering::Relaxed)) {
                        let (solid, context, scale) = keys[k];
                        let r = geo
                            .use_scaled(context, f64::from_bits(scale))
                            .and_then(|_| topology::solid(&mut geo, solid))
                            .map(|(b, names, healed)| (b, names, healed, geo.uncertainty()))
                            .map_err(|e| e.to_string());
                        mine.push((k, r));
                    }
                    mine
                })
            })
            .collect();
        workers
            .into_iter()
            .map(|w| w.join().unwrap_or_default())
            .collect()
    });
    let mut out = HashMap::new();
    for (k, r) in results.into_iter().flatten() {
        out.insert(keys[k], r);
    }
    for &k in keys {
        out.entry(k)
            .or_insert_with(|| Err("the thread building it failed".to_string()));
    }
    out
}

/// The names a face this kernel wrote carries: `feature.face` entries joined by `;`.
fn parse_names(s: &str) -> Vec<FaceName> {
    s.split(';')
        .filter_map(|n| {
            let (f, face) = n.split_once('.')?;
            Some(FaceName::new(f, face))
        })
        .collect()
}