Feat: PEDIT command (PE) — polyline edit (close/open/width)

Entity pick → text input subcommands:
  C / CLOSE  → set closed flag
  O / OPEN   → clear closed flag
  W <value>  → 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 <noreply@anthropic.com>
This commit is contained in:
Hakan Seven 2026-04-03 09:19:36 +03:00
commit 330b8b58f4
5 changed files with 139 additions and 0 deletions

View file

@ -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()

View file

@ -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();

View file

@ -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<Handle>),
/// 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.

View file

@ -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;

View file

@ -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 <width> — 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<Handle>,
}
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<CmdResult> {
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,
},
}
}