Skip to main content

brep_kernel/feature_pipeline/pmi/annotations/
mod.rs

1//! The PMI annotation type table — twelve types, each a module exposing its
2//! [`PmiTypeDef`] (`DEF`), its `schema()` and its resolver. The table is the
3//! third applicability family beside the feature catalogue and the assembly
4//! constraint types: an annotation predicate accepts plain AND component
5//! geometry.
6
7pub mod balloon;
8pub mod angle;
9pub mod centerline;
10pub mod datum;
11pub mod explode;
12pub mod fcf;
13pub mod hide;
14pub mod hole_callout;
15pub mod weld_callout;
16pub mod leader;
17pub mod linear;
18pub mod note;
19pub mod radial;
20
21use super::{PmiAnnotation, PmiContext, Resolved};
22use crate::feature_pipeline::SelectionProbe;
23use serde_json::{Map, Value};
24
25/// One annotation type.
26pub struct PmiTypeDef {
27    /// Canonical `type` string (the persisted key).
28    pub type_id: &'static str,
29    /// Id-mint prefix (`DIM` → `DIM3`).
30    pub short_name: &'static str,
31    /// The type's ONE icon — a catalogued glyph character (the app's
32    /// `assets/glyphs` set). Every surface that stands for the type by picture
33    /// (tree row, viewport chip, context offer) reads this.
34    pub icon: &'static str,
35    /// `"{icon} {label}"`.
36    pub long_name: &'static str,
37    /// Plain label for messages and the tree.
38    pub label: &'static str,
39    /// Does the current selection make creating this annotation meaningful?
40    pub applicable: fn(&SelectionProbe) -> bool,
41    /// The annotation's schema (`{type, shortName, longName, label, icon,
42    /// inputParamsSchema}`), the shape the dialog engine consumes.
43    pub schema: fn() -> Value,
44    /// Resolve the annotation against the scene.
45    pub resolve: fn(&PmiAnnotation, &PmiContext<'_>) -> Result<Resolved, String>,
46}
47
48/// Panel / `+` dropdown order.
49pub const PMI_TYPES: [PmiTypeDef; 13] = [
50    linear::DEF,
51    radial::DEF,
52    angle::DEF,
53    leader::DEF,
54    balloon::DEF,
55    note::DEF,
56    hole_callout::DEF,
57    weld_callout::DEF,
58    centerline::DEF,
59    datum::DEF,
60    fcf::DEF,
61    explode::DEF,
62    hide::DEF,
63];
64
65/// Look up a type by its canonical `type` string.
66pub fn pmi_type(type_id: &str) -> Option<&'static PmiTypeDef> {
67    PMI_TYPES.iter().find(|def| def.type_id == type_id)
68}
69
70/// The twelve annotation schemas, table order.
71pub fn pmi_schema_catalogue() -> Value {
72    Value::Array(PMI_TYPES.iter().map(|def| (def.schema)()).collect())
73}
74
75/// The solids a list of SOLID / COMPONENT references names, `(scene name,
76/// handle)`, each once: a solid by its name, a component (`ACOMP1`, or a
77/// nested `ACOMP1:ACOMP2`) by every solid it owns. What an explode step
78/// moves and a hide step hides.
79pub(super) fn target_solids(targets: &[String], context: &PmiContext<'_>) -> Result<Vec<(String, u32)>, String> {
80    if targets.is_empty() {
81        return Err("select at least one solid or component".into());
82    }
83    let mut solids: Vec<(String, u32)> = Vec::new();
84    for target in targets {
85        if let Some(handle) = context.scene.resolve_solid(target) {
86            solids.push((target.clone(), handle));
87        } else if crate::is_component_reference(crate::split_component_namespace(target).1) {
88            let members = context.scene.occurrence_solids(target);
89            if members.is_empty() {
90                return Err(format!("component '{target}' has no solids"));
91            }
92            solids.extend(members);
93        } else {
94            return Err(format!("'{target}' is not a solid or component"));
95        }
96    }
97    let mut seen = std::collections::HashSet::new();
98    solids.retain(|(name, _)| seen.insert(name.clone()));
99    Ok(solids)
100}
101
102// --- shared schema-field builders --------------------------------------------
103
104pub(super) fn id_field() -> Value {
105    serde_json::json!({
106        "type": "string",
107        "default_value": null,
108        "hint": "Unique identifier for the annotation"
109    })
110}
111
112pub(super) fn reference_field(
113    label: &str,
114    filter: &[&str],
115    multiple: bool,
116    min: usize,
117    max: usize,
118    hint: &str,
119) -> Value {
120    serde_json::json!({
121        "type": "reference_selection",
122        "label": label,
123        "selectionFilter": filter,
124        "multiple": multiple,
125        "minSelections": min,
126        "maxSelections": max,
127        "default_value": null,
128        "hint": hint
129    })
130}
131
132pub(super) fn number_field(label: &str, default: f64, step: f64, hint: &str) -> Value {
133    serde_json::json!({
134        "type": "number",
135        "label": label,
136        "default_value": default,
137        "step": step,
138        "hint": hint
139    })
140}
141
142pub(super) fn boolean_field(label: &str, default: bool, hint: &str) -> Value {
143    serde_json::json!({
144        "type": "boolean",
145        "label": label,
146        "default_value": default,
147        "hint": hint
148    })
149}
150
151pub(super) fn string_field(label: &str, default: &str, hint: &str) -> Value {
152    serde_json::json!({
153        "type": "string",
154        "label": label,
155        "default_value": default,
156        "hint": hint
157    })
158}
159
160pub(super) fn options_field(label: &str, options: &[&str], default: &str, hint: &str) -> Value {
161    serde_json::json!({
162        "type": "options",
163        "label": label,
164        "options": options,
165        "default_value": default,
166        "hint": hint
167    })
168}
169
170/// The dimension tolerance block + decimals + reference flag, appended to a
171/// dimension schema's params in this order.
172pub(super) fn dimension_fields(params: &mut Map<String, Value>, decimals_default: u64) {
173    params.insert(
174        "decimals".into(),
175        serde_json::json!({
176            "type": "number",
177            "label": "Decimals",
178            "default_value": decimals_default,
179            "step": 1,
180            "hint": "Decimal places shown (0–8)"
181        }),
182    );
183    params.insert(
184        "isReference".into(),
185        boolean_field("Reference", false, "A reference dimension: shown in parentheses, no tolerance"),
186    );
187    params.insert(
188        "tolMode".into(),
189        options_field(
190            "Tolerance",
191            &["none", "symmetric", "deviation", "limits"],
192            "none",
193            "none · ± symmetric · +upper/−lower deviation · upper/lower limit values",
194        ),
195    );
196    params.insert(
197        "tolUpper".into(),
198        number_field("Upper (+)", 0.0, 0.01, "The upper deviation (the ± value for symmetric)"),
199    );
200    params.insert(
201        "tolLower".into(),
202        number_field("Lower (−)", 0.0, 0.01, "The lower deviation (deviation / limits modes)"),
203    );
204}
205
206/// The annotation-plane field every drawn type carries (explode draws
207/// nothing): a planar face or reference plane the annotation lies in;
208/// empty aligns it to the view camera.
209pub(super) fn plane_field() -> Value {
210    reference_field(
211        "Annotation plane",
212        &["FACE", "PLANE"],
213        false,
214        0,
215        1,
216        "A planar face or reference plane the annotation lies in — leave empty to align it to the view camera",
217    )
218}
219
220pub(super) fn schema_entry(def: &PmiTypeDef, mut params: Map<String, Value>) -> Value {
221    if def.type_id != "explode" {
222        params.insert("plane".into(), plane_field());
223    }
224    serde_json::json!({
225        "type": def.type_id,
226        "shortName": def.short_name,
227        "longName": def.long_name,
228        "label": def.label,
229        "icon": def.icon,
230        "inputParamsSchema": Value::Object(params),
231    })
232}
233
234/// Build a params object in insertion order.
235pub(super) fn params(entries: Vec<(&str, Value)>) -> Map<String, Value> {
236    let mut map = Map::new();
237    for (key, value) in entries {
238        map.insert(key.to_string(), value);
239    }
240    map
241}
242
243/// The decimals param clamped to `0..=8`.
244pub(super) fn decimals_of(annotation: &PmiAnnotation, context: &PmiContext<'_>, default: f64) -> usize {
245    annotation
246        .number("decimals", context.env, default)
247        .unwrap_or(default)
248        .round()
249        .clamp(0.0, 8.0) as usize
250}