How to create TopTools_ListOfShape of two or more shapes in OpenCASCADE
TopTools_ListOfShape
is just another name for NCollection_List<TopoDS_Shape>
.
Using my OCCUtils library this is really easy:
#include <TopTools_ListOfShape.hxx>
#include <occutils/ListUtils.hxx>
using namespace OCCUtils;
/* ... */
TopoDS_Solid solid1 = /* ... */;
TopoDS_Solid solid2 = /* ... */;
TopTools_ListOfShape shapes = ListUtils::ToOCCList({solid1, solid2});
ListUtils::ToOCCList()
takes almost anything as an argument, a std::initializer_list
(this is the bracket syntax from the example above) but also any type of STL container like a std::vector
or a std::list
, and converts it into a NCollection_List<T>
:
#include <TopTools_ListOfShape.hxx>
#include <occutils/ListUtils.hxx>
#include <vector>
using namespace OCCUtils;
/* ... */
TopoDS_Solid solid1 = /* ... */;
TopoDS_Solid solid2 = /* ... */;
std::vector<TopoDS_Shape> shapeVector({solid1, solid2});
TopTools_ListOfShape shapes = ListUtils::ToOCCList(shapeVector);
In case you don’t want to use OCCUtils, this is how you can create a TopTools_ListOfShape
manually:
TopTools_ListOfShape list;
list.Append(shape1);
list.Append(shape2);
Since NCollection_List
and therefore also TopTools_ListOfShape
is a linked list internally, you can also use list.Prepend(shape);
to add a shape to the front of the list without any performance penalty.
In case you want to create the list from a std::vector
or any similar STL container, use this snippet:
std::vector<TopoDS_Shape>& shapeVector = /* ... */;
TopTools_ListOfShape list;
for(const TopoDS_Shape& shape : shapeVector) {
list.Append(arg);
}
After creating the container, you can iterate it using the C++11 range-based for-loop:
TopTools_ListOfShape myShapes = /* ... */;
// Iterate myShapes
for(const TopoDS_Shape& shape : myShapes) {
/* ... */
}
SeeĀ How to iterate TopTools_ListOfShape in OpenCASCADE andĀ How to iterate NCollection_List in OpenCASCADE for more details on that.