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
//! The isometric's required scale disclaimer, planned before free late furniture.
//! Python: projection._fit_iso_view, builder's post-fit furniture, and
//! annotations._placement_occupancy.place_iso_nts_note.

use serde::{Deserialize, Serialize};

use crate::dims::{Placed, PlannedDimension};
use crate::items::State;
use crate::model::{Analysis, NTS_CAPTION, NTS_CAPTION_ID, Plan, PlannedSheet, PlannedView};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PlannedCaption {
    pub id: String,
    pub view: String,
    pub sheet: u32,
    pub text: String,
    pub height: f64,
    /// Centre of the caption's ink, in paper mm.
    pub at: [f64; 2],
    pub ink: [f64; 4],
    pub default: bool,
    pub basis: String,
    pub state: State,
}

pub(crate) fn proxy(c: &PlannedCaption) -> PlannedDimension {
    PlannedDimension {
        callout_offset: None,
        id: c.id.clone(),
        kind: "caption".into(),
        view: c.view.clone(),
        label: c.text.clone(),
        value: 0.0,
        reference: false,
        determined_by: Vec::new(),
        region: None,
        terms: Vec::new(),
        measurements: Vec::new(),
        default: true,
        from_spec: Vec::new(),
        basis: c.basis.clone(),
        state: State::Shown,
        placed: Some(Placed {
            lines: Vec::new(),
            arrows: Vec::new(),
            label_at: c.at,
            rotation: 0.0,
            label_box: c.ink,
        }),
    }
}

fn view_box(analysis: &Analysis, v: &PlannedView) -> [f64; 4] {
    if v.id == "v:iso" && v.direction == crate::model::VIEWS[3].1 {
        let b = analysis.drawing_iso_bounds();
        [
            v.at[0] + v.scale * b[0],
            v.at[1] + v.scale * b[1],
            v.at[0] + v.scale * b[2],
            v.at[1] + v.scale * b[3],
        ]
    } else {
        let (size, _) = crate::detail::crop_extent(analysis, v);
        [
            v.at[0] - v.scale * size[0] / 2.,
            v.at[1] - v.scale * size[1] / 2.,
            v.at[0] + v.scale * size[0] / 2.,
            v.at[1] + v.scale * size[1] / 2.,
        ]
    }
}

pub(crate) fn plan(
    analysis: &Analysis,
    sheet: &PlannedSheet,
    views: &[PlannedView],
    ink: &[PlannedDimension],
) -> Vec<PlannedCaption> {
    let mut captions = Vec::new();
    let mut ink = ink.to_vec();
    for view in views
        .iter()
        .filter(|v| matches!(v.id.as_str(), "v:iso" | "v:iso_below") && v.sheet == sheet.number)
    {
        let isometric = {
            let d = view.direction.map(f64::abs);
            (d[0] - d[1]).abs() <= 1e-9 * d[0] && (d[0] - d[2]).abs() <= 1e-9 * d[0]
        };
        if view.id == "v:iso" && isometric && (view.scale / sheet.scale_factor - 1.).abs() < 1e-6 {
            continue;
        }
        if analysis.drawing_iso_bounds() == [0.; 4] {
            continue;
        }
        let text = match (isometric, view.id == "v:iso_below") {
            (true, false) => NTS_CAPTION,
            (true, true) => "ISO VIEW FROM BELOW (NTS)",
            (false, false) => "PICTORIAL VIEW (NTS)",
            (false, true) => "PICTORIAL VIEW FROM BELOW (NTS)",
        };
        let caption = plan_one(analysis, sheet, views, &ink, view, text);
        ink.push(proxy(&caption));
        captions.push(caption);
    }
    captions
}
fn plan_one(
    analysis: &Analysis,
    sheet: &PlannedSheet,
    views: &[PlannedView],
    ink: &[PlannedDimension],
    iso: &PlannedView,
    text: &str,
) -> PlannedCaption {
    let mut obstacles: Vec<_> = views
        .iter()
        .filter(|v| v.sheet == sheet.number)
        .map(|v| {
            let b = view_box(analysis, v);
            if v.id == iso.id {
                b
            } else {
                // The caption belongs to the isometric's composed footprint.
                // A clear silhouette does not release a neighbouring view's
                // reserved ladder band for that footprint to occupy.
                [
                    b[0].min(v.area[0]),
                    b[1].min(v.area[1]),
                    b[2].max(v.area[2]),
                    b[3].max(v.area[3]),
                ]
            }
        })
        .collect();
    obstacles.push(sheet.title_block);
    obstacles.extend(crate::sheet_fit::furniture_bounds(analysis, sheet, views));
    obstacles.extend(section_text_obstacles(analysis, sheet, views));
    let pad = 1.2f64.max(crate::dims::ARROW / 2.0);
    for d in ink.iter().filter(|d| {
        views
            .iter()
            .find(|v| v.id == d.view)
            .is_none_or(|v| v.sheet == sheet.number)
    }) {
        let Some(p) = &d.placed else { continue };
        obstacles.push(p.label_box);
        // A PMI item's rendered ink box spans its glyph and leader together.
        // Keep the caption outside that whole item, including the empty corner of
        // a diagonal shaft's box, as the rendered caption audit requires.
        if d.id.starts_with("pmi:") {
            obstacles.extend(crate::dims::ink_box(d));
        }
        obstacles.extend(p.lines.iter().chain(&p.arrows).map(|[a, b]| {
            [
                a[0].min(b[0]) - pad,
                a[1].min(b[1]) - pad,
                a[0].max(b[0]) + pad,
                a[1].max(b[1]) + pad,
            ]
        }));
    }
    let height = crate::section::FONT_SIZE;
    let laid = crate::dims::font().text(text, height);
    let size = [laid.max[0] - laid.min[0], laid.max[1] - laid.min[1]];
    let at = crate::model::nts_caption_at(
        sheet.page,
        view_box(analysis, iso),
        iso.at[0],
        size,
        &obstacles,
    );
    PlannedCaption {
        id: if iso.id == "v:iso" {
            NTS_CAPTION_ID.into()
        } else {
            "note_iso_below_nts".into()
        },
        view: iso.id.clone(),
        sheet: iso.sheet,
        text: text.into(),
        height,
        at,
        ink: [
            at[0] - size[0] / 2.0,
            at[1] - size[1] / 2.0,
            at[0] + size[0] / 2.0,
            at[1] + size[1] / 2.0,
        ],
        default: true,
        basis: "the drawn isometric scale differs from the sheet scale".into(),
        state: State::Shown,
    }
}

