]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp
Salome HOME
Compsolids creation in revolution
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeTools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_ShapeTools.h
4 // Created:     3 August 2015
5 // Author:      Dmitry Bobylev
6
7 #include <GeomAlgoAPI_ShapeTools.h>
8
9 #include <GeomAlgoAPI_CompoundBuilder.h>
10
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>
20
21 //=================================================================================================
22 double GeomAlgoAPI_ShapeTools::volume(std::shared_ptr<GeomAPI_Shape> theShape)
23 {
24   GProp_GProps aGProps;
25   if(!theShape) {
26     return 0.0;
27   }
28   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
29   if(aShape.IsNull()) {
30     return 0.0;
31   }
32   BRepGProp::VolumeProperties(aShape, aGProps);
33   return aGProps.Mass();
34 }
35
36 //=================================================================================================
37 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(std::shared_ptr<GeomAPI_Shape> theShape)
38 {
39   GProp_GProps aGProps;
40   if(!theShape) {
41     return std::shared_ptr<GeomAPI_Pnt>();
42   }
43   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
44   if(aShape.IsNull()) {
45     return std::shared_ptr<GeomAPI_Pnt>();
46   }
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()));
50 }
51
52 //=================================================================================================
53 void GeomAlgoAPI_ShapeTools::combineFacesToShells(const ListOfShape& theFacesList,
54                                                   ListOfShape& theShells,
55                                                   ListOfShape& theFreeFaces)
56 {
57   if(theFacesList.empty()) {
58     return;
59   }
60
61   // Adding all faces to compoud.
62   std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(theFacesList);
63   if(!aFacesCompound.get()) {
64     return;
65   }
66
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()) {
72     return;
73   }
74
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()) {
82       continue;
83     }
84     else if(aListOfShape.Size() == 1) {
85       aFreeFaces.Add(aListOfShape.First());
86       aListOfShape.Clear();
87     } else {
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());
93       aListOfShape.Clear();
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()) {
99             continue;
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();
111             }
112           }
113         }
114       }
115       aFacesWithCommonEdges.Append(aTempMap);
116     }
117   }
118
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()) {
122     TopoDS_Shell aShell;
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);
129     }
130     std::shared_ptr<GeomAPI_Shape> aGeomShell(new GeomAPI_Shape);
131     aGeomShell->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShell));
132     theShells.push_back(aGeomShell);
133   }
134
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(new GeomAPI_Shape);
139     aGeomFace->setImpl<TopoDS_Shape>(new TopoDS_Shape(aFace));
140     theFreeFaces.push_back(aGeomFace);
141   }
142 }