draftwright 0.6.0-alpha.2

Automated technical drawings from STEP B-rep: a Rust port of draftwright.
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
//! An opt-in on-disk memo of finished plans and of the analyses they are made from, so that
//! processes planning the same part under the same Spec (CI's test shards and acceptance
//! measurements) analyse and plan it once and read it after.
//!
//! A plan is keyed by the engine's source fingerprint (`DRAFTWRIGHT_ENGINE_VERSION`: every
//! source file, asset, the manifest and the lockfile), the planning function, and the complete
//! Analysis and Spec as JSON; an analysis by the fingerprint, the file's name and its bytes. So
//! an entry is only ever read by the engine that made it, for exactly the inputs it was made
//! from. A miss computes as usual; refusals are not stored.
//!
//! A plan's entry holds its JSON and the planning state the JSON leaves out (table escalation,
//! callout geometry, axis overruns, each problem's fixes as held). Every entry is only written
//! when reading it back gives a value identical to the one made, field for field (`Debug`), so
//! a cached plan is the plan; a refused entry leaves a `.refused` note saying where it differs.
//!
//! Nothing here is used unless asked for: the functions take the directory, and the command
//! line reads `DRAFTWRIGHT_PLAN_CACHE` ([`dir`]).
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::fs::{self, File};
use std::io::Write as _;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::haecceity::Part;
use crate::hole_table::TableOutcome;
use crate::model::{self, Analysis, Invalid, Plan, Spec, Unanalysed};

/// The variable naming the command line's plan cache directory.
pub const VARIABLE: &str = "DRAFTWRIGHT_PLAN_CACHE";
const FORMAT: &str = "draftwright.plan-cache/1";

/// The command line's plan cache directory: `DRAFTWRIGHT_PLAN_CACHE`, when set and not empty.
pub fn dir() -> Option<PathBuf> {
    std::env::var_os(VARIABLE)
        .filter(|v| !v.is_empty())
        .map(PathBuf::from)
}

/// [`model::plan_recovered`], read from or written to the cache in *dir* when given.
pub fn plan_recovered_in(
    dir: Option<&Path>,
    analysis: &Analysis,
    spec: &Spec,
) -> Result<Plan, Invalid> {
    planned(dir, "recovered", analysis, spec, model::plan_recovered)
}

/// [`model::plan`], read from or written to the cache in *dir* when given.
pub fn plan_in(dir: Option<&Path>, analysis: &Analysis, spec: &Spec) -> Result<Plan, Invalid> {
    planned(dir, "plan", analysis, spec, model::plan)
}

/// [`model::analyse_native`], its Analysis read from or written to the cache in *dir* when
/// given. On a hit the part is read from the file again, which is a small share of analysing
/// it: the same part, its lazily computed surface grids not yet computed.
pub fn analyse_native_in(dir: Option<&Path>, path: &Path) -> Result<(Part, Analysis), Unanalysed> {
    analysed_in(dir, path, &[b"analyse_native"], || {
        model::analyse_native(path)
    })
}

/// *compute*'s part and Analysis of the STEP file at *path* with no part of an assembly chosen
/// (the whole file, or the part `binding.part_index` names when its bodies coincide), the
/// Analysis read from or written to the cache in *dir* when given, keyed by the file's name and
/// bytes and *recipe*: which reading *compute* makes, with every other input it reads
/// (recognition JSON, for one). On a hit the part is read from the file again.
pub fn analysed_in<E>(
    dir: Option<&Path>,
    path: &Path,
    recipe: &[&[u8]],
    compute: impl FnOnce() -> Result<(Part, Analysis), E>,
) -> Result<(Part, Analysis), E> {
    let Some(dir) = dir else {
        return compute();
    };
    let Ok(bytes) = fs::read(path) else {
        return compute();
    };
    let name = path.file_name().unwrap_or_default().to_string_lossy();
    let mut inputs: Vec<&[u8]> = vec![b"analysis", name.as_bytes(), &bytes];
    inputs.extend(recipe);
    let mut computed = None;
    let mut compute = Some(compute);
    let analysis = memo(
        dir,
        &key(&inputs),
        "analysis",
        |a: &Analysis| serde_json::to_vec(a).ok(),
        |text| serde_json::from_slice(text).ok(),
        || {
            let (p, a) = (compute.take().unwrap())()?;
            computed = Some(p);
            Ok(a)
        },
    )?;
    match computed {
        Some(part) => Ok((part, analysis)),
        // The part the Analysis was read from: one part alone when every body coincided with
        // it (#212), set square to the file's axes when it was (#216).
        None => {
            match model::step_bytes(&bytes)
                .ok()
                .and_then(|b| match analysis.binding.part_index {
                    None => crate::haecceity::read_step(&b).ok(),
                    Some(k) => crate::haecceity::step::StepFile::read(&b)
                        .and_then(|file| file.part(k))
                        .ok(),
                })
                .and_then(|part| model::as_analysed(part, &analysis).ok())
            {
                Some(part) => Ok((part, analysis)),
                None => (compute.take().unwrap())(),
            }
        }
    }
}

