kittycad-modeling-cmds 0.2.199

Commands in the KittyCAD Modeling API
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
use bon::Builder;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::shared::{FileExportFormat, FileExportFormat2d, FileImportFormat};

/// AutoCAD drawing interchange format.
pub mod dxf;
/// Autodesk Filmbox (FBX) format.
pub mod fbx;
/// glTF 2.0.
/// We refer to this as glTF since that is how our customers refer to it, although by default
/// it will be in binary format and thus technically (glb).
/// If you prefer ASCII output, you can set that option for the export.
pub mod gltf;
/// Wavefront OBJ format.
pub mod obj;
/// The PLY Polygon File Format.
pub mod ply;
/// ISO 10303-21 (STEP) format.
pub mod step;
/// **ST**ereo**L**ithography format.
pub mod stl;

/// Output 2D format specifier.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
pub enum OutputFormat2d {
    /// AutoCAD drawing interchange format.
    Dxf(dxf::export::Options),
}

/// Alias for backward compatibility.
#[deprecated(since = "0.2.96", note = "use `OutputFormat3d` instead")]
pub type OutputFormat = OutputFormat3d;

/// Output 3D format specifier.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
pub enum OutputFormat3d {
    /// Autodesk Filmbox (FBX) format.
    Fbx(fbx::export::Options),
    /// glTF 2.0.
    /// We refer to this as glTF since that is how our customers refer to it, although by default
    /// it will be in binary format and thus technically (glb).
    /// If you prefer ASCII output, you can set that option for the export.
    Gltf(gltf::export::Options),
    /// Wavefront OBJ format.
    Obj(obj::export::Options),
    /// The PLY Polygon File Format.
    Ply(ply::export::Options),
    /// ISO 10303-21 (STEP) format.
    Step(step::export::Options),
    /// **ST**ereo**L**ithography format.
    Stl(stl::export::Options),
}

/// Alias for backward compatibility.
#[deprecated(since = "0.2.96", note = "use `InputFormat3d` instead")]
pub type InputFormat = InputFormat3d;

/// Input format specifier.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass,
    pyo3_stub_gen::derive::gen_stub_pyclass_complex_enum
)]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
pub enum InputFormat3d {
    /// ACIS part format.
    Acis(acis::import::Options),
    /// CATIA part format.
    Catia(catia::import::Options),
    /// PTC Creo part format.
    Creo(creo::import::Options),
    /// Autodesk Filmbox (FBX) format.
    Fbx(fbx::import::Options),
    /// Binary glTF 2.0.
    /// We refer to this as glTF since that is how our customers refer to it,
    /// but this can also import binary glTF (glb).
    Gltf(gltf::import::Options),
    /// Autodesk Inventor part format.
    Inventor(inventor::import::Options),
    /// Siemens NX part format.
    Nx(nx::import::Options),
    /// Wavefront OBJ format.
    Obj(obj::import::Options),
    /// Parasolid part format.
    Parasolid(parasolid::import::Options),
    /// The PLY Polygon File Format.
    Ply(ply::import::Options),
    /// SolidWorks part (SLDPRT) format.
    Sldprt(sldprt::import::Options),
    /// ISO 10303-21 (STEP) format.
    Step(step::import::Options),
    /// **ST**ereo**L**ithography format.
    Stl(stl::import::Options),
}

impl InputFormat3d {
    /// Get the name of this format.
    pub fn name(&self) -> &'static str {
        match self {
            InputFormat3d::Acis(_) => "acis",
            InputFormat3d::Catia(_) => "catia",
            InputFormat3d::Creo(_) => "creo",
            InputFormat3d::Fbx(_) => "fbx",
            InputFormat3d::Gltf(_) => "gltf",
            InputFormat3d::Inventor(_) => "inventor",
            InputFormat3d::Nx(_) => "nx",
            InputFormat3d::Parasolid(_) => "parasolid",
            InputFormat3d::Obj(_) => "obj",
            InputFormat3d::Ply(_) => "ply",
            InputFormat3d::Sldprt(_) => "sldprt",
            InputFormat3d::Step(_) => "step",
            InputFormat3d::Stl(_) => "stl",
        }
    }
}

/// Data item selection.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, JsonSchema, Deserialize, Serialize)]
#[serde(rename_all = "snake_case", tag = "type")]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
pub enum Selection {
    /// Visit the default scene.
    #[default]
    DefaultScene,

    /// Visit the indexed scene.
    SceneByIndex {
        /// The index.
        index: usize,
    },

    /// Visit the first scene with the given name.
    SceneByName {
        /// The name.
        name: String,
    },

    /// Visit the indexed mesh.
    MeshByIndex {
        /// The index.
        index: usize,
    },

    /// Visit the first mesh with the given name.
    MeshByName {
        /// The name.
        name: String,
    },
}

