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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
//! Import a part from KiCad: its symbol, its pads and its 3D model, as ONE edit.
//!
//! The Symbol workbench imports a `.kicad_sym` symbol and the Pads workbench a
//! `.kicad_mod` footprint (eCAD plan, decision 9). The host reads the files and
//! eCAD parses them (`brep_ecad_core::kicad`). A symbol import follows KiCad's chain:
//!
//! 1. the symbol's `Footprint` property names `library:name`, found as
//!    `<footprints>/<library>.pretty/<name>.kicad_mod`; a generic symbol leaves it
//!    empty and offers `ki_fp_filters` patterns, and the user chooses among the
//!    footprints they match;
//! 2. the footprint's `(model …)` entry names a `.wrl` in KiCad's 3D library,
//!    usually under `${KICAD9_3DMODEL_DIR}`;
//! 3. BREP imports STEP, not VRML, so the model read is the `.step` of the same
//!    name beside it.
//!
//! Every link that fails is a stated outcome, not an error: the part imports
//! with what was found, and a note names the file that was looked for. KiCad's
//! own library tables (`fp-lib-table`) are not read; a footprint library is found
//! only under the footprints folder, by its nickname.
//!
//! # The part's frame
//!
//! The model and the ports are placed in KiCad's 3D frame of the footprint: x as
//! the footprint's, y the footprint's NEGATED, z up from the board's top copper,
//! millimetres. That is where KiCad's own 3D view and STEP exporter put a
//! footprint's model ([`kicad::model_placement`]), so a pad at `(x, y)` µm in the
//! pads block is at `(x / 1000, −y / 1000, 0)` mm in the part.
//!
//! # One undo
//!
//! The model becomes one IMPORT3D feature, each symbol pin one connection point
//! at its pad, and the symbol and pads are written as the part's `symbol` and
//! `pads` blocks. The features go in with ONE `add_features` (one checkpoint),
//! and the blocks after it without a checkpoint of their own, so one Ctrl+Z takes
//! the whole import away. A block written BEFORE the checkpoint would be inside
//! the snapshot undo restores and survive the undo (`model_io.rs` explains the
//! same trap for PMI).
//!
//! The model is imported here, not by the pipeline: KiCad turns a model by
//! `Rz(−rz)·Ry(−ry)·Rx(−rx)`, and BREP's transform param by `Rx·Ry·Rz` with the
//! angles as given, so KiCad's numbers cannot be handed to a transform feature.
//! The STEP bodies are moved by KiCad's own matrix and sealed into the feature's
//! `nativeBrep` payload, the lane a STEP-assembly part rides on, and the numbers
//! they were placed by are kept in its `persistentData.kicadModel`.

use crate::store::{ModelStore, KICAD_LIBRARY_KEY};
use brep_render::brep_kernel as kernel;
use brep_render::engine_state::EngineState;
use brep_ecad_core::board::{Footprint, Model};
use brep_ecad_core::kicad::{self, FootprintLink};
use brep_ecad_core::Symbol;
use serde_json::{json, Value};
use std::path::Path;

/// Which KiCad file an import starts from.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KicadKind {
    /// A `.kicad_sym` library: a symbol, and through it the footprint and model.
    Symbol,
    /// A `.kicad_mod` footprint: pads and the model.
    Footprint,
}

impl KicadKind {
    pub fn extension(self) -> &'static str {
        match self {
            KicadKind::Symbol => "kicad_sym",
            KicadKind::Footprint => "kicad_mod",
        }
    }

    /// The kind a file name's extension names, if any.
    pub fn of_file(name: &str) -> Option<Self> {
        let lower = name.to_ascii_lowercase();
        [KicadKind::Symbol, KicadKind::Footprint]
            .into_iter()
            .find(|kind| lower.ends_with(&format!(".{}", kind.extension())))
    }
}

// ============================================================================
// Reading the user's files
// ============================================================================

/// The files an import reads: the user's KiCad library on disk. A trait so the
/// tests can hand the chain a library without one being installed.
pub trait KicadFiles {
    /// Whether this build can read the user's files at all. The web build
    /// cannot: the chain stops at the file the user uploaded.
    fn available(&self) -> bool {
        true
    }
    fn read(&self, path: &str) -> Option<Vec<u8>>;
    fn is_file(&self, path: &str) -> bool;
    fn is_dir(&self, path: &str) -> bool;
    /// The names of `dir`'s entries.
    fn list(&self, dir: &str) -> Vec<String>;
}

/// The filesystem, for the native build.
pub struct DiskFiles;

impl KicadFiles for DiskFiles {
    fn available(&self) -> bool {
        !cfg!(target_arch = "wasm32")
    }
    fn read(&self, path: &str) -> Option<Vec<u8>> {
        std::fs::read(path).ok()
    }
    fn is_file(&self, path: &str) -> bool {
        Path::new(path).is_file()
    }
    fn is_dir(&self, path: &str) -> bool {
        Path::new(path).is_dir()
    }
    fn list(&self, dir: &str) -> Vec<String> {
        std::fs::read_dir(dir)
            .into_iter()
            .flatten()
            .flatten()
            .map(|entry| entry.file_name().to_string_lossy().into_owned())
            .collect()
    }
}

fn join(dir: &str, name: &str) -> String {
    Path::new(dir).join(name).to_string_lossy().into_owned()
}

fn parent(path: &str) -> Option<String> {
    Path::new(path)
        .parent()
        .map(|dir| dir.to_string_lossy().into_owned())
        .filter(|dir| !dir.is_empty())
}

fn stem(path: &str) -> String {
    let name = path.rsplit(['/', '\\']).next().unwrap_or(path);
    name.rsplit_once('.').map_or(name, |(stem, _)| stem).to_owned()
}

// ============================================================================
// Where the library is
// ============================================================================

/// The variables KiCad names its footprint folder by, newest first.
const FOOTPRINT_VARS: &[&str] = &[
    "KICAD9_FOOTPRINT_DIR",
    "KICAD8_FOOTPRINT_DIR",
    "KICAD7_FOOTPRINT_DIR",
    "KICAD6_FOOTPRINT_DIR",
];
/// The variables KiCad names its 3D-model folder by, newest first.
/// `KISYS3DMOD` is the name before KiCad 6.
const MODEL_VARS: &[&str] = &[
    "KICAD9_3DMODEL_DIR",
    "KICAD8_3DMODEL_DIR",
    "KICAD7_3DMODEL_DIR",
    "KICAD6_3DMODEL_DIR",
    "KISYS3DMOD",
];

