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
//! The active PMI view's annotation graphics: dimension lines, extension
//! lines, arcs, leaders and arrowheads baked into the `pmi-overlay` group
//! from the kernel's presentation layout (the SAME layout the STEP file
//! writes), sized in pixels at bake time and re-baked when the zoom or the
//! viewing direction moves materially; plus the label feed the app's chip
//! pass draws.
use super::*;
use brep_kernel::{pmi_present, pmi_type, PmiLayoutStyle, PmiStatus};
const PMI_OVERLAY_GROUP: &str = "pmi-overlay";
/// Arrowhead length, in pixels.
pub(crate) const ARROW_PX: f64 = 12.0;
/// The reference text cap height at 12 pt, in pixels (frames scale with the
/// view's text size).
pub(crate) const TEXT_PX_AT_12PT: f64 = 11.0;
/// PMI line / arrow colour (amber, distinct from the dimension gizmo's rods
/// and the constraint status colours).
const PMI_RGB: [f32; 3] = [0.98, 0.78, 0.22];
/// An error annotation's chip colour.
const ERROR_RGB: [f32; 3] = [0.97, 0.32, 0.29];
/// A disabled annotation's chip colour.
const DISABLED_RGB: [f32; 3] = [0.55, 0.55, 0.55];
/// The error a balloon shows while its target is hidden in the viewport and
/// the view's hidden list has not caught up (no run has happened).
pub(crate) const LIVE_HIDDEN_MESSAGE: &str = "target hidden in the viewport: show it, or update the view's visibility";
/// One chip of [`EngineState::pmi_labels`]; serialised, the
/// `__brepPmiLabels` row.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PmiLabel {
pub id: String,
#[serde(rename = "type")]
pub kind: String,
pub icon: &'static str,
/// What the chip reads: the resolved text, or the type's glyph and the
/// id for an error or an empty one.
pub text: String,
/// `ok` or `error`.
pub status: &'static str,
pub message: String,
pub color: [f32; 3],
/// The label anchor, world coordinates.
pub world: [f64; 3],
pub enabled: bool,
/// Its form is open.
pub open: bool,
/// Selected in the PMI tree.
pub selected: bool,
pub text_size_pt: f64,
}
impl EngineState {
/// Re-bake the overlay from the cached report (empty without an active
/// view or while a sketch edit is live).
pub fn refresh_pmi_overlay(&mut self) {
let Some(active) = self.pmi_active_view.clone() else {
self.clear_pmi_overlay();
return;
};
if self.any_sketch_mode() {
self.clear_pmi_overlay();
return;
}
// Camera movement changes an automatic leader's closest screen point.
// Fetch missing topology in one batch; the topology callback retries
// this bake once the last pending balloon can be projected.
let camera = self.snapshot_camera();
let ids: Vec<String> = self.pmi_report.as_ref().and_then(|r| r.view(&active))
.map(|v| v.annotations.iter().filter(|row| matches!(&row.geometry,
brep_kernel::PmiGeometry::Leader { anchor: Some(a), balloon: true, .. }
if matches!(a.mode, brep_kernel::BalloonAnchorMode::Surface) && a.camera.as_ref() != Some(&camera)))
.map(|row| row.id.clone()).collect()).unwrap_or_default();
let mut missing = Vec::new();
for id in &ids {
if let Some(names) = self.pmi_balloon_head_now(id) {
missing.extend(names);
self.pmi_balloon_stale = Some(id.clone());
}
}
if !missing.is_empty() {
missing.sort();
missing.dedup();
self.request_exact_solids(missing.iter().map(String::as_str));
for id in &ids { let _ = self.pmi_balloon_head_now(id); }
}
// Borrowed, not cloned: a 500-step view's rows are ~4 ms to copy, every
// animation frame. Nothing below writes `self` until the bake is done.
let Some(view) = self.pmi_report.as_ref().and_then(|report| report.view(&active)) else {
self.clear_pmi_overlay();
return;
};
let text_size = self
.pmi_state_shared()
.find_view(&active)
.map(|view| view.display.text_size_pt)
.unwrap_or(12.0);
let wpp = self.camera.world_per_pixel();
let (_, up, view_dir) = self.camera.basis();
let style = PmiLayoutStyle {
arrow: ARROW_PX * wpp,
text_height: TEXT_PX_AT_12PT * wpp * (text_size / 12.0),
view_dir,
view_up: up,
plane: None,
};
let mut lines: Vec<f32> = Vec::new();
let mut line_colors: Vec<f32> = Vec::new();
let mut tris: Vec<f32> = Vec::new();
let mut tri_colors: Vec<f32> = Vec::new();
let mut explode_step = 0;
// Part-way through playback, a balloon rides its moving target.
let playback = self.explode_playback_poses();
for row in &view.annotations {
if !row.enabled || row.status != PmiStatus::Ok || self.balloon_target_hidden_live(row) {
continue;
}
// An explode trail follows its step's playback fraction, like its bodies.
let geometry = if matches!(row.geometry, brep_kernel::PmiGeometry::Explode { .. }) {
explode_step += 1;
super::exploded_ops::explode_at(&row.geometry, self.explode_fraction(explode_step - 1))
} else {
playback.as_ref().and_then(|poses| poses.shown_geometry(row)).unwrap_or_else(|| row.geometry.clone())
};
let label = playback.as_ref().map_or(row.label_world, |poses| poses.shown(row, row.label_world));
// A picked annotation plane lays the row out in that plane.
let row_style = PmiLayoutStyle {
plane: row.plane,
..style
};
let drawn = pmi_present(&geometry, label, &row.text, &row_style);
for polyline in &drawn.polylines {
for pair in polyline.windows(2) {
for point in pair {
lines.extend(point.iter().map(|c| *c as f32));
line_colors.extend_from_slice(&PMI_RGB);
}
}
}
for arrow in &drawn.arrows {
for point in arrow {
tris.extend(point.iter().map(|c| *c as f32));
tri_colors.extend_from_slice(&PMI_RGB);
}
// Both windings, so the arrowhead reads from either side.
for point in [arrow[0], arrow[2], arrow[1]] {
tris.extend(point.iter().map(|c| *c as f32));
tri_colors.extend_from_slice(&PMI_RGB);
}
}
}
let group = if lines.is_empty() && tris.is_empty() {
serde_json::json!({ "name": PMI_OVERLAY_GROUP })
} else {
serde_json::json!({
"name": PMI_OVERLAY_GROUP,
"renderOrder": 10001,
"lines": { "positions": lines, "colors": line_colors },
"tris": { "positions": tris, "colors": tri_colors },
})
};
let _ = self.set_overlay_json(&serde_json::json!({ "groups": [group] }).to_string());
self.pmi_overlay_key = Some((if wpp > 0.0 { wpp } else { f64::MIN_POSITIVE }, view_dir));
self.dirty = true;
}
/// Whether a balloon row's target is hidden in the LIVE scene: every
/// solid its recipe projects onto is invisible now. The kernel resolves
/// against the view's stored hidden list; a hide made in the viewport
/// after the run is only known here, so the leader is withheld and the
/// chip reads as an error until the view's visibility is updated.
pub(crate) fn balloon_target_hidden_live(&self, row: &brep_kernel::PmiAnnotationReport) -> bool {
match &row.geometry {
brep_kernel::PmiGeometry::Leader { balloon: true, anchor: Some(anchor), .. } => {
!anchor.solids.is_empty()
&& anchor
.solids
.iter()
.all(|name| self.scene.solid(name).is_none_or(|solid| !solid.visible))
}
_ => false,
}
}
fn clear_pmi_overlay(&mut self) {
if self.pmi_overlay_key.take().is_some() {
let _ = self.set_overlay_json(
&serde_json::json!({ "groups": [ { "name": PMI_OVERLAY_GROUP } ] }).to_string(),
);
self.dirty = true;
}
}
/// Per-frame upkeep: re-bake when the zoom (>0.5 %) or the viewing
/// direction (>0.5°) moved, so arrowheads stay pixel-sized and face the
/// camera.
pub fn ensure_pmi_overlay_current(&mut self) {
let Some((baked_wpp, baked_dir)) = self.pmi_overlay_key else {
if self.pmi_active_view.is_some() {
self.refresh_pmi_overlay();
}
return;
};
if self.any_sketch_mode() {
self.clear_pmi_overlay();
return;
}
let wpp = self.camera.world_per_pixel();
let (_, _, view_dir) = self.camera.basis();
let dot = baked_dir[0] * view_dir[0] + baked_dir[1] * view_dir[1] + baked_dir[2] * view_dir[2];
let camera = self.snapshot_camera();
let balloon_camera_changed = self.pmi_report.as_ref()
.and_then(|r| self.pmi_active_view.as_ref().and_then(|id| r.view(id)))
.is_some_and(|v| v.annotations.iter().any(|row| matches!(&row.geometry,
brep_kernel::PmiGeometry::Leader { anchor: Some(a), balloon: true, .. }
if matches!(a.mode, brep_kernel::BalloonAnchorMode::Surface) && a.camera.as_ref() != Some(&camera))));
if balloon_camera_changed || super::overlay_wpp_stale(baked_wpp, wpp) || dot < (0.5f64).to_radians().cos() {
self.refresh_pmi_overlay();
}
}
/// The label feed for the app's chip pass — the active view's
/// annotations: `[{id, type, icon, text, status, message, color:[r,g,b],
/// world:[x,y,z], enabled, open, selected, textSizePt}]`. Empty without an
/// active view. The automation registry's copy of [`Self::pmi_labels`].
pub fn pmi_labels_json(&self) -> String {
serde_json::to_string(&self.pmi_labels()).unwrap_or_else(|_| "[]".into())
}
/// The label feed, typed: what the viewport's chip pass reads every
/// frame. It reads the shared parse of the PMI block, so a frame with
/// nothing changed builds the rows and nothing else.
pub fn pmi_labels(&self) -> Vec<PmiLabel> {
let Some(active) = self.pmi_active_view.as_deref() else {
return Vec::new();
};
let Some(view) = self.pmi_report.as_ref().and_then(|report| report.view(active)) else {
return Vec::new();
};
// An explode step's chip is an editing handle of the PMI workbench; an
// exploded view shown from the Assembly workbench draws its bodies and
// trails without them (`exploded_ops`).
let explode_chips = matches!(self.settings.workbench.as_str(), "pmi" | "all");
if !explode_chips && view.annotations.iter().all(|row| row.kind == "explode") {
return Vec::new();
}
let text_size = self
.pmi_state_shared()
.find_view(active)
.map(|view| view.display.text_size_pt)
.unwrap_or(12.0);
// Part-way through playback a balloon rides its body.
let playback = self.explode_playback_poses();
// An explode step draws NO label in the 3D view (owner 2026-10-26:
// "I don't want to see the exploded labels at all"): its name lives
// in the PMI tree and the Assembly workbench's step list, and while
// its form is open the transform gizmo stands on its parts.
view.annotations
.iter()
.filter(|row| row.kind != "explode")
.map(|row| {
// A balloon whose target was hidden in the viewport AFTER
// the run (the view's hidden list not yet updated) is an
// error chip with no leader: the kernel's head is on a body
// nobody sees.
let (status, message) = if row.status == PmiStatus::Ok && self.balloon_target_hidden_live(row) {
(PmiStatus::Error, LIVE_HIDDEN_MESSAGE.to_string())
} else {
(row.status, row.message.clone())
};
let color = if status == PmiStatus::Error {
ERROR_RGB
} else if !row.enabled {
DISABLED_RGB
} else {
PMI_RGB
};
let icon = pmi_type(&row.kind).map(|def| def.icon).unwrap_or("");
let text = if status == PmiStatus::Error || row.text.is_empty() {
format!("{} {}", icon, row.id)
} else {
row.text.clone()
};
PmiLabel {
id: row.id.clone(),
kind: row.kind.clone(),
icon,
text,
status: match status { PmiStatus::Ok => "ok", PmiStatus::Error => "error" },
message,
color,
world: playback.as_ref().map_or(row.label_world, |poses| poses.shown(row, row.label_world)),
enabled: row.enabled,
open: self.pmi_open_annotation.as_deref() == Some(row.id.as_str()),
selected: self.pmi_selected_annotation.as_deref() == Some(row.id.as_str()),
text_size_pt: text_size,
}
})
.collect()
}
}