Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@
"cutting_plan.the_project_name_is_too_long_to_fit": "The project name is too long to fit above the workshop labels.",
"cutting_plan.unnamed_part": "Unnamed part",
"cutting_plan.workshop_labels": "Workshop Labels",
"desktop_files.missing_project_output_path": "Missing BenchUp project output path",
"desktop_files.missing_sketchup_file_path": "Missing SketchUp file path",
"drawing_groups.the_drawing_could_not_be_grouped": "The drawing could not be grouped",
"entity_gizmo.axis_lengths": "Axis lengths: {}. {}. {}.",
"entity_gizmo.object_lenghts": "Object lenghts",
Expand Down
2 changes: 2 additions & 0 deletions assets/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,8 @@
"cutting_plan.the_project_name_is_too_long_to_fit": "项目名称过长,无法放在车间标签上方。",
"cutting_plan.unnamed_part": "未命名部件",
"cutting_plan.workshop_labels": "车间标签",
"desktop_files.missing_project_output_path": "缺少 BenchUp 项目输出路径",
"desktop_files.missing_sketchup_file_path": "缺少 SketchUp 文件路径",
"drawing_groups.the_drawing_could_not_be_grouped": "无法对绘图进行分组",
"entity_gizmo.axis_lengths": "轴长度:{}。{}。{}。",
"entity_gizmo.object_lenghts": "对象长度",
Expand Down
40 changes: 40 additions & 0 deletions crates/benchup-app/src/desktop_file_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::*;
use benchup_core::tr;

#[test]
fn skp_conversion_writes_the_same_document_as_import_then_save() {
let directory = tempfile::tempdir().unwrap();
for (fixture, bytes) in [
("placed-window", include_bytes!("../tests/fixtures/placed-window.skp").as_slice()),
("modern-triangle", include_bytes!("../tests/fixtures/modern-triangle.skp").as_slice()),
] {
let input = directory.path().join(format!("{fixture}.skp"));
let output = directory.path().join(format!("{fixture}-converted.bup"));
std::fs::write(&input, bytes).unwrap();
convert_skp(&input, &output).unwrap();
let written = std::fs::read(&output).unwrap();
assert!(written.starts_with(&[0x1f, 0x8b]), "conversion must not embed a preview");
let mut doc = crate::project_files::decode(&written).unwrap();
assert_eq!(doc.name, format!("{fixture}-converted"));
doc.model.validate().unwrap();
let imported = crate::interchange::import_skp(bytes, fixture).unwrap();
assert_eq!(doc.model.faces.len(), imported.faces.len());
assert_eq!(doc.model.objects.len(), imported.objects.len());
assert_eq!(doc.model.objects.last_key_value().unwrap().1.name, fixture);
assert!(!doc.selection.is_empty());
assert_eq!(doc.history.undo_label(), Some(tr!("projects.import_sketchup")));
assert!(doc.history.undo(&mut doc.model));
assert_eq!(doc.model, Model::default());
}
}

#[test]
fn skp_conversion_rejects_bad_or_missing_input_without_writing_output() {
let directory = tempfile::tempdir().unwrap();
let input = directory.path().join("bad.skp");
let output = directory.path().join("out.bup");
std::fs::write(&input, b"not a SketchUp file").unwrap();
assert!(convert_skp(&input, &output).is_err());
assert!(convert_skp(&directory.path().join("missing.skp"), &output).is_err());
assert!(!output.exists());
}
47 changes: 46 additions & 1 deletion crates/benchup-app/src/desktop_files.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Desktop shell entry points. Thumbnail extraction never starts a GPU or UI.
//! Desktop shell entry points. Thumbnail extraction and conversion never start a GPU or UI.
use benchup_core::{DVec2, Document, Model, tr};
use std::{
collections::VecDeque,
path::PathBuf,
Expand All @@ -25,6 +26,50 @@ pub fn thumbnail_command() -> Option<Result<(), String>> {
)
})())
}
pub fn convert_command() -> Option<Result<(), String>> {
let mut args = std::env::args_os().skip(1);
if args.next()?.to_str()? != "--convert" {
return None;
}
Some((|| {
let input = args.next().ok_or(tr!("desktop_files.missing_sketchup_file_path"))?;
let output = args.next().ok_or(tr!("desktop_files.missing_project_output_path"))?;
convert_skp(std::path::Path::new(&input), std::path::Path::new(&output))
})())
}
/// Same document as File > Import into an empty model, then Save, without a preview.
fn convert_skp(input: &std::path::Path, output: &std::path::Path) -> Result<(), String> {
let bytes = std::fs::read(input).map_err(|e| e.to_string())?;
let name = input
.file_stem()
.and_then(|n| n.to_str())
.unwrap_or(tr!("project_files.sketchup_import"));
let imported = crate::interchange::import_skp(&bytes, name)?;
let crate::project_files::Outcome::Imported(model, selected) =
crate::project_files::prepare_import(Model::default(), &imported)?
else {
unreachable!("prepare_import always imports")
};
let mut doc = Document {
model: *model,
selection: selected,
..Document::default()
};
doc.history.record_applied_attributed(
tr!("projects.import_sketchup"),
Model::default(),
&doc.model,
None,
None,
);
doc.camera.fit(doc.model.bounds(), DVec2::new(1440., 900.));
crate::project_files::set_document_name(&mut doc, output);
let json = serde_json::to_string(&doc).map_err(|e| e.to_string())?;
crate::project_files::write(output, &crate::project_files::encode(&json)?)
}
#[cfg(all(test, not(target_arch = "wasm32")))]
#[path = "desktop_file_tests.rs"]
mod tests;
pub fn initialize() {
for arg in std::env::args_os().skip(1) {
let path = PathBuf::from(arg);
Expand Down
4 changes: 3 additions & 1 deletion crates/benchup-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use benchup_core::tr;
#[cfg(not(target_arch = "wasm32"))]
fn main() -> eframe::Result {
if let Some(result) = benchup_app::desktop_files::thumbnail_command() {
if let Some(result) = benchup_app::desktop_files::thumbnail_command()
.or_else(benchup_app::desktop_files::convert_command)
{
if let Err(error) = result { eprintln!("{error}"); std::process::exit(1); }
return Ok(());
}
Expand Down
6 changes: 6 additions & 0 deletions docs/desktop.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ with an image editor: it may discard project data. Previous BenchUp versions
cannot open this format. Existing gzip and plain JSON projects remain readable.
`benchup-app --thumbnail input.bup output.png` extracts the image without
launching the editor or initializing a GPU.

`benchup-app --convert input.skp output.bup` converts a SketchUp file to a
project without a window or GPU. The result matches File → Import SketchUp into
an empty model followed by Save, except that it has no viewport preview. The
project is named after the output file, and the SketchUp group after the input
file. Use a `.bup` output path.
Loading