/// Where a KiCad install keeps its libraries, `(footprints, 3D models)`, for
/// this platform. The first is also what a message names when none exists.
fn install_defaults() -> &'static [(&'static str, &'static str)] {
    if cfg!(target_os = "windows") {
        &[(
            r"C:\Program Files\KiCad\9.0\share\kicad\footprints",
            r"C:\Program Files\KiCad\9.0\share\kicad\3dmodels",
        )]
    } else if cfg!(target_os = "macos") {
        &[(
            "/Applications/KiCad/KiCad.app/Contents/SharedSupport/footprints",
            "/Applications/KiCad/KiCad.app/Contents/SharedSupport/3dmodels",
        )]
    } else {
        &[
            ("/usr/share/kicad/footprints", "/usr/share/kicad/3dmodels"),
            ("/usr/local/share/kicad/footprints", "/usr/local/share/kicad/3dmodels"),
            (
                "/var/lib/flatpak/app/org.kicad.KiCad/current/active/files/share/kicad/footprints",
                "/var/lib/flatpak/app/org.kicad.KiCad/current/active/files/share/kicad/3dmodels",
            ),
        ]
    }
}

/// One library folder and where it came from, in words the dialog shows.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Folder {
    pub path: String,
    pub from: String,
}

/// The user's KiCad library: the folder holding the `*.pretty` footprint
/// libraries and the folder holding the `*.3dshapes` models.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct KicadLibrary {
    pub footprints: Folder,
    pub models: Folder,
}

impl KicadLibrary {
    /// The folders a fresh session uses: KiCad's variable when it is set to a
    /// folder, else the first install default that exists, else the first
    /// install default (so a message can name where it looked).
    pub fn seeded(env: &dyn Fn(&str) -> Option<String>, files: &dyn KicadFiles) -> Self {
        let seed = |vars: &[&str], pick: fn(&(&'static str, &'static str)) -> &'static str| -> Folder {
            for var in vars {
                if let Some(path) = env(var).filter(|path| files.is_dir(path)) {
                    return Folder { path, from: format!("from {var}") };
                }
            }
            let defaults = install_defaults();
            match defaults.iter().map(pick).find(|path| files.is_dir(path)) {
                Some(path) => Folder { path: path.into(), from: "KiCad's install folder".into() },
                None => Folder {
                    path: pick(&defaults[0]).into(),
                    from: format!("not found: {} unset, and no KiCad install here", vars.join(", ")),
                },
            }
        };
        Self {
            footprints: seed(FOOTPRINT_VARS, |(footprints, _)| footprints),
            models: seed(MODEL_VARS, |(_, models)| models),
        }
    }

    /// The folders the user last chose (the [`KICAD_LIBRARY_KEY`] record), each
    /// falling back to the seed when it is unset or no longer a folder. A stored
    /// folder that is gone is SAID: the source names it and what is used instead.
    pub fn load(
        store: &dyn ModelStore,
        env: &dyn Fn(&str) -> Option<String>,
        files: &dyn KicadFiles,
    ) -> Self {
        let mut library = Self::seeded(env, files);
        let stored: Value = store
            .read(KICAD_LIBRARY_KEY)
            .and_then(|text| serde_json::from_str(&text).ok())
            .unwrap_or(Value::Null);
        for (key, folder, what) in [
            ("footprints", &mut library.footprints, "footprint"),
            ("models", &mut library.models, "3D-model"),
        ] {
            let Some(path) = stored.get(key).and_then(Value::as_str).filter(|p| !p.is_empty()) else {
                continue;
            };
            if files.is_dir(path) {
                *folder = Folder { path: path.into(), from: "your saved folder".into() };
            } else {
                folder.from = format!(
                    "your saved {what} folder {path} no longer exists, so this is {}",
                    folder.from
                );
            }
        }
        library
    }

    /// Remember the folders for the next session.
    pub fn save(&self, store: &dyn ModelStore) -> Result<(), String> {
        let record = json!({ "footprints": self.footprints.path, "models": self.models.path });
        store.write(KICAD_LIBRARY_KEY, &record.to_string())
    }

    /// Where footprint `library:name` is: `<footprints>/<library>.pretty/<name>.kicad_mod`.
    pub fn footprint_file(&self, library: &str, name: &str) -> String {
        join(&join(&self.footprints.path, &format!("{library}.pretty")), &format!("{name}.kicad_mod"))
    }

    /// A model path with KiCad's variables replaced: any of its 3D-model
    /// variables is the models folder, and `KIPRJMOD` the folder the footprint
    /// was read from. A relative path is relative to that folder too. Any other
    /// variable is refused by name.
    pub fn model_file(&self, path: &str, footprint_dir: Option<&str>) -> Result<String, String> {
        let mut out = String::new();
        let mut rest = path;
        while let Some(start) = rest.find('$') {
            let close = match rest[start + 1..].chars().next() {
                Some('{') => '}',
                Some('(') => ')',
                _ => {
                    out.push_str(&rest[..=start]);
                    rest = &rest[start + 1..];
                    continue;
                }
            };
            let Some(end) = rest[start + 2..].find(close) else {
                return Err(format!("the model path {path} has an unclosed variable"));
            };
            let name = &rest[start + 2..start + 2 + end];
            let value = if MODEL_VARS.contains(&name) || is_model_var(name) {
                self.models.path.clone()
            } else if name == "KIPRJMOD" {
                footprint_dir.map(str::to_owned).ok_or_else(|| {
                    format!("the model path {path} uses ${{KIPRJMOD}}, and there is no project folder")
                })?
            } else {
                return Err(format!(
                    "the model path {path} uses ${{{name}}}, which names no KiCad 3D-model folder"
                ));
            };
            out.push_str(&rest[..start]);
            out.push_str(&value);
            rest = &rest[start + 3 + end..];
        }
        out.push_str(rest);
        if Path::new(&out).is_relative() {
            if let Some(dir) = footprint_dir {
                return Ok(join(dir, &out));
            }
        }
        Ok(out)
    }
}

/// `KICAD<n>_3DMODEL_DIR` for any version `n`.
fn is_model_var(name: &str) -> bool {
    name.strip_prefix("KICAD")
        .and_then(|rest| rest.strip_suffix("_3DMODEL_DIR"))
        .is_some_and(|version| !version.is_empty() && version.chars().all(|c| c.is_ascii_digit()))
}

// ============================================================================
// Following the chain
// ============================================================================

/// A model's `.step`, read but not yet parsed. Parsing is the slow half: the
/// real KiCad 9.0.0 SOIC-8 model (254 KB) took 591 ms in a release build, so
/// the dialog parses on a reader thread ([`parse_step`]) and says so.
#[derive(Clone)]
pub struct StepSource {
    /// The `(model …)` entry as the footprint gives it.
    pub model: Model,
    /// The `.step` that was read.
    pub step_file: String,
    pub text: String,
}

/// The 3D model a footprint names, read and parsed.
#[derive(Clone)]
pub struct ModelFile {
    /// The `(model …)` entry as the footprint gives it.
    pub model: Model,
    /// The `.step` that was read.
    pub step_file: String,
    /// The file's bodies, in its own frame; placed at import.
    pub bodies: Vec<kernel::BrepSolid>,
    pub appearances: Vec<kernel::BodyAppearance>,
}

