Skip to content

Commit

Permalink
more cleanup and casting fixes. compiles!
Browse files Browse the repository at this point in the history
  • Loading branch information
fraguada committed Aug 6, 2024
1 parent 42f8b6c commit 7bbce1e
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 114 deletions.
3 changes: 0 additions & 3 deletions src/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ EMSCRIPTEN_BINDINGS(rhino3dm) {

ON::Begin();
initFileUtilitiesBindings(m);
/*
initDefines(m);
initIntersectBindings(m);
initPolylineBindings(m);
Expand Down Expand Up @@ -84,8 +83,6 @@ EMSCRIPTEN_BINDINGS(rhino3dm) {
initDracoBindings(m);
initRTreeBindings(m);
initLinetypeBindings(m);
*/

}

#if defined(ON_PYTHON_COMPILE)
Expand Down
3 changes: 3 additions & 0 deletions src/bindings/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/tuple.h>
#include <nanobind/operators.h>
namespace py = nanobind;
typedef nanobind::module_ rh3dmpymodule;
#define RH3DM_PYTHON_BINDING(name, variable) NB_MODULE(name, variable)
Expand All @@ -19,11 +20,13 @@
#define def_readonly def_ro
#define def_readwrite def_rw
#define def_property_readonly_static def_prop_ro_static
#define import import_
#define UNIMPLEMENTED_EXCEPTION throw std::exception()

#else
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/operators.h>
namespace py = pybind11;
#define RH3DM_PYTHON_BINDING(name, variable) PYBIND11_MODULE(name, variable)
typedef pybind11::module rh3dmpymodule;
Expand Down
6 changes: 0 additions & 6 deletions src/bindings/bnd_annotationbase.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#include "bindings.h"

#if defined(NANOBIND)
namespace py = nanobind;
#else
namespace py = pybind11;
#endif

BND_AnnotationBase::BND_AnnotationBase()
{
}
Expand Down
14 changes: 2 additions & 12 deletions src/bindings/bnd_boundingbox.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#include "bindings.h"

#if defined(NANOBIND)
namespace py = nanobind;
#else
namespace py = pybind11;
#endif

BND_BoundingBox::BND_BoundingBox(const ON_3dPoint& min, const ON_3dPoint& max)
: m_bbox(min, max)
{
Expand Down Expand Up @@ -104,19 +98,15 @@ py::dict BND_BoundingBox::Encode() const
return d;
}

#if defined(NANOBIND)
// TODO: BND_BoundingBox::Decode for NANOBIND
#else
BND_BoundingBox* BND_BoundingBox::Decode(py::dict jsonObject)
{
ON_BoundingBox bbox;
py::dict d = jsonObject["Min"].cast<py::dict>();
py::dict d = py::cast<py::dict>(jsonObject["Min"]);
bbox.m_min = PointFromDict(d);
d = jsonObject["Max"].cast<py::dict>();
d = py::cast<py::dict>(jsonObject["Max"]);
bbox.m_max = PointFromDict(d);
return new BND_BoundingBox(bbox);
}
#endif

#endif

Expand Down
8 changes: 0 additions & 8 deletions src/bindings/bnd_brep.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#include "bindings.h"

#if defined(ON_PYTHON_COMPILE)
#if defined(NANOBIND)
namespace py = nanobind;
#else
namespace py = pybind11;
#endif
#endif

