1use crate::geometry::{Point, Polygon};
7use crate::material::Material;
8use crate::section::Section;
9use crate::section_properties::SectionProperties;
10use serde::{Deserialize, Serialize};
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct JsonSection {
15 pub designation: Option<String>,
16 pub outer_vertices: Vec<[f64; 2]>,
17 pub holes: Vec<Vec<[f64; 2]>>,
18 pub properties: Option<JsonSectionProperties>,
19 pub material: Option<JsonMaterial>,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct JsonSectionProperties {
25 pub area: f64,
26 pub centroid: [f64; 2],
27 pub ix: f64,
28 pub iy: f64,
29 pub ixy: f64,
30 pub principal: Option<JsonPrincipalProperties>,
31 pub gyration: Option<JsonGyrationProperties>,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct JsonPrincipalProperties {
37 pub i1: f64,
38 pub i2: f64,
39 pub angle: f64,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct JsonGyrationProperties {
45 pub rx: f64,
46 pub ry: f64,
47 pub polar: f64,
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct JsonMaterial {
53 pub name: String,
54 pub youngs_modulus: f64,
55 pub shear_modulus: f64,
56 pub poissons_ratio: f64,
57 pub density: f64,
58 pub thermal_expansion: f64,
59 pub yield_strength: f64,
60 pub ultimate_strength: f64,
61 pub color: Option<[u8; 3]>,
62}
63
64pub fn to_json(
66 section: &Section,
67 properties: Option<&SectionProperties>,
68 material: Option<&Material>,
69 designation: Option<&str>,
70 pretty: bool,
71) -> String {
72 let json_section = JsonSection {
73 designation: designation.map(String::from),
74 outer_vertices: section.outer.vertices.iter().map(|v| [v.x, v.y]).collect(),
75 holes: section
76 .holes
77 .iter()
78 .map(|h| h.vertices.iter().map(|v| [v.x, v.y]).collect())
79 .collect(),
80 properties: properties.map(|p| JsonSectionProperties {
81 area: p.area,
82 centroid: [p.centroid.x, p.centroid.y],
83 ix: p.ix,
84 iy: p.iy,
85 ixy: p.ixy,
86 principal: Some(JsonPrincipalProperties {
87 i1: p.principal_moments().0,
88 i2: p.principal_moments().1,
89 angle: p.principal_moments().2,
90 }),
91 gyration: Some(JsonGyrationProperties {
92 rx: p.radius_of_gyration().0,
93 ry: p.radius_of_gyration().1,
94 polar: p.radius_of_gyration().2,
95 }),
96 }),
97 material: material.map(|m| JsonMaterial {
98 name: m.name.to_string(),
99 youngs_modulus: m.youngs_modulus,
100 shear_modulus: m.shear_modulus,
101 poissons_ratio: m.poissons_ratio,
102 density: m.density,
103 thermal_expansion: m.thermal_expansion,
104 yield_strength: m.yield_strength,
105 ultimate_strength: m.ultimate_strength,
106 color: m.color.map(|c| [c.0, c.1, c.2]),
107 }),
108 };
109
110 if pretty {
111 serde_json::to_string_pretty(&json_section).unwrap()
112 } else {
113 serde_json::to_string(&json_section).unwrap()
114 }
115}
116
117pub fn from_json(json_str: &str) -> Result<JsonSection, serde_json::Error> {
119 serde_json::from_str(json_str)
120}
121
122impl From<JsonSection> for Section {
124 fn from(json: JsonSection) -> Self {
125 let outer = Polygon::new(
126 json.outer_vertices
127 .into_iter()
128 .map(|v| Point::new(v[0], v[1]))
129 .collect(),
130 );
131
132 let holes = json
133 .holes
134 .into_iter()
135 .map(|h| Polygon::new(h.into_iter().map(|v| Point::new(v[0], v[1])).collect()))
136 .collect();
137
138 Section::new(outer, holes)
139 }
140}
141
142impl JsonMaterial {
143 pub fn into_material(self) -> Material {
145 Material {
146 youngs_modulus: self.youngs_modulus,
147 shear_modulus: self.shear_modulus,
148 poissons_ratio: self.poissons_ratio,
149 density: self.density,
150 thermal_expansion: self.thermal_expansion,
151 yield_strength: self.yield_strength,
152 ultimate_strength: self.ultimate_strength,
153 name: Box::leak(self.name.into_boxed_str()), color: self.color.map(|c| (c[0], c[1], c[2])),
155 }
156 }
157}
158
159pub fn export_section_catalog(sections: &[(String, Section)], pretty: bool) -> String {
161 #[derive(Serialize)]
162 struct Catalog {
163 version: String,
164 sections: Vec<CatalogEntry>,
165 }
166
167 #[derive(Serialize)]
168 struct CatalogEntry {
169 name: String,
170 designation: Option<String>,
171 outer_vertices: Vec<[f64; 2]>,
172 holes: Vec<Vec<[f64; 2]>>,
173 }
174
175 let catalog = Catalog {
176 version: "1.0".to_string(),
177 sections: sections
178 .iter()
179 .map(|(name, section)| CatalogEntry {
180 name: name.clone(),
181 designation: None,
182 outer_vertices: section.outer.vertices.iter().map(|v| [v.x, v.y]).collect(),
183 holes: section
184 .holes
185 .iter()
186 .map(|h| h.vertices.iter().map(|v| [v.x, v.y]).collect())
187 .collect(),
188 })
189 .collect(),
190 };
191
192 if pretty {
193 serde_json::to_string_pretty(&catalog).unwrap()
194 } else {
195 serde_json::to_string(&catalog).unwrap()
196 }
197}
198
199#[cfg(test)]
200mod tests {
201 use super::*;
202 use crate::geometry::{Point, Polygon};
203 use crate::material::presets::STEEL_S355;
204 use crate::section::Section;
205 use crate::section_library::ParametricSection;
206 use crate::section_library::steel::ISection;
207
208 #[test]
209 fn json_export_import() {
210 let i = ISection::from_designation("IPE300").unwrap();
211 let section = i.build();
212
213 let json = to_json(§ion, None, Some(&STEEL_S355), Some("IPE300"), true);
214
215 assert!(json.contains("IPE300"));
216 assert!(json.contains("vertices"));
217 assert!(json.contains("youngs_modulus"));
218
219 let imported = from_json(&json).unwrap();
220 assert_eq!(imported.designation, Some("IPE300".to_string()));
221 assert_eq!(imported.outer_vertices.len(), section.outer.vertices.len());
222 }
223
224 #[test]
225 fn json_section_properties() {
226 let i = ISection::from_designation("IPE300").unwrap();
227 let section = i.build();
228 let props = SectionProperties::from_section(§ion);
229
230 let json = to_json(
231 §ion,
232 Some(&props),
233 Some(&STEEL_S355),
234 Some("IPE300"),
235 true,
236 );
237
238 assert!(json.contains("ix"));
239 assert!(json.contains("principal"));
240 assert!(json.contains("gyration"));
241
242 let imported = from_json(&json).unwrap();
243 assert!(imported.properties.is_some());
244 let p = imported.properties.unwrap();
245 assert!((p.area - props.area).abs() < 1e-6);
246 assert!((p.ix - props.ix).abs() < 1e-6);
247 }
248
249 #[test]
250 fn json_roundtrip_geometry() {
251 let outer = Polygon::new(vec![
252 Point::new(0.0, 0.0),
253 Point::new(1.0, 0.0),
254 Point::new(1.0, 1.0),
255 Point::new(0.0, 1.0),
256 ]);
257 let hole = Polygon::new(vec![
258 Point::new(0.3, 0.3),
259 Point::new(0.3, 0.7),
260 Point::new(0.7, 0.7),
261 Point::new(0.7, 0.3),
262 ]);
263 let section = Section::new(outer, vec![hole]);
264
265 let json = to_json(§ion, None, None, Some("TestSection"), false);
266 let imported = from_json(&json).unwrap();
267
268 assert_eq!(imported.outer_vertices.len(), 4);
269 assert_eq!(imported.holes.len(), 1);
270 assert_eq!(imported.holes[0].len(), 4);
271 }
272}