draftwright 0.6.1

Dimensioned engineering drawings from STEP, with SVG, PDF and editable DXF output.
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
//! The common Python CLI subset, shared with `render` so both author the same Spec.
use std::path::{Path, PathBuf};

use crate::{api::Error, model::Spec};
use clap::{Args, CommandFactory, Parser};

use super::{Cli, Given};

#[derive(Args, Default)]
pub(super) struct SheetOptions {
    #[arg(long)]
    title: Option<String>,
    #[arg(long)]
    number: Option<String>,
    #[arg(long)]
    material: Option<String>,
    #[arg(long)]
    drawn_by: Option<String>,
    #[arg(long)]
    revision: Option<String>,
    #[arg(long)]
    date: Option<String>,
    #[arg(long)]
    company: Option<String>,
    #[arg(long)]
    approved_by: Option<String>,
    /// General tolerance in the title block
    #[arg(long)]
    tolerance: Option<String>,
    /// Drawing scale as a number or ratio, e.g. 5, 0.5 or 1:2 (default: auto)
    #[arg(long, allow_negative_numbers = true)]
    pub(super) scale: Option<String>,
    /// Draw a sheet frame (Rust always draws one)
    #[arg(long)]
    frame: bool,
}

impl SheetOptions {
    pub(super) fn with_scale_text(scale: Option<String>) -> Self {
        Self {
            scale,
            ..Self::default()
        }
    }
    pub(super) fn with_scale(scale: Option<f64>) -> Self {
        Self {
            scale: scale.map(|s| s.to_string()),
            ..Self::default()
        }
    }

    pub(super) fn paths(&self) -> Vec<String> {
        let mut paths = Vec::new();
        for (key, value) in [
            ("title", &self.title),
            ("number", &self.number),
            ("material", &self.material),
            ("drawn_by", &self.drawn_by),
            ("revision", &self.revision),
            ("date", &self.date),
            ("company", &self.company),
            ("approved_by", &self.approved_by),
            ("tolerance", &self.tolerance),
        ] {
            if value.is_some() {
                paths.push(format!("sheet/title/{key}"));
            }
        }
        if self.scale.is_some() {
            paths.push("sheet/scale".into());
        }
        paths
    }

    pub(super) fn apply(&self, spec: &mut Spec) -> Result<(), Error> {
        for (key, value) in [
            ("title", &self.title),
            ("number", &self.number),
            ("material", &self.material),
            ("drawn_by", &self.drawn_by),
            ("revision", &self.revision),
            ("date", &self.date),
            ("company", &self.company),
            ("approved_by", &self.approved_by),
            ("tolerance", &self.tolerance),
        ] {
            if let Some(value) = value {
                spec.sheet.title.insert(key.into(), value.clone());
            }
        }
        if let Some(scale) = &self.scale {
            spec.sheet.scale = Some(parse_scale(scale)?);
        }
        // SheetSpec has no frame switch: its renderer always draws the ISO frame. Accepting
        // --frame therefore requests the existing behaviour, including its clearance.
        Ok(())
    }
}

/// Pictorial shortcuts shared by the bare form, render, review and plan.
#[derive(Args, Default)]
pub(super) struct PictorialOptions {
    #[command(flatten)]
    pub(super) overrides: super::overrides::Overrides,
    /// Printed length units (geometry remains in mm)
    #[arg(long, value_parser = ["mm", "in"])]
    units: Option<String>,
    /// Automatic paper family
    #[arg(long, value_parser = ["iso", "ansi", "arch"])]
    paper: Option<String>,
    /// Shade the isometric view
    #[arg(long)]
    shaded: bool,
    /// Add a view from below (shaded with --shaded, otherwise lines)
    #[arg(long)]
    below: bool,
    /// Base shaded colour as six hex digits, with optional # (requires --shaded)
    #[arg(
        long,
        visible_alias = "color",
        value_name = "HEX",
        allow_hyphen_values = true
    )]
    colour: Option<String>,
}

