Skip to content

Commit 37e5b69

Browse files
committed
Add a top_loc bridge
1 parent 87923cf commit 37e5b69

File tree

6 files changed

+56
-27
lines changed

6 files changed

+56
-27
lines changed

crates/opencascade-sys/src/lib.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod gc_pnts;
55
pub mod poly;
66
pub mod shape_analysis;
77
pub mod shape_upgrade;
8+
pub mod top_loc;
89

910
#[cxx::bridge]
1011
pub mod ffi {
@@ -431,16 +432,12 @@ pub mod ffi {
431432
pub fn TopoDS_cast_to_compound(shape: &TopoDS_Shape) -> &TopoDS_Compound;
432433

433434
#[cxx_name = "Move"]
434-
pub fn translate(
435-
self: Pin<&mut TopoDS_Shape>,
436-
position: &TopLoc_Location,
437-
raise_exception: bool,
438-
);
435+
pub fn translate(self: Pin<&mut TopoDS_Shape>, position: &Location, raise_exception: bool);
439436

440437
#[cxx_name = "Location"]
441438
pub fn set_global_translation(
442439
self: Pin<&mut TopoDS_Shape>,
443-
translation: &TopLoc_Location,
440+
translation: &Location,
444441
raise_exception: bool,
445442
);
446443

@@ -1133,7 +1130,7 @@ pub mod ffi {
11331130
pub fn BRep_Tool_Pnt(vertex: &TopoDS_Vertex) -> UniquePtr<gp_Pnt>;
11341131
pub fn BRep_Tool_Triangulation(
11351132
face: &TopoDS_Face,
1136-
location: Pin<&mut TopLoc_Location>,
1133+
location: Pin<&mut Location>,
11371134
) -> UniquePtr<HandlePoly_Triangulation>;
11381135

11391136
type BRepIntCurveSurface_Inter;
@@ -1246,14 +1243,8 @@ pub mod ffi {
12461243
pub fn Shape(self: &BRepMesh_IncrementalMesh) -> &TopoDS_Shape;
12471244
pub fn IsDone(self: &BRepMesh_IncrementalMesh) -> bool;
12481245

1249-
type TopLoc_Location;
1250-
#[cxx_name = "construct_unique"]
1251-
pub fn TopLoc_Location_ctor() -> UniquePtr<TopLoc_Location>;
1252-
1253-
#[cxx_name = "construct_unique"]
1254-
pub fn TopLoc_Location_from_transform(transform: &gp_Trsf) -> UniquePtr<TopLoc_Location>;
1255-
1256-
pub fn TopLoc_Location_Transformation(location: &TopLoc_Location) -> UniquePtr<gp_Trsf>;
1246+
#[cxx_name = "TopLoc_Location"]
1247+
type Location = crate::top_loc::Location;
12571248

12581249
#[cxx_name = "Poly_Triangulation"]
12591250
type Poly_Triangulation = crate::poly::Triangulation;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use cxx::UniquePtr;
2+
pub use inner::*;
3+
4+
#[cxx::bridge]
5+
mod inner {
6+
unsafe extern "C++" {
7+
include!("opencascade-sys/include/wrapper.hxx");
8+
9+
type gp_Trsf = crate::ffi::gp_Trsf;
10+
11+
#[cxx_name = "TopLoc_Location"]
12+
type Location;
13+
#[cxx_name = "construct_unique"]
14+
pub fn Location_new() -> UniquePtr<Location>;
15+
16+
#[cxx_name = "construct_unique"]
17+
fn Location_from_transform(transform: &gp_Trsf) -> UniquePtr<Location>;
18+
19+
fn TopLoc_Location_Transformation(location: &Location) -> UniquePtr<gp_Trsf>;
20+
}
21+
}
22+
23+
impl Location {
24+
pub fn new() -> UniquePtr<Self> {
25+
Location_new()
26+
}
27+
28+
pub fn from_transform(transform: &gp_Trsf) -> UniquePtr<Location> {
29+
Location_from_transform(transform)
30+
}
31+
32+
pub fn transform(&self) -> UniquePtr<gp_Trsf> {
33+
TopLoc_Location_Transformation(self)
34+
}
35+
}

crates/opencascade-sys/tests/triangulation.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use opencascade_sys::ffi::{
2-
new_point, BRepMesh_IncrementalMesh_ctor, BRepPrimAPI_MakeBox_ctor, BRep_Tool_Triangulation,
3-
HandlePoly_Triangulation_Get, TopAbs_ShapeEnum, TopExp_Explorer_ctor, TopLoc_Location_ctor,
4-
TopoDS_cast_to_face,
1+
use opencascade_sys::{
2+
ffi::{
3+
new_point, BRepMesh_IncrementalMesh_ctor, BRepPrimAPI_MakeBox_ctor,
4+
BRep_Tool_Triangulation, HandlePoly_Triangulation_Get, TopAbs_ShapeEnum,
5+
TopExp_Explorer_ctor, TopoDS_cast_to_face,
6+
},
7+
top_loc::Location,
58
};
69

710
#[test]
@@ -17,7 +20,7 @@ fn it_can_access_mesh_triangulation() {
1720
TopExp_Explorer_ctor(mesh.pin_mut().Shape(), TopAbs_ShapeEnum::TopAbs_FACE);
1821
while edge_explorer.More() {
1922
let face = TopoDS_cast_to_face(edge_explorer.Current());
20-
let mut location = TopLoc_Location_ctor();
23+
let mut location = Location::new();
2124

2225
let triangulation_handle = BRep_Tool_Triangulation(face, location.pin_mut());
2326
if let Ok(triangulation) = HandlePoly_Triangulation_Get(&triangulation_handle) {

crates/opencascade/src/mesh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
};
55
use cxx::UniquePtr;
66
use glam::{dvec2, dvec3, DVec2, DVec3};
7-
use opencascade_sys::ffi;
7+
use opencascade_sys::{ffi, top_loc::Location};
88

99
#[derive(Debug)]
1010
pub struct Mesh {
@@ -38,7 +38,7 @@ impl Mesher {
3838
let triangulated_shape = Shape::from_shape(self.inner.pin_mut().Shape());
3939

4040
for face in triangulated_shape.faces() {
41-
let mut location = ffi::TopLoc_Location_ctor();
41+
let mut location = Location::new();
4242

4343
let triangulation_handle =
4444
ffi::BRep_Tool_Triangulation(&face.inner, location.pin_mut());
@@ -51,7 +51,7 @@ impl Mesher {
5151

5252
for i in 1..=face_point_count {
5353
let mut point = triangulation.node(i);
54-
point.pin_mut().Transform(&ffi::TopLoc_Location_Transformation(&location));
54+
point.pin_mut().Transform(&location.transform());
5555
vertices.push(dvec3(point.X(), point.Y(), point.Z()));
5656
}
5757

crates/opencascade/src/primitives/shape.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
};
99
use cxx::UniquePtr;
1010
use glam::{dvec2, dvec3, DVec3};
11-
use opencascade_sys::{ffi, shape_upgrade::UnifySameDomain};
11+
use opencascade_sys::{ffi, shape_upgrade::UnifySameDomain, top_loc::Location};
1212
use std::path::Path;
1313

1414
pub struct Shape {
@@ -626,7 +626,7 @@ impl Shape {
626626
let translation_vec = make_vec(translation);
627627
transform.pin_mut().set_translation_vec(&translation_vec);
628628

629-
let location = ffi::TopLoc_Location_from_transform(&transform);
629+
let location = Location::from_transform(&transform);
630630

631631
self.inner.pin_mut().set_global_translation(&location, false);
632632
}

crates/opencascade/src/primitives/wire.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use opencascade_sys::{b_rep_tools, shape_analysis::connect_edges_to_wires};
1+
use opencascade_sys::{b_rep_tools, shape_analysis::connect_edges_to_wires, top_loc::Location};
22
use std::iter::once;
33

44
use crate::{
@@ -235,7 +235,7 @@ impl Wire {
235235

236236
transform.pin_mut().SetRotation(&rotation_axis_vec, angle.radians());
237237
transform.pin_mut().set_translation_vec(&translation_vec);
238-
let location = ffi::TopLoc_Location_from_transform(&transform);
238+
let location = Location::from_transform(&transform);
239239

240240
let wire_shape = ffi::cast_wire_to_shape(&self.inner);
241241
let mut wire_shape = Shape::from_shape(wire_shape).inner;

0 commit comments

Comments
 (0)