fn planned(
    dir: Option<&Path>,
    kind: &str,
    analysis: &Analysis,
    spec: &Spec,
    compute: fn(&Analysis, &Spec) -> Result<Plan, Invalid>,
) -> Result<Plan, Invalid> {
    let Some(dir) = dir else {
        return compute(analysis, spec);
    };
    memo(
        dir,
        &plan_key(kind, analysis, spec),
        "plan",
        |plan: &Plan| serde_json::to_vec(&Entry::of(plan)).ok(),
        |text| {
            serde_json::from_slice::<Entry>(text)
                .ok()
                .map(Entry::into_plan)
        },
        || compute(analysis, spec),
    )
}

fn plan_key(kind: &str, analysis: &Analysis, spec: &Spec) -> String {
    key(&[
        kind.as_bytes(),
        &serde_json::to_vec(analysis).expect("an Analysis serialises"),
        &serde_json::to_vec(spec).expect("a Spec serialises"),
    ])
}

/// The entry's file name: the digest of the engine and of everything the value is a function of.
fn key(inputs: &[&[u8]]) -> String {
    let mut hash = Sha256::new();
    let engine: [&[u8]; 5] = [
        FORMAT.as_bytes(),
        env!("DRAFTWRIGHT_ENGINE_VERSION").as_bytes(),
        std::env::consts::ARCH.as_bytes(),
        std::env::consts::OS.as_bytes(),
        if cfg!(debug_assertions) {
            b"debug"
        } else {
            b"release"
        },
    ];
    for part in engine.iter().chain(inputs) {
        hash.update((part.len() as u64).to_le_bytes());
        hash.update(part);
    }
    hash.finalize().iter().map(|b| format!("{b:02x}")).collect()
}

/// The value stored under *key* in *dir*, or *compute*'s, stored when it reads back identically.
fn memo<T: Debug, E>(
    dir: &Path,
    key: &str,
    kind: &str,
    encode: impl Fn(&T) -> Option<Vec<u8>>,
    decode: impl Fn(&[u8]) -> Option<T>,
    compute: impl FnOnce() -> Result<T, E>,
) -> Result<T, E> {
    let path = dir.join(format!("{kind}-{key}.json"));
    let read = || fs::read(&path).ok().and_then(|text| decode(&text));
    if let Some(value) = read() {
        return Ok(value);
    }
    // One process computes a key while the others wanting it wait, then read its entry. The
    // lock is advisory and only orders work: without it (or on any I/O error) each computes.
    let lock = fs::create_dir_all(dir)
        .and_then(|()| File::create(dir.join(format!("{kind}-{key}.lock"))))
        .ok()
        .filter(|f| f.lock().is_ok());
    if lock.is_some()
        && let Some(value) = read()
    {
        return Ok(value);
    }
    let value = compute()?;
    if let Some(text) = encode(&value) {
        write(
            dir,
            &path,
            &text,
            decode(&text).map(|v| format!("{v:?}")),
            &value,
        );
    }
    drop(lock);
    Ok(value)
}

