c361630954
Adds File > Export > Current View as DXF: the visible bodies projected from the current camera onto 2D LINE/LWPOLYLINE entities, with hidden edges omitted or dashed. Co-authored-by: cronos <19868002+crono2250@users.noreply.github.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
103 lines
4.5 KiB
Rust
103 lines
4.5 KiB
Rust
use super::*;
|
|
use solidcraft_geom::{Vec2, Vec3};
|
|
use solidcraft_render::StandardView;
|
|
|
|
fn run(s: &mut Session, id: &str, p: Value) -> Value {
|
|
s.execute(id, &p).unwrap()
|
|
}
|
|
fn sample() -> Session {
|
|
let mut s = Session::default();
|
|
run(&mut s, "solid.box", json!({"length":40,"width":30,"height":10,"body_name":"Plate"}));
|
|
// A blind hole on the bottom, completely hidden from above.
|
|
run(&mut s, "solid.cylinder", json!({"radius":3,"height":4,"base":[10,10,0],"operation":"cut","targets":["Plate"]}));
|
|
s
|
|
}
|
|
fn camera(view: StandardView) -> Camera {
|
|
let mut c = Camera { target: Vec3::new(20., 15., 5.), fov: 0., distance: 150., ..Default::default() };
|
|
c.set_view(view);
|
|
c
|
|
}
|
|
fn export(s: &mut Session, c: Camera, hidden: &str) -> Value {
|
|
run(s, CMD, json!({"camera":c,"hidden":hidden}))
|
|
}
|
|
|
|
#[test]
|
|
fn view_dxf_blind_back_hole_is_omitted_or_dashed_and_front_hole_is_visible() {
|
|
let mut s = sample();
|
|
let undo = s.undo.len();
|
|
let doc = s.doc.clone();
|
|
let top = export(&mut s, camera(StandardView::Top), "omit");
|
|
let dashed = export(&mut s, camera(StandardView::Top), "dashed");
|
|
assert!(top["visible"].as_u64().unwrap() > 0);
|
|
assert_eq!(top["hidden"], 0);
|
|
assert!(dashed["hidden"].as_u64().unwrap() > 12, "{dashed}");
|
|
assert_eq!(top["visible"], dashed["visible"]);
|
|
assert!(!top["dxf"].as_str().unwrap().contains("HIDDEN"));
|
|
assert!(dashed["dxf"].as_str().unwrap().contains("LWPOLYLINE"));
|
|
assert!(dashed["dxf"].as_str().unwrap().contains("128\n"));
|
|
let bottom = export(&mut s, camera(StandardView::Bottom), "omit");
|
|
assert!(bottom["visible"].as_u64().unwrap() > top["visible"].as_u64().unwrap());
|
|
assert_eq!(s.undo.len(), undo);
|
|
assert!(std::sync::Arc::ptr_eq(&doc, &s.doc));
|
|
}
|
|
|
|
#[test]
|
|
fn view_dxf_respects_hidden_bodies_mm_and_camera_roll() {
|
|
let mut s = Session::default();
|
|
run(&mut s, "solid.box", json!({"length":40,"width":30,"height":10,"body_name":"Plate"}));
|
|
run(&mut s, "solid.box", json!({"length":5,"width":5,"height":5,"corner":[100,0,0],"body_name":"Other"}));
|
|
run(&mut s, "browser.visibility", json!({"items":["Other"],"visible":false}));
|
|
let mut c = camera(StandardView::Top);
|
|
let drawing = export(&mut s, c, "omit");
|
|
let size = |v: &Value| {
|
|
let mut points = Vec::new();
|
|
for g in solidcraft_io::read_dxf(v["dxf"].as_str().unwrap()).unwrap() {
|
|
if let solidcraft_io::Geom2::Line(a, b) = g {
|
|
points.extend([a, b]);
|
|
}
|
|
}
|
|
let min = points.iter().fold(Vec2::new(f64::INFINITY, f64::INFINITY), |a, p| Vec2::new(a.x.min(p.x), a.y.min(p.y)));
|
|
let max = points.iter().fold(Vec2::new(f64::NEG_INFINITY, f64::NEG_INFINITY), |a, p| Vec2::new(a.x.max(p.x), a.y.max(p.y)));
|
|
max - min
|
|
};
|
|
let d = size(&drawing);
|
|
assert!((d.x - 40.).abs() < 0.001 && (d.y - 30.).abs() < 0.001, "{d:?}");
|
|
c.roll = std::f64::consts::FRAC_PI_2;
|
|
let d = size(&export(&mut s, c, "omit"));
|
|
assert!((d.x - 30.).abs() < 0.001 && (d.y - 40.).abs() < 0.001, "{d:?}");
|
|
run(&mut s, "browser.visibility", json!({"items":["Plate"],"visible":false}));
|
|
assert!(s.execute(CMD, &json!({"camera":c})).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn view_dxf_cylinders_have_side_silhouettes_and_perspective_is_supported() {
|
|
let mut s = Session::default();
|
|
run(&mut s, "solid.cylinder", json!({"radius":10,"height":20}));
|
|
let mut c = camera(StandardView::Front);
|
|
c.target = Vec3::new(0., 0., 10.);
|
|
let v = export(&mut s, c, "omit");
|
|
let lines = solidcraft_io::read_dxf(v["dxf"].as_str().unwrap()).unwrap();
|
|
assert!(lines.iter().filter(|g| matches!(g,solidcraft_io::Geom2::Line(a,b) if (a.y-b.y).abs()>19.9)).count() >= 2);
|
|
c.fov = 0.6;
|
|
let v = export(&mut s, c, "dashed");
|
|
assert_eq!(v["projection"], "perspective");
|
|
assert!(v["visible"].as_u64().unwrap() > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn view_dxf_rejects_invalid_options_and_near_eye_geometry_without_panicking() {
|
|
let mut s = sample();
|
|
for extra in [json!({"hidden":"all"}), json!({"hidden":true}), json!({"tolerance":0}), json!({"tolerance":"x"}), json!({"path":false})] {
|
|
let mut p = extra;
|
|
p["camera"] = json!(camera(StandardView::Top));
|
|
assert!(s.execute(CMD, &p).is_err(), "{p}");
|
|
}
|
|
assert!(s.execute(CMD, &json!({})).is_err());
|
|
let mut c = camera(StandardView::Top);
|
|
c.fov = 0.5;
|
|
c.distance = 1.;
|
|
assert!(s.execute(CMD, &json!({"camera":c})).is_err());
|
|
c.fov = 4.;
|
|
assert!(s.execute(CMD, &json!({"camera":c})).is_err());
|
|
}
|