Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions crates/opencascade-sys/include/wrapper.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Shape.hxx>
#include <TDF_Data.hxx>
#include <TDF_Label.hxx>
#include <TDF_Delta.hxx>
#include <TDF_Transaction.hxx>
#include <gp.hxx>
#include <gp_Ax2.hxx>
#include <gp_Ax3.hxx>
Expand All @@ -88,6 +92,46 @@
#include <gp_Trsf.hxx>
#include <gp_Vec.hxx>

// BEGIN Tdf stuff
typedef opencascade::handle<TDF_Data> HandleTdfData;
typedef opencascade::handle<TDF_Delta> HandleTdfDelta;

inline std::unique_ptr<HandleTdfData> TDF_Data_new() {
return std::unique_ptr<HandleTdfData>(new opencascade::handle<TDF_Data>(new TDF_Data()));
}

inline std::unique_ptr<TDF_Label> TDF_Data_root(const HandleTdfData& data) {
return std::unique_ptr<TDF_Label>(new TDF_Label(data->Root()));
}

inline std::unique_ptr<TDF_Label> TDF_Label_new_child(const TDF_Label& label) {
return std::unique_ptr<TDF_Label>(new TDF_Label(label.NewChild()));
}

inline bool TDF_Label_is_null(const TDF_Label& label) {
return label.IsNull();
}
inline std::unique_ptr<TDF_Transaction> TDF_Transaction_new(const HandleTdfData& data) {
return std::unique_ptr<TDF_Transaction>(new TDF_Transaction(data));
}

inline Standard_Integer TDF_Transaction_open(TDF_Transaction& transaction) {
return transaction.Open();
}

inline std::unique_ptr<HandleTdfDelta> TDF_Transaction_commit(TDF_Transaction& transaction) {
return std::unique_ptr<HandleTdfDelta>(new opencascade::handle<TDF_Delta>(transaction.Commit(true)));
}

inline void TDF_Transaction_abort(TDF_Transaction& transaction) {
transaction.Abort();
}

inline bool TDF_Transaction_is_open(const TDF_Transaction& transaction) {
return transaction.IsOpen();
}
// END Tdf stuff

// Generic template constructor
template <typename T, typename... Args> std::unique_ptr<T> construct_unique(Args... args) {
return std::unique_ptr<T>(new T(args...));
Expand Down
16 changes: 16 additions & 0 deletions crates/opencascade-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ pub mod ffi {
#[cxx_name = "construct_unique"]
pub fn Message_ProgressRange_ctor() -> UniquePtr<Message_ProgressRange>;

// TDF_Data management
type HandleTdfData;
type TDF_Label;
type TDF_Transaction;
type HandleTdfDelta;

pub fn TDF_Data_new() -> UniquePtr<HandleTdfData>;
pub fn TDF_Data_root(data: &HandleTdfData) -> UniquePtr<TDF_Label>;
pub fn TDF_Label_new_child(label: &TDF_Label) -> UniquePtr<TDF_Label>;
pub fn TDF_Label_is_null(label: &TDF_Label) -> bool;
pub fn TDF_Transaction_new(data: &HandleTdfData) -> UniquePtr<TDF_Transaction>;
pub fn TDF_Transaction_open(transaction: Pin<&mut TDF_Transaction>) -> i32;
pub fn TDF_Transaction_commit(transaction: Pin<&mut TDF_Transaction>) -> UniquePtr<HandleTdfDelta>;
pub fn TDF_Transaction_abort(transaction: Pin<&mut TDF_Transaction>);
pub fn TDF_Transaction_is_open(transaction: &TDF_Transaction) -> bool;

// Handles
type HandleStandardType;
type HandleGeomCurve;
Expand Down
1 change: 1 addition & 0 deletions crates/opencascade/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod workplane;

mod law_function;
mod make_pipe_shell;
pub mod tdf;

#[derive(Error, Debug)]
pub enum Error {
Expand Down
107 changes: 107 additions & 0 deletions crates/opencascade/src/tdf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use cxx::UniquePtr;
use opencascade_sys::ffi;
use std::marker::PhantomData;

/// ```compile_fail
/// use opencascade::tdf::TdfData;
/// let root = {
/// let doc = TdfData::new();
/// doc.root()
/// };
/// ```
pub struct TdfData {
inner: UniquePtr<ffi::HandleTdfData>,
}

impl TdfData {
pub fn new() -> Self {
Self { inner: ffi::TDF_Data_new() }
}

pub fn root(&self) -> TdfLabel<'_> {
TdfLabel { inner: ffi::TDF_Data_root(self.inner.as_ref().unwrap()), _phantom: PhantomData }
}
pub fn transaction(&self) -> TdfTransaction<'_> {
TdfTransaction {
inner: ffi::TDF_Transaction_new(self.inner.as_ref().unwrap()),
_phantom: PhantomData,
}
}
}

pub struct TdfLabel<'doc> {
inner: UniquePtr<ffi::TDF_Label>,
_phantom: PhantomData<&'doc TdfData>,
}

impl<'doc> TdfLabel<'doc> {
pub fn new_child(&self) -> TdfLabel<'doc> {
TdfLabel {
inner: ffi::TDF_Label_new_child(self.inner.as_ref().unwrap()),
_phantom: PhantomData,
}
}

pub fn is_null(&self) -> bool {
ffi::TDF_Label_is_null(self.inner.as_ref().unwrap())
}
}
pub struct TdfTransaction<'doc> {
inner: UniquePtr<ffi::TDF_Transaction>,
_phantom: PhantomData<&'doc TdfData>,
}

impl<'doc> TdfTransaction<'doc> {
pub fn open(&mut self) -> i32 {
ffi::TDF_Transaction_open(self.inner.pin_mut())
}

// TODO: wrap HandleTdfDelta in a safe TdfDelta type before contributing upstream.
// Currently leaks the raw FFI type into the safe wrapper layer.
pub fn commit(mut self) -> UniquePtr<ffi::HandleTdfDelta> {
ffi::TDF_Transaction_commit(self.inner.pin_mut())
}

pub fn abort(mut self) {
ffi::TDF_Transaction_abort(self.inner.pin_mut())
}

pub fn is_open(&self) -> bool {
ffi::TDF_Transaction_is_open(self.inner.as_ref().unwrap())
}
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_tdf_data_constructs() {
let doc = TdfData::new();
drop(doc);
}
#[test]
fn test_tdf_data_root_is_not_null() {
let doc = TdfData::new();
let root = doc.root();
assert!(!root.is_null());
}

#[test]
fn test_new_child_is_not_null() {
let doc = TdfData::new();
let root = doc.root();
// let child = root.new_child();
assert!(!root.is_null());
// drop(child);
}
#[test]
fn test_new_child_with_transaction() {
let doc = TdfData::new();
let mut tx = doc.transaction();
tx.open();
let root = doc.root();
let child = root.new_child();
assert!(!child.is_null());
tx.commit();
}
}