Merge branch 'ucs-core' into ucs-drafting

This commit is contained in:
SBaksa
2026-10-19 14:44:42 +11:00
5 changed files with 88 additions and 11 deletions
+2 -1
View File
@@ -59,7 +59,8 @@ pub fn specs() -> Vec<CommandSpec> {
.interactive(|_| Ok(Box::new(UcsM::starting(Stage::ZAngle)))),
CommandSpec::new("ucsicon", "UCS Icon", run_ucsicon)
.params("{on?: bool, origin?: bool} (UCSICON: the icon shown, and at the UCS origin when that is on screen) → {on, origin}")
.noundo()
// Undoable like SETVAR: UCSICON lives in the drawing, so without a step of its own an
// undo of the edit before it would put the old value back.
.interactive(|_| Ok(Box::new(UcsIconM))),
]
}
+20
View File
@@ -475,6 +475,26 @@ fn ucsicon_options() {
assert!(cadcraft_engine::sysvars::set(&mut s, "UCSICON", &json!(9)).is_err());
}
/// UCSICON is its own undo step: undoing it brings the icon back, and an undo never undoes the
/// edit before it while leaving the icon setting behind (or the other way round).
#[test]
fn ucsicon_is_undone_on_its_own() {
let mut s = session();
let lines = |s: &Session| s.doc().unwrap().model.iter().filter(|e| matches!(e.kind, EntityKind::Line(_))).count();
s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap();
typed(&mut s, &["ucsicon off"]);
assert_eq!(s.ucsicon_bits() & 1, 0);
s.undo().unwrap();
assert_eq!(s.ucsicon_bits() & 1, 1, "undo takes back UCSICON OFF");
assert_eq!(lines(&s), 1, "and leaves the line drawn before it");
s.undo().unwrap();
assert_eq!(lines(&s), 0);
assert_eq!(s.ucsicon_bits() & 1, 1);
s.redo().unwrap();
s.redo().unwrap();
assert_eq!((lines(&s), s.ucsicon_bits() & 1), (1, 0), "redo brings both back");
}
#[test]
fn a_tilted_ucs_from_a_file_is_kept_and_input_stays_in_the_world_ucs() {
let mut s = session();
+16 -6
View File
@@ -304,11 +304,15 @@ fn model_ucs(d: &Drawing) -> Ucs {
let h = &d.header;
let p = |name: &str| h.point(name).filter(|v| v.is_finite());
let axis = |name: &str| p(name).filter(|v| v.len() > 1e-12);
let (x, y) = match (axis("UCSXDIR"), axis("UCSYDIR")) {
(Some(x), Some(y)) if x.cross(y).len() > 1e-9 * x.len() * y.len() => (x, y),
_ => (Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 1.0, 0.0)),
let (x, y, usable) = match (axis("UCSXDIR"), axis("UCSYDIR")) {
(Some(x), Some(y)) if x.cross(y).len() > 1e-9 * x.len() * y.len() => (x, y, true),
_ => (Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 1.0, 0.0), false),
};
Ucs { name: h.str("UCSNAME", ""), origin: p("UCSORG").unwrap_or(Vec3::ZERO), x_axis: x, y_axis: y }
// The name only of a UCS table record, and only with the axes it came with: a name read from a
// file without its record, or beside axes replaced by the World UCS's, is dropped.
let raw = h.str("UCSNAME", "");
let name = d.ucss.iter().find(|u| usable && u.name.eq_ignore_ascii_case(raw.trim())).map(|u| u.name.clone()).unwrap_or_default();
Ucs { name, origin: p("UCSORG").unwrap_or(Vec3::ZERO), x_axis: x, y_axis: y }
}
fn header_vars(w: &mut W, d: &Drawing) {
@@ -344,6 +348,11 @@ fn header_vars(w: &mut W, d: &Drawing) {
w.i(c, i);
}
}
// Only the name of a UCS the drawing has, with the axes written above.
(c, _) if *name == "UCSNAME" => {
w.s(9, "$UCSNAME");
w.s(c, model_ucs(d).name);
}
(c, v) => {
if let Some(t) = v.as_str() {
w.s(9, format!("${name}"));
@@ -1783,8 +1792,9 @@ fn tags_for(d: &Drawing, version: Version, target: Version) -> Vec<Tag> {
if let Some(name) = d.header.get("CMLEADERSTYLE").and_then(HVal::as_str) {
xrecords.push((dxf_ext::MLEADER_STYLE_KEY, w.h(), vec![name.to_string()]));
}
if let Some(name) = d.header.get("UCSNAME").and_then(HVal::as_str).map(str::trim).filter(|n| !n.is_empty()) {
xrecords.push((dxf_ext::UCS_NAME_KEY, w.h(), vec![name.to_string()]));
let ucs_name = model_ucs(d).name;
if !ucs_name.is_empty() {
xrecords.push((dxf_ext::UCS_NAME_KEY, w.h(), vec![ucs_name]));
}
if let Some(chunks) = dxf_ext::constraint_chunks(&d.constraints, &d.parametric) {
xrecords.push((dxf_ext::CONSTRAINTS_KEY, w.h(), chunks));
+26
View File
@@ -183,6 +183,32 @@ fn broken_ucs_values_are_not_written_back() {
assert!(back.layouts[0].ucs.is_none());
}
/// Model space's UCS name is written only for a UCS the drawing has, with the axes it came with:
/// a name without its record, or beside axes replaced by the World UCS's, is left out of both
/// $UCSNAME and CADCraft's copy of it.
#[test]
fn a_ucs_name_without_its_ucs_is_not_written() {
let name_written = |d: &Drawing| {
let text = cadcraft_io::write_dxf(d);
let back = cadcraft_io::read_dxf(text.as_bytes()).unwrap();
(
group(&header_var(&text, "UCSNAME"), "2").unwrap_or_default().trim().to_string(),
text.contains("CADCRAFT_UCSNAME"),
back.header.str("UCSNAME", ""),
)
};
// Kept: SITE is in the UCS table and its axes are current.
assert_eq!(name_written(&drawing()), ("SITE".to_string(), true, "SITE".to_string()));
// No such record (as a file can say).
let mut ghost = drawing();
ghost.header.set_str("UCSNAME", "GHOST");
assert_eq!(name_written(&ghost), (String::new(), false, String::new()));
// The record is there, but the axes beside the name are unusable: written as the World UCS.
let mut broken = drawing();
broken.header.set("UCSXDIR", HVal::Point(Vec3::ZERO));
assert_eq!(name_written(&broken), (String::new(), false, String::new()));
}
#[test]
fn hostile_layout_ucs_groups_are_ignored() {
let d = Drawing::default();
+24 -4
View File
@@ -298,13 +298,17 @@ fn ucs_menu(app: &mut CadApp, popup: egui::Popup<'_>) {
ui.close();
}
});
if let Some((id, params)) = action
&& let Err(e) = app.run(id, params)
{
app.session.echo(e);
if let Some((id, params)) = action {
menu_action(app, id, params);
}
}
/// Run a coordinate-system menu pick. A failure is already in the command history:
/// [`CadApp::run`] echoes it (and the icon switch runs UCSICON through it).
fn menu_action(app: &mut CadApp, id: &str, params: serde_json::Value) {
let _ = app.run(id, params);
}
#[cfg(test)]
mod tests {
use super::*;
@@ -348,6 +352,22 @@ mod tests {
app.canvas.rect.unwrap_or(Rect::NOTHING)
}
/// A menu pick that fails (a tilted named UCS) tells the user once, not twice.
#[test]
fn a_failed_menu_pick_is_reported_once() {
let (mut app, _ctx) = app_and_ctx();
let tilted = cadcraft_engine::doc::Ucs {
name: "T".into(),
origin: cadcraft_engine::geom::Vec3::ZERO,
x_axis: cadcraft_engine::geom::Vec3::new(1.0, 0.0, 0.0),
y_axis: cadcraft_engine::geom::Vec3::new(0.0, 0.0, 1.0),
};
app.session.doc_mut().unwrap().ucss.push(tilted);
menu_action(&mut app, "ucs", json!({"option": "named", "action": "restore", "name": "T"}));
let told = app.session.log.iter().filter(|l| l.contains("tilted out of the XY plane")).count();
assert_eq!(told, 1, "{:?}", app.session.log);
}
fn view(app: &CadApp) -> (f64, f64, f64) {
let v = app.session.state().map(|s| s.view()).unwrap_or_default();
(v.center.x, v.center.y, v.height)