BREP_app 0.19.0

The BREP CAD application: an eframe (egui + wgpu) host that draws the brep-render 3D engine into an egui frame — native + wasm from one codebase.
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
//! An isolated, disposable reconstruction session for an imported MESH file —
//! STL or 3MF, which reach the same reconstruction chain. Accept transfers the
//! exact previewed STEP into the destination document as one ordinary import
//! feature.
use crate::automation::hit_keys::HitKeyDoc;
use brep_render::engine_state::EngineState;
use brep_render::runner::{
    ConversionPolicy, MeshImportFormat, StlConversionOptions, StlConversionOutput,
};
use eframe::egui;
use std::collections::BTreeMap;

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum PreviewAction {
    None,
    Accept,
    Cancel,
}

pub struct StlImportPreview {
    pub destination: u64,
    name: String,
    /// Which reader the runner parses `bytes` with. The tolerances, the
    /// preview and the accept path are identical for every mesh format.
    format: MeshImportFormat,
    bytes: Vec<u8>,
    pub engine: EngineState,
    options: StlConversionOptions,
    revision: u64,
    pending: Option<(u64, u64)>,
    result: Option<(u64, StlConversionOutput)>,
    error: Option<String>,
    scene_changed: bool,
    hits: BTreeMap<String, [f32; 4]>,
    /// The interactive phases: the mesh at once, its regions, per-region refits.
    pub regions: crate::panels::stl_import_regions::RegionView,
}

impl StlImportPreview {
    pub fn new(
        destination: u64,
        format: MeshImportFormat,
        name: String,
        bytes: Vec<u8>,
        mut engine: EngineState,
    ) -> Self {
        engine.settings.wireframe = false;
        let mut preview = Self {
            destination,
            name,
            format,
            bytes,
            engine,
            options: Default::default(),
            revision: 0,
            pending: None,
            result: None,
            error: None,
            scene_changed: true,
            hits: BTreeMap::new(),
            regions: Default::default(),
        };
        preview.rebuild();
        preview
    }

    fn busy(&self) -> bool {
        self.pending.is_some() || self.engine.run_pending()
    }

    pub fn ready(&self) -> bool {
        !self.busy()
            && !self.regions.applying()
            && self.error.is_none()
            && self
                .result
                .as_ref()
                .is_some_and(|(revision, _)| *revision == self.revision)
            && self.result_solids() > 0
    }

    /// The reconstructed bodies on screen (the region view's display body is
    /// not one).
    fn result_solids(&self) -> usize {
        self.engine
            .scene
            .solids()
            .iter()
            .filter(|solid| solid.name != crate::panels::stl_import_regions::REGION_SOLID)
            .count()
    }

    /// Faces of the reconstructed bodies on screen (what the STEP imports as:
    /// the importer splits an off-plane polygon face into planar triangles, so
    /// this can exceed the file's ADVANCED_FACE count).
    pub(crate) fn body_faces(&self) -> usize {
        self.engine
            .scene
            .solids()
            .iter()
            .filter(|solid| solid.name != crate::panels::stl_import_regions::REGION_SOLID)
            .map(|solid| solid.faces.len())
            .sum()
    }

    /// Whether the displayed result is the reconstruction of the mesh the
    /// region view shows (Update has not been pressed since).
    fn built(&self) -> bool {
        self.pending.is_none() && self.result.is_some() && self.result_solids() > 0
    }

    pub fn step_text(&self) -> Option<&str> {
        self.ready()
            .then(|| self.result.as_ref().unwrap().1.step_text.as_str())
    }

    pub fn accept_into(
        &self,
        destination_id: u64,
        destination: &mut EngineState,
    ) -> Result<(), String> {
        if destination_id != self.destination {
            return Err(
                "The destination document changed; cancel this preview and import again.".into(),
            );
        }
        let step = self
            .step_text()
            .ok_or("Update the preview before accepting the import.")?;
        destination.import_step_feature(step).map(|_| ())
    }

