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
//! Product properties: user-defined attributes, validation properties and
//! persistent identifiers.
//!
//! This is what `stepq props` prints. A property is a `property_definition`
//! that points at what it describes: a product definition, its shape, or a
//! shape aspect of it. Its values are the items of the representations that
//! `property_definition_representation`s tie to it. Values are returned as
//! written: numbers are the source text, never re-parsed and re-printed.

use std::collections::HashSet;

use crate::model::{Graph, ProductStructure};
use crate::p21::{Exchange, Literal, Param, Record};
use crate::record::{entity, text, upper};

/// Everything [`properties`] found.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Properties {
    /// Every `property_definition`, in file order.
    pub properties: Vec<Property>,
    /// Every `id_attribute`, in file order.
    pub identifiers: Vec<Identifier>,
}

/// One `property_definition` and its values.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Property {
    /// The `#id` of the `property_definition`.
    pub instance: u64,
    /// What kind of property this is.
    pub kind: PropertyKind,
    /// `property_definition.name`.
    pub name: Option<String>,
    /// `property_definition.description`.
    pub description: Option<String>,
    /// What the property describes.
    pub subject: Subject,
    /// Values from every representation tied to the property, in order.
    pub values: Vec<Value>,
}

impl Property {
    /// A name for display: the description of a validation property
    /// (`volume of #602`), otherwise the name, falling back to the
    /// description.
    pub fn label(&self) -> &str {
        let name = self.name.as_deref().filter(|name| !name.is_empty());
        let description = self.description.as_deref().filter(|d| !d.is_empty());
        let label = if self.kind == PropertyKind::Validation {
            description.or(name)
        } else {
            name.or(description)
        };
        label.unwrap_or_default()
    }
}

/// The kind of a property.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[non_exhaustive]
pub enum PropertyKind {
    /// A validation property: geometric (volume, area, centroid, …) or of
    /// attributes (how many user attributes of each type), named
    /// `geometric validation property` or `attribute validation property`,
    /// with spaces or underscores.
    Validation,
    /// A user-defined attribute: associated with a `general_property`, or
    /// described as `user defined attribute`. Serialized as `user`, the
    /// name `stepq props --kind` uses.
    #[cfg_attr(feature = "serde", serde(rename = "user"))]
    UserDefined,
    /// Anything else.
    Other,
}

/// What a property or identifier is attached to.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Subject {
    /// The `#id` the property or identifier points at.
    pub instance: u64,
    /// Its upper-cased entity type; for a complex instance, the partial
    /// entities joined with `+`.
    pub entity: String,
    /// The product definition it belongs to, found by following product
    /// definition shapes, shape aspects and property definitions; `None` if
    /// that leads nowhere, as for a property of an assembly usage.
    pub product_definition: Option<u64>,
}

/// One value of a property: one representation item.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Value {
    /// The `#id` of the representation item.
    pub instance: u64,
    /// The item's name.
    pub name: Option<String>,
    /// The measure type, such as `VOLUME_MEASURE`, for measure and value
    /// items; the entity type for other items.
    pub measure: Option<String>,
    /// The value as written: a number's source text, a decoded string, or
    /// for items that are not values (a centroid point, say) the item's
    /// whole instance text.
    pub value: String,
    /// The `#id` of the unit, for measures with one.
    pub unit: Option<u64>,
}

/// One `id_attribute`: a persistent identifier.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Identifier {
    /// The `#id` of the `id_attribute`.
    pub instance: u64,
    /// The identifier.
    pub id: String,
    /// What it identifies.
    pub subject: Subject,
}

