Stand up Y-up STEP and IGES files from SolidWorks automatically (#5191)

* claim: #5150

* Stand it up: don't offer it for SolidCraft's own STEP files (#5150)

STEP replies from file.insert_step and doc.open now carry the file's
originating system, and the window skips the stand-up offer when it is
exactly SolidCraft, as SolidCraft's STEP export writes it.

* Stand up Y-up STEP and IGES files from SolidWorks automatically

The STEP header (originating system, preprocessor version, FILE_DESCRIPTION) and the IGES
global section's native system ID say which program wrote a file. A SolidWorks header means
Y up: the import's component is stood up as it opens, as one undo step, and the message offers
Undo. SolidCraft's own files are left alone; an unknown exporter keeps the offer.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Ray <raykholo@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bherbruck
2026-10-23 02:53:20 +00:00
committed by GitHub
parent 0c72ad56d2
commit edf57cec29
15 changed files with 238 additions and 16 deletions
+19 -2
View File
@@ -103,6 +103,19 @@ fn read_step(path: &str, names: solidcraft_io::StepNames) -> Result<solidcraft_i
solidcraft_io::step_import_feature_named(&bytes, path, names).map_err(|e| EngineError::Other(format!("{path}: {e}")))
}
/// Which way is up in a STEP or IGES file, from the program its header names: `("y", name)` for
/// an exporter whose files have Y up, `("z", name)` for SolidCraft's own (its STEP and IGES exports both name it), else `None`. Only
/// SolidWorks is listed as Y up: its STEP and IGES writers are well known to keep that program's
/// Y-up front view, and its name is in FILE_NAME (originating system, preprocessor version) and
/// often FILE_DESCRIPTION. Programs we aren't sure of stay unknown, so the file is only offered.
fn exporter_up(import: &solidcraft_kernel::StepImport) -> Option<(&'static str, &'static str)> {
if import.system.trim().starts_with("SolidCraft") {
return Some(("z", "SolidCraft"));
}
let named = [&import.system, &import.preprocessor, &import.description].iter().any(|h| h.to_ascii_lowercase().contains("solidworks"));
named.then_some(("y", "SolidWorks"))
}
/// A quarter turn about X through the origin: a file's Y up becomes Z up.
fn quarter_turn_x() -> solidcraft_doc::Mat {
solidcraft_doc::rigid(solidcraft_geom::Vec3::ZERO, solidcraft_geom::Vec3::ZERO, solidcraft_geom::Vec3::X, std::f64::consts::FRAC_PI_2)
@@ -120,7 +133,11 @@ fn add_step(s: &mut Session, path: &str, name: Option<&str>, options: &StepOptio
let turned = if options.y_up { turn_up(s, &placed)? } else { None };
let created = created_bodies(s, id);
let fname = s.doc.feature(id).map(|f| f.name.clone()).unwrap_or_default();
let mut out = json!({"path": path, "feature": id, "name": fname, "bodies": created, "created_bodies": created, "warnings": sf.warnings});
let mut out = json!({"path": path, "feature": id, "name": fname, "bodies": created, "created_bodies": created, "warnings": sf.warnings, "system": sf.import.system});
if let Some((up, exporter)) = exporter_up(&sf.import) {
out["exporter"] = json!(exporter);
out["exporter_up"] = json!(up);
}
placed.report(&mut out);
if let Some(turned) = turned {
out["stand_up"] = json!(turned);
@@ -261,7 +278,7 @@ fn open(s: &mut Session, p: &Value) -> Result<Value> {
s.replace_design(fresh);
let errors = s.model.results.iter().filter(|r| r.error.is_some()).count();
let mut out = json!({"path": path, "features": s.doc.features.len(), "errors": errors, "bodies": r["bodies"], "warnings": r["warnings"]});
for k in ["component", "component_name", "occurrence", "subcomponents", "rigid_joints", "stand_up"] {
for k in ["component", "component_name", "occurrence", "subcomponents", "rigid_joints", "stand_up", "system", "exporter", "exporter_up"] {
if let Some(v) = r.get(k) {
out[k] = v.clone();
}
@@ -274,3 +274,61 @@ fn automatic_part_names_and_assembly_survive_reopening() {
assert!(near(extent(&again).0, [0.0, 0.0, 0.0]) && near(extent(&again).1, [40.0, 34.0, 20.0]));
assert!((total_volume(&mut again) - (40.0 * 4.0 * 20.0 + 2.0 * 4.0 * 30.0 * 4.0)).abs() < 1e-6);
}
#[test]
fn step_replies_include_originating_system() -> std::result::Result<(), Box<dyn std::error::Error>> {
let dir = std::env::temp_dir().join(format!("solidcraft-step-system-{}", std::process::id()));
std::fs::create_dir_all(&dir)?;
let path = dir.join("box.step");
let mut source = Session::default();
source.execute("solid.box", &json!({"length": 10, "width": 40, "height": 5}))?;
source.execute("file.export", &json!({"path": path.to_string_lossy()}))?;
let text = std::fs::read_to_string(&path)?;
assert!(text.contains("'SolidCraft','SolidCraft'"));
for system in ["SolidCraft", "Example CAD"] {
std::fs::write(&path, text.replace("'SolidCraft','SolidCraft'", &format!("'SolidCraft','{system}'")))?;
for command in ["file.insert_step", "doc.open"] {
let reply = Session::default().execute(command, &json!({"path": path.to_string_lossy()}))?;
assert_eq!(reply.get("system").and_then(Value::as_str), Some(system), "{command}: {reply}");
}
}
std::fs::remove_dir_all(dir)?;
Ok(())
}
/// A header naming SolidWorks says the file has Y up; SolidCraft's own says Z; anything else
/// says nothing (synthetic headers only).
#[test]
fn step_replies_name_a_y_up_exporter() -> std::result::Result<(), Box<dyn std::error::Error>> {
let dir = std::env::temp_dir().join(format!("solidcraft-step-exporter-{}", std::process::id()));
std::fs::create_dir_all(&dir)?;
let path = dir.join("box.step");
let mut source = Session::default();
source.execute("solid.box", &json!({"length": 10, "width": 40, "height": 5}))?;
source.execute("file.export", &json!({"path": path.to_string_lossy()}))?;
let text = std::fs::read_to_string(&path)?;
let header = "'SolidCraft','SolidCraft'";
assert!(text.contains(header));
for (preprocessor, system, want) in [
("SolidCraft", "SolidCraft", Some(("z", "SolidCraft"))),
("SolidWorks 2021", "SolidWorks 2021", Some(("y", "SolidWorks"))),
("Example CAD", "SOLIDWORKS 2019", Some(("y", "SolidWorks"))),
("Example CAD", "Example CAD", None),
] {
std::fs::write(&path, text.replace(header, &format!("'{preprocessor}','{system}'")))?;
for command in ["file.insert_step", "doc.open"] {
let reply = Session::default().execute(command, &json!({"path": path.to_string_lossy()}))?;
let got = reply.get("exporter_up").and_then(Value::as_str).zip(reply.get("exporter").and_then(Value::as_str));
assert_eq!(got, want, "{command} {system}: {reply}");
}
}
// The program may be named only in FILE_DESCRIPTION.
std::fs::write(
&path,
text.replace("FILE_DESCRIPTION((", "FILE_DESCRIPTION(('SolidWorks export',").replace(header, "'Example CAD','Example CAD'"),
)?;
let reply = Session::default().execute("file.insert_step", &json!({"path": path.to_string_lossy()}))?;
assert_eq!(reply.get("exporter_up").and_then(Value::as_str), Some("y"), "{reply}");
std::fs::remove_dir_all(dir)?;
Ok(())
}