Files
solidcraft/crates/doc/src/eval_frame.rs
T
Karl Von RichterandClaude Opus 5.5 27d2fe6ec0 Frames: profile library, members along frame sketch lines, and the Frame Member dialog (#4003) (#4004)
* claim: #4003

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

* Frames: the profile library (#4003)

The first slice of frames and weldments: the cross-sections structural members
will be made of, each SolidCraft's own parametric definition built in code
(no vendor or standard size tables are shipped).

- solidcraft_doc::frame: bar, round bar, rectangular and round tube, angle,
  channel, I-beam and a generic T-slot extrusion as exact line/arc regions,
  with defaults worked out from the profile's size, refusals that name the
  dimension that does not fit, anchors and placement, and exact section
  properties (area, centroid, Ixx/Iyy/Ixy by Green's theorem).
- frame.profile.list, frame.profile.properties (with mass per metre for a
  density) and frame.profile.insert (draws a profile into a sketch, locked by
  default, at a point, anchor and angle).
- Tests against hand-computed values (rectangles, circles, composite
  rectangles by the parallel-axis theorem, fillet spandrels), hostile input,
  extrusion volume, undo; catalog entries in every locale; docs/frames.md;
  ROADMAP M15.

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

* Frames: drop the built-in T-slot profile (#4003)

Aluminium extrusions and other vendor sections come from the user's own
sketch (drawn or inserted from the vendor's DXF), so the library keeps the
plain steel shapes only.

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

* Frames: members along frame sketch lines (#4003)

frame.member lays one body per straight line of a frame sketch (2D lines or
3D sketch lines), its cross-section square to the line from end to end:

- profile: a built-in profile, or profile_sketch, a sketch whose material
  (nested loops alternating, so two circles make a tube) is the section in
  its own x and y around its origin; profiles picks override it.
- anchor (origin, centre, centroid or a bounding-box corner or edge middle),
  angle (an angle expression, so a parameter), mirror.
- Orientation: the profile's y as near world up as the line allows, x
  square to both; a vertical member's x is world X (frame::member_axes).
- Members follow sketch and parameter edits, undo as one step, save and
  reopen (format: pending/frame-members.rs), and are named after the profile
  (Rect Tube 40x40x3, (2)…), numbering on past names already taken.

Tests against hand-computed volumes and bounding boxes (rectangle, 3D
tower lines, parameter edit, sketch tube profile and picks, anchor/angle/
mirror), refusals, undo/redo and reopen; walk fixtures and goldens; docs.

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

* Frames: fixes from the adversarial review (#4003)

- Default member lines are what was drawn: projected and other linked curves
  and 3D wires (a face sketch's edges) and lines of no length are left out.
- A profile sketch's material starts from its drawn profiles, as
  solid.extrude's "all" does, so a section sketched on a face works; the
  even-odd test uses outlines and boxes worked out once, capped at 256.
- Member names are pinned to their lines (`names`, by line id; `body_name`
  is the base): deleting or adding a line, or deleting another feature, no
  longer renames other members, and frame.member stores every name it gives.
  body.rename renames a member on its line (also for later member features);
  a copy whose body_names were set still names its members by position.
- Built-in profile dimensions are expressions (ProfileSpec): they follow
  parameters, bare numbers take the design's units, and they are feature
  inputs. frame.profile.properties/insert read them the same way.
- Default root/toe radii shrink to the room a thick section leaves; radii
  given are checked by name; a radius must be 0 or at least 0.01 mm; error
  messages report the wall or flange value given, not twice it.
- A line within 1e-6 rad of vertical orients as vertical, so rounding never
  turns a post a quarter turn.
- A 2D frame sketch in a differently placed component is mapped into the
  member's frame, and member features recompute when placements change.
- Caps before lookups for a design's `lines`; a line listed twice is
  refused; a 3D wire that runs back on itself is not a line.

Regression tests for each, from the review's scenarios.

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

* Frames: the Frame Member dialog on the ribbon (#4003)

- Frame Member (MODEL > Create) opens a dialog: the frame sketch's lines
  picked in the view (or All Lines), a built-in profile and its dimensions or
  a section picked in another sketch, anchor, angle, mirror and name, with a
  live preview. Edit Feature reopens it filled in.
- Lines picked in the view keep their sketch (a SKETCH_LINES input picks
  ExternalSketchCurve): line ids are each sketch's own, so two 3D sketches
  both have a `w2`, and the members went on the newer sketch's lines.
- timeline.redefine takes frame.member. Members keep their names through an
  edit unless it gives them a new body_name; frame.member takes `names`.
- A member whose section is a whole profile sketch keeps it when edited
  (the dialog shows the sketch instead of picking every region, which would
  fill a tube's hole).
- An original frame_member icon, open for review, on the ribbon, timeline
  and browser.
- The angle profile is listed as L-Angle, and web and flange as Web/Flange
  thickness, so they translate apart from the existing Angle, Web and Flange;
  the dialog's labels are in en, ja and zh. en-GB no longer repeats the frame
  command labels it spells the same.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 14:22:41 +00:00

244 lines
12 KiB
Rust

//! Frame members (#4003): one body per straight line of a frame sketch, its cross-section a
//! profile laid square to the line (`frame::member_axes`) and run from one end to the other.
use std::borrow::Cow;
use std::collections::{BTreeMap, HashSet};
use solidcraft_geom::{Aabb2, Plane, Region2, Ring, Vec2, Vec3};
use solidcraft_kernel as kernel;
use solidcraft_sketch::CurveKind;
use super::{ModelBody, ModelState, SolvedSketch, new_name, path_in_frame, select_profiles, sketch_in_feature_frame, val};
use crate::expr::{Kind, Value};
use crate::frame::{self, MAX_MEMBERS, MemberProfile};
use crate::{DocError, Document, Feature, FeatureKind, ProfileSel, Result};
/// Most profiles a profile sketch may have for its material to be worked out.
const MAX_PROFILE_PIECES: usize = 256;
/// Shortest member (mm).
const MIN_LENGTH: f64 = 1e-6;
/// How far (mm) a 3D sketch curve may stray from the line between its ends and still be a line.
const STRAIGHT_TOL: f64 = 1e-3;
/// Evaluate a `FrameMember` feature: one new body per line, each named
/// ([`member_names`], or by position for a copy whose `body_names` were set).
pub(super) fn evaluate(doc: &Document, vals: &BTreeMap<String, Value>, f: &Feature, st: &mut ModelState) -> Result<()> {
let FeatureKind::FrameMember { path_sketch, lines, profile, anchor, angle, mirror, body_name, names } = &f.kind else {
return Err(DocError::Invalid("not a frame member feature".into()));
};
let ps = st.sketch(path_sketch.0).ok_or_else(|| DocError::Unknown(format!("sketch {path_sketch} (it must come earlier in the timeline)")))?;
// The frame sketch in this feature's component frame: its plane, and its 3D lines.
let mapped =
if ps.sketch.wires.is_empty() { Cow::Owned(sketch_in_feature_frame(doc, ps, f.component)?) } else { path_in_frame(doc, ps, f.component)? };
let segments = member_lines(mapped.as_ref(), lines)?;
let (mut regions, description) = profile_regions(vals, st, profile)?;
if *mirror {
regions = regions.iter().map(frame::mirrored).collect();
}
let at = frame::anchor_point(&regions, *anchor);
let turn = match angle {
Some(e) => val(vals, e, Kind::Angle)?,
None => 0.0,
};
let mut made = Vec::with_capacity(segments.len());
for (id, a, b) in &segments {
let (x, y) = frame::member_axes(*b - *a, turn).ok_or_else(|| DocError::Invalid(format!("line `{id}` has no length")))?;
let plane = Plane::new(*a - x * at.x - y * at.y, x, y).ok_or_else(|| DocError::Invalid(format!("line `{id}`: no plane square to it")))?;
let mut bodies = kernel::extrude(&plane, &regions, 0.0, a.dist(*b))?;
let body = match (bodies.pop(), bodies.is_empty()) {
(Some(body), true) => body,
_ => return Err(DocError::Invalid(format!("the member on line `{id}` is not one solid"))),
};
made.push(crate::lineage::numbered_tool(body));
}
if !f.body_names.is_empty() {
// A copy (pasted, or in a copied component) names its members by position, as any
// feature's bodies.
for (k, t) in made.into_iter().enumerate() {
let name = new_name(st, f, k);
st.bodies.push(ModelBody::traced_new(name, t, f.id));
}
return Ok(());
}
let base = body_name.as_deref().map(str::trim).filter(|n| !n.is_empty()).map_or(description, str::to_string);
let ids: Vec<String> = segments.into_iter().map(|(id, _, _)| id).collect();
for (name, t) in member_names(st, &ids, names, &base).into_iter().zip(made) {
st.bodies.push(ModelBody::traced_new(name, t, f.id));
}
Ok(())
}
/// The body name of the member on each of `lines`: its own name from `pinned` (by line id) when no
/// other body has it, else the first free one of `base`, `base (2)`, `base (3)`… that no line has
/// pinned. A member therefore keeps its name when other lines are added, deleted or reordered.
pub fn member_names(st: &ModelState, lines: &[String], pinned: &BTreeMap<String, String>, base: &str) -> Vec<String> {
let reserved: HashSet<&str> = pinned.values().map(String::as_str).collect();
let mut used: HashSet<String> = HashSet::new();
let mut out: Vec<Option<String>> = lines
.iter()
.map(|line| {
let n = pinned.get(line).map(|n| n.trim()).filter(|n| !n.is_empty() && st.body(n).is_none())?;
used.insert(n.to_string()).then(|| n.to_string())
})
.collect();
let base = if base.trim().is_empty() { "Member" } else { base.trim() };
// Each candidate is refused at most once per body, used or reserved name: past that many,
// a free one has been found for every member.
let limit = st.bodies.len() + reserved.len() + lines.len() + 2;
let mut next = 1usize;
for slot in out.iter_mut().filter(|n| n.is_none()) {
while next <= limit {
let candidate = if next == 1 { base.to_string() } else { format!("{base} ({next})") };
next += 1;
if st.body(&candidate).is_none() && !used.contains(&candidate) && !reserved.contains(candidate.as_str()) {
used.insert(candidate.clone());
*slot = Some(candidate);
break;
}
}
}
out.into_iter().map(|n| n.unwrap_or_else(|| base.to_string())).collect()
}
/// The ids of a `FrameMember` feature's lines in `st`, in the order its members are made.
pub fn member_line_ids(st: &ModelState, kind: &FeatureKind) -> Result<Vec<String>> {
let FeatureKind::FrameMember { path_sketch, lines, .. } = kind else {
return Err(DocError::Invalid("not a frame member feature".into()));
};
let ps = st.sketch(path_sketch.0).ok_or_else(|| DocError::Unknown(format!("sketch {path_sketch}")))?;
Ok(member_lines(ps, lines)?.into_iter().map(|(id, _, _)| id).collect())
}
/// The member lines of the frame sketch, each as (curve id, start, end) in its frame: the lines
/// asked for, or every straight line drawn in it (not construction, not projected or otherwise
/// linked, and with a length).
fn member_lines(ss: &SolvedSketch, ids: &[String]) -> Result<Vec<(String, Vec3, Vec3)>> {
let sk = &ss.sketch;
let mut out = Vec::new();
if ids.is_empty() {
for (ci, c) in sk.curves.iter().enumerate() {
if !c.construction
&& !c.centerline
&& c.link.is_none()
&& matches!(c.kind, CurveKind::Line { .. })
&& let Some((a, b)) = planar_line(ss, ci)
&& a.dist(b) > MIN_LENGTH
{
out.push((c.id.clone(), a, b));
}
}
for w in sk.wires.iter().filter(|w| w.link.is_none()) {
if let Some((a, b)) = straight(&w.pts)
&& a.dist(b) > MIN_LENGTH
{
out.push((w.id.clone(), a, b));
}
}
if out.is_empty() {
return Err(DocError::Invalid(format!("{} has no lines for members", ss.name)));
}
if out.len() > MAX_MEMBERS {
return Err(DocError::Invalid(format!("at most {MAX_MEMBERS} members in one feature (pick the lines)")));
}
return Ok(out);
}
if ids.len() > MAX_MEMBERS {
return Err(DocError::Invalid(format!("at most {MAX_MEMBERS} members in one feature")));
}
let mut seen = HashSet::new();
for id in ids {
if !seen.insert(id.as_str()) {
return Err(DocError::Invalid(format!("line `{id}` is listed twice")));
}
let line = if let Some(wi) = sk.wire_index(id) {
sk.wires.get(wi).and_then(|w| straight(&w.pts))
} else {
let ci = sk.curve_index(id).ok_or_else(|| DocError::Unknown(format!("line `{id}` in {}", ss.name)))?;
planar_line(ss, ci)
};
let (a, b) = line.ok_or_else(|| DocError::Invalid(format!("`{id}` is not a straight line (members run along lines)")))?;
if !(a.dist(b) > MIN_LENGTH) {
return Err(DocError::Invalid(format!("line `{id}` has no length")));
}
out.push((id.clone(), a, b));
}
Ok(out)
}
/// A sketch line's ends in the sketch's frame.
fn planar_line(ss: &SolvedSketch, ci: usize) -> Option<(Vec3, Vec3)> {
let CurveKind::Line { a, b } = ss.sketch.curves.get(ci)?.kind else { return None };
let (pa, pb) = (ss.sketch.points.get(a)?.pos, ss.sketch.points.get(b)?.pos);
Some((ss.plane.to_world(pa), ss.plane.to_world(pb)))
}
/// The ends of a 3D polyline that runs straight from its first point to its last: every point
/// within [`STRAIGHT_TOL`] of the line between them, none behind the start, past the end or back
/// from a point before it.
fn straight(pts: &[Vec3]) -> Option<(Vec3, Vec3)> {
let (a, b) = (*pts.first()?, *pts.last()?);
let (len, d) = (a.dist(b), (b - a).normalized()?);
let mut reached = 0.0_f64;
for p in pts {
let q = *p - a;
let t = q.dot(d);
if (q - d * t).len() > STRAIGHT_TOL || t < reached - STRAIGHT_TOL || t > len + STRAIGHT_TOL {
return None;
}
reached = reached.max(t);
}
Some((a, b))
}
/// The regions of a member's profile, in the profile's own coordinates: one piece of material,
/// holes and all; and what its members are named after when they have no name of their own.
fn profile_regions(vals: &BTreeMap<String, Value>, st: &ModelState, profile: &MemberProfile) -> Result<(Vec<Region2>, String)> {
let (regions, description) = match profile {
MemberProfile::Library { profile } => {
let p = profile.resolve(|name, e| val(vals, e, Kind::Length).map_err(|err| DocError::Invalid(format!("profile `{name}`: {err}"))))?;
(vec![p.region()?], p.description())
}
MemberProfile::Sketch { sketch, profiles } => {
let ss = st.sketch(sketch.0).ok_or_else(|| DocError::Unknown(format!("sketch {sketch} (it must come earlier in the timeline)")))?;
let picked = match profiles {
ProfileSel::All => material(ss)?,
sel => select_profiles(ss, sel)?,
};
(solidcraft_sketch::merge_regions(&picked), ss.name.clone())
}
};
match regions.len() {
1 => Ok((regions, description)),
0 => Err(DocError::Invalid("the member profile has no area".into())),
n => Err(DocError::Invalid(format!("the member profile is {n} separate pieces (pick one with `profiles`)"))),
}
}
/// The material of a profile sketch: of what was drawn (as `solid.extrude` takes all profiles:
/// projected reference geometry only when the drawn curves make no profile, letters without
/// their counters filled in), each profile inside an even number of other profiles' outlines,
/// so nested loops alternate between material and hole.
fn material(ss: &SolvedSketch) -> Result<Vec<Region2>> {
let drawn = solidcraft_sketch::find_drawn_profiles(&ss.sketch);
let ps = solidcraft_sketch::without_text_counters(&ss.sketch, if drawn.is_empty() { ss.profiles.clone() } else { drawn });
if ps.is_empty() {
return Err(DocError::Invalid(format!("{} has no closed profiles", ss.name)));
}
if ps.len() > MAX_PROFILE_PIECES {
return Err(DocError::Invalid(format!("{} has more than {MAX_PROFILE_PIECES} profiles (pick the member's with `profiles`)", ss.name)));
}
// Outer polylines and their boxes, once.
let polys: Vec<Ring<Vec2>> = ps.iter().map(|p| p.region.outer.polyline(1e-3)).collect();
let boxes: Vec<Aabb2> = polys.iter().map(|poly| poly.bounds()).collect();
Ok(ps
.iter()
.enumerate()
.filter(|(i, p)| {
let at = p.region.interior_point();
let depth = polys.iter().zip(&boxes).enumerate().filter(|(j, (poly, bb))| j != i && bb.contains(at) && poly.contains(at)).count();
depth % 2 == 0
})
.map(|(_, p)| p.region.clone())
.collect())
}