/// Text and cutting-plane furniture use the same font, anchors and settled line work as
/// rendering, including section references whose view lives on another sheet. The fill
/// estimator's conservative text metrics are deliberately not used for these labels.
pub(crate) fn section_text_obstacles(
    analysis: &Analysis,
    sheet: &PlannedSheet,
    views: &[PlannedView],
) -> Vec<[f64; 4]> {
    let text_box = |text: &str, at: [f64; 2]| {
        let laid = crate::dims::font().text(text, crate::section::FONT_SIZE);
        [
            at[0] + laid.min[0],
            at[1] + laid.min[1],
            at[0] + laid.max[0],
            at[1] + laid.max[1],
        ]
    };
    let mut out = Vec::new();
    for v in views {
        if v.sheet == sheet.number {
            // Invariant: Planned cameras come from the fixed views or checked section/detail cameras, with nonparallel axes.
            let view = haecceity::hlr::View::new(v.direction, v.up).expect("planned view");
            let half = 0.5 * crate::model::extent(analysis, &view).0[1] * v.scale;
            if let Some(note) = &v.scale_note {
                let gap = if v.cut.is_some() { 17.0 } else { 7.0 };
                out.push(text_box(note, [v.at[0], v.at[1] - half - gap]));
            }
            if let Some(cut) = &v.cut {
                out.push(text_box(
                    &format!("SECTION {}–{}", cut.label, cut.label),
                    [v.at[0], v.at[1] - half - 7.0],
                ));
                if let Some(r) = &v.xref {
                    out.push(text_box(
                        &format!("FROM SHEET {}, ZONE {}", r.sheet, r.zone),
                        r.at,
                    ));
                }
            }
        }
        if let Some(cut) = &v.cut
            && let Some(marker) = views
                .iter()
                .find(|m| m.id == cut.marked_in && m.sheet == sheet.number)
            && let Some(work) = marker.line_work.iter().find(|w| w.kind == "cutting_plane")
            && let Some(crate::output::Curve::Line(a, b)) = work.curves.first()
        {
            let (ink, letters) = crate::section::cutting_plane(a[0], b[0], a[1]);
            out.extend(crate::output::items_bbox(&ink));
            out.extend(letters.map(|at| text_box(&cut.label, at)));
            if let Some(r) = &cut.xref {
                out.push(text_box(
                    &format!(
                        "SECTION {}–{}, SHEET {}, ZONE {}",
                        cut.label, cut.label, r.sheet, r.zone
                    ),
                    r.at,
                ));
            }
        }
    }
    out
}