    fn rebuild(&mut self) {
        if self.busy() {
            return;
        }
        self.error = None;
        self.regions.restart();
        match self.engine.reconstruct_mesh_preview(
            self.format,
            self.bytes.clone(),
            self.options.clone(),
        ) {
            Ok(id) => self.pending = Some((id, self.revision)),
            Err(error) => self.error = Some(error),
        }
    }

    /// Send "Apply chosen fits" to the runner with the built STEP.
    pub(crate) fn submit_region_choices(&mut self) {
        if !self.built() {
            return;
        }
        let Some((_, output)) = self.result.as_ref() else { return };
        if let Some(job) = self.regions.take_apply_job(&output.step_text) {
            self.engine.submit_apply_region_choices(job);
        }
    }

    pub(crate) fn pump(&mut self) {
        self.engine.pump();
        // The phases come before the final reply: the mesh, then its regions.
        while let Some(phase) = self.engine.take_mesh_preview_phase() {
            if self.pending.is_some_and(|(id, _)| id == phase.id)
                && self.regions.take_phase(phase.phase, &mut self.engine)
            {
                self.scene_changed = true;
            }
        }
        self.regions.pump(&mut self.engine);
        // The chosen regions came back as NURBS faces: the preview shows that
        // body and Accept imports its STEP (the history replays it).
        if let Some(text) = self.regions.take_new_step() {
            if let Some((_, output)) = self.result.as_mut().filter(|_| self.pending.is_none()) {
                if text != output.step_text {
                    let document = serde_json::json!({
                        "features": [{"type": "IMPORT3D", "inputParams": {"id": "IMPORT3D1", "stepText": text}}],
                        "featureCounter": 1
                    });
                    match self.engine.load_model_and_fit(&document.to_string()) {
                        Ok(_) => {
                            // The face tally the dialog shows counts the
                            // file it will accept.
                            output.report.exported_advanced_faces = text.matches("ADVANCED_FACE(").count();
                            output.step_text = text;
                            self.scene_changed = true;
                        }
                        Err(error) => self.error = Some(error),
                    }
                }
            }
        }
        while let Some(reply) = self.engine.take_mesh_preview() {
            let Some((id, revision)) = self.pending else {
                continue;
            };
            if reply.id != id {
                continue;
            }
            self.pending = None;
            match reply.result {
                Ok(output) => {
                    self.regions.apply_result(&output.report);
                    let document = serde_json::json!({
                        "features": [{"type": "IMPORT3D", "inputParams": {
                            "id": "IMPORT3D1", "stepText": output.step_text
                        }}], "featureCounter": 1
                    });
                    match self.engine.load_model_and_fit(&document.to_string()) {
                        Ok(_) => {
                            self.result = Some((revision, output));
                            self.scene_changed = true;
                        }
                        Err(error) => self.error = Some(error),
                    }
                }
                Err(error) => self.error = Some(error),
            }
        }
        if !self.busy()
            && self.result.is_some()
            && self.result_solids() == 0
            && self.error.is_none()
        {
            self.error = Some("The reconstructed BREP could not be displayed. Adjust the settings and update the preview.".into());
        }
    }

    fn hit(&mut self, key: &str, response: &egui::Response) {
        let r = response.rect;
        self.hits
            .insert(key.into(), [r.min.x, r.min.y, r.width(), r.height()]);
    }

    pub fn show(
        &mut self,
        ui: &mut egui::Ui,
        viewport: &mut crate::viewport::Viewport,
    ) -> PreviewAction {
        self.pump();
        self.hits.clear();
        let mut action = PreviewAction::None;
        egui::containers::panel::Panel::left("stl-preview-settings")
            .resizable(true)
            .default_size(350.0)
            .size_range(300.0..=550.0)
            .show(ui, |ui| {
                egui::ScrollArea::vertical().show(ui, |ui| {
                    action = self.controls(ui);
                });
            });
        // The interactive phases in a panel of their own on the right, so
        // the region form is in view the moment a region is picked.
        let built = self.built();
        let reconstructing = self.busy();
        egui::containers::panel::Panel::right("stl-preview-regions")
            .resizable(true)
            .default_size(340.0)
            .size_range(280.0..=520.0)
            .show(ui, |ui| {
                egui::ScrollArea::vertical().id_salt("stl-preview-regions-scroll").show(ui, |ui| {
                    self.regions.controls(ui, &mut self.engine, built, reconstructing, &mut self.hits);
                });
            });
        self.submit_region_choices();
        if self.regions.sync(&mut self.engine, built) {
            self.scene_changed = true;
        }
        if self.scene_changed {
            viewport.forget_document();
            self.scene_changed = false;
        }
        viewport.show(ui, &mut self.engine);
        if self.busy() || self.regions.busy() {
            ui.ctx().request_repaint();
        }
        if ui
            .ctx()
            .input_mut(|input| input.consume_key(egui::Modifiers::NONE, egui::Key::Escape))
        {
            action = PreviewAction::Cancel;
        }
        action
    }