/// Everything one import brings, and what it could not.
#[derive(Clone, Default)]
pub struct KicadPart {
    pub symbol: Option<Symbol>,
    pub footprint: Option<Footprint>,
    /// Where the footprint was read from, when it came from the library.
    pub footprint_file: Option<String>,
    pub model: Option<ModelFile>,
    /// The model's `.step` while it waits to be parsed ([`KicadPart::parsed`]).
    pub step: Option<StepSource>,
    /// What the user is told: every link that failed, naming the file looked
    /// for, and the parsers' own warnings.
    pub notes: Vec<String>,
}

/// The symbols of a `.kicad_sym` library, named `nickname:symbol` after the
/// file, with the importer's warnings.
pub fn read_symbol_library(file: &str, bytes: &[u8]) -> Result<(Vec<Symbol>, Vec<String>), String> {
    let text = std::str::from_utf8(bytes).map_err(|_| format!("{file} is not UTF-8 text"))?;
    let report = kicad::import_library(text, &stem(file)).map_err(|error| format!("{file}: {error}"))?;
    if report.symbols.is_empty() {
        return Err(format!("{file} holds no symbol this importer reads"));
    }
    Ok((report.symbols, report.warnings))
}

/// How the footprint of a symbol import is found.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FootprintChoice {
    /// The one the symbol's `Footprint` property names.
    Named,
    /// One the user chose, `(library, name)`.
    Chosen(String, String),
    /// None: the part imports with its symbol only.
    Skip,
}

/// A symbol import: the symbol, then its footprint as `choice` says, then the
/// footprint's model.
pub fn part_from_symbol(
    symbol: Symbol,
    choice: &FootprintChoice,
    library: &KicadLibrary,
    files: &dyn KicadFiles,
) -> KicadPart {
    let id = symbol.library_id.clone();
    let mut part = KicadPart { symbol: Some(symbol), ..KicadPart::default() };
    let wanted = match (choice, kicad::footprint_link(part.symbol.as_ref().unwrap())) {
        (FootprintChoice::Chosen(lib, name), _) => Some((lib.clone(), name.clone())),
        (FootprintChoice::Skip, _) => {
            part.notes.push(format!("{id}: no footprint chosen, so the part imports with its symbol only"));
            None
        }
        (FootprintChoice::Named, FootprintLink::Named { library: lib, name }) if lib.is_empty() => {
            part.notes.push(format!(
                "{id} names footprint '{name}' without a library, so it cannot be found; \
                 the part imports with its symbol only"
            ));
            None
        }
        (FootprintChoice::Named, FootprintLink::Named { library: lib, name }) => Some((lib, name)),
        (FootprintChoice::Named, FootprintLink::Chosen { .. }) => {
            part.notes.push(format!("{id} names no footprint, so the part imports with its symbol only"));
            None
        }
    };
    let Some((lib, name)) = wanted else {
        return part;
    };
    if !files.available() {
        part.notes.push(format!(
            "this build cannot read your KiCad library, so footprint {lib}:{name} is not followed"
        ));
        return part;
    }
    let file = library.footprint_file(&lib, &name);
    let Some(bytes) = files.read(&file) else {
        part.notes.push(format!("footprint {lib}:{name} not found: looked for {file}"));
        return part;
    };
    match read_footprint(&file, &bytes) {
        Ok((footprint, warnings)) => {
            part.notes.extend(warnings);
            // The symbol keeps `library:name`, KiCad's footprint id, so a netlist
            // names a footprint Pcbnew can find (the third eCAD audit, B10).
            if let Some(symbol) = &mut part.symbol { kicad::assign_footprint(symbol, &lib, &name); }
            part.footprint = Some(footprint);
            part.footprint_file = Some(file);
            attach_model(&mut part, library, files);
        }
        Err(error) => part.notes.push(error),
    }
    part
}

/// A footprint import: the pads of `file`, then its model.
pub fn part_from_footprint(
    file: &str,
    bytes: &[u8],
    library: &KicadLibrary,
    files: &dyn KicadFiles,
) -> Result<KicadPart, String> {
    let (footprint, warnings) = read_footprint(file, bytes)?;
    let mut part = KicadPart {
        footprint: Some(footprint),
        footprint_file: Some(file.into()),
        notes: warnings,
        ..KicadPart::default()
    };
    attach_model(&mut part, library, files);
    Ok(part)
}

fn read_footprint(file: &str, bytes: &[u8]) -> Result<(Footprint, Vec<String>), String> {
    let text = std::str::from_utf8(bytes).map_err(|_| format!("{file} is not UTF-8 text"))?;
    let (footprint, warnings) =
        kicad::import_footprint(text).map_err(|error| format!("footprint {file}: {error}"))?;
    Ok((footprint, warnings.into_iter().map(|w| format!("footprint {}: {w}", stem(file))).collect()))
}

/// The last two links: the footprint's `(model …)` entry, then the `.step`
/// beside the `.wrl` it names.
fn attach_model(part: &mut KicadPart, library: &KicadLibrary, files: &dyn KicadFiles) {
    let footprint = part.footprint.as_ref().expect("a footprint to follow");
    let Some(model) = footprint.model.clone() else {
        part.notes.push(format!(
            "footprint {} names no 3D model, so the part imports without one",
            footprint.name
        ));
        return;
    };
    if !files.available() {
        part.notes.push(format!(
            "this build cannot read your KiCad library, so the 3D model {} is not read",
            model.path
        ));
        return;
    }
    let beside = part.footprint_file.as_deref().and_then(parent);
    let step_file = match library.model_file(&model.step_path(), beside.as_deref()) {
        Ok(file) => file,
        Err(error) => {
            part.notes.push(format!("3D model not read: {error}"));
            return;
        }
    };
    let Some(bytes) = files.read(&step_file) else {
        let named = library.model_file(&model.path, beside.as_deref()).unwrap_or_default();
        part.notes.push(if named != step_file && files.is_file(&named) {
            format!(
                "3D model: the library has {named} but no {step_file} beside it, and BREP imports \
                 STEP, not VRML; the part imports without a model"
            )
        } else {
            format!("3D model not found: looked for {step_file}; the part imports without a model")
        });
        return;
    };
    match String::from_utf8(bytes) {
        Ok(text) => part.step = Some(StepSource { model, step_file, text }),
        Err(_) => part.notes.push(format!(
            "3D model {step_file} is not UTF-8 text; the part imports without a model"
        )),
    }
}

/// Parse a model's `.step` into its bodies: the slow half of the chain, which
/// the dialog runs on a reader thread.
pub fn parse_step(source: &StepSource) -> Result<ModelFile, String> {
    let (bodies, appearances) = kernel::import_step_with_appearance(&source.text).map_err(|error| {
        format!(
            "3D model {} did not import ({error}); the part imports without a model",
            source.step_file
        )
    })?;
    Ok(ModelFile { model: source.model.clone(), step_file: source.step_file.clone(), bodies, appearances })
}

impl KicadPart {
    /// Take a parse's outcome: the model, or a note saying why there is none.
    pub fn settle_model(&mut self, parsed: Result<ModelFile, String>) {
        self.step = None;
        match parsed {
            Ok(model) => self.model = Some(model),
            Err(note) => self.notes.push(note),
        }
    }

