kittycad_modeling_cmds/format/
stl.rs

1use parse_display::{Display, FromStr};
2use schemars::JsonSchema;
3use serde::{Deserialize, Serialize};
4
5use crate::{coord, format::Selection, units::UnitLength};
6
7/// Import models in STL format.
8pub mod import {
9    use super::*;
10
11    /// Options for importing STL.
12    #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
13    #[display("coords: {coords}, units: {units}")]
14    #[serde(rename = "StlImportOptions")]
15    #[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
16    #[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
17    #[cfg_attr(
18        feature = "python",
19        pyo3_stub_gen::derive::gen_stub_pyclass,
20        pyo3::pyclass(name = "StlImportOptions")
21    )]
22    pub struct Options {
23        /// Co-ordinate system of input data.
24        ///
25        /// Defaults to the [KittyCAD co-ordinate system].
26        ///
27        /// [KittyCAD co-ordinate system]: ../coord/constant.KITTYCAD.html
28        pub coords: coord::System,
29        /// The units of the input data.
30        ///
31        /// This is very important for correct scaling and when calculating physics properties like
32        /// mass, etc.
33        ///
34        /// Defaults to millimeters.
35        pub units: UnitLength,
36    }
37
38    impl Default for Options {
39        fn default() -> Self {
40            Self {
41                coords: *coord::KITTYCAD,
42                units: UnitLength::Millimeters,
43            }
44        }
45    }
46}
47
48/// Export models in STL format.
49pub mod export {
50
51    use super::*;
52
53    /// Options for exporting STL.
54    #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
55    #[display("coords: {coords}, selection: {selection}, storage: {storage}, units: {units}")]
56    #[serde(rename = "StlExportOptions")]
57    #[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
58    #[cfg_attr(
59        feature = "python",
60        pyo3_stub_gen::derive::gen_stub_pyclass,
61        pyo3::pyclass(name = "StlExportOptions")
62    )]
63    #[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
64    pub struct Options {
65        /// Co-ordinate system of output data.
66        ///
67        /// Defaults to the [KittyCAD co-ordinate system].
68        ///
69        /// [KittyCAD co-ordinate system]: ../coord/constant.KITTYCAD.html
70        pub coords: coord::System,
71
72        /// Export selection.
73        pub selection: Selection,
74
75        /// Export storage.
76        pub storage: Storage,
77
78        /// Export length unit.
79        ///
80        /// Defaults to millimeters.
81        pub units: UnitLength,
82    }
83
84    #[cfg(feature = "python")]
85    #[pyo3_stub_gen::derive::gen_stub_pymethods]
86    #[pyo3::pymethods]
87    impl Options {
88        #[new]
89        /// Set the options to their defaults.
90        pub fn new() -> Self {
91            Default::default()
92        }
93    }
94
95    impl Default for Options {
96        fn default() -> Self {
97            Self {
98                coords: *coord::KITTYCAD,
99                selection: Default::default(),
100                storage: Default::default(),
101                units: UnitLength::Millimeters,
102            }
103        }
104    }
105
106    /// Export storage.
107    #[derive(
108        Clone, Copy, Debug, Default, Deserialize, Display, Eq, FromStr, Hash, JsonSchema, PartialEq, Serialize,
109    )]
110    #[display(style = "snake_case")]
111    #[serde(rename = "StlStorage", rename_all = "snake_case")]
112    #[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
113    #[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
114    #[cfg_attr(
115        feature = "python",
116        pyo3_stub_gen::derive::gen_stub_pyclass_enum,
117        pyo3::pyclass(name = "StlStorage")
118    )]
119    pub enum Storage {
120        /// Plaintext encoding.
121        Ascii,
122
123        /// Binary STL encoding.
124        ///
125        /// This is the default setting.
126        #[default]
127        Binary,
128    }
129}