/// Stores *text* when what it decodes to (*read*) is identical to *made*; otherwise notes where
/// they differ beside the entry, and stores nothing.
fn write<T: Debug>(dir: &Path, path: &Path, text: &[u8], read: Option<String>, made: &T) {
    let made = format!("{made:?}");
    let read = read.unwrap_or_default();
    if read != made {
        let at = read
            .bytes()
            .zip(made.bytes())
            .take_while(|(r, m)| r == m)
            .count();
        let context = |s: &str| {
            s.get(at.saturating_sub(200)..(at + 200).min(s.len()))
                .unwrap_or("")
                .to_string()
        };
        let _ = fs::write(
            path.with_extension("refused"),
            format!("read: {}\nmade: {}\n", context(&read), context(&made)),
        );
        return;
    }
    let staged = dir.join(format!(
        ".{}.{}.tmp",
        path.file_name().unwrap().to_string_lossy(),
        std::process::id()
    ));
    let done = File::create(&staged)
        .and_then(|mut f| f.write_all(text))
        .and_then(|()| fs::rename(&staged, path));
    if done.is_err() {
        let _ = fs::remove_file(&staged);
    }
}

/// A plan and the planning state its JSON leaves out.
#[derive(Serialize, Deserialize)]
struct Entry {
    plan: Plan,
    hidden: Hidden,
}

#[derive(Serialize, Deserialize)]
struct Hidden {
    table_outcome: u8,
    table_restore: Option<Box<Entry>>,
    callout_widths: BTreeMap<String, f64>,
    callout_tips: BTreeMap<String, (String, [f64; 2])>,
    dimension_labels: BTreeMap<String, (String, [f64; 2])>,
    /// Each view's gutter translation, which a pin replay of this plan spends.
    gutter_deltas: BTreeMap<String, [f64; 2]>,
    /// Each view's line work's overruns, in order.
    overruns: Vec<Vec<f64>>,
    /// Each problem's fixes as held: a problem's JSON states its offered fixes instead.
    problems: Vec<RawProblem>,
}

#[derive(Serialize, Deserialize)]
struct RawProblem {
    fix: Vec<crate::items::Op>,
    fixes: Vec<RawFix>,
}

#[derive(Serialize, Deserialize)]
struct RawFix {
    description: String,
    ops: Vec<crate::items::Op>,
    minimum_scope: Option<crate::remedy_scope::Scope>,
}

impl Entry {
    fn of(plan: &Plan) -> Self {
        Entry {
            plan: plan.clone(),
            hidden: Hidden {
                table_outcome: match plan.table_outcome {
                    TableOutcome::Kept => 0,
                    TableOutcome::NoPlace => 1,
                    TableOutcome::Balloons => 2,
                    TableOutcome::None => 3,
                },
                table_restore: plan
                    .table_restore
                    .as_deref()
                    .map(|p| Box::new(Entry::of(p))),
                callout_widths: plan.callout_widths.clone(),
                callout_tips: plan.callout_tips.clone(),
                dimension_labels: plan.dimension_labels.clone(),
                gutter_deltas: plan.gutter_deltas.clone(),
                overruns: plan
                    .views
                    .iter()
                    .map(|v| v.line_work.iter().map(|w| w.overrun).collect())
                    .collect(),
                problems: plan
                    .problems
                    .iter()
                    .map(|p| RawProblem {
                        fix: p.fix.clone(),
                        fixes: p
                            .fixes
                            .iter()
                            .map(|f| RawFix {
                                description: f.description.clone(),
                                ops: f.ops.clone(),
                                minimum_scope: f.minimum_scope,
                            })
                            .collect(),
                    })
                    .collect(),
            },
        }
    }