    fn controls(&mut self, ui: &mut egui::Ui) -> PreviewAction {
        ui.heading(format!("{} import preview", format_label(self.format)));
        ui.label(&self.name);
        ui.weak("Orbit and zoom to inspect the reconstructed BREP. Distances are in mm.");
        ui.separator();
        let mut changed = false;
        egui::Grid::new("stl-tolerances").num_columns(2).spacing([8.0, 8.0]).show(ui, |ui| {
            changed |= number(ui, &mut self.hits, "distance", "Surface distance", &mut self.options.recognition.distance_tolerance, 1e-12..=1e6, 1e-6,
                "Maximum absolute distance from mesh samples to a recognized surface. Binary STL coordinate precision sets a minimum.");
            changed |= number(ui, &mut self.hits, "relative", "Relative distance", &mut self.options.recognition.relative_tolerance, 0.0..=1.0, 1e-8,
                "Additional recognition tolerance relative to the model size.");
            let mut normal = self.options.recognition.normal_tolerance.to_degrees();
            if number(ui, &mut self.hits, "normal", "Normal angle (°)", &mut normal, 0.01..=89.0, 0.1,
                "Maximum angle between a recognized surface normal and the mesh normal.") {
                self.options.recognition.normal_tolerance = normal.to_radians(); changed = true;
            }
            let mut feature = self.options.recognition.feature_angle.to_degrees();
            if number(ui, &mut self.hits, "feature", "Feature angle (°)", &mut feature, 0.1..=89.0, 0.1,
                "Edges sharper than this angle split surface regions during recognition and BREP reconstruction.") {
                self.options.recognition.feature_angle = feature.to_radians();
                self.options.kernel_deflection_angle_degrees = feature; changed = true;
            }
            changed |= number(ui, &mut self.hits, "fit", "BREP relative fit", &mut self.options.kernel_fit_tolerance, 1e-12..=0.1, 1e-5,
                "Surface fitting tolerance for BREP reconstruction, relative to the bounding-box diagonal. Shared boundaries must also pass stricter geometry checks.");
            changed |= number(ui, &mut self.hits, "brepNormal", "BREP normal angle (°)", &mut self.options.kernel_normal_tolerance_degrees, 0.01..=89.0, 0.1,
                "Normal agreement required when grouping mesh triangles into BREP faces.");
        });
        ui.add_space(8.0);
        let mut auto_weld = self.options.weld_tolerance < 0.0;
        let auto = ui.checkbox(&mut auto_weld, "Automatic vertex weld tolerance");
        self.hit("autoWeld", &auto);
        if auto.changed() {
            self.options.weld_tolerance = if auto_weld { -1.0 } else { 1e-6 };
            changed = true;
        }
        if !auto_weld {
            egui::Grid::new("stl-weld").num_columns(2).show(ui, |ui| {
                changed |= number(ui, &mut self.hits, "weld", "Vertex weld distance", &mut self.options.weld_tolerance, 0.0..=1e3, 1e-6,
                    "Merge vertices closer than this distance. Zero merges identical coordinates only.");
            });
        }
        let mut strict = self.options.policy == ConversionPolicy::RequireFullyAnalytic;
        let response = ui
            .checkbox(&mut strict, "Require fully analytic surfaces")
            .on_hover_text("Reject reconstruction if any region must remain faceted.");
        self.hit("strict", &response);
        if response.changed() {
            self.options.policy = if strict {
                ConversionPolicy::RequireFullyAnalytic
            } else {
                ConversionPolicy::AllowFacetedFallback
            };
            changed = true;
        }
        let reset = ui.button("Reset tolerances");
        self.hit("reset", &reset);
        if reset.clicked() {
            self.options = Default::default();
            changed = true;
        }
        if changed {
            self.revision += 1;
        }
        ui.separator();
        let update = ui.add_enabled(!self.busy(), egui::Button::new("Update preview"));
        self.hit("update", &update);
        if update.clicked() {
            self.rebuild();
        }
        // Accept and Cancel sit HERE, above everything a reconstruction adds
        // or removes (the spinner, the warnings, the result's report): a row
        // drawn below them jumped down the moment a result landed, so a click
        // on Cancel in that frame hit whatever had moved into its place — and
        // the dialog stayed open (the stl-preview script, under load).
        let mut action = PreviewAction::None;
        ui.horizontal(|ui| {
            let accept = ui.add_enabled(self.ready(), egui::Button::new("Accept import"));
            self.hit("accept", &accept);
            if accept.clicked() {
                action = PreviewAction::Accept;
            }
            let cancel = ui.button("Cancel");
            self.hit("cancel", &cancel);
            if cancel.clicked() {
                action = PreviewAction::Cancel;
            }
        });
        if self.busy() {
            ui.horizontal(|ui| {
                ui.spinner();
                ui.label("Reconstructing BREP…");
            });
        }
        if self
            .result
            .as_ref()
            .is_some_and(|(revision, _)| *revision != self.revision)
        {
            ui.colored_label(
                ui.visuals().warn_fg_color,
                "Settings changed — update the preview before accepting.",
            );
        }
        if let Some(error) = &self.error {
            ui.colored_label(ui.visuals().error_fg_color, error);
        }
        if let Some((_, output)) = &self.result {
            let report = &output.report;
            ui.add_space(8.0);
            ui.label(format!(
                "{} BREP faces · {} solid(s)",
                report.exported_advanced_faces, report.roundtrip_solids
            ));
            let cylinders = output.step_text.matches("CYLINDRICAL_SURFACE(").count();
            let cones = output.step_text.matches("CONICAL_SURFACE(").count();
            let spheres = output.step_text.matches("SPHERICAL_SURFACE(").count();
            ui.label(format!(
                "{cylinders} cylindrical · {cones} conical · {spheres} spherical surfaces"
            ));
            let facets = report
                .hybrid_rebuild
                .map(|r| r.faceted_faces)
                .or(report.faceted_faces_after_merge)
                .unwrap_or(0);
            if facets > 0 {
                let applied = self.regions.applied_faces();
                ui.colored_label(
                    ui.visuals().warn_fg_color,
                    if applied > 0 {
                        format!("{facets} faces remained faceted as built; {applied} region(s) since rebuilt as NURBS faces")
                    } else {
                        format!("{facets} faces remain faceted")
                    },
                );
            }
            ui.weak(format!(
                "Effective surface distance: {:.3e} mm",
                report.effective_distance_tolerance
            ));
            ui.collapsing("Reconstruction details", |ui| {
                ui.label(&report.backend_reason);
                match &report.recognition_skipped {
                    Some(reason) => ui.label(format!(
                        "{} input triangles · whole-primitive recognition skipped: {reason}",
                        report.input_triangles
                    )),
                    None => ui.label(format!(
                        "{} input triangles · {} unresolved",
                        report.input_triangles, report.unresolved_triangles
                    )),
                };
                ui.label(format!(
                    "Completed in {:.2} seconds",
                    report.timings.total_seconds
                ));
                for message in &report.messages {
                    ui.label(message);
                }
            });
        }
        ui.separator();
        ui.checkbox(&mut self.engine.settings.wireframe, "Wireframe preview");
        if ui.button("Fit preview to view").clicked() {
            self.engine.zoom_to_fit();
        }
        action
    }