/// Represents an in-memory file with an associated potentially foreign file path.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Builder)]
#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
pub struct VirtualFile {
    /// Original file path.
    pub path: std::path::PathBuf,
    /// File payload.
    pub data: Vec<u8>,
}

impl VirtualFile {
    /// Returns true if the file name has the given extension.
    pub fn has_extension(&self, required_extension: &str) -> bool {
        self.path
            .extension()
            .and_then(std::ffi::OsStr::to_str)
            .map(|extension| extension.eq_ignore_ascii_case(required_extension))
            .unwrap_or(false)
    }

    fn read_fs_impl(path: std::path::PathBuf) -> std::io::Result<Self> {
        let data = std::fs::read(&path)?;
        Ok(Self { path, data })
    }

    /// Read from file system.
    pub fn read_fs<P>(path: P) -> std::io::Result<Self>
    where
        P: Into<std::path::PathBuf>,
    {
        Self::read_fs_impl(path.into())
    }
}

impl From<OutputFormat3d> for FileExportFormat {
    fn from(output_format: OutputFormat3d) -> Self {
        match output_format {
            OutputFormat3d::Fbx(_) => Self::Fbx,
            OutputFormat3d::Gltf(_) => Self::Gltf,
            OutputFormat3d::Obj(_) => Self::Obj,
            OutputFormat3d::Ply(_) => Self::Ply,
            OutputFormat3d::Step(_) => Self::Step,
            OutputFormat3d::Stl(_) => Self::Stl,
        }
    }
}

impl From<OutputFormat2d> for FileExportFormat2d {
    fn from(output_format: OutputFormat2d) -> Self {
        match output_format {
            OutputFormat2d::Dxf(_) => Self::Dxf,
        }
    }
}

impl From<FileExportFormat2d> for OutputFormat2d {
    fn from(export_format: FileExportFormat2d) -> Self {
        match export_format {
            FileExportFormat2d::Dxf => OutputFormat2d::Dxf(Default::default()),
        }
    }
}

impl From<FileExportFormat> for OutputFormat3d {
    fn from(export_format: FileExportFormat) -> Self {
        match export_format {
            FileExportFormat::Fbx => OutputFormat3d::Fbx(Default::default()),
            FileExportFormat::Glb => OutputFormat3d::Gltf(gltf::export::Options {
                storage: gltf::export::Storage::Binary,
                ..Default::default()
            }),
            FileExportFormat::Gltf => OutputFormat3d::Gltf(gltf::export::Options {
                storage: gltf::export::Storage::Embedded,
                presentation: gltf::export::Presentation::Pretty,
            }),
            FileExportFormat::Obj => OutputFormat3d::Obj(Default::default()),
            FileExportFormat::Ply => OutputFormat3d::Ply(Default::default()),
            FileExportFormat::Step => OutputFormat3d::Step(Default::default()),
            FileExportFormat::Stl => OutputFormat3d::Stl(stl::export::Options {
                storage: stl::export::Storage::Ascii,
                ..Default::default()
            }),
        }
    }
}

impl From<InputFormat3d> for FileImportFormat {
    fn from(input_format: InputFormat3d) -> Self {
        match input_format {
            InputFormat3d::Acis(_) => Self::Acis,
            InputFormat3d::Catia(_) => Self::Catia,
            InputFormat3d::Creo(_) => Self::Creo,
            InputFormat3d::Fbx(_) => Self::Fbx,
            InputFormat3d::Gltf(_) => Self::Gltf,
            InputFormat3d::Inventor(_) => Self::Inventor,
            InputFormat3d::Nx(_) => Self::Nx,
            InputFormat3d::Obj(_) => Self::Obj,
            InputFormat3d::Parasolid(_) => Self::Parasolid,
            InputFormat3d::Ply(_) => Self::Ply,
            InputFormat3d::Sldprt(_) => Self::Sldprt,
            InputFormat3d::Step(_) => Self::Step,
            InputFormat3d::Stl(_) => Self::Stl,
        }
    }
}

impl From<FileImportFormat> for InputFormat3d {
    fn from(import_format: FileImportFormat) -> Self {
        match import_format {
            FileImportFormat::Acis => InputFormat3d::Acis(Default::default()),
            FileImportFormat::Catia => InputFormat3d::Catia(Default::default()),
            FileImportFormat::Creo => InputFormat3d::Creo(Default::default()),
            FileImportFormat::Fbx => InputFormat3d::Fbx(Default::default()),
            FileImportFormat::Gltf => InputFormat3d::Gltf(Default::default()),
            FileImportFormat::Inventor => InputFormat3d::Inventor(Default::default()),
            FileImportFormat::Nx => InputFormat3d::Nx(Default::default()),
            FileImportFormat::Obj => InputFormat3d::Obj(Default::default()),
            FileImportFormat::Parasolid => InputFormat3d::Parasolid(Default::default()),
            FileImportFormat::Ply => InputFormat3d::Ply(Default::default()),
            FileImportFormat::Sldprt => InputFormat3d::Sldprt(Default::default()),
            FileImportFormat::Step => InputFormat3d::Step(Default::default()),
            FileImportFormat::Stl => InputFormat3d::Stl(Default::default()),
        }
    }
}

