Complete CHAMFEREDGE workflow and history

This commit is contained in:
ramox81 committed 2026-09-08 15:45:10 +03:00
1 parent ed8cddc556
commit 8822371e18
10 files changed
+678 -157

No files matched your search

+299 -62
View File
@@ -1,7 +1,7 @@
use std::sync::atomic::{AtomicU64, Ordering};
use acadrust::Handle;
use cadkernel::brep::{Body, EdgeKey};
use cadkernel::brep::{Body, EdgeKey, FaceKey};
use glam::DVec3;
use crate::command::{
@@ -39,7 +39,8 @@ enum EdgeStep {
PreviewConfirm,
Radius,
Expression,
ChamferValue,
ChamferDistance1,
ChamferDistance2,
}
pub struct SolidEdgeCommand {
@@ -48,14 +49,17 @@ pub struct SolidEdgeCommand {
bodies: Vec<(Handle, Body)>,
handle: Option<Handle>,
selected_edges: Vec<EdgeKey>,
base_face: Option<FaceKey>,
selection_batches: Vec<(Vec<EdgeKey>, DVec3)>,
selection_mode: EdgeSelectionMode,
step: EdgeStep,
radius_return: EdgeStep,
loop_candidates: Vec<Vec<EdgeKey>>,
value_return: EdgeStep,
expression_return: EdgeStep,
loop_candidates: Vec<(FaceKey, Vec<EdgeKey>)>,
loop_index: usize,
last_pick: Option<DVec3>,
default_value: f64,
other_value: f64,
preview_color: [f32; 4],
preview_wires: Vec<WireModel>,
preview_hidden: Vec<Handle>,
@@ -74,10 +78,12 @@ impl SolidEdgeCommand {
bodies,
handle: None,
selected_edges: Vec::new(),
base_face: None,
selection_batches: Vec::new(),
selection_mode: EdgeSelectionMode::Edge,
step: EdgeStep::Selecting,
radius_return: EdgeStep::Selecting,
value_return: EdgeStep::Selecting,
expression_return: EdgeStep::Radius,
loop_candidates: Vec::new(),
loop_index: 0,
last_pick: None,
@@ -86,12 +92,29 @@ impl SolidEdgeCommand {
} else {
1.0
},
other_value: 1.0,
preview_color,
preview_wires: Vec::new(),
preview_hidden: Vec::new(),
}
}
pub fn new_chamfer(
target: Option<Handle>,
bodies: Vec<(Handle, Body)>,
preview_color: [f32; 4],
distances: (f64, f64),
) -> Self {
let mut command = Self::new(EdgeOperation::Chamfer, target, bodies, preview_color);
command.default_value = positive_default(distances.0);
command.other_value = positive_default(distances.1);
command
}
pub fn chamfer_distances(&self) -> (f64, f64) {
(self.default_value, self.other_value)
}
fn body(&self, handle: Handle) -> Option<&Body> {
self.bodies
.iter()
@@ -126,7 +149,13 @@ impl SolidEdgeCommand {
fn current_loop(&self) -> Option<&[EdgeKey]> {
self.loop_candidates
.get(self.loop_index)
.map(Vec::as_slice)
.map(|(_, edges)| edges.as_slice())
}
fn current_loop_face(&self) -> Option<FaceKey> {
self.loop_candidates
.get(self.loop_index)
.map(|(face, _)| *face)
}
fn preview_edges(&self) -> Vec<EdgeKey> {
@@ -144,17 +173,24 @@ impl SolidEdgeCommand {
}
fn preview_active(&self) -> bool {
self.operation == EdgeOperation::Fillet
&& matches!(
self.step,
EdgeStep::LoopConfirm
| EdgeStep::PreviewConfirm
| EdgeStep::Radius
| EdgeStep::Expression
)
matches!(
self.step,
EdgeStep::Selecting
| EdgeStep::PickingLoop
| EdgeStep::LoopConfirm
| EdgeStep::PreviewConfirm
| EdgeStep::Radius
| EdgeStep::Expression
| EdgeStep::ChamferDistance1
| EdgeStep::ChamferDistance2
)
}
fn preview_for_value(&self, value: f64) -> Option<(Handle, Vec<WireModel>)> {
fn preview_for_values(
&self,
value: f64,
other_value: f64,
) -> Option<(Handle, Vec<WireModel>)> {
if !self.preview_active() {
return None;
}
@@ -163,11 +199,21 @@ impl SolidEdgeCommand {
return None;
}
let (handle, body) = self.active_body()?;
let result = cadkernel::brep::fillet_edges(body, &edges, value).ok()?;
let result = match self.operation {
EdgeOperation::Fillet => cadkernel::brep::fillet_edges(body, &edges, value).ok()?,
EdgeOperation::Chamfer => cadkernel::brep::chamfer_edges(
body,
&edges,
self.base_face.or_else(|| self.current_loop_face())?,
value,
other_value,
)
.ok()?,
};
let mut wires = crate::scene::model::solid_model::grip_preview_wires(&result, handle);
for wire in &mut wires {
wire.color = self.preview_color;
wire.name = format!("{}-FILLETEDGE-PREVIEW", handle.value());
wire.name = format!("{}-{}-PREVIEW", handle.value(), self.name());
}
(!wires.is_empty()).then_some((handle, wires))
}
@@ -175,14 +221,16 @@ impl SolidEdgeCommand {
fn rebuild_preview(&mut self) {
self.preview_wires.clear();
self.preview_hidden.clear();
if let Some((handle, wires)) = self.preview_for_value(self.default_value) {
if let Some((handle, wires)) =
self.preview_for_values(self.default_value, self.other_value)
{
self.preview_wires = wires;
self.preview_hidden.push(handle);
}
}
fn begin_radius(&mut self, return_to: EdgeStep) -> CmdResult {
self.radius_return = return_to;
self.value_return = return_to;
self.step = EdgeStep::Radius;
self.rebuild_preview();
CmdResult::NeedPoint
@@ -196,7 +244,33 @@ impl SolidEdgeCommand {
if self.operation == EdgeOperation::Fillet {
set_fillet_radius(value);
}
self.step = self.radius_return;
self.step = self.value_return;
self.rebuild_preview();
Some(CmdResult::NeedPoint)
}
fn begin_distances(&mut self, return_to: EdgeStep) -> CmdResult {
self.value_return = return_to;
self.step = EdgeStep::ChamferDistance1;
self.rebuild_preview();
CmdResult::NeedPoint
}
fn accept_distance(&mut self, value: f64) -> Option<CmdResult> {
if value <= 0.0 || !value.is_finite() {
return None;
}
match self.step {
EdgeStep::ChamferDistance1 => {
self.default_value = value;
self.step = EdgeStep::ChamferDistance2;
}
EdgeStep::ChamferDistance2 => {
self.other_value = value;
self.step = self.value_return;
}
_ => return None,
}
self.rebuild_preview();
Some(CmdResult::NeedPoint)
}
@@ -206,6 +280,9 @@ impl SolidEdgeCommand {
self.current_loop().map(|edges| edges.to_vec()),
self.last_pick,
) {
if self.operation == EdgeOperation::Chamfer && self.base_face.is_none() {
self.base_face = self.current_loop_face();
}
self.add_batch(edges, anchor);
}
self.loop_candidates.clear();
@@ -225,7 +302,9 @@ impl SolidEdgeCommand {
CmdResult::SolidEdgeBlend {
handle,
edges: self.selected_edges.clone(),
base_face: self.base_face,
value: self.default_value,
other_value: self.other_value,
fillet: self.operation == EdgeOperation::Fillet,
}
}
@@ -235,20 +314,31 @@ impl CadCommand for SolidEdgeCommand {
fn name(&self) -> &'static str {
match self.operation {
EdgeOperation::Fillet => "FILLETEDGE",
EdgeOperation::Chamfer => "SOLIDCHAMFER",
EdgeOperation::Chamfer => "CHAMFEREDGE",
}
}
fn prompt(&self) -> String {
match (self.operation, self.step, self.selection_mode) {
(EdgeOperation::Chamfer, EdgeStep::Selecting, _) => {
crate::t!("Select a solid edge:").into_owned()
crate::t!("Select an edge or [Loop/Distance]:").into_owned()
}
(EdgeOperation::Chamfer, EdgeStep::ChamferValue, _) => crate::tf!(
"Specify chamfer distance <{:.3}>:",
(EdgeOperation::Chamfer, EdgeStep::PickingLoop, _) => {
crate::t!("Select edge of loop or [Edge/Distance]:").into_owned()
}
(EdgeOperation::Chamfer, EdgeStep::PreviewConfirm, _) => {
crate::t!("Press Enter to accept the chamfer or [Distance]:").into_owned()
}
(EdgeOperation::Chamfer, EdgeStep::ChamferDistance1, _) => crate::tf!(
"Specify Distance1 or [Expression] <{:.4}>:",
self.default_value
)
.into_owned(),
(EdgeOperation::Chamfer, EdgeStep::ChamferDistance2, _) => crate::tf!(
"Specify Distance2 or [Expression] <{:.4}>:",
self.other_value
)
.into_owned(),
(_, EdgeStep::Selecting, EdgeSelectionMode::Edge) => {
crate::t!("Select an edge or [Chain/Loop/Radius]:").into_owned()
}
@@ -264,7 +354,7 @@ impl CadCommand for SolidEdgeCommand {
(_, EdgeStep::PreviewConfirm, _) => {
crate::t!("Press Enter to accept the fillet or [Radius]:").into_owned()
}
(_, EdgeStep::Radius, _) if self.radius_return == EdgeStep::PreviewConfirm => crate::tf!(
(_, EdgeStep::Radius, _) if self.value_return == EdgeStep::PreviewConfirm => crate::tf!(
"Specify Radius or [Expression] <{:.4}>:",
self.default_value
)
@@ -281,6 +371,26 @@ impl CadCommand for SolidEdgeCommand {
fn options(&self) -> Vec<CmdOption> {
match (self.operation, self.step, self.selection_mode) {
(EdgeOperation::Chamfer, EdgeStep::Selecting, _) => vec![
CmdOption::new("Loop", "L"),
CmdOption::new("Distance", "D"),
],
(EdgeOperation::Chamfer, EdgeStep::PickingLoop, _) => vec![
CmdOption::new("Edge", "E"),
CmdOption::new("Distance", "D"),
],
(EdgeOperation::Chamfer, EdgeStep::LoopConfirm, _) => vec![
CmdOption::new("Accept", "A"),
CmdOption::new("Next", "N"),
],
(EdgeOperation::Chamfer, EdgeStep::PreviewConfirm, _) => {
vec![CmdOption::new("Distance", "D")]
}
(
EdgeOperation::Chamfer,
EdgeStep::ChamferDistance1 | EdgeStep::ChamferDistance2,
_,
) => vec![CmdOption::new("Expression", "E")],
(EdgeOperation::Fillet, EdgeStep::Selecting, EdgeSelectionMode::Edge) => vec![
CmdOption::new("Chain", "C"),
CmdOption::new("Loop", "L"),
@@ -338,14 +448,14 @@ impl CadCommand for SolidEdgeCommand {
self.handle = Some(handle);
self.last_pick = Some(point);
if self.operation == EdgeOperation::Chamfer {
self.add_batch(vec![seed], point);
self.step = EdgeStep::ChamferValue;
return CmdResult::NeedPoint;
}
if self.step == EdgeStep::PickingLoop {
self.loop_candidates = edge_loops(self.body(handle).expect("pick body exists"), seed);
if self.operation == EdgeOperation::Chamfer {
if let Some(base_face) = self.base_face {
self.loop_candidates
.retain(|(face, _)| *face == base_face);
}
}
self.loop_index = 0;
self.step = if self.loop_candidates.is_empty() {
EdgeStep::Selecting
@@ -356,16 +466,38 @@ impl CadCommand for SolidEdgeCommand {
return CmdResult::NeedPoint;
}
if self.operation == EdgeOperation::Chamfer {
if self.base_face.is_none() {
self.base_face = self.body(handle).and_then(|body| {
nearest_edge_face(body, seed, point.to_array())
});
}
let Some(base_face) = self.base_face else {
return CmdResult::NeedPoint;
};
if !edge_belongs_to_face(self.body(handle).expect("pick body exists"), seed, base_face)
{
return CmdResult::NeedPoint;
}
self.add_batch(vec![seed], point);
self.rebuild_preview();
return CmdResult::NeedPoint;
}
let edges = match self.selection_mode {
EdgeSelectionMode::Edge => vec![seed],
EdgeSelectionMode::Chain => edge_chain(self.body(handle).expect("pick body exists"), seed),
};
self.add_batch(edges, point);
self.rebuild_preview();
CmdResult::NeedPoint
}
fn on_point(&mut self, point: DVec3) -> CmdResult {
if !matches!(self.step, EdgeStep::Radius | EdgeStep::ChamferValue) {
if !matches!(
self.step,
EdgeStep::Radius | EdgeStep::ChamferDistance1 | EdgeStep::ChamferDistance2
) {
return CmdResult::NeedPoint;
}
let Some(anchor) = self.last_pick else {
@@ -375,26 +507,65 @@ impl CadCommand for SolidEdgeCommand {
if value <= 0.0 || !value.is_finite() {
return CmdResult::NeedPoint;
}
if self.step == EdgeStep::ChamferValue {
self.default_value = value;
self.finish()
} else {
self.accept_radius(value).unwrap_or(CmdResult::NeedPoint)
match self.step {
EdgeStep::Radius => self.accept_radius(value).unwrap_or(CmdResult::NeedPoint),
EdgeStep::ChamferDistance1 | EdgeStep::ChamferDistance2 => {
self.accept_distance(value).unwrap_or(CmdResult::NeedPoint)
}
_ => CmdResult::NeedPoint,
}
}
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
let keyword = text.trim().to_ascii_uppercase();
if self.operation == EdgeOperation::Chamfer {
if self.step != EdgeStep::ChamferValue {
return None;
}
let value = keyword.parse::<f64>().ok()?;
if value <= 0.0 || !value.is_finite() {
return None;
}
self.default_value = value;
return Some(self.finish());
return match self.step {
EdgeStep::Selecting => match keyword.as_str() {
"L" | "LOOP" => {
self.step = EdgeStep::PickingLoop;
Some(CmdResult::NeedPoint)
}
"D" | "DISTANCE" => Some(self.begin_distances(EdgeStep::Selecting)),
_ => None,
},
EdgeStep::PickingLoop => match keyword.as_str() {
"E" | "EDGE" => {
self.step = EdgeStep::Selecting;
Some(CmdResult::NeedPoint)
}
"D" | "DISTANCE" => Some(self.begin_distances(EdgeStep::PickingLoop)),
_ => None,
},
EdgeStep::LoopConfirm => match keyword.as_str() {
"A" | "ACCEPT" => Some(self.accept_loop()),
"N" | "NEXT" if !self.loop_candidates.is_empty() => {
self.loop_index = (self.loop_index + 1) % self.loop_candidates.len();
self.rebuild_preview();
Some(CmdResult::NeedPoint)
}
_ => None,
},
EdgeStep::PreviewConfirm => match keyword.as_str() {
"D" | "DISTANCE" => Some(self.begin_distances(EdgeStep::PreviewConfirm)),
_ => None,
},
EdgeStep::ChamferDistance1 | EdgeStep::ChamferDistance2 => {
if matches!(keyword.as_str(), "E" | "EXPRESSION") {
self.expression_return = self.step;
self.step = EdgeStep::Expression;
Some(CmdResult::NeedPoint)
} else {
let value = keyword.parse::<f64>().ok()?;
self.accept_distance(value)
}
}
EdgeStep::Expression => {
let value = crate::app::expr_eval::eval_number(text)?;
self.step = self.expression_return;
self.accept_distance(value)
}
EdgeStep::Radius => None,
};
}
match self.step {
@@ -450,16 +621,29 @@ impl CadCommand for SolidEdgeCommand {
self.accept_radius(value)
}
EdgeStep::Expression => {
let value = keyword.parse::<f64>().ok()?;
let value = crate::app::expr_eval::eval_number(text)?;
self.accept_radius(value)
}
EdgeStep::ChamferValue => None,
EdgeStep::ChamferDistance1 | EdgeStep::ChamferDistance2 => None,
}
}
fn on_enter(&mut self) -> CmdResult {
match (self.operation, self.step) {
(EdgeOperation::Chamfer, EdgeStep::ChamferValue) => self.finish(),
(EdgeOperation::Chamfer, EdgeStep::Selecting) if !self.selected_edges.is_empty() => {
self.step = EdgeStep::PreviewConfirm;
self.rebuild_preview();
CmdResult::NeedPoint
}
(EdgeOperation::Chamfer, EdgeStep::LoopConfirm) => self.accept_loop(),
(EdgeOperation::Chamfer, EdgeStep::PreviewConfirm) => self.finish(),
(EdgeOperation::Chamfer, EdgeStep::ChamferDistance1) => self
.accept_distance(self.default_value)
.unwrap_or(CmdResult::NeedPoint),
(EdgeOperation::Chamfer, EdgeStep::ChamferDistance2) => self
.accept_distance(self.other_value)
.unwrap_or(CmdResult::NeedPoint),
(EdgeOperation::Chamfer, EdgeStep::Expression) => CmdResult::NeedPoint,
(EdgeOperation::Chamfer, _) => CmdResult::Cancel,
(_, EdgeStep::Selecting) if !self.selected_edges.is_empty() => {
self.step = EdgeStep::PreviewConfirm;
@@ -477,9 +661,6 @@ impl CadCommand for SolidEdgeCommand {
}
fn on_undo_step(&mut self) -> Option<CmdResult> {
if self.operation != EdgeOperation::Fillet {
return None;
}
if self.step == EdgeStep::LoopConfirm {
self.loop_candidates.clear();
self.loop_index = 0;
@@ -495,6 +676,7 @@ impl CadCommand for SolidEdgeCommand {
}
if self.selected_edges.is_empty() {
self.handle = None;
self.base_face = None;
}
self.last_pick = self
.selection_batches
@@ -508,17 +690,27 @@ impl CadCommand for SolidEdgeCommand {
fn wants_text_input(&self) -> bool {
matches!(
self.step,
EdgeStep::Radius | EdgeStep::Expression | EdgeStep::ChamferValue
EdgeStep::Radius
| EdgeStep::Expression
| EdgeStep::ChamferDistance1
| EdgeStep::ChamferDistance2
)
}
fn dyn_commit_as_text(&self) -> bool {
matches!(self.step, EdgeStep::Radius | EdgeStep::ChamferValue)
matches!(
self.step,
EdgeStep::Radius | EdgeStep::ChamferDistance1 | EdgeStep::ChamferDistance2
)
}
fn dyn_spec(&self) -> Option<DynSpec> {
let pick = self.last_pick?;
matches!(self.step, EdgeStep::Radius | EdgeStep::ChamferValue).then(|| DynSpec {
matches!(
self.step,
EdgeStep::Radius | EdgeStep::ChamferDistance1 | EdgeStep::ChamferDistance2
)
.then(|| DynSpec {
anchor: DynAnchor::Point(pick),
fields: vec![DynFieldSpec::new(match self.operation {
EdgeOperation::Fillet => DynRole::Radius,
@@ -530,16 +722,26 @@ impl CadCommand for SolidEdgeCommand {
}
fn dyn_live_value(&self, cursor: DVec3) -> Option<f64> {
matches!(self.step, EdgeStep::Radius | EdgeStep::ChamferValue)
matches!(
self.step,
EdgeStep::Radius | EdgeStep::ChamferDistance1 | EdgeStep::ChamferDistance2
)
.then(|| self.last_pick.map(|pick| cursor.distance(pick)))
.flatten()
}
fn on_preview_wires(&mut self, cursor: DVec3) -> Vec<WireModel> {
if self.step == EdgeStep::Radius {
if matches!(
self.step,
EdgeStep::Radius | EdgeStep::ChamferDistance1 | EdgeStep::ChamferDistance2
) {
if let Some(value) = self.last_pick.map(|pick| cursor.distance(pick)) {
if value > 0.0 && value.is_finite() {
if let Some((handle, wires)) = self.preview_for_value(value) {
let values = match self.step {
EdgeStep::ChamferDistance2 => (self.default_value, value),
_ => (value, self.other_value),
};
if let Some((handle, wires)) = self.preview_for_values(values.0, values.1) {
self.preview_wires = wires;
self.preview_hidden.clear();
self.preview_hidden.push(handle);
@@ -558,7 +760,7 @@ impl CadCommand for SolidEdgeCommand {
}
}
fn edge_loops(body: &Body, seed: EdgeKey) -> Vec<Vec<EdgeKey>> {
fn edge_loops(body: &Body, seed: EdgeKey) -> Vec<(FaceKey, Vec<EdgeKey>)> {
let Some(edge) = body.edges.get(seed) else {
return Vec::new();
};
@@ -575,13 +777,48 @@ fn edge_loops(body: &Body, seed: EdgeKey) -> Vec<Vec<EdgeKey>> {
.iter()
.filter_map(|key| body.coedges.get(*key).map(|coedge| coedge.edge))
.collect::<Vec<_>>();
if !edges.is_empty() && !candidates.contains(&edges) {
candidates.push(edges);
let candidate = (edge_loop.owner, edges);
if !candidate.1.is_empty() && !candidates.contains(&candidate) {
candidates.push(candidate);
}
}
candidates
}
fn edge_belongs_to_face(body: &Body, edge: EdgeKey, face: FaceKey) -> bool {
body.edges.get(edge).is_some_and(|edge| {
edge.coedges.iter().any(|coedge| {
body.coedges
.get(*coedge)
.and_then(|coedge| body.loops.get(coedge.owner))
.is_some_and(|edge_loop| edge_loop.owner == face)
})
})
}
fn nearest_edge_face(body: &Body, edge: EdgeKey, point: [f64; 3]) -> Option<FaceKey> {
let edge = body.edges.get(edge)?;
edge.coedges
.iter()
.filter_map(|coedge| {
let edge_loop = body.loops.get(body.coedges.get(*coedge)?.owner)?;
let face = body.faces.get(edge_loop.owner)?;
let surface = body.surfaces.get(face.surface)?;
let distance = surface.distance_to(point).abs();
distance.is_finite().then_some((edge_loop.owner, distance))
})
.min_by(|first, second| first.1.total_cmp(&second.1))
.map(|(face, _)| face)
}
fn positive_default(value: f64) -> f64 {
if value.is_finite() && value > 0.0 {
value
} else {
1.0
}
}
fn edge_chain(body: &Body, seed: EdgeKey) -> Vec<EdgeKey> {
let mut chain = vec![seed];
let mut next = 0;
@@ -643,5 +880,5 @@ fn edge_tangent_from_vertex(
}
inventory::submit!(crate::command::CommandRegistration {
names: &["FILLETEDGE", "SOLIDFILLET", "SOLIDCHAMFER"]
names: &["FILLETEDGE", "SOLIDFILLET", "CHAMFEREDGE", "SOLIDCHAMFER"]
});
+1 -1
View File
@@ -89,7 +89,7 @@ impl CadModule for ModelModule {
title: "Edges",
tools: vec![
RibbonItem::LargeTool(tool("FILLETEDGE", "Fillet Edge", FILLET_ICON)),
RibbonItem::LargeTool(tool("SOLIDCHAMFER", "Chamfer", CHAMFER_ICON)),
RibbonItem::LargeTool(tool("CHAMFEREDGE", "Chamfer", CHAMFER_ICON)),
],
},
]