    pub fn state_json(&self) -> String {
        // The analytic tally the dialog SHOWS ("2 cylindrical · 0 conical · 0
        // spherical") counted from the same STEP text, so a reader outside the
        // window sees the same answer as a reader in front of it. It is the
        // whole point of a reconstruction — whether the walls of a bore came
        // back as cylinders or as a tessellation — and nothing published it.
        let surfaces = |kind: &str| {
            self.result
                .as_ref()
                .map(|(_, output)| output.step_text.matches(kind).count())
        };
        serde_json::json!({"name": self.name, "format": format_label(self.format),
            "ready": self.ready(), "busy": self.busy(),
            "revision": self.revision, "previewRevision": self.result.as_ref().map(|(r, _)| r),
            "error": self.error, "options": self.options, "solidCount": self.result_solids(),
            "faces": self.result.as_ref().map(|(_, r)| r.report.exported_advanced_faces),
            "bodyFaces": self.body_faces(),
            "cylindricalSurfaces": surfaces("CYLINDRICAL_SURFACE("),
            "conicalSurfaces": surfaces("CONICAL_SURFACE("),
            "sphericalSurfaces": surfaces("SPHERICAL_SURFACE(")
        }).to_string()
    }
    /// The interactive phases (`__brepImportRegions`).
    pub fn regions_json(&self) -> String {
        self.regions.state_json(self.built()).to_string()
    }
    pub fn hits_json(&self) -> String {
        serde_json::to_string(&self.hits).unwrap()
    }
}

