scadman 0.2.2

Code generator for OpenSCAD.
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
use ambassador::Delegate;
use derive_builder::Builder;
use derive_more::derive::From;

use crate::{
    common::{AffineMatrix2D, DimensionType as _, Point2D, Unit},
    internal::generate_sentence_repr,
    scad_display::{ambassador_impl_ScadDisplay, ScadDisplay},
    value_type::Angle,
};

use crate::common::D2;

macro_rules! __impl_apply_2d {
    ($mod_ty:ident) => {
        __impl_apply_to_modifier!(
            apply_to,
            $mod_ty,
            $crate::scad_2d::ScadModifierBody2D,
            $crate::common::ScadObjectGeneric<$crate::common::D2>, // Typed output object
            $crate::scad_2d::ScadModifier2D,
            $crate::scad_2d::ScadObject2D,
            $crate::common::ScadObjectImpl::Object2D,
            $crate::common::D2, // output_marker
            $crate::common::D2  // child_marker
        );
        __impl_panicking_modifier_methods!($mod_ty, D2);
    };
}

macro_rules! __impl_apply_3d {
    ($mod_ty:ident) => {
        __impl_apply_to_modifier!(
            apply_to,
            $mod_ty,
            $crate::scad_2d::ScadModifierBody2D, // Modifier body will be ScadModifierBody2D::Projection
            $crate::common::ScadObjectGeneric<$crate::common::D2>, // Typed output object
            $crate::scad_2d::ScadModifier2D,     // Concrete ScadModifier type
            $crate::scad_2d::ScadObject2D,       // Concrete ScadObject enum type
            $crate::common::ScadObjectImpl::Object2D, // ScadObjectImpl variant
            $crate::common::D2,                  // Output dimension marker (Projection outputs 2D)
            $crate::common::D3 // Child dimension marker (Projection takes 3D child)
        );
        __impl_panicking_modifier_methods!($mod_ty, D2);
    };
}

/// Translate modifier `translate()` in SCAD.
/// This Rust type is regarded as 2D object and only applys to 2D objects.
#[derive(Builder, Debug, Clone, Copy)]
pub struct Translate2D {
    /// Translation vector.
    /// `v` option in SCAD.
    #[builder(setter(into))]
    pub v: Point2D,
}

__impl_builder_modifier!(Translate2D);
__impl_modifier_chaining!(Translate2D);
__impl_apply_2d!(Translate2D);

impl ScadDisplay for Translate2D {
    fn repr_scad(&self) -> String {
        generate_sentence_repr("translate", __generate_scad_options!(("", self.v);))
    }
}

/// Rotate modifier `rotate()` in SCAD.
/// This Rust type is regarded as 2D object and only applys to 2D objects.
#[derive(Builder, Debug, Clone, Copy)]
pub struct Rotate2D {
    /// Rotation angle.
    /// `a` option in SCAD.
    ///
    /// See also [`Angle`].
    #[builder(setter(custom))]
    pub a: Angle,
}

__impl_builder_modifier!(Rotate2D);
__impl_modifier_chaining!(Rotate2D);
__impl_apply_2d!(Rotate2D);

impl Rotate2DBuilder {
    /// Set rotation angle in degrees.
    ///
    /// # Arguments
    ///
    /// + `value` - The rotation angle in degrees.
    pub const fn deg(&mut self, value: Unit) -> &mut Self {
        let new = self;
        new.a = Some(Angle::Deg(value));
        new
    }
    /// Set rotation angle in radians.
    ///
    /// # Arguments
    ///
    /// + `value` - The rotation angle in radians.
    pub const fn rad(&mut self, value: Unit) -> &mut Self {
        let new = self;
        new.a = Some(Angle::Rad(value));
        new
    }
}

impl ScadDisplay for Rotate2D {
    fn repr_scad(&self) -> String {
        generate_sentence_repr("rotate", __generate_scad_options!(("a", self.a);))
    }
}

/// Scale modifier `scale()` in SCAD.
/// This Rust type is regarded as 2D object and only applys to 2D objects.
#[derive(Builder, Debug, Clone, Copy)]
pub struct Scale2D {
    /// Scaling vector.
    /// `v` option in SCAD.
    #[builder(setter(into))]
    pub v: Point2D,
}

__impl_builder_modifier!(Scale2D);
__impl_modifier_chaining!(Scale2D);
__impl_apply_2d!(Scale2D);

impl ScadDisplay for Scale2D {
    fn repr_scad(&self) -> String {
        generate_sentence_repr("scale", __generate_scad_options!(("", self.v);))
    }
}

