diff --git a/Cargo.lock b/Cargo.lock index 5be43a446..e5611164b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -698,6 +698,7 @@ dependencies = [ "clap", "figment", "fj", + "fj-export", "fj-host", "fj-interop", "fj-kernel", @@ -708,7 +709,6 @@ dependencies = [ "parry3d-f64", "serde", "thiserror", - "threemf", "tracing", "tracing-subscriber", "wgpu", @@ -716,6 +716,15 @@ dependencies = [ "winit", ] +[[package]] +name = "fj-export" +version = "0.5.0" +dependencies = [ + "fj-interop", + "fj-math", + "threemf", +] + [[package]] name = "fj-host" version = "0.5.0" @@ -2543,9 +2552,9 @@ dependencies = [ [[package]] name = "threemf" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0c98800ca8044fa88359515e47c1e2a432dd463b7cfb0764307d5e643e6cc93" +checksum = "02a12818d477c54ac38deb1bd8e222036385b594c020f02097914b6ac81cfff3" dependencies = [ "thiserror", "zip", diff --git a/fj-app/Cargo.toml b/fj-app/Cargo.toml index f90fbfaf3..63ad7538c 100644 --- a/fj-app/Cargo.toml +++ b/fj-app/Cargo.toml @@ -18,7 +18,6 @@ futures = "0.3.21" nalgebra = "0.30.0" parry3d-f64 = "0.8.0" thiserror = "1.0.30" -threemf = "0.2.0" tracing = "0.1.33" wgpu = "0.12.0" wgpu_glyph = "0.16.0" @@ -36,6 +35,10 @@ features = ["env", "toml"] version = "0.5.0" path = "../fj" +[dependencies.fj-export] +version = "0.5.0" +path = "../fj-export" + [dependencies.fj-host] version = "0.5.0" path = "../fj-host" diff --git a/fj-app/src/main.rs b/fj-app/src/main.rs index 43b3953d7..9e770c87e 100644 --- a/fj-app/src/main.rs +++ b/fj-app/src/main.rs @@ -9,6 +9,7 @@ use std::path::PathBuf; use std::time::Instant; use anyhow::anyhow; +use fj_export::export; use fj_host::{Model, Parameters}; use fj_operations::shape_processor::ShapeProcessor; use futures::executor::block_on; @@ -65,27 +66,7 @@ fn main() -> anyhow::Result<()> { let shape = model.load_once(¶meters)?; let shape = shape_processor.process(&shape); - let vertices = - shape.mesh.vertices().map(|vertex| vertex.into()).collect(); - - let indices: Vec<_> = shape.mesh.indices().collect(); - let triangles = indices - .chunks(3) - .map(|triangle| { - [ - triangle[0] as usize, - triangle[1] as usize, - triangle[2] as usize, - ] - }) - .collect(); - - let mesh = threemf::TriangleMesh { - vertices, - triangles, - }; - - threemf::write(path, &mesh)?; + export(&shape.mesh, &path)?; return Ok(()); } diff --git a/fj-export/Cargo.toml b/fj-export/Cargo.toml new file mode 100644 index 000000000..5086dcba6 --- /dev/null +++ b/fj-export/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "fj-export" +version = "0.5.0" +edition = "2021" + +description = "The world needs another CAD program." +readme = "../README.md" +repository = "https://github.com/hannobraun/fornjot" +license = "0BSD" +keywords = ["cad", "programmatic", "code-cad"] +categories = ["encoding"] + + +[dependencies] +threemf = "0.3.0" + +[dependencies.fj-interop] +version = "0.5.0" +path = "../fj-interop" + +[dependencies.fj-math] +version = "0.5.0" +path = "../fj-math" diff --git a/fj-export/src/lib.rs b/fj-export/src/lib.rs new file mode 100644 index 000000000..635802f8c --- /dev/null +++ b/fj-export/src/lib.rs @@ -0,0 +1,39 @@ +//! Exporting Fornjot models to external files + +#![deny(missing_docs)] + +use std::path::Path; + +use fj_interop::mesh::Mesh; +use fj_math::Point; + +/// Export the provided mesh to the file at the given path +/// +/// Currently only 3MF is supported as an export format. The file extension of +/// the provided path is ignored. +pub fn export(mesh: &Mesh>, path: &Path) -> Result<(), Error> { + let vertices = mesh.vertices().map(|vertex| vertex.into()).collect(); + + let indices: Vec<_> = mesh.indices().collect(); + let triangles = indices + .chunks(3) + .map(|triangle| { + [ + triangle[0] as usize, + triangle[1] as usize, + triangle[2] as usize, + ] + }) + .collect(); + + let mesh = threemf::TriangleMesh { + vertices, + triangles, + }; + + threemf::write(path, &mesh)?; + + Ok(()) +} + +pub use threemf::Error;