/// The format's name as the preview heading and `state_json` spell it.
fn format_label(format: MeshImportFormat) -> &'static str {
    match format {
        MeshImportFormat::Stl => "STL",
        MeshImportFormat::Obj => "OBJ",
        MeshImportFormat::ThreeMf => "3MF",
    }
}

fn number(
    ui: &mut egui::Ui,
    hits: &mut BTreeMap<String, [f32; 4]>,
    key: &str,
    label: &str,
    value: &mut f64,
    range: std::ops::RangeInclusive<f64>,
    speed: f64,
    help: &str,
) -> bool {
    ui.label(label).on_hover_text(help);
    let response = ui
        .add(
            egui::DragValue::new(value)
                .range(range)
                .speed(speed)
                .max_decimals(12),
        )
        .on_hover_text(help);
    let r = response.rect;
    // `field:<key>`, the prefix every other panel keys an editable value under
    // — and the one this panel's registry entry has always DOCUMENTED. The
    // rows published bare keys instead, so `hit_keys_check` called all six
    // undocumented the first time anything opened this dialog with it: the
    // browser verifier read the rect map directly and never asked.
    hits.insert(format!("field:{key}"), [r.min.x, r.min.y, r.width(), r.height()]);
    ui.end_row();
    response.changed()
}


/// The hit keys this panel publishes (see `automation::hit_keys`).
pub static HIT_KEYS: &[HitKeyDoc] = &[
    HitKeyDoc { panel: "importpreview", prefix: "accept", meaning: "accept the reconstructed import", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "cancel", meaning: "cancel the import", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "update", meaning: "update the preview with the current tolerances", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "reset", meaning: "reset the tolerances", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "strict", meaning: "require fully analytic surfaces", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "autoWeld", meaning: "automatic vertex welding", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "field:", meaning: "a tolerance field (field:distance, field:relative, field:normal, field:feature, field:fit, field:brepNormal)", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "view", meaning: "what the viewport shows: view#regions (one colour per region), view#fit (what each region fits as), view#result (the reconstructed BREP)", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "regionsFaceted", meaning: "list only the regions left as triangles", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "regionsApply", meaning: "Apply chosen fits: rebuild the regions chosen with Use this NURBS fit as one NURBS face each, in the body Accept imports", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "regionrow:", meaning: "a region's row, by region id: select it (as a click on its face does)", command: None },
    HitKeyDoc { panel: "importpreview", prefix: "regionform:", meaning: "the selected region's form: regionform:field:kind (Fit as, with #<kind label> entries), regionform:field:tolerance, regionform:field:normalAngle, regionform:field:retry (Retry fit), regionform:field:useFit (Use this NURBS fit / Do not use this fit, after an in-tolerance NURBS refit of a region left as triangles), regionform:form:return (Close)", command: None },
];