From 42c6601aaeca4857be0ad1c68dac3fa169a6c880 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Tue, 27 Jul 2021 20:59:41 +0200 Subject: [PATCH] colors from python-occ --- libsrc/occ/occgenmesh.cpp | 9 ++++++- libsrc/occ/occgeom.cpp | 1 + libsrc/occ/occgeom.hpp | 1 + libsrc/occ/python_occ.cpp | 56 +++++++++++++++++++++++++++++++++++---- 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/libsrc/occ/occgenmesh.cpp b/libsrc/occ/occgenmesh.cpp index aca4fbfe..e82fea92 100644 --- a/libsrc/occ/occgenmesh.cpp +++ b/libsrc/occ/occgenmesh.cpp @@ -410,7 +410,14 @@ namespace netgen } else { - mesh.GetFaceDescriptor(facenr).SetSurfColour({0.0,1.0,0.0}); + auto it = OCCGeometry::global_shape_cols.find(face.TShape()); + if (it != OCCGeometry::global_shape_cols.end()) + { + Vec<3> col = it->second; + mesh.GetFaceDescriptor(facenr).SetSurfColour(col); + } + else + mesh.GetFaceDescriptor(facenr).SetSurfColour({0.0,1.0,0.0}); } if(geom.fnames.Size()>=facenr) diff --git a/libsrc/occ/occgeom.cpp b/libsrc/occ/occgeom.cpp index b46749a1..2edad454 100644 --- a/libsrc/occ/occgeom.cpp +++ b/libsrc/occ/occgeom.cpp @@ -42,6 +42,7 @@ namespace netgen { std::map OCCGeometry::global_shape_names; + std::map> OCCGeometry::global_shape_cols; OCCGeometry::OCCGeometry(const TopoDS_Shape& _shape) diff --git a/libsrc/occ/occgeom.hpp b/libsrc/occ/occgeom.hpp index 411f5ac1..a07f6d45 100644 --- a/libsrc/occ/occgeom.hpp +++ b/libsrc/occ/occgeom.hpp @@ -210,6 +210,7 @@ namespace netgen public: static std::map global_shape_names; + static std::map> global_shape_cols; TopoDS_Shape shape; TopTools_IndexedMapOfShape fmap, emap, vmap, somap, shmap, wmap; diff --git a/libsrc/occ/python_occ.cpp b/libsrc/occ/python_occ.cpp index 7fb99e15..c3cdf742 100644 --- a/libsrc/occ/python_occ.cpp +++ b/libsrc/occ/python_occ.cpp @@ -313,6 +313,18 @@ DLL_HEADER void ExportNgOCC(py::module &m) OCCGeometry::global_shape_names[e.Current().TShape()] = name; return shape; }) + + .def_property("col", [](const TopoDS_Shape & self) { + auto it = OCCGeometry::global_shape_cols.find(self.TShape()); + Vec<3> col(0.2, 0.2, 0.2); + if (it != OCCGeometry::global_shape_cols.end()) + col = it->second; + return std::vector ( { col(0), col(1), col(2) } ); + }, [](const TopoDS_Shape & self, std::vector c) { + Vec<3> col(c[0], c[1], c[2]); + OCCGeometry::global_shape_cols[self.TShape()] = col; + }) + .def("__add__", [] (const TopoDS_Shape & shape1, const TopoDS_Shape & shape2) { return BRepAlgoAPI_Fuse(shape1, shape2).Shape(); @@ -355,7 +367,38 @@ DLL_HEADER void ExportNgOCC(py::module &m) }) .def("__sub__", [] (const TopoDS_Shape & shape1, const TopoDS_Shape & shape2) { - return BRepAlgoAPI_Cut(shape1, shape2).Shape(); + // return BRepAlgoAPI_Cut(shape1, shape2).Shape(); + + BRepAlgoAPI_Cut builder(shape1, shape2); +#ifdef OCC_HAVE_HISTORY + Handle(BRepTools_History) history = builder.History (); + + for (auto s : { shape1, shape2 }) + for (TopExp_Explorer e(s, TopAbs_FACE); e.More(); e.Next()) + { + const string & name = OCCGeometry::global_shape_names[e.Current().TShape()]; + for (auto s : history->Modified(e.Current())) + OCCGeometry::global_shape_names[s.TShape()] = name; + + auto it = OCCGeometry::global_shape_cols.find(e.Current().TShape()); + if (it != OCCGeometry::global_shape_cols.end()) + for (auto s : history->Modified(e.Current())) + OCCGeometry::global_shape_cols[s.TShape()] = it->second; + } + + /* + for (TopExp_Explorer e(shape2, TopAbs_FACE); e.More(); e.Next()) + { + auto it = OCCGeometry::global_shape_cols[e.Current().TShape()]; + if (it != OCCGeometry::global_shape_cols.end()) + for (auto s : history->Modified(e.Current())) + OCCGeometry::global_shape_cols[s.TShape()] = it->second; + } + */ +#endif // OCC_HAVE_HISTORY + + + return builder.Shape(); }) ; @@ -376,8 +419,12 @@ DLL_HEADER void ExportNgOCC(py::module &m) { BOPAlgo_Builder builder; for (auto & s : shapes) - for (TopExp_Explorer e(s, TopAbs_SOLID); e.More(); e.Next()) - builder.AddArgument(e.Current()); + { + for (TopExp_Explorer e(s, TopAbs_SOLID); e.More(); e.Next()) + builder.AddArgument(e.Current()); + if (s.ShapeType() == TopAbs_FACE) + builder.AddArgument(s); + } builder.Perform(); @@ -388,8 +435,7 @@ DLL_HEADER void ExportNgOCC(py::module &m) for (TopExp_Explorer e(s, TopAbs_SOLID); e.More(); e.Next()) { auto name = OCCGeometry::global_shape_names[e.Current().TShape()]; - TopTools_ListOfShape modlist = history->Modified(e.Current()); - for (auto mods : modlist) + for (auto mods : history->Modified(e.Current())) OCCGeometry::global_shape_names[mods.TShape()] = name; } #endif // OCC_HAVE_HISTORY