stepq 0.6.0

Query, inspect, split and reshape STEP (ISO 10303-21) files — a library and a CLI, no geometry kernel needed
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
//! Semantic PMI: geometric tolerances, dimensions and datums.
//!
//! This is what `stepq pmi` prints. AP242 represents product and
//! manufacturing information twice: as presentation (annotation curves and
//! text for display) and as semantics (tolerances, datums and dimensions a
//! program can act on). This module reads the semantic half from the entity
//! graph. Every value is kept as written — the source text of a measure,
//! never re-printed — and every item is tied to the product definition it
//! belongs to.

use std::collections::HashSet;

mod items;

pub use items::{
    AspectItems, ItemUsage, ItemUsageKind, Naming, Resolution, ResolvedItem, Step, StepKind,
    StepKinds, Unresolved, UnresolvedReason,
};

use crate::model::{Graph, ProductStructure};
use crate::p21::{Exchange, Literal, Param, Record};
use crate::props::{Subject, Value, representation_items, subject, value};
use crate::record::{text, upper};

/// Everything [`pmi`] found, each list in file order.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Pmi {
    /// Every `datum`.
    pub datums: Vec<Datum>,
    /// Every geometric tolerance.
    pub tolerances: Vec<Tolerance>,
    /// Every size and location dimension.
    pub dimensions: Vec<Dimension>,
}

impl Pmi {
    /// True if the file holds no semantic PMI.
    pub fn is_empty(&self) -> bool {
        self.datums.is_empty() && self.tolerances.is_empty() && self.dimensions.is_empty()
    }
}

/// A datum, such as `A`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Datum {
    /// The `#id` of the `datum`.
    pub instance: u64,
    /// `datum.identification`, the letter on the drawing.
    pub label: String,
    /// The datum itself, and the product definition it belongs to.
    pub subject: Subject,
}

/// A geometric tolerance, such as a position tolerance of 0.75 to A|B|C.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Tolerance {
    /// The `#id` of the tolerance.
    pub instance: u64,
    /// The characteristic, lower case with spaces: `position`, `flatness`,
    /// `surface profile`, …; `geometric` if the file names none.
    pub kind: String,
    /// `geometric_tolerance.name`.
    pub name: Option<String>,
    /// `geometric_tolerance.magnitude`.
    pub magnitude: Option<Value>,
    /// The datum references, in order of precedence.
    pub datums: Vec<DatumReference>,
    /// Tolerance modifiers, lower case, such as
    /// `maximum_material_requirement`.
    pub modifiers: Vec<String>,
    /// For a tolerance per unit area or length, that unit.
    pub unit_size: Option<Value>,
    /// The toleranced shape aspect.
    pub target: Subject,
}

/// One datum in a tolerance's datum reference frame.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct DatumReference {
    /// The datum's label; a common datum's labels joined with `-`.
    pub label: String,
    /// Datum modifiers, lower case.
    pub modifiers: Vec<String>,
}

/// Whether a dimension is a size or a location.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[non_exhaustive]
pub enum DimensionKind {
    /// A `dimensional_size`: the size of one feature.
    Size,
    /// A `dimensional_location`: the distance or angle between two.
    Location,
}

/// A dimension with its values and tolerance.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Dimension {
    /// The `#id` of the dimension.
    pub instance: u64,
    /// Size or location.
    pub kind: DimensionKind,
    /// The dimension's name, such as `diameter` or `linear distance`.
    pub name: Option<String>,
    /// The values its representation gives, such as `nominal value`.
    pub values: Vec<Value>,
    /// A plus-minus tolerance's bounds, if it has one.
    pub tolerance: Option<Bounds>,
    /// The feature a size applies to (for a size with a datum feature, the
    /// dimension itself), or a location's relating feature: the one it is
    /// measured from for a `directed_dimensional_location`; for any other
    /// location, only the first written.
    pub target: Subject,
    /// For a location, its related feature: the one a directed location is
    /// measured to.
    pub related: Option<Subject>,
}

/// The lower and upper bounds of a plus-minus tolerance.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Bounds {
    /// `tolerance_value.lower_bound`.
    pub lower: Option<Value>,
    /// `tolerance_value.upper_bound`.
    pub upper: Option<Value>,
}