    /// The part with its model's `.step` parsed here and now.
    pub fn parsed(mut self) -> Self {
        if let Some(step) = self.step.take() {
            let outcome = parse_step(&step);
            self.settle_model(outcome);
        }
        self
    }
}

/// The footprints a generic symbol's patterns offer, `(library, name)`, from
/// every `*.pretty` folder under the footprints folder, sorted.
pub fn footprint_candidates(
    filters: &[String],
    library: &KicadLibrary,
    files: &dyn KicadFiles,
) -> Vec<(String, String)> {
    let mut found = Vec::new();
    for entry in files.list(&library.footprints.path) {
        let Some(lib) = entry.strip_suffix(".pretty") else {
            continue;
        };
        for file in files.list(&join(&library.footprints.path, &entry)) {
            let Some(name) = file.strip_suffix(".kicad_mod") else {
                continue;
            };
            if kicad::footprint_filter_matches(filters, lib, name) {
                found.push((lib.to_owned(), name.to_owned()));
            }
        }
    }
    found.sort();
    found
}

// ============================================================================
// The import itself
// ============================================================================

/// What an import did, for the status line and the toast.
#[derive(Clone, Debug, Default)]
pub struct Imported {
    pub summary: String,
    pub notes: Vec<String>,
}

/// Where a pad centre is in the part: `(x / 1000, −y / 1000, 0)` mm (see the
/// module doc for the frame).
pub fn pad_point(footprint: &Footprint, index: usize) -> [f64; 3] {
    let at = footprint.pads[index].at;
    [f64::from(at.x) / 1000., -f64::from(at.y) / 1000., 0.]
}

/// A new connection point's direction: +Z, up out of the board, the way a lead
/// or a wire leaves a pad. A point's direction is its transform's rotated +X
/// (`feature_pipeline/ports.rs`), and `Ry(-90 deg)` turns +X onto +Z.
const PORT_UP: [f64; 3] = [0., -90., 0.];
/// The port group a KiCad import declares its pins' points in, and its purpose:
/// these points ARE the part's pads, so the group is a `pcb` one.
const IMPORT_PORT: &str = "Pins";
const IMPORT_PURPOSE: &str = "pcb";

/// Write `part` into the open part document as ONE undoable edit.
///
/// Refused, with nothing written, when the part already has the block this
/// import would write: a KiCad import does not replace a symbol or pads.
pub fn import_part(state: &mut EngineState, part: &KicadPart) -> Result<Imported, String> {
    let parsed;
    let part = if part.step.is_some() {
        parsed = part.clone().parsed();
        &parsed
    } else {
        part
    };
    if part.symbol.is_none() && part.footprint.is_none() {
        return Err("nothing to import".into());
    }
    if part.symbol.is_some() && state.history.symbol_block().is_some() {
        return Err("this part already has a symbol, and a KiCad import does not replace one".into());
    }
    if part.footprint.is_some() && state.history.pads_block().is_some() {
        return Err("this part already has pads, and a KiCad import does not replace them".into());
    }
    let symbol_block = part.symbol.as_ref().map(serde_json::to_value).transpose().map_err(|e| e.to_string())?;
    let pads_block = part.footprint.as_ref().map(serde_json::to_value).transpose().map_err(|e| e.to_string())?;

    let mut notes = part.notes.clone();
    let mut features = Vec::new();
    let mut summary = Vec::new();
    if let Some(symbol) = &part.symbol {
        summary.push(format!("symbol {} ({})", symbol.library_id, crate::panels::count(symbol.pins.len(), "pin", "pins")));
    }
    if let Some(footprint) = &part.footprint {
        summary.push(format!("{} pads", footprint.pads.len()));
    }
    if let Some(model) = &part.model {
        features.push(model_feature(state, model)?);
        summary.push(format!("3D model ({})", crate::panels::count(model.bodies.len(), "body", "bodies")));
    }
    let mut ports_block = None;
    if let Some(symbol) = &part.symbol {
        let (block, port_notes) = port_points(state, symbol, part.footprint.as_ref());
        notes.extend(port_notes);
        let added = block["points"].as_array().map_or(0, Vec::len);
        if added > 0 {
            summary.push(format!("{added} connection points"));
            ports_block = Some(block);
        }
    }

    // The one checkpoint: the features' when there are any, else the first
    // block's own. Every later write falls after it.
    let mut checkpointed = !features.is_empty();
    if checkpointed {
        state.add_features(&features);
    }
    // Before the symbol, so the symbol write's own pin follow finds each pin's
    // point already declared and adds none.
    if let Some(block) = ports_block {
        let mut blocks = state
            .history
            .ports_block()
            .and_then(|value| value.as_array().cloned())
            .unwrap_or_default();
        blocks.push(block);
        let blocks = Some(serde_json::Value::Array(blocks));
        if checkpointed {
            state.history.set_ports_block_no_undo(blocks);
        } else {
            state.history.set_ports_block(blocks, None);
            checkpointed = true;
        }
    }
    if let Some(block) = symbol_block {
        if checkpointed {
            state.history.set_symbol_block_no_undo(Some(block));
        } else {
            state.history.set_symbol_block(Some(block), None);
            checkpointed = true;
        }
    }
    if let Some(block) = pads_block {
        if checkpointed {
            state.history.set_pads_block_no_undo(Some(block));
        } else {
            state.history.set_pads_block(Some(block), None);
        }
    }

    // Writing the symbol ran History's pin/point binding. Each pin found the
    // point declared for it above (a pin pairs with the point carrying its
    // name), so it added none; what it could not bind, it says.
    if let Some(hold) = state.history.pin_port_hold() {
        notes.push(format!("the pins were not bound to connection points: {hold}"));
    }
    if let Some(report) = state.history.pin_point_report() {
        notes.extend(report.problems.iter().map(|problem| problem.message()));
    }
    Ok(Imported { summary: format!("imported {}", summary.join(", ")), notes })
}

/// The IMPORT3D feature for the model: its bodies moved by KiCad's matrix and
/// sealed, under their final names, into a `nativeBrep` payload.
fn model_feature(state: &mut EngineState, file: &ModelFile) -> Result<Value, String> {
    let transform = kernel::AffineTransform::new(kicad::model_placement(&file.model))
        .map_err(|error| format!("3D model {}: {error}", file.step_file))?;
    // A negative scale mirrors, and a mirror must reverse the faces or the
    // solid turns inside out (`transform_brep` refuses it otherwise).
    let mirrored = transform.determinant3() < 0.0;
    let bodies = file
        .bodies
        .iter()
        .map(|body| kernel::transform_brep(body, transform, mirrored))
        .collect::<Result<Vec<_>, _>>()
        .map_err(|error| format!("3D model {}: {error}", file.step_file))?;
    let id = state.next_feature_id(&brep_render::features::feature_short_name("IMPORT3D"));
    // The file's colours are stamped into the payload, not into the open
    // document's metadata: the feature's run merges them back from there.
    let payload = {
        let _isolation = kernel::IsolatedSceneMetadata::begin();
        kernel::native_import_payload_with_appearance(&id, &bodies, &file.appearances)
    }
    .map_err(|error| format!("3D model {}: {error}", file.step_file))?;
    let model = &file.model;
    Ok(json!({
        "type": "IMPORT3D",
        "inputParams": { "id": id, "nativeBrep": payload },
        "persistentData": {
            "kicadModel": {
                "path": model.path,
                "stepFile": file.step_file,
                "offset": model.offset,
                "scale": model.scale,
                "rotation": model.rotation,
            }
        },
    }))
}