impl PictorialOptions {
    pub(super) fn with_overrides(overrides: super::overrides::Overrides) -> Self {
        Self {
            overrides,
            ..Default::default()
        }
    }

    pub(super) fn requested(&self) -> bool {
        self.overrides.requested()
            || self.units.is_some()
            || self.paper.is_some()
            || self.shaded
            || self.below
            || self.colour.is_some()
    }

    pub(super) fn paths(&self) -> Vec<String> {
        let mut paths = self.overrides.paths("");
        for (requested, path) in [
            (self.units.is_some(), "units"),
            (self.paper.is_some(), "sheet/paper"),
            (self.shaded, "views/iso/style"),
            (self.colour.is_some(), "views/iso/colour"),
            (self.below, "views/iso_below/omit"),
            (self.below, "views/iso_below/style"),
            (
                self.below && self.colour.is_some(),
                "views/iso_below/colour",
            ),
        ] {
            if requested {
                paths.push(path.into());
            }
        }
        paths
    }

    pub(super) fn apply(&self, spec: &mut Spec) -> Result<(), Error> {
        use crate::shaded::ViewStyle;
        if let Some(u) = &self.units {
            spec.units = if u == "in" {
                crate::units::Units::In
            } else {
                crate::units::Units::Mm
            };
        }
        if let Some(p) = &self.paper {
            spec.sheet.paper = Some(match p.as_str() {
                "ansi" => crate::units::Paper::Ansi,
                "arch" => crate::units::Paper::Arch,
                _ => crate::units::Paper::Iso,
            });
        }
        if self.colour.is_some() && !self.shaded {
            return Err(Error::invalid("--colour/--color requires --shaded"));
        }
        let colour = self.colour.as_deref().map(hex_colour).transpose()?;
        if self.shaded {
            let iso = spec.views.entry("iso".into()).or_default();
            iso.style = Some(ViewStyle::Shaded);
            if let Some(colour) = colour {
                iso.colour = Some(colour);
            }
        }
        if self.below {
            let below = spec.views.entry("iso_below".into()).or_default();
            below.omit = false;
            below.style = Some(if self.shaded {
                ViewStyle::Shaded
            } else {
                ViewStyle::Wireframe
            });
            if let Some(colour) = colour {
                below.colour = Some(colour);
            }
        }
        Ok(())
    }
}

fn hex_colour(value: &str) -> Result<[u8; 3], Error> {
    let hex = value.strip_prefix('#').unwrap_or(value);
    if hex.len() != 6 || !hex.bytes().all(|b| b.is_ascii_hexdigit()) {
        return Err(Error::invalid(format!(
            "--colour/--color expects six hexadecimal digits, e.g. '#b8c4cc' or b8c4cc; got {value:?}"
        )));
    }
    // The length and ASCII check above makes these byte boundaries and digits valid.
    Ok([0, 2, 4].map(|i| u8::from_str_radix(&hex[i..i + 2], 16).expect("checked hex pair")))
}

pub(super) fn parse_scale(value: &str) -> Result<String, Error> {
    let number = |v: &str| {
        v.parse::<f64>().map_err(|_| {
            Error::invalid(format!(
                "--scale {value:?} must be a positive number or ratio"
            ))
        })
    };
    let scale = if let Some((a, b)) = value.split_once(':') {
        let (a, b) = (number(a)?, number(b)?);
        if !a.is_finite() || !b.is_finite() || a <= 0. || b <= 0. {
            return Err(Error::invalid("--scale must be a finite positive ratio"));
        }
        a / b
    } else {
        number(value)?
    };
    scale_ratio(scale)
}

