Skip to content

Commit

Permalink
double-clicking a model file (obj, stl, etc) will auto generate a fil…
Browse files Browse the repository at this point in the history
…e with an import statement and open it (#2400)

* open model files and generate the content

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

---------

Signed-off-by: Jess Frazelle <github@jessfraz.com>
  • Loading branch information
jessfraz committed May 20, 2024
1 parent 15418e9 commit a9ab35e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
38 changes: 38 additions & 0 deletions src/wasm-lib/kcl/src/settings/types/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,44 @@ impl ProjectState {
.await
.map_err(|e| anyhow::anyhow!("Error loading project from path {}: {:?}", source_path.display(), e))?;

// If we got a import model file, we need to check if we have a file in the project for
// this import model.
if crate::settings::utils::IMPORT_FILE_EXTENSIONS.contains(&ext) {
let import_file_name = source_path
.file_name()
.ok_or_else(|| anyhow::anyhow!("Error getting the file name of the file: {}", source_path.display()))?
.to_string_lossy()
.to_string();
// Check if we have a file in the project for this import model.
let kcl_wrapper_filename = format!("{}.kcl", import_file_name);
let kcl_wrapper_file_path = parent.join(&kcl_wrapper_filename);

if !kcl_wrapper_file_path.exists() {
// Create the file in the project.
// With the default import content.
tokio::fs::write(
&kcl_wrapper_file_path,
format!(
r#"// This file was automatically generated by the application when you
// double-clicked on the model file.
// You can edit this file to add your own content.
// But we recommend you keep the import statement as it is.
// For more information on the import statement, see the documentation at:
// https://zoo.dev/docs/kcl/import
const model = import("{}")"#,
import_file_name
)
.as_bytes(),
)
.await?;
}

return Ok(ProjectState {
project,
current_file: Some(kcl_wrapper_file_path.display().to_string()),
});
}

Ok(ProjectState {
project,
current_file: Some(source_path.display().to_string()),
Expand Down
13 changes: 10 additions & 3 deletions src/wasm-lib/kcl/src/settings/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ use clap::ValueEnum;
use crate::settings::types::file::FileEntry;

lazy_static::lazy_static! {
pub static ref RELEVANT_EXTENSIONS: Vec<String> = {
let mut relevant_extensions = vec!["kcl".to_string(), "stp".to_string(), "glb".to_string(), "fbxb".to_string()];

pub static ref IMPORT_FILE_EXTENSIONS: Vec<String> = {
let mut import_file_extensions = vec!["stp".to_string(), "glb".to_string(), "fbxb".to_string()];
let named_extensions = kittycad::types::FileImportFormat::value_variants()
.iter()
.map(|x| format!("{}", x))
.collect::<Vec<String>>();
// Add all the default import formats.
relevant_extensions.extend_from_slice(&named_extensions);
import_file_extensions.extend_from_slice(&named_extensions);
import_file_extensions
};

pub static ref RELEVANT_EXTENSIONS: Vec<String> = {
let mut relevant_extensions = IMPORT_FILE_EXTENSIONS.clone();
relevant_extensions.push("kcl".to_string());
relevant_extensions
};
}
Expand Down

0 comments on commit a9ab35e

Please sign in to comment.