/// The characteristic subtypes of `geometric_tolerance`, which a simple
/// instance names directly.
const TOLERANCE_KINDS: &[&str] = &[
    "ANGULARITY_TOLERANCE",
    "CIRCULAR_RUNOUT_TOLERANCE",
    "COAXIALITY_TOLERANCE",
    "CONCENTRICITY_TOLERANCE",
    "CYLINDRICITY_TOLERANCE",
    "FLATNESS_TOLERANCE",
    "LINE_PROFILE_TOLERANCE",
    "PARALLELISM_TOLERANCE",
    "PERPENDICULARITY_TOLERANCE",
    "POSITION_TOLERANCE",
    "ROUNDNESS_TOLERANCE",
    "STRAIGHTNESS_TOLERANCE",
    "SURFACE_PROFILE_TOLERANCE",
    "SYMMETRY_TOLERANCE",
    "TOTAL_RUNOUT_TOLERANCE",
];

/// The subtypes of `geometric_tolerance_with_datum_reference`; a simple
/// instance of one keeps its datum system as attribute 4.
const WITH_DATUMS: &[&str] = &[
    "ANGULARITY_TOLERANCE",
    "CIRCULAR_RUNOUT_TOLERANCE",
    "COAXIALITY_TOLERANCE",
    "CONCENTRICITY_TOLERANCE",
    "PARALLELISM_TOLERANCE",
    "PERPENDICULARITY_TOLERANCE",
    "SYMMETRY_TOLERANCE",
    "TOTAL_RUNOUT_TOLERANCE",
];

/// Size dimensions read, with the positions of `name` and `applies_to`.
/// One with a datum feature is a shape aspect first, and applies to itself.
const SIZES: &[(&str, usize, usize)] = &[
    ("DIMENSIONAL_SIZE", 1, 0),
    ("ANGULAR_SIZE", 1, 0),
    ("DIMENSIONAL_SIZE_WITH_PATH", 1, 0),
    ("DIMENSIONAL_SIZE_WITH_DATUM_FEATURE", 5, 4),
];

/// Location dimensions read, with the positions of `name`,
/// `relating_shape_aspect` and `related_shape_aspect`. One with a datum
/// feature is a shape aspect first.
const LOCATIONS: &[(&str, usize, usize, usize)] = &[
    ("DIMENSIONAL_LOCATION", 0, 2, 3),
    ("ANGULAR_LOCATION", 0, 2, 3),
    ("DIMENSIONAL_LOCATION_WITH_PATH", 0, 2, 3),
    ("DIRECTED_DIMENSIONAL_LOCATION", 0, 2, 3),
    ("DIMENSIONAL_LOCATION_WITH_DATUM_FEATURE", 4, 6, 7),
];

/// Whether a dimension entity is a size, and the positions of its `name`,
/// its target (`applies_to`, or `relating_shape_aspect`) and a location's
/// `related_shape_aspect`; `None` if it is not one read.
fn dimension_layout(entity: &str) -> Option<(bool, usize, usize, Option<usize>)> {
    SIZES
        .iter()
        .find(|(size, ..)| *size == entity)
        .map(|&(_, name, applies_to)| (true, name, applies_to, None))
        .or_else(|| {
            LOCATIONS
                .iter()
                .find(|(location, ..)| *location == entity)
                .map(|&(_, name, relating, related)| (false, name, relating, Some(related)))
        })
}

