Skip to content

Commit

Permalink
Use import statement for foreign files
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Cameron <[email protected]>
  • Loading branch information
nrc committed Jan 21, 2025
1 parent 64872f5 commit d19a93e
Show file tree
Hide file tree
Showing 17 changed files with 3,895 additions and 353 deletions.
4 changes: 4 additions & 0 deletions docs/kcl/import.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ excerpt: "Import a CAD file."
layout: manual
---

**WARNING:** This function is deprecated.

Import a CAD file.

**DEPRECATED** Prefer to use import statements.

For formats lacking unit data (such as STL, OBJ, or PLY files), the default unit of measurement is millimeters. Alternatively you may specify the unit by passing your desired measurement unit in the options parameter. When importing a GLTF file, the bin file will be imported as well. Import paths are relative to the current project directory.

Note: The import command currently only works when using the native Modeling App.
Expand Down
1 change: 0 additions & 1 deletion docs/kcl/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ layout: manual
* [`helixRevolutions`](kcl/helixRevolutions)
* [`hole`](kcl/hole)
* [`hollow`](kcl/hollow)
* [`import`](kcl/import)
* [`inch`](kcl/inch)
* [`lastSegX`](kcl/lastSegX)
* [`lastSegY`](kcl/lastSegY)
Expand Down
4 changes: 2 additions & 2 deletions docs/kcl/std.json
Original file line number Diff line number Diff line change
Expand Up @@ -86797,7 +86797,7 @@
{
"name": "import",
"summary": "Import a CAD file.",
"description": "For formats lacking unit data (such as STL, OBJ, or PLY files), the default unit of measurement is millimeters. Alternatively you may specify the unit by passing your desired measurement unit in the options parameter. When importing a GLTF file, the bin file will be imported as well. Import paths are relative to the current project directory.\n\nNote: The import command currently only works when using the native Modeling App.\n\nFor importing KCL functions using the `import` statement, see the docs on [KCL modules](/docs/kcl/modules).",
"description": "**DEPRECATED** Prefer to use import statements.\n\nFor formats lacking unit data (such as STL, OBJ, or PLY files), the default unit of measurement is millimeters. Alternatively you may specify the unit by passing your desired measurement unit in the options parameter. When importing a GLTF file, the bin file will be imported as well. Import paths are relative to the current project directory.\n\nNote: The import command currently only works when using the native Modeling App.\n\nFor importing KCL functions using the `import` statement, see the docs on [KCL modules](/docs/kcl/modules).",
"tags": [],
"keywordArguments": false,
"args": [
Expand Down Expand Up @@ -87200,7 +87200,7 @@
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"deprecated": true,
"examples": [
"model = import(\"tests/inputs/cube.obj\")",
"model = import(\"tests/inputs/cube.obj\", { format = \"obj\", units = \"m\" })",
Expand Down
294 changes: 294 additions & 0 deletions src/wasm-lib/kcl/src/execution/import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
use std::{ffi::OsStr, path::Path, str::FromStr};

use anyhow::Result;
use kcmc::{
coord::{Axis, AxisDirectionPair, Direction, System},
each_cmd as mcmd,
format::InputFormat,
ok_response::OkModelingCmdResponse,
shared::FileImportFormat,
units::UnitLength,
websocket::OkWebSocketResponseData,
ImportFile, ModelingCmd,
};
use kittycad_modeling_cmds as kcmc;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::{
errors::{KclError, KclErrorDetails},
execution::{ExecState, ImportedGeometry},
fs::FileSystem,
source_range::SourceRange,
};

use super::ExecutorContext;

// Zoo co-ordinate system.
//
// * Forward: -Y
// * Up: +Z
// * Handedness: Right
pub const ZOO_COORD_SYSTEM: System = System {
forward: AxisDirectionPair {
axis: Axis::Y,
direction: Direction::Negative,
},
up: AxisDirectionPair {
axis: Axis::Z,
direction: Direction::Positive,
},
};

pub async fn import_foreign(
file_path: &Path,
format: Option<InputFormat>,
exec_state: &mut ExecState,
ctxt: &ExecutorContext,
source_range: SourceRange,
) -> Result<PreImportedGeometry, KclError> {
// Make sure the file exists.
if !ctxt.fs.exists(file_path, source_range).await? {
return Err(KclError::Semantic(KclErrorDetails {
message: format!("File `{}` does not exist.", file_path.display()),
source_ranges: vec![source_range],
}));
}

let ext_format = get_import_format_from_extension(file_path.extension().ok_or_else(|| {
KclError::Semantic(KclErrorDetails {
message: format!("No file extension found for `{}`", file_path.display()),
source_ranges: vec![source_range],
})

Check warning on line 62 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L59-L62

Added lines #L59 - L62 were not covered by tests
})?)
.map_err(|e| {
KclError::Semantic(KclErrorDetails {
message: e.to_string(),
source_ranges: vec![source_range],
})

Check warning on line 68 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L65-L68

Added lines #L65 - L68 were not covered by tests
})?;

// Get the format type from the extension of the file.
let format = if let Some(format) = format {
// Validate the given format with the extension format.
validate_extension_format(ext_format, format.clone()).map_err(|e| {
KclError::Semantic(KclErrorDetails {
message: e.to_string(),
source_ranges: vec![source_range],
})
})?;
format
} else {
ext_format
};

// Get the file contents for each file path.
let file_contents = ctxt.fs.read(file_path, source_range).await.map_err(|e| {
KclError::Semantic(KclErrorDetails {
message: e.to_string(),
source_ranges: vec![source_range],
})

Check warning on line 90 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L87-L90

Added lines #L87 - L90 were not covered by tests
})?;

// We want the file_path to be without the parent.
let file_name = std::path::Path::new(&file_path)
.file_name()
.map(|p| p.to_string_lossy().to_string())
.ok_or_else(|| {
KclError::Semantic(KclErrorDetails {
message: format!("Could not get the file name from the path `{}`", file_path.display()),
source_ranges: vec![source_range],
})

Check warning on line 101 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L98-L101

Added lines #L98 - L101 were not covered by tests
})?;
let mut import_files = vec![kcmc::ImportFile {
path: file_name.to_string(),
data: file_contents.clone(),
}];

// In the case of a gltf importing a bin file we need to handle that! and figure out where the
// file is relative to our current file.
if let InputFormat::Gltf(..) = format {
// Check if the file is a binary gltf file, in that case we don't need to import the bin
// file.
if !file_contents.starts_with(b"glTF") {
let json = gltf_json::Root::from_slice(&file_contents).map_err(|e| {
KclError::Semantic(KclErrorDetails {
message: e.to_string(),
source_ranges: vec![source_range],
})

Check warning on line 118 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L115-L118

Added lines #L115 - L118 were not covered by tests
})?;

// Read the gltf file and check if there is a bin file.
for buffer in json.buffers.iter() {
if let Some(uri) = &buffer.uri {
if !uri.starts_with("data:") {
// We want this path relative to the file_path given.
let bin_path = std::path::Path::new(&file_path)
.parent()
.map(|p| p.join(uri))
.map(|p| p.to_string_lossy().to_string())
.ok_or_else(|| {
KclError::Semantic(KclErrorDetails {
message: format!(
"Could not get the parent path of the file `{}`",
file_path.display()
),
source_ranges: vec![source_range],
})

Check warning on line 137 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L131-L137

Added lines #L131 - L137 were not covered by tests
})?;

let bin_contents = ctxt.fs.read(&bin_path, source_range).await.map_err(|e| {
KclError::Semantic(KclErrorDetails {
message: e.to_string(),
source_ranges: vec![source_range],
})

Check warning on line 144 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L141-L144

Added lines #L141 - L144 were not covered by tests
})?;

import_files.push(ImportFile {
path: uri.to_string(),
data: bin_contents,
});
}
}

Check warning on line 152 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L152

Added line #L152 was not covered by tests
}
}
}
Ok(PreImportedGeometry {
id: exec_state.next_uuid(),
source_range,
command: mcmd::ImportFiles {
files: import_files.clone(),
format,
},
})
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct PreImportedGeometry {
id: Uuid,
command: mcmd::ImportFiles,
source_range: SourceRange,
}

pub async fn send_to_engine(pre: PreImportedGeometry, ctxt: &ExecutorContext) -> Result<ImportedGeometry, KclError> {
if ctxt.is_mock() {
return Ok(ImportedGeometry {
id: pre.id,
value: pre.command.files.iter().map(|f| f.path.to_string()).collect(),
meta: vec![pre.source_range.into()],
});
}

let resp = ctxt
.engine
.send_modeling_cmd(pre.id, pre.source_range, &ModelingCmd::from(pre.command.clone()))
.await?;

let OkWebSocketResponseData::Modeling {
modeling_response: OkModelingCmdResponse::ImportFiles(imported_files),
} = &resp
else {
return Err(KclError::Engine(KclErrorDetails {
message: format!("ImportFiles response was not as expected: {:?}", resp),
source_ranges: vec![pre.source_range],
}));

Check warning on line 194 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L191-L194

Added lines #L191 - L194 were not covered by tests
};

Ok(ImportedGeometry {
id: imported_files.object_id,
value: pre.command.files.iter().map(|f| f.path.to_string()).collect(),
meta: vec![pre.source_range.into()],
})
}

/// Get the source format from the extension.
fn get_import_format_from_extension(ext: &OsStr) -> Result<InputFormat> {
let ext = ext
.to_str()
.ok_or_else(|| anyhow::anyhow!("Invalid file extension: `{ext:?}`"))?;
let format = match FileImportFormat::from_str(ext) {
Ok(format) => format,
Err(_) => {
if ext == "stp" {
FileImportFormat::Step

Check warning on line 213 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L213

Added line #L213 was not covered by tests
} else if ext == "glb" {
FileImportFormat::Gltf
} else {
anyhow::bail!("unknown source format for file extension: {ext}. Try setting the `--src-format` flag explicitly or use a valid format.")

Check warning on line 217 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L217

Added line #L217 was not covered by tests
}
}
};

// Make the default units millimeters.
let ul = UnitLength::Millimeters;

// Zoo co-ordinate system.
//
// * Forward: -Y
// * Up: +Z
// * Handedness: Right
match format {
FileImportFormat::Step => Ok(InputFormat::Step(kcmc::format::step::import::Options {
split_closed_faces: false,
})),
FileImportFormat::Stl => Ok(InputFormat::Stl(kcmc::format::stl::import::Options {
coords: ZOO_COORD_SYSTEM,
units: ul,
})),
FileImportFormat::Obj => Ok(InputFormat::Obj(kcmc::format::obj::import::Options {
coords: ZOO_COORD_SYSTEM,
units: ul,
})),
FileImportFormat::Gltf => Ok(InputFormat::Gltf(kcmc::format::gltf::import::Options {})),
FileImportFormat::Ply => Ok(InputFormat::Ply(kcmc::format::ply::import::Options {
coords: ZOO_COORD_SYSTEM,
units: ul,
})),
FileImportFormat::Fbx => Ok(InputFormat::Fbx(kcmc::format::fbx::import::Options {})),

Check warning on line 247 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L243-L247

Added lines #L243 - L247 were not covered by tests
FileImportFormat::Sldprt => Ok(InputFormat::Sldprt(kcmc::format::sldprt::import::Options {
split_closed_faces: false,
})),
}
}

fn validate_extension_format(ext: InputFormat, given: InputFormat) -> Result<()> {
if let InputFormat::Stl(_) = ext {
if let InputFormat::Stl(_) = given {
return Ok(());
}

Check warning on line 258 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L256-L258

Added lines #L256 - L258 were not covered by tests
}

if let InputFormat::Obj(_) = ext {
if let InputFormat::Obj(_) = given {
return Ok(());
}

Check warning on line 264 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L264

Added line #L264 was not covered by tests
}

if let InputFormat::Ply(_) = ext {
if let InputFormat::Ply(_) = given {
return Ok(());
}

Check warning on line 270 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L268-L270

Added lines #L268 - L270 were not covered by tests
}

if ext == given {
return Ok(());

Check warning on line 274 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L274

Added line #L274 was not covered by tests
}

anyhow::bail!(
"The given format does not match the file extension. Expected: `{}`, Given: `{}`",
get_name_of_format(ext),
get_name_of_format(given)
)
}

fn get_name_of_format(type_: InputFormat) -> &'static str {
match type_ {
InputFormat::Fbx(_) => "fbx",

Check warning on line 286 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L286

Added line #L286 was not covered by tests
InputFormat::Gltf(_) => "gltf",
InputFormat::Obj(_) => "obj",
InputFormat::Ply(_) => "ply",
InputFormat::Sldprt(_) => "sldprt",
InputFormat::Step(_) => "step",
InputFormat::Stl(_) => "stl",

Check warning on line 292 in src/wasm-lib/kcl/src/execution/import.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/import.rs#L289-L292

Added lines #L289 - L292 were not covered by tests
}
}
Loading

0 comments on commit d19a93e

Please sign in to comment.