a770fa5a5e5f
layout.rs
use super::ctx::Ctx;
use super::{Bounds2, Drawing, Entity, EntityKind, Handle, HashMap, Layout, Vec2, Vec3, VportConfig, VportTile, W, dxf_ext};
/// Layouts with viewports but without the paper-space viewport (id 1, the sheet itself), keyed
/// by their first viewport. AutoCAD and DWG take a layout's first viewport as its paper-space
/// viewport (DWG does not store viewport ids; readers number them in order), so a layout made in
/// CADCraft, whose paper-space viewport is implicit, gets one written ahead of its viewports.
pub(super) fn paper_views(d: &Drawing) -> HashMap<Handle, usize> {
let mut out = HashMap::new();
'layouts: for (i, l) in d.layouts.iter().enumerate() {
let mut first = None;
for e in l.entities.iter() {
if let EntityKind::Viewport(v) = &e.kind {
if v.id == 1 {
continue 'layouts;
}
first.get_or_insert(e.handle);
}
}
if let Some(h) = first {
out.insert(h, i);
}
}
out
}
/// A layout's entities with its paper-space viewport (id 1) first. DWG stores no viewport ids and
/// readers number a layout's viewports in order, so the sheet must lead even when the draw order
/// (SORTENTS) put it behind a model viewport; otherwise that viewport becomes the sheet.
pub(super) fn sheet_first(l: &Layout) -> Vec<&Entity> {
let sheet = |e: &&Entity| matches!(&e.kind, EntityKind::Viewport(v) if v.id == 1);
let all = || l.entities.iter().map(|e| e.as_ref());
all().filter(sheet).chain(all().filter(|e| !sheet(e))).collect()
}
/// Paper units per paper-space unit of layout `l`: its custom plot scale (groups 142/143), so
/// the sheet is as large in paper space as it plots (a 420 mm sheet at 49.6 mm per unit is 8.47
/// units wide, #2108). 1 when it is scaled to fit or the scale is unusable.
fn sheet_scale(l: &Layout) -> f64 {
let s = l.page.scale;
if l.page.scale_to_fit || !(s.is_finite() && (1e-6..=1e6).contains(&s)) { 1.0 } else { s }
}
/// The paper-space viewport (id 1) of the layout whose first viewport is `e`, if it needs one
/// ([`paper_views`]): the whole sheet, viewed 1:1. Marked with CADCraft xdata so that our reader
/// leaves it implicit again.
pub(super) fn paper_view(w: &mut W, d: &Drawing, e: &Entity, owner: &str, cx: &Ctx) {
let Some(l) = cx.paper_views.get(&e.handle).and_then(|i| d.layouts.get(*i)) else { return };
let paper = cadcraft_render::paper::sheet(d, &l.name).map(|s| s.size).unwrap_or(Vec2::new(12.0, 9.0));
let size = paper / sheet_scale(l);
let center = size * 0.5;
let h = w.h();
w.s(0, "VIEWPORT");
w.s(5, h);
w.s(330, owner);
w.s(100, "AcDbEntity");
w.i(67, 1);
w.s(8, "0");
w.s(100, "AcDbViewport");
w.p(10, center.to3(0.0));
w.f(40, size.x);
w.f(41, size.y);
w.i(68, 1);
w.i(69, 1);
w.p2(12, center);
w.f(45, size.y);
w.i(90, 32768);
w.s(1001, dxf_ext::APP);
w.s(1000, dxf_ext::PAPER_VIEW);
}
/// The VPORT table: the `*Active` model space tiles (one viewport on the extents when there is no
/// tiled configuration) and the saved configurations, each tile a record, the current one first.
pub(super) fn vport_table(w: &mut W, d: &Drawing, ext: Bounds2) {
// The view the drawing is saved at, so that reading a drawing and writing it back keeps it;
// a drawing that stores none is written on its extents. The fallback fits the width as well,
// through the aspect the record is written with: a drawing 100 wide and 10 tall otherwise got
// a view 11 tall, which shows a sliver of it, and a horizontal line got 1e-6 (#1602).
const ASPECT: f64 = 1.6;
// An untiled view's twist: the lone `*Active` tile the editor keeps for it.
let twist = d
.vports
.iter()
.find(|c| c.is_active() && c.tiles.len() == 1)
.and_then(|c| c.tiles.first())
.map_or(0.0, |t| cadcraft_geom::norm_angle(t.twist));
let (center, height) = match d.header.saved_view() {
Some(v) => v,
None if ext.is_empty() => (Vec2::new(6.0, 4.5), 9.0),
None if twist == 0.0 => (ext.center(), (ext.height().max(ext.width() / ASPECT) * 1.1).max(1e-6)),
// The extents fitted as the turned view sees them.
None => {
let r = Bounds2::from_points(ext.corners().map(|p| p.rotate(twist)));
let height = r.height().max(r.width() / ASPECT) * 1.1;
(r.center().rotate(-twist), if height.is_finite() { height.max(1e-6) } else { 9.0 })
}
};
let single = VportTile { min: Vec2::ZERO, max: Vec2::new(1.0, 1.0), center, height, aspect: ASPECT, twist };
let tiled = d.vports.iter().find(|c| c.is_active() && c.tiles.len() > 1);
// The first `*ACTIVE` record is the one a reader takes the view from, so it carries the saved
// view while keeping its own place on the screen and the tiles after it unchanged. Writing
// the stored tiles as they stand put the new view in HEADER and the old one here, and the
// read takes this one, so updating the view of a tiled drawing did not survive a round trip
// (noted in review).
//
// Only where there is a saved view to carry. `center` and `height` are the extents fallback
// when the header holds none, and a tiled drawing can hold none -- `-VPORTS` splits the
// current view into tiles without setting one -- so overriding regardless replaced a tile's
// own good view with that fallback, and the drawing opened on the extents instead of where
// its tile was (noted in review).
let saved = d.header.saved_view().is_some();
let first = tiled.and_then(|c| c.tiles.first()).map(|t| if saved { VportTile { center, height, ..*t } } else { *t });
let mut records: Vec<(&str, &VportTile)> = match (tiled, first.as_ref()) {
(Some(c), Some(first)) => {
std::iter::once((VportConfig::ACTIVE, first)).chain(c.tiles.iter().skip(1).map(|t| (VportConfig::ACTIVE, t))).collect()
}
_ => vec![(VportConfig::ACTIVE, &single)],
};
records.extend(d.vports.iter().filter(|c| !c.is_active() && !c.name.is_empty()).flat_map(|c| c.tiles.iter().map(|t| (c.name.as_str(), t))));
let th = table_head(w, "VPORT", records.len());
let finite = |p: Vec2, or: Vec2| if p.is_finite() { p } else { or };
for (name, t) in records {
record_head(w, "VPORT", &th, "AcDbViewportTableRecord");
w.s(2, name);
w.i(70, 0);
w.p2(10, finite(t.min, Vec2::ZERO));
w.p2(11, finite(t.max, Vec2::new(1.0, 1.0)));
let twist = cadcraft_geom::norm_angle(t.twist);
w.p2(12, dxf_ext::model_center_to_dcs(finite(t.center, Vec2::ZERO), twist));
// Snap grid origin and rotation (SNAPBASE, SNAPANG in degrees): R13+ files keep them here.
w.p2(13, d.header.point("SNAPBASE").map(|p| p.xy()).filter(|p| p.is_finite()).unwrap_or(Vec2::ZERO));
w.p2(14, d.header.snap_unit());
w.p2(15, d.header.grid_unit());
w.p(16, Vec3::Z);
w.p(17, Vec3::ZERO);
w.f(40, if t.height.is_finite() && t.height > 0.0 { t.height } else { 1.0 });
w.f(41, if t.aspect.is_finite() && t.aspect > 0.0 { t.aspect } else { 1.0 });
w.f(42, 50.0);
w.f(50, d.header.f64("SNAPANG", 0.0));
w.f(51, twist.to_degrees());
}
w.s(0, "ENDTAB");
}
pub(super) fn table_head(w: &mut W, name: &str, count: usize) -> String {
let h = w.h();
w.s(0, "TABLE");
w.s(2, name);
w.s(5, h.clone());
w.s(330, "0");
w.s(100, "AcDbSymbolTable");
w.i(70, count as i64);
h
}
pub(super) fn record_head(w: &mut W, kind: &str, owner: &str, subclass: &str) -> String {
let h = w.h();
w.s(0, kind);
w.s(if kind == "DIMSTYLE" { 105 } else { 5 }, h.clone());
w.s(330, owner);
w.s(100, "AcDbSymbolTableRecord");
w.s(100, subclass);
h
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.