/// One connection point per symbol pin, NAMED with the pin's label, at the
/// FIRST pad carrying that label — as one declared port group. A pin whose
/// name the part already declares keeps that point where it is. Returns the
/// group and what the user is told: a pin on several pads, a pin on none.
fn port_points(
    state: &EngineState,
    symbol: &Symbol,
    footprint: Option<&Footprint>,
) -> (Value, Vec<String>) {
    let existing = state.history.declared_points();
    let mut points = Vec::new();
    let mut notes = Vec::new();
    if footprint.is_none() && !symbol.pins.is_empty() {
        notes.push("no pads were imported, so every new connection point sits at the part origin".into());
    }
    for pin in &symbol.pins {
        let label = &pin.number;
        if let Some(point) = existing.iter().find(|point| &point.point == label) {
            notes.push(format!(
                "pin '{label}' takes the part's existing connection point {}, which stays where it is",
                point.address()
            ));
            continue;
        }
        let mut position = [0., 0., 0.];
        if let Some(footprint) = footprint {
            let pads: Vec<usize> =
                (0..footprint.pads.len()).filter(|&i| &footprint.pads[i].number == label).collect();
            match pads.as_slice() {
                [] => notes.push(format!("pin '{label}' matches no pad, so its point sits at the part origin")),
                [first, rest @ ..] => {
                    position = pad_point(footprint, *first);
                    if !rest.is_empty() {
                        notes.push(format!(
                            "pin '{label}' is on {} pads; its point is on the first, at ({}, {}) mm",
                            pads.len(),
                            position[0],
                            position[1]
                        ));
                    }
                }
            }
        }
        points.push(json!({
            "name": label,
            "transform": { "position": position, "rotationEuler": PORT_UP }
        }));
    }
    (json!({ "name": IMPORT_PORT, "purpose": IMPORT_PURPOSE, "points": points }), notes)
}

// ============================================================================
// The dialog
// ============================================================================

/// Where a reviewed part came from, so new library folders can re-read it.
#[derive(Clone)]
enum Origin {
    Symbol { symbol: Symbol, choice: FootprintChoice },
    Footprint { file: String, bytes: Vec<u8> },
}

enum Stage {
    /// Choosing the file; the file dialog's explorer draws this one.
    File,
    /// A library holding several symbols: choose one.
    Symbol { file: String, symbols: Vec<Symbol>, warnings: Vec<String>, selected: Option<usize> },
    /// A generic symbol: choose its footprint among those its patterns offer.
    Footprint {
        symbol: Symbol,
        warnings: Vec<String>,
        filters: Vec<String>,
        candidates: Vec<(String, String)>,
        selected: Option<usize>,
    },
    /// Everything read: the user confirms.
    Review {
        origin: Origin,
        warnings: Vec<String>,
        part: KicadPart,
        /// The reader parsing the model's `.step`, while it runs.
        reading: Option<Reader>,
        /// The symbol and its file's warnings when the chain STOPPED at the
        /// footprint folder (a generic symbol, and no folder to offer
        /// footprints from). New folders then re-run the chain from the
        /// symbol, which offers the footprints the review could not; reading
        /// the review again would only repeat "no footprint folder".
        rechoose: Option<(Symbol, Vec<String>)>,
    },
}

/// What a click in a stage asks for, done once the stage is drawn.
enum Step {
    ChooseSymbol(Symbol, Vec<String>),
    Review(Origin, Vec<String>),
}

/// A `.step` being parsed off the UI thread: the answer arrives on `answer`.
struct Reader {
    answer: std::sync::mpsc::Receiver<Result<ModelFile, String>>,
    /// The file's size, for the progress line.
    bytes: usize,
}

impl Reader {
    /// Start parsing `source`. The native build parses on a thread of its own;
    /// the web build never gets here (it cannot read a KiCad library, so a part
    /// there has no `.step` to parse) and would parse in place.
    fn start(source: StepSource) -> Self {
        let bytes = source.text.len();
        let (send, answer) = std::sync::mpsc::channel();
        #[cfg(not(target_arch = "wasm32"))]
        std::thread::spawn(move || {
            // A dialog cancelled meanwhile has dropped the receiver; nobody waits.
            let _ = send.send(parse_step(&source));
        });
        #[cfg(target_arch = "wasm32")]
        let _ = send.send(parse_step(&source));
        Self { answer, bytes }
    }
}

/// What a frame of the dialog came to.
pub enum Outcome {
    Open,
    Closed,
    Imported(Imported),
}

/// The KiCad import modal's state, owned by the file dialog, which draws its
/// file-choosing stage with its own explorer.
pub struct KicadImport {
    kind: KicadKind,
    stage: Stage,
    library: Option<KicadLibrary>,
    footprints_buf: String,
    models_buf: String,
    search: String,
    pub status: String,
}

fn process_env(var: &str) -> Option<String> {
    std::env::var(var).ok().filter(|value| !value.is_empty())
}

impl KicadImport {
    pub fn new() -> Self {
        Self {
            kind: KicadKind::Symbol,
            stage: Stage::File,
            library: None,
            footprints_buf: String::new(),
            models_buf: String::new(),
            search: String::new(),
            status: String::new(),
        }
    }

    pub fn kind(&self) -> KicadKind {
        self.kind
    }

    /// Begin an import of `kind`: the file comes next.
    pub fn start(&mut self, kind: KicadKind, store: &dyn ModelStore) {
        self.kind = kind;
        self.stage = Stage::File;
        self.search.clear();
        self.status.clear();
        self.set_library(KicadLibrary::load(store, &process_env, &DiskFiles));
    }

    fn set_library(&mut self, library: KicadLibrary) {
        self.footprints_buf = library.footprints.path.clone();
        self.models_buf = library.models.path.clone();
        self.library = Some(library);
    }

    /// Whether the file is still to be chosen.
    pub fn wants_file(&self) -> bool {
        matches!(self.stage, Stage::File)
    }

