Complete EXTRUDE behavior and properties

This commit is contained in:
ramox81 committed 2026-09-02 10:25:00 +03:00
1 parent bde9cc2715
commit 847d218c35
13 files changed
+912 -108

No files matched your search

+190 -1
View File
@@ -57,6 +57,8 @@ pub const PROP_BANK: &str = "solid_history_bank";
pub const PROP_TWIST_ALONG_PATH: &str = "solid_history_twist_along_path";
pub const PROP_SCALE_ALONG_PATH: &str = "solid_history_scale_along_path";
pub const PROP_SWEEP_LENGTH: &str = "solid_history_sweep_length";
pub const PROP_EXTRUSION_HEIGHT: &str = "solid_history_extrusion_height";
pub const PROP_TAPER_ANGLE: &str = "solid_history_taper_angle";
pub const PROP_PYRAMID_TYPE: &str = "solid_history_pyramid_type";
pub const PROP_HISTORY: &str = "solid_history_record";
pub const PROP_SHOW_HISTORY: &str = "solid_history_show";
@@ -110,6 +112,7 @@ pub fn has_specialized_primitive_properties(
| SolidHistoryOperation::Torus(_)
| SolidHistoryOperation::Pyramid(_)
| SolidHistoryOperation::Sweep(_)
| SolidHistoryOperation::Extrusion(_)
)
)
}
@@ -125,7 +128,7 @@ pub fn reference_point(operation: &SolidHistoryOperation) -> Option<glam::DVec3>
}
SolidHistoryOperation::Cone(value) => world_point(value.base.transform, [0.0; 3]),
SolidHistoryOperation::Cylinder(value) => world_point(value.base.transform, [0.0; 3]),
SolidHistoryOperation::Sweep(value) => world_point(
SolidHistoryOperation::Sweep(value) | SolidHistoryOperation::Extrusion(value) => world_point(
value.base.transform,
[
value.reference_point.x,
@@ -139,6 +142,100 @@ pub fn reference_point(operation: &SolidHistoryOperation) -> Option<glam::DVec3>
}
}
fn extrusion_properties(
document: &acadrust::CadDocument,
handle: acadrust::Handle,
value: &SolidHistorySweep,
) -> Vec<PropSection> {
let position = world_point(
value.base.transform,
[
value.reference_point.x,
value.reference_point.y,
value.reference_point.z,
],
)
.unwrap_or(glam::DVec3::ZERO);
let (record_history, object_show_history, show_history_mode) =
history_flags(document, handle).unwrap_or((false, false, 1));
let (show_history, show_history_editable) =
displayed_history_state(object_show_history, show_history_mode);
let path_length = sweep_path_length(value);
let height_value = path_length.unwrap_or(value.end_draft_distance);
let height = if value.path_entity.is_some() {
PropValue::ReadOnly(crate::entities::common::format_length(height_value))
} else {
PropValue::EditText(crate::entities::common::format_length(height_value))
};
let taper = if value.path_entity.is_some() {
PropValue::ReadOnly(crate::entities::common::format_angle(value.draft_angle))
} else {
PropValue::EditText(crate::entities::common::format_angle(value.draft_angle))
};
vec![
PropSection {
title: t!("Geometry").into_owned(),
props: vec![
Property {
label: t!("Solid type").into_owned(),
field: "solid_history_type",
value: PropValue::ReadOnly(t!("Extrusion").into_owned()),
},
crate::entities::common::edit_prop(
t!("Position X").as_ref(),
PROP_POSITION_X,
position.x,
),
crate::entities::common::edit_prop(
t!("Position Y").as_ref(),
PROP_POSITION_Y,
position.y,
),
crate::entities::common::edit_prop(
t!("Position Z").as_ref(),
PROP_POSITION_Z,
position.z,
),
Property {
label: t!("Height").into_owned(),
field: PROP_EXTRUSION_HEIGHT,
value: height,
},
Property {
label: t!("Taper angle").into_owned(),
field: PROP_TAPER_ANGLE,
value: taper,
},
],
},
PropSection {
title: t!("Solid History").into_owned(),
props: vec![
Property {
label: t!("History").into_owned(),
field: PROP_HISTORY,
value: PropValue::Choice {
selected: if record_history { "Record" } else { "None" }.to_string(),
options: vec!["None".to_string(), "Record".to_string()],
},
},
Property {
label: t!("Show History").into_owned(),
field: PROP_SHOW_HISTORY,
value: if show_history_editable {
PropValue::Choice {
selected: if show_history { "Yes" } else { "No" }.to_string(),
options: vec!["No".to_string(), "Yes".to_string()],
}
} else {
PropValue::ReadOnly(if show_history { "Yes" } else { "No" }.to_string())
},
},
],
},
]
}
fn sweep_properties(
document: &acadrust::CadDocument,
handle: acadrust::Handle,
@@ -889,6 +986,9 @@ pub fn primitive_properties(
SolidHistoryOperation::Pyramid(value) => pyramid_properties(document, handle, value),
SolidHistoryOperation::Torus(value) => torus_properties(document, handle, value),
SolidHistoryOperation::Sweep(value) => sweep_properties(document, handle, value),
SolidHistoryOperation::Extrusion(value) => {
extrusion_properties(document, handle, value)
}
_ => Vec::new(),
}
}
@@ -919,6 +1019,8 @@ pub fn is_primitive_property(field: &str) -> bool {
| PROP_PROFILE_ROTATION
| PROP_TWIST_ALONG_PATH
| PROP_SCALE_ALONG_PATH
| PROP_EXTRUSION_HEIGHT
| PROP_TAPER_ANGLE
| PROP_PYRAMID_TYPE
)
}
@@ -933,6 +1035,86 @@ pub fn is_specialized_property(field: &str) -> bool {
|| is_history_choice(field)
}
fn apply_extrusion_geometry_property(
value: &mut SolidHistorySweep,
field: &str,
text: &str,
) -> Option<bool> {
if value.path_entity.is_some()
&& matches!(field, PROP_EXTRUSION_HEIGHT | PROP_TAPER_ANGLE)
{
return Some(false);
}
match field {
PROP_POSITION_X | PROP_POSITION_Y | PROP_POSITION_Z => {
let axis = match field {
PROP_POSITION_X => 0,
PROP_POSITION_Y => 1,
_ => 2,
};
let target = crate::entities::common::parse_length(text)?;
if !target.is_finite() {
return Some(false);
}
let current = matrix(value.base.transform)?;
let reference = current.transform_point3(glam::DVec3::new(
value.reference_point.x,
value.reference_point.y,
value.reference_point.z,
));
if !reference.is_finite() || (target - reference[axis]).abs() <= 1e-12 {
return Some(false);
}
let mut delta = glam::DVec3::ZERO;
delta[axis] = target - reference[axis];
let updated = glam::DMat4::from_translation(delta) * current;
if !updated.is_finite() || updated.determinant().abs() <= 1e-12 {
return Some(false);
}
value.base.transform = updated.to_cols_array();
Some(true)
}
PROP_EXTRUSION_HEIGHT => {
let height = crate::entities::common::parse_length(text)?;
if !height.is_finite() || height.abs() <= 1e-9 {
return Some(false);
}
let direction = glam::DVec3::new(
value.direction.x,
value.direction.y,
value.direction.z,
);
let Some(unit) = direction.try_normalize() else {
return Some(false);
};
let sign = if height.signum() == value.end_draft_distance.signum() {
1.0
} else {
-1.0
};
let updated = unit * height.abs() * sign;
if (updated - direction).length_squared() <= 1e-24 {
return Some(false);
}
value.direction = acadrust::types::Vector3::new(updated.x, updated.y, updated.z);
value.end_draft_distance = height;
Some(true)
}
PROP_TAPER_ANGLE => {
let angle = crate::entities::common::parse_angle(text)?;
if !angle.is_finite()
|| angle.abs() >= std::f64::consts::FRAC_PI_2
|| (angle - value.draft_angle).abs() <= 1e-12
{
return Some(false);
}
value.draft_angle = angle;
Some(true)
}
_ => None,
}
}
pub fn apply_history_choice(
document: &mut acadrust::CadDocument,
handle: acadrust::Handle,
@@ -1362,6 +1544,13 @@ pub fn apply_primitive_property(
return applied;
}
}
if let SolidHistoryOperation::Extrusion(extrusion_value) = operation {
if let Some(applied) =
apply_extrusion_geometry_property(extrusion_value, field, value)
{
return applied;
}
}
if let SolidHistoryOperation::Torus(torus_value) = operation {
if let Some(applied) = apply_torus_position_property(torus_value, field, value) {
return applied;
+275 -19
View File
@@ -32,26 +32,130 @@ pub fn profile_of(entity: &EntityType) -> Option<Profile> {
if !planar.curve.is_closed() {
return None;
}
let pieces = match &planar.curve {
Curve::Polyline(_) => planar.curve.segments(),
Curve::Circle(circle) => quarters(circle.centre, circle.radius),
Curve::Arc(arc) => split_arc(*arc),
Curve::Ellipse(arc) => split_ellipse(*arc),
Curve::Nurbs(curve) => (0..4)
.map(|part| {
curve
.trimmed(part as f64 / 4.0, (part + 1) as f64 / 4.0)
.map(Curve::Nurbs)
})
.collect::<Option<Vec<_>>>()?,
_ => return None,
};
let pieces = profile_pieces(&planar.curve, true)?;
(pieces.len() >= 3).then_some(Profile {
plane: planar.plane,
pieces,
})
}
/// An entity-level planar curve suitable for either a solid profile or an
/// open surface profile.
pub fn extrusion_profile_of(entity: &EntityType) -> Option<(Profile, bool)> {
let planar = entity_curve(entity).or_else(|| planar_polygon_entity(entity))?;
let closed = planar.curve.is_closed();
let pieces = profile_pieces(&planar.curve, closed)?;
(!pieces.is_empty()).then_some((
Profile {
plane: planar.plane,
pieces,
},
closed,
))
}
fn profile_pieces(curve: &Curve, closed: bool) -> Option<Vec<Curve>> {
let pieces = match curve {
Curve::Polyline(_) => curve.segments(),
Curve::Circle(circle) => quarters(circle.centre, circle.radius),
Curve::Arc(arc) if closed => split_arc(*arc),
Curve::Arc(arc) => vec![Curve::Arc(*arc)],
Curve::Ellipse(arc) if closed => split_ellipse(*arc),
Curve::Ellipse(arc) => vec![Curve::Ellipse(*arc)],
Curve::Line(line) => vec![Curve::Line(*line)],
Curve::Nurbs(nurbs) if closed => (0..4)
.map(|part| {
nurbs
.trimmed(part as f64 / 4.0, (part + 1) as f64 / 4.0)
.map(Curve::Nurbs)
})
.collect::<Option<Vec<_>>>()?,
Curve::Nurbs(nurbs) => vec![Curve::Nurbs(nurbs.clone())],
_ => return None,
};
Some(pieces)
}
fn planar_polygon_entity(entity: &EntityType) -> Option<PlanarCurve> {
let (points, closed) = match entity {
EntityType::Polyline3D(value) => (
value
.vertices
.iter()
.map(|vertex| [vertex.position.x, vertex.position.y, vertex.position.z])
.collect::<Vec<_>>(),
value.is_closed(),
),
EntityType::Face3D(value) => (
value
.corners()
.into_iter()
.map(|point| [point.x, point.y, point.z])
.collect::<Vec<_>>(),
true,
),
EntityType::Solid(value) => (
value
.boundary_corners()
.into_iter()
.map(|point| [point.x, point.y, point.z])
.collect::<Vec<_>>(),
true,
),
EntityType::Region(value) => (
value
.wires
.first()?
.points
.iter()
.map(|point| [point.x, point.y, point.z])
.collect::<Vec<_>>(),
true,
),
_ => return None,
};
let mut points = points;
if closed && points.len() > 2 && points.first() == points.last() {
points.pop();
}
if points.len() < if closed { 3 } else { 2 } {
return None;
}
let origin = Vec3::from(points[0]);
let first = Vec3::from(points[1]) - origin;
let normal = points[2..]
.iter()
.find_map(|point| first.cross(Vec3::from(*point) - origin).normalize())
.or_else(|| {
(!closed).then(|| first.cross(Vec3::Z).normalize().unwrap_or(Vec3::Y))
})?;
let plane = Plane::orthonormal(points[0], first.to_array(), normal.to_array())?;
let scale = points
.iter()
.flatten()
.map(|value| value.abs())
.fold(1.0_f64, f64::max);
if points
.iter()
.any(|point| !plane.contains(*point, scale * 1e-9))
{
return None;
}
let vertices = points
.iter()
.map(|point| {
plane.project(*point).map(|position| cadkernel::geom2d::PolylineVertex {
position,
bulge: 0.0,
})
})
.collect::<Option<Vec<_>>>()?;
Some(PlanarCurve::new(
plane,
Curve::Polyline(cadkernel::geom2d::Polyline { vertices, closed }),
))
}
/// A circle as four arcs.
fn quarters(centre: [f64; 2], radius: f64) -> Vec<Curve> {
use std::f64::consts::FRAC_PI_2;
@@ -103,13 +207,46 @@ fn split_ellipse(arc: EllipseArc) -> Vec<Curve> {
pub fn extruded(entity: &EntityType, height: f64) -> Option<Body> {
let profile = profile_of(entity)?;
let normal = profile.plane.normal()?;
brep::extrude(
profile.plane,
&profile.pieces,
extruded_direction(
entity,
[normal[0] * height, normal[1] * height, normal[2] * height],
0.0,
)
}
pub fn extruded_direction(
entity: &EntityType,
direction: [f64; 3],
taper_angle: f64,
) -> Option<Body> {
let profile = profile_of(entity)?;
brep::extrude_tapered(profile.plane, &profile.pieces, direction, taper_angle)
}
pub fn extruded_surface(
entity: &EntityType,
direction: [f64; 3],
taper_angle: f64,
) -> Option<Body> {
let (profile, closed) = extrusion_profile_of(entity)?;
if closed {
brep::extrude_surface_tapered(
profile.plane,
&profile.pieces,
direction,
taper_angle,
)
} else if taper_angle.abs() <= 1e-12 {
brep::extrude_surface(profile.plane, &profile.pieces, direction)
} else {
None
}
}
pub fn extruded_along_path(entity: &EntityType, path: &EntityType) -> Option<Body> {
swept(entity, path)
}
/// Signed distance of a drag along a profile's normal.
pub fn projected_drag(entity: &EntityType, from: glam::DVec3, to: glam::DVec3) -> Option<f64> {
let normal = entity_curve(entity)?.plane.normal()?;
@@ -142,13 +279,40 @@ pub fn revolved(
/// SWEEP as an exact B-rep along straight and circular path pieces.
pub fn swept(profile: &EntityType, path: &EntityType) -> Option<Body> {
let profile = profile_of(profile)?;
let path = entity_curve(path)?;
let mut path = entity_curve(path)?;
let pieces = match &path.curve {
Curve::Line(line) => vec![Curve::Line(*line)],
Curve::Arc(arc) => vec![Curve::Arc(*arc)],
Curve::Circle(circle) => quarters(circle.centre, circle.radius),
Curve::Polyline(_) => path.curve.segments(),
Curve::Ellipse(_) | Curve::Nurbs(_) => {
let scale = path
.curve
.point_at(0.0)
.into_iter()
.chain(path.curve.point_at(1.0))
.map(f64::abs)
.fold(1.0_f64, f64::max);
path.curve
.tessellate_within(scale * 1e-4)
.windows(2)
.filter(|pair| pair[0] != pair[1])
.map(|pair| Curve::Line(Line {
start: pair[0],
end: pair[1],
}))
.collect()
}
_ => return None,
};
let profile_center = profile
.pieces
.iter()
.map(|piece| Vec3::from(profile.plane.point_at(piece.point_at(0.0))))
.fold(Vec3::ZERO, |sum, point| sum + point)
/ profile.pieces.len() as f64;
let path_start = Vec3::from(path.plane.point_at(path.curve.point_at(0.0)));
path.plane.origin = (Vec3::from(path.plane.origin) + profile_center - path_start).to_array();
brep::sweep_along(profile.plane, &profile.pieces, path.plane, &pieces)
}
@@ -174,7 +338,7 @@ pub enum PolysolidJustification {
Right,
}
fn embedded_path(entity: &EntityType) -> Option<EmbeddedEntity> {
pub fn embedded_path(entity: &EntityType) -> Option<EmbeddedEntity> {
match entity {
EntityType::Line(value) => Some(EmbeddedEntity::Line(value.clone())),
EntityType::Arc(value) => Some(EmbeddedEntity::Arc(value.clone())),
@@ -186,6 +350,98 @@ fn embedded_path(entity: &EntityType) -> Option<EmbeddedEntity> {
}
}
fn embedded_planar_entity(entity: &EntityType) -> Option<(EmbeddedEntity, [f64; 16])> {
if let Some(embedded) = embedded_path(entity) {
return Some((embedded, glam::DMat4::IDENTITY.to_cols_array()));
}
let (profile, closed) = extrusion_profile_of(entity)?;
let mut polyline = LwPolyline::new();
for piece in &profile.pieces {
let Curve::Line(line) = piece else {
return None;
};
polyline
.vertices
.push(LwVertex::new(Vector2::new(line.start[0], line.start[1])));
}
if !closed {
let Curve::Line(last) = profile.pieces.last()? else {
return None;
};
polyline
.vertices
.push(LwVertex::new(Vector2::new(last.end[0], last.end[1])));
}
polyline.is_closed = closed;
let normal = profile.plane.normal()?;
let transform = glam::DMat4::from_cols(
glam::DVec4::new(
profile.plane.x_axis[0],
profile.plane.x_axis[1],
profile.plane.x_axis[2],
0.0,
),
glam::DVec4::new(
profile.plane.y_axis[0],
profile.plane.y_axis[1],
profile.plane.y_axis[2],
0.0,
),
glam::DVec4::new(normal[0], normal[1], normal[2], 0.0),
glam::DVec4::new(
profile.plane.origin[0],
profile.plane.origin[1],
profile.plane.origin[2],
1.0,
),
)
.to_cols_array();
Some((EmbeddedEntity::LwPolyline(polyline), transform))
}
pub fn extrusion_history(
profile: &EntityType,
path: Option<&EntityType>,
direction: [f64; 3],
taper_angle: f64,
reference_point: [f64; 3],
) -> Option<SolidHistoryOperation> {
let signed_height = extrusion_profile_of(profile)
.and_then(|(profile, _)| profile.plane.normal())
.map(|normal| Vec3::from(direction).dot(Vec3::from(normal)))
.filter(|value| value.is_finite())
.unwrap_or_else(|| Vec3::from(direction).length());
let mut base = SolidHistoryNodeBase::new(1);
base.transform = glam::DMat4::IDENTITY.to_cols_array();
let (sweep_entity, sweep_entity_transform) = embedded_planar_entity(profile)?;
let (path_entity, path_entity_transform) = match path {
Some(path) => {
let (entity, transform) = embedded_planar_entity(path)?;
(Some(entity), transform)
}
None => (None, glam::DMat4::IDENTITY.to_cols_array()),
};
Some(SolidHistoryOperation::Extrusion(SolidHistorySweep {
base,
operation_major: 1,
direction: Vector3::new(direction[0], direction[1], direction[2]),
sweep_entity: Some(sweep_entity),
path_entity,
draft_angle: taper_angle,
start_draft_distance: 0.0,
end_draft_distance: signed_height,
scale_factor: 1.0,
sweep_entity_transform,
path_entity_transform,
reference_point: Vector3::new(
reference_point[0],
reference_point[1],
reference_point[2],
),
..SolidHistorySweep::default()
}))
}
/// Builds a history-backed wall solid along a supported planar curve.
pub fn polysolid(
entity: &EntityType,