ifc_lite_export/plan/mod.rs
1// SPDX-License-Identifier: MPL-2.0
2//! The export-plan executor: callers speak IFC (objects by express id or
3//! GlobalId, properties by measure type, placements by matrix), and
4//! [`execute`] is the one place that lowers a plan into the STEP-level
5//! primitives the rest of this crate already carries
6//! (`AttrMutation`-shaped edits, `CopyOnWriteMutation`'s copy-then-repoint
7//! shape, [`crate::step_cow`]'s id allocator role) plus the parts no STEP
8//! option expresses: occurrence overrides, placement rewrites, body geometry
9//! replacement, and delete with reverse-reference repair.
10//! [`Disposition::Add`] builds a new object (placement, geometry,
11//! containment, type, property sets, material, classifications, an opening
12//! in a host) from an IFC-level description; see `add.rs`.
13//! [`Disposition::AddType`] adds a type nothing instances yet.
14//!
15//! # Design
16//!
17//! Memory follows the plan, not the file. [`index::FileIndex::scan`] makes
18//! one pass that keeps where each record is and little else; attributes are
19//! parsed on demand, only for records the plan reaches; who refers to a
20//! record is collected by [`referrers`] only for the records a plan asks
21//! about, in streaming passes over the file that repeat until
22//! [`resolve::resolve`] completes without asking about anything new
23//! ([`referrers`] explains how many passes that takes, and why).
24//! [`write::write_output`] then streams the output in file order, validating
25//! each record as it goes. A single [`crate::step_ids::IdAllocator`], the one
26//! every STEP writer in this crate allocates with, is threaded through every
27//! appender so a plan's copy-on-write, an occurrence override, and a
28//! synthesized owner history can never land on the same id.
29//!
30//! # What this does not do
31//!
32//! - It does not convert between IFC2X3/IFC4/IFC4X3 — `crate::schema_convert`
33//! already does that, as a separate concern a caller composes with this one.
34//! - A property or quantity write never edits a shared record in place.
35//! Sharing is judged at the set (how many relationships or types reach it)
36//! and at the value (how many sets list it); either one shared means a
37//! copy, or a refusal for a set shared between types.
38
39mod add;
40mod add_assoc_types;
41mod add_builders;
42mod add_geometry;
43mod add_profile;
44mod add_props;
45mod add_rels;
46mod add_types;
47mod attribution;
48mod attrs;
49mod builders;
50mod delete;
51mod geometry;
52mod identity;
53mod index;
54mod orphans;
55mod owner_history;
56mod placement;
57mod patch;
58mod props;
59mod props_lookup;
60mod props_synth;
61mod props_target;
62mod referrers;
63mod removal;
64mod resolve;
65mod skeleton;
66mod source_text;
67mod tessellation;
68#[cfg(test)]
69mod add_test_support;
70#[cfg(test)]
71mod test_fixtures;
72mod types;
73mod validate;
74mod write;
75
76pub use add_assoc_types::{
77 ClassificationLink, ClassificationSystem, LayerMaterial, MaterialLink, NewClassification, NewClassificationReference,
78 NewLayerSet, NewMaterial, NewMaterialLayer, NewProperty, NewPropertySet, PropertyValue, ValueUnit,
79};
80pub use add_types::{
81 ClassSpec, Extrusion, GlobalId, IfcClass, MappedBody, NewObject, NewOpening, NewPlacement, NewType, Representation, TypeLink,
82};
83pub use placement::IDENTITY;
84pub use removal::{Removal, RemovalReason};
85pub use skeleton::{skeleton, skeleton_with_findings, MapConversion, MapUnit, Skeleton, SkeletonRoot, SKELETON_SCHEMA};
86pub use types::{
87 Disposition, DroppedRepresentation, ExportPlan, ExportingApplication, ExportingPerson, GeometryPatch, ObjectRef, Patch, PlacementPatch, PlanContext, PlanError,
88 PropScope, PropWrite, PsetKind, Refusal, SurfaceColour, TriangleMesh, ValidationFinding, WriteReport,
89};
90
91use rustc_hash::FxHashSet;
92
93use index::FileIndex;
94
95/// Execute `plan` against `source`, writing the result to `out`.
96///
97/// Callers never pass express ids for attributes or attribute indices, only
98/// [`ObjectRef`], property values as STEP literals with their measure type
99/// (each checked to be one value), and a 4x4 placement matrix. See the
100/// module docs for what is deliberately out of scope. `out` is buffered here.
101pub fn execute<W: std::io::Write>(source: &[u8], plan: &ExportPlan, out: W) -> Result<WriteReport, PlanError> {
102 // An Add's own GlobalId is looked up too: it must not already be in use.
103 let add_guids: Vec<String> = plan
104 .dispositions
105 .iter()
106 .filter_map(|(_, d)| match d {
107 Disposition::Add(new) => Some(new.guids_to_look_up()),
108 Disposition::AddType(t) => Some(t.guids_to_look_up()),
109 _ => None,
110 })
111 .flatten()
112 .collect();
113 let guids: FxHashSet<&str> = plan
114 .dispositions
115 .iter()
116 .filter_map(|(obj, _)| match obj {
117 ObjectRef::Guid(g) => Some(g.as_str()),
118 ObjectRef::Id(_) => None,
119 })
120 .chain(add_guids.iter().map(String::as_str))
121 .collect();
122 let mut index = FileIndex::scan(source, &guids);
123 let schema = crate::schema_detect::detect_schema(source);
124 let resolution = loop {
125 let resolution = resolve::resolve(&index, &schema, plan);
126 if !index.collect_missing_referrers() {
127 break resolution;
128 }
129 };
130 write::write_output(&index, &schema, plan, resolution, out)
131}
132
133#[cfg(test)]
134#[path = "exec_tests.rs"]
135mod exec_tests;
136#[cfg(test)]
137#[path = "combination_tests.rs"]
138mod combination_tests;
139#[cfg(test)]
140#[path = "identity_tests.rs"]
141mod identity_tests;