Carry hatch origin through creation and pattern editing

This commit is contained in:
ramox81 committed 2026-09-11 13:40:40 +03:00
1 parent dbd85c0559
commit 6272722baa
11 files changed
+42 -85

No files matched your search

+5 -32
View File
@@ -4545,11 +4545,6 @@ impl OpenCADStudio {
if let Some(entry) = if let Some(entry) =
crate::scene::model::hatch_patterns::find(&name) crate::scene::model::hatch_patterns::find(&name)
{ {
let old_origin = hatch
.pattern
.lines
.first()
.map(|line| line.base_point);
let mut pattern = crate::scene::model::hatch_patterns::build_dxf_pattern(entry); let mut pattern = crate::scene::model::hatch_patterns::build_dxf_pattern(entry);
crate::entities::hatch::scale_pattern_geometry( crate::entities::hatch::scale_pattern_geometry(
&mut pattern, &mut pattern,
@@ -4559,16 +4554,8 @@ impl OpenCADStudio {
&mut pattern, &mut pattern,
(angle as f64).to_radians(), (angle as f64).to_radians(),
); );
if let (Some(old), Some(new)) = ( let origin = hatch.pattern_origin();
old_origin, crate::entities::hatch::translate_pattern_geometry(&mut pattern, origin.x, origin.y);
pattern.lines.first().map(|line| line.base_point),
) {
crate::entities::hatch::translate_pattern_geometry(
&mut pattern,
old.x - new.x,
old.y - new.y,
);
}
hatch.pattern = pattern; hatch.pattern = pattern;
hatch.is_solid = matches!( hatch.is_solid = matches!(
entry.gpu, entry.gpu,
@@ -4582,30 +4569,16 @@ impl OpenCADStudio {
let requested_scale = scale.max(1.0e-6) as f64; let requested_scale = scale.max(1.0e-6) as f64;
if hatch.pattern_scale > 1.0e-12 { if hatch.pattern_scale > 1.0e-12 {
let factor = requested_scale / hatch.pattern_scale; let factor = requested_scale / hatch.pattern_scale;
crate::entities::hatch::scale_pattern_geometry( hatch.scale_pattern_about_origin(factor);
&mut hatch.pattern,
factor,
);
} }
let requested_angle = (angle as f64).to_radians(); let requested_angle = (angle as f64).to_radians();
let delta = requested_angle - hatch.pattern_angle; let delta = requested_angle - hatch.pattern_angle;
crate::entities::hatch::rotate_pattern_geometry( hatch.rotate_pattern_about_origin(delta);
&mut hatch.pattern,
delta,
);
} }
hatch.pattern_scale = scale.max(1.0e-6) as f64; hatch.pattern_scale = scale.max(1.0e-6) as f64;
hatch.pattern_angle = (angle as f64).to_radians(); hatch.pattern_angle = (angle as f64).to_radians();
if let Some((x, y)) = origin { if let Some((x, y)) = origin {
if let Some(current) = hatch.set_pattern_origin(acadrust::types::Vector2::new(x, y));
hatch.pattern.lines.first().map(|line| line.base_point)
{
crate::entities::hatch::translate_pattern_geometry(
&mut hatch.pattern,
x - current.x,
y - current.y,
);
}
} }
if disassociate { if disassociate {
for path in &mut hatch.paths { for path in &mut hatch.paths {
+2 -14
View File
@@ -1997,9 +1997,6 @@ pub(super) fn on_tab_close(&mut self, idx: usize) -> Task<Message> {
if let Some(acadrust::EntityType::Hatch(dxf)) = if let Some(acadrust::EntityType::Hatch(dxf)) =
self.tabs[i].scene.document.get_entity_mut(handle) self.tabs[i].scene.document.get_entity_mut(handle)
{ {
let old_origin = dxf.pattern.lines.first().map(|line| {
(line.base_point.x, line.base_point.y)
});
let mut pattern = hatch_patterns::build_dxf_pattern(entry); let mut pattern = hatch_patterns::build_dxf_pattern(entry);
// Stored pattern lines are final world-space // Stored pattern lines are final world-space
// geometry. Preserve the selected hatch's scale, // geometry. Preserve the selected hatch's scale,
@@ -2013,17 +2010,8 @@ pub(super) fn on_tab_close(&mut self, idx: usize) -> Task<Message> {
&mut pattern, &mut pattern,
dxf.pattern_angle, dxf.pattern_angle,
); );
if let (Some((old_x, old_y)), Some(new_origin)) = let origin = dxf.pattern_origin();
(old_origin, pattern.lines.first()) crate::entities::hatch::translate_pattern_geometry(&mut pattern, origin.x, origin.y);
{
let dx = old_x - new_origin.base_point.x;
let dy = old_y - new_origin.base_point.y;
crate::entities::hatch::translate_pattern_geometry(
&mut pattern,
dx,
dy,
);
}
dxf.pattern = pattern; dxf.pattern = pattern;
dxf.is_solid = matches!( dxf.is_solid = matches!(
entry.gpu, entry.gpu,
+19 -39
View File
@@ -182,18 +182,14 @@ fn boundary_centroid(h: &Hatch) -> Option<(f64, f64)> {
(n > 0.0).then(|| (sx / n, sy / n)) (n > 0.0).then(|| (sx / n, sy / n))
} }
/// Scale the stored, final hatch-pattern geometry around the pattern origin. /// Scale a catalog pattern about its intrinsic coordinate origin.
/// Pattern lines loaded from DXF/DWG are rendered prebaked, so their metadata /// Pattern lines loaded from DXF/DWG are rendered prebaked, so their metadata
/// scale is not applied again by the renderer. /// scale is not applied again by the renderer.
pub(crate) fn scale_pattern_geometry( pub(crate) fn scale_pattern_geometry(
pattern: &mut acadrust::entities::HatchPattern, pattern: &mut acadrust::entities::HatchPattern,
factor: f64, factor: f64,
) { ) {
let (origin_x, origin_y) = pattern let (origin_x, origin_y) = (0.0, 0.0);
.lines
.first()
.map(|line| (line.base_point.x, line.base_point.y))
.unwrap_or((0.0, 0.0));
for line in pattern.lines.iter_mut() { for line in pattern.lines.iter_mut() {
line.base_point.x = origin_x + (line.base_point.x - origin_x) * factor; line.base_point.x = origin_x + (line.base_point.x - origin_x) * factor;
line.base_point.y = origin_y + (line.base_point.y - origin_y) * factor; line.base_point.y = origin_y + (line.base_point.y - origin_y) * factor;
@@ -205,18 +201,14 @@ pub(crate) fn scale_pattern_geometry(
} }
} }
/// Rotate the stored, final hatch-pattern geometry around the pattern origin. /// Rotate a catalog pattern about its intrinsic coordinate origin.
/// This keeps the line angle, base point and world-space offset in sync. /// This keeps the line angle, base point and world-space offset in sync.
pub(crate) fn rotate_pattern_geometry( pub(crate) fn rotate_pattern_geometry(
pattern: &mut acadrust::entities::HatchPattern, pattern: &mut acadrust::entities::HatchPattern,
angle: f64, angle: f64,
) { ) {
let (sin, cos) = angle.sin_cos(); let (sin, cos) = angle.sin_cos();
let (origin_x, origin_y) = pattern let (origin_x, origin_y) = (0.0, 0.0);
.lines
.first()
.map(|line| (line.base_point.x, line.base_point.y))
.unwrap_or((0.0, 0.0));
for line in pattern.lines.iter_mut() { for line in pattern.lines.iter_mut() {
line.angle += angle; line.angle += angle;
let (x, y) = ( let (x, y) = (
@@ -613,7 +605,7 @@ fn apply_geom_prop(h: &mut Hatch, field: &str, value: &str) {
_ => h.pattern_type, _ => h.pattern_type,
}; };
if requested != h.pattern_type { if requested != h.pattern_type {
let old_origin = h.pattern.lines.first().map(|line| line.base_point); let old_origin = h.pattern_origin();
h.pattern_type = requested; h.pattern_type = requested;
h.is_solid = false; h.is_solid = false;
match requested { match requested {
@@ -629,15 +621,7 @@ fn apply_geom_prop(h: &mut Hatch, field: &str, value: &str) {
crate::scene::model::hatch_patterns::build_dxf_pattern(entry); crate::scene::model::hatch_patterns::build_dxf_pattern(entry);
scale_pattern_geometry(&mut pattern, h.pattern_scale); scale_pattern_geometry(&mut pattern, h.pattern_scale);
rotate_pattern_geometry(&mut pattern, h.pattern_angle); rotate_pattern_geometry(&mut pattern, h.pattern_angle);
if let (Some(old), Some(new)) = translate_pattern_geometry(&mut pattern, old_origin.x, old_origin.y);
(old_origin, pattern.lines.first().map(|line| line.base_point))
{
translate_pattern_geometry(
&mut pattern,
old.x - new.x,
old.y - new.y,
);
}
h.pattern = pattern; h.pattern = pattern;
} }
} }
@@ -695,7 +679,7 @@ fn apply_geom_prop(h: &mut Hatch, field: &str, value: &str) {
"pattern_angle" => { "pattern_angle" => {
let angle = v.to_radians(); let angle = v.to_radians();
let delta = angle - h.pattern_angle; let delta = angle - h.pattern_angle;
rotate_pattern_geometry(&mut h.pattern, delta); h.rotate_pattern_about_origin(delta);
h.pattern_angle = angle; h.pattern_angle = angle;
} }
// The stored pattern lines are the FINAL rendered geometry (offsets, // The stored pattern lines are the FINAL rendered geometry (offsets,
@@ -706,7 +690,7 @@ fn apply_geom_prop(h: &mut Hatch, field: &str, value: &str) {
let old = h.pattern_scale; let old = h.pattern_scale;
if old > 1e-12 { if old > 1e-12 {
let k = v / old; let k = v / old;
scale_pattern_geometry(&mut h.pattern, k); h.scale_pattern_about_origin(k);
} }
h.pattern_scale = v; h.pattern_scale = v;
} }
@@ -724,19 +708,17 @@ fn apply_geom_prop(h: &mut Hatch, field: &str, value: &str) {
} }
// Origin rows are relative offsets and return to zero after commit. // Origin rows are relative offsets and return to zero after commit.
"origin_x" => { "origin_x" => {
for line in h.pattern.lines.iter_mut() { let origin = h.pattern_origin();
line.base_point.x += v; h.set_pattern_origin(acadrust::types::Vector2::new(origin.x + v, origin.y));
}
} }
"origin_y" => { "origin_y" => {
for line in h.pattern.lines.iter_mut() { let origin = h.pattern_origin();
line.base_point.y += v; h.set_pattern_origin(acadrust::types::Vector2::new(origin.x, origin.y + v));
}
} }
"iso_pen_width" if v > 0.0 => { "iso_pen_width" if v > 0.0 => {
let old = h.pattern_scale; let old = h.pattern_scale;
if old > 1e-12 { if old > 1e-12 {
scale_pattern_geometry(&mut h.pattern, v / old); h.scale_pattern_about_origin(v / old);
} }
h.pattern_scale = v; h.pattern_scale = v;
} }
@@ -876,10 +858,8 @@ impl Grippable for Hatch {
GripApply::Absolute(point) => (point.x - gx, point.y - gy), GripApply::Absolute(point) => (point.x - gx, point.y - gy),
GripApply::Translate(delta) => (delta.x, delta.y), GripApply::Translate(delta) => (delta.x, delta.y),
}; };
for line in self.pattern.lines.iter_mut() { let origin = self.pattern_origin();
line.base_point.x += dx; self.set_pattern_origin(acadrust::types::Vector2::new(origin.x + dx, origin.y + dy));
line.base_point.y += dy;
}
return; return;
} }
id += 1; id += 1;
@@ -1005,11 +985,11 @@ impl Grippable for Hatch {
fn apply_grip_menu(&mut self, grip_id: usize, action: crate::scene::model::object::GripMenuAction) { fn apply_grip_menu(&mut self, grip_id: usize, action: crate::scene::model::object::GripMenuAction) {
use crate::scene::model::object::GripMenuAction as A; use crate::scene::model::object::GripMenuAction as A;
if grip_id == 0 && matches!(action, A::OriginPoint) { if grip_id == 0 && matches!(action, A::OriginPoint) {
if let (Some((gx, gy)), Some((ox, oy))) = ( if let (Some((gx, gy)), Some((_ox, _oy))) = (
boundary_centroid(self), boundary_centroid(self),
self.pattern.lines.first().map(|line| (line.base_point.x, line.base_point.y)), self.pattern.lines.first().map(|line| (line.base_point.x, line.base_point.y)),
) { ) {
translate_pattern_geometry(&mut self.pattern, gx - ox, gy - oy); self.set_pattern_origin(acadrust::types::Vector2::new(gx, gy));
} }
} }
} }
@@ -1038,14 +1018,14 @@ impl Grippable for Hatch {
A::HatchAngle => { A::HatchAngle => {
let angle = value.to_radians(); let angle = value.to_radians();
let delta = angle - self.pattern_angle; let delta = angle - self.pattern_angle;
rotate_pattern_geometry(&mut self.pattern, delta); self.rotate_pattern_about_origin(delta);
self.pattern_angle = angle; self.pattern_angle = angle;
} }
A::HatchScale => { A::HatchScale => {
if value > 0.0 { if value > 0.0 {
if self.pattern_scale > 1e-12 { if self.pattern_scale > 1e-12 {
let factor = value / self.pattern_scale; let factor = value / self.pattern_scale;
scale_pattern_geometry(&mut self.pattern, factor); self.scale_pattern_about_origin(factor);
} }
self.pattern_scale = value; self.pattern_scale = value;
} }
+1
View File
@@ -927,6 +927,7 @@ fn emit_wire_fills(
} }
boundary.push(boundary[0]); boundary.push(boundary[0]);
let hatch = HatchModel { let hatch = HatchModel {
pattern_origin: None,
render_instance: wire.render_instance.clone(), render_instance: wire.render_instance.clone(),
world_origin: [0.0, 0.0], world_origin: [0.0, 0.0],
boundary: std::sync::Arc::new(boundary), boundary: std::sync::Arc::new(boundary),
+3
View File
@@ -387,6 +387,7 @@ impl HatchCommand {
} }
} }
return HatchModel { return HatchModel {
pattern_origin: source.pattern_origin,
render_instance: None, render_instance: None,
boundary: std::sync::Arc::new(rel), boundary: std::sync::Arc::new(rel),
pattern, pattern,
@@ -445,6 +446,7 @@ impl HatchCommand {
} }
} }
HatchModel { HatchModel {
pattern_origin: Some(self.default_origin),
render_instance: None, render_instance: None,
boundary: std::sync::Arc::new(rel), boundary: std::sync::Arc::new(rel),
pattern, pattern,
@@ -969,6 +971,7 @@ impl GradientCommand {
1.0e-6, 1.0e-6,
); );
HatchModel { HatchModel {
pattern_origin: None,
render_instance: None, render_instance: None,
boundary: std::sync::Arc::new(rel), boundary: std::sync::Arc::new(rel),
pattern: HatchPattern::Gradient { pattern: HatchPattern::Gradient {
+6
View File
@@ -1674,6 +1674,7 @@ impl Scene {
let fill_plane_boundary = Some(Arc::new(fill_plane.1)); let fill_plane_boundary = Some(Arc::new(fill_plane.1));
let fill_plane = Some(fill_plane.0); let fill_plane = Some(fill_plane.0);
models.push(HatchModel { models.push(HatchModel {
pattern_origin: None,
render_instance, render_instance,
boundary: Arc::new(boundary), boundary: Arc::new(boundary),
boundary_wcs: None, boundary_wcs: None,
@@ -2107,6 +2108,7 @@ impl Scene {
let storage = crate::entities::curve::ocs_plane(dxf.normal, dxf.elevation); let storage = crate::entities::curve::ocs_plane(dxf.normal, dxf.elevation);
Some(HatchModel { Some(HatchModel {
pattern_origin: Some([dxf.pattern_origin().x, dxf.pattern_origin().y]),
render_instance: None, render_instance: None,
boundary: std::sync::Arc::new(boundary_f32), boundary: std::sync::Arc::new(boundary_f32),
boundary_wcs: None, boundary_wcs: None,
@@ -2429,6 +2431,7 @@ impl Scene {
}) })
.collect(); .collect();
HatchModel { HatchModel {
pattern_origin: None,
render_instance: None, render_instance: None,
boundary: std::sync::Arc::new(boundary), boundary: std::sync::Arc::new(boundary),
boundary_wcs: None, boundary_wcs: None,
@@ -2663,6 +2666,9 @@ impl Scene {
// carries `world_origin: [0, 0]`, which after the world_offset removal // carries `world_origin: [0, 0]`, which after the world_offset removal
// leaves the fill mis-placed and effectively invisible until a later // leaves the fill mis-placed and effectively invisible until a later
// edit rebuilds it from the DXF — so keep the seed, don't overwrite it. // edit rebuilds it from the DXF — so keep the seed, don't overwrite it.
if let Some(origin) = model.pattern_origin {
dxf.record_pattern_origin(Vector2::new(origin[0], origin[1]));
}
let mut entity = EntityType::Hatch(dxf); let mut entity = EntityType::Hatch(dxf);
if let Some(layer) = layer { if let Some(layer) = layer {
+1
View File
@@ -3794,6 +3794,7 @@ impl Scene {
let ((x0, y0), (x1, y1)) = self.paper_limits()?; let ((x0, y0), (x1, y1)) = self.paper_limits()?;
let (x0, y0, x1, y1) = (x0 as f32, y0 as f32, x1 as f32, y1 as f32); let (x0, y0, x1, y1) = (x0 as f32, y0 as f32, x1 as f32, y1 as f32);
Some(HatchModel { Some(HatchModel {
pattern_origin: None,
render_instance: None, render_instance: None,
world_origin: [0.0, 0.0], world_origin: [0.0, 0.0],
boundary: Arc::new(vec![[x0, y0], [x1, y0], [x1, y1], [x0, y1], [x0, y0]]), boundary: Arc::new(vec![[x0, y0], [x1, y0], [x1, y1], [x0, y1], [x0, y0]]),
+2
View File
@@ -208,6 +208,8 @@ pub struct FillPlane {
/// A hatched region defined by a closed polygon boundary. /// A hatched region defined by a closed polygon boundary.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct HatchModel { pub struct HatchModel {
/// Recorded hatch origin in its fill plane, independent of intrinsic PAT line bases.
pub pattern_origin: Option<[f64; 2]>,
pub render_instance: Option<super::instance_model::RenderInstance>, pub render_instance: Option<super::instance_model::RenderInstance>,
/// World XY anchor (in the same offset-relative coordinate space as /// World XY anchor (in the same offset-relative coordinate space as
/// the rest of the scene — `world_offset` already subtracted, but /// the rest of the scene — `world_offset` already subtracted, but
+1
View File
@@ -154,6 +154,7 @@ impl Scene {
return None; return None;
} }
let mut model = HatchModel { let mut model = HatchModel {
pattern_origin: None,
render_instance: None, render_instance: None,
world_origin: origin, world_origin: origin,
boundary: std::sync::Arc::new(boundary), boundary: std::sync::Arc::new(boundary),
+1
View File
@@ -2886,6 +2886,7 @@ impl Scene {
} }
boundary.push(boundary[0]); boundary.push(boundary[0]);
hatches.push(HatchModel { hatches.push(HatchModel {
pattern_origin: None,
render_instance: wire.render_instance.clone(), render_instance: wire.render_instance.clone(),
world_origin: [0.0, 0.0], world_origin: [0.0, 0.0],
boundary: Arc::new(boundary), boundary: Arc::new(boundary),
+1
View File
@@ -131,6 +131,7 @@ impl canvas::Program<Message> for HatchPatternPreview {
} }
HatchPattern::Pattern(_) => { HatchPattern::Pattern(_) => {
let model = HatchModel { let model = HatchModel {
pattern_origin: None,
render_instance: None, render_instance: None,
world_origin: [0.0, 0.0], world_origin: [0.0, 0.0],
boundary: Arc::new(vec![ boundary: Arc::new(vec![