cargo fmt

This commit is contained in:
Hakan 2026-05-12 10:36:15 +03:00
commit ca07268c7c
204 changed files with 13988 additions and 5516 deletions

View file

@ -16,9 +16,18 @@ use crate::scene::wire_model::WireModel;
enum Step {
Tag,
Prompt { tag: String },
Default { tag: String, prompt: String },
Insertion { tag: String, prompt: String, default: String },
Prompt {
tag: String,
},
Default {
tag: String,
prompt: String,
},
Insertion {
tag: String,
prompt: String,
default: String,
},
}
pub struct AttdefCommand {
@ -29,18 +38,25 @@ pub struct AttdefCommand {
impl AttdefCommand {
pub fn new() -> Self {
Self { step: Step::Tag, height: 0.2 }
Self {
step: Step::Tag,
height: 0.2,
}
}
}
impl CadCommand for AttdefCommand {
fn name(&self) -> &'static str { "ATTDEF" }
fn name(&self) -> &'static str {
"ATTDEF"
}
fn prompt(&self) -> String {
match &self.step {
Step::Tag => "ATTDEF Enter attribute tag (no spaces):".into(),
Step::Tag => "ATTDEF Enter attribute tag (no spaces):".into(),
Step::Prompt { tag } => format!("ATTDEF Enter prompt for '{tag}' (Enter=use tag):"),
Step::Default { tag, .. } => format!("ATTDEF Enter default value for '{tag}' (Enter=blank):"),
Step::Default { tag, .. } => {
format!("ATTDEF Enter default value for '{tag}' (Enter=blank):")
}
Step::Insertion { tag, .. } => format!("ATTDEF Specify insertion point for '{tag}':"),
}
}
@ -61,7 +77,11 @@ impl CadCommand for AttdefCommand {
}
Step::Prompt { tag } => {
let tag = tag.clone();
let prompt = if text.trim().is_empty() { tag.clone() } else { text.trim().to_string() };
let prompt = if text.trim().is_empty() {
tag.clone()
} else {
text.trim().to_string()
};
self.step = Step::Default { tag, prompt };
Some(CmdResult::NeedPoint)
}
@ -69,7 +89,11 @@ impl CadCommand for AttdefCommand {
let tag = tag.clone();
let prompt = prompt.clone();
let default = text.trim().to_string();
self.step = Step::Insertion { tag, prompt, default };
self.step = Step::Insertion {
tag,
prompt,
default,
};
Some(CmdResult::NeedPoint)
}
Step::Insertion { .. } => None,
@ -77,7 +101,12 @@ impl CadCommand for AttdefCommand {
}
fn on_point(&mut self, pt: Vec3) -> CmdResult {
if let Step::Insertion { tag, prompt, default } = &self.step {
if let Step::Insertion {
tag,
prompt,
default,
} = &self.step
{
let mut attdef = AttributeDefinition {
tag: tag.clone(),
prompt: prompt.clone(),
@ -99,12 +128,19 @@ impl CadCommand for AttdefCommand {
// Treat Enter as empty text input for prompt/default steps.
Step::Prompt { tag } => {
let tag = tag.clone();
self.step = Step::Default { tag: tag.clone(), prompt: tag };
self.step = Step::Default {
tag: tag.clone(),
prompt: tag,
};
CmdResult::NeedPoint
}
Step::Default { tag, prompt } => {
let (tag, prompt) = (tag.clone(), prompt.clone());
self.step = Step::Insertion { tag, prompt, default: String::new() };
self.step = Step::Insertion {
tag,
prompt,
default: String::new(),
};
CmdResult::NeedPoint
}
Step::Insertion { .. } => CmdResult::Cancel,
@ -120,9 +156,11 @@ impl CadCommand for AttdefCommand {
Some(WireModel {
name: "attdef_preview".into(),
points: vec![
[pt.x - d, pt.y, pt.z], [pt.x + d, pt.y, pt.z],
[pt.x - d, pt.y, pt.z],
[pt.x + d, pt.y, pt.z],
[f32::NAN, 0.0, 0.0],
[pt.x, pt.y, pt.z - d], [pt.x, pt.y, pt.z + d],
[pt.x, pt.y, pt.z - d],
[pt.x, pt.y, pt.z + d],
],
color: WireModel::CYAN,
selected: false,

View file

@ -39,12 +39,14 @@ impl DonutCommand {
}
impl CadCommand for DonutCommand {
fn name(&self) -> &'static str { "DONUT" }
fn name(&self) -> &'static str {
"DONUT"
}
fn prompt(&self) -> String {
match &self.state {
DonutState::AskInner => "DONUT Specify inside diameter <0>:".into(),
DonutState::AskOuter => "DONUT Specify outside diameter:".into(),
DonutState::AskInner => "DONUT Specify inside diameter <0>:".into(),
DonutState::AskOuter => "DONUT Specify outside diameter:".into(),
DonutState::PlaceCenter => "DONUT Specify center of donut (Enter to exit):".into(),
}
}
@ -54,7 +56,12 @@ impl CadCommand for DonutCommand {
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let val: f64 = text.trim().replace(',', ".").parse().ok().filter(|&v: &f64| v >= 0.0)?;
let val: f64 = text
.trim()
.replace(',', ".")
.parse()
.ok()
.filter(|&v: &f64| v >= 0.0)?;
match &self.state {
DonutState::AskInner => {
self.inner_r = val / 2.0;
@ -62,7 +69,9 @@ impl CadCommand for DonutCommand {
Some(CmdResult::NeedPoint)
}
DonutState::AskOuter => {
if val <= 0.0 { return None; }
if val <= 0.0 {
return None;
}
self.outer_r = val / 2.0;
if self.inner_r > self.outer_r {
std::mem::swap(&mut self.inner_r, &mut self.outer_r);

View file

@ -525,10 +525,10 @@ fn line_wire(from: Vec3, to: Vec3) -> WireModel {
snap_pts: vec![],
tangent_geoms: vec![],
aci: 0,
key_vertices: vec![],
aabb: WireModel::UNBOUNDED_AABB,
plinegen: true,
vp_scissor: None,
fill_tris: vec![],
key_vertices: vec![],
aabb: WireModel::UNBOUNDED_AABB,
plinegen: true,
vp_scissor: None,
fill_tris: vec![],
}
}

View file

@ -29,23 +29,34 @@ pub struct HatcheditCommand {
impl HatcheditCommand {
pub fn new() -> Self {
Self { step: HatcheditStep::PickHatch }
Self {
step: HatcheditStep::PickHatch,
}
}
pub fn with_handle(handle: Handle, name: String, scale: f32, angle: f32) -> Self {
Self {
step: HatcheditStep::EditOptions { handle, name, scale, angle },
step: HatcheditStep::EditOptions {
handle,
name,
scale,
angle,
},
}
}
}
impl CadCommand for HatcheditCommand {
fn name(&self) -> &'static str { "HATCHEDIT" }
fn name(&self) -> &'static str {
"HATCHEDIT"
}
fn prompt(&self) -> String {
match &self.step {
HatcheditStep::PickHatch => "HATCHEDIT Select hatch:".into(),
HatcheditStep::EditOptions { name, scale, angle, .. } => format!(
HatcheditStep::EditOptions {
name, scale, angle, ..
} => format!(
"HATCHEDIT Pattern:{name} Scale:{scale:.4} Angle:{angle:.1} \
[P <pat> / S <scale> / A <angle> | Enter to apply]:"
),
@ -57,7 +68,9 @@ impl CadCommand for HatcheditCommand {
}
fn on_entity_pick(&mut self, handle: Handle, _pt: Vec3) -> CmdResult {
if handle.is_null() { return CmdResult::NeedPoint; }
if handle.is_null() {
return CmdResult::NeedPoint;
}
// Actual hatch model retrieval happens in commands.rs dispatch.
// Store handle; name/scale/angle filled in by dispatch.
self.step = HatcheditStep::EditOptions {
@ -75,9 +88,12 @@ impl CadCommand for HatcheditCommand {
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let (handle, name, scale, angle) = match &mut self.step {
HatcheditStep::EditOptions { handle, name, scale, angle } => {
(*handle, name, scale, angle)
}
HatcheditStep::EditOptions {
handle,
name,
scale,
angle,
} => (*handle, name, scale, angle),
_ => return None,
};
@ -96,12 +112,16 @@ impl CadCommand for HatcheditCommand {
// Parse option: P/S/A followed by value
if let Some(rest) = text.strip_prefix('P') {
let n = rest.trim().to_string();
if !n.is_empty() { *name = n; }
if !n.is_empty() {
*name = n;
}
return Some(CmdResult::NeedPoint);
}
if let Some(rest) = text.strip_prefix('S') {
if let Ok(v) = rest.trim().replace(',', ".").parse::<f32>() {
if v > 0.0 { *scale = v; }
if v > 0.0 {
*scale = v;
}
}
return Some(CmdResult::NeedPoint);
}
@ -116,16 +136,28 @@ impl CadCommand for HatcheditCommand {
Some(CmdResult::NeedPoint)
}
fn on_point(&mut self, _pt: Vec3) -> CmdResult { CmdResult::NeedPoint }
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
// Enter without text → apply current settings
let (handle, name, scale, angle) = match &self.step {
HatcheditStep::EditOptions { handle, name, scale, angle } => {
(*handle, name.clone(), *scale, *angle)
}
HatcheditStep::EditOptions {
handle,
name,
scale,
angle,
} => (*handle, name.clone(), *scale, *angle),
_ => return CmdResult::Cancel,
};
CmdResult::HatcheditApply { handle, name, scale, angle }
CmdResult::HatcheditApply {
handle,
name,
scale,
angle,
}
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult { CmdResult::Cancel }
}

View file

@ -23,16 +23,28 @@ pub struct MlineCommand {
impl MlineCommand {
#[allow(dead_code)]
pub fn new() -> Self {
Self { points: vec![], scale: 1.0, waiting_scale: false, style_name: "Standard".into() }
Self {
points: vec![],
scale: 1.0,
waiting_scale: false,
style_name: "Standard".into(),
}
}
pub fn with_style(style_name: impl Into<String>) -> Self {
Self { points: vec![], scale: 1.0, waiting_scale: false, style_name: style_name.into() }
Self {
points: vec![],
scale: 1.0,
waiting_scale: false,
style_name: style_name.into(),
}
}
}
impl CadCommand for MlineCommand {
fn name(&self) -> &'static str { "MLINE" }
fn name(&self) -> &'static str {
"MLINE"
}
fn prompt(&self) -> String {
if self.waiting_scale {
@ -40,7 +52,10 @@ impl CadCommand for MlineCommand {
} else if self.points.is_empty() {
format!("MLINE Specify start point (scale={:.2}):", self.scale)
} else {
format!("MLINE Specify next point ({} pts, Enter to finish, C to close, S to set scale):", self.points.len())
format!(
"MLINE Specify next point ({} pts, Enter to finish, C to close, S to set scale):",
self.points.len()
)
}
}
@ -51,7 +66,12 @@ impl CadCommand for MlineCommand {
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
// Waiting for scale value
if self.waiting_scale {
let v: f64 = text.trim().replace(',', ".").parse().ok().filter(|&v: &f64| v > 0.0)?;
let v: f64 = text
.trim()
.replace(',', ".")
.parse()
.ok()
.filter(|&v: &f64| v > 0.0)?;
self.scale = v;
self.waiting_scale = false;
return Some(CmdResult::NeedPoint);
@ -74,7 +94,9 @@ impl CadCommand for MlineCommand {
// Scale: "S <value>" inline
if let Some(rest) = up.strip_prefix("S ") {
if let Ok(v) = rest.trim().replace(',', ".").parse::<f64>() {
if v > 0.0 { self.scale = v; }
if v > 0.0 {
self.scale = v;
}
return Some(CmdResult::NeedPoint);
}
}
@ -96,9 +118,10 @@ impl CadCommand for MlineCommand {
}
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
if self.points.is_empty() { return None; }
let mut pts: Vec<[f32; 3]> =
self.points.iter().map(|p| [p.x, p.y, p.z]).collect();
if self.points.is_empty() {
return None;
}
let mut pts: Vec<[f32; 3]> = self.points.iter().map(|p| [p.x, p.y, p.z]).collect();
pts.push([pt.x, pt.y, pt.z]);
Some(WireModel {
name: "mline_preview".into(),
@ -121,7 +144,8 @@ impl CadCommand for MlineCommand {
}
fn build_mline(pts: &[Vec3], scale: f64, closed: bool, style_name: &str) -> EntityType {
let verts: Vec<Vector3> = pts.iter()
let verts: Vec<Vector3> = pts
.iter()
.map(|p| Vector3::new(p.x as f64, p.z as f64, p.y as f64))
.collect();
let mut mline = if closed {

View file

@ -3,8 +3,8 @@ pub mod attdef;
pub mod circle;
pub mod donut;
pub mod ellipse;
pub mod hatchedit;
pub mod hatch;
pub mod hatchedit;
pub mod line;
pub mod mline;
pub mod point;

View file

@ -130,7 +130,8 @@ fn update_tangent_after_arc(tangent: &mut Option<Vec2>, bulge: f64) {
// the entry tangent rotated by that angle.
let theta = 4.0 * (bulge as f32).atan();
let (sin_t, cos_t) = theta.sin_cos();
*tangent = Some(Vec2::new(t.x * cos_t - t.y * sin_t, t.x * sin_t + t.y * cos_t).normalize_or_zero());
*tangent =
Some(Vec2::new(t.x * cos_t - t.y * sin_t, t.x * sin_t + t.y * cos_t).normalize_or_zero());
}
/// Sample a circular arc defined by bulge into `n` line-segment points.
@ -169,7 +170,11 @@ fn arc_sample_points(a: Vec3, bulge: f64, b: Vec3, n: usize) -> Vec<[f32; 3]> {
for i in 0..=n {
let t = i as f64 / n as f64;
let angle = start_angle + t * theta;
pts.push([(cx + r * angle.cos()) as f32, (cy + r * angle.sin()) as f32, a.z]);
pts.push([
(cx + r * angle.cos()) as f32,
(cy + r * angle.sin()) as f32,
a.z,
]);
}
pts
}
@ -308,6 +313,11 @@ impl CadCommand for PlineCommand {
}
}
Some(WireModel::solid("rubber_band".into(), pts, WireModel::CYAN, false))
Some(WireModel::solid(
"rubber_band".into(),
pts,
WireModel::CYAN,
false,
))
}
}

View file

@ -23,7 +23,12 @@ pub struct ImageCommand {
impl ImageCommand {
pub fn new(file_path: String, pixel_width: u32, pixel_height: u32) -> Self {
Self { file_path, pixel_width, pixel_height, origin: None }
Self {
file_path,
pixel_width,
pixel_height,
origin: None,
}
}
fn aspect(&self) -> f64 {
@ -61,7 +66,10 @@ impl CadCommand for ImageCommand {
fn prompt(&self) -> String {
if self.origin.is_none() {
format!("IMAGE Specify insertion point ({}): ", short_name(&self.file_path))
format!(
"IMAGE Specify insertion point ({}): ",
short_name(&self.file_path)
)
} else {
"IMAGE Specify width (drag right):".into()
}

View file

@ -6,7 +6,7 @@
use acadrust::entities::LwPolyline;
use acadrust::types::Vector2;
use acadrust::{EntityType, entities::LwVertex};
use acadrust::{entities::LwVertex, EntityType};
use glam::Vec3;
use crate::command::{CadCommand, CmdResult};
@ -41,13 +41,21 @@ impl RevCloudCommand {
}
impl CadCommand for RevCloudCommand {
fn name(&self) -> &'static str { "REVCLOUD" }
fn name(&self) -> &'static str {
"REVCLOUD"
}
fn prompt(&self) -> String {
if self.points.is_empty() {
format!("REVCLOUD Specify start point (arc length = {:.2}):", self.arc_length)
format!(
"REVCLOUD Specify start point (arc length = {:.2}):",
self.arc_length
)
} else {
format!("REVCLOUD Specify next point ({} pts, Enter to close):", self.points.len())
format!(
"REVCLOUD Specify next point ({} pts, Enter to close):",
self.points.len()
)
}
}
@ -65,9 +73,10 @@ impl CadCommand for RevCloudCommand {
}
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
if self.points.is_empty() { return None; }
let mut preview_pts: Vec<[f32; 3]> =
self.points.iter().map(|p| [p.x, p.y, p.z]).collect();
if self.points.is_empty() {
return None;
}
let mut preview_pts: Vec<[f32; 3]> = self.points.iter().map(|p| [p.x, p.y, p.z]).collect();
preview_pts.push([pt.x, pt.y, pt.z]);
preview_pts.push([self.points[0].x, self.points[0].y, self.points[0].z]);
Some(WireModel {
@ -101,7 +110,9 @@ fn make_revcloud(pts: &[Vec3], arc_len: f64) -> EntityType {
let p0 = pts[i];
let p1 = pts[(i + 1) % n];
let seg_len = ((p1.x - p0.x).powi(2) + (p1.z - p0.z).powi(2)).sqrt() as f64;
if seg_len < 1e-6 { continue; }
if seg_len < 1e-6 {
continue;
}
// How many full arcs fit?
let num_arcs = ((seg_len / arc_len).round() as usize).max(1);

View file

@ -37,15 +37,25 @@ enum WipeoutMode {
impl WipeoutCommand {
pub fn new_rectangular() -> Self {
Self { mode: WipeoutMode::Rectangular, first: None, poly_pts: vec![] }
Self {
mode: WipeoutMode::Rectangular,
first: None,
poly_pts: vec![],
}
}
pub fn new_polygonal() -> Self {
Self { mode: WipeoutMode::Polygonal, first: None, poly_pts: vec![] }
Self {
mode: WipeoutMode::Polygonal,
first: None,
poly_pts: vec![],
}
}
}
impl CadCommand for WipeoutCommand {
fn name(&self) -> &'static str { "WIPEOUT" }
fn name(&self) -> &'static str {
"WIPEOUT"
}
fn prompt(&self) -> String {
match &self.mode {
@ -60,7 +70,10 @@ impl CadCommand for WipeoutCommand {
if self.poly_pts.is_empty() {
"WIPEOUT (Polygon) Specify first point:".into()
} else {
format!("WIPEOUT (Polygon) Specify next point ({} pts, Enter to close):", self.poly_pts.len())
format!(
"WIPEOUT (Polygon) Specify next point ({} pts, Enter to close):",
self.poly_pts.len()
)
}
}
}
@ -117,15 +130,17 @@ impl CadCommand for WipeoutCommand {
snap_pts: vec![],
tangent_geoms: vec![],
aci: 0,
key_vertices: vec![],
aabb: WireModel::UNBOUNDED_AABB,
plinegen: true,
vp_scissor: None,
fill_tris: vec![],
key_vertices: vec![],
aabb: WireModel::UNBOUNDED_AABB,
plinegen: true,
vp_scissor: None,
fill_tris: vec![],
})
}
WipeoutMode::Polygonal => {
if self.poly_pts.is_empty() { return None; }
if self.poly_pts.is_empty() {
return None;
}
let mut pts: Vec<[f32; 3]> =
self.poly_pts.iter().map(|p| [p.x, p.y, p.z]).collect();
pts.push([pt.x, pt.y, pt.z]);
@ -141,11 +156,11 @@ impl CadCommand for WipeoutCommand {
snap_pts: vec![],
tangent_geoms: vec![],
aci: 0,
key_vertices: vec![],
aabb: WireModel::UNBOUNDED_AABB,
plinegen: true,
vp_scissor: None,
fill_tris: vec![],
key_vertices: vec![],
aabb: WireModel::UNBOUNDED_AABB,
plinegen: true,
vp_scissor: None,
fill_tris: vec![],
})
}
}
@ -154,23 +169,16 @@ impl CadCommand for WipeoutCommand {
fn make_rect_wipeout(p1: Vec3, p2: Vec3) -> EntityType {
// Y-up world: X→DXF X, Z→DXF Y, Y→DXF Z
let c1 = Vector3::new(
p1.x.min(p2.x) as f64,
p1.z.min(p2.z) as f64,
p1.y as f64,
);
let c2 = Vector3::new(
p1.x.max(p2.x) as f64,
p1.z.max(p2.z) as f64,
p1.y as f64,
);
let c1 = Vector3::new(p1.x.min(p2.x) as f64, p1.z.min(p2.z) as f64, p1.y as f64);
let c2 = Vector3::new(p1.x.max(p2.x) as f64, p1.z.max(p2.z) as f64, p1.y as f64);
EntityType::Wipeout(Wipeout::from_corners(c1, c2))
}
fn make_poly_wipeout(pts: &[Vec3]) -> EntityType {
use acadrust::types::Vector2;
let z = pts[0].y as f64;
let verts: Vec<Vector2> = pts.iter()
let verts: Vec<Vector2> = pts
.iter()
.map(|p| Vector2::new(p.x as f64, p.z as f64))
.collect();
EntityType::Wipeout(Wipeout::polygonal(&verts, z))

View file

@ -19,12 +19,17 @@ pub struct DivideCommand {
impl DivideCommand {
pub fn new() -> Self {
Self { target: None, waiting_for_n: false }
Self {
target: None,
waiting_for_n: false,
}
}
}
impl CadCommand for DivideCommand {
fn name(&self) -> &'static str { "DIVIDE" }
fn name(&self) -> &'static str {
"DIVIDE"
}
fn prompt(&self) -> String {
if self.target.is_none() {
@ -34,16 +39,22 @@ impl CadCommand for DivideCommand {
}
}
fn needs_entity_pick(&self) -> bool { self.target.is_none() }
fn needs_entity_pick(&self) -> bool {
self.target.is_none()
}
fn on_entity_pick(&mut self, handle: Handle, _pt: Vec3) -> CmdResult {
if handle.is_null() { return CmdResult::NeedPoint; }
if handle.is_null() {
return CmdResult::NeedPoint;
}
self.target = Some(handle);
self.waiting_for_n = true;
CmdResult::NeedPoint
}
fn wants_text_input(&self) -> bool { self.waiting_for_n }
fn wants_text_input(&self) -> bool {
self.waiting_for_n
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let n: usize = text.trim().parse().ok().filter(|&n| n >= 2)?;
@ -52,8 +63,12 @@ impl CadCommand for DivideCommand {
Some(CmdResult::DivideEntity { handle, n })
}
fn on_point(&mut self, _pt: Vec3) -> CmdResult { CmdResult::NeedPoint }
fn on_enter(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
}
// ── MEASURE ────────────────────────────────────────────────────────────────
@ -65,12 +80,17 @@ pub struct MeasureCommand {
impl MeasureCommand {
pub fn new() -> Self {
Self { target: None, waiting_for_dist: false }
Self {
target: None,
waiting_for_dist: false,
}
}
}
impl CadCommand for MeasureCommand {
fn name(&self) -> &'static str { "MEASURE" }
fn name(&self) -> &'static str {
"MEASURE"
}
fn prompt(&self) -> String {
if self.target.is_none() {
@ -80,47 +100,75 @@ impl CadCommand for MeasureCommand {
}
}
fn needs_entity_pick(&self) -> bool { self.target.is_none() }
fn needs_entity_pick(&self) -> bool {
self.target.is_none()
}
fn on_entity_pick(&mut self, handle: Handle, _pt: Vec3) -> CmdResult {
if handle.is_null() { return CmdResult::NeedPoint; }
if handle.is_null() {
return CmdResult::NeedPoint;
}
self.target = Some(handle);
self.waiting_for_dist = true;
CmdResult::NeedPoint
}
fn wants_text_input(&self) -> bool { self.waiting_for_dist }
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let dist: f64 = text.trim().replace(',', ".").parse().ok().filter(|&d: &f64| d > 0.0)?;
let handle = self.target?;
self.waiting_for_dist = false;
Some(CmdResult::MeasureEntity { handle, segment_length: dist })
fn wants_text_input(&self) -> bool {
self.waiting_for_dist
}
fn on_point(&mut self, _pt: Vec3) -> CmdResult { CmdResult::NeedPoint }
fn on_enter(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let dist: f64 = text
.trim()
.replace(',', ".")
.parse()
.ok()
.filter(|&d: &f64| d > 0.0)?;
let handle = self.target?;
self.waiting_for_dist = false;
Some(CmdResult::MeasureEntity {
handle,
segment_length: dist,
})
}
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
}
// ── Geometry ───────────────────────────────────────────────────────────────
/// Compute N-1 equally spaced points along the entity (DIVIDE).
pub fn divide_entity(entity: &EntityType, n: usize) -> Vec<EntityType> {
if n < 2 { return vec![]; }
if n < 2 {
return vec![];
}
let total = entity_length(entity);
if total < 1e-10 { return vec![]; }
if total < 1e-10 {
return vec![];
}
let step = total / n as f64;
(1..n).filter_map(|k| {
let t = step * k as f64;
point_at_distance(entity, t).map(make_point)
}).collect()
(1..n)
.filter_map(|k| {
let t = step * k as f64;
point_at_distance(entity, t).map(make_point)
})
.collect()
}
/// Compute points at fixed `segment_length` intervals along the entity (MEASURE).
pub fn measure_entity(entity: &EntityType, segment_length: f64) -> Vec<EntityType> {
if segment_length <= 0.0 { return vec![]; }
if segment_length <= 0.0 {
return vec![];
}
let total = entity_length(entity);
if total < 1e-10 { return vec![]; }
if total < 1e-10 {
return vec![];
}
let mut pts = Vec::new();
let mut t = segment_length;
while t < total - 1e-6 {
@ -144,7 +192,7 @@ fn entity_length(entity: &EntityType) -> f64 {
let dx = l.end.x - l.start.x;
let dy = l.end.y - l.start.y;
let dz = l.end.z - l.start.z;
(dx*dx + dy*dy + dz*dz).sqrt()
(dx * dx + dy * dy + dz * dz).sqrt()
}
EntityType::Arc(a) => {
let span = arc_span_rad(a.start_angle, a.end_angle);
@ -153,15 +201,19 @@ fn entity_length(entity: &EntityType) -> f64 {
EntityType::Circle(c) => 2.0 * PI * c.radius,
EntityType::LwPolyline(p) => {
let n = p.vertices.len();
if n < 2 { return 0.0; }
if n < 2 {
return 0.0;
}
let segs = if p.is_closed { n } else { n - 1 };
(0..segs).map(|i| {
let v0 = &p.vertices[i];
let v1 = &p.vertices[(i + 1) % n];
let dx = v1.location.x - v0.location.x;
let dy = v1.location.y - v0.location.y;
(dx*dx + dy*dy).sqrt()
}).sum()
(0..segs)
.map(|i| {
let v0 = &p.vertices[i];
let v1 = &p.vertices[(i + 1) % n];
let dx = v1.location.x - v0.location.x;
let dy = v1.location.y - v0.location.y;
(dx * dx + dy * dy).sqrt()
})
.sum()
}
_ => 0.0,
}
@ -171,7 +223,9 @@ fn point_at_distance(entity: &EntityType, d: f64) -> Option<Vector3> {
match entity {
EntityType::Line(l) => {
let total = entity_length(entity);
if total < 1e-10 { return None; }
if total < 1e-10 {
return None;
}
let t = (d / total).clamp(0.0, 1.0);
Some(Vector3::new(
l.start.x + t * (l.end.x - l.start.x),
@ -182,7 +236,9 @@ fn point_at_distance(entity: &EntityType, d: f64) -> Option<Vector3> {
EntityType::Arc(a) => {
let span = arc_span_rad(a.start_angle, a.end_angle);
let t = d / a.radius; // arc_length = r * theta
if t > span { return None; }
if t > span {
return None;
}
let angle = a.start_angle + t;
Some(Vector3::new(
a.center.x + a.radius * angle.cos(),
@ -192,7 +248,9 @@ fn point_at_distance(entity: &EntityType, d: f64) -> Option<Vector3> {
}
EntityType::Circle(c) => {
let circumference = 2.0 * PI * c.radius;
if circumference < 1e-10 { return None; }
if circumference < 1e-10 {
return None;
}
let angle = 2.0 * PI * (d / circumference);
Some(Vector3::new(
c.center.x + c.radius * angle.cos(),
@ -202,7 +260,9 @@ fn point_at_distance(entity: &EntityType, d: f64) -> Option<Vector3> {
}
EntityType::LwPolyline(p) => {
let n = p.vertices.len();
if n < 2 { return None; }
if n < 2 {
return None;
}
let segs = if p.is_closed { n } else { n - 1 };
let mut acc = 0.0f64;
for i in 0..segs {
@ -210,7 +270,7 @@ fn point_at_distance(entity: &EntityType, d: f64) -> Option<Vector3> {
let v1 = &p.vertices[(i + 1) % n];
let dx = v1.location.x - v0.location.x;
let dy = v1.location.y - v0.location.y;
let seg_len = (dx*dx + dy*dy).sqrt();
let seg_len = (dx * dx + dy * dy).sqrt();
if acc + seg_len >= d - 1e-10 {
let t = (d - acc) / seg_len.max(1e-10);
return Some(Vector3::new(
@ -229,5 +289,9 @@ fn point_at_distance(entity: &EntityType, d: f64) -> Option<Vector3> {
fn arc_span_rad(start: f64, end: f64) -> f64 {
let span = (end - start).rem_euclid(2.0 * PI);
if span < 1e-6 { 2.0 * PI } else { span }
if span < 1e-6 {
2.0 * PI
} else {
span
}
}

View file

@ -4,7 +4,9 @@ pub fn tool() -> ToolDef {
ToolDef {
id: "LAYMCUR",
label: "Make Current",
icon: IconKind::Svg(include_bytes!("../../../../assets/icons/layers/laymcur.svg")),
icon: IconKind::Svg(include_bytes!(
"../../../../assets/icons/layers/laymcur.svg"
)),
event: ModuleEvent::Command("LAYMCUR".to_string()),
}
}

View file

@ -6,7 +6,9 @@ pub fn tool() -> ToolDef {
ToolDef {
id: "LAYMATCH",
label: "Match Layer",
icon: IconKind::Svg(include_bytes!("../../../../assets/icons/layers/laymatch.svg")),
icon: IconKind::Svg(include_bytes!(
"../../../../assets/icons/layers/laymatch.svg"
)),
event: ModuleEvent::Command("LAYMATCH".to_string()),
}
}

View file

@ -1,17 +1,17 @@
// Home module — Draw, Modify, Annotation and Layer tools.
pub mod clipboard;
pub mod defaults;
mod about;
mod changelog;
pub mod clipboard;
pub mod defaults;
mod donate;
pub mod draw;
mod report;
pub mod groups;
pub mod inquiry;
pub mod layers;
pub mod modify;
pub mod properties;
mod report;
pub mod select;
use crate::modules::{CadModule, RibbonGroup, RibbonItem};
@ -27,21 +27,25 @@ impl CadModule for HomeModule {
}
fn ribbon_groups(&self) -> Vec<RibbonGroup> {
use crate::modules::annotate::{angular_dim, leader_cmd, linear_dim, mleader_cmd, mtext, radius_dim, text};
use crate::modules::annotate::{
angular_dim, leader_cmd, linear_dim, mleader_cmd, mtext, radius_dim, text,
};
use crate::modules::insert::{create_block, insert_block};
use clipboard::{copy_clip, cut, paste};
use about;
use changelog;
use clipboard::{copy_clip, cut, paste};
use donate;
use report;
use draw::{arc, circle, ellipse, hatch, line, polyline, shapes};
use layers::{layfrz, laylck, layoff, layon, laythw, layulk, make_current, match_layer, panel};
use groups::{group, ungroup};
use layers::{
layfrz, laylck, layoff, layon, laythw, layulk, make_current, match_layer, panel,
};
use modify::{
array, copy, delete, explode, fillet, mirror, offset, rotate, scale, stretch,
translate, trim,
};
use properties::match_prop;
use report;
vec![
RibbonGroup {
@ -133,9 +137,21 @@ impl CadModule for HomeModule {
label: "Dimensions",
icon: linear_dim::ICON,
items: vec![
(linear_dim::tool().id, linear_dim::tool().label, linear_dim::tool().icon),
(radius_dim::tool().id, radius_dim::tool().label, radius_dim::tool().icon),
(angular_dim::tool().id, angular_dim::tool().label, angular_dim::tool().icon),
(
linear_dim::tool().id,
linear_dim::tool().label,
linear_dim::tool().icon,
),
(
radius_dim::tool().id,
radius_dim::tool().label,
radius_dim::tool().icon,
),
(
angular_dim::tool().id,
angular_dim::tool().label,
angular_dim::tool().icon,
),
],
default: "DIMLINEAR",
},
@ -144,8 +160,16 @@ impl CadModule for HomeModule {
label: "Leader",
icon: leader_cmd::ICON,
items: vec![
(mleader_cmd::tool().id, mleader_cmd::tool().label, mleader_cmd::tool().icon),
(leader_cmd::tool().id, leader_cmd::tool().label, leader_cmd::tool().icon),
(
mleader_cmd::tool().id,
mleader_cmd::tool().label,
mleader_cmd::tool().icon,
),
(
leader_cmd::tool().id,
leader_cmd::tool().label,
leader_cmd::tool().icon,
),
],
default: "MLEADER",
},
@ -156,8 +180,18 @@ impl CadModule for HomeModule {
tools: vec![
RibbonItem::LargeTool(panel::tool()),
RibbonItem::LayerComboGroup {
row2: vec![layoff::tool(), layfrz::tool(), laylck::tool(), make_current::tool()],
row3: vec![layon::tool(), laythw::tool(), layulk::tool(), match_layer::tool()],
row2: vec![
layoff::tool(),
layfrz::tool(),
laylck::tool(),
make_current::tool(),
],
row3: vec![
layon::tool(),
laythw::tool(),
layulk::tool(),
match_layer::tool(),
],
},
],
},
@ -170,9 +204,9 @@ impl CadModule for HomeModule {
},
RibbonGroup {
title: "Properties",
tools: vec![
RibbonItem::PropertiesGroup { match_prop: match_prop::tool() },
],
tools: vec![RibbonItem::PropertiesGroup {
match_prop: match_prop::tool(),
}],
},
RibbonGroup {
title: "Groups",

View file

@ -9,8 +9,8 @@
// With 1 pair: pure translation (src1 → dst1)
// With 2 pairs: translate + rotate (+ optional uniform scale to fit)
use glam::Vec3;
use acadrust::Handle;
use glam::Vec3;
use crate::command::{CadCommand, CmdResult, EntityTransform};
@ -38,23 +38,30 @@ impl AlignCommand {
Self {
state: AlignState::Gathering,
handles: vec![],
src1: None, dst1: None,
src2: None, dst2: None,
src1: None,
dst1: None,
src2: None,
dst2: None,
}
}
}
impl CadCommand for AlignCommand {
fn name(&self) -> &'static str { "ALIGN" }
fn name(&self) -> &'static str {
"ALIGN"
}
fn prompt(&self) -> String {
match self.state {
AlignState::Gathering => format!("ALIGN Select objects ({} selected, Enter when done):", self.handles.len()),
AlignState::Src1 => "ALIGN Specify 1st source point:".into(),
AlignState::Dst1 => "ALIGN Specify 1st destination point:".into(),
AlignState::Src2 => "ALIGN Specify 2nd source point (Enter = translate only):".into(),
AlignState::Dst2 => "ALIGN Specify 2nd destination point:".into(),
AlignState::AskScale => "ALIGN Scale objects based on alignment points? [Y/N]:".into(),
AlignState::Gathering => format!(
"ALIGN Select objects ({} selected, Enter when done):",
self.handles.len()
),
AlignState::Src1 => "ALIGN Specify 1st source point:".into(),
AlignState::Dst1 => "ALIGN Specify 1st destination point:".into(),
AlignState::Src2 => "ALIGN Specify 2nd source point (Enter = translate only):".into(),
AlignState::Dst2 => "ALIGN Specify 2nd destination point:".into(),
AlignState::AskScale => "ALIGN Scale objects based on alignment points? [Y/N]:".into(),
}
}
@ -97,7 +104,9 @@ impl CadCommand for AlignCommand {
fn on_enter(&mut self) -> CmdResult {
match self.state {
AlignState::Gathering => {
if self.handles.is_empty() { return CmdResult::Cancel; }
if self.handles.is_empty() {
return CmdResult::Cancel;
}
self.state = AlignState::Src1;
CmdResult::NeedPoint
}
@ -127,7 +136,9 @@ impl CadCommand for AlignCommand {
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
if self.state != AlignState::AskScale { return None; }
if self.state != AlignState::AskScale {
return None;
}
let scale = text.trim().to_uppercase().starts_with('Y');
Some(self.compute_align(scale))
}

View file

@ -705,11 +705,31 @@ impl CadCommand for ArrayPathCommand {
#[derive(Debug, Clone, Copy)]
enum Array3DStep {
Rows,
Cols { rows: u32 },
Levels { rows: u32, cols: u32 },
RowSp { rows: u32, cols: u32, levels: u32 },
ColSp { rows: u32, cols: u32, levels: u32, row_sp: f32 },
LvlSp { rows: u32, cols: u32, levels: u32, row_sp: f32, col_sp: f32 },
Cols {
rows: u32,
},
Levels {
rows: u32,
cols: u32,
},
RowSp {
rows: u32,
cols: u32,
levels: u32,
},
ColSp {
rows: u32,
cols: u32,
levels: u32,
row_sp: f32,
},
LvlSp {
rows: u32,
cols: u32,
levels: u32,
row_sp: f32,
col_sp: f32,
},
}
pub struct Array3DCommand {
@ -719,18 +739,27 @@ pub struct Array3DCommand {
impl Array3DCommand {
pub fn new(handles: Vec<Handle>) -> Self {
Self { handles, step: Array3DStep::Rows }
Self {
handles,
step: Array3DStep::Rows,
}
}
fn build_transforms(
rows: u32, cols: u32, levels: u32,
row_sp: f32, col_sp: f32, lvl_sp: f32,
rows: u32,
cols: u32,
levels: u32,
row_sp: f32,
col_sp: f32,
lvl_sp: f32,
) -> Vec<EntityTransform> {
let mut t = Vec::new();
for l in 0..levels {
for r in 0..rows {
for c in 0..cols {
if l == 0 && r == 0 && c == 0 { continue; }
if l == 0 && r == 0 && c == 0 {
continue;
}
// World: X=col dir, Z=row dir (DXF Y), Y=level (up)
t.push(EntityTransform::Translate(Vec3::new(
col_sp * c as f32,
@ -745,65 +774,142 @@ impl Array3DCommand {
}
impl CadCommand for Array3DCommand {
fn name(&self) -> &'static str { "ARRAY3D" }
fn name(&self) -> &'static str {
"ARRAY3D"
}
fn prompt(&self) -> String {
match self.step {
Array3DStep::Rows => "ARRAY3D Enter row count:".into(),
Array3DStep::Cols { rows } => format!("ARRAY3D Enter column count [{rows} rows]:"),
Array3DStep::Levels { rows, cols } => format!("ARRAY3D Enter level count [{rows}×{cols}]:"),
Array3DStep::RowSp { rows, cols, levels } => format!("ARRAY3D Row spacing [{rows}×{cols}×{levels}]:"),
Array3DStep::ColSp { rows, cols, levels, row_sp } => format!("ARRAY3D Column spacing [{rows}×{cols}×{levels}, row={row_sp:.0}]:"),
Array3DStep::LvlSp { rows, cols, levels, row_sp, col_sp } => format!("ARRAY3D Level spacing [{rows}×{cols}×{levels}, r={row_sp:.0} c={col_sp:.0}]:"),
Array3DStep::Levels { rows, cols } => {
format!("ARRAY3D Enter level count [{rows}×{cols}]:")
}
Array3DStep::RowSp { rows, cols, levels } => {
format!("ARRAY3D Row spacing [{rows}×{cols}×{levels}]:")
}
Array3DStep::ColSp {
rows,
cols,
levels,
row_sp,
} => format!("ARRAY3D Column spacing [{rows}×{cols}×{levels}, row={row_sp:.0}]:"),
Array3DStep::LvlSp {
rows,
cols,
levels,
row_sp,
col_sp,
} => format!(
"ARRAY3D Level spacing [{rows}×{cols}×{levels}, r={row_sp:.0} c={col_sp:.0}]:"
),
}
}
fn wants_text_input(&self) -> bool { true }
fn wants_text_input(&self) -> bool {
true
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let t = text.trim().replace(',', ".");
let t = t.as_str();
match self.step {
Array3DStep::Rows => {
let v = if t.is_empty() { 2 } else { t.parse::<u32>().unwrap_or(2).max(1) };
let v = if t.is_empty() {
2
} else {
t.parse::<u32>().unwrap_or(2).max(1)
};
self.step = Array3DStep::Cols { rows: v };
Some(CmdResult::NeedPoint)
}
Array3DStep::Cols { rows } => {
let v = if t.is_empty() { 2 } else { t.parse::<u32>().unwrap_or(2).max(1) };
let v = if t.is_empty() {
2
} else {
t.parse::<u32>().unwrap_or(2).max(1)
};
self.step = Array3DStep::Levels { rows, cols: v };
Some(CmdResult::NeedPoint)
}
Array3DStep::Levels { rows, cols } => {
let v = if t.is_empty() { 2 } else { t.parse::<u32>().unwrap_or(2).max(1) };
self.step = Array3DStep::RowSp { rows, cols, levels: v };
let v = if t.is_empty() {
2
} else {
t.parse::<u32>().unwrap_or(2).max(1)
};
self.step = Array3DStep::RowSp {
rows,
cols,
levels: v,
};
Some(CmdResult::NeedPoint)
}
Array3DStep::RowSp { rows, cols, levels } => {
let v: f32 = if t.is_empty() { 1.0 } else { t.parse().unwrap_or(1.0) };
self.step = Array3DStep::ColSp { rows, cols, levels, row_sp: v };
let v: f32 = if t.is_empty() {
1.0
} else {
t.parse().unwrap_or(1.0)
};
self.step = Array3DStep::ColSp {
rows,
cols,
levels,
row_sp: v,
};
Some(CmdResult::NeedPoint)
}
Array3DStep::ColSp { rows, cols, levels, row_sp } => {
let v: f32 = if t.is_empty() { 1.0 } else { t.parse().unwrap_or(1.0) };
self.step = Array3DStep::LvlSp { rows, cols, levels, row_sp, col_sp: v };
Array3DStep::ColSp {
rows,
cols,
levels,
row_sp,
} => {
let v: f32 = if t.is_empty() {
1.0
} else {
t.parse().unwrap_or(1.0)
};
self.step = Array3DStep::LvlSp {
rows,
cols,
levels,
row_sp,
col_sp: v,
};
Some(CmdResult::NeedPoint)
}
Array3DStep::LvlSp { rows, cols, levels, row_sp, col_sp } => {
let v: f32 = if t.is_empty() { 1.0 } else { t.parse().unwrap_or(1.0) };
Array3DStep::LvlSp {
rows,
cols,
levels,
row_sp,
col_sp,
} => {
let v: f32 = if t.is_empty() {
1.0
} else {
t.parse().unwrap_or(1.0)
};
let transforms = Self::build_transforms(rows, cols, levels, row_sp, col_sp, v);
Some(CmdResult::BatchCopy(self.handles.clone(), transforms))
}
}
}
fn on_point(&mut self, _pt: Vec3) -> CmdResult { CmdResult::NeedPoint }
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
self.on_text_input("").map_or(CmdResult::NeedPoint, |r| r)
}
fn on_escape(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_preview_wires(&mut self, _pt: Vec3) -> Vec<WireModel> { vec![] }
fn on_preview_wires(&mut self, _pt: Vec3) -> Vec<WireModel> {
vec![]
}
}

View file

@ -31,19 +31,26 @@ enum Step {
impl AtteditCommand {
pub fn new() -> Self {
Self { step: Step::SelectInsert }
Self {
step: Step::SelectInsert,
}
}
}
impl CadCommand for AtteditCommand {
fn name(&self) -> &'static str { "ATTEDIT" }
fn name(&self) -> &'static str {
"ATTEDIT"
}
fn prompt(&self) -> String {
match &self.step {
Step::SelectInsert => "ATTEDIT Select block with attributes:".to_string(),
Step::EditAttr { attrs, idx, .. } => {
let (tag, val) = &attrs[*idx];
format!("ATTEDIT {} = <{}> (Enter to keep, type new value):", tag, val)
format!(
"ATTEDIT {} = <{}> (Enter to keep, type new value):",
tag, val
)
}
}
}
@ -61,7 +68,7 @@ impl CadCommand for AtteditCommand {
// Instead, signal the host to call prepare_attedit().
self.step = Step::EditAttr {
handle,
attrs: vec![], // will be filled by init_with_attrs() in cmd_result.rs
attrs: vec![], // will be filled by init_with_attrs() in cmd_result.rs
idx: 0,
};
CmdResult::NeedPoint
@ -76,7 +83,9 @@ impl CadCommand for AtteditCommand {
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let Step::EditAttr { handle, attrs, idx } = &mut self.step else { return None; };
let Step::EditAttr { handle, attrs, idx } = &mut self.step else {
return None;
};
let handle = *handle;
// Update the current attribute value if the user typed something.
@ -99,9 +108,15 @@ impl CadCommand for AtteditCommand {
None
}
fn on_point(&mut self, _pt: Vec3) -> CmdResult { CmdResult::NeedPoint }
fn on_enter(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_preview_wires(&mut self, _pt: Vec3) -> Vec<WireModel> { vec![] }
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_preview_wires(&mut self, _pt: Vec3) -> Vec<WireModel> {
vec![]
}
fn attedit_pending_handle(&self) -> Option<acadrust::Handle> {
if let Step::EditAttr { handle, attrs, .. } = &self.step {
@ -122,10 +137,7 @@ impl CadCommand for AtteditCommand {
/// Make a sentinel entity carrying the edited attribute values.
/// Encodes all (tag=value) pairs in the layer field as "tag1\x01val1\x02tag2\x01val2...".
fn make_attedit_sentinel(
_handle: acadrust::Handle,
pairs: &[(String, String)],
) -> EntityType {
fn make_attedit_sentinel(_handle: acadrust::Handle, pairs: &[(String, String)]) -> EntityType {
let encoded: String = pairs
.iter()
.map(|(t, v)| format!("{}\x01{}", t, v))
@ -141,19 +153,20 @@ fn make_attedit_sentinel(
/// Apply edited attribute values to an INSERT entity in the document.
/// Called from `cmd_result.rs` when the sentinel is detected.
pub fn apply_attedit(
doc: &mut acadrust::CadDocument,
handle: acadrust::Handle,
encoded: &str,
) {
let Some(EntityType::Insert(ins)) = doc.get_entity_mut(handle) else { return; };
pub fn apply_attedit(doc: &mut acadrust::CadDocument, handle: acadrust::Handle, encoded: &str) {
let Some(EntityType::Insert(ins)) = doc.get_entity_mut(handle) else {
return;
};
for pair in encoded.split('\x02') {
let mut parts = pair.splitn(2, '\x01');
let Some(tag) = parts.next() else { continue; };
let Some(val) = parts.next() else { continue; };
let Some(tag) = parts.next() else {
continue;
};
let Some(val) = parts.next() else {
continue;
};
if let Some(attrib) = ins.attributes.iter_mut().find(|a| a.tag == tag) {
attrib.set_value(val);
}
}
}

View file

@ -7,12 +7,16 @@
//
// BREAK @ (at-sign as second point) → Break at a single point (splits without gap).
use acadrust::entities::{Arc as ArcEnt, Ellipse as EllipseEnt, Line as LineEnt, LwPolyline, Spline as SplineEnt};
use truck_modeling::base::{BoundedCurve, Cut};
use crate::modules::home::modify::spline_ops::{bspline_to_spline, spline_nearest_t, spline_to_bspline};
use crate::modules::home::modify::spline_ops::{
bspline_to_spline, spline_nearest_t, spline_to_bspline,
};
use acadrust::entities::{
Arc as ArcEnt, Ellipse as EllipseEnt, Line as LineEnt, LwPolyline, Spline as SplineEnt,
};
use acadrust::types::Vector3;
use acadrust::{EntityType, Handle};
use glam::Vec3;
use truck_modeling::base::{BoundedCurve, Cut};
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
@ -37,17 +41,21 @@ pub fn tool() -> ToolDef {
pub fn break_entity(entity: &EntityType, p1: Vec3, p2: Vec3) -> Option<Vec<EntityType>> {
match entity {
EntityType::Line(line) => Some(break_line(line, p1, p2)),
EntityType::Arc(arc) => Some(break_arc(arc, p1, p2)),
EntityType::Circle(c) => Some(break_circle(c, p1, p2)),
EntityType::Arc(arc) => Some(break_arc(arc, p1, p2)),
EntityType::Circle(c) => Some(break_circle(c, p1, p2)),
EntityType::LwPolyline(p) => Some(break_lwpolyline(p, p1, p2)),
EntityType::Ellipse(e) => Some(break_ellipse(e, p1, p2)),
EntityType::Spline(s) => Some(break_spline(s, p1, p2)),
EntityType::Ellipse(e) => Some(break_ellipse(e, p1, p2)),
EntityType::Spline(s) => Some(break_spline(s, p1, p2)),
_ => None,
}
}
fn break_line(line: &LineEnt, p1: Vec3, p2: Vec3) -> Vec<EntityType> {
let s = Vec3::new(line.start.x as f32, line.start.z as f32, line.start.y as f32);
let s = Vec3::new(
line.start.x as f32,
line.start.z as f32,
line.start.y as f32,
);
let e = Vec3::new(line.end.x as f32, line.end.z as f32, line.end.y as f32);
let dir = e - s;
let len2 = dir.length_squared();
@ -189,7 +197,9 @@ fn break_ellipse(ell: &EllipseEnt, p1: Vec3, p2: Vec3) -> Vec<EntityType> {
let cx = ell.center.x;
let cy = ell.center.y;
let a = (ell.major_axis.x.powi(2) + ell.major_axis.y.powi(2)).sqrt();
if a < 1e-9 { return vec![EntityType::Ellipse(ell.clone())]; }
if a < 1e-9 {
return vec![EntityType::Ellipse(ell.clone())];
}
let _b = a * ell.minor_axis_ratio;
let nx = ell.major_axis.x / a;
let ny = ell.major_axis.y / a;
@ -198,7 +208,7 @@ fn break_ellipse(ell: &EllipseEnt, p1: Vec3, p2: Vec3) -> Vec<EntityType> {
let param_of = |pt: Vec3| -> f64 {
let rx = pt.x as f64 - cx;
let ry = pt.z as f64 - cy; // Y-up: DXF Y → world Z
let xl = rx * nx + ry * ny;
let xl = rx * nx + ry * ny;
let yl = -rx * ny + ry * nx;
yl.atan2(xl) // atan2(yl/b*b, xl/a*a) simplifies to atan2(yl,xl) for ordering
};
@ -212,11 +222,22 @@ fn break_ellipse(ell: &EllipseEnt, p1: Vec3, p2: Vec3) -> Vec<EntityType> {
// Clamp both params to arc range (same logic as clamp_to_arc for arcs)
let span_deg = {
let s = t1 - t0;
if s <= 0.0 { s + std::f64::consts::TAU } else { s }
if s <= 0.0 {
s + std::f64::consts::TAU
} else {
s
}
};
let clamp = |a: f64| -> f64 {
let rel = ((a - t0) % std::f64::consts::TAU + std::f64::consts::TAU) % std::f64::consts::TAU;
if rel <= span_deg { a } else if rel < span_deg + (std::f64::consts::TAU - span_deg) / 2.0 { t1 } else { t0 }
let rel =
((a - t0) % std::f64::consts::TAU + std::f64::consts::TAU) % std::f64::consts::TAU;
if rel <= span_deg {
a
} else if rel < span_deg + (std::f64::consts::TAU - span_deg) / 2.0 {
t1
} else {
t0
}
};
let a1_on = clamp(pa1);
let a2_on = clamp(pa2);
@ -229,7 +250,7 @@ fn break_ellipse(ell: &EllipseEnt, p1: Vec3, p2: Vec3) -> Vec<EntityType> {
let mut result = ell.clone();
result.common.handle = Handle::NULL;
result.start_parameter = a2_on;
result.end_parameter = a1_on;
result.end_parameter = a1_on;
vec![EntityType::Ellipse(result)]
}
@ -257,11 +278,16 @@ fn clamp_to_arc(a: f32, start: f32, end: f32) -> f32 {
/// Find the index of the polyline vertex closest to `pt`.
fn nearest_pline_param(p: &LwPolyline, pt: Vec3) -> usize {
p.vertices.iter().enumerate().min_by_key(|(_, v)| {
let dx = v.location.x as f32 - pt.x;
let dy = v.location.y as f32 - pt.z;
((dx * dx + dy * dy) * 1e6) as i64
}).map(|(i, _)| i).unwrap_or(0)
p.vertices
.iter()
.enumerate()
.min_by_key(|(_, v)| {
let dx = v.location.x as f32 - pt.x;
let dy = v.location.y as f32 - pt.z;
((dx * dx + dy * dy) * 1e6) as i64
})
.map(|(i, _)| i)
.unwrap_or(0)
}
fn world_to_dxf(v: Vec3) -> Vec3 {
@ -307,13 +333,13 @@ fn break_spline(spl: &SplineEnt, p1: Vec3, p2: Vec3) -> Vec<EntityType> {
// Left piece [t0, ta]
if ta - t0 > 1e-9 {
let mut left = bs.clone();
let _right = left.cut(ta); // left = [t0, ta]
let _right = left.cut(ta); // left = [t0, ta]
result.push(EntityType::Spline(bspline_to_spline(&left, spl)));
}
// Right piece [tb, t_end]
if t_end - tb > 1e-9 {
let mut full = bs.clone();
let right = full.cut(tb); // right = [tb, t_end]
let right = full.cut(tb); // right = [tb, t_end]
result.push(EntityType::Spline(bspline_to_spline(&right, spl)));
}
result
@ -330,12 +356,17 @@ pub struct BreakInteractiveCommand {
impl BreakInteractiveCommand {
pub fn new() -> Self {
Self { target: None, p1: None }
Self {
target: None,
p1: None,
}
}
}
impl CadCommand for BreakInteractiveCommand {
fn name(&self) -> &'static str { "BREAK" }
fn name(&self) -> &'static str {
"BREAK"
}
fn prompt(&self) -> String {
if self.target.is_none() {
@ -397,7 +428,9 @@ impl BreakAtPointCommand {
}
impl CadCommand for BreakAtPointCommand {
fn name(&self) -> &'static str { "BREAKATPOINT" }
fn name(&self) -> &'static str {
"BREAKATPOINT"
}
fn prompt(&self) -> String {
if self.target.is_none() {
@ -421,7 +454,11 @@ impl CadCommand for BreakAtPointCommand {
fn on_point(&mut self, pt: Vec3) -> CmdResult {
match self.target {
Some(handle) => CmdResult::BreakEntity { handle, p1: pt, p2: pt },
Some(handle) => CmdResult::BreakEntity {
handle,
p1: pt,
p2: pt,
},
None => CmdResult::Cancel,
}
}

View file

@ -310,10 +310,16 @@ fn explode_mline(ml: &MLine) -> Vec<EntityType> {
// Helper: build a Line from two Vector3 positions.
let make_line = |common: &acadrust::entities::EntityCommon,
s: &acadrust::types::Vector3,
e: &acadrust::types::Vector3| -> EntityType {
e: &acadrust::types::Vector3|
-> EntityType {
let mut c = common.clone();
c.handle = Handle::NULL;
EntityType::Line(LineEnt { common: c, start: s.clone(), end: e.clone(), ..LineEnt::new() })
EntityType::Line(LineEnt {
common: c,
start: s.clone(),
end: e.clone(),
..LineEnt::new()
})
};
// For each segment, emit the center-spine line and the two ±scale/2 offset lines.
@ -361,20 +367,30 @@ fn explode_dimension(dim: &Dimension) -> Vec<EntityType> {
let make_seg = |a: &Vector3, b: &Vector3, common: &EntityCommon| -> EntityType {
let mut c = common.clone();
c.handle = Handle::NULL;
EntityType::Line(LineEnt { common: c, start: a.clone(), end: b.clone(), ..LineEnt::new() })
EntityType::Line(LineEnt {
common: c,
start: a.clone(),
end: b.clone(),
..LineEnt::new()
})
};
let v3 = |x: f64, y: f64, z: f64| Vector3::new(x, y, z);
match dim {
Dimension::Aligned(d) => {
let fx = d.first_point.x; let fy = d.first_point.y;
let sx = d.second_point.x; let sy = d.second_point.y;
let dx_s = sx - fx; let dy_s = sy - fy;
let len = (dx_s*dx_s + dy_s*dy_s).sqrt().max(1e-12);
let fx = d.first_point.x;
let fy = d.first_point.y;
let sx = d.second_point.x;
let sy = d.second_point.y;
let dx_s = sx - fx;
let dy_s = sy - fy;
let len = (dx_s * dx_s + dy_s * dy_s).sqrt().max(1e-12);
let axis_angle = dy_s.atan2(dx_s);
let perp_x = -(axis_angle.sin()); let perp_y = axis_angle.cos();
let offset = (d.definition_point.x - fx) * perp_x + (d.definition_point.y - fy) * perp_y;
let perp_x = -(axis_angle.sin());
let perp_y = axis_angle.cos();
let offset =
(d.definition_point.x - fx) * perp_x + (d.definition_point.y - fy) * perp_y;
let d1 = v3(fx + perp_x * offset, fy + perp_y * offset, d.first_point.z);
let d2 = v3(sx + perp_x * offset, sy + perp_y * offset, d.second_point.z);
result.push(make_seg(&d.first_point, &d1, &common));
@ -384,10 +400,14 @@ fn explode_dimension(dim: &Dimension) -> Vec<EntityType> {
}
Dimension::Linear(d) => {
let angle = d.rotation.to_radians();
let perp_x = -(angle.sin()); let perp_y = angle.cos();
let fx = d.first_point.x; let fy = d.first_point.y;
let sx = d.second_point.x; let sy = d.second_point.y;
let offset = (d.definition_point.x - fx) * perp_x + (d.definition_point.y - fy) * perp_y;
let perp_x = -(angle.sin());
let perp_y = angle.cos();
let fx = d.first_point.x;
let fy = d.first_point.y;
let sx = d.second_point.x;
let sy = d.second_point.y;
let offset =
(d.definition_point.x - fx) * perp_x + (d.definition_point.y - fy) * perp_y;
let d1 = v3(fx + perp_x * offset, fy + perp_y * offset, d.first_point.z);
let d2 = v3(sx + perp_x * offset, sy + perp_y * offset, d.second_point.z);
result.push(make_seg(&d.first_point, &d1, &common));
@ -416,15 +436,20 @@ fn explode_dimension(dim: &Dimension) -> Vec<EntityType> {
// Text entity for the dimension label
let text_val = if let Some(u) = &base.user_text {
if !u.trim().is_empty() { u.clone() } else { format!("{:.4}", dim.measurement()) }
if !u.trim().is_empty() {
u.clone()
} else {
format!("{:.4}", dim.measurement())
}
} else if !base.text.trim().is_empty() {
base.text.clone()
} else {
match dim {
Dimension::Radius(_) => format!("R{:.4}", dim.measurement()),
Dimension::Radius(_) => format!("R{:.4}", dim.measurement()),
Dimension::Diameter(_) => format!("Ø{:.4}", dim.measurement()),
Dimension::Angular2Ln(_) | Dimension::Angular3Pt(_) =>
format!("{:.2}°", dim.measurement()),
Dimension::Angular2Ln(_) | Dimension::Angular3Pt(_) => {
format!("{:.2}°", dim.measurement())
}
_ => format!("{:.4}", dim.measurement()),
}
};

View file

@ -306,16 +306,28 @@ fn entity_pts(e: &EntityType) -> Vec<[f32; 3]> {
/// Extract center, radius, start/end angle (radians), elevation from an arc.
fn arc_geom(a: &ArcEnt) -> ([f64; 2], f64, f64, f64, f64) {
([a.center.x, a.center.y], a.radius, a.start_angle, a.end_angle, a.center.z)
(
[a.center.x, a.center.y],
a.radius,
a.start_angle,
a.end_angle,
a.center.z,
)
}
/// Normalize angle to [0, 2π).
fn norm_angle(a: f64) -> f64 { ((a % TAU) + TAU) % TAU }
fn norm_angle(a: f64) -> f64 {
((a % TAU) + TAU) % TAU
}
/// Return the CCW angular span from `start` to `end`.
fn arc_span(start: f64, end: f64) -> f64 {
let s = (end - start).rem_euclid(TAU);
if s < 1e-6 { TAU } else { s }
if s < 1e-6 {
TAU
} else {
s
}
}
/// Project a pick point onto an arc: return the angle in radians.
@ -328,9 +340,13 @@ fn arc_angle_at(center: [f64; 2], pt: [f64; 2]) -> f64 {
fn clamp_angle_to_arc(a: f64, start: f64, end: f64) -> f64 {
let span = arc_span(start, end);
let rel = (a - start).rem_euclid(TAU);
if rel <= span { a }
else if rel < span + (TAU - span) / 2.0 { end }
else { start }
if rel <= span {
a
} else if rel < span + (TAU - span) / 2.0 {
end
} else {
start
}
}
/// Trim an arc so it goes from `new_start` to `new_end` (both in radians).
@ -338,7 +354,7 @@ fn trim_arc(orig: &ArcEnt, new_start: f64, new_end: f64) -> ArcEnt {
let mut a = orig.clone();
a.common.handle = Handle::NULL;
a.start_angle = norm_angle(new_start);
a.end_angle = norm_angle(new_end);
a.end_angle = norm_angle(new_end);
a
}
@ -351,7 +367,9 @@ fn line_circle_ts(px: f64, py: f64, dx: f64, dy: f64, cx: f64, cy: f64, r: f64)
let b = 2.0 * (fx * dx + fy * dy);
let c = fx * fx + fy * fy - r * r;
let disc = b * b - 4.0 * a * c;
if disc < 0.0 { return vec![]; }
if disc < 0.0 {
return vec![];
}
let sq = disc.sqrt();
if disc < 1e-14 {
vec![(-b) / (2.0 * a)]
@ -365,10 +383,14 @@ fn circle_circle_pts(c1: [f64; 2], r1: f64, c2: [f64; 2], r2: f64) -> Vec<[f64;
let dx = c2[0] - c1[0];
let dy = c2[1] - c1[1];
let d = (dx * dx + dy * dy).sqrt();
if d < 1e-12 || d > r1 + r2 + 1e-9 || d < (r1 - r2).abs() - 1e-9 { return vec![]; }
if d < 1e-12 || d > r1 + r2 + 1e-9 || d < (r1 - r2).abs() - 1e-9 {
return vec![];
}
let a = (r1 * r1 - r2 * r2 + d * d) / (2.0 * d);
let h2 = r1 * r1 - a * a;
if h2 < 0.0 { return vec![]; }
if h2 < 0.0 {
return vec![];
}
let h = h2.sqrt();
let mx = c1[0] + a * dx / d;
let my = c1[1] + a * dy / d;
@ -387,7 +409,11 @@ fn circle_circle_pts(c1: [f64; 2], r1: f64, c2: [f64; 2], r2: f64) -> Vec<[f64;
/// Find the index of the LwPolyline segment nearest to `click` (DXF XY).
fn lwpoly_nearest_seg(poly: &LwPolyline, click: [f64; 2]) -> usize {
let n = poly.vertices.len();
let seg_count = if poly.is_closed { n } else { n.saturating_sub(1) };
let seg_count = if poly.is_closed {
n
} else {
n.saturating_sub(1)
};
let mut best_idx = 0;
let mut best_dist = f64::MAX;
for i in 0..seg_count {
@ -424,7 +450,7 @@ fn lwpoly_seg_as_line(poly: &LwPolyline, seg_idx: usize) -> LineEnt {
l.common = poly.common.clone();
l.common.handle = Handle::NULL;
l.start = Vector3::new(v0.location.x, v0.location.y, poly.elevation);
l.end = Vector3::new(v1.location.x, v1.location.y, poly.elevation);
l.end = Vector3::new(v1.location.x, v1.location.y, poly.elevation);
l
}
@ -441,10 +467,10 @@ fn compute_bulge(t1: [f64; 2], t2: [f64; 2], arc_center: [f64; 2]) -> f64 {
let to_c = [arc_center[0] - mid[0], arc_center[1] - mid[1]];
let cross = chord[0] * to_c[1] - chord[1] * to_c[0];
if cross >= 0.0 {
(span_ccw / 4.0).tan() // CCW arc
(span_ccw / 4.0).tan() // CCW arc
} else {
let span_cw = TAU - span_ccw;
-(span_cw / 4.0).tan() // CW arc
-(span_cw / 4.0).tan() // CW arc
}
}
@ -503,7 +529,11 @@ fn lwpoly_shorten_seg(
fn lwpoly_pts(poly: &LwPolyline) -> Vec<[f32; 3]> {
let elev = poly.elevation as f32;
let n = poly.vertices.len();
let seg_count = if poly.is_closed { n } else { n.saturating_sub(1) };
let seg_count = if poly.is_closed {
n
} else {
n.saturating_sub(1)
};
let mut pts = Vec::with_capacity(seg_count * 2);
for i in 0..seg_count {
let v0 = &poly.vertices[i];
@ -522,27 +552,35 @@ enum FilletEntity {
Line(LineEnt),
Arc(ArcEnt),
/// A segment of an LwPolyline identified by its entity handle and segment index.
LwPoly { poly: LwPolyline, handle: Handle, seg_idx: usize },
LwPoly {
poly: LwPolyline,
handle: Handle,
seg_idx: usize,
},
}
impl FilletEntity {
fn from_entity(e: &EntityType) -> Option<Self> {
match e {
EntityType::Line(l) => Some(Self::Line(l.clone())),
EntityType::Arc(a) => Some(Self::Arc(a.clone())),
EntityType::Arc(a) => Some(Self::Arc(a.clone())),
_ => None,
}
}
fn from_lwpoly(poly: &LwPolyline, handle: Handle, click: [f64; 2]) -> Self {
let seg_idx = lwpoly_nearest_seg(poly, click);
Self::LwPoly { poly: poly.clone(), handle, seg_idx }
Self::LwPoly {
poly: poly.clone(),
handle,
seg_idx,
}
}
fn to_entity_type(&self) -> EntityType {
match self {
Self::Line(l) => EntityType::Line(l.clone()),
Self::Arc(a) => EntityType::Arc(a.clone()),
Self::Arc(a) => EntityType::Arc(a.clone()),
Self::LwPoly { poly, .. } => EntityType::LwPolyline(poly.clone()),
}
}
@ -550,7 +588,7 @@ impl FilletEntity {
fn elevation(&self) -> f64 {
match self {
Self::Line(l) => l.start.z,
Self::Arc(a) => a.center.z,
Self::Arc(a) => a.center.z,
Self::LwPoly { poly, .. } => poly.elevation,
}
}
@ -587,23 +625,35 @@ fn compute_fillet_entities(
}
// ── LwPoly × LwPoly (same entity — corner fillet) ─────────────────
(
FilletEntity::LwPoly { poly: p1, handle: h1, seg_idx: s1 },
FilletEntity::LwPoly { handle: h2, seg_idx: s2, .. },
FilletEntity::LwPoly {
poly: p1,
handle: h1,
seg_idx: s1,
},
FilletEntity::LwPoly {
handle: h2,
seg_idx: s2,
..
},
) if h1 == h2 => {
// Adjacent segments share a corner vertex — fillet that corner.
let (low, high) = if *s1 < *s2 { (*s1, *s2) } else { (*s2, *s1) };
let n = p1.vertices.len();
// Segments must be consecutive (high == low+1, or wrap-around on closed poly).
let consecutive = high == low + 1
|| (p1.is_closed && low == 0 && high == n.saturating_sub(1));
let consecutive =
high == low + 1 || (p1.is_closed && low == 0 && high == n.saturating_sub(1));
if !consecutive {
return None;
}
let corner_idx = high % n; // vertex shared by both segments
let l1 = lwpoly_seg_as_line(p1, low);
let l2 = lwpoly_seg_as_line(p1, high % n.max(1)); // seg after corner
// Re-map click to whichever segment each was picked on.
let (c1, c2) = if *s1 == low { (click1, click2) } else { (click2, click1) };
// Re-map click to whichever segment each was picked on.
let (c1, c2) = if *s1 == low {
(click1, click2)
} else {
(click2, click1)
};
match compute_fillet(&l1, c1, &l2, c2, radius)? {
(EntityType::Line(tl1), EntityType::Line(tl2), maybe_arc) => {
let t1 = [tl1.end.x, tl1.end.y]; // trimmed end of seg before corner
@ -624,8 +674,16 @@ fn compute_fillet_entities(
}
// ── LwPoly × LwPoly (different entities) ──────────────────────────
(
FilletEntity::LwPoly { poly: p1, seg_idx: s1, .. },
FilletEntity::LwPoly { poly: p2, seg_idx: s2, .. },
FilletEntity::LwPoly {
poly: p1,
seg_idx: s1,
..
},
FilletEntity::LwPoly {
poly: p2,
seg_idx: s2,
..
},
) => {
let l1 = lwpoly_seg_as_line(p1, *s1);
let l2 = lwpoly_seg_as_line(p2, *s2);
@ -633,7 +691,11 @@ fn compute_fillet_entities(
if let (EntityType::Line(tl1), EntityType::Line(tl2)) = (&tl1_e, &tl2_e) {
let np1 = rebuild_poly_from_trimmed_line(p1, *s1, &l1, tl1);
let np2 = rebuild_poly_from_trimmed_line(p2, *s2, &l2, tl2);
Some((EntityType::LwPolyline(np1), EntityType::LwPolyline(np2), maybe_arc))
Some((
EntityType::LwPolyline(np1),
EntityType::LwPolyline(np2),
maybe_arc,
))
} else {
None
}
@ -692,9 +754,17 @@ fn rebuild_poly_from_trimmed_line(
trimmed: &LineEnt,
) -> LwPolyline {
let start_moved = (trimmed.start.x - orig.start.x).hypot(trimmed.start.y - orig.start.y) > 1e-9;
let end_moved = (trimmed.end.x - orig.end.x ).hypot(trimmed.end.y - orig.end.y ) > 1e-9;
let new_start = if start_moved { Some([trimmed.start.x, trimmed.start.y]) } else { None };
let new_end = if end_moved { Some([trimmed.end.x, trimmed.end.y ]) } else { None };
let end_moved = (trimmed.end.x - orig.end.x).hypot(trimmed.end.y - orig.end.y) > 1e-9;
let new_start = if start_moved {
Some([trimmed.start.x, trimmed.start.y])
} else {
None
};
let new_end = if end_moved {
Some([trimmed.end.x, trimmed.end.y])
} else {
None
};
lwpoly_shorten_seg(poly, seg_idx, new_start, new_end)
}
@ -715,12 +785,13 @@ fn fillet_line_arc(
if radius < 1e-9 {
// r=0: trim to intersection (nearest to each click)
let t_best = ts.iter().copied()
.min_by(|a, b| {
let da = (p1[0]+a*u[0]-click_line[0]).powi(2)+(p1[1]+a*u[1]-click_line[1]).powi(2);
let db = (p1[0]+b*u[0]-click_line[0]).powi(2)+(p1[1]+b*u[1]-click_line[1]).powi(2);
da.partial_cmp(&db).unwrap()
})?;
let t_best = ts.iter().copied().min_by(|a, b| {
let da = (p1[0] + a * u[0] - click_line[0]).powi(2)
+ (p1[1] + a * u[1] - click_line[1]).powi(2);
let db = (p1[0] + b * u[0] - click_line[0]).powi(2)
+ (p1[1] + b * u[1] - click_line[1]).powi(2);
da.partial_cmp(&db).unwrap()
})?;
let ix = p1[0] + t_best * u[0];
let iy = p1[1] + t_best * u[1];
@ -749,7 +820,7 @@ fn fillet_line_arc(
// and at distance |ar ± radius| from the arc center.
// Sign: outside=ar+radius (external), inside=ar-radius (internal).
let perp_x = -u[1];
let perp_y = u[0];
let perp_y = u[0];
let mut best: Option<(EntityType, EntityType, EntityType)> = None;
let mut best_dist = f64::MAX;
@ -759,7 +830,9 @@ fn fillet_line_arc(
// Candidate fillet center offset from line by ±radius in perp direction.
// Find point on offset line closest to arc center at distance |ar + sign*radius|.
let off_dist = ar + sign_circle * radius;
if off_dist < 1e-9 { continue; }
if off_dist < 1e-9 {
continue;
}
// The fillet center is at distance `off_dist` from the arc center
// and also at distance `radius` from the line (perpendicular).
@ -769,38 +842,49 @@ fn fillet_line_arc(
let qx = p1[0] + sign_perp * radius * perp_x - ac[0];
let qy = p1[1] + sign_perp * radius * perp_y - ac[1];
// (qx + t*u[0])^2 + (qy + t*u[1])^2 = off_dist^2
let qa = u[0]*u[0] + u[1]*u[1]; // = 1.0
let qb = 2.0*(qx*u[0] + qy*u[1]);
let qc = qx*qx + qy*qy - off_dist*off_dist;
let disc = qb*qb - 4.0*qa*qc;
if disc < 0.0 { continue; }
let qa = u[0] * u[0] + u[1] * u[1]; // = 1.0
let qb = 2.0 * (qx * u[0] + qy * u[1]);
let qc = qx * qx + qy * qy - off_dist * off_dist;
let disc = qb * qb - 4.0 * qa * qc;
if disc < 0.0 {
continue;
}
let sq = disc.sqrt();
for &sign_t in &[-1.0_f64, 1.0_f64] {
let t_fc = (-qb + sign_t * sq) / (2.0 * qa);
let fc = [p1[0] + t_fc*u[0] + sign_perp*radius*perp_x,
p1[1] + t_fc*u[1] + sign_perp*radius*perp_y];
let fc = [
p1[0] + t_fc * u[0] + sign_perp * radius * perp_x,
p1[1] + t_fc * u[1] + sign_perp * radius * perp_y,
];
// Tangent point on the line
let tp_line = [p1[0] + t_fc*u[0], p1[1] + t_fc*u[1]];
let tp_line = [p1[0] + t_fc * u[0], p1[1] + t_fc * u[1]];
// Tangent point on the arc circle
let fd = [(ac[0] - fc[0]), (ac[1] - fc[1])];
let fdl = (fd[0]*fd[0]+fd[1]*fd[1]).sqrt().max(1e-12);
let tp_arc = [ac[0] + fd[0]/fdl * ar, ac[1] + fd[1]/fdl * ar];
let fdl = (fd[0] * fd[0] + fd[1] * fd[1]).sqrt().max(1e-12);
let tp_arc = [ac[0] + fd[0] / fdl * ar, ac[1] + fd[1] / fdl * ar];
// The tangent point on the arc must be within the arc's angular range
let tp_arc_angle = arc_angle_at(ac, tp_arc);
let tp_arc_clamped = clamp_angle_to_arc(tp_arc_angle, a_start, a_end);
if (norm_angle(tp_arc_angle) - norm_angle(tp_arc_clamped)).abs() > 0.01 { continue; }
if (norm_angle(tp_arc_angle) - norm_angle(tp_arc_clamped)).abs() > 0.01 {
continue;
}
// The tangent point on the line must be on the correct side of the click
// (prefer the intersection closest to the click)
let dist_to_click_line = (tp_line[0]-click_line[0]).hypot(tp_line[1]-click_line[1]);
let dist_to_click_arc = (tp_arc[0]-click_arc[0]).hypot(tp_arc[1]-click_arc[1]);
let dist_to_click_line =
(tp_line[0] - click_line[0]).hypot(tp_line[1] - click_line[1]);
let dist_to_click_arc = (tp_arc[0] - click_arc[0]).hypot(tp_arc[1] - click_arc[1]);
let dist_total = dist_to_click_line + dist_to_click_arc;
if dist_total >= best_dist { continue; }
if dist_total >= best_dist {
continue;
}
// Build trimmed line
let Some(new_line) = trim_line_to_point(line, tp_line, click_line) else { continue };
let Some(new_line) = trim_line_to_point(line, tp_line, click_line) else {
continue;
};
// Build trimmed arc
let arc_click_angle = arc_angle_at(ac, click_arc);
@ -814,19 +898,28 @@ fn fillet_line_arc(
// Build fillet arc angles
let fa_line = arc_angle_at(fc, tp_line);
let fa_arc = arc_angle_at(fc, tp_arc);
let cross = (tp_line[0]-fc[0])*(tp_arc[1]-fc[1]) - (tp_line[1]-fc[1])*(tp_arc[0]-fc[0]);
let (fstart, fend) = if cross >= 0.0 { (fa_line, fa_arc) } else { (fa_arc, fa_line) };
let fa_arc = arc_angle_at(fc, tp_arc);
let cross = (tp_line[0] - fc[0]) * (tp_arc[1] - fc[1])
- (tp_line[1] - fc[1]) * (tp_arc[0] - fc[0]);
let (fstart, fend) = if cross >= 0.0 {
(fa_line, fa_arc)
} else {
(fa_arc, fa_line)
};
let mut fillet_arc = ArcEnt::new();
fillet_arc.common = line.common.clone();
fillet_arc.common.handle = Handle::NULL;
fillet_arc.center = Vector3::new(fc[0], fc[1], z);
fillet_arc.radius = radius;
fillet_arc.start_angle = norm_angle(fstart);
fillet_arc.end_angle = norm_angle(fend);
fillet_arc.end_angle = norm_angle(fend);
best_dist = dist_total;
best = Some((EntityType::Line(new_line), EntityType::Arc(new_arc), EntityType::Arc(fillet_arc)));
best = Some((
EntityType::Line(new_line),
EntityType::Arc(new_arc),
EntityType::Arc(fillet_arc),
));
}
}
}
@ -849,12 +942,17 @@ fn fillet_arc_arc(
if radius < 1e-9 {
// r=0: trim both arcs to their intersection point
let pts = circle_circle_pts(c1, r1, c2, r2);
if pts.is_empty() { return None; }
if pts.is_empty() {
return None;
}
// Pick the intersection point nearest to the average of the two clicks
let cx = (click1[0] + click2[0]) / 2.0;
let cy = (click1[1] + click2[1]) / 2.0;
let ip = *pts.iter().min_by(|a, b| {
(a[0]-cx).hypot(a[1]-cy).partial_cmp(&(b[0]-cx).hypot(b[1]-cy)).unwrap()
(a[0] - cx)
.hypot(a[1] - cy)
.partial_cmp(&(b[0] - cx).hypot(b[1] - cy))
.unwrap()
})?;
let ia1 = arc_angle_at(c1, ip);
@ -885,31 +983,41 @@ fn fillet_arc_arc(
for sign1 in &[-1.0_f64, 1.0_f64] {
let d1 = r1 + sign1 * radius;
if d1 < 1e-9 { continue; }
if d1 < 1e-9 {
continue;
}
for sign2 in &[-1.0_f64, 1.0_f64] {
let d2 = r2 + sign2 * radius;
if d2 < 1e-9 { continue; }
if d2 < 1e-9 {
continue;
}
for fc in circle_circle_pts(c1, d1, c2, d2) {
// Tangent points on each arc
let fd1 = [(c1[0]-fc[0]), (c1[1]-fc[1])];
let fdl1 = (fd1[0]*fd1[0]+fd1[1]*fd1[1]).sqrt().max(1e-12);
let tp1 = [c1[0]+fd1[0]/fdl1*r1, c1[1]+fd1[1]/fdl1*r1];
let fd1 = [(c1[0] - fc[0]), (c1[1] - fc[1])];
let fdl1 = (fd1[0] * fd1[0] + fd1[1] * fd1[1]).sqrt().max(1e-12);
let tp1 = [c1[0] + fd1[0] / fdl1 * r1, c1[1] + fd1[1] / fdl1 * r1];
let fd2 = [(c2[0]-fc[0]), (c2[1]-fc[1])];
let fdl2 = (fd2[0]*fd2[0]+fd2[1]*fd2[1]).sqrt().max(1e-12);
let tp2 = [c2[0]+fd2[0]/fdl2*r2, c2[1]+fd2[1]/fdl2*r2];
let fd2 = [(c2[0] - fc[0]), (c2[1] - fc[1])];
let fdl2 = (fd2[0] * fd2[0] + fd2[1] * fd2[1]).sqrt().max(1e-12);
let tp2 = [c2[0] + fd2[0] / fdl2 * r2, c2[1] + fd2[1] / fdl2 * r2];
// Tangent points must lie within respective arc ranges
let tp1a = arc_angle_at(c1, tp1);
let tp2a = arc_angle_at(c2, tp2);
let tc1 = clamp_angle_to_arc(tp1a, s1, e1);
let tc2 = clamp_angle_to_arc(tp2a, s2, e2);
if (norm_angle(tp1a) - norm_angle(tc1)).abs() > 0.01 { continue; }
if (norm_angle(tp2a) - norm_angle(tc2)).abs() > 0.01 { continue; }
if (norm_angle(tp1a) - norm_angle(tc1)).abs() > 0.01 {
continue;
}
if (norm_angle(tp2a) - norm_angle(tc2)).abs() > 0.01 {
continue;
}
let dist_total = (tp1[0]-click1[0]).hypot(tp1[1]-click1[1])
+ (tp2[0]-click2[0]).hypot(tp2[1]-click2[1]);
if dist_total >= best_dist { continue; }
let dist_total = (tp1[0] - click1[0]).hypot(tp1[1] - click1[1])
+ (tp2[0] - click2[0]).hypot(tp2[1] - click2[1]);
if dist_total >= best_dist {
continue;
}
let ca1 = clamp_angle_to_arc(arc_angle_at(c1, click1), s1, e1);
let ca2 = clamp_angle_to_arc(arc_angle_at(c2, click2), s2, e2);
@ -927,7 +1035,8 @@ fn fillet_arc_arc(
let fa1 = arc_angle_at(fc, tp1);
let fa2 = arc_angle_at(fc, tp2);
let cross = (tp1[0]-fc[0])*(tp2[1]-fc[1]) - (tp1[1]-fc[1])*(tp2[0]-fc[0]);
let cross =
(tp1[0] - fc[0]) * (tp2[1] - fc[1]) - (tp1[1] - fc[1]) * (tp2[0] - fc[0]);
let (fstart, fend) = if cross >= 0.0 { (fa1, fa2) } else { (fa2, fa1) };
let mut fillet_arc = ArcEnt::new();
fillet_arc.common = a1.common.clone();
@ -935,10 +1044,14 @@ fn fillet_arc_arc(
fillet_arc.center = Vector3::new(fc[0], fc[1], z);
fillet_arc.radius = radius;
fillet_arc.start_angle = norm_angle(fstart);
fillet_arc.end_angle = norm_angle(fend);
fillet_arc.end_angle = norm_angle(fend);
best_dist = dist_total;
best = Some((EntityType::Arc(new_a1), EntityType::Arc(new_a2), EntityType::Arc(fillet_arc)));
best = Some((
EntityType::Arc(new_a1),
EntityType::Arc(new_a2),
EntityType::Arc(fillet_arc),
));
}
}
}
@ -951,16 +1064,18 @@ fn trim_line_to_point(line: &LineEnt, isect: [f64; 2], click: [f64; 2]) -> Optio
let mut l = line.clone();
l.common.handle = Handle::NULL;
let (p1, _, u, len) = line_geom(line);
if len < 1e-12 { return None; }
if len < 1e-12 {
return None;
}
// Parameter of intersection
let t_i = (isect[0]-p1[0])*u[0] + (isect[1]-p1[1])*u[1];
let t_i = (isect[0] - p1[0]) * u[0] + (isect[1] - p1[1]) * u[1];
// Parameter of click
let t_c = (click[0]-p1[0])*u[0] + (click[1]-p1[1])*u[1];
let t_c = (click[0] - p1[0]) * u[0] + (click[1] - p1[1]) * u[1];
// If click is past intersection on the + side, keep intersection..end
if t_c >= t_i {
l.start = Vector3::new(isect[0], isect[1], line.start.z);
} else {
l.end = Vector3::new(isect[0], isect[1], line.end.z);
l.end = Vector3::new(isect[0], isect[1], line.end.z);
}
Some(l)
}
@ -1060,12 +1175,14 @@ impl CadCommand for FilletCommand {
"FILLET Select first object (Line/Arc/LwPolyline) [R={:.4} | type R to change]:",
self.radius
),
FilletStep::WaitingForRadius => format!(
"FILLET Enter fillet radius <{:.4}>:",
self.radius
),
FilletStep::WaitingForRadius => {
format!("FILLET Enter fillet radius <{:.4}>:", self.radius)
}
FilletStep::Second { .. } => {
format!("FILLET Select second object (Line/Arc/LwPolyline) [R={:.4}]:", self.radius)
format!(
"FILLET Select second object (Line/Arc/LwPolyline) [R={:.4}]:",
self.radius
)
}
}
}
@ -1145,7 +1262,11 @@ impl CadCommand for FilletCommand {
other => FilletEntity::from_entity(other),
});
if let Some(e) = e1 {
self.step = FilletStep::Second { h1: handle, e1: e, click1: click };
self.step = FilletStep::Second {
h1: handle,
e1: e,
click1: click,
};
CmdResult::NeedPoint
} else {
CmdResult::NeedPoint
@ -1177,7 +1298,9 @@ impl CadCommand for FilletCommand {
match compute_fillet_entities(&e1, click1, &e2, click, self.radius) {
Some((new_e1, new_e2, maybe_arc)) => {
let mut additions = vec![];
if let Some(arc) = maybe_arc { additions.push(arc); }
if let Some(arc) = maybe_arc {
additions.push(arc);
}
if same_entity {
// Corner fillet: both results are the same rebuilt poly.
CmdResult::ReplaceMany(vec![(h1, vec![new_e1])], additions)
@ -1214,7 +1337,12 @@ impl CadCommand for FilletCommand {
FilletEntity::from_entity(e).map(|fe| entity_pts(&fe.to_entity_type()))
});
if let Some(pts) = pts {
vec![WireModel::solid("fillet_hover".into(), pts, WireModel::CYAN, false)]
vec![WireModel::solid(
"fillet_hover".into(),
pts,
WireModel::CYAN,
false,
)]
} else {
vec![]
}
@ -1240,11 +1368,26 @@ impl CadCommand for FilletCommand {
compute_fillet_entities(&e1, click1, &e2, click, self.radius)
{
let mut out = vec![
WireModel::solid("fillet_e1".into(), entity_pts(&new_e1), WireModel::CYAN, false),
WireModel::solid("fillet_e2".into(), entity_pts(&new_e2), WireModel::CYAN, false),
WireModel::solid(
"fillet_e1".into(),
entity_pts(&new_e1),
WireModel::CYAN,
false,
),
WireModel::solid(
"fillet_e2".into(),
entity_pts(&new_e2),
WireModel::CYAN,
false,
),
];
if let Some(arc) = maybe_arc {
out.push(WireModel::solid("fillet_arc".into(), entity_pts(&arc), WireModel::CYAN, false));
out.push(WireModel::solid(
"fillet_arc".into(),
entity_pts(&arc),
WireModel::CYAN,
false,
));
}
return out;
}
@ -1309,10 +1452,9 @@ impl CadCommand for ChamferCommand {
"CHAMFER Select first line [D1={:.4} D2={:.4} | type D to change]:",
self.dist1, self.dist2
),
ChamferStep::WaitingForDist1 => format!(
"CHAMFER Enter first chamfer distance <{:.4}>:",
self.dist1
),
ChamferStep::WaitingForDist1 => {
format!("CHAMFER Enter first chamfer distance <{:.4}>:", self.dist1)
}
ChamferStep::WaitingForDist2 => format!(
"CHAMFER Enter second chamfer distance <{:.4}>:",
self.dist2

View file

@ -22,18 +22,28 @@ pub struct JoinCommand {
impl JoinCommand {
pub fn new() -> Self {
Self { handles: vec![], gathering: true }
Self {
handles: vec![],
gathering: true,
}
}
}
impl CadCommand for JoinCommand {
fn name(&self) -> &'static str { "JOIN" }
fn prompt(&self) -> String {
format!("JOIN Select objects to join ({} selected, Enter to apply):", self.handles.len())
fn name(&self) -> &'static str {
"JOIN"
}
fn is_selection_gathering(&self) -> bool { self.gathering }
fn prompt(&self) -> String {
format!(
"JOIN Select objects to join ({} selected, Enter to apply):",
self.handles.len()
)
}
fn is_selection_gathering(&self) -> bool {
self.gathering
}
fn on_selection_complete(&mut self, handles: Vec<Handle>) -> CmdResult {
self.handles = handles;
@ -63,10 +73,12 @@ pub fn join_entities(entities: &[(Handle, &EntityType)]) -> Option<(Vec<Handle>,
}
// Split into lines and arcs
let lines: Vec<_> = entities.iter()
let lines: Vec<_> = entities
.iter()
.filter(|(_, e)| matches!(e, EntityType::Line(_)))
.collect();
let arcs: Vec<_> = entities.iter()
let arcs: Vec<_> = entities
.iter()
.filter(|(_, e)| matches!(e, EntityType::Arc(_)))
.collect();
@ -81,93 +93,123 @@ pub fn join_entities(entities: &[(Handle, &EntityType)]) -> Option<(Vec<Handle>,
fn try_join_lines(lines: &[&(Handle, &EntityType)]) -> Option<(Vec<Handle>, Vec<EntityType>)> {
// Collect endpoints in XZ plane
let segs: Vec<_> = lines.iter().map(|(h, e)| {
if let EntityType::Line(l) = e {
let s = Vec3::new(l.start.x as f32, l.start.z as f32, 0.0);
let e2 = Vec3::new(l.end.x as f32, l.end.z as f32, 0.0);
(*h, l, s, e2)
} else { unreachable!() }
}).collect();
let segs: Vec<_> = lines
.iter()
.map(|(h, e)| {
if let EntityType::Line(l) = e {
let s = Vec3::new(l.start.x as f32, l.start.z as f32, 0.0);
let e2 = Vec3::new(l.end.x as f32, l.end.z as f32, 0.0);
(*h, l, s, e2)
} else {
unreachable!()
}
})
.collect();
// Check collinearity: all lines must be parallel and on the same infinite line.
let (_, first_line, s0, e0) = segs[0];
let dir0 = (e0 - s0).normalize_or_zero();
if dir0.length_squared() < 1e-12 { return None; }
if dir0.length_squared() < 1e-12 {
return None;
}
for (_, _, si, ei) in &segs[1..] {
// Check parallel
let diri = (*ei - *si).normalize_or_zero();
let cross = (dir0.x * diri.y - dir0.y * diri.x).abs();
if cross > 1e-4 { return None; }
if cross > 1e-4 {
return None;
}
// Check co-linear (point on same line)
let off = *si - s0;
let perp = (off - dir0 * off.dot(dir0)).length();
if perp > 1e-3 { return None; }
if perp > 1e-3 {
return None;
}
}
// Project all endpoints onto the direction axis
let params: Vec<f32> = segs.iter().flat_map(|(_, _, si, ei)| {
[si.dot(dir0), ei.dot(dir0)]
}).collect();
let params: Vec<f32> = segs
.iter()
.flat_map(|(_, _, si, ei)| [si.dot(dir0), ei.dot(dir0)])
.collect();
let t_min = params.iter().cloned().fold(f32::INFINITY, f32::min);
let t_max = params.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
// Check connectivity: no gaps larger than tolerance
let mut sorted: Vec<[f32; 2]> = segs.iter().map(|(_, _, si, ei)| {
let ta = si.dot(dir0);
let tb = ei.dot(dir0);
[ta.min(tb), ta.max(tb)]
}).collect();
let mut sorted: Vec<[f32; 2]> = segs
.iter()
.map(|(_, _, si, ei)| {
let ta = si.dot(dir0);
let tb = ei.dot(dir0);
[ta.min(tb), ta.max(tb)]
})
.collect();
sorted.sort_by(|a, b| a[0].partial_cmp(&b[0]).unwrap());
for w in sorted.windows(2) {
if w[1][0] > w[0][1] + 1e-3 { return None; } // gap
if w[1][0] > w[0][1] + 1e-3 {
return None;
} // gap
}
let new_start = s0 + dir0 * t_min;
let new_end = s0 + dir0 * t_max;
let new_end = s0 + dir0 * t_max;
let mut merged = first_line.clone();
merged.common.handle = Handle::NULL;
merged.start = vec3_xz_to_v3(new_start, first_line.start.z);
merged.end = vec3_xz_to_v3(new_end, first_line.start.z);
merged.end = vec3_xz_to_v3(new_end, first_line.start.z);
let handles: Vec<Handle> = lines.iter().map(|(h, _)| *h).collect();
Some((handles, vec![EntityType::Line(merged)]))
}
fn try_join_arcs(arcs: &[&(Handle, &EntityType)]) -> Option<(Vec<Handle>, Vec<EntityType>)> {
let segs: Vec<_> = arcs.iter().map(|(h, e)| {
if let EntityType::Arc(a) = e { (*h, a) }
else { unreachable!() }
}).collect();
let segs: Vec<_> = arcs
.iter()
.map(|(h, e)| {
if let EntityType::Arc(a) = e {
(*h, a)
} else {
unreachable!()
}
})
.collect();
// All arcs must share the same center and radius
let (_, first) = segs[0];
let cx = first.center.x;
let cy = first.center.y;
let r = first.radius;
let r = first.radius;
for (_, a) in &segs[1..] {
if (a.center.x - cx).abs() > 1e-3 || (a.center.y - cy).abs() > 1e-3 { return None; }
if (a.radius - r).abs() > 1e-3 { return None; }
if (a.center.x - cx).abs() > 1e-3 || (a.center.y - cy).abs() > 1e-3 {
return None;
}
if (a.radius - r).abs() > 1e-3 {
return None;
}
}
// Collect (start_angle, end_angle) in radians.
let mut intervals: Vec<[f32; 2]> = segs.iter().map(|(_, a)| {
[a.start_angle as f32, a.end_angle as f32]
}).collect();
let mut intervals: Vec<[f32; 2]> = segs
.iter()
.map(|(_, a)| [a.start_angle as f32, a.end_angle as f32])
.collect();
// Sort by start angle
intervals.sort_by(|a, b| a[0].partial_cmp(&b[0]).unwrap());
// Try to merge into one contiguous arc
let merged_start = intervals[0][0];
let mut merged_end = intervals[0][1];
let mut merged_end = intervals[0][1];
for &[s, e] in &intervals[1..] {
let span = (e - merged_end).rem_euclid(std::f32::consts::TAU);
let gap = (s - merged_end).rem_euclid(std::f32::consts::TAU);
let gap = (s - merged_end).rem_euclid(std::f32::consts::TAU);
let _ = span;
if gap > 1e-3 { return None; } // discontinuous
if gap > 1e-3 {
return None;
} // discontinuous
merged_end = e;
}
@ -188,7 +230,7 @@ fn try_join_arcs(arcs: &[&(Handle, &EntityType)]) -> Option<(Vec<Handle>, Vec<En
let mut merged_arc = first.clone();
merged_arc.common.handle = Handle::NULL;
merged_arc.start_angle = merged_start as f64;
merged_arc.end_angle = merged_end as f64;
merged_arc.end_angle = merged_end as f64;
Some((handles, vec![EntityType::Arc(merged_arc)]))
}

View file

@ -7,12 +7,14 @@
//
// The entity is modified at whichever end is closest to the pick point.
use acadrust::entities::{Arc as ArcEnt, Ellipse as EllipseEnt, Line as LineEnt, LwPolyline, Spline as SplineEnt};
use truck_modeling::base::{BoundedCurve, Cut};
use crate::modules::home::modify::spline_ops::{bspline_to_spline, spline_to_bspline};
use acadrust::entities::{
Arc as ArcEnt, Ellipse as EllipseEnt, Line as LineEnt, LwPolyline, Spline as SplineEnt,
};
use acadrust::types::Vector3;
use acadrust::{EntityType, Handle};
use glam::Vec3;
use truck_modeling::base::{BoundedCurve, Cut};
use crate::command::{CadCommand, CmdResult};
@ -27,18 +29,23 @@ enum LenState {
impl LengthenCommand {
pub fn new() -> Self {
Self { state: LenState::PickEntity }
Self {
state: LenState::PickEntity,
}
}
}
impl CadCommand for LengthenCommand {
fn name(&self) -> &'static str { "LENGTHEN" }
fn name(&self) -> &'static str {
"LENGTHEN"
}
fn prompt(&self) -> String {
match &self.state {
LenState::PickEntity => "LENGTHEN Select object:".into(),
LenState::PickOption { .. } =>
"LENGTHEN Enter option [DE <delta> / TO <total> / P <pct>]:".into(),
LenState::PickOption { .. } => {
"LENGTHEN Enter option [DE <delta> / TO <total> / P <pct>]:".into()
}
}
}
@ -47,8 +54,13 @@ impl CadCommand for LengthenCommand {
}
fn on_entity_pick(&mut self, handle: Handle, pt: Vec3) -> CmdResult {
if handle.is_null() { return CmdResult::NeedPoint; }
self.state = LenState::PickOption { handle, pick_pt: pt };
if handle.is_null() {
return CmdResult::NeedPoint;
}
self.state = LenState::PickOption {
handle,
pick_pt: pt,
};
CmdResult::NeedPoint
}
@ -65,22 +77,52 @@ impl CadCommand for LengthenCommand {
let text = text.trim().to_uppercase();
if let Some(rest) = text.strip_prefix("DE ").or_else(|| text.strip_prefix("DE")) {
let delta: f64 = rest.trim().replace(',', ".").parse().ok()?;
Some(CmdResult::LengthenEntity { handle, pick_pt, mode: LenMode::Delta(delta) })
Some(CmdResult::LengthenEntity {
handle,
pick_pt,
mode: LenMode::Delta(delta),
})
} else if let Some(rest) = text.strip_prefix("TO ").or_else(|| text.strip_prefix("TO")) {
let total: f64 = rest.trim().replace(',', ".").parse().ok().filter(|&v: &f64| v > 0.0)?;
Some(CmdResult::LengthenEntity { handle, pick_pt, mode: LenMode::Total(total) })
let total: f64 = rest
.trim()
.replace(',', ".")
.parse()
.ok()
.filter(|&v: &f64| v > 0.0)?;
Some(CmdResult::LengthenEntity {
handle,
pick_pt,
mode: LenMode::Total(total),
})
} else if let Some(rest) = text.strip_prefix("P ").or_else(|| text.strip_prefix("P")) {
let pct: f64 = rest.trim().replace(',', ".").parse().ok().filter(|&v: &f64| v > 0.0)?;
Some(CmdResult::LengthenEntity { handle, pick_pt, mode: LenMode::Percent(pct) })
let pct: f64 = rest
.trim()
.replace(',', ".")
.parse()
.ok()
.filter(|&v: &f64| v > 0.0)?;
Some(CmdResult::LengthenEntity {
handle,
pick_pt,
mode: LenMode::Percent(pct),
})
} else {
// Try plain number as delta
let delta: f64 = text.replace(',', ".").parse().ok()?;
Some(CmdResult::LengthenEntity { handle, pick_pt, mode: LenMode::Delta(delta) })
Some(CmdResult::LengthenEntity {
handle,
pick_pt,
mode: LenMode::Delta(delta),
})
}
}
fn on_point(&mut self, _pt: Vec3) -> CmdResult { CmdResult::NeedPoint }
fn on_enter(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
}
// ── Mode enum (also used in CmdResult) ────────────────────────────────────
@ -98,10 +140,10 @@ pub enum LenMode {
/// `pick_pt` determines which end to extend/trim (closest end is modified).
pub fn lengthen_entity(entity: &EntityType, pick_pt: Vec3, mode: &LenMode) -> Option<EntityType> {
match entity {
EntityType::Line(l) => lengthen_line(l, pick_pt, mode),
EntityType::Arc(a) => lengthen_arc(a, pick_pt, mode),
EntityType::Ellipse(e) => lengthen_ellipse(e, pick_pt, mode),
EntityType::Spline(s) => lengthen_spline(s, pick_pt, mode),
EntityType::Line(l) => lengthen_line(l, pick_pt, mode),
EntityType::Arc(a) => lengthen_arc(a, pick_pt, mode),
EntityType::Ellipse(e) => lengthen_ellipse(e, pick_pt, mode),
EntityType::Spline(s) => lengthen_spline(s, pick_pt, mode),
EntityType::LwPolyline(p) => lengthen_lwpoly(p, pick_pt, mode),
_ => None,
}
@ -109,20 +151,24 @@ pub fn lengthen_entity(entity: &EntityType, pick_pt: Vec3, mode: &LenMode) -> Op
fn lengthen_line(line: &LineEnt, pick_pt: Vec3, mode: &LenMode) -> Option<EntityType> {
let s = Vec3::new(line.start.x as f32, line.start.z as f32, 0.0);
let e = Vec3::new(line.end.x as f32, line.end.z as f32, 0.0);
let e = Vec3::new(line.end.x as f32, line.end.z as f32, 0.0);
let p = Vec3::new(pick_pt.x, pick_pt.z, 0.0);
let current_len = (e - s).length() as f64;
if current_len < 1e-10 { return None; }
if current_len < 1e-10 {
return None;
}
let new_len = apply_mode(current_len, mode)?;
if new_len < 1e-10 { return None; }
if new_len < 1e-10 {
return None;
}
let dir = (e - s) / current_len as f32;
// Which end is closer to pick?
let dist_to_start = (p - s).length();
let dist_to_end = (p - e).length();
let dist_to_end = (p - e).length();
let mut result = line.clone();
result.common.handle = Handle::NULL;
@ -148,12 +194,14 @@ fn lengthen_arc(arc: &ArcEnt, pick_pt: Vec3, mode: &LenMode) -> Option<EntityTyp
let current_arc_len = arc.radius * span;
let new_arc_len = apply_mode(current_arc_len, mode)?;
if new_arc_len < 1e-10 { return None; }
if new_arc_len < 1e-10 {
return None;
}
let new_span = new_arc_len / arc.radius;
// Which end (start or end angle) is closer to pick?
let start_rad = arc.start_angle;
let end_rad = arc.end_angle;
let end_rad = arc.end_angle;
let start_pt = Vec3::new(
cx + arc.radius as f32 * start_rad.cos() as f32,
@ -166,7 +214,7 @@ fn lengthen_arc(arc: &ArcEnt, pick_pt: Vec3, mode: &LenMode) -> Option<EntityTyp
cy + arc.radius as f32 * end_rad.sin() as f32,
);
let dist_start = (pick_pt - start_pt).length();
let dist_end = (pick_pt - end_pt).length();
let dist_end = (pick_pt - end_pt).length();
let delta_span = new_span - span;
@ -186,14 +234,18 @@ fn lengthen_arc(arc: &ArcEnt, pick_pt: Vec3, mode: &LenMode) -> Option<EntityTyp
fn lengthen_ellipse(ell: &EllipseEnt, pick_pt: Vec3, mode: &LenMode) -> Option<EntityType> {
let a = (ell.major_axis.x.powi(2) + ell.major_axis.y.powi(2)).sqrt();
if a < 1e-9 { return None; }
if a < 1e-9 {
return None;
}
let b = a * ell.minor_axis_ratio;
let nx = ell.major_axis.x / a;
let ny = ell.major_axis.y / a;
let t0 = ell.start_parameter;
let mut t1 = ell.end_parameter;
if t1 <= t0 { t1 += std::f64::consts::TAU; }
if t1 <= t0 {
t1 += std::f64::consts::TAU;
}
let span = t1 - t0;
// Approximate arc length via 128-point Gaussian quadrature estimate.
@ -213,10 +265,14 @@ fn lengthen_ellipse(ell: &EllipseEnt, pick_pt: Vec3, mode: &LenMode) -> Option<E
};
let current_len = arc_len_approx(span);
if current_len < 1e-10 { return None; }
if current_len < 1e-10 {
return None;
}
let new_len = apply_mode(current_len, mode)?;
if new_len < 1e-10 { return None; }
if new_len < 1e-10 {
return None;
}
// Find the new span via bisection so that arc_len_approx(new_span) ≈ new_len.
let max_span = std::f64::consts::TAU;
@ -224,7 +280,11 @@ fn lengthen_ellipse(ell: &EllipseEnt, pick_pt: Vec3, mode: &LenMode) -> Option<E
let mut hi = max_span;
for _ in 0..40 {
let mid = (lo + hi) * 0.5;
if arc_len_approx(mid) < new_len { lo = mid; } else { hi = mid; }
if arc_len_approx(mid) < new_len {
lo = mid;
} else {
hi = mid;
}
}
let new_span = (lo + hi) * 0.5;
@ -233,10 +293,10 @@ fn lengthen_ellipse(ell: &EllipseEnt, pick_pt: Vec3, mode: &LenMode) -> Option<E
let p_y = pick_pt.z as f64; // Y-up: world Z → DXF Y
let pt_start_x = ell.center.x + a * t0.cos() * nx - b * t0.sin() * ny;
let pt_start_y = ell.center.y + a * t0.cos() * ny + b * t0.sin() * nx;
let pt_end_x = ell.center.x + a * t1.cos() * nx - b * t1.sin() * ny;
let pt_end_y = ell.center.y + a * t1.cos() * ny + b * t1.sin() * nx;
let pt_end_x = ell.center.x + a * t1.cos() * nx - b * t1.sin() * ny;
let pt_end_y = ell.center.y + a * t1.cos() * ny + b * t1.sin() * nx;
let dist_start = (p_x - pt_start_x).hypot(p_y - pt_start_y);
let dist_end = (p_x - pt_end_x).hypot(p_y - pt_end_y);
let dist_end = (p_x - pt_end_x).hypot(p_y - pt_end_y);
let mut result = ell.clone();
result.common.handle = Handle::NULL;
@ -251,15 +311,19 @@ fn lengthen_ellipse(ell: &EllipseEnt, pick_pt: Vec3, mode: &LenMode) -> Option<E
fn apply_mode(current: f64, mode: &LenMode) -> Option<f64> {
match mode {
LenMode::Delta(d) => Some(current + d),
LenMode::Total(t) => Some(*t),
LenMode::Delta(d) => Some(current + d),
LenMode::Total(t) => Some(*t),
LenMode::Percent(p) => Some(current * p / 100.0),
}
}
fn arc_span_rad(start: f64, end: f64) -> f64 {
let span = (end - start).rem_euclid(std::f64::consts::TAU);
if span < 1e-6 { std::f64::consts::TAU } else { span }
if span < 1e-6 {
std::f64::consts::TAU
} else {
span
}
}
fn xz_to_v3(v: Vec3, z: f64) -> Vector3 {
@ -269,34 +333,48 @@ fn xz_to_v3(v: Vec3, z: f64) -> Vector3 {
fn lengthen_lwpoly(poly: &LwPolyline, pick_pt: Vec3, mode: &LenMode) -> Option<EntityType> {
let n = poly.vertices.len();
if n < 2 { return None; }
if n < 2 {
return None;
}
// Determine which end is closer to the pick point (DXF XY: pick_pt.x, pick_pt.z).
let px = pick_pt.x as f64;
let py = pick_pt.z as f64; // Y-up: world Z = DXF Y
let first = &poly.vertices[0];
let last = &poly.vertices[n - 1];
let last = &poly.vertices[n - 1];
let d_first = (first.location.x - px).hypot(first.location.y - py);
let d_last = (last.location.x - px).hypot(last.location.y - py);
let d_last = (last.location.x - px).hypot(last.location.y - py);
let at_end = d_last <= d_first;
// Terminal segment direction and current length.
let (sx, sy, ex, ey) = if at_end {
(poly.vertices[n - 2].location.x, poly.vertices[n - 2].location.y,
last.location.x, last.location.y)
(
poly.vertices[n - 2].location.x,
poly.vertices[n - 2].location.y,
last.location.x,
last.location.y,
)
} else {
(poly.vertices[1].location.x, poly.vertices[1].location.y,
first.location.x, first.location.y)
(
poly.vertices[1].location.x,
poly.vertices[1].location.y,
first.location.x,
first.location.y,
)
};
let dx = ex - sx;
let dy = ey - sy;
let current_len = (dx * dx + dy * dy).sqrt();
if current_len < 1e-10 { return None; }
if current_len < 1e-10 {
return None;
}
let new_len = apply_mode(current_len, mode)?;
if new_len < 1e-10 { return None; }
if new_len < 1e-10 {
return None;
}
let ux = dx / current_len;
let uy = dy / current_len;
@ -320,7 +398,9 @@ fn lengthen_lwpoly(poly: &LwPolyline, pick_pt: Vec3, mode: &LenMode) -> Option<E
fn lengthen_spline(spl: &SplineEnt, pick_pt: Vec3, mode: &LenMode) -> Option<EntityType> {
let bs = spline_to_bspline(spl)?;
let (t0, t1) = bs.range_tuple();
if (t1 - t0).abs() < 1e-12 { return None; }
if (t1 - t0).abs() < 1e-12 {
return None;
}
// Approximate arc length via 64-point numerical integration.
use truck_modeling::base::ParametricCurve;
@ -336,16 +416,20 @@ fn lengthen_spline(spl: &SplineEnt, pick_pt: Vec3, mode: &LenMode) -> Option<Ent
}
len
};
if arc_len < 1e-10 { return None; }
if arc_len < 1e-10 {
return None;
}
let new_len = apply_mode(arc_len, mode)?;
if new_len < 1e-10 { return None; }
if new_len < 1e-10 {
return None;
}
// Determine which end (start or end) is closer to pick_pt.
let p_start = bs.subs(t0);
let p_end = bs.subs(t1);
let p_end = bs.subs(t1);
let dist_start = (p_start.x - pick_pt.x as f64).hypot(p_start.y - pick_pt.z as f64);
let dist_end = (p_end.x - pick_pt.x as f64).hypot(p_end.y - pick_pt.z as f64);
let dist_end = (p_end.x - pick_pt.x as f64).hypot(p_end.y - pick_pt.z as f64);
let extend_end = dist_end <= dist_start;
// Find the parameter `t_new` such that the arc length from the fixed end to t_new = new_len.
@ -355,8 +439,14 @@ fn lengthen_spline(spl: &SplineEnt, pick_pt: Vec3, mode: &LenMode) -> Option<Ent
// Find t_new via bisection: cumulative_len(fixed_t..t_new) = new_len.
let cum_len = |t_end_param: f64| -> f64 {
let (lo, hi) = if extend_end { (t0, t_end_param) } else { (t_end_param, t1) };
if hi <= lo { return 0.0; }
let (lo, hi) = if extend_end {
(t0, t_end_param)
} else {
(t_end_param, t1)
};
if hi <= lo {
return 0.0;
}
let n = 32usize;
let mut len = 0.0f64;
for i in 0..n {
@ -382,9 +472,17 @@ fn lengthen_spline(spl: &SplineEnt, pick_pt: Vec3, mode: &LenMode) -> Option<Ent
for _ in 0..40 {
let mid = (lo_t + hi_t) * 0.5;
if cum_len(mid) < new_len {
if extend_end { hi_t = mid; } else { lo_t = mid; }
if extend_end {
hi_t = mid;
} else {
lo_t = mid;
}
} else {
if extend_end { lo_t = mid; } else { hi_t = mid; }
if extend_end {
lo_t = mid;
} else {
hi_t = mid;
}
}
}
let t_new = (lo_t + hi_t) * 0.5;

View file

@ -1,22 +1,22 @@
pub mod align;
pub mod attedit;
pub mod array;
pub mod attedit;
pub mod break_cmd;
pub mod copy;
pub mod delete;
pub mod explode;
pub mod join;
pub mod lengthen;
pub mod pedit;
mod extend;
pub mod fillet;
pub mod join;
pub mod lengthen;
pub mod mirror;
pub mod offset;
pub mod pedit;
pub mod refedit;
pub mod rotate;
pub mod scale;
pub mod stretch;
pub mod translate;
pub mod spline_ops;
pub mod splinedit;
pub mod refedit;
pub mod stretch;
pub mod translate;
pub mod trim;

View file

@ -11,9 +11,12 @@
use std::f64::consts::TAU;
use crate::modules::home::modify::spline_ops::{spline_pts_wire, spline_sample_xy};
use acadrust::entities::LwVertex;
use acadrust::entities::{Arc as ArcEnt, Circle as CircleEnt, Ellipse as EllipseEnt, Line as LineEnt, LwPolyline, Spline as SplineEnt};
use crate::modules::home::modify::spline_ops::{spline_sample_xy, spline_pts_wire};
use acadrust::entities::{
Arc as ArcEnt, Circle as CircleEnt, Ellipse as EllipseEnt, Line as LineEnt, LwPolyline,
Spline as SplineEnt,
};
use acadrust::{EntityType, Handle};
use glam::Vec3;
@ -265,21 +268,25 @@ fn offset_lwpolyline(p: &LwPolyline, dist: f64, side_pt: Vec3) -> Option<EntityT
fn offset_ellipse(e: &EllipseEnt, dist: f64, side_pt: Vec3) -> Option<EntityType> {
let a = (e.major_axis.x.powi(2) + e.major_axis.y.powi(2)).sqrt();
if a < 1e-9 { return None; }
if a < 1e-9 {
return None;
}
let b = a * e.minor_axis_ratio;
let nx = e.major_axis.x / a;
let ny = e.major_axis.y / a;
// Project side_pt onto ellipse local frame and test inside/outside.
let rx = side_pt.x as f64 - e.center.x;
let ry = side_pt.y as f64 - e.center.y;
let xl = rx * nx + ry * ny;
let xl = rx * nx + ry * ny;
let yl = -rx * ny + ry * nx;
let inside = (xl / a).powi(2) + (yl / b).powi(2) < 1.0;
let sign = if inside { -1.0 } else { 1.0 };
let new_a = a + sign * dist;
let new_b = b + sign * dist;
if new_a <= 1e-9 || new_b <= 1e-9 { return None; }
if new_a <= 1e-9 || new_b <= 1e-9 {
return None;
}
let mut new_e = e.clone();
new_e.common.handle = Handle::NULL;
@ -301,14 +308,18 @@ fn offset_ellipse(e: &EllipseEnt, dist: f64, side_pt: Vec3) -> Option<EntityType
fn offset_spline(spl: &SplineEnt, dist: f64, side_pt: Vec3) -> Option<EntityType> {
let (ts_knot, pts) = spline_sample_xy(spl, 64);
let n = pts.len();
if n < 2 { return None; }
if n < 2 {
return None;
}
// Determine offset sign from the first non-degenerate tangent.
let sign: f64 = (0..n - 1).find_map(|i| {
let dx = pts[i + 1][0] - pts[i][0];
let dy = pts[i + 1][1] - pts[i][1];
let len = (dx * dx + dy * dy).sqrt();
if len < 1e-12 { return None; }
if len < 1e-12 {
return None;
}
let vx = side_pt.x as f64 - pts[i][0];
let vy = side_pt.y as f64 - pts[i][1];
let cross = dx * vy - dy * vx;
@ -328,18 +339,16 @@ fn offset_spline(spl: &SplineEnt, dist: f64, side_pt: Vec3) -> Option<EntityType
let d = [pts[n - 1][0] - pts[n - 2][0], pts[n - 1][1] - pts[n - 2][1]];
(d[0], d[1])
} else {
((pts[i + 1][0] - pts[i - 1][0]) * 0.5,
(pts[i + 1][1] - pts[i - 1][1]) * 0.5)
(
(pts[i + 1][0] - pts[i - 1][0]) * 0.5,
(pts[i + 1][1] - pts[i - 1][1]) * 0.5,
)
};
let len = (dx * dx + dy * dy).sqrt().max(1e-12);
let nx = -dy / len; // left perpendicular
let ny = dx / len;
let nx = -dy / len; // left perpendicular
let ny = dx / len;
let z = spl.control_points.first().map(|v| v.z).unwrap_or(0.0);
acadrust::types::Vector3::new(
p[0] + sign * nx * dist,
p[1] + sign * ny * dist,
z,
)
acadrust::types::Vector3::new(p[0] + sign * nx * dist, p[1] + sign * ny * dist, z)
})
.collect();
@ -419,23 +428,31 @@ fn entity_wire_pts(e: &EntityType) -> Vec<[f32; 3]> {
EntityType::LwPolyline(p) => lwpolyline_pts(p),
EntityType::Ellipse(e) => {
let a = (e.major_axis.x.powi(2) + e.major_axis.y.powi(2)).sqrt();
if a < 1e-9 { return vec![]; }
if a < 1e-9 {
return vec![];
}
let b = a * e.minor_axis_ratio;
let nx = e.major_axis.x / a;
let ny = e.major_axis.y / a;
let t0 = e.start_parameter;
let mut t1 = e.end_parameter;
if t1 <= t0 { t1 += TAU; }
if t1 <= t0 {
t1 += TAU;
}
let span = t1 - t0;
let steps = ((span.abs() * 20.0).ceil() as usize).max(4);
(0..=steps).map(|i| {
let t = t0 + span * (i as f64 / steps as f64);
let lx = a * t.cos();
let ly = b * t.sin();
[(e.center.x + lx * nx - ly * ny) as f32,
(e.center.y + lx * ny + ly * nx) as f32,
e.center.z as f32]
}).collect()
(0..=steps)
.map(|i| {
let t = t0 + span * (i as f64 / steps as f64);
let lx = a * t.cos();
let ly = b * t.sin();
[
(e.center.x + lx * nx - ly * ny) as f32,
(e.center.y + lx * ny + ly * nx) as f32,
e.center.z as f32,
]
})
.collect()
}
EntityType::Spline(s) => spline_pts_wire(s),
_ => vec![],

View file

@ -25,7 +25,9 @@ impl PeditCommand {
}
impl CadCommand for PeditCommand {
fn name(&self) -> &'static str { "PEDIT" }
fn name(&self) -> &'static str {
"PEDIT"
}
fn prompt(&self) -> String {
if self.target.is_none() {
@ -40,7 +42,9 @@ impl CadCommand for PeditCommand {
}
fn on_entity_pick(&mut self, handle: Handle, _pt: Vec3) -> CmdResult {
if handle.is_null() { return CmdResult::NeedPoint; }
if handle.is_null() {
return CmdResult::NeedPoint;
}
self.target = Some(handle);
CmdResult::NeedPoint
}
@ -55,21 +59,43 @@ impl CadCommand for PeditCommand {
match up.as_str() {
"X" | "EXIT" => return Some(CmdResult::Cancel),
"C" | "CLOSE" => return Some(CmdResult::PeditOp { handle, op: PeditOp::SetClosed(true) }),
"O" | "OPEN" => return Some(CmdResult::PeditOp { handle, op: PeditOp::SetClosed(false) }),
"C" | "CLOSE" => {
return Some(CmdResult::PeditOp {
handle,
op: PeditOp::SetClosed(true),
})
}
"O" | "OPEN" => {
return Some(CmdResult::PeditOp {
handle,
op: PeditOp::SetClosed(false),
})
}
_ => {}
}
if let Some(rest) = up.strip_prefix("W ").or_else(|| up.strip_prefix("W")) {
let w: f64 = rest.trim().replace(',', ".").parse().ok().filter(|&v: &f64| v >= 0.0)?;
return Some(CmdResult::PeditOp { handle, op: PeditOp::SetWidth(w) });
let w: f64 = rest
.trim()
.replace(',', ".")
.parse()
.ok()
.filter(|&v: &f64| v >= 0.0)?;
return Some(CmdResult::PeditOp {
handle,
op: PeditOp::SetWidth(w),
});
}
None
}
fn on_point(&mut self, _pt: Vec3) -> CmdResult { CmdResult::NeedPoint }
fn on_enter(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
}
// ── Op enum (used in CmdResult) ────────────────────────────────────────────
@ -85,9 +111,16 @@ pub enum PeditOp {
pub fn apply_pedit(entity: &mut EntityType, op: &PeditOp) -> bool {
match op {
PeditOp::SetClosed(closed) => match entity {
EntityType::LwPolyline(p) => { p.is_closed = *closed; true }
EntityType::LwPolyline(p) => {
p.is_closed = *closed;
true
}
EntityType::Polyline2D(p) => {
if *closed { p.close(); } else { p.flags.set_closed(false); }
if *closed {
p.close();
} else {
p.flags.set_closed(false);
}
true
}
_ => false,

View file

@ -71,10 +71,7 @@ impl CadCommand for RefEditPickCommand {
}
// Signal the host to enter the editing session for this handle.
// We reuse Relaunch("REFEDIT_BEGIN:<handle>") as a convention.
CmdResult::Relaunch(
format!("REFEDIT_BEGIN:{}", handle.value()),
vec![handle],
)
CmdResult::Relaunch(format!("REFEDIT_BEGIN:{}", handle.value()), vec![handle])
}
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
@ -142,7 +139,10 @@ pub fn apply_insert_transform(entity: &mut EntityType, session: &RefEditSession)
if (session.scale - 1.0).abs() > 1e-10 {
dispatch::apply_transform(
entity,
&EntityTransform::Scale { center: origin, factor: session.scale as f32 },
&EntityTransform::Scale {
center: origin,
factor: session.scale as f32,
},
);
}

View file

@ -10,8 +10,8 @@
use acadrust::entities::Spline;
use acadrust::types::Vector3;
use acadrust::Handle;
use truck_modeling::{BSplineCurve, KnotVec, Point3};
use truck_modeling::base::{BoundedCurve, ParametricCurve};
use truck_modeling::{BSplineCurve, KnotVec, Point3};
// ── Conversion ─────────────────────────────────────────────────────────────
@ -39,11 +39,7 @@ pub fn spline_to_bspline(spl: &Spline) -> Option<BSplineCurve<Point3>> {
/// Rebuild an acadrust `Spline` from a trimmed `BSplineCurve<Point3>`.
/// The `template` spline provides the entity common data, degree, and Z-elevation.
pub fn bspline_to_spline(bs: &BSplineCurve<Point3>, template: &Spline) -> Spline {
let z = template
.control_points
.first()
.map(|v| v.z)
.unwrap_or(0.0);
let z = template.control_points.first().map(|v| v.z).unwrap_or(0.0);
let mut spl = template.clone();
spl.common.handle = Handle::NULL;
spl.degree = bs.degree() as i32;
@ -53,7 +49,7 @@ pub fn bspline_to_spline(bs: &BSplineCurve<Point3>, template: &Spline) -> Spline
.iter()
.map(|p| Vector3::new(p.x, p.y, z))
.collect();
spl.weights.clear(); // not rational after split
spl.weights.clear(); // not rational after split
spl.fit_points.clear(); // fit points are no longer valid
spl
}
@ -71,10 +67,13 @@ pub fn spline_sample_xy(spl: &Spline, n: usize) -> (Vec<f64>, Vec<[f64; 2]>) {
let ts: Vec<f64> = (0..=n)
.map(|i| t0 + (t1 - t0) * (i as f64 / n as f64))
.collect();
let pts: Vec<[f64; 2]> = ts.iter().map(|&t| {
let p = bs.subs(t);
[p.x, p.y]
}).collect();
let pts: Vec<[f64; 2]> = ts
.iter()
.map(|&t| {
let p = bs.subs(t);
[p.x, p.y]
})
.collect();
(ts, pts)
}
@ -112,7 +111,11 @@ pub fn spline_nearest_t(spl: &Spline, x: f64, y: f64) -> Option<f64> {
let p2 = bs.subs(m2);
let d1 = (p1.x - x).powi(2) + (p1.y - y).powi(2);
let d2 = (p2.x - x).powi(2) + (p2.y - y).powi(2);
if d1 < d2 { b = m2; } else { a = m1; }
if d1 < d2 {
b = m2;
} else {
a = m1;
}
}
Some((a + b) * 0.5)
}
@ -120,7 +123,9 @@ pub fn spline_nearest_t(spl: &Spline, x: f64, y: f64) -> Option<f64> {
/// Returns the normalised parameter in [0, 1] given an actual B-spline
/// parameter `t_actual` and the range `[t0, t1]`.
pub fn t_to_rel(t_actual: f64, t0: f64, t1: f64) -> f64 {
if (t1 - t0).abs() < 1e-12 { return 0.0; }
if (t1 - t0).abs() < 1e-12 {
return 0.0;
}
((t_actual - t0) / (t1 - t0)).clamp(0.0, 1.0)
}
@ -130,8 +135,14 @@ pub fn t_to_rel(t_actual: f64, t0: f64, t1: f64) -> f64 {
/// Y-up convention: world (x, 0, y) for DXF (x, y).
pub fn spline_pts_wire(spl: &Spline) -> Vec<[f32; 3]> {
let (_, pts) = spline_sample_xy(spl, 64);
if pts.is_empty() { return vec![]; }
let elev = spl.control_points.first().map(|v| v.z as f32).unwrap_or(0.0);
if pts.is_empty() {
return vec![];
}
let elev = spl
.control_points
.first()
.map(|v| v.z as f32)
.unwrap_or(0.0);
pts.iter()
.map(|p| [p[0] as f32, elev, p[1] as f32])
.collect()

View file

@ -39,19 +39,21 @@ pub struct SplineditCommand {
impl SplineditCommand {
pub fn new() -> Self {
Self { step: Step::SelectSpline }
Self {
step: Step::SelectSpline,
}
}
}
impl CadCommand for SplineditCommand {
fn name(&self) -> &'static str { "SPLINEDIT" }
fn name(&self) -> &'static str {
"SPLINEDIT"
}
fn prompt(&self) -> String {
match &self.step {
Step::SelectSpline => "SPLINEDIT Select spline:".to_string(),
Step::SubCommand { .. } => {
"SPLINEDIT [CLOSE/OPEN/REVERSE/EXIT]:".to_string()
}
Step::SubCommand { .. } => "SPLINEDIT [CLOSE/OPEN/REVERSE/EXIT]:".to_string(),
}
}
@ -72,7 +74,9 @@ impl CadCommand for SplineditCommand {
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let Step::SubCommand { handle } = &self.step else { return None; };
let Step::SubCommand { handle } = &self.step else {
return None;
};
let handle = *handle;
match text.trim().to_uppercase().as_str() {
"CLOSE" | "C" => Some(CmdResult::ReplaceEntity(
@ -92,9 +96,15 @@ impl CadCommand for SplineditCommand {
}
}
fn on_point(&mut self, _pt: Vec3) -> CmdResult { CmdResult::NeedPoint }
fn on_enter(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_preview_wires(&mut self, _pt: Vec3) -> Vec<WireModel> { vec![] }
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
CmdResult::NeedPoint
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_preview_wires(&mut self, _pt: Vec3) -> Vec<WireModel> {
vec![]
}
}
// ── Spline operation helpers ───────────────────────────────────────────────
@ -108,7 +118,11 @@ impl CadCommand for SplineditCommand {
// we only have the handle. The real transformation (close/open/reverse) is
// applied in the Scene via `apply_spline_op`.
enum SplineOp { Close, Open, Reverse }
enum SplineOp {
Close,
Open,
Reverse,
}
impl SplineOp {
/// Return a placeholder that encodes the op in a comment field.
@ -130,8 +144,8 @@ impl SplineOp {
acadrust::types::Vector3::new(1.0, 0.0, 0.0),
);
sentinel.common.layer = match self {
SplineOp::Close => "__SPLINEDIT_CLOSE__".to_string(),
SplineOp::Open => "__SPLINEDIT_OPEN__".to_string(),
SplineOp::Close => "__SPLINEDIT_CLOSE__".to_string(),
SplineOp::Open => "__SPLINEDIT_OPEN__".to_string(),
SplineOp::Reverse => "__SPLINEDIT_REVERSE__".to_string(),
};
EntityType::XLine(sentinel)
@ -141,8 +155,9 @@ impl SplineOp {
/// Apply a spline operation (CLOSE/OPEN/REVERSE) to a spline entity.
/// Called from `cmd_result.rs` when the ReplaceEntity sentinel is detected.
pub fn apply_spline_op(doc: &mut acadrust::CadDocument, handle: acadrust::Handle, op: &str) {
let Some(EntityType::Spline(spline)) = doc.get_entity_mut(handle) else { return; };
let Some(EntityType::Spline(spline)) = doc.get_entity_mut(handle) else {
return;
};
match op {
"__SPLINEDIT_CLOSE__" => {
if spline.control_points.len() >= 2 {

View file

@ -43,7 +43,11 @@ enum Step {
/// Crossing window defined; waiting for base point.
Base { win_min: Vec3, win_max: Vec3 },
/// Waiting for target point.
Target { win_min: Vec3, win_max: Vec3, base: Vec3 },
Target {
win_min: Vec3,
win_max: Vec3,
base: Vec3,
},
}
pub struct StretchCommand {
@ -53,12 +57,17 @@ pub struct StretchCommand {
impl StretchCommand {
pub fn new(handles: Vec<Handle>) -> Self {
Self { handles, step: Step::WindowCorner1 }
Self {
handles,
step: Step::WindowCorner1,
}
}
}
impl CadCommand for StretchCommand {
fn name(&self) -> &'static str { "STRETCH" }
fn name(&self) -> &'static str {
"STRETCH"
}
fn prompt(&self) -> String {
match &self.step {
@ -69,7 +78,8 @@ impl CadCommand for StretchCommand {
Step::WindowCorner2(_) => "STRETCH Specify opposite corner:".into(),
Step::Base { .. } => "STRETCH Specify base point:".into(),
Step::Target { base, .. } => format!(
"STRETCH Specify new point [base {:.3},{:.3}]:", base.x, base.z
"STRETCH Specify new point [base {:.3},{:.3}]:",
base.x, base.z
),
}
}
@ -88,10 +98,18 @@ impl CadCommand for StretchCommand {
}
Step::Base { win_min, win_max } => {
let (wmin, wmax) = (*win_min, *win_max);
self.step = Step::Target { win_min: wmin, win_max: wmax, base: pt };
self.step = Step::Target {
win_min: wmin,
win_max: wmax,
base: pt,
};
CmdResult::NeedPoint
}
Step::Target { win_min, win_max, base } => {
Step::Target {
win_min,
win_max,
base,
} => {
let delta = pt - *base;
CmdResult::StretchEntities {
handles: self.handles.clone(),
@ -103,8 +121,12 @@ impl CadCommand for StretchCommand {
}
}
fn on_enter(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_escape(&mut self) -> CmdResult { CmdResult::Cancel }
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
match &self.step {
@ -113,32 +135,35 @@ impl CadCommand for StretchCommand {
let c1 = *c1;
let pts = vec![
[c1.x, c1.y, c1.z],
[pt.x, c1.y, c1.z],
[pt.x, pt.y, pt.z],
[c1.x, pt.y, pt.z],
[pt.x, c1.y, c1.z],
[pt.x, pt.y, pt.z],
[c1.x, pt.y, pt.z],
[c1.x, c1.y, c1.z],
];
Some(WireModel::solid("stretch_window".into(), pts, [0.3, 1.0, 0.3, 0.7], false))
}
Step::Target { base, .. } => {
Some(WireModel {
name: "rubber_band".into(),
points: vec![[base.x, base.y, base.z], [pt.x, pt.y, pt.z]],
color: WireModel::CYAN,
selected: false,
pattern_length: 0.0,
pattern: [0.0; 8],
line_weight_px: 1.0,
snap_pts: vec![],
tangent_geoms: vec![],
aci: 0,
key_vertices: vec![],
aabb: WireModel::UNBOUNDED_AABB,
plinegen: true,
vp_scissor: None,
fill_tris: vec![],
})
Some(WireModel::solid(
"stretch_window".into(),
pts,
[0.3, 1.0, 0.3, 0.7],
false,
))
}
Step::Target { base, .. } => Some(WireModel {
name: "rubber_band".into(),
points: vec![[base.x, base.y, base.z], [pt.x, pt.y, pt.z]],
color: WireModel::CYAN,
selected: false,
pattern_length: 0.0,
pattern: [0.0; 8],
line_weight_px: 1.0,
snap_pts: vec![],
tangent_geoms: vec![],
aci: 0,
key_vertices: vec![],
aabb: WireModel::UNBOUNDED_AABB,
plinegen: true,
vp_scissor: None,
fill_tris: vec![],
}),
_ => None,
}
}

File diff suppressed because it is too large Load diff