BND_BrepEdge::BND_BrepEdge(ON_BrepEdge* edge, const ON_ModelComponentReference* compref)
{
m_edge = edge;
Expand Down
31 changes: 9 additions & 22 deletions src/bindings/bnd_color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,34 @@

#if defined(ON_PYTHON_COMPILE)

#if defined(NANOBIND)
namespace py = nanobind;
#else
namespace py = pybind11;
#endif

BND_Color ON_Color_to_Binding(const ON_Color& color)
{
return py::make_tuple(color.Red(), color.Green(), color.Blue(), 255 - color.Alpha());
}

#if defined(NANOBIND)
// TODO: Binding_to_ON_Color is not implemented in nanobind
#else
ON_Color Binding_to_ON_Color(const BND_Color& color)
{
int r = color[0].cast<int>();
int g = color[1].cast<int>();
int b = color[2].cast<int>();
int a = color[3].cast<int>();
int r = py::cast<int>(color[0]);
int g = py::cast<int>(color[1]);
int b = py::cast<int>(color[2]);
int a = py::cast<int>(color[3]);
return ON_Color(r, g, b, 255-a);
}
#endif

BND_Color4f ON_4fColor_to_Binding(const ON_4fColor& color)
{
return py::make_tuple(color.Red(), color.Green(), color.Blue(), color.Alpha());
}

#if defined(NANOBIND)
// TODO: Binding_to_ON_4fColor is not implemented in nanobind
#else

ON_4fColor Binding_to_ON_4fColor(const BND_Color4f& color)
{
float r = color[0].cast<float>();
float g = color[1].cast<float>();
float b = color[2].cast<float>();
float a = color[3].cast<float>();
float r = py::cast<float>(color[0]);
float g = py::cast<float>(color[1]);
float b = py::cast<float>(color[2]);
float a = py::cast<float>(color[3]);
return ON_4fColor(r, g, b, a);
}
#endif

#endif

Expand Down
5 changes: 0 additions & 5 deletions src/bindings/bnd_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

#if defined(ON_PYTHON_COMPILE)

#if defined(NANOBIND)
namespace py = nanobind;
#else
namespace py = pybind11;
#endif
void initDefines(rh3dmpymodule& m);

py::dict PointToDict(const ON_3dPoint& point);
Expand Down
40 changes: 33 additions & 7 deletions src/bindings/bnd_extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1646,12 +1646,8 @@ struct PyBNDIterator {


#if defined(ON_PYTHON_COMPILE)
#if defined(NANOBIND)
namespace py = nanobind;
void initExtensionsBindings(py::module_& m){}
#else
namespace py = pybind11;
void initExtensionsBindings(py::module& m)

void initExtensionsBindings(rh3dmpymodule& m)
{
py::class_<BND_File3dmPlugInData>(m, "File3dmPlugInData")
;
Expand All @@ -1678,7 +1674,9 @@ void initExtensionsBindings(py::module& m)
py::class_<BND_ONXModel_ObjectTable>(m, "File3dmObjectTable")
.def("__len__", &BND_ONXModel_ObjectTable::Count)
.def("__getitem__", &BND_ONXModel_ObjectTable::ModelObjectAt)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_ONXModel_ObjectTable&, BND_FileObject*>(s.cast<BND_ONXModel_ObjectTable &>(), s); })
#endif
.def("AddPoint", &BND_ONXModel_ObjectTable::AddPoint1, py::arg("x"), py::arg("y"), py::arg("z"))
.def("AddPoint", &BND_ONXModel_ObjectTable::AddPoint2, py::arg("point"))
.def("AddPoint", &BND_ONXModel_ObjectTable::AddPoint4, py::arg("point"))
Expand Down Expand Up @@ -1714,7 +1712,9 @@ void initExtensionsBindings(py::module& m)
py::class_<BND_File3dmMaterialTable>(m, "File3dmMaterialTable")
.def("__len__", &BND_File3dmMaterialTable::Count)
.def("__getitem__", &BND_File3dmMaterialTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmMaterialTable&, BND_Material*>(s.cast<BND_File3dmMaterialTable &>(), s); })
#endif
.def("Add", &BND_File3dmMaterialTable::Add, py::arg("material"))
.def("FindIndex", &BND_File3dmMaterialTable::FindIndex, py::arg("index"))
.def("FindId", &BND_File3dmMaterialTable::FindId, py::arg("id"))
Expand All @@ -1730,7 +1730,9 @@ void initExtensionsBindings(py::module& m)
.def("__len__", &BND_File3dmLinetypeTable::Count)
.def("__getitem__", static_cast<BND_Linetype* (BND_File3dmLinetypeTable::*)(int)>(&BND_File3dmLinetypeTable::FindIndex))
.def("__getitem__", static_cast<BND_Linetype* (BND_File3dmLinetypeTable::*)(BND_UUID)>(&BND_File3dmLinetypeTable::FindId))
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmLinetypeTable&, BND_Linetype*>(s.cast<BND_File3dmLinetypeTable &>(), s); })
#endif
.def("Add", &BND_File3dmLinetypeTable::Add, py::arg("linetype"))
.def("Delete", &BND_File3dmLinetypeTable::Delete, py::arg("id"))
.def("FindIndex", &BND_File3dmLinetypeTable::FindIndex, py::arg("index"))
Expand All @@ -1751,7 +1753,9 @@ void initExtensionsBindings(py::module& m)
py::class_<BND_File3dmBitmapTable>(m, "File3dmBitmapTable")
.def("__len__", &BND_File3dmBitmapTable::Count)
.def("__getitem__", &BND_File3dmBitmapTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmBitmapTable&, BND_Bitmap*>(s.cast<BND_File3dmBitmapTable &>(), s); })
#endif
.def("Add", &BND_File3dmBitmapTable::Add, py::arg("bitmap"))
.def("FindIndex", &BND_File3dmBitmapTable::FindIndex, py::arg("index"))
.def("FindId", &BND_File3dmBitmapTable::FindId, py::arg("id"))
Expand All @@ -1765,7 +1769,9 @@ void initExtensionsBindings(py::module& m)
py::class_<BND_File3dmLayerTable>(m, "File3dmLayerTable")
.def("__len__", &BND_File3dmLayerTable::Count)
.def("__getitem__", &BND_File3dmLayerTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmLayerTable&, BND_Layer*>(s.cast<BND_File3dmLayerTable &>(), s); })
#endif
.def("Add", &BND_File3dmLayerTable::Add, py::arg("layer"))
.def("AddLayer", &BND_File3dmLayerTable::AddLayer, py::arg("name"), py::arg("color"))
.def("FindName", &BND_File3dmLayerTable::FindName, py::arg("name"), py::arg("parentId"))
Expand All @@ -1781,7 +1787,9 @@ void initExtensionsBindings(py::module& m)
py::class_<BND_File3dmGroupTable>(m, "File3dmGroupTable")
.def("__len__", &BND_File3dmGroupTable::Count)
.def("__getitem__", &BND_File3dmGroupTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmGroupTable&, BND_Group*>(s.cast<BND_File3dmGroupTable &>(), s); })
#endif
.def("Add", &BND_File3dmGroupTable::Add, py::arg("group"))
.def("Delete", &BND_File3dmGroupTable::Delete, py::arg("group"))
.def("Delete", &BND_File3dmGroupTable::DeleteIndex, py::arg("index"))
Expand All @@ -1799,7 +1807,9 @@ void initExtensionsBindings(py::module& m)
py::class_<BND_File3dmDimStyleTable>(m, "File3dmDimStyleTable")
.def("__len__", &BND_File3dmDimStyleTable::Count)
.def("__getitem__", &BND_File3dmDimStyleTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmDimStyleTable&, BND_DimensionStyle*>(s.cast<BND_File3dmDimStyleTable &>(), s); })
#endif
.def("Add", &BND_File3dmDimStyleTable::Add, py::arg("dimstyle"))
.def("FindIndex", &BND_File3dmDimStyleTable::FindIndex, py::arg("index"))
.def("FindId", &BND_File3dmDimStyleTable::FindId, py::arg("id"))
Expand All @@ -1813,7 +1823,9 @@ void initExtensionsBindings(py::module& m)
py::class_<BND_File3dmInstanceDefinitionTable>(m, "File3dmInstanceDefinitionTable")
.def("__len__", &BND_File3dmInstanceDefinitionTable::Count)
.def("__getitem__", &BND_File3dmInstanceDefinitionTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmInstanceDefinitionTable&, BND_InstanceDefinitionGeometry*>(s.cast<BND_File3dmInstanceDefinitionTable &>(), s); })
#endif
.def("AddInstanceDefinition", &BND_File3dmInstanceDefinitionTable::AddInstanceDefinition, py::arg("idef"))
.def("Add", &BND_File3dmInstanceDefinitionTable::Add, py::arg("name"), py::arg("description"), py::arg("url"), py::arg("urlTag"), py::arg("basePoint"), py::arg("geometry"), py::arg("attributes"))
.def("FindIndex", &BND_File3dmInstanceDefinitionTable::FindIndex, py::arg("index"))
Expand All @@ -1829,7 +1841,9 @@ void initExtensionsBindings(py::module& m)
.def("__len__", &BND_File3dmViewTable::Count)
.def("__getitem__", &BND_File3dmViewTable::GetItem)
.def("__setitem__", &BND_File3dmViewTable::SetItem)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmViewTable&, BND_ViewInfo*>(s.cast<BND_File3dmViewTable &>(), s); })
#endif
.def("Add", &BND_File3dmViewTable::Add, py::arg("view"))
;

