Parameters: unit-aware parameters (an inch parameter reads bare numbers as inches), atan2/mod/trunc and clearer unit errors, cycle paths (a → b → a), rename with every reference following (expressions, feature inputs, dimensions, favourites, comments), deletes refused with the list of users, parameter errors shown on the features using them, parameters.add/rename/delete/comment/users/graph/complete and expr.evaluate; ChangeParameterCommand renames and favourites; hostile test no longer writes stray files
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AS5Tn3xieewUSg3uxrMVdL
This commit is contained in:
1 parent
a88b8eb6b3
commit
dd7485503a
13 files changed
+1470
-322
No files matched your search
@@ -1,4 +1,4 @@
|
||||
//! Undo/redo, parameters, the timeline, deletion and selection.
|
||||
//! Undo/redo, the timeline, deletion and selection.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use solvecraft_doc::{Feature, FeatureKind};
|
||||
@@ -10,17 +10,6 @@ use crate::{EngineError, Result, Sel, Session, Snapshot};
|
||||
pub static COMMANDS: &[CommandSpec] = &[
|
||||
CommandSpec::new("UndoCommand", "Undo", undo).icon("undo").key("Ctrl+Z").noundo(),
|
||||
CommandSpec::new("RedoCommand", "Redo", redo).icon("redo").key("Ctrl+Y").noundo(),
|
||||
CommandSpec::new("parameters.list", "List Parameters", params_list).noundo().params("favorites?: bool (only favourites)"),
|
||||
CommandSpec::new("parameters.favorite", "Favourite Parameter", params_favorite).params("name, favorite?: bool (default true)"),
|
||||
CommandSpec::new("parameters.export", "Export Parameters", params_export)
|
||||
.noundo()
|
||||
.params("format?: csv|json (default csv), path?: write to a file"),
|
||||
CommandSpec::new("parameters.import", "Import Parameters", params_import)
|
||||
.params("text: CSV (name,unit,expression,comment) or JSON [{name, expression, unit?, comment?}] | path"),
|
||||
CommandSpec::new("ChangeParameterCommand", "Change Parameters", change_param)
|
||||
.at("SOLID", "MODIFY")
|
||||
.icon("params")
|
||||
.params("name, expression (number or text), unit?: mm|deg|\"\", comment?; or delete: name"),
|
||||
CommandSpec::new("FusionComputeAllCommand", "Compute All", compute_all).at("SOLID", "MODIFY").icon("compute").noundo(),
|
||||
CommandSpec::new("FusionDeleteCommand", "Delete", delete)
|
||||
.at("SOLID", "MODIFY")
|
||||
@@ -69,29 +58,6 @@ fn redo(s: &mut Session, _p: &Value) -> Result<Value> {
|
||||
Ok(json!({"redone": label}))
|
||||
}
|
||||
|
||||
fn change_param(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "ChangeParameterCommand";
|
||||
if let Some(n) = str_(p, "delete") {
|
||||
s.doc_mut().remove_param(n)?;
|
||||
return Ok(json!({"deleted": n}));
|
||||
}
|
||||
let name = str_(p, "name").ok_or_else(|| bad(cmd, "`name` is required"))?;
|
||||
let e = match p.get("expression").or_else(|| p.get("value")) {
|
||||
Some(Value::String(x)) => x.clone(),
|
||||
Some(Value::Number(n)) => n.to_string(),
|
||||
_ => return Err(bad(cmd, "`expression` must be a number or text")),
|
||||
};
|
||||
let unit = str_(p, "unit");
|
||||
let comment = str_(p, "comment");
|
||||
s.doc_mut().change_param(name, &e, unit, comment)?;
|
||||
s.refresh();
|
||||
let (vals, _) = s.doc.param_values();
|
||||
let v = vals.get(name).map(|v| v.v);
|
||||
// Report features that now fail.
|
||||
let errors: Vec<Value> = s.model.results.iter().filter_map(|r| r.error.as_ref().map(|e| json!({"feature": r.name, "error": e}))).collect();
|
||||
Ok(json!({"name": name, "expression": e, "value": v, "recomputed": s.model.last_recomputed, "errors": errors}))
|
||||
}
|
||||
|
||||
fn compute_all(s: &mut Session, _p: &Value) -> Result<Value> {
|
||||
s.model = solvecraft_doc::Model::new();
|
||||
s.refresh();
|
||||
@@ -340,57 +306,3 @@ fn dependents(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let broken: Vec<String> = newly_broken(&s.model, &after).into_iter().map(|(n, _)| n).collect();
|
||||
Ok(json!({"deleted_with_it": names(&gone), "would_fail": broken}))
|
||||
}
|
||||
|
||||
fn params_list(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let only_fav = bool_(p, "favorites").unwrap_or(false);
|
||||
let rows: Vec<solvecraft_doc::ParamRow> = s.doc.param_rows().into_iter().filter(|r| !only_fav || r.favorite).collect();
|
||||
let cycles = solvecraft_doc::cycles(&s.doc.all_param_exprs());
|
||||
Ok(json!({"parameters": rows, "cycles": cycles}))
|
||||
}
|
||||
|
||||
fn params_favorite(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "parameters.favorite";
|
||||
let name = str_(p, "name").ok_or_else(|| bad(cmd, "`name` is required"))?;
|
||||
let on = bool_(p, "favorite").unwrap_or(true);
|
||||
s.doc_mut().set_favorite(name, on)?;
|
||||
Ok(json!({"name": name, "favorite": on}))
|
||||
}
|
||||
|
||||
fn params_export(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "parameters.export";
|
||||
let text = match str_(p, "format").unwrap_or("csv") {
|
||||
"csv" => s.doc.params_csv(),
|
||||
"json" => {
|
||||
let rows: Vec<Value> = s
|
||||
.doc
|
||||
.param_rows()
|
||||
.into_iter()
|
||||
.filter(|r| r.source == "user")
|
||||
.map(|r| json!({"name": r.name, "expression": r.expression, "unit": r.unit, "value": r.value, "comment": r.comment, "favorite": r.favorite}))
|
||||
.collect();
|
||||
serde_json::to_string_pretty(&rows).map_err(|e| bad(cmd, e.to_string()))?
|
||||
}
|
||||
other => return Err(bad(cmd, format!("unknown format `{other}`"))),
|
||||
};
|
||||
if let Some(path) = str_(p, "path") {
|
||||
std::fs::write(path, &text).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
}
|
||||
Ok(json!({ "text": text }))
|
||||
}
|
||||
|
||||
fn params_import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "parameters.import";
|
||||
let text = match (str_(p, "text"), str_(p, "path")) {
|
||||
(Some(t), _) => t.to_string(),
|
||||
(None, Some(path)) => {
|
||||
let meta = std::fs::metadata(path).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
if meta.len() > 16 << 20 {
|
||||
return Err(bad(cmd, "file too large"));
|
||||
}
|
||||
std::fs::read_to_string(path).map_err(|e| EngineError::Other(format!("{path}: {e}")))?
|
||||
}
|
||||
_ => return Err(bad(cmd, "give `text` or `path`")),
|
||||
};
|
||||
let names = s.doc_mut().import_params(&text)?;
|
||||
Ok(json!({ "imported": names }))
|
||||
}
|
||||
Reference in new issue
Block a user