/// Finds every property and identifier in the file behind `graph`.
/// `structure` must be built from the same graph; it says which instances
/// are product definitions.
pub fn properties(graph: &Graph<'_>, structure: &ProductStructure) -> Properties {
    let exchange = graph.exchange();
    let definitions: HashSet<u64> = structure
        .definitions()
        .iter()
        .map(|definition| definition.instance)
        .collect();
    let mut properties = Vec::new();
    let mut identifiers = Vec::new();

    for (node, instance) in exchange.instances().iter().enumerate() {
        let Some(record) = exchange.records(instance).next() else {
            continue;
        };
        if instance.is_complex() {
            continue;
        }
        if record.is("PROPERTY_DEFINITION") {
            let Some(target) = record.param(2).and_then(|p| p.reference()) else {
                continue;
            };
            let name = text(record.param(0));
            let description = text(record.param(1));
            let mut values = Vec::new();
            let mut general = false;
            for &user in graph.referenced_by(node) {
                let user = graph.instance(user);
                let Some(link) = exchange.records(user).next() else {
                    continue;
                };
                let points_here =
                    |index| link.param(index).and_then(|p| p.reference()) == Some(instance.id);
                if (link.is("PROPERTY_DEFINITION_REPRESENTATION")
                    || link.is("SHAPE_DEFINITION_REPRESENTATION"))
                    && points_here(0)
                {
                    let items = link
                        .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 link.is("GENERAL_PROPERTY_ASSOCIATION") && points_here(3) {
                    general = true;
                }
            }
            // AP214 practice writes `geometric_validation_property`, AP242
            // practice `geometric validation property`.
            let is = |field: &Option<String>, expected: &str| {
                field
                    .as_deref()
                    .is_some_and(|field| field.replace('_', " ").eq_ignore_ascii_case(expected))
            };
            let kind = if is(&name, "geometric validation property")
                || is(&name, "attribute validation property")
            {
                PropertyKind::Validation
            } else if general || is(&description, "user defined attribute") {
                PropertyKind::UserDefined
            } else {
                PropertyKind::Other
            };
            properties.push(Property {
                instance: instance.id,
                kind,
                name,
                description,
                subject: subject(exchange, &definitions, target),
                values,
            });
        } else if record.is("ID_ATTRIBUTE") {
            let (Some(id), Some(target)) = (
                text(record.param(0)),
                record.param(1).and_then(|p| p.reference()),
            ) else {
                continue;
            };
            identifiers.push(Identifier {
                instance: instance.id,
                id,
                subject: subject(exchange, &definitions, target),
            });
        }
    }
    Properties {
        properties,
        identifiers,
    }
}

/// What `#target` is and which of `definitions` it belongs to.
pub(crate) fn subject(exchange: &Exchange<'_>, definitions: &HashSet<u64>, target: u64) -> Subject {
    let entity = exchange
        .get(target)
        .map(|instance| entity(exchange, instance))
        .unwrap_or_default();
    // Shapes, shape aspects (and their subtypes, such as datums) and
    // property definitions all keep what they describe as attribute 2.
    let mut current = target;
    let mut product_definition = None;
    for _ in 0..16 {
        if definitions.contains(&current) {
            product_definition = Some(current);
            break;
        }
        let Some(instance) = exchange.get(current) else {
            break;
        };
        // A complex instance keeps of_shape in its SHAPE_ASPECT partial (a
        // COMPOSITE_SHAPE_ASPECT partial holds nothing); a simple one is a
        // shape aspect subtype that keeps it third.
        let record = if instance.is_complex() {
            [
                "SHAPE_ASPECT",
                "PRODUCT_DEFINITION_SHAPE",
                "PROPERTY_DEFINITION",
            ]
            .iter()
            .find_map(|name| crate::record::partial(exchange, instance, name))
        } else {
            exchange.records(instance).next().filter(|record| {
                let name = upper(record.name());
                name == "PRODUCT_DEFINITION_SHAPE"
                    || name == "PROPERTY_DEFINITION"
                    || crate::record::SHAPE_ASPECTS.contains(&name.as_str())
            })
        };
        let Some(next) = record.and_then(|r| r.param(2)).and_then(|p| p.reference()) else {
            break;
        };
        current = next;
    }
    Subject {
        instance: target,
        entity,
        product_definition,
    }
}

/// The items of representation `#id`, in order.
pub(crate) fn representation_items(exchange: &Exchange<'_>, id: u64) -> Vec<u64> {
    let Some(instance) = exchange.get(id) else {
        return Vec::new();
    };
    let mut records = exchange.records(instance);
    let record = if instance.is_complex() {
        records.find(|r| r.is("REPRESENTATION"))
    } else {
        records.next()
    };
    record
        .and_then(|r| r.param(1))
        .and_then(|p| p.list())
        .into_iter()
        .flatten()
        .filter_map(|p| p.reference())
        .collect()
}

/// Instance `#id` read as a value: a measure, a descriptive or literal item,
/// or failing those its whole text.
pub(crate) fn value(exchange: &Exchange<'_>, id: u64) -> Option<Value> {
    let instance = exchange.get(id)?;
    let records: Vec<Record<'_>> = exchange.records(instance).collect();
    let find = |name: &str| records.iter().copied().find(|r| r.is(name));
    let reference = |param: Option<Param<'_>>| param.and_then(|p| p.reference());

    if instance.is_complex() {
        if let (Some(item), Some(measure)) =
            (find("REPRESENTATION_ITEM"), find("MEASURE_WITH_UNIT"))
        {
            let (kind, value) = typed(measure.param(0));
            return Some(Value {
                instance: id,
                name: text(item.param(0)),
                measure: kind,
                value,
                unit: reference(measure.param(1)),
            });
        }
    } else if let Some(record) = records.first().copied() {
        if record.is("MEASURE_REPRESENTATION_ITEM") || record.is("VALUE_REPRESENTATION_ITEM") {
            let (kind, value) = typed(record.param(1));
            return Some(Value {
                instance: id,
                name: text(record.param(0)),
                measure: kind,
                value,
                unit: reference(record.param(2)),
            });
        }
        // LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(0.05),#99) and the like.
        if upper(record.name()).ends_with("MEASURE_WITH_UNIT") && record.params().count() == 2 {
            let (kind, value) = typed(record.param(0));
            return Some(Value {
                instance: id,
                name: None,
                measure: kind,
                value,
                unit: reference(record.param(1)),
            });
        }
        if record.is("DESCRIPTIVE_REPRESENTATION_ITEM") {
            return Some(Value {
                instance: id,
                name: text(record.param(0)),
                measure: None,
                value: text(record.param(1)).unwrap_or_default(),
                unit: None,
            });
        }
        // INTEGER_, REAL_, BOOLEAN_, STRING_REPRESENTATION_ITEM and the
        // like: a name and a plain literal.
        let literal = record.param(1).and_then(|p| p.literal());
        if upper(record.name()).ends_with("_REPRESENTATION_ITEM") && record.params().count() == 2 {
            if let Some(literal) = literal {
                return Some(Value {
                    instance: id,
                    name: text(record.param(0)),
                    measure: None,
                    value: literal_text(literal),
                    unit: None,
                });
            }
        }
    }
    // Not a value item: a centroid point, a placement, … Keep it whole.
    Some(Value {
        instance: id,
        name: records.first().and_then(|r| text(r.param(0))),
        measure: Some(entity(exchange, instance)),
        value: String::from_utf8_lossy(exchange.text(instance)).into_owned(),
        unit: None,
    })
}

/// A typed value such as `VOLUME_MEASURE(96858.9)`: its type and its
/// value's source text.
fn typed(param: Option<Param<'_>>) -> (Option<String>, String) {
    let Some(param) = param else {
        return (None, String::new());
    };
    match param.typed() {
        Some(record) => (
            Some(upper(record.name())),
            record
                .param(0)
                .and_then(|p| p.literal())
                .map(literal_text)
                .unwrap_or_default(),
        ),
        None => (None, param.literal().map(literal_text).unwrap_or_default()),
    }
}

/// A string literal decoded, or any other literal's source text.
fn literal_text(literal: Literal<'_>) -> String {
    let source = literal.text();
    if source.first() == Some(&b'\'') {
        if let Ok(decoded) = literal.decode() {
            return decoded.into_owned();
        }
    }
    String::from_utf8_lossy(source).into_owned()
}

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

    /// A part with a validated volume on a shape aspect, a user-defined
    /// attribute with three values, a property of nothing product-shaped,
    /// and a persistent identifier.
    pub(crate) const SAMPLE: &str = "ISO-10303-21;HEADER;FILE_SCHEMA(('AP242'));ENDSEC;DATA;
#1=APPLICATION_CONTEXT('');
#10=PRODUCT('P1','bracket','',());
#11=PRODUCT_DEFINITION_FORMATION('','',#10);
#12=PRODUCT_DEFINITION('design','',#11,#13);
#13=PRODUCT_DEFINITION_CONTEXT('',#1,'design');
#20=PRODUCT_DEFINITION_SHAPE('','',#12);
#21=SHAPE_ASPECT('','solid',#20,.F.);
#30=PROPERTY_DEFINITION('geometric_validation_property','volume of solid',#21);
#31=PROPERTY_DEFINITION_REPRESENTATION(#30,#32);
#32=REPRESENTATION('volume',(#33),#1);
#33=MEASURE_REPRESENTATION_ITEM('volume measure',VOLUME_MEASURE(96858.91343205),#99);
#40=PROPERTY_DEFINITION('MATERIAL','user defined attribute',#12);
#41=GENERAL_PROPERTY('','MATERIAL','user defined attribute');
#42=GENERAL_PROPERTY_ASSOCIATION('user defined attribute','',#41,#40);
#43=PROPERTY_DEFINITION_REPRESENTATION(#40,#44);
#44=REPRESENTATION('',(#45,#46,#47),#1);
#45=DESCRIPTIVE_REPRESENTATION_ITEM('MATERIAL','Steel ''S235''');
#46=VALUE_REPRESENTATION_ITEM('THICKNESS',LENGTH_MEASURE(1.E0));
#47=(MEASURE_REPRESENTATION_ITEM()MEASURE_WITH_UNIT(MASS_MEASURE(2.5),#99)REPRESENTATION_ITEM('mass'));
#50=PROPERTY_DEFINITION('centroid','',#1);
#51=PROPERTY_DEFINITION_REPRESENTATION(#50,#52);
#52=REPRESENTATION('',(#53),#1);
#53=CARTESIAN_POINT('centre',(1.,2.,3.));
#70=ID_ATTRIBUTE('aspect.1',#21);
#99=DERIVED_UNIT(());
ENDSEC;END-ISO-10303-21;";

    #[test]
    fn ap242_spelling_and_literal_items() {
        let src = "ISO-10303-21;HEADER;FILE_SCHEMA(('AP242'));ENDSEC;DATA;
#1=APPLICATION_CONTEXT('');
#10=PRODUCT('P1','bracket','',());
#11=PRODUCT_DEFINITION_FORMATION('','',#10);
#12=PRODUCT_DEFINITION('design','',#11,#13);
#13=PRODUCT_DEFINITION_CONTEXT('',#1,'design');
#30=PROPERTY_DEFINITION('attribute validation property','',#12);
#31=PROPERTY_DEFINITION_REPRESENTATION(#30,#32);
#32=REPRESENTATION('',(#33,#34),#1);
#33=INTEGER_REPRESENTATION_ITEM('text user attributes',3.);
#34=STRING_REPRESENTATION_ITEM('note','it''s');
#40=PROPERTY_DEFINITION('Geometric Validation Property','volume',#12);
ENDSEC;END-ISO-10303-21;";
        let graph = Graph::new(parse(src.as_bytes()).unwrap()).unwrap();
        let found = properties(&graph, &ProductStructure::new(&graph));
        assert_eq!(found.properties[0].kind, PropertyKind::Validation);
        assert_eq!(found.properties[1].kind, PropertyKind::Validation);
        assert_eq!(
            found.properties[0].values,
            [
                Value {
                    instance: 33,
                    name: Some("text user attributes".to_owned()),
                    measure: None,
                    value: "3.".to_owned(),
                    unit: None,
                },
                Value {
                    instance: 34,
                    name: Some("note".to_owned()),
                    measure: None,
                    value: "it's".to_owned(),
                    unit: None,
                },
            ]
        );
    }

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

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

    #[test]
    fn validation_properties_belong_to_the_part_through_their_shape_aspect() {
        let found = found();
        let volume = &found.properties[0];
        assert_eq!(volume.kind, PropertyKind::Validation);
        assert_eq!(volume.label(), "volume of solid");
        assert_eq!(
            volume.subject,
            Subject {
                instance: 21,
                entity: "SHAPE_ASPECT".to_owned(),
                product_definition: Some(12),
            }
        );
        assert_eq!(
            volume.values,
            [value(
                33,
                "volume measure",
                Some("VOLUME_MEASURE"),
                "96858.91343205",
                Some(99)
            )]
        );
    }

    #[test]
    fn user_defined_attributes_keep_values_as_written() {
        let found = found();
        let material = &found.properties[1];
        assert_eq!(material.kind, PropertyKind::UserDefined);
        assert_eq!(material.label(), "MATERIAL");
        assert_eq!(material.subject.product_definition, Some(12));
        assert_eq!(
            material.values,
            [
                value(45, "MATERIAL", None, "Steel 'S235'", None),
                value(46, "THICKNESS", Some("LENGTH_MEASURE"), "1.E0", None),
                value(47, "mass", Some("MASS_MEASURE"), "2.5", Some(99)),
            ]
        );
    }

    #[test]
    fn other_items_and_identifiers() {
        let found = found();
        let centroid = &found.properties[2];
        assert_eq!(centroid.kind, PropertyKind::Other);
        assert_eq!(centroid.subject.product_definition, None);
        assert_eq!(centroid.subject.entity, "APPLICATION_CONTEXT");
        assert_eq!(
            centroid.values,
            [value(
                53,
                "centre",
                Some("CARTESIAN_POINT"),
                "#53=CARTESIAN_POINT('centre',(1.,2.,3.));",
                None
            )]
        );
        assert_eq!(found.properties.len(), 3);
        assert_eq!(
            found.identifiers,
            [Identifier {
                instance: 70,
                id: "aspect.1".to_owned(),
                subject: Subject {
                    instance: 21,
                    entity: "SHAPE_ASPECT".to_owned(),
                    product_definition: Some(12),
                },
            }]
        );
    }
}