Merge pull request #1642 from ramox81/codex/sweep-corner-followups

This commit is contained in:
Hakan Seven committed 2026-10-07 12:55:08 +03:00
commit 737981791b
29 files changed
+482 -108

No files matched your search

+26 -19
View File
@@ -497,22 +497,34 @@ impl OpenCADStudio {
let mut created_handles = Vec::new();
let mut consumed = Vec::new();
for (handle, profile) in profiles {
let result = path.as_ref().zip(options).and_then(|(path, options)| {
let mut record = sweep_model::sweep_record(&profile, path, options)?;
// A refused sweep reports the modeler error code, as the
// reference does.
let result = (|| -> Result<_, Option<u32>> {
let (path, options) = path.as_ref().zip(options).ok_or(None)?;
let mut record = sweep_model::sweep_record(&profile, path, options).ok_or(None)?;
record.flags_294_296[0] |= picked_base;
let (_, _, closed) = kernel::acis::sweep_profile_geometry(
record.sweep_entity.as_ref()?,
record.sweep_entity.as_ref().ok_or(None)?,
record.sweep_entity_transform,
)
.ok()?;
.map_err(|_| None)?;
let surface = mode == ExtrudeMode::Surface || !closed;
let body =
kernel::acis::rebuild_sweep_with_mode(&record, surface).ok()?;
Some((body, record, surface))
});
let Some((body, record, surface)) = result else {
failed += 1;
continue;
let body = kernel::acis::rebuild_sweep_with_mode(&record, surface).map_err(|error| match error {
kernel::acis::HistoryRebuildError::Refused(why) => Some(sweep_model::sweep_refusal_code(why)),
_ => None,
})?;
Ok((body, record, surface))
})();
let (body, record, surface) = match result {
Ok(value) => value,
Err(code) => {
if let Some(code) = code {
self.command_line.push_output(crate::t!("Modeling Operation Error:").as_ref());
self.command_line.push_output(crate::tf!("Error Code Number is {code}", code = code).as_ref());
}
failed += 1;
continue;
}
};
let deletes_path =
crate::app::delobj_deletes_auxiliary(delete_objects, surface);
@@ -550,14 +562,9 @@ impl OpenCADStudio {
self.tabs[i].scene.select_entity(*handle, true);
}
self.tabs[i].dirty = true;
self.command_line.push_output(crate::tf!(
"SWEEP: created {created} object(s); {failed} source(s) could not be swept.",
created = created_handles.len(), failed = failed
).as_ref());
} else {
self.command_line.push_error(
crate::t!("SWEEP: could not sweep the profile along the path.").as_ref(),
);
}
if failed > 0 {
self.command_line.push_output(crate::tf!("Unable to sweep {failed} selected objects.", failed = failed).as_ref());
}
if let Some(pending) = pending {
self.commit_undo_delta(i, pending);
+1
View File
@@ -817,6 +817,7 @@ impl OpenCADStudio {
let color = self.tabs[i].scene.layer_color(&self.tabs[i].active_layer);
let isolines = crate::entities::solid3d::clamp_header_isolines(self.tabs[i].scene.document.header.isolines);
let mut cmd = SweepCommand::new(color, isolines);
cmd.set_parameters(self.tabs[i].scene.named_parameters().clone());
let selected = self.tabs[i].scene.selected_handles_in_order()
.into_iter()
.filter_map(|handle| self.tabs[i].scene.document.get_entity(handle)
+12 -2
View File
@@ -3951,11 +3951,21 @@ pub(super) fn on_tab_close(&mut self, idx: usize) -> Task<Message> {
if crate::scene::model::solid_history::is_primitive_property(
field,
) {
self.tabs[i].scene.apply_solid_history_property(
if !self.tabs[i].scene.apply_solid_history_property(
handle,
field,
&val,
);
) {
if let Some(code) = self.tabs[i]
.scene
.solid_history_property_refusal(handle, field, &val)
{
self.command_line.push_output(crate::t!("Modeling Operation Error:").as_ref());
self.command_line.push_output(
crate::tf!("Error Code Number is {code}", code = code).as_ref(),
);
}
}
} else if self.tabs[i]
.scene
.apply_solid_position_property(
+13 -2
View File
@@ -4463,13 +4463,24 @@ pub(super) fn message_attribute(source: &str) -> Option<(&'static str, &'static
"SWEEP Select sweep path or [Alignment/Base point/Scale/Twist]:" => Some(("insert", "sweep-select-sweep-path-or-alignment-base-point-scale-twist")),
"SWEEP Align sweep object perpendicular to path [Yes/No]" => Some(("insert", "sweep-align-sweep-object-perpendicular-to-path-yes-no")),
"SWEEP Specify base point:" => Some(("insert", "sweep-specify-base-point")),
"SWEEP Enter scale factor or [Reference]" => Some(("insert", "sweep-enter-scale-factor-or-reference")),
"SWEEP Enter scale factor or [Reference/Expression]" => Some(("insert", "sweep-enter-scale-factor-or-reference")),
"SWEEP Specify reference length or first point" => Some(("insert", "sweep-specify-reference-length-or-first-point")),
"SWEEP Specify second reference point:" => Some(("insert", "sweep-specify-second-reference-point")),
"SWEEP Specify new length or [Points]" => Some(("insert", "sweep-specify-new-length-or-points")),
"SWEEP Specify first point of new length:" => Some(("insert", "sweep-specify-first-point-of-new-length")),
"SWEEP Specify second point of new length:" => Some(("insert", "sweep-specify-second-point-of-new-length")),
"SWEEP Specify twist angle or [Bank]" => Some(("insert", "sweep-specify-twist-angle-or-bank")),
"SWEEP Enter twist angle or allow banking for a non-planar sweep path [Bank/EXpression]" => Some(("insert", "sweep-specify-twist-angle-or-bank")),
"SWEEP Enter expression:" => Some(("insert", "sweep-enter-expression")),
"SWEEP Specify start reference length" => Some(("insert", "sweep-specify-start-reference-length")),
"SWEEP Specify end reference length" => Some(("insert", "sweep-specify-end-reference-length")),
"SWEEP Specify second point:" => Some(("insert", "sweep-specify-second-point")),
"The syntax is incorrect or a variable is not defined. Reenter the expression or value." => Some(("insert", "sweep-expression-invalid")),
"Modeling Operation Error:" => Some(("insert", "sweep-modeling-operation-error")),
"Error Code Number is {code}" => Some(("insert", "sweep-error-code-number")),
"Unable to sweep {failed} selected objects." => Some(("insert", "sweep-unable-to-sweep-selected-objects")),
"Cannot use scale option when path curve is closed." => Some(("insert", "sweep-scale-closed-path")),
"Path curve must be smooth when using scale option." => Some(("insert", "sweep-scale-smooth-path")),
"The selected path curve is not valid." => Some(("insert", "sweep-path-curve-not-valid")),
"Point cross-sections are allowed only at the first or last end, with a curve between point ends." => Some(("insert", "point-cross-sections-are-allowed-only-at-the-first-or-last-end-with-a-curve-between-p")),
"LOFT requires at least two cross-sections." => Some(("insert", "loft-requires-at-least-two-cross-sections")),
"A closed loft requires at least three curve cross-sections and no point ends." => Some(("insert", "a-closed-loft-requires-at-least-three-curve-cross-sections-and-no-point-ends")),
+93 -35
View File
@@ -1606,6 +1606,10 @@ impl CadCommand for RevolveCommand {
// ── SWEEP command ──────────────────────────────────────────────────────────
/// Banking stays on for later sweeps until a twist angle is entered, as in
/// the reference.
static SWEEP_BANK: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
pub struct SweepCommand {
step: SweepStep,
profiles: Vec<(Handle, EntityType)>,
@@ -1618,6 +1622,9 @@ pub struct SweepCommand {
reference_start: Option<DVec3>,
reference_length: f64,
new_length_start: Option<DVec3>,
end_length: f64,
/// The drawing's named parameters, for Expression input.
parameters: crate::scene::named_parameters::ParameterTable,
isolines: usize,
color: [f32; 4],
}
@@ -1630,12 +1637,15 @@ enum SweepStep {
Alignment,
BasePoint,
Scale,
/// The start reference length: a distance, or two points.
ReferenceLength,
ReferenceEnd,
/// The end reference length: a distance, or two points.
NewLength,
NewLengthStart,
NewLengthEnd,
Twist,
ScaleExpression,
TwistExpression,
}
impl SweepCommand {
@@ -1648,15 +1658,31 @@ impl SweepCommand {
preview_key: None,
preview_cache: Vec::new(),
mode: ExtrudeMode::Solid,
options: SweepOptions::default(),
options: SweepOptions {
bank: SWEEP_BANK.load(std::sync::atomic::Ordering::Relaxed),
..SweepOptions::default()
},
reference_start: None,
reference_length: 1.0,
new_length_start: None,
end_length: 1.0,
parameters: Default::default(),
isolines,
color,
}
}
pub fn set_parameters(&mut self, parameters: crate::scene::named_parameters::ParameterTable) {
self.parameters = parameters;
}
/// An Expression input, evaluated against the drawing's parameters.
fn evaluate(&self, text: &str) -> Option<f64> {
let mut table = self.parameters.clone();
table.set("sweepExpression", text).ok()?;
table.resolve("sweepExpression").ok().filter(|value| value.is_finite())
}
pub fn set_preselection(&mut self, profiles: Vec<(Handle, EntityType)>) {
self.set_profiles(profiles);
if !self.profiles.is_empty() {
@@ -1715,10 +1741,13 @@ impl SweepCommand {
true
}
fn length_anchor(&self) -> DVec3 {
self.reference_start
.or_else(|| self.selection_options().and_then(|options| options.base_point))
.unwrap_or(DVec3::ZERO)
/// The Reference scale is the start reference length over the end one.
fn set_end_length(&mut self, length: f64) -> bool {
if !length.is_finite() || length <= 1e-12 {
return false;
}
self.end_length = length;
self.set_scale(self.reference_length / length)
}
}
@@ -1742,25 +1771,28 @@ impl CadCommand for SweepCommand {
),
SweepStep::BasePoint => t!("SWEEP Specify base point:").into_owned(),
SweepStep::Scale => format!(
"{} <{}>:", t!("SWEEP Enter scale factor or [Reference]"), self.options.scale,
"{}<{:.4}>:", t!("SWEEP Enter scale factor or [Reference/Expression]"), self.options.scale,
),
SweepStep::ScaleExpression | SweepStep::TwistExpression => t!("SWEEP Enter expression:").into_owned(),
SweepStep::ReferenceLength => format!(
"{} <{}>:",
t!("SWEEP Specify reference length or first point"),
t!("SWEEP Specify start reference length"),
crate::entities::common::format_length(self.reference_length),
),
SweepStep::ReferenceEnd => t!("SWEEP Specify second reference point:").into_owned(),
SweepStep::ReferenceEnd | SweepStep::NewLengthEnd => t!("SWEEP Specify second point:").into_owned(),
SweepStep::NewLength => format!(
"{} <{}>:",
t!("SWEEP Specify new length or [Points]"),
crate::entities::common::format_length(self.reference_length * self.options.scale),
t!("SWEEP Specify end reference length"),
crate::entities::common::format_length(self.end_length),
),
SweepStep::NewLengthStart => t!("SWEEP Specify first point of new length:").into_owned(),
SweepStep::NewLengthEnd => t!("SWEEP Specify second point of new length:").into_owned(),
SweepStep::Twist => format!(
"{} <{}>:",
t!("SWEEP Specify twist angle or [Bank]"),
crate::entities::common::format_angle(self.options.twist_angle),
"{}<{}>:",
t!("SWEEP Enter twist angle or allow banking for a non-planar sweep path [Bank/EXpression]"),
if self.options.bank {
"Bank".to_string()
} else {
format!("{:.4}", self.options.twist_angle.to_degrees())
},
),
}
}
@@ -1776,9 +1808,8 @@ impl CadCommand for SweepCommand {
CmdOption::new("Twist", "TWIST"),
],
SweepStep::Alignment => vec![CmdOption::new("Yes", "YES"), CmdOption::new("No", "NO")],
SweepStep::Scale => vec![CmdOption::new("Reference", "REFERENCE")],
SweepStep::NewLength => vec![CmdOption::new("Points", "POINTS")],
SweepStep::Twist => vec![CmdOption::new("Bank", "BANK")],
SweepStep::Scale => vec![CmdOption::new("Reference", "REFERENCE"), CmdOption::new("Expression", "EXPRESSION")],
SweepStep::Twist => vec![CmdOption::new("Bank", "BANK"), CmdOption::new("EXpression", "EXPRESSION")],
_ => Vec::new(),
}
}
@@ -1801,11 +1832,18 @@ impl CadCommand for SweepCommand {
.filter(|(hovered, _)| *hovered == handle)
.map(|(_, entity)| entity.clone())
});
if path.as_ref().is_some_and(crate::scene::model::sweep_model::is_sweep_path) {
self.finish(handle)
} else {
CmdResult::NeedPoint
let Some(path) = path.filter(crate::scene::model::sweep_model::is_sweep_path) else {
return CmdResult::NeedPoint;
};
if (self.options.scale - 1.0).abs() > 1e-12 {
if let Some(message) = crate::scene::model::sweep_model::scaled_sweep_path_refusal(&path) {
return CmdResult::ReportMeasurement(format!(
"{}
{}", t!(message), t!("The selected path curve is not valid."),
));
}
}
self.finish(handle)
}
fn on_point(&mut self, point: DVec3) -> CmdResult {
@@ -1827,15 +1865,12 @@ impl CadCommand for SweepCommand {
}
}
SweepStep::NewLength => {
self.set_scale(point.distance(self.length_anchor()) / self.reference_length);
}
SweepStep::NewLengthStart => {
self.new_length_start = Some(point);
self.step = SweepStep::NewLengthEnd;
}
SweepStep::NewLengthEnd => {
if let Some(start) = self.new_length_start {
self.set_scale(point.distance(start) / self.reference_length);
self.set_end_length(point.distance(start));
}
}
_ => {}
@@ -1848,6 +1883,7 @@ impl CadCommand for SweepCommand {
SweepStep::PickProfiles | SweepStep::Mode | SweepStep::PickPath
| SweepStep::Alignment | SweepStep::Scale | SweepStep::ReferenceLength
| SweepStep::NewLength | SweepStep::Twist
| SweepStep::ScaleExpression | SweepStep::TwistExpression
)
}
@@ -1900,7 +1936,9 @@ impl CadCommand for SweepCommand {
self.step = SweepStep::PickPath;
}
SweepStep::Scale => {
if "REFERENCE".starts_with(&keyword) {
if "EXPRESSION".starts_with(&keyword) {
self.step = SweepStep::ScaleExpression;
} else if "REFERENCE".starts_with(&keyword) {
self.reference_start = None;
self.new_length_start = None;
self.step = SweepStep::ReferenceLength;
@@ -1916,22 +1954,39 @@ impl CadCommand for SweepCommand {
}
}
SweepStep::NewLength => {
if "POINTS".starts_with(&keyword) {
self.step = SweepStep::NewLengthStart;
} else if let Some(length) = crate::entities::common::parse_typed_length(value) {
self.set_scale(length / self.reference_length);
if let Some(length) = crate::entities::common::parse_typed_length(value) {
self.set_end_length(length);
} else {
return None;
}
}
SweepStep::ScaleExpression | SweepStep::TwistExpression => {
let Some(value) = self.evaluate(value) else {
return Some(CmdResult::ReportMeasurement(t!(
"The syntax is incorrect or a variable is not defined. Reenter the expression or value."
).into_owned()));
};
if self.step == SweepStep::ScaleExpression {
self.set_scale(value);
} else {
self.options.twist_angle = value.to_radians();
self.options.bank = false;
SWEEP_BANK.store(false, std::sync::atomic::Ordering::Relaxed);
}
self.step = SweepStep::PickPath;
}
SweepStep::Twist => {
if "BANK".starts_with(&keyword) {
if keyword.len() >= 2 && "EXPRESSION".starts_with(&keyword) {
self.step = SweepStep::TwistExpression;
} else if "BANK".starts_with(&keyword) {
self.options.bank = true;
SWEEP_BANK.store(true, std::sync::atomic::Ordering::Relaxed);
self.step = SweepStep::PickPath;
} else if let Some(angle) = crate::entities::common::parse_angle(value) {
if angle.is_finite() {
self.options.twist_angle = angle;
self.options.bank = false;
SWEEP_BANK.store(false, std::sync::atomic::Ordering::Relaxed);
self.step = SweepStep::PickPath;
}
}
@@ -1947,9 +2002,12 @@ impl CadCommand for SweepCommand {
self.step = SweepStep::PickPath;
}
SweepStep::Mode => self.step = SweepStep::PickProfiles,
SweepStep::Alignment | SweepStep::Scale | SweepStep::Twist
| SweepStep::NewLength => self.step = SweepStep::PickPath,
SweepStep::Alignment | SweepStep::Scale | SweepStep::Twist => self.step = SweepStep::PickPath,
SweepStep::ScaleExpression | SweepStep::TwistExpression => {}
SweepStep::ReferenceLength => self.step = SweepStep::NewLength,
SweepStep::NewLength => {
self.set_end_length(self.end_length);
}
_ => return CmdResult::Cancel,
}
CmdResult::NeedPoint
+50 -7
View File
@@ -9,20 +9,25 @@ use kernel::brep::Body;
use crate::command::{ExtrudeMode, SweepOptions};
use super::sweep_model::{embedded_path, embedded_revolve_profile};
/// A 3D polyline profile or path as the reference records it: a wire body.
fn embedded_sweep_profile(entity: &EntityType) -> Option<(codec::entities::EmbeddedEntity, [f64; 16])> {
if let EntityType::Region(region) = entity {
Some((codec::entities::EmbeddedEntity::Region(region.clone()), glam::DMat4::IDENTITY.to_cols_array()))
} else {
embedded_revolve_profile(entity)
match entity {
EntityType::Region(region) => Some((codec::entities::EmbeddedEntity::Region(region.clone()), glam::DMat4::IDENTITY.to_cols_array())),
EntityType::Polyline3D(value) => Some((polyline_wire(value)?, glam::DMat4::IDENTITY.to_cols_array())),
_ => embedded_revolve_profile(entity),
}
}
/// The sweep path as the reference records it: a 3D polyline as a wire
/// body, anything else as its embedded entity.
fn embedded_sweep_path(path: &EntityType) -> Option<codec::entities::EmbeddedEntity> {
let EntityType::Polyline3D(value) = path else {
return embedded_path(path);
};
match path {
EntityType::Polyline3D(value) => polyline_wire(value),
_ => embedded_path(path),
}
}
fn polyline_wire(value: &codec::entities::Polyline3D) -> Option<codec::entities::EmbeddedEntity> {
let points = value.vertices.iter()
.map(|vertex| [vertex.position.x, vertex.position.y, vertex.position.z])
.collect::<Vec<_>>();
@@ -41,6 +46,44 @@ pub fn is_sweep_profile(entity: &EntityType) -> bool {
})
}
/// The modeling error code the reference reports for a refused sweep.
pub fn sweep_refusal_code(refusal: kernel::brep::SweepRefusal) -> u32 {
match refusal {
kernel::brep::SweepRefusal::Scale => 5016,
kernel::brep::SweepRefusal::Twist => 115065,
kernel::brep::SweepRefusal::Bank => 115007,
}
}
/// Why the reference refuses a path for a scaled sweep: scaling needs an
/// open path, and a spatial path must be smooth.
pub fn scaled_sweep_path_refusal(path: &EntityType) -> Option<&'static str> {
let (closed, smooth) = match crate::entities::curve::entity_curve(path) {
Some(planar) => (planar.curve.is_closed(), true),
None => match path {
EntityType::Polyline3D(value) => {
let points = value.vertices.iter()
.map(|vertex| glam::DVec3::new(vertex.position.x, vertex.position.y, vertex.position.z))
.collect::<Vec<_>>();
let directions = points.windows(2)
.filter_map(|pair| (pair[1] - pair[0]).try_normalize())
.collect::<Vec<_>>();
let straight = directions.windows(2).all(|pair| pair[0].dot(pair[1]) >= 1.0 - 1e-9);
(value.is_closed(), straight && !value.is_closed())
}
EntityType::Spline(value) => (value.flags.closed, value.degree > 1),
_ => (false, true),
},
};
if closed {
Some("Cannot use scale option when path curve is closed.")
} else if !smooth {
Some("Path curve must be smooth when using scale option.")
} else {
None
}
}
pub fn is_sweep_path(entity: &EntityType) -> bool {
match embedded_path(entity) {
Some(codec::entities::EmbeddedEntity::Spline(value)) => {
+1 -1
View File
@@ -13,7 +13,7 @@ use codec::EntityType;
use crate::entities::curve::entity_curve;
pub use super::sweep_command_model::{
is_sweep_path, is_sweep_profile, sweep_record, sweep_selection_options, swept_surface_entity, swept_with_options,
is_sweep_path, is_sweep_profile, scaled_sweep_path_refusal, sweep_record, sweep_refusal_code, sweep_selection_options, swept_surface_entity, swept_with_options,
};
/// A drawn profile, as the kernel wants it: the plane it lies in and the
+13
View File
@@ -1454,6 +1454,19 @@ impl Scene {
self.rebuild_solid_history(handle, operation)
}
/// The modeler error code the reference reports when it refuses a sweep
/// property edit (twist or scale along a path with a corner).
pub fn solid_history_property_refusal(&self, handle: Handle, field: &str, value: &str) -> Option<u32> {
let mut operation = crate::scene::model::solid_history::primitive_property_operation(&self.document, handle)?;
if !crate::scene::model::solid_history::apply_primitive_property(&mut operation, field, value) {
return None;
}
let codec::objects::SolidHistoryOperation::Sweep(record) = &operation else {
return None;
};
kernel::acis::sweep_history_refusal(record).map(crate::scene::model::sweep_model::sweep_refusal_code)
}
pub fn apply_solid_position_property(
&mut self,
handle: Handle,