mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 10:47:31 -06:00
fix(engine): frame only visible extents on initial paper layout switch
This commit is contained in:
@@ -395,3 +395,96 @@ fn layout_set_frames_extents_away_from_origin() {
|
||||
assert!((v.center.y - 4450.5).abs() < 1e-6, "center.y={}", v.center.y);
|
||||
assert!(v.height >= 287.0, "height={}", v.height);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layout_set_ignores_off_layer_entities_far_from_visible_content() {
|
||||
let mut s = Session::new();
|
||||
let paper = Space::Paper("COVER".into());
|
||||
{
|
||||
let doc = s.doc_mut().unwrap();
|
||||
doc.layouts.push(cadcraft_doc::Layout::new("COVER", 2));
|
||||
let mut off_layer = cadcraft_doc::Layer::new("HIDDEN_REF");
|
||||
off_layer.on = false;
|
||||
doc.layers.push(off_layer);
|
||||
doc.add(
|
||||
&paper,
|
||||
cadcraft_doc::Common::default(),
|
||||
cadcraft_doc::EntityKind::Polyline3d(cadcraft_doc::Polyline3d {
|
||||
points: vec![
|
||||
cadcraft_geom::Vec3::new(12000.0, 4000.0, 0.0),
|
||||
cadcraft_geom::Vec3::new(12400.0, 4000.0, 0.0),
|
||||
cadcraft_geom::Vec3::new(12400.0, 4300.0, 0.0),
|
||||
cadcraft_geom::Vec3::new(12000.0, 4300.0, 0.0),
|
||||
],
|
||||
closed: true,
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
doc.add(
|
||||
&paper,
|
||||
cadcraft_doc::Common { layer: "HIDDEN_REF".into(), ..Default::default() },
|
||||
cadcraft_doc::EntityKind::Line(cadcraft_doc::Line {
|
||||
a: cadcraft_geom::Vec3::new(1_000_000.0, 1_000_000.0, 0.0),
|
||||
b: cadcraft_geom::Vec3::new(1_000_100.0, 1_000_100.0, 0.0),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
s.execute("layout.set", &json!({"name": "COVER"})).unwrap();
|
||||
let v = s.state().unwrap().view();
|
||||
assert!((v.center.x - 12200.0).abs() < 1e-6, "center.x={}", v.center.x);
|
||||
assert!((v.center.y - 4150.0).abs() < 1e-6, "center.y={}", v.center.y);
|
||||
assert!(v.height >= 300.0 && v.height < 2000.0, "height={}", v.height);
|
||||
|
||||
// Explicit ZOOM Extents continues to include entities on turned-off layers.
|
||||
s.execute("zoom", &json!({"mode": "extents"})).unwrap();
|
||||
let after_zoom = s.state().unwrap().view();
|
||||
assert!(after_zoom.center.x > 100_000.0, "zoom extents center.x={}", after_zoom.center.x);
|
||||
assert!(after_zoom.height > v.height * 100.0, "zoom extents height={}", after_zoom.height);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layout_set_falls_back_to_sheet_when_all_layers_hidden() {
|
||||
let mut s = Session::new();
|
||||
let paper = Space::Paper("ALL_OFF".into());
|
||||
{
|
||||
let doc = s.doc_mut().unwrap();
|
||||
doc.layouts.push(cadcraft_doc::Layout::new("ALL_OFF", 2));
|
||||
let mut off_layer = cadcraft_doc::Layer::new("OFF_LAYER");
|
||||
off_layer.on = false;
|
||||
doc.layers.push(off_layer);
|
||||
let mut frozen_layer = cadcraft_doc::Layer::new("FROZEN_LAYER");
|
||||
frozen_layer.frozen = true;
|
||||
doc.layers.push(frozen_layer);
|
||||
doc.add(
|
||||
&paper,
|
||||
cadcraft_doc::Common { layer: "OFF_LAYER".into(), ..Default::default() },
|
||||
cadcraft_doc::EntityKind::Line(cadcraft_doc::Line {
|
||||
a: cadcraft_geom::Vec3::new(12000.0, 4000.0, 0.0),
|
||||
b: cadcraft_geom::Vec3::new(12400.0, 4300.0, 0.0),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
doc.add(
|
||||
&paper,
|
||||
cadcraft_doc::Common { layer: "FROZEN_LAYER".into(), ..Default::default() },
|
||||
cadcraft_doc::EntityKind::Line(cadcraft_doc::Line {
|
||||
a: cadcraft_geom::Vec3::new(15000.0, 5000.0, 0.0),
|
||||
b: cadcraft_geom::Vec3::new(15400.0, 5300.0, 0.0),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let (expected_center, expected_height) = {
|
||||
let doc = s.doc().unwrap();
|
||||
let page = doc.layout("ALL_OFF").map(|l| l.page.clone()).unwrap_or_default();
|
||||
let (w, h) = if page.landscape { (page.height_mm, page.width_mm) } else { (page.width_mm, page.height_mm) };
|
||||
let unit = if doc.header.i64("MEASUREMENT", 0) == 1 { 1.0 } else { 1.0 / 25.4 };
|
||||
(cadcraft_geom::Vec2::new(w * unit / 2.0, h * unit / 2.0), h * unit * 1.15)
|
||||
};
|
||||
s.execute("layout.set", &json!({"name": "ALL_OFF"})).unwrap();
|
||||
let v = s.state().unwrap().view();
|
||||
assert!((v.center.x - expected_center.x).abs() < 1e-6, "center.x={}", v.center.x);
|
||||
assert!((v.center.y - expected_center.y).abs() < 1e-6, "center.y={}", v.center.y);
|
||||
assert!((v.height - expected_height).abs() < 1e-6, "height={}", v.height);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! View menu: ZOOM (with Realtime and Dynamic), PAN, REGEN, REDRAW, named views (VIEW), the view
|
||||
//! twist (DVIEW TWist), layout switching.
|
||||
|
||||
use cadcraft_doc::{Space, entity_bounds};
|
||||
use cadcraft_doc::{BlockBoundsCache, Drawing, Space, entity_bounds, entity_bounds_cached};
|
||||
use cadcraft_geom::{Bounds2, Vec2};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -306,6 +306,21 @@ fn run_view_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
ok()
|
||||
}
|
||||
|
||||
/// Bounds of the currently visible entities in `space`, excluding entities and block contents
|
||||
/// on layers that are turned off or frozen.
|
||||
fn visible_extents(doc: &Drawing, space: &Space) -> Bounds2 {
|
||||
let mut b = Bounds2::EMPTY;
|
||||
let mut cache = BlockBoundsCache::default();
|
||||
if let Some(store) = doc.space(space) {
|
||||
for e in store.iter() {
|
||||
if doc.is_visible(e) {
|
||||
b = b.union(&entity_bounds_cached(doc, e, 0, &mut cache));
|
||||
}
|
||||
}
|
||||
}
|
||||
b
|
||||
}
|
||||
|
||||
fn run_layout_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = str_param(p, "name").unwrap_or("Model");
|
||||
let space = if name.eq_ignore_ascii_case("model") {
|
||||
@@ -323,7 +338,10 @@ fn run_layout_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Space::Paper(n) = &space
|
||||
&& !st.views.iter().any(|(sp, _)| sp == &space)
|
||||
{
|
||||
let ext = st.doc.extents(&space);
|
||||
// Why: fit only visible content on the automatic initial switch so far-away geometry on
|
||||
// turned-off layers does not push the visible title block out of sight; explicit
|
||||
// ZOOM Extents continues to use `Drawing::extents` (which includes turned-off layers).
|
||||
let ext = visible_extents(&st.doc, &space);
|
||||
if !ext.is_empty() && ext.width().is_finite() && ext.height().is_finite() {
|
||||
let aspect = st.zoom_aspect(vw / vh.max(1.0));
|
||||
let (center, bw, bh) = View::twisted_box(ext, 0.0);
|
||||
|
||||
Reference in New Issue
Block a user