    /// The chosen file arrived: parse it and go on to what it needs next.
    /// `file` is its full path where the store has one, which is where a
    /// footprint's relative model path starts from.
    pub fn take_file(&mut self, file: &str, bytes: &[u8], files: &dyn KicadFiles) {
        let Some(kind) = KicadKind::of_file(file) else {
            self.status = format!("{file} is not a .kicad_sym or .kicad_mod file");
            return;
        };
        self.kind = kind;
        let library = self.library.clone().unwrap_or_else(|| KicadLibrary::seeded(&process_env, files));
        self.library = Some(library.clone());
        match kind {
            KicadKind::Symbol => match read_symbol_library(file, bytes) {
                Ok((mut symbols, warnings)) if symbols.len() == 1 => {
                    self.choose_symbol(symbols.remove(0), warnings, files)
                }
                Ok((symbols, warnings)) => {
                    self.search.clear();
                    self.stage = Stage::Symbol { file: file.into(), symbols, warnings, selected: None };
                }
                Err(error) => self.status = format!("import failed: {error}"),
            },
            KicadKind::Footprint => {
                let origin = Origin::Footprint { file: file.into(), bytes: bytes.to_vec() };
                self.review(origin, Vec::new(), files);
            }
        }
    }

    /// A symbol is chosen: follow its footprint, or offer the footprints its
    /// patterns match.
    fn choose_symbol(&mut self, symbol: Symbol, warnings: Vec<String>, files: &dyn KicadFiles) {
        let library = self.library.clone().expect("a library once a file is taken");
        match kicad::footprint_link(&symbol) {
            FootprintLink::Chosen { filters } if files.available() && files.is_dir(&library.footprints.path) => {
                let candidates = footprint_candidates(&filters, &library, files);
                self.search.clear();
                self.stage = Stage::Footprint { symbol, warnings, filters, candidates, selected: None };
            }
            FootprintLink::Chosen { .. } => {
                let rechoose = (symbol.clone(), warnings.clone());
                let mut warnings = warnings;
                warnings.push(if files.available() {
                    format!(
                        "no footprint folder at {}, so no footprint can be chosen",
                        library.footprints.path
                    )
                } else {
                    "this build cannot read your KiCad library, so no footprint can be chosen".into()
                });
                self.review(Origin::Symbol { symbol, choice: FootprintChoice::Skip }, warnings, files);
                if let Stage::Review { rechoose: slot, .. } = &mut self.stage {
                    *slot = Some(rechoose);
                }
            }
            FootprintLink::Named { .. } => {
                self.review(Origin::Symbol { symbol, choice: FootprintChoice::Named }, warnings, files)
            }
        }
    }

    fn review(&mut self, origin: Origin, warnings: Vec<String>, files: &dyn KicadFiles) {
        let library = self.library.clone().expect("a library once a file is taken");
        let part = match &origin {
            Origin::Symbol { symbol, choice } => part_from_symbol(symbol.clone(), choice, &library, files),
            Origin::Footprint { file, bytes } => match part_from_footprint(file, bytes, &library, files) {
                Ok(part) => part,
                Err(error) => {
                    self.status = format!("import failed: {error}");
                    return;
                }
            },
        };
        let mut part = part;
        let reading = part.step.take().map(Reader::start);
        self.stage = Stage::Review { origin, warnings, part, reading, rechoose: None };
    }

    /// Take the reader's answer if it has come. While it has not, the dialog
    /// asks for another frame, so the answer shows without the pointer moving.
    fn poll_reader(&mut self, ctx: &egui::Context) {
        let Stage::Review { part, reading: Some(reader), .. } = &mut self.stage else {
            return;
        };
        match reader.answer.try_recv() {
            Ok(parsed) => part.settle_model(parsed),
            Err(std::sync::mpsc::TryRecvError::Empty) => {
                ctx.request_repaint_after(std::time::Duration::from_millis(50));
                return;
            }
            Err(std::sync::mpsc::TryRecvError::Disconnected) => {
                part.notes.push("the 3D model's reader stopped without an answer; the part imports without a model".into())
            }
        }
        if let Stage::Review { reading, .. } = &mut self.stage {
            *reading = None;
        }
    }

    /// Whether the model's `.step` is still being parsed.
    pub fn reading(&self) -> bool {
        matches!(self.stage, Stage::Review { reading: Some(_), .. })
    }


    /// Where the dialog is, for the published file state: its kind, its
    /// stage, and what a review would import.
    pub fn state_json(&self) -> Value {
        let stage = match &self.stage {
            Stage::File => "file",
            Stage::Symbol { .. } => "symbol",
            Stage::Footprint { .. } => "footprint",
            Stage::Review { .. } => "review",
        };
        let review = self.reviewed().map(|part| {
            json!({
                "symbol": part.symbol.as_ref().map(|s| s.library_id.clone()),
                "pads": part.footprint.as_ref().map(|f| f.pads.len()),
                "footprintFile": part.footprint_file,
                "stepFile": part.model.as_ref().map(|m| m.step_file.clone()),
                "notes": part.notes,
                "reading": self.reading(),
            })
        });
        json!({
            "kind": self.kind.extension(),
            "stage": stage,
            "status": self.status,
            "footprints": self.library.as_ref().map(|l| l.footprints.path.clone()),
            "models": self.library.as_ref().map(|l| l.models.path.clone()),
            "review": review,
        })
    }

    /// The part the dialog would import now, once it has been read.
    pub fn reviewed(&self) -> Option<&KicadPart> {
        match &self.stage {
            Stage::Review { part, .. } => Some(part),
            _ => None,
        }
    }

    /// Import the reviewed part; the dialog closes on success.
    pub fn confirm(&mut self, state: &mut EngineState) -> Result<Imported, String> {
        let Stage::Review { part, warnings, reading, .. } = &self.stage else {
            return Err("nothing has been read yet".into());
        };
        if reading.is_some() {
            return Err("the 3D model is still being read".into());
        }
        let mut imported = import_part(state, part)?;
        imported.notes.splice(0..0, warnings.iter().cloned());
        self.stage = Stage::File;
        Ok(imported)
    }

    /// Draw the stages after the file: the symbol list, the footprint list,
    /// the review. Hit keys are `kicad:*` (see `file::HIT_KEYS`).
    pub fn show(
        &mut self,
        ctx: &egui::Context,
        state: &mut EngineState,
        store: &dyn ModelStore,
        hits: &mut std::collections::HashMap<String, egui::Rect>,
    ) -> Outcome {
        self.show_with(ctx, state, store, hits, &DiskFiles)
    }