/// Reads the semantic PMI of the file behind `graph`. `structure` must be
/// built from the same graph.
pub fn pmi(graph: &Graph<'_>, structure: &ProductStructure) -> Pmi {
    let exchange = graph.exchange();
    let definitions: HashSet<u64> = structure
        .definitions()
        .iter()
        .map(|definition| definition.instance)
        .collect();
    let mut found = Pmi {
        datums: Vec::new(),
        tolerances: Vec::new(),
        dimensions: Vec::new(),
    };

    for (node, instance) in exchange.instances().iter().enumerate() {
        let records: Vec<Record<'_>> = exchange.records(instance).collect();
        let find = |name: &str| records.iter().copied().find(|record| record.is(name));
        let simple = if instance.is_complex() {
            None
        } else {
            records.first().copied()
        };
        let simple_name = simple.map(|record| upper(record.name()));
        let simple_is = |names: &[&str]| simple_name.as_deref().is_some_and(|n| names.contains(&n));

        if let Some(record) = simple.filter(|r| r.is("DATUM")) {
            if let Some(label) = text(record.param(4)) {
                found.datums.push(Datum {
                    instance: instance.id,
                    label,
                    subject: subject(exchange, &definitions, instance.id),
                });
            }
        } else if let Some(base) =
            find("GEOMETRIC_TOLERANCE").or(simple.filter(|_| simple_is(TOLERANCE_KINDS)))
        {
            let datum_system = match (find("GEOMETRIC_TOLERANCE_WITH_DATUM_REFERENCE"), simple) {
                (Some(record), _) => record.param(0),
                (None, Some(record)) if simple_is(WITH_DATUMS) => record.param(4),
                _ => None,
            };
            let kind = records
                .iter()
                .map(|record| upper(record.name()))
                .find(|name| TOLERANCE_KINDS.contains(&name.as_str()))
                .map_or_else(
                    || "geometric".to_owned(),
                    |name| {
                        name.trim_end_matches("_TOLERANCE")
                            .to_ascii_lowercase()
                            .replace('_', " ")
                    },
                );
            let Some(target) = base.param(3).and_then(|p| p.reference()) else {
                continue;
            };
            found.tolerances.push(Tolerance {
                instance: instance.id,
                kind,
                name: text(base.param(0)),
                magnitude: base
                    .param(2)
                    .and_then(|p| p.reference())
                    .and_then(|id| value(exchange, id)),
                datums: datum_references(exchange, datum_system),
                modifiers: find("GEOMETRIC_TOLERANCE_WITH_MODIFIERS")
                    .map(|record| enumerations(record.param(0)))
                    .unwrap_or_default(),
                unit_size: find("GEOMETRIC_TOLERANCE_WITH_DEFINED_UNIT")
                    .and_then(|record| record.param(0))
                    .and_then(|p| p.reference())
                    .and_then(|id| value(exchange, id)),
                target: subject(exchange, &definitions, target),
            });
        } else if let Some((record, (size, name, target, related))) =
            simple.zip(simple_name.as_deref().and_then(dimension_layout))
        {
            let (name, target, related) = (
                record.param(name),
                record.param(target),
                related.and_then(|related| record.param(related)),
            );
            let Some(target) = target.and_then(|p| p.reference()) else {
                continue;
            };
            let (values, tolerance) = dimension_values(graph, node, instance.id);
            found.dimensions.push(Dimension {
                instance: instance.id,
                kind: if size {
                    DimensionKind::Size
                } else {
                    DimensionKind::Location
                },
                name: text(name),
                values,
                tolerance,
                target: subject(exchange, &definitions, target),
                related: related
                    .and_then(|p| p.reference())
                    .map(|id| subject(exchange, &definitions, id)),
            });
        }
    }
    found
}

/// The enumeration values in a set or list, lower case.
fn enumerations(param: Option<Param<'_>>) -> Vec<String> {
    param
        .and_then(|p| p.list())
        .into_iter()
        .flatten()
        .filter_map(|p| p.literal())
        .filter_map(Literal::enumeration)
        .map(str::to_ascii_lowercase)
        .collect()
}