Expand All @@ -1856,7 +1870,9 @@ void initExtensionsBindings(py::module& m)
py::class_<BND_File3dmEmbeddedFileTable>(m, "File3dmEmbeddedFileTable")
.def("__len__", &BND_File3dmEmbeddedFileTable::Count)
.def("__getitem__", &BND_File3dmEmbeddedFileTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmEmbeddedFileTable&, BND_File3dmEmbeddedFile*>(s.cast<BND_File3dmEmbeddedFileTable &>(), s); })
#endif
.def("Add", &BND_File3dmEmbeddedFileTable::Add, py::arg("embedded_file"))
.def("FindIndex", &BND_File3dmEmbeddedFileTable::FindIndex, py::arg("index"))
////.def("FindId", &BND_File3dmEmbeddedFileTable::FindId, py::arg("id"))
Expand All @@ -1869,7 +1885,9 @@ void initExtensionsBindings(py::module& m)
py::class_<BND_File3dmRenderContentTable>(m, "File3dmRenderContentTable")
.def("__len__", &BND_File3dmRenderContentTable::Count)
.def("__getitem__", &BND_File3dmRenderContentTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmRenderContentTable&, BND_File3dmRenderContent*>(s.cast<BND_File3dmRenderContentTable &>(), s); })
#endif
.def("Add", &BND_File3dmRenderContentTable::Add, py::arg("render_content"))
.def("FindIndex", &BND_File3dmRenderContentTable::FindIndex, py::arg("index"))
.def("FindId", &BND_File3dmRenderContentTable::FindId, py::arg("id"))
Expand All @@ -1885,7 +1903,9 @@ void initExtensionsBindings(py::module& m)
.def(py::init<const BND_File3dmPostEffectTable&>(), py::arg("other"))
.def("__len__", &BND_File3dmPostEffectTable::Count)
.def("__getitem__", &BND_File3dmPostEffectTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmPostEffectTable&, BND_File3dmPostEffect*>(s.cast<BND_File3dmPostEffectTable &>(), s); })
#endif
////.def("Add", &BND_File3dmRenderContentTable::Add, py::arg("render_content"))
.def("FindIndex", &BND_File3dmPostEffectTable::FindIndex, py::arg("index"))
.def("FindId", &BND_File3dmPostEffectTable::FindId, py::arg("id"))
Expand All @@ -1901,7 +1921,9 @@ void initExtensionsBindings(py::module& m)
.def(py::init<const BND_File3dmDecalTable&>(), py::arg("other"))
.def("__len__", &BND_File3dmDecalTable::Count)
.def("__getitem__", &BND_File3dmDecalTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmDecalTable&, BND_File3dmDecal*>(s.cast<BND_File3dmDecalTable&>(), s); })
#endif
.def("FindIndex", &BND_File3dmDecalTable::FindIndex, py::arg("index"))
;