    /// [`Self::show`] reading the library through `files`.
    ///
    /// The layout is three bands. The folders, the stage's question and its
    /// search field sit at the top; the stage's buttons (Next, Use this
    /// footprint, Import) sit in the footer with Cancel; only the LIST between
    /// them scrolls. With Next laid out after the list, a 100-row search put it
    /// at y = 2540 in a 960-point window (ecad-reaudit-2026-09-23, B3).
    fn show_with(
        &mut self,
        ctx: &egui::Context,
        state: &mut EngineState,
        store: &dyn ModelStore,
        hits: &mut std::collections::HashMap<String, egui::Rect>,
        files: &dyn KicadFiles,
    ) -> Outcome {
        self.poll_reader(ctx);
        let reading = self.reading();
        let mut outcome = Outcome::Open;
        let mut step: Option<Step> = None;
        let mut hit = |key: &str, response: &egui::Response| {
            hits.insert(format!("kicad:{key}"), response.rect);
        };
        let modal = egui::Modal::new(egui::Id::new("brep-kicad-import")).show(ctx, |ui| {
            super::file_explorer::dialog_body(ui, |ui| {
                ui.heading("Import from KiCad");
                ui.add_space(4.0);
                super::file_explorer::dialog_footer(ui, "kicad", |ui| {
                    if !self.status.is_empty() {
                        ui.add_space(4.0);
                        ui.weak(&self.status);
                    }
                    ui.horizontal(|ui| {
                        let cancel = ui.button("Cancel");
                        hit("cancel", &cancel);
                        if cancel.clicked() {
                            outcome = Outcome::Closed;
                        }
                        step = step.take().or(self.show_buttons(ui, &mut hit));
                        if matches!(self.stage, Stage::Review { .. }) {
                            let import = ui.add_enabled(!reading, egui::Button::new("Import"));
                            hit("import", &import);
                            if import.clicked() {
                                match self.confirm(state) {
                                    Ok(imported) => outcome = Outcome::Imported(imported),
                                    Err(error) => self.status = format!("import refused: {error}"),
                                }
                            }
                        }
                    });
                });
                self.show_library(ui, store, files, &mut hit);
                ui.separator();
                self.show_question(ui, &mut hit);
                egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
                    step = step.take().or(self.show_list(ui, &mut hit));
                });
            })
        });
        match step {
            Some(Step::ChooseSymbol(symbol, warnings)) => self.choose_symbol(symbol, warnings, files),
            Some(Step::Review(origin, warnings)) => self.review(origin, warnings, files),
            None => {}
        }
        if modal.should_close() && matches!(outcome, Outcome::Open) {
            outcome = Outcome::Closed;
        }
        outcome
    }

    /// The two library folders, where each came from, and Apply.
    fn show_library(
        &mut self,
        ui: &mut egui::Ui,
        store: &dyn ModelStore,
        files: &dyn KicadFiles,
        hit: &mut impl FnMut(&str, &egui::Response),
    ) {
        let Some(library) = self.library.clone() else {
            return;
        };
        // Rows, not a grid: where each folder came from can name a path, and a
        // grid cell is as wide as its widest content ([`path_label`]).
        ui.horizontal(|ui| {
            ui.label("Footprints");
            let field = ui.text_edit_singleline(&mut self.footprints_buf);
            hit("field:footprints", &field);
        });
        path_label(ui, &library.footprints.from);
        ui.horizontal(|ui| {
            ui.label("3D models");
            let field = ui.text_edit_singleline(&mut self.models_buf);
            hit("field:models", &field);
        });
        path_label(ui, &library.models.from);
        let changed = self.footprints_buf != library.footprints.path || self.models_buf != library.models.path;
        let apply = ui.add_enabled(changed, egui::Button::new("Use these folders"));
        hit("apply-folders", &apply);
        if apply.clicked() {
            self.apply_folders(store, files);
        }
    }

    /// Take the typed folders, remember them, and read the chain again from
    /// them without the user starting over.
    fn apply_folders(&mut self, store: &dyn ModelStore, files: &dyn KicadFiles) {
        let Some(library) = self.library.clone() else {
            return;
        };
        let chosen = |path: &str, old: &Folder| {
            if path == old.path {
                old.clone()
            } else if files.is_dir(path) {
                Folder { path: path.into(), from: "your saved folder".into() }
            } else {
                Folder { path: path.into(), from: format!("{path} is not a folder") }
            }
        };
        let library = KicadLibrary {
            footprints: chosen(&self.footprints_buf, &library.footprints),
            models: chosen(&self.models_buf, &library.models),
        };
        self.status = match library.save(store) {
            Ok(()) => String::new(),
            Err(error) => format!("the folders were not saved: {error}"),
        };
        self.library = Some(library);
        // Read the chain again from the new folders: from the symbol when
        // the chain stopped at the footprint folder, else from the review.
        match &mut self.stage {
            Stage::Review { rechoose: Some((symbol, warnings)), .. } => {
                let (symbol, warnings) = (symbol.clone(), warnings.clone());
                self.choose_symbol(symbol, warnings, files);
            }
            Stage::Review { origin, warnings, .. } => {
                let (origin, warnings) = (origin.clone(), warnings.clone());
                self.review(origin, warnings, files);
            }
            Stage::Footprint { symbol, warnings, .. } => {
                let (symbol, warnings) = (symbol.clone(), warnings.clone());
                self.choose_symbol(symbol, warnings, files);
            }
            Stage::File | Stage::Symbol { .. } => {}
        }
    }

    /// The stage's question and its search field: above the list, so they
    /// stay put while it scrolls.
    fn show_question(&mut self, ui: &mut egui::Ui, hit: &mut impl FnMut(&str, &egui::Response)) {
        match &self.stage {
            Stage::File | Stage::Review { .. } => return,
            Stage::Symbol { file, symbols, .. } => {
                // The file's NAME: its folder is where the user just was.
                let name = file.rsplit(['/', '\\']).next().unwrap_or(file);
                ui.label(format!("{name} holds {} symbols. Choose one.", symbols.len()))
                    .on_hover_text(file.as_str());
            }
            Stage::Footprint { symbol, filters, .. } => {
                ui.label(format!(
                    "{} names no footprint. It offers footprints matching {}.",
                    symbol.library_id,
                    if filters.is_empty() { "anything".to_string() } else { filters.join(" ") }
                ));
            }
        }
        let search = ui.add(
            egui::TextEdit::singleline(&mut self.search).hint_text("search: an exact name comes first"),
        );
        hit("search", &search);
    }

    /// The stage's rows, best match first. A double click on a row chooses
    /// it and goes on, as the stage's button would.
    fn show_list(&mut self, ui: &mut egui::Ui, hit: &mut impl FnMut(&str, &egui::Response)) -> Option<Step> {
        let needle = self.search.clone();
        match &mut self.stage {
            Stage::File => {
                ui.weak("Choose a file.");
                None
            }
            Stage::Symbol { symbols, warnings, selected, .. } => {
                let mut opened = None;
                for index in ranked(symbols.iter().map(|s| s.library_id.as_str()), &needle) {
                    let symbol = &symbols[index];
                    let row = ui.selectable_label(
                        *selected == Some(index),
                        format!("{}  ({})", symbol.library_id, crate::panels::count(symbol.pins.len(), "pin", "pins")),
                    );
                    hit(&format!("symbol:{}", symbol.library_id), &row);
                    if row.clicked() {
                        *selected = Some(index);
                    }
                    if opens(&row) {
                        opened = Some(Step::ChooseSymbol(symbol.clone(), warnings.clone()));
                    }
                }
                opened
            }
            Stage::Footprint { symbol, warnings, candidates, selected, .. } => {
                let labels: Vec<String> = candidates.iter().map(|(lib, name)| format!("{lib}:{name}")).collect();
                let mut opened = None;
                for (shown, index) in ranked(labels.iter().map(String::as_str), &needle).into_iter().enumerate() {
                    if shown == 500 {
                        ui.weak("\u{2026} type to narrow the list");
                        break;
                    }
                    let label = &labels[index];
                    let row = ui.selectable_label(*selected == Some(index), label);
                    hit(&format!("footprint:{label}"), &row);
                    if row.clicked() {
                        *selected = Some(index);
                    }
                    if opens(&row) {
                        let (lib, name) = candidates[index].clone();
                        let choice = FootprintChoice::Chosen(lib, name);
                        opened = Some(Step::Review(Origin::Symbol { symbol: symbol.clone(), choice }, warnings.clone()));
                    }
                }
                if candidates.is_empty() {
                    ui.weak("No footprint in the library matches.");
                }
                opened
            }
            Stage::Review { warnings, part, reading, .. } => {
                show_review(ui, part, warnings, reading.as_ref().map(|reader| reader.bytes));
                None
            }
        }
    }

    /// The stage's own buttons, in the footer beside Cancel.
    fn show_buttons(
        &mut self,
        ui: &mut egui::Ui,
        hit: &mut impl FnMut(&str, &egui::Response),
    ) -> Option<Step> {
        match &self.stage {
            Stage::Symbol { symbols, warnings, selected, .. } => {
                let next = ui.add_enabled(selected.is_some(), egui::Button::new("Next"));
                hit("next", &next);
                let symbol = selected.map(|index| symbols[index].clone())?;
                next.clicked().then(|| Step::ChooseSymbol(symbol, warnings.clone()))
            }
            Stage::Footprint { symbol, warnings, candidates, selected, .. } => {
                let use_it = ui.add_enabled(selected.is_some(), egui::Button::new("Use this footprint"));
                hit("use-footprint", &use_it);
                let skip = ui.button("No footprint");
                hit("no-footprint", &skip);
                let choice = if use_it.clicked() {
                    let (lib, name) = candidates[selected.unwrap()].clone();
                    FootprintChoice::Chosen(lib, name)
                } else if skip.clicked() {
                    FootprintChoice::Skip
                } else {
                    return None;
                };
                Some(Step::Review(Origin::Symbol { symbol: symbol.clone(), choice }, warnings.clone()))
            }
            Stage::File | Stage::Review { .. } => None,
        }
    }
}