    fn into_plan(self) -> Plan {
        let Entry { mut plan, hidden } = self;
        plan.table_outcome = match hidden.table_outcome {
            0 => TableOutcome::Kept,
            1 => TableOutcome::NoPlace,
            2 => TableOutcome::Balloons,
            _ => TableOutcome::None,
        };
        plan.table_restore = hidden.table_restore.map(|e| Box::new(e.into_plan()));
        plan.callout_widths = hidden.callout_widths;
        plan.callout_tips = hidden.callout_tips;
        plan.dimension_labels = hidden.dimension_labels;
        plan.gutter_deltas = hidden.gutter_deltas;
        for (view, overruns) in plan.views.iter_mut().zip(hidden.overruns) {
            for (work, overrun) in view.line_work.iter_mut().zip(overruns) {
                work.overrun = overrun;
            }
        }
        for (problem, raw) in plan.problems.iter_mut().zip(hidden.problems) {
            problem.fix = raw.fix;
            problem.fixes = raw
                .fixes
                .into_iter()
                .map(|f| crate::fixes::Fix {
                    description: f.description,
                    ops: f.ops,
                    minimum_scope: f.minimum_scope,
                })
                .collect();
        }
        plan
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A cached plan is the plan: written on the first call, read on the second, identical in
    /// every field to planning afresh (including what the plan's JSON leaves out); another Spec
    /// or planning function is another entry.
    #[test]
    fn a_cached_plan_is_the_plan_and_other_inputs_miss() {
        let dir =
            std::env::temp_dir().join(format!("draftwright-plan-cache-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/parts");
        for file in ["mounting_plate.step", "turned.step", "scattered_holes.step"] {
            let (_, analysis) = model::analyse_native(&root.join(file)).unwrap();
            let spec = Spec::default();
            let fresh = model::plan_recovered(&analysis, &spec).unwrap();
            let first = plan_recovered_in(Some(&dir), &analysis, &spec).unwrap();
            let entry = dir.join(format!(
                "plan-{}.json",
                plan_key("recovered", &analysis, &spec)
            ));
            assert!(entry.is_file(), "{file}: the plan is stored");
            let second = plan_recovered_in(Some(&dir), &analysis, &spec).unwrap();
            assert_eq!(format!("{first:?}"), format!("{fresh:?}"), "{file}");
            assert_eq!(
                format!("{second:?}"),
                format!("{fresh:?}"),
                "{file}: read back"
            );
            assert!(
                fresh.views.iter().any(|v| !v.line_work.is_empty())
                    || !fresh.callout_tips.is_empty(),
                "{file}: the part exercises planning state the plan's JSON leaves out"
            );
            assert_ne!(
                plan_key("plan", &analysis, &spec),
                plan_key("recovered", &analysis, &spec)
            );
            let mut other = spec.clone();
            other.sheet.view_gutter = Some(3.0);
            assert_ne!(
                plan_key("recovered", &analysis, &other),
                plan_key("recovered", &analysis, &spec)
            );
            let direct = plan_in(Some(&dir), &analysis, &spec).unwrap();
            let direct_fresh = model::plan(&analysis, &spec).unwrap();
            assert_eq!(
                format!("{direct:?}"),
                format!("{direct_fresh:?}"),
                "{file}: plan"
            );
        }
        let _ = fs::remove_dir_all(&dir);
    }

    /// A cached analysis is the analysis, and the part read again beside it draws the same
    /// drawing as the part analysed afresh.
    #[test]
    fn a_cached_analysis_is_the_analysis_and_its_part_draws_the_same() {
        let dir =
            std::env::temp_dir().join(format!("draftwright-analysis-cache-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        let font = crate::text::Font::new(crate::api::MONO.to_vec()).unwrap();
        let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/parts");
        for file in [
            "mounting_plate.step",
            "turned.step",
            "manufacturing_pmi.step.gz",
        ] {
            let path = root.join(file);
            let (part, fresh) = model::analyse_native(&path).unwrap();
            let (_, first) = analyse_native_in(Some(&dir), &path).unwrap();
            let (read, second) = analyse_native_in(Some(&dir), &path).unwrap();
            assert_eq!(
                dir.read_dir()
                    .unwrap()
                    .filter(|e| e
                        .as_ref()
                        .unwrap()
                        .path()
                        .extension()
                        .is_some_and(|x| x == "refused"))
                    .count(),
                0
            );
            assert_eq!(format!("{first:?}"), format!("{fresh:?}"), "{file}");
            assert_eq!(
                format!("{second:?}"),
                format!("{fresh:?}"),
                "{file}: read back"
            );
            let plan = model::plan_recovered(&fresh, &Spec::default()).unwrap();
            let drawn = |p: &Part| {
                format!(
                    "{:?}",
                    model::render_sheets(p, &fresh, &plan, &font).unwrap()
                )
            };
            assert_eq!(drawn(&read), drawn(&part), "{file}: the part read again");
        }
        let _ = fs::remove_dir_all(&dir);
    }
}