forked from 1b-agent/opencadstudio-upstream
feat(ucs): build axis-aligned shapes in the active UCS
Commands that construct geometry with implicit axis directions ignored the UCS, so a rectangle drawn under a rotated UCS came out world-aligned. Give commands the UCS and build square to it: - CadCommand::set_ucs(Mat4) (UCS→wire affine, default no-op); the host pushes the current UCS via App::push_ucs_to_cmd before each point / preview / text dispatch (DocumentTab::ucs_wire_affine, identity outside model space). New axis-aligned commands just override set_ucs. - RECT (two-corner) and RECT_CEN (center) place their derived corners on the UCS axes (ucs_box_corners / ucs_box_around_center). - ARRAYRECT row/column offsets run along the UCS axes. RECT_ROT, POLYGON, and ELLIPSE already take their orientation from picked points, so they need no change. Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
1 parent
d63602161f
commit
0c2a97e17e
5 files changed
+128
-35
No files matched your search
@@ -17,7 +17,32 @@ use acadrust::{EntityType, LwPolyline};
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::modules::IconKind;
|
||||
use crate::scene::model::wire_model::WireModel;
|
||||
use glam::Vec3;
|
||||
use glam::{Mat4, Vec3};
|
||||
|
||||
/// Build the four corners of an axis-aligned box between opposite corners `a`
|
||||
/// and `b`, axis-aligned in the active UCS (`ucs` = UCS→wire affine, identity =
|
||||
/// world). The two given corners stay put; the other two are placed square to
|
||||
/// the UCS axes instead of the world axes.
|
||||
fn ucs_box_corners(a: Vec3, b: Vec3, ucs: Mat4) -> [Vec3; 4] {
|
||||
let inv = ucs.inverse();
|
||||
let au = inv.transform_point3(a);
|
||||
let bu = inv.transform_point3(b);
|
||||
[
|
||||
ucs.transform_point3(Vec3::new(au.x, au.y, au.z)),
|
||||
ucs.transform_point3(Vec3::new(bu.x, au.y, au.z)),
|
||||
ucs.transform_point3(Vec3::new(bu.x, bu.y, au.z)),
|
||||
ucs.transform_point3(Vec3::new(au.x, bu.y, au.z)),
|
||||
]
|
||||
}
|
||||
|
||||
/// Four corners of a box centred at `c` with half-extents taken from `corner`,
|
||||
/// axis-aligned in the active UCS (`ucs` = UCS→wire affine, identity = world).
|
||||
fn ucs_box_around_center(c: Vec3, corner: Vec3, ucs: Mat4) -> [Vec3; 4] {
|
||||
let d = ucs.inverse().transform_vector3(corner - c);
|
||||
let rx = ucs.transform_vector3(Vec3::new(d.x.abs(), 0.0, 0.0));
|
||||
let ry = ucs.transform_vector3(Vec3::new(0.0, d.y.abs(), 0.0));
|
||||
[c - rx - ry, c + rx - ry, c + rx + ry, c - rx + ry]
|
||||
}
|
||||
|
||||
const TAU: f32 = std::f32::consts::TAU;
|
||||
const PI: f32 = std::f32::consts::PI;
|
||||
@@ -120,11 +145,12 @@ fn angle_xy(from: Vec3, to: Vec3) -> f32 {
|
||||
|
||||
pub struct RectCommand {
|
||||
a: Option<Vec3>,
|
||||
ucs: Mat4,
|
||||
}
|
||||
|
||||
impl RectCommand {
|
||||
pub fn new() -> Self {
|
||||
Self { a: None }
|
||||
Self { a: None, ucs: Mat4::IDENTITY }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +158,9 @@ impl CadCommand for RectCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"RECT"
|
||||
}
|
||||
fn set_ucs(&mut self, ucs: Mat4) {
|
||||
self.ucs = ucs;
|
||||
}
|
||||
fn prompt(&self) -> String {
|
||||
if self.a.is_none() {
|
||||
"RECT Specify first corner:".into()
|
||||
@@ -145,12 +174,15 @@ impl CadCommand for RectCommand {
|
||||
self.a = Some(pt);
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
Some(a) => CmdResult::CommitAndExit(make_pline(&[
|
||||
[a.x as f64, a.y as f64],
|
||||
[pt.x as f64, a.y as f64],
|
||||
[pt.x as f64, pt.y as f64],
|
||||
[a.x as f64, pt.y as f64],
|
||||
])),
|
||||
Some(a) => {
|
||||
let c = ucs_box_corners(a, pt, self.ucs);
|
||||
CmdResult::CommitAndExit(make_pline(&[
|
||||
[c[0].x as f64, c[0].y as f64],
|
||||
[c[1].x as f64, c[1].y as f64],
|
||||
[c[2].x as f64, c[2].y as f64],
|
||||
[c[3].x as f64, c[3].y as f64],
|
||||
]))
|
||||
}
|
||||
}
|
||||
}
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
@@ -161,11 +193,12 @@ impl CadCommand for RectCommand {
|
||||
}
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
let a = self.a?;
|
||||
let c = ucs_box_corners(a, pt, self.ucs);
|
||||
Some(wire_loop(vec![
|
||||
[a.x, a.y, a.z],
|
||||
[pt.x, a.y, a.z],
|
||||
[pt.x, pt.y, a.z],
|
||||
[a.x, pt.y, a.z],
|
||||
[c[0].x, c[0].y, c[0].z],
|
||||
[c[1].x, c[1].y, c[1].z],
|
||||
[c[2].x, c[2].y, c[2].z],
|
||||
[c[3].x, c[3].y, c[3].z],
|
||||
]))
|
||||
}
|
||||
fn dyn_spec(&self) -> Option<crate::command::DynSpec> {
|
||||
@@ -302,11 +335,12 @@ impl CadCommand for RectRotCommand {
|
||||
|
||||
pub struct RectCenCommand {
|
||||
center: Option<Vec3>,
|
||||
ucs: Mat4,
|
||||
}
|
||||
|
||||
impl RectCenCommand {
|
||||
pub fn new() -> Self {
|
||||
Self { center: None }
|
||||
Self { center: None, ucs: Mat4::IDENTITY }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,6 +348,9 @@ impl CadCommand for RectCenCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"RECT_CEN"
|
||||
}
|
||||
fn set_ucs(&mut self, ucs: Mat4) {
|
||||
self.ucs = ucs;
|
||||
}
|
||||
fn prompt(&self) -> String {
|
||||
if self.center.is_none() {
|
||||
"RECT CEN Specify center point:".into()
|
||||
@@ -328,15 +365,12 @@ impl CadCommand for RectCenCommand {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
Some(c) => {
|
||||
let hw = (pt.x - c.x).abs() as f64;
|
||||
let hh = (pt.y - c.y).abs() as f64;
|
||||
let cx = c.x as f64;
|
||||
let cy = c.y as f64;
|
||||
let q = ucs_box_around_center(c, pt, self.ucs);
|
||||
CmdResult::CommitAndExit(make_pline(&[
|
||||
[cx - hw, cy - hh],
|
||||
[cx + hw, cy - hh],
|
||||
[cx + hw, cy + hh],
|
||||
[cx - hw, cy + hh],
|
||||
[q[0].x as f64, q[0].y as f64],
|
||||
[q[1].x as f64, q[1].y as f64],
|
||||
[q[2].x as f64, q[2].y as f64],
|
||||
[q[3].x as f64, q[3].y as f64],
|
||||
]))
|
||||
}
|
||||
}
|
||||
@@ -349,13 +383,12 @@ impl CadCommand for RectCenCommand {
|
||||
}
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
let c = self.center?;
|
||||
let hw = (pt.x - c.x).abs();
|
||||
let hh = (pt.y - c.y).abs();
|
||||
let q = ucs_box_around_center(c, pt, self.ucs);
|
||||
Some(wire_loop(vec![
|
||||
[c.x - hw, c.y - hh, c.z],
|
||||
[c.x + hw, c.y - hh, c.z],
|
||||
[c.x + hw, c.y + hh, c.z],
|
||||
[c.x - hw, c.y + hh, c.z],
|
||||
[q[0].x, q[0].y, q[0].z],
|
||||
[q[1].x, q[1].y, q[1].z],
|
||||
[q[2].x, q[2].y, q[2].z],
|
||||
[q[3].x, q[3].y, q[3].z],
|
||||
]))
|
||||
}
|
||||
fn dyn_spec(&self) -> Option<crate::command::DynSpec> {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
// 1. Center point → 2. Item count (text) → 3. Total angle in degrees (text)
|
||||
|
||||
use acadrust::Handle;
|
||||
use glam::Vec3;
|
||||
use glam::{Mat4, Vec3};
|
||||
|
||||
use crate::command::{CadCommand, CmdResult, EntityTransform};
|
||||
use crate::modules::draw::defaults;
|
||||
@@ -62,6 +62,7 @@ pub struct ArrayRectCommand {
|
||||
default_cols: u32,
|
||||
default_row_sp: f32,
|
||||
default_col_sp: f32,
|
||||
ucs: Mat4,
|
||||
}
|
||||
|
||||
impl ArrayRectCommand {
|
||||
@@ -74,20 +75,27 @@ impl ArrayRectCommand {
|
||||
default_cols: defaults::get_array_cols() as u32,
|
||||
default_row_sp: defaults::get_array_row_sp(),
|
||||
default_col_sp: defaults::get_array_col_sp(),
|
||||
ucs: Mat4::IDENTITY,
|
||||
}
|
||||
}
|
||||
|
||||
fn build_transforms(rows: u32, cols: u32, row_sp: f32, col_sp: f32) -> Vec<EntityTransform> {
|
||||
/// Row/column offsets run along the active UCS axes (`ucs` rotates the
|
||||
/// world-frame grid step), identity = world.
|
||||
fn build_transforms(
|
||||
rows: u32,
|
||||
cols: u32,
|
||||
row_sp: f32,
|
||||
col_sp: f32,
|
||||
ucs: Mat4,
|
||||
) -> Vec<EntityTransform> {
|
||||
let mut t = Vec::new();
|
||||
for r in 0..rows {
|
||||
for c in 0..cols {
|
||||
if r == 0 && c == 0 {
|
||||
continue;
|
||||
}
|
||||
t.push(EntityTransform::Translate(Vec3::new(
|
||||
col_sp * c as f32,
|
||||
row_sp * r as f32,
|
||||
0.0,
|
||||
t.push(EntityTransform::Translate(ucs.transform_vector3(
|
||||
Vec3::new(col_sp * c as f32, row_sp * r as f32, 0.0),
|
||||
)));
|
||||
}
|
||||
}
|
||||
@@ -100,6 +108,10 @@ impl CadCommand for ArrayRectCommand {
|
||||
"ARRAYRECT"
|
||||
}
|
||||
|
||||
fn set_ucs(&mut self, ucs: Mat4) {
|
||||
self.ucs = ucs;
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
match self.step {
|
||||
RectStep::Rows => format!("ARRAYRECT Enter row count <{}>:", self.default_rows),
|
||||
@@ -176,7 +188,7 @@ impl CadCommand for ArrayRectCommand {
|
||||
};
|
||||
Some(CmdResult::BatchCopy(
|
||||
self.handles.clone(),
|
||||
Self::build_transforms(rows, cols, row_sp, col_sp),
|
||||
Self::build_transforms(rows, cols, row_sp, col_sp, self.ucs),
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -201,7 +213,7 @@ impl CadCommand for ArrayRectCommand {
|
||||
}
|
||||
RectStep::ColSp { rows, cols, row_sp } => (rows, cols, row_sp, self.default_col_sp),
|
||||
};
|
||||
Self::build_transforms(rows, cols, row_sp, col_sp)
|
||||
Self::build_transforms(rows, cols, row_sp, col_sp, self.ucs)
|
||||
.iter()
|
||||
.flat_map(|t| {
|
||||
if let EntityTransform::Translate(delta) = t {
|
||||
|
||||
Reference in new issue
Block a user