/// Whether a click on a list row opens it: a double click, or a triple one.
/// egui counts clicks by TIME alone, not place, so a click in the search field
/// a moment before makes the row's double click its triple: the audit's
/// double click on `Device:R` came within 0.3 s of another click and never
/// read as double (ecad-reaudit-2026-09-23, B3).
fn opens(row: &egui::Response) -> bool {
    row.double_clicked() || row.triple_clicked()
}

/// The indices of `ids` that `needle` matches, best first: an exact name, then
/// a name that starts with it, then a name that holds it anywhere; the file's
/// order within each. An id is `library:name`, and the needle is matched
/// against both the whole id and the name after its `:` (a leading `:` in the
/// needle anchors it to the name), ignoring case. Before this ranking a search
/// for "R" put `Device:R` some 250 rows down, under every symbol holding an r.
fn ranked<'a>(ids: impl Iterator<Item = &'a str>, needle: &str) -> Vec<usize> {
    let needle = needle.trim().to_lowercase();
    let bare = needle.strip_prefix(':').unwrap_or(&needle);
    let mut found: Vec<(u8, usize)> = ids
        .enumerate()
        .filter_map(|(index, id)| {
            let id = id.to_lowercase();
            let name = id.rsplit_once(':').map_or(id.as_str(), |(_, name)| name);
            let rank = if needle.is_empty() {
                2
            } else if id == needle || name == bare {
                0
            } else if id.starts_with(&needle) || name.starts_with(bare) {
                1
            } else if id.contains(&needle) {
                2
            } else {
                return None;
            };
            Some((rank, index))
        })
        .collect();
    found.sort();
    found.into_iter().map(|(_, index)| index).collect()
}

/// A file path in the width the dialog has: one line, cut at the end with an
/// ellipsis, the whole path on hover. Laid out at its natural width, a
/// 203-character footprint path made the review 2046 points wide, and Import
/// and Cancel went off a 1400-point window.
fn path_label(ui: &mut egui::Ui, path: &str) {
    ui.add(egui::Label::new(egui::RichText::new(path).weak()).truncate())
        .on_hover_text(path);
}

/// What the import will write, and every note, before the user confirms. The
/// short facts sit in a grid; each file PATH gets a line of its own under its
/// fact, as wide as the dialog and cut to fit it ([`path_label`]), because a
/// grid cell is as wide as its widest content.
fn show_review(ui: &mut egui::Ui, part: &KicadPart, warnings: &[String], reading: Option<usize>) {
    let fact = |ui: &mut egui::Ui, name: &str, value: String| {
        ui.horizontal_wrapped(|ui| {
            ui.label(egui::RichText::new(name).strong());
            ui.label(value);
        });
    };
    fact(
        ui,
        "Symbol",
        part.symbol.as_ref().map_or("none".into(), |s| format!("{} ({})", s.library_id, crate::panels::count(s.pins.len(), "pin", "pins"))),
    );
    fact(
        ui,
        "Pads",
        part.footprint.as_ref().map_or("none".into(), |f| format!("{} ({})", f.name, crate::panels::count(f.pads.len(), "pad", "pads"))),
    );
    if let (Some(_), Some(file)) = (&part.footprint, &part.footprint_file) {
        path_label(ui, file);
    }
    match (&part.model, reading) {
        (_, Some(bytes)) => {
            ui.horizontal_wrapped(|ui| {
                ui.label(egui::RichText::new("3D model").strong());
                ui.spinner();
                ui.label(format!("reading the model's STEP file ({} KB)\u{2026}", bytes.div_ceil(1024)));
            });
        }
        (Some(m), None) => {
            fact(ui, "3D model", crate::panels::count(m.bodies.len(), "body", "bodies"));
            path_label(ui, &m.step_file);
            ui.weak(format!(
                "offset {:?} mm, rotation {:?}°, scale {:?}",
                m.model.offset, m.model.rotation, m.model.scale
            ));
        }
        (None, None) => fact(ui, "3D model", "none".into()),
    }
    if let Some(symbol) = &part.symbol {
        fact(
            ui,
            "Connection points",
            format!(
                "{} in the part's `Pins` group, {}",
                symbol.pins.len(),
                if part.footprint.is_some() { "each at its pin's pad" } else { "at the part origin" }
            ),
        );
    }
    let notes: Vec<&String> = warnings.iter().chain(&part.notes).collect();
    if !notes.is_empty() {
        ui.add_space(6.0);
        for note in notes {
            // A note names the file it looked for, so it WRAPS (it is a
            // sentence, not a path to scan), breaking inside a long path too.
            ui.add(
                egui::Label::new(
                    egui::RichText::new(format!("\u{2022} {note}")).color(ui.visuals().warn_fg_color),
                )
                .wrap_mode(egui::TextWrapMode::Wrap),
            );
        }
    }
}