/// Reconcile after measured refits, pinned placement, or allocation to another sheet.
/// Tables already avoided the reserved caption; include them here in case later recovery
/// changed a view. Never retain an obsolete NTS claim when a refit restores sheet scale.
pub(crate) fn refresh(analysis: &Analysis, p: &mut Plan) {
    let mut ink = p.dimensions.clone();
    ink.extend(crate::centrelines::obstacles(&p.views));
    ink.extend(p.gdt.iter().filter_map(crate::dims::gdt::proxy));
    // Balloons and their leaders are settled defining annotation ink.
    for b in &p.balloons {
        let mut d = proxy(&PlannedCaption {
            id: b.id.clone(),
            view: b.view.clone(),
            sheet: 1,
            text: b.tag.clone(),
            height: 3.0,
            at: b.at,
            ink: {
                let label = b.label_box.unwrap_or([b.at[0], b.at[1], b.at[0], b.at[1]]);
                [
                    label[0].min(b.at[0] - b.radius),
                    label[1].min(b.at[1] - b.radius),
                    label[2].max(b.at[0] + b.radius),
                    label[3].max(b.at[1] + b.radius),
                ]
            },
            default: true,
            basis: String::new(),
            state: State::Shown,
        });
        d.placed
            .as_mut()
            // Invariant: The proxy constructor always supplies a placement.
            .expect("balloon proxy")
            .lines
            .extend(b.leader);
        ink.push(d);
    }
    let mut captions: Vec<PlannedCaption> = std::iter::once(&p.sheet)
        .chain(&p.sheets)
        .flat_map(|sheet| {
            let mut local = ink.clone();
            if sheet.number == 1 {
                local.extend(
                    p.requirements
                        .iter()
                        .filter_map(crate::dims::gdt::finish_proxy),
                );
                local.extend(
                    p.radius_table
                        .iter()
                        .filter_map(|t| crate::tables::proxy(&t.table)),
                );
                local.extend(
                    p.notes_block
                        .iter()
                        .chain(&p.radius_notes_block)
                        .map(crate::notes::proxy),
                );
            }
            local.extend(
                p.tables
                    .iter()
                    .filter(|t| t.sheet == sheet.number)
                    .filter_map(crate::tables::proxy),
            );
            for t in p
                .hole_table
                .iter()
                .chain(&p.hole_tables)
                .filter(|t| t.sheet == sheet.number)
            {
                local.extend(t.blocks.iter().filter_map(crate::tables::proxy));
            }
            plan(analysis, sheet, &p.views, &local)
        })
        .collect();
    // Refresh the ink without discarding already published provenance for this same caption.
    for caption in &mut captions {
        if let Some(previous) = p.captions.iter().find(|old| old.id == caption.id) {
            caption.basis.clone_from(&previous.basis);
        }
    }
    p.captions = captions;
}

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

    #[test]
    fn detail_obstacle_equals_its_crop_footprint() {
        let (_, a) = crate::model::analyse_native(
            &std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("tests/fixtures/parts/mounting_plate.step"),
        )
        .unwrap();
        let spec = serde_json::from_value(serde_json::json!({"sheet":{"size":"A2","scale":"1:2"},"views":{"detail_c":{"detail":{"around":"f:hole:f6","from":"plan","scale_factor":2}}}})).unwrap();
        let p = crate::model::plan(&a, &spec).unwrap();

        let v = p.views.iter().find(|v| v.id == "v:detail_c").unwrap();
        let mut crop_only = p.clone();
        crop_only.views = vec![v.clone()];
        let footprint = crate::sheet_fit::footprints(&a, &crop_only)[0];
        assert_eq!(view_box(&a, v), footprint);
    }

    #[test]
    fn a_section_reference_is_settled_ink_for_the_iso_caption() {
        let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
        let (_, analysis) =
            crate::model::analyse_native(&root.join("tests/fixtures/parts/plate.step")).unwrap();
        let p = crate::model::plan(&analysis, &crate::model::Spec::default()).unwrap();
        let base = plan(&analysis, &p.sheet, &p.views, &[]);
        let at = base.first().expect("scaled iso fixture").at;
        let mut views = p.views.clone();
        let mut section = views.iter().find(|v| v.id == "v:front").unwrap().clone();
        section.id = "v:section_aa".into();
        section.cut = Some(crate::model::Cut {
            xref: None,
            label: "A".into(),
            origin: [0.0; 3],
            normal: [0.0, 1.0, 0.0],
            marked_in: "v:plan".into(),
        });
        section.xref = Some(crate::sheets::CrossRef {
            sheet: 2,
            zone: "B3".into(),
            at,
        });
        views.push(section);
        let chosen = plan(&analysis, &p.sheet, &views, &[]);
        assert_ne!(
            chosen[0].at, at,
            "the reference label occupies the natural caption position"
        );
        let text = crate::dims::font().text("FROM SHEET 2, ZONE B3", crate::section::FONT_SIZE);
        let b = [
            at[0] + text.min[0],
            at[1] + text.min[1],
            at[0] + text.max[0],
            at[1] + text.max[1],
        ];
        let c = chosen[0].ink;
        assert!(!(c[0] < b[2] && c[2] > b[0] && c[1] < b[3] && c[3] > b[1]));
    }
}