forked from 1b-agent/opencadstudio-upstream
feat: correct viewport scale calculation with correct dimension and text scaling
This commit is contained in:
1 parent
a0aa0f3c51
commit
66243e53ca
6 files changed
+326
-71
No files matched your search
+5
-1
@@ -2493,7 +2493,11 @@ impl H7CAD {
|
||||
vp.center.clone(),
|
||||
vp.width,
|
||||
vp.height,
|
||||
vp.custom_scale,
|
||||
crate::scene::vp_effective_scale(
|
||||
vp.custom_scale,
|
||||
vp.view_height,
|
||||
vp.height,
|
||||
),
|
||||
vp.status.is_on,
|
||||
vp.status.locked,
|
||||
))
|
||||
|
||||
@@ -105,7 +105,12 @@ fn grips(vp: &Viewport) -> Vec<GripDef> {
|
||||
|
||||
fn properties(vp: &Viewport) -> PropSection {
|
||||
let scale_opts: Vec<String> = STANDARD_SCALES.iter().map(|(s, _)| s.to_string()).collect();
|
||||
let current_scale_label = scale_label(vp.custom_scale);
|
||||
let effective_scale = crate::scene::vp_effective_scale(
|
||||
vp.custom_scale,
|
||||
vp.view_height,
|
||||
vp.height,
|
||||
);
|
||||
let current_scale_label = scale_label(effective_scale);
|
||||
|
||||
let view_opts: Vec<String> = STD_VIEWS.iter().map(|s| s.to_string()).collect();
|
||||
let current_view = viewport_view_label(vp);
|
||||
@@ -124,8 +129,8 @@ fn properties(vp: &Viewport) -> PropSection {
|
||||
edit("Center Z", "center_z", vp.center.z),
|
||||
edit("Width", "vp_w", vp.width),
|
||||
edit("Height", "vp_h", vp.height),
|
||||
// Numeric scale entry.
|
||||
edit("Scale (num)", "vscale", vp.custom_scale),
|
||||
// Numeric scale entry (derived from view_height; writes both).
|
||||
edit("Scale (num)", "vscale", effective_scale),
|
||||
// Standard scale picker.
|
||||
Property {
|
||||
label: "Scale".into(),
|
||||
|
||||
+39
-33
@@ -70,6 +70,24 @@ use std::sync::Arc;
|
||||
/// GPU Pipeline to skip re-uploading geometry when switching tabs.
|
||||
static GEOMETRY_EPOCH: AtomicU64 = AtomicU64::new(1);
|
||||
|
||||
/// Resolve a viewport's paper-to-model scale ratio from its two
|
||||
/// DXF-derived sources.
|
||||
///
|
||||
/// `view_height` (model-space view extent) is the canonical source — it
|
||||
/// is what AutoCAD actually uses to draw, and what we keep in sync on
|
||||
/// every write. `custom_scale` is consulted only when `view_height` is
|
||||
/// missing or zero (some third-party exporters omit it).
|
||||
#[inline]
|
||||
pub fn vp_effective_scale(custom_scale: f64, view_height: f64, vp_height: f64) -> f64 {
|
||||
if view_height.abs() > 1e-9 {
|
||||
return vp_height / view_height;
|
||||
}
|
||||
if custom_scale.abs() > 1e-9 {
|
||||
return custom_scale;
|
||||
}
|
||||
1.0
|
||||
}
|
||||
|
||||
/// Pre-built entity caches returned by [`build_derived_caches`].
|
||||
/// Produced in the file-load background task so the UI thread only assigns.
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -839,14 +857,11 @@ impl Scene {
|
||||
self.document.entities().find_map(|e| {
|
||||
if let EntityType::Viewport(vp) = e {
|
||||
if Self::is_content_viewport(vp) && vp.common.owner_handle == layout_block {
|
||||
let scale = if vp.custom_scale.abs() > 1e-9 {
|
||||
vp.custom_scale
|
||||
} else if vp.view_height.abs() > 1e-9 {
|
||||
vp.height / vp.view_height
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
return Some(scale);
|
||||
return Some(vp_effective_scale(
|
||||
vp.custom_scale,
|
||||
vp.view_height,
|
||||
vp.height,
|
||||
));
|
||||
}
|
||||
}
|
||||
None
|
||||
@@ -1821,12 +1836,18 @@ impl Scene {
|
||||
effective_view_center = (self.world_offset[0], self.world_offset[1]);
|
||||
}
|
||||
|
||||
let scale = if effective_view_height.abs() > 1e-9 {
|
||||
// When the saved view did not overlap the model cluster we just
|
||||
// forced `effective_view_height` to a fit value above — that
|
||||
// override must win regardless of the configured priority,
|
||||
// otherwise broken files render blank under custom_scale-first.
|
||||
let scale = if !saved_overlaps {
|
||||
vp.height as f32 / effective_view_height
|
||||
} else if vp.custom_scale.abs() > 1e-9 {
|
||||
vp.custom_scale as f32
|
||||
} else {
|
||||
1.0
|
||||
vp_effective_scale(
|
||||
vp.custom_scale,
|
||||
effective_view_height as f64,
|
||||
vp.height,
|
||||
) as f32
|
||||
};
|
||||
|
||||
let pcx = vp.center.x as f32;
|
||||
@@ -1994,13 +2015,8 @@ impl Scene {
|
||||
Some(acadrust::EntityType::Viewport(vp)) => vp,
|
||||
_ => return paper_pt,
|
||||
};
|
||||
let scale = if vp.custom_scale.abs() > 1e-9 {
|
||||
vp.custom_scale as f32
|
||||
} else if vp.view_height.abs() > 1e-9 {
|
||||
(vp.height / vp.view_height) as f32
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let scale =
|
||||
vp_effective_scale(vp.custom_scale, vp.view_height, vp.height) as f32;
|
||||
if scale.abs() < 1e-9 {
|
||||
return paper_pt;
|
||||
}
|
||||
@@ -2083,13 +2099,8 @@ impl Scene {
|
||||
|
||||
if let Some(cp) = cursor_paper {
|
||||
// Compute the model-space point under the cursor before zoom.
|
||||
let scale_before = if vp.custom_scale.abs() > 1e-9 {
|
||||
vp.custom_scale as f32
|
||||
} else if vp.view_height.abs() > 1e-9 {
|
||||
(vp.height / vp.view_height) as f32
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let scale_before =
|
||||
vp_effective_scale(vp.custom_scale, vp.view_height, vp.height) as f32;
|
||||
let cx = vp.center.x as f32;
|
||||
let cy = vp.center.y as f32;
|
||||
let tx = vp.view_target.x as f32;
|
||||
@@ -4327,13 +4338,8 @@ impl Scene {
|
||||
let (frozen, vp_anno_scale) = match self.document.get_entity(vp_handle) {
|
||||
Some(EntityType::Viewport(vp)) => {
|
||||
let f: HSet<Handle> = vp.frozen_layers.iter().cloned().collect();
|
||||
let vp_scale = if vp.custom_scale.abs() > 1e-9 {
|
||||
vp.custom_scale
|
||||
} else if vp.view_height.abs() > 1e-9 {
|
||||
vp.height / vp.view_height
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let vp_scale =
|
||||
vp_effective_scale(vp.custom_scale, vp.view_height, vp.height);
|
||||
let anno = if vp_scale > 1e-9 {
|
||||
(1.0 / vp_scale) as f32
|
||||
} else {
|
||||
|
||||
+40
-33
@@ -120,17 +120,20 @@ pub fn tessellate(
|
||||
let name = handle.value().to_string();
|
||||
|
||||
// Determine effective annotation scale for this entity.
|
||||
// AutoCAD marks annotative objects with "AcAnnoPO" xdata (R2007+).
|
||||
// If no xdata at all: old file without annotation system → treat as annotative.
|
||||
// If xdata present but no "AcAnnoPO": R2007+ file, entity is non-annotative → no scaling.
|
||||
//
|
||||
// AutoCAD's R2007+ "annotative" system marks objects via extension-
|
||||
// dictionary records or "AcAnnoPO" / "AcAnnotativeData" xdata. Only
|
||||
// entities so marked should be auto-scaled by the viewport's
|
||||
// paper-scale; everything else is treated as manually pre-scaled
|
||||
// (old DXF/DWG convention with $DIMSCALE and oversized text).
|
||||
//
|
||||
// Default: NOT annotative (anno_scale = 1.0). Opt-in via explicit
|
||||
// xdata marker. Files that mark every entity annotative are rare; the
|
||||
// pre-R2007 manual-scale convention is far more common in field data.
|
||||
let anno_scale = {
|
||||
let xdata = &entity.common().extended_data;
|
||||
let is_annotative = if xdata.is_empty() {
|
||||
// Old DXF / no annotation metadata — treat as annotative.
|
||||
true
|
||||
} else {
|
||||
xdata.get_record("AcAnnoPO").is_some()
|
||||
};
|
||||
let is_annotative = xdata.get_record("AcAnnoPO").is_some()
|
||||
|| xdata.get_record("AcAnnotativeData").is_some();
|
||||
if is_annotative {
|
||||
anno_scale
|
||||
} else {
|
||||
@@ -465,22 +468,33 @@ pub fn tessellate_dimension(
|
||||
};
|
||||
let name = handle.value().to_string();
|
||||
let style_name = &dim.base().style_name;
|
||||
let (arrow_size, dimexo, dimexe) = document
|
||||
.dim_styles
|
||||
.iter()
|
||||
.find(|s| {
|
||||
s.name.eq_ignore_ascii_case(style_name)
|
||||
|| (style_name.trim().is_empty() && s.name.eq_ignore_ascii_case("Standard"))
|
||||
let style = document.dim_styles.iter().find(|s| {
|
||||
s.name.eq_ignore_ascii_case(style_name)
|
||||
|| (style_name.trim().is_empty() && s.name.eq_ignore_ascii_case("Standard"))
|
||||
});
|
||||
|
||||
// DIMSCALE rule:
|
||||
// dimstyle.dimscale > 0 → that's the *final* multiplier; ignore anno_scale.
|
||||
// dimstyle.dimscale == 0 → annotative: use anno_scale (= 1/vp_scale in viewports).
|
||||
let dim_scale = style
|
||||
.map(|s| {
|
||||
if s.dimscale > 1e-6 {
|
||||
s.dimscale
|
||||
} else {
|
||||
anno_scale as f64
|
||||
}
|
||||
})
|
||||
.unwrap_or(1.0);
|
||||
let (arrow_size, dimexo, dimexe, dim_txt) = style
|
||||
.map(|s| {
|
||||
let scale = (if s.dimscale > 1e-6 { s.dimscale } else { 1.0 }) * anno_scale as f64;
|
||||
(
|
||||
((s.dimasz * scale) as f32).max(0.001),
|
||||
(s.dimexo * scale) as f32,
|
||||
(s.dimexe * scale) as f32,
|
||||
((s.dimasz * dim_scale) as f32).max(0.001),
|
||||
(s.dimexo * dim_scale) as f32,
|
||||
(s.dimexe * dim_scale) as f32,
|
||||
s.dimtxt * dim_scale,
|
||||
)
|
||||
})
|
||||
.unwrap_or((0.12, 0.0, 0.0));
|
||||
.unwrap_or((0.12, 0.0, 0.0, 2.5));
|
||||
let points = dimension_geometry(dim, arrow_size, dimexo, dimexe, world_offset);
|
||||
let key_vertices = points
|
||||
.iter()
|
||||
@@ -508,7 +522,7 @@ pub fn tessellate_dimension(
|
||||
fill_tris: vec![],
|
||||
}];
|
||||
|
||||
if let Some(text) = dimension_text_entity(dim) {
|
||||
if let Some(text) = dimension_text_entity(dim, dim_txt) {
|
||||
let mut wire = tessellate(
|
||||
document,
|
||||
handle,
|
||||
@@ -519,7 +533,9 @@ pub fn tessellate_dimension(
|
||||
[0.0; 8],
|
||||
line_weight_px,
|
||||
world_offset,
|
||||
anno_scale,
|
||||
// dim text already baked dim_scale into its height — don't
|
||||
// let the inner tessellate re-apply anno_scale.
|
||||
1.0,
|
||||
);
|
||||
wire.name = name;
|
||||
wires.push(wire);
|
||||
@@ -1688,7 +1704,7 @@ fn dimension_snap_pts(dim: &Dimension, world_offset: [f64; 3]) -> Vec<(Vec3, Sna
|
||||
}
|
||||
}
|
||||
|
||||
fn dimension_text_entity(dim: &Dimension) -> Option<Text> {
|
||||
fn dimension_text_entity(dim: &Dimension, text_height: f64) -> Option<Text> {
|
||||
let value = dimension_text_value(dim)?;
|
||||
// Use f64 position directly to avoid f32 round-trip precision loss at large
|
||||
// coordinates (e.g. Turkish UTM ~4,000,000 m). tessellate() will apply
|
||||
@@ -1701,7 +1717,7 @@ fn dimension_text_entity(dim: &Dimension) -> Option<Text> {
|
||||
dimension_text_natural_rotation(dim)
|
||||
};
|
||||
let mut text = Text::with_value(value, pos_f64)
|
||||
.with_height(dimension_text_height(dim))
|
||||
.with_height(text_height)
|
||||
.with_rotation(rotation);
|
||||
text.style = base.style_name.clone();
|
||||
text.common = base.common.clone();
|
||||
@@ -1767,15 +1783,6 @@ fn dimension_text_position(dim: &Dimension, world_offset: [f64; 3]) -> Vec3 {
|
||||
}
|
||||
}
|
||||
|
||||
fn dimension_text_height(dim: &Dimension) -> f64 {
|
||||
let scale = (dim.measurement().abs() * 0.12).clamp(0.25, 2.0);
|
||||
if scale.is_finite() {
|
||||
scale
|
||||
} else {
|
||||
0.25
|
||||
}
|
||||
}
|
||||
|
||||
fn vec3_local(v: Vector3, off: [f64; 3]) -> Vec3 {
|
||||
Vec3::new(
|
||||
(v.x - off[0]) as f32,
|
||||
|
||||
@@ -27,7 +27,7 @@ const COMMON_SCALES: &[(&str, f32, f64)] = &[
|
||||
///
|
||||
/// - `is_model`: true = model space (dispatches SetAnnotationScale), false = paper space (SetViewportScale).
|
||||
/// - `current_anno_scale`: current annotation_scale from Scene (used to highlight active row in model space).
|
||||
/// - `viewport_scale`: current vp.custom_scale (used to highlight in paper space).
|
||||
/// - `viewport_scale`: current effective vp scale, view_height-first (used to highlight in paper space).
|
||||
pub fn scale_popup_overlay(
|
||||
is_model: bool,
|
||||
current_anno_scale: f32,
|
||||
|
||||
Reference in new issue
Block a user