/// Avoid printing binary reciprocal noise or silently rounding authored scales.
fn scale_ratio(scale: f64) -> Result<String, Error> {
    if !scale.is_finite() || scale <= 0.0 {
        return Err(Error::invalid("--scale must be a finite positive number"));
    }
    let ratio = if scale >= 1.0 { scale } else { scale.recip() };
    let unclean = || {
        Error::invalid(
            "--scale must give a clean finite n:1 or 1:n ratio with at most six decimal places",
        )
    };
    if !ratio.is_finite() {
        return Err(unclean());
    }
    let text = format!("{ratio:.6}");
    let clean: f64 = text.parse().map_err(|_| unclean())?;
    // Permit floating-point representation noise, not a change in the requested scale.
    if (clean - ratio).abs() > ratio * f64::EPSILON * 8.0 {
        return Err(unclean());
    }
    let text = text.trim_end_matches('0').trim_end_matches('.');
    Ok(if scale >= 1.0 {
        format!("{text}:1")
    } else {
        format!("1:{text}")
    })
}

#[derive(Parser)]
#[command(name = "draftwright", version = super::version::version(), args_override_self = true,
    about = "Render a STEP file using the common Python CLI options",
    after_help = "Subcommand names take priority over file names. Use ./render for a file named render.\nFor more controls use draftwright render --help and --spec.")]
struct Bare {
    step: String,
    #[command(flatten)]
    sheet: SheetOptions,
    #[command(flatten)]
    pictorial: PictorialOptions,
    /// the Spec to use as the base for command-line overrides
    #[arg(long, value_name = "JSON")]
    spec: Option<String>,
    /// Output prefix; relative paths use the working directory
    #[arg(long, conflicts_with = "out_dir")]
    out: Option<String>,
    /// Output directory (default: beside the STEP input)
    #[arg(long)]
    out_dir: Option<String>,
}

pub(super) fn is_bare(args: &[std::ffi::OsString]) -> bool {
    let Some(first) = args.get(1) else {
        return false;
    };
    let first = first.to_string_lossy();
    let mut command = Cli::command();
    command.build(); // Clap adds its help subcommand during build.
    if command.get_subcommands().any(|c| c.get_name() == first) {
        return false;
    }
    // Top-level help and version still describe the subcommand interface. Other leading
    // options may precede the bare filename, as in Python's CLI.
    !matches!(first.as_ref(), "--help" | "-h" | "--version" | "-V")
}

pub(super) fn parse(args: Vec<std::ffi::OsString>) -> Result<Given, clap::Error> {
    let bare = Bare::try_parse_from(args).map_err(|mut error| {
        if let Some(clap::error::ContextValue::String(option)) =
            error.get(clap::error::ContextKind::InvalidArg)
            && option.starts_with('-')
        {
            let option = option.split('=').next().unwrap_or(option);
            let hint = match option {
                "--document-type" => "use render --spec with sheet.title.document_type",
                "--sheet" => "use render --spec with sheet.title.sheet",
                "--projection" => "use render --spec with sheet.projection",
                "--zones" => "use render --spec with sheet.zones",
                "--page" => "use render --spec with sheet.size (A4..A0)",
                "--pmi" => "use render --spec with pmi (first, strict or ignore)",
                "--format" | "-f" => "use render --formats svg,pdf,png,dxf",
                "--install-completion" | "--show-completion" => {
                    "use draftwright completions <shell>"
                }
                _ => "there is no Rust equivalent for this option",
            };
            error.insert(
                clap::error::ContextKind::Suggested,
                clap::error::ContextValue::StyledStrs(vec![
                    format!("Python option {option} is refused: {hint}").into(),
                ]),
            );
        }
        error
    })?;
    Ok(Given {
        positionals: vec![bare.step],
        output: bare.out,
        out_dir: bare.out_dir,
        sheet: bare.sheet,
        pictorial: bare.pictorial,
        spec: bare.spec,
        bare: true,
        ..Given::default()
    })
}