/// A tolerance's datum references: through a `datum_system`'s compartments,
/// or older files' `datum_reference`s ordered by precedence.
fn datum_references(exchange: &Exchange<'_>, param: Option<Param<'_>>) -> Vec<DatumReference> {
    let ids: Vec<u64> = param
        .and_then(|p| p.list())
        .into_iter()
        .flatten()
        .filter_map(|p| p.reference())
        .collect();
    let mut references = Vec::new();
    let mut by_precedence = Vec::new();
    for id in ids {
        let Some(record) = exchange.get(id).and_then(|i| exchange.records(i).next()) else {
            continue;
        };
        if record.is("DATUM_SYSTEM") {
            let constituents = record.param(4).and_then(|p| p.list()).into_iter().flatten();
            for compartment in constituents.filter_map(|p| p.reference()) {
                if let Some(reference) = compartment_reference(exchange, compartment) {
                    references.push(reference);
                }
            }
        } else if record.is("DATUM_REFERENCE") {
            let precedence = record
                .param(0)
                .and_then(|p| p.literal())
                .and_then(Literal::to_i64)
                .unwrap_or(i64::MAX);
            if let Some(label) = record
                .param(1)
                .and_then(|p| p.reference())
                .and_then(|d| datum_label(exchange, d))
            {
                by_precedence.push((
                    precedence,
                    DatumReference {
                        label,
                        modifiers: Vec::new(),
                    },
                ));
            }
        } else if let Some(reference) = compartment_reference(exchange, id) {
            references.push(reference);
        }
    }
    by_precedence.sort_by_key(|(precedence, _)| *precedence);
    references.extend(by_precedence.into_iter().map(|(_, reference)| reference));
    references
}

/// A `datum_reference_compartment` or `_element`: its base datum (or common
/// datum) and modifiers.
fn compartment_reference(exchange: &Exchange<'_>, id: u64) -> Option<DatumReference> {
    let instance = exchange.get(id)?;
    let record = exchange.records(instance).next()?;
    if !upper(record.name()).starts_with("DATUM_REFERENCE_") {
        return None;
    }
    let base = record.param(4)?;
    let label = match base.list() {
        // A common datum: a list of datum reference elements.
        Some(elements) => elements
            .filter_map(|p| p.reference())
            .filter_map(|element| compartment_reference(exchange, element))
            .map(|element| element.label)
            .collect::<Vec<_>>()
            .join("-"),
        None => base.reference().and_then(|d| datum_label(exchange, d))?,
    };
    let modifiers = record
        .param(5)
        .map(|p| enumerations(Some(p)))
        .unwrap_or_default();
    Some(DatumReference { label, modifiers })
}

fn datum_label(exchange: &Exchange<'_>, id: u64) -> Option<String> {
    let record = exchange.get(id).and_then(|i| exchange.records(i).next())?;
    record.is("DATUM").then(|| text(record.param(4))).flatten()
}

