From 330b8b58f400da815b8b7af2d8f98b86b82b36b1 Mon Sep 17 00:00:00 2001 From: Hakan Seven Date: Fri, 3 Apr 2026 09:19:36 +0300 Subject: [PATCH] =?UTF-8?q?Feat:=20PEDIT=20command=20(PE)=20=E2=80=94=20po?= =?UTF-8?q?lyline=20edit=20(close/open/width)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Entity pick → text input subcommands: C / CLOSE → set closed flag O / OPEN → clear closed flag W → set uniform width on LwPolyline vertices X / EXIT → cancel Command stays active after each op (CmdResult::PeditOp) for multiple edits. Supports LwPolyline and Polyline2D. Co-Authored-By: Claude Sonnet 4.6 --- src/app/cmd_result.rs | 19 ++++++ src/app/commands.rs | 7 ++ src/command/mod.rs | 5 ++ src/modules/home/modify/mod.rs | 1 + src/modules/home/modify/pedit.rs | 107 +++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 src/modules/home/modify/pedit.rs diff --git a/src/app/cmd_result.rs b/src/app/cmd_result.rs index 5c3e43bd..54deb88a 100644 --- a/src/app/cmd_result.rs +++ b/src/app/cmd_result.rs @@ -487,6 +487,25 @@ impl H7CAD { self.tabs[i].scene.clear_preview_wire(); self.restore_pre_cmd_tangent(); } + CmdResult::PeditOp { handle, op } => { + use crate::modules::home::modify::pedit::apply_pedit; + let changed = self.tabs[i].scene.document + .get_entity_mut(handle) + .map(|e| apply_pedit(e, &op)) + .unwrap_or(false); + if changed { + self.push_undo_snapshot(i, "PEDIT"); + self.tabs[i].dirty = true; + self.command_line.push_output("PEDIT: applied."); + self.refresh_properties(); + } else { + self.command_line.push_error("PEDIT: operation not applicable to this entity."); + } + // Keep command active — user may apply more ops + self.command_line.push_info( + "PEDIT Enter option [C=Close O=Open W=Width X=Exit]:" + ); + } CmdResult::JoinEntities(handles) => { use crate::modules::home::modify::join::join_entities; let pairs: Vec<_> = handles.iter() diff --git a/src/app/commands.rs b/src/app/commands.rs index 5c3f3e8b..0a4014fb 100644 --- a/src/app/commands.rs +++ b/src/app/commands.rs @@ -1018,6 +1018,13 @@ impl H7CAD { self.tabs[i].active_cmd = Some(Box::new(cmd)); } + "PEDIT"|"PE" => { + use crate::modules::home::modify::pedit::PeditCommand; + let cmd_obj = PeditCommand::new(); + self.command_line.push_info(&cmd_obj.prompt()); + self.tabs[i].active_cmd = Some(Box::new(cmd_obj)); + } + "ALIGN"|"AL" => { use crate::modules::home::modify::align::AlignCommand; let cmd = AlignCommand::new(); diff --git a/src/command/mod.rs b/src/command/mod.rs index f8046015..e938c1fe 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -99,6 +99,11 @@ pub enum CmdResult { BreakEntity { handle: Handle, p1: Vec3, p2: Vec3 }, /// Attempt to join the given entities into fewer merged entities. JoinEntities(Vec), + /// Apply a polyline-edit operation to one entity; keep command active. + PeditOp { + handle: Handle, + op: crate::modules::home::modify::pedit::PeditOp, + }, /// Place Point entities at N equal intervals along the entity. DivideEntity { handle: Handle, n: usize }, /// Place Point entities at `segment_length` intervals along the entity. diff --git a/src/modules/home/modify/mod.rs b/src/modules/home/modify/mod.rs index 42f0cc66..681a6155 100644 --- a/src/modules/home/modify/mod.rs +++ b/src/modules/home/modify/mod.rs @@ -6,6 +6,7 @@ pub mod delete; pub mod explode; pub mod join; pub mod lengthen; +pub mod pedit; mod extend; pub mod fillet; pub mod mirror; diff --git a/src/modules/home/modify/pedit.rs b/src/modules/home/modify/pedit.rs new file mode 100644 index 00000000..7141ac88 --- /dev/null +++ b/src/modules/home/modify/pedit.rs @@ -0,0 +1,107 @@ +// PEDIT command — edit a polyline entity. +// +// Supports LwPolyline and (partially) Polyline2D. +// Subcommands (text input after entity pick): +// C / CLOSE — toggle closed flag +// O / OPEN — clear closed flag +// W — set uniform width (LwPolyline) +// E — enter vertex editing mode (not implemented, use grips) +// J — join (same as JOIN command, triggered as alias) +// X / EXIT — exit + +use acadrust::{EntityType, Handle}; +use glam::Vec3; + +use crate::command::{CadCommand, CmdResult}; + +pub struct PeditCommand { + target: Option, +} + +impl PeditCommand { + pub fn new() -> Self { + Self { target: None } + } +} + +impl CadCommand for PeditCommand { + fn name(&self) -> &'static str { "PEDIT" } + + fn prompt(&self) -> String { + if self.target.is_none() { + "PEDIT Select polyline:".into() + } else { + "PEDIT Enter option [C=Close O=Open W=Width X=Exit]:".into() + } + } + + 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; } + self.target = Some(handle); + CmdResult::NeedPoint + } + + fn wants_text_input(&self) -> bool { + self.target.is_some() + } + + fn on_text_input(&mut self, text: &str) -> Option { + let handle = self.target?; + let up = text.trim().to_uppercase(); + + 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) }), + _ => {} + } + + 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) }); + } + + None + } + + fn on_point(&mut self, _pt: Vec3) -> CmdResult { CmdResult::NeedPoint } + fn on_enter(&mut self) -> CmdResult { CmdResult::Cancel } +} + +// ── Op enum (used in CmdResult) ──────────────────────────────────────────── + +#[derive(Clone)] +pub enum PeditOp { + SetClosed(bool), + SetWidth(f64), +} + +// ── Apply logic ──────────────────────────────────────────────────────────── + +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::Polyline2D(p) => { + if *closed { p.close(); } else { p.flags.set_closed(false); } + true + } + _ => false, + }, + PeditOp::SetWidth(w) => match entity { + EntityType::LwPolyline(p) => { + p.constant_width = *w; + for v in &mut p.vertices { + v.start_width = *w; + v.end_width = *w; + } + true + } + _ => false, + }, + } +}