Files
makeCAD/crates/ui-egui/src/dialogs_plastic.rs
T
Daniel 2e566e2c58 Placement handles for Rest, Boss and Snap Fit (#4384) (#5362)
* claim: #4384

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: shared module, with Rest as the first tool

The Rest handles spike becomes a shared module, place_gizmo, so other features placed on a face can
use the same marker and knob. The mechanics (crosshair with its three states, the grab offset, the
footprint while dragging, the turn knob) live in place_gizmo.rs. The handles own the pointer through
canvas::owns_pointer, as the move triad does, so navigation and Select Other's press-and-hold leave
them alone. Each tool is one file under place_gizmo/ that gives a Spec: which dialogs it serves,
which commands' Position input also takes sketch points, where the place is, how the pointer
re-places it, its footprint and its turn, if any. The module lists the tools one per line, so tools
added on separate branches each add one `mod` line and one list line. Rest is the first.

The handles are always on; the preference the Rest spike had is gone. The POINTS accept flag on a
Position input is granted from the tools' Specs instead of a Rest-only arm in the plastic dialog's
start. Handles are published as `rest:position` and `rest:rotate` as before.

Seen straight down, the dialog's height arrow collapses onto the place and its value box sits just
right of it, where it took the press meant for a knob pointing that way. The knob now slides out
along its ray until it is clear of the box.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Placement handles: Boss

The marker moves a boss across the model, also from Edit Feature, and its Position input takes
sketch points while the handles are on, so a screw boss can be dragged onto the centre of a
circle drawn for it. A boss is round, so it gets no knob; the footprint while dragging is its
outer circle. Published as `boss:position`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Placement handles: Snap Fit

The marker moves a snap fit across the model and its Position input takes sketch points while
the handles are on, so the arm can be put where a sketch says (#3423). The knob points the
catch: it turns between the four directions the Catch Direction combo offers for the face, so
the knob and the combo always agree; the command keeps only those four, so there is no free
turn. The footprint while dragging is the arm's foot, its thickness behind the place and its
width across. Published as `snap_fit:position` and `snap_fit:rotate`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Placement handles: the look in one file

Everything that draws the handles (the crosshair and its three eased states, the knob's ball, the
cased lines, the angle label, the colours) moves from place_gizmo.rs into place_gizmo/look.rs, so a
restyle touches one file and the mechanics (hit radius, drags, knob geometry) stay where they are.
No change in behaviour.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: behind a Design preference, off by default

The handles sit behind Preferences > Design > Placement > Placement handles (`placement_handles`,
also through `ui.prefs`), off by default as the hole placement work in #2912 is: a Rest, Boss or
Snap Fit is then placed by its click alone, its Position input takes faces only, and no handle is
drawn or published. The tests switch the preference on; one checks the default.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: the tests hide the origin through set_origin_shown, as main does since #1849

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: the move marker as designed in the prototype, drawn with theme tokens

The marker is a dot with four arms to the screen cardinals: short and dotted at rest, longer with open arrowheads when pointed at, solid while dragging, each state growing out of the previous one (360 ms). Sizes are the prototype's. Its core is the icon highlight and its casing the marker halo, the tokens main's hole handle uses, so the literal allowance goes. The tip shows while pointing, not during a drag.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: the dialog's drag arrow stands above the move marker

With the handles on, the Height arrow started at the marker's centre and ran through its upper arm, head on arm tip. The arrow now keeps the marker's full reach and a gap free at the base, its shortest length grows by as much, and the base dot is left to the marker. Other dialogs are unchanged (clearance zero).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: a turn ring on the face replaces the knob

The ring lies on the face about the move marker's centre, drawn in perspective, a fixed 65 px on screen like the move triad's arrows (it is a control of something on the screen, not a measure of the model). At rest it is a short dotted arc with an open chevron grip just outside; pointed at, the dotted circle opens from the grip both ways round and the grip slides in and fills; while turning, a solid arc around the grip. Dragging the grip turns the feature as the knob did, through the same handle name, so the harness tests stand.

The floating value box yields: with a handle's centre under it, it moves to the other side of the arrow's tip or above it, since seen straight down the box sat exactly where the grip is. A handle the arrow's stem crosses keeps the pointer.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: a compass while turning, and the turn pulled onto the face's compass points

Four ticks just outside the ring at the face's in-face world directions (the ones the Snap Fit combo names, +X, −Y, …) appear while turning and stay put, so you can see how far you have gone. A Rest's turn is pulled onto one of them within 8°, otherwise it steps by 5° as before; Alt or Ctrl frees it. The readout beside the grip names the point it sits on ("90° · +X") and keeps clear of the ring.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: the footprint in the preview's blue, the idle handle steps back

The footprint drawn while the marker drags is a filled shape in the preview's own blue with a thin darker edge, not a white outline, so white stays reserved for what can be grabbed. While one handle is dragged the other fades to a quarter and comes back when the drag ends.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: the ring in the preference text, the docs and the tests' wording

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: the look as tuned in the app

Marker: drawn to the redrawn design (dot r 3.9 pt, two dashes per arm, a filled head 8.7 × 7.1 pt whose base is where the outer dash began); pointed at, the outer dash becomes the head (its far end narrows to a point, its near end widens); dragging runs the inner dash on into the head as a shaft. Every casing is 0.4 pt outside the edge, for lines, heads and the dot alike.

Ring: the grip and the compass lie in the ring's plane like the circle, so the grip is never skewed against it; the grip is a thick chevron with a hole that flattens into the base as the grip slides onto the ring, and fills; the dotted circle stays while turning, with the solid arc laid over it; opening eases in and takes 0.3 s, leaving is a 0.15 s fade over the resting ring, not the sweep backwards; a drag keeps the angular gap of the press, so the grip does not jump onto the pointer; the compass ticks and the readout's point name are in their axis colours, and the grip takes the axis colour (eased) while aligned with one, with that point's tick left out; free turning keeps a hair of pull (0.5°) so an axis can still be hit; the readout wraps to a half turn.

Value box: with a ring it keeps out of the circle the ring, grip, compass and readout sweep, one place per view, nothing moves when a turn starts; also when the value does not evaluate and there is no arrow.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: the readout shows a half turn as 180°, not −180°

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: the motions in one place

The animations the handles grew (a factor eased in and out each way on its own time and easing, a handle stepping back while the other works, a colour that comes and goes from the last one it was on, the gap a press keeps for its drag, an offset that slides instead of jumping) move out of place_gizmo into motion.rs, so the next manipulator reuses them. The look's timings become Transitions in look.rs. No change in behaviour: the contact sheet before and after is identical, the harness tests stand.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Placement handles: the axis colours as main names them since #4430

The compass and the aligned grip take the one set of X, Y and Z colours (axis_x, axis_y, axis_z); the Move gizmo's own array is gone.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-26 15:35:10 +00:00

481 lines
23 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Plastic dialogs: Boss, Lip / Groove, Snap Fit and Rest (placed by a pick on a face), Manage
//! Plastic Rules and Assign Plastic Rule. Values left empty take the body's plastic rule.
use crate::dialogs::put;
use crate::ui_kit::{choice_row, expr_row, hint_row};
use serde_json::{Map, Value, json};
use solidcraft_engine::Sel;
use solidcraft_engine::doc::FeatureKind;
use solidcraft_engine::doc::expr::Kind as ValueKind;
use solidcraft_engine::doc::plastic::PlasticRule;
use solidcraft_engine::geom::Vec3;
use crate::SolveApp;
use crate::dialogs::{Kind, combo, face_sel, field, form_row, shown_expr};
use crate::selection::{BODIES, FACES, PLANAR_FACES, SelInput, SelectionExt as _, point_value as pt};
crate::dialog_descriptor::dialog_descriptors! {
#[derive(Clone, Debug)]
pub enum Pl {
Boss("plastic.boss", "BOSS", [("Position", PLANAR_FACES, false)],
FeatureKind::Boss { diameter, height, hole_diameter, hole_depth, draft, fillet, ribs, rib_thickness, rib_length, rib_offset, .. }) {
diameter: String = "6 mm".into() => shown_expr(diameter, ValueKind::Length),
height: String = "10 mm".into() => shown_expr(height, ValueKind::Length),
hole_diameter: String = "3 mm".into() => shown_expr(&s(hole_diameter), ValueKind::Length),
hole_depth: String = String::new() => shown_expr(&s(hole_depth), ValueKind::Length),
draft: String = String::new() => shown_expr(&s(draft), ValueKind::Angle),
fillet: String = String::new() => shown_expr(&s(fillet), ValueKind::Length),
ribs: String = "0".into() => ribs.as_deref().map(str::to_string).unwrap_or_else(|| "0".into()),
rib_thickness: String = "1 mm".into() => rib_thickness.as_deref().map(|v| shown_expr(v, ValueKind::Length)).unwrap_or_else(|| "1 mm".into()),
rib_length: String = "3 mm".into() => rib_length.as_deref().map(|v| shown_expr(v, ValueKind::Length)).unwrap_or_else(|| "3 mm".into()),
rib_offset: String = "1 mm".into() => rib_offset.as_deref().map(|v| shown_expr(v, ValueKind::Length)).unwrap_or_else(|| "1 mm".into()),
} => json!({"diameter": diameter, "height": height});
Lip("plastic.lip", "LIP", [("Rim face", FACES, false)],
FeatureKind::Lip { groove, width, height, gap, outside, .. }) {
groove: bool = false => *groove,
width: String = "1.5 mm".into() => shown_expr(width, ValueKind::Length),
height: String = "2 mm".into() => shown_expr(height, ValueKind::Length),
gap: String = String::new() => shown_expr(&s(gap), ValueKind::Length),
outside: bool = false => *outside,
} => json!({"width": width, "height": height, "type": if *groove { "groove" } else { "lip" }, "side": if *outside { "outside" } else { "inside" }});
Rest("plastic.rest", "REST", [("Position", PLANAR_FACES, false)],
FeatureKind::Rest { width, length, height, draft, thickness, .. }) {
round: bool = true => length.is_none(),
width: String = "10 mm".into() => shown_expr(width, ValueKind::Length),
length: String = "15 mm".into() => length.as_deref().map(|l| shown_expr(l, ValueKind::Length)).unwrap_or_else(|| "15 mm".into()),
height: String = "2 mm".into() => shown_expr(height, ValueKind::Length),
draft: String = String::new() => shown_expr(&s(draft), ValueKind::Angle),
thickness: String = String::new() => shown_expr(&s(thickness), ValueKind::Length),
} => json!({"width": width, "height": height});
; custom {
SnapFit {
hook: usize,
length: String,
thickness: String,
width: String,
catch_depth: String,
catch_length: String,
base_height: String,
new_body: bool,
},
/// The rule manager: the rule shown (index into the rules, past the end for a new one).
Rules {
pick: usize,
name: String,
values: Vec<String>,
active: bool,
loaded: Option<usize>,
},
Assign {
rule: usize,
},
}
}
}
impl Pl {
pub fn title(&self) -> &'static str {
match self {
Pl::Lip { groove: true, .. } => "GROOVE",
Pl::SnapFit { .. } => "SNAP FIT",
Pl::Rules { .. } => "MANAGE PLASTIC RULES",
Pl::Assign { .. } => "ASSIGN PLASTIC RULE",
_ => self.simple_metadata().map_or("", |(_, title)| title),
}
}
pub fn previews(&self) -> bool {
!matches!(self, Pl::Rules { .. } | Pl::Assign { .. })
}
pub fn primary(&mut self) -> Option<(&'static str, ValueKind, &mut String)> {
Some(match self {
Pl::Boss { height, .. } | Pl::Lip { height, .. } | Pl::Rest { height, .. } => ("Height", ValueKind::Length, height),
Pl::SnapFit { length, .. } => ("Length", ValueKind::Length, length),
_ => return None,
})
}
}
fn snap_fit(app: &SolveApp) -> Pl {
Pl::SnapFit {
hook: 0,
length: crate::dialogs::length_default(&app.session, "10"),
thickness: crate::dialogs::length_default(&app.session, "1.5"),
width: crate::dialogs::length_default(&app.session, "5"),
catch_depth: crate::dialogs::length_default(&app.session, "1"),
catch_length: crate::dialogs::length_default(&app.session, "2"),
base_height: String::new(),
new_body: false,
}
}
/// The dialog for a plastic command.
pub fn start(app: &SolveApp, id: &str) -> Option<(Kind, Vec<SelInput>)> {
let (pl, inputs) = match id {
"plastic.snap_fit" => (snap_fit(app), vec![SelInput::new("Position", PLANAR_FACES, false)]),
"plastic.manage_rules" => {
let rules = app.session.doc.plastic_rules();
let pick = app.session.doc.plastic.active.as_ref().and_then(|a| rules.iter().position(|r| &r.name == a)).unwrap_or(0);
(Pl::Rules { pick, name: String::new(), values: Vec::new(), active: false, loaded: None }, vec![])
}
"plastic.assign_rule" => (Pl::Assign { rule: 1 }, vec![SelInput::new("Bodies", BODIES, true)]),
_ => Pl::simple_start(id)?,
};
let mut inputs = inputs;
// A tool with placement handles snaps its place to sketch points on the face as a Single
// hole's does, so the marker can be dragged onto one; a bare sketch point is refused by OK as
// before.
if crate::place_gizmo::snaps_to_points(app, id)
&& let Some(position) = inputs.first_mut()
{
position.accept |= crate::selection::POINTS;
}
Some((crate::length_input::defaults(&app.session.doc, Kind::Plastic(pl)), inputs))
}
/// A value row whose empty value means "from the rule" (or another default).
fn opt_field(ui: &mut egui::Ui, label: &str, v: &mut String, unit: &str, hint: &str) -> bool {
let hint = format!("{unit}, {}", crate::i18n::tr_label(hint));
hint_row(ui, label, v, &hint)
}
/// Directions across a face (with normal `n`) a snap fit's catch can point: two axes and their
/// opposites, named after the world axis they run along.
pub fn hook_dirs(n: Vec3) -> Vec<(String, Vec3)> {
let u = [Vec3::X, Vec3::Y, Vec3::Z].into_iter().filter_map(|a| (a - n * a.dot(n)).normalized()).next().unwrap_or_else(|| n.any_perp());
let v = n.cross(u);
let name = |d: Vec3| -> String {
let (axis, c) = [("X", d.x), ("Y", d.y), ("Z", d.z)].into_iter().max_by(|a, b| a.1.abs().total_cmp(&b.1.abs())).unwrap_or(("X", 0.0));
format!("{}{axis}", if c < 0.0 { "−" } else { "+" })
};
[u, -u, v, -v].into_iter().map(|d| (name(d), d)).collect()
}
fn face_normal(app: &SolveApp, inputs: &[SelInput]) -> Option<Vec3> {
match inputs.first()?.items.first()? {
Sel::Face { body, index, .. } => crate::dialogs::planar_face(&app.session, body, *index).map(|(_, n)| n),
_ => None,
}
}
/// The dialog's own rows (after the selection inputs); true when Enter was pressed in a value.
pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Pl, inputs: &[SelInput]) -> bool {
let mut enter = false;
match k {
Pl::Boss { diameter, height, hole_diameter, hole_depth, draft, fillet, ribs, rib_thickness, rib_length, rib_offset } => {
enter |= expr_row(ui, "Diameter", diameter);
enter |= expr_row(ui, "Height", height);
crate::ui_kit::section(ui, "boss_options", "Options", true, |ui| {
enter |= opt_field(ui, "Hole Diameter", hole_diameter, crate::length_input::unit(&app.session.doc), "no hole");
enter |= opt_field(ui, "Hole Depth", hole_depth, crate::length_input::unit(&app.session.doc), "to the face");
});
crate::ui_kit::section(ui, "boss_advanced", "Advanced", false, |ui| {
enter |= opt_field(ui, "Draft Angle", draft, "deg", "from the rule");
enter |= opt_field(ui, "Root Fillet", fillet, crate::length_input::unit(&app.session.doc), "none");
enter |= expr_row(ui, "Ribs", ribs);
if app.session.doc.eval(ribs, ValueKind::Unitless).is_ok_and(|n| n >= 1.0) {
enter |= expr_row(ui, " Rib Thickness", rib_thickness);
enter |= expr_row(ui, " Rib Length", rib_length);
enter |= expr_row(ui, " Rib Offset", rib_offset);
}
});
}
Pl::Lip { groove, width, height, gap, outside } => {
form_row(ui, "Type", |ui| {
let mut ty = usize::from(*groove);
combo(ui, "pl_lip", &["Lip", "Groove"], &mut ty);
*groove = ty == 1;
});
form_row(ui, "Rim Edge", |ui| {
let mut side = usize::from(*outside);
combo(ui, "pl_side", &["Inside", "Outside"], &mut side);
*outside = side == 1;
});
enter |= expr_row(ui, "Width", width);
enter |= expr_row(ui, "Height", height);
if *groove {
enter |= opt_field(ui, "Clearance", gap, crate::length_input::unit(&app.session.doc), "from the rule");
}
}
Pl::SnapFit { hook, length, thickness, width, catch_depth, catch_length, base_height, new_body } => {
let dirs = face_normal(app, inputs).map(hook_dirs).unwrap_or_default();
if !dirs.is_empty() {
let names: Vec<&str> = dirs.iter().map(|(n, _)| n.as_str()).collect();
choice_row(ui, "Catch Direction", "pl_hook", hook, &names);
}
enter |= expr_row(ui, "Length", length);
enter |= expr_row(ui, "Thickness", thickness);
enter |= expr_row(ui, "Width", width);
enter |= expr_row(ui, "Catch Depth", catch_depth);
enter |= expr_row(ui, "Catch Length", catch_length);
enter |= opt_field(ui, "Base Height", base_height, crate::length_input::unit(&app.session.doc), "at the face");
form_row(ui, "Body", |ui| {
let mut op = usize::from(*new_body);
combo(ui, "pl_snap_body", &["Join", "New Body"], &mut op);
*new_body = op == 1;
});
}
Pl::Rest { round, width, length, height, draft, thickness } => {
form_row(ui, "Shape", |ui| {
let mut sh = usize::from(!*round);
combo(ui, "pl_rest", &["Round", "Rectangular"], &mut sh);
*round = sh == 0;
});
enter |= expr_row(ui, if *round { "Diameter" } else { "Width" }, width);
if !*round {
enter |= expr_row(ui, "Length", length);
}
enter |= expr_row(ui, "Height", height);
crate::ui_kit::section(ui, "rest_advanced", "Advanced", false, |ui| {
enter |= opt_field(ui, "Draft Angle", draft, "deg", "from the rule");
enter |= opt_field(ui, "Wall Thickness", thickness, crate::length_input::unit(&app.session.doc), "solid");
});
}
Pl::Rules { pick, name, values, active, loaded } => {
let rules = app.session.doc.plastic_rules();
crate::rule_editor::rule_editor! {
ui, "pl_rules", rules, pick, name, active, loaded;
new_name: format!("Plastic Rule{}", app.session.doc.plastic.rules.len() + 1),
load: {
let base = rules.get(*pick).or(rules.first()).cloned();
if let Some(b) = base {
*values = std::iter::once(b.material.clone())
.chain(b.fields().iter().map(|(_, e, kind)| {
if *kind == ValueKind::Length {
crate::length_input::stored(&app.session.doc, e)
} else {
crate::dialogs::shown_expr(e, *kind)
}
}))
.collect();
*active = app.session.doc.plastic.active.as_deref() == Some(b.name.as_str()) && *pick < rules.len();
}
},
fields: {
let keys = rule_keys();
for (i, v) in values.iter_mut().enumerate() {
let label = if i == 0 { "Material".to_string() } else { keys.get(i - 1).map(|k| label_of(k)).unwrap_or_default() };
form_row(ui, &label, |ui| {
enter |= if i == 0 { crate::ui_kit::text_field(ui, v, "Material").0 } else { field(ui, v) };
});
}
}
}
}
Pl::Assign { rule } => {
let mut names: Vec<String> = vec!["(no rule)".into()];
names.extend(app.session.doc.plastic_rules().into_iter().map(|r| r.name));
let refs: Vec<&str> = names.iter().map(String::as_str).collect();
choice_row(ui, "Rule", "pl_assign", rule, &refs);
}
}
enter
}
/// The rule's value fields, in the order `PlasticRule::fields` gives them.
fn rule_keys() -> Vec<&'static str> {
PlasticRule::library().first().map(|r| r.fields().iter().map(|(k, _, _)| *k).collect()).unwrap_or_default()
}
fn label_of(key: &str) -> String {
key.split('_')
.map(|w| {
let mut c = w.chars();
c.next().map(|f| f.to_uppercase().collect::<String>() + c.as_str()).unwrap_or_default()
})
.collect::<Vec<_>>()
.join(" ")
}
/// The commands the dialog's OK runs (`extra`: parameters kept from the feature being edited).
pub fn commands(app: &SolveApp, k: &Pl, inputs: &[SelInput], extra: &Map<String, Value>) -> Result<Vec<(String, Value)>, String> {
let face = inputs
.first()
.and_then(|i| i.items.first())
.and_then(|x| if let Sel::Face { body, point, .. } = x { Some((body.clone(), *point)) } else { None });
let placed = |what: &str| -> Result<Value, String> {
let (body, at) = face.clone().ok_or_else(|| format!("select {what} first"))?;
Ok(json!({"position": pt(at), "body": body}))
};
let (cmd, mut p): (&str, Value) = match k {
Pl::Boss { hole_diameter, hole_depth, draft, fillet, ribs, rib_thickness, rib_length, rib_offset, .. } => {
let mut p = placed("a point on a face")?;
put(&mut p, &[("hole_diameter", hole_diameter), ("hole_depth", hole_depth), ("draft", draft), ("fillet", fillet)]);
if app.session.doc.eval(ribs, ValueKind::Unitless).is_ok_and(|n| n >= 1.0) {
put(&mut p, &[("ribs", ribs), ("rib_thickness", rib_thickness), ("rib_length", rib_length), ("rib_offset", rib_offset)]);
}
("plastic.boss", p)
}
Pl::Lip { groove, gap, .. } => {
let (body, at) = face.clone().ok_or("select the rim face first")?;
let mut p = json!({"face": pt(at), "body": body});
if *groove {
put(&mut p, &[("gap", gap)]);
}
("plastic.lip", p)
}
Pl::SnapFit { hook, length, thickness, width, catch_depth, catch_length, base_height, new_body } => {
let mut p = placed("a point on a face")?;
let n = extra
.get("direction")
.and_then(|v| Some(Vec3::new(v.get(0)?.as_f64()?, v.get(1)?.as_f64()?, v.get(2)?.as_f64()?)))
.or(face_normal(app, inputs));
let dir = n.map(hook_dirs).and_then(|d| d.get(*hook).map(|x| x.1)).ok_or("pick a planar face")?;
p["hook"] = pt(dir);
for (key, v) in
[("length", length), ("thickness", thickness), ("width", width), ("catch_depth", catch_depth), ("catch_length", catch_length)]
{
p[key] = json!(v);
}
put(&mut p, &[("base_height", base_height)]);
if *new_body {
p["new_body"] = json!(true);
}
("plastic.snap_fit", p)
}
Pl::Rest { round, length, draft, thickness, .. } => {
let mut p = placed("a point on a face")?;
if !*round {
p["length"] = json!(length);
}
put(&mut p, &[("draft", draft), ("thickness", thickness)]);
("plastic.rest", p)
}
Pl::Rules { pick, name, values, active, .. } => {
let rules = app.session.doc.plastic_rules();
let fields = std::iter::once("material").chain(rule_keys()).zip(values);
let p = crate::rule_editor::params(rules.get(*pick).map(|r| r.name.as_str()), name, *active, fields)?;
("plastic.manage_rules", p)
}
Pl::Assign { rule } => {
let bodies = crate::selection::items(inputs, 0).body_names();
if bodies.is_empty() {
return Err("select bodies first".into());
}
let name = rule.checked_sub(1).and_then(|i| app.session.doc.plastic_rules().get(i).map(|r| r.name.clone())).unwrap_or_default();
("plastic.assign_rule", json!({"bodies": bodies, "rule": name}))
}
};
k.fill_simple_params(&mut p);
if let Value::Object(m) = &mut p {
for (key, v) in extra {
m.entry(key.clone()).or_insert_with(|| v.clone());
}
}
Ok(vec![(cmd.to_string(), p)])
}
fn s(o: &Option<solidcraft_engine::doc::Expr>) -> String {
o.as_deref().map(str::to_string).unwrap_or_default()
}
/// The dialog editing a plastic feature, filled from it.
pub fn for_feature(app: &SolveApp, kind: &FeatureKind) -> Option<(Kind, Vec<SelInput>, Map<String, Value>)> {
let ss = &app.session;
let mut extra = Map::new();
let (pl, id, at): (Pl, &str, Vec3) = match kind {
FeatureKind::Boss { position, direction, body, .. } => {
extra.insert("direction".into(), pt(direction.0));
if let Some(b) = body {
extra.insert("body".into(), json!(b));
}
(Pl::simple_restore(kind)?, "plastic.boss", position.0)
}
FeatureKind::Lip { face, body, .. } => {
if let Some(b) = body {
extra.insert("body".into(), json!(b));
}
(Pl::simple_restore(kind)?, "plastic.lip", face.0)
}
FeatureKind::SnapFit { position, direction, hook, length, thickness, width, catch_depth, catch_length, base_height, new_body, body } => {
extra.insert("direction".into(), pt(direction.0));
if let Some(b) = body {
extra.insert("body".into(), json!(b));
}
let h = hook_dirs(direction.0).iter().position(|(_, d)| d.dist(hook.0) < 1e-6);
if h.is_none() {
extra.insert("hook".into(), pt(hook.0));
}
let f = Pl::SnapFit {
hook: h.unwrap_or(0),
length: shown_expr(length, ValueKind::Length),
thickness: shown_expr(thickness, ValueKind::Length),
width: shown_expr(width, ValueKind::Length),
catch_depth: shown_expr(catch_depth, ValueKind::Length),
catch_length: shown_expr(catch_length, ValueKind::Length),
base_height: base_height.as_deref().map(|v| shown_expr(v, ValueKind::Length)).unwrap_or_default(),
new_body: *new_body,
};
(f, "plastic.snap_fit", position.0)
}
FeatureKind::Rest { position, direction, along, body, .. } => {
extra.insert("direction".into(), pt(direction.0));
if let Some(a) = along {
extra.insert("along".into(), pt(a.0));
}
if let Some(b) = body {
extra.insert("body".into(), json!(b));
}
let r = Pl::simple_restore(kind)?;
(r, "plastic.rest", position.0)
}
_ => return None,
};
let (_, mut inputs) = start(app, id)?;
if let Some(i) = inputs.first_mut() {
i.items = face_sel(ss, at).into_iter().collect();
}
// A pick that no longer finds its face keeps the stored point.
if let Some(i) = inputs.first_mut().filter(|i| i.items.is_empty()) {
i.items = vec![Sel::Face { body: extra.get("body").and_then(Value::as_str).unwrap_or_default().to_string(), index: 0, point: at }];
}
Some((Kind::Plastic(pl), inputs, extra))
}
/// The placement handles' frame for a feature placed through its first input (a face place): the
/// picked point and the direction the feature grows, the picked face's normal, else the stored
/// `direction`.
pub(crate) fn handle_frame(app: &SolveApp, d: &crate::dialogs::Dialog) -> Option<(Vec3, Vec3)> {
let Sel::Face { body, index, point } = d.inputs.first()?.items.first()? else { return None };
let n = crate::dialogs::planar_face(&app.session, body, *index)
.map(|(_, n)| n)
.or_else(|| d.extra.get("direction").and_then(crate::dialogs::vec3_of))
.and_then(|n| n.normalized())?;
Some((*point, n))
}
/// The placement handles' move for a feature placed through its first input: re-place it where a
/// click at `pos` would (snapped to a sketch point as a click is when the input takes points), with
/// the stored `direction` and in-face `along` carried onto the new face. False when nothing there
/// can take it.
pub(crate) fn handle_move(app: &SolveApp, d: &mut crate::dialogs::Dialog, proj: &crate::viewport::Proj, pos: egui::Pos2) -> bool {
use crate::place_gizmo::{angle_of, dir_at};
let s = &app.session;
let turned = handle_frame(app, d).and_then(|(_, n)| d.extra.get("along").and_then(crate::dialogs::vec3_of).map(|a| angle_of(a, n)));
let hits = crate::viewport::pick(app, proj, pos);
let snap = d.face_snap(s, &hits);
let Some(to) = hits.iter().find_map(|h| d.candidate_on(s, h, snap.as_ref()).filter(|c| matches!(c, Sel::Face { .. }))) else {
return false;
};
if let Sel::Face { body, index, .. } = &to
&& let Some((_, nn)) = crate::dialogs::planar_face(s, body, *index)
{
if d.extra.contains_key("direction") {
d.extra.insert("direction".into(), pt(nn));
}
if let Some(a) = turned {
d.extra.insert("along".into(), pt(dir_at(nn, a)));
}
}
if let Some(inp) = d.inputs.first_mut() {
inp.items = vec![to];
}
d.error = None;
true
}
#[cfg(test)]
#[path = "dialogs_plastic_tests.rs"]
mod tests;