Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

smlib and kernel: solidcore's refusal reasons carried through as typed errors

jyvikoae45cc662341
diff --git a/crates/kernel/src/blend_smlib.rs b/crates/kernel/src/blend_smlib.rs
index 272b1666be..4e4088a88c 100644
--- a/crates/kernel/src/blend_smlib.rs
+++ b/crates/kernel/src/blend_smlib.rs
@@ -445,11 +445,14 @@ mod imp {
run on tangentially into each other, or meet in a way no blend corner builds)"
)),
SmError::NoRoom => KernelError::TooLarge(format!("the {what} is larger than the faces beside its edges allow")),
- SmError::TooLarge { size, kind, at } => {
- let beside = match at {
+ SmError::TooLarge { size, kind, at, why } => {
+ let mut beside = match at {
Some([x, y, z]) => format!(", at the edge at ({x:.3}, {y:.3}, {z:.3})"),
None => String::new(),
};
+ if let Some(why) = why {
+ beside = format!("{beside}: {}", why.words());
+ }
let fits = match kind {
solidcraft_smlib::FitKind::Scale => {
format!("the largest that fits is {} times the distances given", crate::blend::trimmed_mm(size))
diff --git a/crates/kernel/src/offset_backend.rs b/crates/kernel/src/offset_backend.rs
index bf88f4ed2b..4fb551cda1 100644
--- a/crates/kernel/src/offset_backend.rs
+++ b/crates/kernel/src/offset_backend.rs
@@ -39,6 +39,11 @@ fn refused(op: &str, wall: Option<f64>, e: SmError) -> KernelError {
use a thinner or thicker wall, or move the feature"
))
}
+ // solidcore's one construction: a face curves tighter than the wall is thick.
+ SmError::WallFolds { largest } => {
+ let largest = largest.map(|w| format!("; the largest wall that works is {} mm", crate::blend::trimmed_mm(w))).unwrap_or_default();
+ KernelError::Invalid(format!("{op}: the wall is thicker than a face curves, so the face's offset would fold{largest}"))
+ }
SmError::NoMatch { point } => KernelError::Invalid(format!("{op}: no face at selection point {}", point + 1)),
SmError::Ambiguous { point } => KernelError::Invalid(format!("{op}: selection point {} is as near to two faces", point + 1)),
SmError::Unsupported => KernelError::Failed(format!("not supported yet: SMLib {op} for this geometry")),
diff --git a/crates/smlib/src/core/blend.rs b/crates/smlib/src/core/blend.rs
index 166ddcda53..8bd296b834 100644
--- a/crates/smlib/src/core/blend.rs
+++ b/crates/smlib/src/core/blend.rs
@@ -290,10 +290,10 @@ fn face_set(b: &Brep) -> solidcore::Result<HashSet<FaceId>> {
/// [`Error::Unported`] naming it.
fn blend_failure(s: &FilletSession<'_>, e: SolidError, corner: bool) -> (Error, Option<EdgeId>) {
let info = s.error_info();
- let fit = match &e {
- SolidError::FilletRefused { fit, .. } => Some(*fit),
- _ if info.code == FilletErrorType::NoRoom as i32 => s.fit_size(),
- _ => None,
+ let (fit, reason) = match &e {
+ SolidError::FilletRefused { fit, reason, .. } => (Some(*fit), reason.or(info.no_room_reason())),
+ _ if info.code == FilletErrorType::NoRoom as i32 => (s.fit_size(), info.no_room_reason()),
+ _ => (None, None),
};
if e.is_unported() || matches!(e, SolidError::Unsupported { reason, .. } if reason == MIXED_CORNER_RAILS_CROSS) {
(status(e), None)
@@ -308,12 +308,36 @@ fn blend_failure(s: &FilletSession<'_>, e: SolidError, corner: bool) -> (Error,
FitKind::Scale => crate::FitKind::Scale,
FitKind::Chord => crate::FitKind::Chord,
};
- (Error::TooLarge { size: fit.size, kind, at: None }, fit.edge)
+ (Error::TooLarge { size: fit.size, kind, at: None, why: reason.and_then(room_reason) }, fit.edge)
} else {
(blend_failure_unsized(info, e, corner), None)
}
}
+/// solidcore's reason for a blend too large, as SolidCraft words it; `None` for the reasons it
+/// does not name (the cliff overflow's own refusals).
+fn room_reason(r: NoRoomReason) -> Option<crate::RoomReason> {
+ use crate::RoomReason as R;
+ Some(match r {
+ NoRoomReason::BallDoesNotFit => R::BallDoesNotFit,
+ NoRoomReason::RailLeavesFace
+ | NoRoomReason::ConcaveOverflow
+ | NoRoomReason::OverflowWithConcaveBlend
+ | NoRoomReason::OverflowWithRollover
+ | NoRoomReason::OverflowWithGlobalMerge => R::RailLeavesFace,
+ NoRoomReason::FaceConsumed => R::FaceConsumed,
+ NoRoomReason::BandsOverlap => R::BandsOverlap,
+ NoRoomReason::BallsFold => R::BallsFold,
+ NoRoomReason::OffsetFolds => R::OffsetFolds,
+ NoRoomReason::CutsThePart => R::CutsThePart,
+ NoRoomReason::BreaksThrough => R::BreaksThrough,
+ NoRoomReason::OffsetCollapses => R::OffsetCollapses,
+ NoRoomReason::CornersTakeWholeEdge => R::CornersTakeWholeEdge,
+ NoRoomReason::RoundIntoMaterial | NoRoomReason::RailRunsPastFaceIntoMaterial => R::RunsIntoMaterial,
+ _ => return None,
+ })
+}
+
/// [`blend_failure`] where solidcore gives no size.
fn blend_failure_unsized(info: &ErrorInfo<FilletObject>, e: SolidError, corner: bool) -> Error {
if e.is_unported() {
@@ -334,7 +358,7 @@ fn blend_failure_unsized(info: &ErrorInfo<FilletObject>, e: SolidError, corner:
/// Where the edge or its point can't be read the size stays as it is, with no point (a chamfer's
/// size then is not a distance, so it is left a plain refusal).
fn sized_on(k: &Kernel, b: &mut Brep, edge: Option<EdgeId>, section: Section<'_>, e: Error) -> solidcore::Result<Error> {
- let Error::TooLarge { size, kind, .. } = e else { return Ok(e) };
+ let Error::TooLarge { size, kind, why, .. } = e else { return Ok(e) };
let Some(edge) = edge else {
return Ok(if matches!(section, Section::Chamfer(_)) && kind == crate::FitKind::Radius { Error::NoRoom } else { e });
};
@@ -348,11 +372,11 @@ fn sized_on(k: &Kernel, b: &mut Brep, edge: Option<EdgeId>, section: Section<'_>
if matches!(section, Section::Chamfer(_)) && kind == crate::FitKind::Radius {
// The ball's radius is linear in the chamfer's distance along an edge.
return Ok(match chamfer_radius(k, b, edge, 1.0)? {
- Some(per_mm) => Error::TooLarge { size: size / per_mm, kind: crate::FitKind::Distance, at },
+ Some(per_mm) => Error::TooLarge { size: size / per_mm, kind: crate::FitKind::Distance, at, why },
None => Error::NoRoom,
});
}
- Ok(Error::TooLarge { size, kind, at })
+ Ok(Error::TooLarge { size, kind, at, why })
}
/// `round_edges`' `overflow_first` argument: with it the session stops, nothing built, when blends
diff --git a/crates/smlib/src/core/blend_tests.rs b/crates/smlib/src/core/blend_tests.rs
index 786e1c91ef..81707d2afd 100644
--- a/crates/smlib/src/core/blend_tests.rs
+++ b/crates/smlib/src/core/blend_tests.rs
@@ -342,14 +342,14 @@ fn a_blend_too_large_is_refused_with_the_largest_size_that_fits() {
let uprights = [[14.0, 0.0, 5.0], [28.0, 0.0, 5.0], [14.0, 6.0, 5.0], [28.0, 6.0, 5.0]];
for r in [3.001, 3.5] {
let e = timed("fit_199", move || blend(&bar.core(), Blend::Round, &uprights, &[r], TOL, &[], &[], Corners::Filled).map(|_| ()).unwrap_err());
- let Error::TooLarge { size, kind, at: Some(at) } = e else { panic!("{r}: {e:?}") };
+ let Error::TooLarge { size, kind, at: Some(at), .. } = e else { panic!("{r}: {e:?}") };
assert!((size - 3.0).abs() < 1e-9 && kind == crate::FitKind::Radius, "{r}: {size} {kind:?}");
assert!(uprights.iter().any(|p| (0..3).all(|i| (p[i] - at[i]).abs() < 1e-9)), "{r}: {at:?}");
}
let rib = Shape::Box([0.0; 3], [40.0, 4.0, 20.0]);
let top = [[20.0, 0.0, 20.0], [20.0, 4.0, 20.0]];
let e = timed("fit_1080", move || blend(&rib.core(), Blend::Chamfer, &top, &[3.0], TOL, &[], &[], Corners::Filled).map(|_| ()).unwrap_err());
- let Error::TooLarge { size, kind, at: Some(_) } = e else { panic!("{e:?}") };
+ let Error::TooLarge { size, kind, at: Some(_), .. } = e else { panic!("{e:?}") };
assert!((size - 2.0).abs() < 1e-12 && kind == crate::FitKind::Distance, "{size} {kind:?}");
}
@@ -363,7 +363,7 @@ fn a_cliff_overflow_the_fork_refuses_has_no_room() {
let r = timed("cliff_refused", move || {
blend(&Shape::Box([0.0; 3], [20.0; 3]).core(), Blend::Round, &corner, &[20.0; 3], TOL, &[], &[], Corners::Filled).map(|_| ()).unwrap_err()
});
- let Error::TooLarge { size, kind: crate::FitKind::Radius, at: Some(_) } = r else { panic!("{r:?}") };
+ let Error::TooLarge { size, kind: crate::FitKind::Radius, at: Some(_), .. } = r else { panic!("{r:?}") };
assert!((size - 19.99).abs() < 1e-9, "{size}");
timed("cliff_fit", move || {
blend(&Shape::Box([0.0; 3], [20.0; 3]).core(), Blend::Round, &corner, &[size; 3], TOL, &[], &[], Corners::Filled).unwrap()
diff --git a/crates/smlib/src/core/offset.rs b/crates/smlib/src/core/offset.rs
index f07d3bcfa4..8c09cc6e11 100644
--- a/crates/smlib/src/core/offset.rs
+++ b/crates/smlib/src/core/offset.rs
@@ -18,6 +18,7 @@
use solidcore::EntityRef;
use solidcore::SolidError;
+use solidcore::offset::construction::ShellRefusal;
use solidcore::geom::attribute::{Attribute, Behavior, ids};
use solidcore::geom::axis2_placement::Axis2Placement;
use solidcore::geom::object::Shape;
@@ -55,10 +56,13 @@ const OFFSET_WALLS_TOUCH: i32 = 1042;
/// `status` (smlib_shim.h): an offset whose walls touch is [`Error::WallsTouch`]; a vertex moved
/// past an edge's other end (`SM_ERR_OFFSET_EDGE_VANISHES`, fork 34318f4) is
-/// `SMLIB_ERR_UNSUPPORTED`; any other status as the blends pass it on.
+/// `SMLIB_ERR_UNSUPPORTED`; a wall whose face's offset folds, which solidcore's one construction
+/// refuses (`fixes.shell_one_construction`, solidcore#2367), is [`Error::WallFolds`] with the
+/// largest wall it names; any other status as the blends pass it on.
fn status(e: SolidError) -> Error {
match e {
SolidError::Upstream { code: OFFSET_WALLS_TOUCH, .. } => Error::WallsTouch,
+ SolidError::ShellRefused { refusal: ShellRefusal::Fold, size, .. } => Error::WallFolds { largest: size },
e if e.is_offset_edge_vanishes() => Error::Unsupported,
e => blend_status(e),
}
diff --git a/crates/smlib/src/lib.rs b/crates/smlib/src/lib.rs
index 19103d6bbd..4da013bc59 100644
--- a/crates/smlib/src/lib.rs
+++ b/crates/smlib/src/lib.rs
@@ -49,9 +49,13 @@ pub enum Error {
/// nothing along a line. A thinner or a thicker wall builds.
WallsTouch,
/// A blend larger than its faces allow, refused with the largest size that builds it
- /// (solidcore's fit, `SolidError::FilletRefused`): the size, what it sizes, and a point at the
- /// middle of the edge it was found for, when the refusal names one.
- TooLarge { size: f64, kind: FitKind, at: Option<[f64; 3]> },
+ /// (solidcore's fit, `SolidError::FilletRefused`): the size, what it sizes, a point at the
+ /// middle of the edge it was found for, and why there is no room, when the refusal names them.
+ TooLarge { size: f64, kind: FitKind, at: Option<[f64; 3]>, why: Option<RoomReason> },
+ /// A shell, offset or thicken whose wall is thicker than a face curves: the face's offset
+ /// folds (solidcore's `ShellRefused`, `ShellRefusal::Fold`). The largest wall that works,
+ /// when solidcore names one.
+ WallFolds { largest: Option<f64> },
/// SMLib cannot build this exactly yet (a variable radius whose law has a corner).
Unsupported,
/// A blend's edges meet at a vertex of a kind no SMLib blend corner handles yet.
@@ -81,6 +85,56 @@ pub enum Error {
Unported(&'static str),
}
+/// Why a blend has no room, as solidcore names its refusal (`NoRoomReason`): the reasons a
+/// refusal with a size gives ([`Error::TooLarge`]).
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum RoomReason {
+ /// The rolling ball does not fit a face it rolls on.
+ BallDoesNotFit,
+ /// A contact line leaves the face it rolls on.
+ RailLeavesFace,
+ /// The blends take the whole of a face they roll on.
+ FaceConsumed,
+ /// One blend's contact line runs inside the band another takes off a face they share.
+ BandsOverlap,
+ /// The balls fold over one another along the edge.
+ BallsFold,
+ /// A face curves tighter than a chamfer's offset along it.
+ OffsetFolds,
+ /// The blend would take all of the part between its contact lines and cut it in pieces.
+ CutsThePart,
+ /// The blend's section would break through a wall into the hollow behind it.
+ BreaksThrough,
+ /// The ball is as wide as a face it rolls on (a cylinder or sphere of its radius), so its
+ /// centre stays put and no round ends at an end of the edge.
+ OffsetCollapses,
+ /// The blend's corners take the whole of its edge.
+ CornersTakeWholeEdge,
+ /// A blend past its faces runs off a face into the material.
+ RunsIntoMaterial,
+}
+
+impl RoomReason {
+ /// The reason in words, as a fillet's message gives it.
+ pub fn words(self) -> &'static str {
+ match self {
+ RoomReason::BallDoesNotFit => "the rolling ball does not fit a face it rolls on",
+ RoomReason::RailLeavesFace => "a contact line leaves the face it rolls on",
+ RoomReason::FaceConsumed => "the blends take the whole of a face they roll on",
+ RoomReason::BandsOverlap => "one blend runs inside the band another takes off a face they share",
+ RoomReason::BallsFold => "the rolling ball folds over itself along the edge",
+ RoomReason::OffsetFolds => "a face curves tighter than the offset along it",
+ RoomReason::CutsThePart => "the blend would cut the part in pieces",
+ RoomReason::BreaksThrough => "the blend would break through a wall into the hollow behind it",
+ RoomReason::OffsetCollapses => {
+ "the ball is as wide as a face it rolls on, so its centre stays put and the round cannot end at the edge's end"
+ }
+ RoomReason::CornersTakeWholeEdge => "its corners take the whole of the edge",
+ RoomReason::RunsIntoMaterial => "the blend runs off a face into the material",
+ }
+ }
+}
+
/// What the largest size that builds a refused blend ([`Error::TooLarge`]) sizes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FitKind {
@@ -118,13 +172,23 @@ impl std::fmt::Display for Error {
Error::Ambiguous { point } => write!(f, "selection point {} is equally near to two edges or faces", point + 1),
Error::NoRoom => write!(f, "there is no room for it: an offset leaves none inside the body, or a blend is larger than its faces allow"),
Error::WallsTouch => write!(f, "the offset's walls would meet along a line, which leaves no solid there"),
- Error::TooLarge { size, kind, at } => {
+ Error::TooLarge { size, kind, at, why } => {
write!(f, "the blend is larger than its faces allow")?;
if let Some([x, y, z]) = at {
write!(f, " at the edge at ({x:.3}, {y:.3}, {z:.3})")?;
}
+ if let Some(why) = why {
+ write!(f, ": {}", why.words())?;
+ }
write!(f, "; the largest {} that fits is {size}", kind.word())
}
+ Error::WallFolds { largest } => {
+ write!(f, "the wall is thicker than a face curves, so the face's offset folds")?;
+ match largest {
+ Some(w) => write!(f, "; the largest wall that works is {w}"),
+ None => Ok(()),
+ }
+ }
Error::Unsupported => write!(f, "not supported yet: SMLib cannot build this exactly"),
Error::CornerUnsupported => write!(f, "not supported yet: a corner of this kind is not supported yet by SMLib's blends"),
Error::CornerRefused { corner } => write!(