/// Options for a 3D export.
pub struct OutputFormat3dOptions {
    src_unit: crate::units::UnitLength,
}

impl OutputFormat3dOptions {
    /// Create the options, setting all optional fields to their defaults.
    pub fn new(src_unit: crate::units::UnitLength) -> Self {
        Self { src_unit }
    }
}

impl OutputFormat3d {
    /// Create the output format, setting the options as given.
    pub fn new(format: &FileExportFormat, options: OutputFormat3dOptions) -> Self {
        let OutputFormat3dOptions { src_unit } = options;
        // Zoo co-ordinate system.
        //
        // * Forward: -Y
        // * Up: +Z
        // * Handedness: Right
        let coords = crate::coord::System {
            forward: crate::coord::AxisDirectionPair {
                axis: crate::coord::Axis::Y,
                direction: crate::coord::Direction::Negative,
            },
            up: crate::coord::AxisDirectionPair {
                axis: crate::coord::Axis::Z,
                direction: crate::coord::Direction::Positive,
            },
        };

        match format {
            FileExportFormat::Fbx => Self::Fbx(fbx::export::Options {
                storage: fbx::export::Storage::Binary,
                created: None,
            }),
            FileExportFormat::Glb => Self::Gltf(gltf::export::Options {
                storage: gltf::export::Storage::Binary,
                presentation: gltf::export::Presentation::Compact,
            }),
            FileExportFormat::Gltf => Self::Gltf(gltf::export::Options {
                storage: gltf::export::Storage::Embedded,
                presentation: gltf::export::Presentation::Pretty,
            }),
            FileExportFormat::Obj => Self::Obj(obj::export::Options {
                coords,
                units: src_unit,
            }),
            FileExportFormat::Ply => Self::Ply(ply::export::Options {
                storage: ply::export::Storage::Ascii,
                coords,
                selection: Selection::DefaultScene,
                units: src_unit,
            }),
            FileExportFormat::Step => Self::Step(step::export::Options {
                coords,
                created: None,
                units: src_unit,
                presentation: step::export::Presentation::Pretty,
            }),
            FileExportFormat::Stl => Self::Stl(stl::export::Options {
                storage: stl::export::Storage::Ascii,
                coords,
                units: src_unit,
                selection: Selection::DefaultScene,
            }),
        }
    }
}

macro_rules! proprietary_brep_formats {
    {
        $(
            (
                $mod_name:ident,
                $spec_name:literal,
                $format_description:literal,
                $coordinate_system:expr
            )
        )*
    } => {
        $(
            #[doc = $format_description]
            pub mod $mod_name {
                /// Import functionality
                pub mod import {
                    use bon::Builder;
                    use schemars::JsonSchema;
                    use serde::{Deserialize, Serialize};
                    use crate::coord;

                    #[doc = std::concat!("Options for importing ", $format_description, ".")]
                    #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Builder)]
                    #[serde(default, rename = $spec_name)]
                    #[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
                    #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
                    #[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
                    #[cfg_attr(
                        feature = "python",
                        pyo3_stub_gen::derive::gen_stub_pyclass,
                        pyo3::pyclass(name = $spec_name)
                    )]
                    pub struct Options {
                        /// Co-ordinate system of input data.
                        #[builder(default = *$coordinate_system)]
                        pub coords: coord::System,

                        /// Splits all closed faces into two open faces.
                        ///
                        /// Defaults to `false` but is implicitly `true` when importing into the engine.
                        #[builder(default)]
                        pub split_closed_faces: bool,
                    }

                    #[cfg(feature = "python")]
                    #[pyo3_stub_gen::derive::gen_stub_pymethods]
                    #[pyo3::pymethods]
                    impl Options {
                        #[new]
                        /// Set the options to their defaults.
                        pub fn new() -> Self {
                            Default::default()
                        }
                    }

                    impl Default for Options {
                        fn default() -> Self {
                            Self {
                                coords: *$coordinate_system,
                                split_closed_faces: false,
                            }
                        }
                    }
                }
            }
        )*
    };
}

proprietary_brep_formats! {
    (acis, "AcisImportOptions", "ACIS part format", coord::KITTYCAD)
    (catia, "CatiaImportOptions", "CATIA part format", coord::KITTYCAD)
    (creo, "CreoImportOptions", "PTC Creo part format", coord::OPENGL)
    (inventor, "InventorImportOptions", "Autodesk Inventor part format", coord::KITTYCAD)
    (nx, "NxImportOptions", "Siemens NX part format", coord::KITTYCAD)
    (parasolid, "ParasolidImportOptions", "Parasolid part format", coord::KITTYCAD)
    (sldprt, "SldprtImportOptions", "SolidWorks part format", coord::OPENGL)
}