Skip to main content

ifc_lite_wasm/api/
export_data.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! WASM API: tabular / semantic data exporters — CSV, JSON, JSON-LD.
6
7use super::IfcAPI;
8use ifc_lite_export::{CsvMode, CsvOptions, Ifc5Options, JsonLdOptions, JsonOptions};
9use wasm_bindgen::prelude::*;
10
11#[wasm_bindgen]
12impl IfcAPI {
13    /// Export tabular **CSV**. `mode` ∈ {`"entities"`, `"properties"`, `"quantities"`,
14    /// `"spatial"`}. `delimiter` defaults to `,` when empty; `include_properties` adds
15    /// flattened `Pset_Prop` columns to the entities view.
16    #[wasm_bindgen(js_name = exportCsv)]
17    pub fn export_csv(
18        &self,
19        content: &[u8],
20        mode: String,
21        delimiter: String,
22        include_properties: bool,
23    ) -> Vec<u8> {
24        let mode = match mode.as_str() {
25            "properties" => CsvMode::Properties,
26            "quantities" => CsvMode::Quantities,
27            "spatial" => CsvMode::SpatialHierarchy,
28            _ => CsvMode::Entities,
29        };
30        let opts = CsvOptions {
31            delimiter: if delimiter.is_empty() { ",".to_string() } else { delimiter },
32            include_properties,
33        };
34        ifc_lite_export::export_csv(content, mode, &opts).into_bytes()
35    }
36
37    /// Export structured **JSON** (array of entity objects with typed property values).
38    #[wasm_bindgen(js_name = exportJson)]
39    pub fn export_json(
40        &self,
41        content: &[u8],
42        pretty: bool,
43        include_properties: bool,
44        include_quantities: bool,
45    ) -> Vec<u8> {
46        let opts = JsonOptions { pretty, include_properties, include_quantities };
47        ifc_lite_export::export_json(content, &opts).into_bytes()
48    }
49
50    /// Export **JSON-LD** (`@graph` of `ifc:` nodes). Empty `context` ⇒ buildingSMART
51    /// IFC4 OWL default. `included` is an express-id isolation filter mirroring the
52    /// OBJ/glTF/STEP exporters (empty ⇒ all entities).
53    #[wasm_bindgen(js_name = exportJsonld)]
54    pub fn export_jsonld(
55        &self,
56        content: &[u8],
57        context: String,
58        include_properties: bool,
59        include_quantities: bool,
60        pretty: bool,
61        included: &[u32],
62    ) -> Vec<u8> {
63        let mut opts = JsonLdOptions {
64            include_properties,
65            include_quantities,
66            pretty,
67            included: included.to_vec(),
68            ..Default::default()
69        };
70        if !context.is_empty() {
71            opts.context = context;
72        }
73        ifc_lite_export::export_jsonld(content, &opts).into_bytes()
74    }
75
76    /// Export **IFC5 / IFCX** (the USD-style node graph). `only_known_properties` keeps
77    /// only properties with an official IFC5 schema.
78    #[wasm_bindgen(js_name = exportIfcx)]
79    pub fn export_ifcx(
80        &self,
81        content: &[u8],
82        only_known_properties: bool,
83        pretty: bool,
84    ) -> Vec<u8> {
85        let opts = Ifc5Options { only_known_properties, pretty, ..Default::default() };
86        ifc_lite_export::export_ifc5(content, &opts).into_bytes()
87    }
88
89    /// Export **OpenUSD** (`.usda` ASCII): a real Z-up USD stage — spatial hierarchy of
90    /// `Xform` prims, `UsdGeomMesh` geometry, `UsdPreviewSurface` materials, IFC
91    /// metadata as custom attributes. Whole-model (geometry-backed).
92    #[wasm_bindgen(js_name = exportUsd)]
93    pub fn export_usd(&self, content: &[u8]) -> Vec<u8> {
94        ifc_lite_export::export_usd(content, &Default::default()).into_bytes()
95    }
96}