pub mod collada;
pub mod color_mapping;
pub mod fbx;
pub mod gltf;
pub mod obj;
pub mod stl;
use std::path::Path;
use crate::document::CadDocument;
use crate::error::{DxfError, Result};
use crate::types::Color;
pub use collada::ColladaImporter;
pub use fbx::FbxImporter;
pub use gltf::GltfImporter;
pub use obj::ObjImporter;
pub use stl::StlImporter;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImportFormat {
Stl,
Collada,
Obj,
Gltf,
Glb,
Fbx,
}
#[derive(Debug, Clone)]
pub struct ImportConfig {
pub layer_prefix: String,
pub default_color: Color,
pub merge_vertices: bool,
pub merge_tolerance: f64,
pub scale_factor: f64,
}
impl Default for ImportConfig {
fn default() -> Self {
Self {
layer_prefix: "imported".to_string(),
default_color: Color::ByLayer,
merge_vertices: true,
merge_tolerance: 1e-9,
scale_factor: 1.0,
}
}
}
pub fn detect_format(path: &Path) -> Result<ImportFormat> {
let ext = path
.extension()
.and_then(|e| e.to_str())
.map(|e| e.to_ascii_lowercase())
.unwrap_or_default();
match ext.as_str() {
"stl" => Ok(ImportFormat::Stl),
"dae" => Ok(ImportFormat::Collada),
"obj" => Ok(ImportFormat::Obj),
"gltf" => Ok(ImportFormat::Gltf),
"glb" => Ok(ImportFormat::Glb),
"fbx" => Ok(ImportFormat::Fbx),
_ => Err(DxfError::ImportError(format!(
"Unsupported import format: .{}",
ext
))),
}
}
pub fn import_file(path: impl AsRef<Path>, config: &ImportConfig) -> Result<CadDocument> {
let path = path.as_ref();
let format = detect_format(path)?;
match format {
ImportFormat::Stl => StlImporter::from_file(path)?.with_config(config.clone()).import(),
ImportFormat::Collada => {
ColladaImporter::from_file(path)?.with_config(config.clone()).import()
}
ImportFormat::Obj => ObjImporter::from_file(path)?.with_config(config.clone()).import(),
ImportFormat::Gltf | ImportFormat::Glb => {
GltfImporter::from_file(path)?.with_config(config.clone()).import()
}
ImportFormat::Fbx => FbxImporter::from_file(path)?.with_config(config.clone()).import(),
}
}