Erstellen von TopTools_ListOfShape aus zwei oder mehr Shapes in OpenCASCADE

English Deutsch

TopTools_ListOfShape ist nur ein anderer Name für NCollection_List<TopoDS_Shape>.

Using my OCCUtils library this is really easy:

list_to_occ_list.cpp
#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() akzeptiert fast alles als Argument, ein std::initializer_list (dies ist die Klammern-Syntax aus dem obigen Beispiel), aber auch jeden Typ von STL-Container wie einen std::vector oder eine std::list, und konvertiert es in ein NCollection_List<T>:

list_to_occ_list.cpp
#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);

Falls du OCCUtils nicht verwenden möchtest, so kannst du ein TopTools_ListOfShape manuell erstellen:

manual_append.cpp
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.

Falls du die Liste aus einem std::vector oder einem ähnlichen STL-Container erstellen möchtest, verwende dieses Snippet:

manual_append.cpp
std::vector<TopoDS_Shape>& shapeVector = /* ... */;

TopTools_ListOfShape list;
for(const TopoDS_Shape& shape : shapeVector) {
    list.Append(shape);
}

Nach dem Erstellen des Containers kannst du es mit dem C++11 range-based for-loop iterieren:

iterate_listofshape.cpp
TopTools_ListOfShape myShapes = /* ... */;

// Iteriere 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.


Check out similar posts by category: C/C++, OpenCASCADE