/// `auto` option in 2D resize modifier.
#[derive(Copy, Clone, Debug, PartialEq, Eq, From, Delegate)]
#[delegate(ScadDisplay)]
pub enum ResizeAuto2D {
    /// Same value for all dimensions.
    B(bool),
    /// Values for each dimension.
    V([bool; 2]),
}

/// Resize modifier `resize()` in SCAD.
/// This Rust type is regarded as 2D object and only applys to 2D objects.
#[derive(Builder, Debug, Clone, Copy)]
pub struct Resize2D {
    /// New size.
    ///
    /// `0` means no change if the corresponding dimension of `auto` is `false`,
    /// or auto value if `true`.
    #[builder(setter(into))]
    pub size: Point2D,
    /// `auto` option in SCAD.
    ///
    /// See also [`ResizeAuto`].
    #[builder(setter(into, strip_option), default)]
    pub auto: Option<ResizeAuto2D>,
}

__impl_builder_modifier!(Resize2D);
__impl_modifier_chaining!(Resize2D);
__impl_apply_2d!(Resize2D);

impl ScadDisplay for Resize2D {
    fn repr_scad(&self) -> String {
        generate_sentence_repr(
            "resize",
            __generate_scad_options!(
                ("", self.size); opt: (("auto", self.auto);)
            ),
        )
    }
}

/// Mirror modifier `mirror()` in SCAD.
/// This Rust type is regarded as 2D object and only applys to 2D objects.
#[derive(Builder, Debug, Clone, Copy)]
pub struct Mirror2D {
    /// Normal vector of the mirror plane.
    #[builder(setter(into))]
    pub v: Point2D,
}

__impl_builder_modifier!(Mirror2D);
__impl_modifier_chaining!(Mirror2D);
__impl_apply_2d!(Mirror2D);

impl ScadDisplay for Mirror2D {
    fn repr_scad(&self) -> String {
        generate_sentence_repr("mirror", __generate_scad_options!(("", self.v);))
    }
}

/// Affine tranformation modifier `multmatrix()` in SCAD.
/// This Rust type is regarded as 2D object and only applys to 2D objects.
#[derive(Builder, Debug, Clone, Copy)]
pub struct MultMatrix2D {
    /// Affine transformation matrix for 2D vector.
    #[builder(setter(into))]
    pub m: AffineMatrix2D,
}

__impl_builder_modifier!(MultMatrix2D);
__impl_modifier_chaining!(MultMatrix2D);
__impl_apply_2d!(MultMatrix2D);

impl ScadDisplay for MultMatrix2D {
    fn repr_scad(&self) -> String {
        generate_sentence_repr("multmatrix", __generate_scad_options!(("m", self.m);))
    }
}

/// Size of offset modifier for SCAD
#[derive(Copy, Clone, Debug, PartialEq, Delegate)]
#[delegate(ScadDisplay)]
pub enum OffsetSize {
    /// Radius of the radial offset.
    R(Unit),
    /// Delta of the delta offset.
    Delta(Unit),
}

impl OffsetSize {
    /// Returns the name of the key in SCAD code
    ///
    /// # Returns
    ///
    /// The name of the key in SCAD code
    pub const fn name(self) -> &'static str {
        match self {
            Self::R(_) => "r",
            Self::Delta(_) => "delta",
        }
    }
}

/// Offset modifier `offset()` in SCAD.
#[derive(Builder, Debug, Clone, Copy)]
pub struct Offset {
    /// Size of the offset.
    /// `r` or `delta` option in SCAD.
    ///
    /// See also [`OffsetSize`].
    #[builder(setter(custom))]
    pub size: OffsetSize,
    /// Flag to determine the shape should be chamfered or not.
    /// `chamfer` option in SCAD.
    ///
    /// This parameter has no effect on radial offsets.
    #[builder(setter(into, strip_option), default)]
    pub chamfer: Option<bool>,
    /// `$fa` option in SCAD.
    #[builder(setter(into, strip_option), default)]
    pub fa: Option<Unit>,
    /// `$fn` option in SCAD.
    #[builder(setter(into, strip_option), default)]
    pub r#fn: Option<u64>,
    /// `$fs` option in SCAD.
    #[builder(setter(into, strip_option), default)]
    pub fs: Option<Unit>,
}

__impl_builder_modifier!(Offset);
__impl_modifier_chaining!(Offset);
__impl_apply_2d!(Offset);

impl OffsetBuilder {
    /// Set `r` option in SCAD.
    ///
    /// # Arguments
    ///
    /// + `value` - `r` option in SCAD. This is the radial offset.
    pub const fn r(&mut self, value: Unit) -> &mut Self {
        let new = self;
        new.size = Some(OffsetSize::R(value));
        new
    }
    /// Set `delta` option in SCAD.
    ///
    /// # Arguments
    ///
    /// + `value` - `delta` option in SCAD. This is the delta offset.
    pub const fn delta(&mut self, value: Unit) -> &mut Self {
        let new = self;
        new.size = Some(OffsetSize::Delta(value));
        new
    }
}