pub(super) fn output(step: &str, out: Option<&str>, out_dir: Option<&str>) -> PathBuf {
    let source = Path::new(step);
    let mut prefix = match (out, out_dir) {
        (Some(out), _) => PathBuf::from(out),
        (_, Some(dir)) => Path::new(dir).join(source.file_stem().unwrap_or_default()),
        _ => source.with_extension(""),
    };
    if prefix
        .extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| matches!(e, "svg" | "pdf" | "png" | "dxf"))
    {
        prefix.set_extension("");
    }
    prefix
}

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

    fn args(values: &[&str]) -> Vec<std::ffi::OsString> {
        std::iter::once("draftwright")
            .chain(values.iter().copied())
            .map(Into::into)
            .collect()
    }

    #[test]
    fn each_common_option_authors_the_same_spec_as_render() {
        for (option, key) in [
            ("--title", "title"),
            ("--number", "number"),
            ("--material", "material"),
            ("--drawn-by", "drawn_by"),
            ("--revision", "revision"),
            ("--date", "date"),
            ("--company", "company"),
            ("--approved-by", "approved_by"),
            ("--tolerance", "tolerance"),
        ] {
            let bare = Bare::try_parse_from(args(&["part.step", option, "value"])).unwrap();
            let render =
                Cli::try_parse_from(args(&["render", "part.step", "-o", "out", option, "value"]))
                    .unwrap();
            let super::super::Command::Render { sheet, .. } = render.command else {
                panic!("render")
            };
            let mut a = Spec::default();
            let mut b = Spec::default();
            bare.sheet.apply(&mut a).unwrap();
            sheet.apply(&mut b).unwrap();
            assert_eq!(a, b, "{option}");
            assert_eq!(a.sheet.title[key], "value", "{option}");
        }
        for (value, expected) in [
            ("5", "5:1"),
            ("0.5", "1:2"),
            ("0.8", "1:1.25"),
            ("2.5", "2.5:1"),
            ("0.3333333333333333", "1:3"),
            ("0.00001", "1:100000"),
        ] {
            let bare = Bare::try_parse_from(args(&["part.step", "--scale", value])).unwrap();
            let mut spec = Spec::default();
            bare.sheet.apply(&mut spec).unwrap();
            assert_eq!(spec.sheet.scale.as_deref(), Some(expected));
        }
        for value in ["0", "-1", "NaN", "inf"] {
            let bare = Bare::try_parse_from(args(&["part.step", "--scale", value])).unwrap();
            assert!(bare.sheet.apply(&mut Spec::default()).is_err());
        }
        let bare = Bare::try_parse_from(args(&["part.step", "--frame"])).unwrap();
        assert!(bare.sheet.frame);
        let mut spec = Spec::default();
        bare.sheet.apply(&mut spec).unwrap();
        assert_eq!(spec, Spec::default()); // The renderer always frames this Spec.
    }

    #[test]
    fn scales_that_need_long_or_infinite_ratios_are_refused() {
        for scale in [0.3, 0.14, 1.1234567, 1e-320] {
            let error = scale_ratio(scale).unwrap_err();
            assert!(error.message.contains("clean finite"), "{error:?}");
            assert!(error.message.contains("six decimal places"), "{error:?}");
        }
    }

    #[test]
    fn destinations_keep_python_prefix_rules() {
        for (values, expected) in [
            (vec!["dir/part.step"], "dir/part"),
            (vec!["dir/part.step", "--out", "drawing.pdf"], "drawing"),
            (vec!["dir/part.step", "--out-dir", "output"], "output/part"),
        ] {
            let bare = Bare::try_parse_from(args(&values)).unwrap();
            assert_eq!(
                output(&bare.step, bare.out.as_deref(), bare.out_dir.as_deref()),
                PathBuf::from(expected)
            );
        }
        assert!(
            Bare::try_parse_from(args(&["part.step", "--out", "a", "--out-dir", "b"])).is_err()
        );
    }

    #[test]
    fn subcommands_always_win_and_dot_slash_selects_a_path() {
        let mut built = Cli::command();
        built.build();
        assert!(
            built
                .get_subcommands()
                .any(|command| command.get_name() == "help")
        );
        assert!(!is_bare(&args(&["help"])));
        for command in built.get_subcommands() {
            assert!(!is_bare(&args(&[command.get_name()])));
            assert!(is_bare(&args(&[&format!("./{}", command.get_name())])));
        }
        assert!(is_bare(&args(&["part.step"])));
        assert!(is_bare(&args(&["--title", "Part", "part.step"])));
    }
}