Expand All @@ -1926,7 +1948,9 @@ void initExtensionsBindings(py::module& m)
py::class_<BND_File3dmShutLiningCurveTable>(m, "File3dmShutLiningCurveTable")
.def("__len__", &BND_File3dmShutLiningCurveTable::Count)
.def("__getitem__", &BND_File3dmShutLiningCurveTable::FindIndex)
#if !defined(NANOBIND)
.def("__iter__", [](py::object s) { return PyBNDIterator<BND_File3dmShutLiningCurveTable&, BND_File3dmShutLiningCurve*>(s.cast<BND_File3dmShutLiningCurveTable&>(), s); })
#endif
.def("Add", &BND_File3dmShutLiningCurveTable::Add, py::arg("id"))
.def("FindIndex", &BND_File3dmShutLiningCurveTable::FindIndex, py::arg("index"))
.def("FindId", &BND_File3dmShutLiningCurveTable::FindId, py::arg("id"))
Expand All @@ -1937,10 +1961,12 @@ void initExtensionsBindings(py::module& m)
.def_static("Read", &BND_ONXModel::Read, py::arg("path"))
.def_static("ReadNotes", &BND_ONXModel::ReadNotes, py::arg("path"))
.def_static("ReadArchiveVersion", &BND_ONXModel::ReadArchiveVersion, py::arg("path"))
/*
.def_static("FromByteArray", [](py::buffer b) {
py::buffer_info info = b.request();
return BND_ONXModel::FromByteArray(static_cast<int>(info.size), info.ptr);
})
*/
.def("Write", &BND_ONXModel::Write, py::arg("path"), py::arg("version")=0)
.def_property("StartSectionComments", &BND_ONXModel::GetStartSectionComments, &BND_ONXModel::SetStartSectionComments)
.def_property("ApplicationName", &BND_ONXModel::GetApplicationName, &BND_ONXModel::SetApplicationName)
Expand Down Expand Up @@ -1976,7 +2002,7 @@ void initExtensionsBindings(py::module& m)
.def("RdkXml", &BND_ONXModel::RdkXml)
;
}
#endif

#endif

#if defined(ON_WASM_COMPILE)
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/bnd_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ BND_DICT BND_ArchivableDictionary::DecodeToDictionary(BND_DICT jsonObject)
}
#if defined(ON_PYTHON_COMPILE)
if (!archive.EndReadDictionaryEntry())
throw py::cast_error();;
throw py::cast_error();
#else
archive.EndReadDictionaryEntry();
#endif
Expand Down
Loading

0 comments on commit 7bbce1e

Please sign in to comment.