Skip to content

Commit

Permalink
add Shell::loft(wires) and underlying machinery (#142)
Browse files Browse the repository at this point in the history
* add Shell::loft(wires) and underlying machinery

* fix typo in comment
  • Loading branch information
mkovaxx authored Oct 16, 2023
1 parent 2dde5c0 commit b6c7a05
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions crates/opencascade-sys/include/wrapper.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,15 @@ inline const TopoDS_Vertex &TopoDS_cast_to_vertex(const TopoDS_Shape &shape) { r
inline const TopoDS_Edge &TopoDS_cast_to_edge(const TopoDS_Shape &shape) { return TopoDS::Edge(shape); }
inline const TopoDS_Wire &TopoDS_cast_to_wire(const TopoDS_Shape &shape) { return TopoDS::Wire(shape); }
inline const TopoDS_Face &TopoDS_cast_to_face(const TopoDS_Shape &shape) { return TopoDS::Face(shape); }
inline const TopoDS_Shell &TopoDS_cast_to_shell(const TopoDS_Shape &shape) { return TopoDS::Shell(shape); }
inline const TopoDS_Solid &TopoDS_cast_to_solid(const TopoDS_Shape &shape) { return TopoDS::Solid(shape); }
inline const TopoDS_Compound &TopoDS_cast_to_compound(const TopoDS_Shape &shape) { return TopoDS::Compound(shape); }

inline const TopoDS_Shape &cast_vertex_to_shape(const TopoDS_Vertex &vertex) { return vertex; }
inline const TopoDS_Shape &cast_edge_to_shape(const TopoDS_Edge &edge) { return edge; }
inline const TopoDS_Shape &cast_wire_to_shape(const TopoDS_Wire &wire) { return wire; }
inline const TopoDS_Shape &cast_face_to_shape(const TopoDS_Face &face) { return face; }
inline const TopoDS_Shape &cast_shell_to_shape(const TopoDS_Shell &shell) { return shell; }
inline const TopoDS_Shape &cast_solid_to_shape(const TopoDS_Solid &solid) { return solid; }
inline const TopoDS_Shape &cast_compound_to_shape(const TopoDS_Compound &compound) { return compound; }

Expand Down
2 changes: 2 additions & 0 deletions crates/opencascade-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,15 @@ pub mod ffi {
pub fn cast_edge_to_shape(wire: &TopoDS_Edge) -> &TopoDS_Shape;
pub fn cast_wire_to_shape(wire: &TopoDS_Wire) -> &TopoDS_Shape;
pub fn cast_face_to_shape(wire: &TopoDS_Face) -> &TopoDS_Shape;
pub fn cast_shell_to_shape(wire: &TopoDS_Shell) -> &TopoDS_Shape;
pub fn cast_solid_to_shape(wire: &TopoDS_Solid) -> &TopoDS_Shape;
pub fn cast_compound_to_shape(wire: &TopoDS_Compound) -> &TopoDS_Shape;

pub fn TopoDS_cast_to_vertex(shape: &TopoDS_Shape) -> &TopoDS_Vertex;
pub fn TopoDS_cast_to_wire(shape: &TopoDS_Shape) -> &TopoDS_Wire;
pub fn TopoDS_cast_to_edge(shape: &TopoDS_Shape) -> &TopoDS_Edge;
pub fn TopoDS_cast_to_face(shape: &TopoDS_Shape) -> &TopoDS_Face;
pub fn TopoDS_cast_to_shell(shape: &TopoDS_Shape) -> &TopoDS_Shell;
pub fn TopoDS_cast_to_solid(shape: &TopoDS_Shape) -> &TopoDS_Solid;
pub fn TopoDS_cast_to_compound(shape: &TopoDS_Shape) -> &TopoDS_Compound;

Expand Down
10 changes: 9 additions & 1 deletion crates/opencascade/src/primitives/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
mesh::{Mesh, Mesher},
primitives::{
make_dir, make_point, make_point2d, make_vec, BooleanShape, Compound, Edge, EdgeIterator,
Face, FaceIterator, ShapeType, Solid, Vertex, Wire,
Face, FaceIterator, ShapeType, Shell, Solid, Vertex, Wire,
},
Error,
};
Expand Down Expand Up @@ -53,6 +53,14 @@ impl From<Face> for Shape {
}
}

impl From<Shell> for Shape {
fn from(shell: Shell) -> Self {
let shape = ffi::cast_shell_to_shape(&shell.inner);

Self::from_shape(shape)
}
}

impl From<Solid> for Shape {
fn from(solid: Solid) -> Self {
let shape = ffi::cast_solid_to_shape(&solid.inner);
Expand Down
29 changes: 28 additions & 1 deletion crates/opencascade/src/primitives/shell.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
use cxx::UniquePtr;
use opencascade_sys::ffi;

use crate::primitives::Wire;

pub struct Shell {
pub(crate) _inner: UniquePtr<ffi::TopoDS_Shell>,
pub(crate) inner: UniquePtr<ffi::TopoDS_Shell>,
}

impl AsRef<Shell> for Shell {
fn as_ref(&self) -> &Shell {
self
}
}

impl Shell {
pub(crate) fn from_shell(shell: &ffi::TopoDS_Shell) -> Self {
let inner = ffi::TopoDS_Shell_to_owned(shell);

Self { inner }
}

pub fn loft<T: AsRef<Wire>>(wires: impl IntoIterator<Item = T>) -> Self {
let is_solid = false;
let mut make_loft = ffi::BRepOffsetAPI_ThruSections_ctor(is_solid);

for wire in wires.into_iter() {
make_loft.pin_mut().AddWire(&wire.as_ref().inner);
}

// Set CheckCompatibility to `true` to avoid twisted results.
make_loft.pin_mut().CheckCompatibility(true);

let shape = make_loft.pin_mut().Shape();
let shell = ffi::TopoDS_cast_to_shell(shape);

Self::from_shell(shell)
}
}

0 comments on commit b6c7a05

Please sign in to comment.