1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_ShapeTools.h
4 // Created: 3 August 2015
5 // Author: Dmitry Bobylev
7 #include <GeomAlgoAPI_ShapeTools.h>
9 #include <GeomAlgoAPI_CompoundBuilder.h>
11 #include <BOPTools.hxx>
12 #include <BRepGProp.hxx>
13 #include <BRepTools.hxx>
14 #include <GProp_GProps.hxx>
15 #include <NCollection_Vector.hxx>
16 #include <TCollection_AsciiString.hxx>
17 #include <TopoDS_Builder.hxx>
18 #include <TopoDS_Shape.hxx>
19 #include <TopoDS_Shell.hxx>
21 //=================================================================================================
22 double GeomAlgoAPI_ShapeTools::volume(std::shared_ptr<GeomAPI_Shape> theShape)
28 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
32 BRepGProp::VolumeProperties(aShape, aGProps);
33 return aGProps.Mass();
36 //=================================================================================================
37 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(std::shared_ptr<GeomAPI_Shape> theShape)
41 return std::shared_ptr<GeomAPI_Pnt>();
43 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
45 return std::shared_ptr<GeomAPI_Pnt>();
47 BRepGProp::SurfaceProperties(aShape, aGProps);
48 gp_Pnt aCentre = aGProps.CentreOfMass();
49 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
52 //=================================================================================================
53 void GeomAlgoAPI_ShapeTools::combineFacesToShells(const ListOfShape& theFacesList,
54 ListOfShape& theShells,
55 ListOfShape& theFreeFaces)
57 if(theFacesList.empty()) {
61 // Adding all faces to compoud.
62 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(theFacesList);
63 if(!aFacesCompound.get()) {
67 // Map edges and faces.
68 const TopoDS_Shape& aFacesComp = aFacesCompound->impl<TopoDS_Shape>();
69 BOPCol_IndexedDataMapOfShapeListOfShape aMapEF;
70 BOPTools::MapShapesAndAncestors(aFacesComp, TopAbs_EDGE, TopAbs_FACE, aMapEF);
71 if(aMapEF.IsEmpty()) {
75 // Get all faces with common edges and free faces.
76 NCollection_Map<TopoDS_Shape> aFreeFaces;
77 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aFacesWithCommonEdges;
78 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapEF); anIter.More(); anIter.Next()) {
79 const TopoDS_Shape& aShape = anIter.Key();
80 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
81 if(aListOfShape.IsEmpty()) {
84 else if(aListOfShape.Size() == 1) {
85 aFreeFaces.Add(aListOfShape.First());
88 NCollection_Map<TopoDS_Shape> aTempMap;
89 aTempMap.Add(aListOfShape.First());
90 aTempMap.Add(aListOfShape.Last());
91 aFreeFaces.Remove(aListOfShape.First());
92 aFreeFaces.Remove(aListOfShape.Last());
94 for(NCollection_Map<TopoDS_Shape>::Iterator aTempIter(aTempMap); aTempIter.More(); aTempIter.Next()) {
95 const TopoDS_Shape& aTempShape = aTempIter.Value();
96 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapEF); anIter.More(); anIter.Next()) {
97 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
98 if(aTempListOfShape.IsEmpty()) {
100 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
101 aTempListOfShape.Clear();
102 } else if(aTempListOfShape.Size() > 1) {
103 if(aTempListOfShape.First() == aTempShape) {
104 aTempMap.Add(aTempListOfShape.Last());
105 aFreeFaces.Remove(aTempListOfShape.Last());
106 aTempListOfShape.Clear();
107 } else if(aTempListOfShape.Last() == aTempShape) {
108 aTempMap.Add(aTempListOfShape.First());
109 aFreeFaces.Remove(aTempListOfShape.First());
110 aTempListOfShape.Clear();
115 aFacesWithCommonEdges.Append(aTempMap);
119 // Make shells from faces with common edges.
120 NCollection_Vector<TopoDS_Shape> aShells;
121 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aFacesWithCommonEdges); anIter.More(); anIter.Next()) {
123 TopoDS_Builder aBuilder;
124 aBuilder.MakeShell(aShell);
125 const NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.Value();
126 for(NCollection_Map<TopoDS_Shape>::Iterator aShIter(aShapesMap); aShIter.More(); aShIter.Next()) {
127 const TopoDS_Shape& aFace = aShIter.Value();
128 aBuilder.Add(aShell, aFace);
130 std::shared_ptr<GeomAPI_Shape> aGeomShell(std::make_shared<GeomAPI_Shape>());
131 aGeomShell->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShell));
132 theShells.push_back(aGeomShell);
135 // Adding free faces.
136 for(NCollection_Map<TopoDS_Shape>::Iterator aShIter(aFreeFaces); aShIter.More(); aShIter.Next()) {
137 const TopoDS_Shape& aFace = aShIter.Value();
138 std::shared_ptr<GeomAPI_Shape> aGeomFace(std::make_shared<GeomAPI_Shape>());
139 aGeomFace->setImpl<TopoDS_Shape>(new TopoDS_Shape(aFace));
140 theFreeFaces.push_back(aGeomFace);