forked from 1b-agent/opencadstudio-upstream
Merge PR #1106: thicken surfaces with exact kernel geometry
Use the maintained cadkernel main revision and retain source surfaces. Verify signed and two-point input, partial failure, DWG round-trips and one-step undo/redo of solids and history objects.
This commit is contained in:
commit
cf12c6a272
7 files changed
+359
-4
No files matched your search
@@ -513,6 +513,172 @@ impl CadCommand for ExtrudeCommand {
|
||||
}
|
||||
}
|
||||
|
||||
// ── THICKEN command ────────────────────────────────────────────────────────
|
||||
|
||||
pub struct ThickenCommand {
|
||||
step: ThickenStep,
|
||||
handles: Vec<Handle>,
|
||||
first_point: Option<DVec3>,
|
||||
last_distance: f64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum ThickenStep {
|
||||
Pick,
|
||||
Distance,
|
||||
SecondPoint,
|
||||
}
|
||||
|
||||
fn thicken_default() -> &'static Mutex<f64> {
|
||||
static DEFAULT: OnceLock<Mutex<f64>> = OnceLock::new();
|
||||
DEFAULT.get_or_init(|| Mutex::new(0.0))
|
||||
}
|
||||
|
||||
impl ThickenCommand {
|
||||
pub fn new(preselection: Vec<(Handle, EntityType)>) -> Self {
|
||||
let handles = preselection
|
||||
.into_iter()
|
||||
.filter_map(|(handle, entity)| {
|
||||
matches!(entity, EntityType::Surface(_)).then_some(handle)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
Self {
|
||||
step: if handles.is_empty() {
|
||||
ThickenStep::Pick
|
||||
} else {
|
||||
ThickenStep::Distance
|
||||
},
|
||||
handles,
|
||||
first_point: None,
|
||||
last_distance: *thicken_default()
|
||||
.lock()
|
||||
.unwrap_or_else(|error| error.into_inner()),
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&mut self, distance: f64) -> CmdResult {
|
||||
self.last_distance = distance;
|
||||
*thicken_default()
|
||||
.lock()
|
||||
.unwrap_or_else(|error| error.into_inner()) = distance;
|
||||
CmdResult::ThickenEntities {
|
||||
handles: self.handles.clone(),
|
||||
distance,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for ThickenCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"THICKEN"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
match self.step {
|
||||
ThickenStep::Pick => t!("Select surfaces to thicken:").into_owned(),
|
||||
ThickenStep::Distance => format!(
|
||||
"{} <{}>:",
|
||||
t!("Specify thickness"),
|
||||
crate::entities::common::format_length(self.last_distance)
|
||||
),
|
||||
ThickenStep::SecondPoint => {
|
||||
t!("Specify second point:").into_owned()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
self.step == ThickenStep::Distance
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
if self.step != ThickenStep::Distance {
|
||||
return None;
|
||||
}
|
||||
let value = text.trim();
|
||||
if value.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(match crate::entities::common::parse_typed_length(value) {
|
||||
Some(distance) if distance.is_finite() => self.finish(distance),
|
||||
_ => CmdResult::ReportError(
|
||||
t!("Requires numeric distance or two points.").into_owned(),
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
fn on_point(&mut self, point: DVec3) -> CmdResult {
|
||||
match self.step {
|
||||
ThickenStep::Distance => {
|
||||
self.first_point = Some(point);
|
||||
self.step = ThickenStep::SecondPoint;
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
ThickenStep::SecondPoint => {
|
||||
let distance = point.distance(self.first_point.unwrap_or(point));
|
||||
if distance.is_finite() {
|
||||
self.finish(distance)
|
||||
} else {
|
||||
CmdResult::ReportError(
|
||||
t!("Requires numeric distance or two points.").into_owned(),
|
||||
)
|
||||
}
|
||||
}
|
||||
ThickenStep::Pick => CmdResult::NeedPoint,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
match self.step {
|
||||
ThickenStep::Pick if self.handles.is_empty() => {
|
||||
CmdResult::Measurement(t!("No surfaces selected.").into_owned())
|
||||
}
|
||||
ThickenStep::Pick => {
|
||||
self.step = ThickenStep::Distance;
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
ThickenStep::Distance => self.finish(self.last_distance),
|
||||
ThickenStep::SecondPoint => CmdResult::ReportError(
|
||||
t!("Requires numeric distance or two points.").into_owned(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_selection_gathering(&self) -> bool {
|
||||
self.step == ThickenStep::Pick
|
||||
}
|
||||
|
||||
fn selection_forces_add(&self) -> bool {
|
||||
self.step == ThickenStep::Pick
|
||||
}
|
||||
|
||||
fn inject_selection_entities(&mut self, entities: Vec<SelectionEntity>) {
|
||||
if self.step != ThickenStep::Pick {
|
||||
return;
|
||||
}
|
||||
self.handles = entities
|
||||
.into_iter()
|
||||
.filter_map(|entry| {
|
||||
matches!(entry.entity, EntityType::Surface(_)).then_some(entry.handle)
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
||||
fn on_selection_complete(&mut self, _handles: Vec<Handle>) -> CmdResult {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn on_undo_step(&mut self) -> Option<CmdResult> {
|
||||
if self.step == ThickenStep::SecondPoint {
|
||||
self.first_point = None;
|
||||
self.step = ThickenStep::Distance;
|
||||
Some(CmdResult::NeedPoint)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── PRESSPULL command ─────────────────────────────────────────────────────
|
||||
|
||||
pub struct PresspullCommand {
|
||||
@@ -2701,3 +2867,29 @@ mod revolve_tests {
|
||||
}
|
||||
}
|
||||
inventory::submit!(crate::command::CommandRegistration { names: &["SWEEP"] });
|
||||
|
||||
#[cfg(test)]
|
||||
mod thicken_command_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn thicken_filters_selection_recovers_from_invalid_text_and_measures_two_points() {
|
||||
let handle = Handle::new(42);
|
||||
let mut command = ThickenCommand::new(vec![
|
||||
(Handle::new(1), EntityType::Line(acadrust::entities::Line::default())),
|
||||
(handle, EntityType::Surface(acadrust::entities::Surface::new(acadrust::entities::SurfaceKind::Plane))),
|
||||
]);
|
||||
assert!(!command.is_selection_gathering());
|
||||
assert_eq!(command.handles, vec![handle]);
|
||||
assert!(matches!(command.on_text_input("bad"), Some(CmdResult::ReportError(_))));
|
||||
assert!(command.input_kind().wants_text());
|
||||
assert!(matches!(command.on_text_input("-2"), Some(CmdResult::ThickenEntities { distance: -2.0, .. })));
|
||||
assert!(matches!(command.on_point(DVec3::ZERO), CmdResult::NeedPoint));
|
||||
assert!(command.on_undo_step().is_some());
|
||||
let _ = command.on_point(DVec3::ZERO);
|
||||
assert!(matches!(command.on_point(DVec3::new(3.0, 4.0, 0.0)), CmdResult::ThickenEntities { distance: 5.0, .. }));
|
||||
let mut empty = ThickenCommand::new(Vec::new());
|
||||
assert!(empty.is_selection_gathering());
|
||||
assert!(matches!(empty.on_enter(), CmdResult::Measurement(_)));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user