/// A dimension's values, from the `shape_dimension_representation` its
/// `dimensional_characteristic_representation` names, and its plus-minus
/// tolerance, from a `plus_minus_tolerance` whose range is a
/// `tolerance_value`.
fn dimension_values(graph: &Graph<'_>, node: usize, id: u64) -> (Vec<Value>, Option<Bounds>) {
    let exchange = graph.exchange();
    let mut values = Vec::new();
    let mut tolerance = None;
    for &user in graph.referenced_by(node) {
        let Some(record) = exchange.records(graph.instance(user)).next() else {
            continue;
        };
        let points_at = |index| record.param(index).and_then(|p| p.reference()) == Some(id);
        if record.is("DIMENSIONAL_CHARACTERISTIC_REPRESENTATION") && points_at(0) {
            let items = record
                .param(1)
                .and_then(|p| p.reference())
                .map(|rep| representation_items(exchange, rep))
                .unwrap_or_default();
            values.extend(items.into_iter().filter_map(|item| value(exchange, item)));
        } else if record.is("PLUS_MINUS_TOLERANCE") && points_at(1) {
            let range = record
                .param(0)
                .and_then(|p| p.reference())
                .and_then(|range| exchange.get(range))
                .and_then(|range| exchange.records(range).next())
                .filter(|range| range.is("TOLERANCE_VALUE"));
            if let Some(range) = range {
                let bound = |index| {
                    range
                        .param(index)
                        .and_then(|p| p.reference())
                        .and_then(|b| value(exchange, b))
                };
                tolerance = Some(Bounds {
                    lower: bound(0),
                    upper: bound(1),
                });
            }
        }
    }
    (values, tolerance)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::p21::parse;

    /// Datums A and B (B with a modifier), a position tolerance to them and
    /// a flatness tolerance on a hole, a toleranced diameter and a location.
    pub(crate) const SAMPLE: &str = "ISO-10303-21;HEADER;FILE_SCHEMA(('AP242'));ENDSEC;DATA;
#1=APPLICATION_CONTEXT('');
#2=PRODUCT_DEFINITION_CONTEXT('',#1,'design');
#10=PRODUCT('P','part','',());
#11=PRODUCT_DEFINITION_FORMATION('','',#10);
#12=PRODUCT_DEFINITION('design','',#11,#2);
#20=PRODUCT_DEFINITION_SHAPE('','',#12);
#30=DATUM('',$,#20,.F.,'A');
#31=DATUM('',$,#20,.F.,'B');
#40=DATUM_REFERENCE_COMPARTMENT('',$,#20,.F.,#30,$);
#41=DATUM_REFERENCE_COMPARTMENT('',$,#20,.F.,#31,(.MAXIMUM_MATERIAL_REQUIREMENT.));
#50=DATUM_SYSTEM('DS',$,#20,.F.,(#40,#41));
#60=SHAPE_ASPECT('','hole',#20,.F.);
#61=SHAPE_ASPECT('','face',#20,.F.);
#70=(LENGTH_MEASURE_WITH_UNIT()MEASURE_REPRESENTATION_ITEM()MEASURE_WITH_UNIT(LENGTH_MEASURE(0.75),#99)REPRESENTATION_ITEM(''));
#80=(GEOMETRIC_TOLERANCE('Position.1','',#70,#60)GEOMETRIC_TOLERANCE_WITH_DATUM_REFERENCE((#50))GEOMETRIC_TOLERANCE_WITH_MODIFIERS((.MAXIMUM_MATERIAL_REQUIREMENT.))POSITION_TOLERANCE());
#81=FLATNESS_TOLERANCE('Flatness.1','',#82,#61);
#82=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(0.05),#99);
#90=DIMENSIONAL_SIZE(#60,'diameter');
#91=DIMENSIONAL_CHARACTERISTIC_REPRESENTATION(#90,#92);
#92=SHAPE_DIMENSION_REPRESENTATION('',(#93),#1);
#93=(LENGTH_MEASURE_WITH_UNIT()MEASURE_REPRESENTATION_ITEM()MEASURE_WITH_UNIT(LENGTH_MEASURE(35.),#99)REPRESENTATION_ITEM('nominal value'));
#94=PLUS_MINUS_TOLERANCE(#95,#90);
#95=TOLERANCE_VALUE(#96,#97);
#96=(LENGTH_MEASURE_WITH_UNIT()MEASURE_REPRESENTATION_ITEM()MEASURE_WITH_UNIT(LENGTH_MEASURE(-0.2),#99)REPRESENTATION_ITEM(''));
#97=(LENGTH_MEASURE_WITH_UNIT()MEASURE_REPRESENTATION_ITEM()MEASURE_WITH_UNIT(LENGTH_MEASURE(0.),#99)REPRESENTATION_ITEM(''));
#98=DIMENSIONAL_LOCATION('linear distance',$,#60,#61);
#99=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));
ENDSEC;END-ISO-10303-21;";

    fn found() -> Pmi {
        let graph = Graph::new(parse(SAMPLE.as_bytes()).unwrap()).unwrap();
        let structure = ProductStructure::new(&graph);
        pmi(&graph, &structure)
    }

    fn on(instance: u64, entity: &str) -> Subject {
        Subject {
            instance,
            entity: entity.to_owned(),
            product_definition: Some(12),
        }
    }

    fn length(instance: u64, name: Option<&str>, value: &str) -> Value {
        Value {
            instance,
            name: name.map(str::to_owned),
            measure: Some("LENGTH_MEASURE".to_owned()),
            value: value.to_owned(),
            unit: Some(99),
        }
    }

    #[test]
    fn datums_are_read_by_letter() {
        let labels: Vec<(u64, String)> = found()
            .datums
            .into_iter()
            .map(|datum| (datum.subject.product_definition.unwrap(), datum.label))
            .collect();
        assert_eq!(labels, [(12, "A".to_owned()), (12, "B".to_owned())]);
    }

    #[test]
    fn tolerances_have_their_characteristic_magnitude_and_datums() {
        let found = found();
        assert_eq!(
            found.tolerances[0],
            Tolerance {
                instance: 80,
                kind: "position".to_owned(),
                name: Some("Position.1".to_owned()),
                magnitude: Some(length(70, Some(""), "0.75")),
                datums: vec![
                    DatumReference {
                        label: "A".to_owned(),
                        modifiers: vec![],
                    },
                    DatumReference {
                        label: "B".to_owned(),
                        modifiers: vec!["maximum_material_requirement".to_owned()],
                    },
                ],
                modifiers: vec!["maximum_material_requirement".to_owned()],
                unit_size: None,
                target: on(60, "SHAPE_ASPECT"),
            }
        );
        let flatness = &found.tolerances[1];
        assert_eq!(flatness.kind, "flatness");
        assert_eq!(flatness.magnitude, Some(length(82, None, "0.05")));
        assert!(flatness.datums.is_empty(), "{:?}", flatness.datums);
        assert_eq!(flatness.target, on(61, "SHAPE_ASPECT"));
    }

    #[test]
    fn dimensions_have_values_tolerances_and_features() {
        let found = found();
        assert_eq!(
            found.dimensions,
            [
                Dimension {
                    instance: 90,
                    kind: DimensionKind::Size,
                    name: Some("diameter".to_owned()),
                    values: vec![length(93, Some("nominal value"), "35.")],
                    tolerance: Some(Bounds {
                        lower: Some(length(96, Some(""), "-0.2")),
                        upper: Some(length(97, Some(""), "0.")),
                    }),
                    target: on(60, "SHAPE_ASPECT"),
                    related: None,
                },
                Dimension {
                    instance: 98,
                    kind: DimensionKind::Location,
                    name: Some("linear distance".to_owned()),
                    values: vec![],
                    tolerance: None,
                    target: on(60, "SHAPE_ASPECT"),
                    related: Some(on(61, "SHAPE_ASPECT")),
                },
            ]
        );
        assert!(!found.is_empty());
    }

    /// A directed location, and a size and a location with a datum feature,
    /// which are shape aspects first: their names and features come after
    /// the shape aspect's attributes, and a size with a datum feature
    /// applies to itself. Each target, complex and derived ones too, has its
    /// product definition.
    #[test]
    fn dimensions_with_datum_features_directions_and_subjects() {
        let src = SAMPLE.replace(
            "ENDSEC;END-ISO-10303-21;",
            "#100=DIRECTED_DIMENSIONAL_LOCATION('linear distance',$,#61,#60);
#101=DIMENSIONAL_SIZE_WITH_DATUM_FEATURE('','',#20,.T.,#101,'diameter');
#102=DIMENSIONAL_LOCATION_WITH_DATUM_FEATURE('','',#20,.T.,'linear distance',$,#60,#61);
#103=(COMPOSITE_SHAPE_ASPECT()DATUM_FEATURE()SHAPE_ASPECT('','',#20,.T.));
#104=CENTRE_OF_SYMMETRY('','',#20,.T.);
#105=FLATNESS_TOLERANCE('','',#82,#103);
#106=POSITION_TOLERANCE('','',#82,#104);
#107=FLATNESS_TOLERANCE('','',#82,#101);
ENDSEC;END-ISO-10303-21;",
        );
        let graph = Graph::new(parse(src.as_bytes()).unwrap()).unwrap();
        let structure = ProductStructure::new(&graph);
        let found = pmi(&graph, &structure);
        let dimension = |id: u64| found.dimensions.iter().find(|d| d.instance == id).unwrap();
        let directed = dimension(100);
        assert_eq!(directed.kind, DimensionKind::Location);
        assert_eq!(
            (
                directed.target.instance,
                directed.related.as_ref().unwrap().instance
            ),
            (61, 60)
        );
        let size = dimension(101);
        assert_eq!(size.kind, DimensionKind::Size);
        assert_eq!(size.name.as_deref(), Some("diameter"));
        assert_eq!(size.target, on(101, "DIMENSIONAL_SIZE_WITH_DATUM_FEATURE"));
        let location = dimension(102);
        assert_eq!(location.name.as_deref(), Some("linear distance"));
        assert_eq!(
            (
                location.target.instance,
                location.related.as_ref().unwrap().instance
            ),
            (60, 61)
        );
        for tolerance in &found.tolerances {
            assert_eq!(
                tolerance.target.product_definition,
                Some(12),
                "{tolerance:?}"
            );
        }
    }
}