impl ScadDisplay for Offset {
    fn repr_scad(&self) -> String {
        generate_sentence_repr(
            "offset",
            __generate_scad_options!(
                (self.size.name(), self.size);
                opt: (
                    ("chamfer", self.chamfer);
                    ("$fa", self.fa);
                    ("$fn", self.r#fn);
                    ("$fs", self.fs);
                )
            ),
        )
    }
}

/// Projection modifier `projection()` in SCAD.
/// This Rust type is regarded as 2D object and only applys to 3D objects.
#[derive(Builder, Debug, Clone, Copy)]
pub struct Projection {
    /// Flag to determine the shape should be cut at z = 0 or not.
    ///
    /// If `cut` is `true`, the shape is cut at z = 0.
    /// If `cut` is `false`, the shape is as projected.
    #[builder(setter(into, strip_option), default)]
    pub cut: Option<bool>,
}

__impl_builder_modifier!(Projection);
__impl_modifier_chaining!(Projection);
__impl_apply_3d!(Projection);

impl ScadDisplay for Projection {
    fn repr_scad(&self) -> String {
        generate_sentence_repr(
            "projection",
            __generate_scad_options!(opt: (("cut", self.cut);)),
        )
    }
}

#[cfg(test)]
mod tests {

    use std::f64::consts::PI;

    use super::*;

    #[test]
    fn test_translate2d() {
        assert_eq!(
            Translate2DBuilder::default()
                .v([8., -4.])
                .build()
                .unwrap()
                .repr_scad(),
            "translate([8, -4])"
        );
    }

    #[test]
    fn test_rotate2d() {
        assert_eq!(
            Rotate2DBuilder::default()
                .deg(45.)
                .build()
                .unwrap()
                .repr_scad(),
            "rotate(a = 45)"
        );
        assert_eq!(
            Rotate2DBuilder::default()
                .rad(PI / 4.)
                .build()
                .unwrap()
                .repr_scad(),
            "rotate(a = 45)"
        );
    }

    #[test]
    fn test_mirror2d() {
        assert_eq!(
            Mirror2DBuilder::default()
                .v([1., -1.])
                .build()
                .unwrap()
                .repr_scad(),
            "mirror([1, -1])"
        );
    }

    #[test]
    fn test_scale2d() {
        assert_eq!(
            Scale2DBuilder::default()
                .v([3., 2.])
                .build()
                .unwrap()
                .repr_scad(),
            "scale([3, 2])"
        );
    }

    #[test]
    fn test_resize2d() {
        let mut r1 = Resize2DBuilder::default();
        _ = r1.size([3., 2.]);
        assert_eq!(r1.clone().build().unwrap().repr_scad(), "resize([3, 2])");
        assert_eq!(
            r1.clone().auto(true).build().unwrap().repr_scad(),
            "resize([3, 2], auto = true)"
        );
        assert_eq!(
            r1.auto([true, false]).build().unwrap().repr_scad(),
            "resize([3, 2], auto = [true, false])"
        );
    }

    #[test]
    fn test_multimatrix2d() {
        let m = AffineMatrix2D::new(1., 2., 3., 4., 5., 6.);
        assert_eq!(
            MultMatrix2DBuilder::default()
                .m(m)
                .build()
                .unwrap()
                .repr_scad(),
            "multmatrix(m = [[1, 2, 0, 3], [4, 5, 0, 6], [0, 0, 1, 0]])"
        );
    }

    #[test]
    fn test_offset() {
        assert_eq!(
            OffsetBuilder::default().r(1.).build().unwrap().repr_scad(),
            "offset(r = 1)"
        );
        assert_eq!(
            OffsetBuilder::default()
                .delta(2.)
                .build()
                .unwrap()
                .repr_scad(),
            "offset(delta = 2)"
        );
        assert_eq!(
            OffsetBuilder::default()
                .r(1.)
                .chamfer(true)
                .fs(10)
                .build()
                .unwrap()
                .repr_scad(),
            "offset(r = 1, chamfer = true, $fs = 10)"
        );
    }

    #[test]
    fn test_projection() {
        assert_eq!(
            ProjectionBuilder::default().build().unwrap().repr_scad(),
            "projection()"
        );
        assert_eq!(
            ProjectionBuilder::default()
                .cut(true)
                .build()
                .unwrap()
                .repr_scad(),
            "projection(cut = true)"
        );
    }
}