1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_Tools.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_ResultPart.h>
26 #include <ModelAPI_Tools.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAPI_PlanarEdges.h>
32 #include <GeomAPI_ShapeIterator.h>
34 //==================================================================================================
35 void FeaturesPlugin_Tools::loadModifiedShapes(ResultBodyPtr theResultBody,
36 const ListOfShape& theBaseShapes,
37 const ListOfShape& theTools,
38 const GeomMakeShapePtr& theMakeShape,
39 const GeomShapePtr theResultShape,
40 const std::string& theNamePrefix)
42 theResultBody->storeModified(theBaseShapes, theResultShape, theMakeShape);
44 ListOfShape aShapes = theBaseShapes;
45 ListOfShape::const_iterator aToolIter = theTools.cbegin();
46 for(; aToolIter != theTools.cend(); aToolIter++)
47 aShapes.push_back(*aToolIter);
49 for (ListOfShape::const_iterator anIter = aShapes.begin(); anIter != aShapes.end(); ++anIter)
51 theResultBody->loadModifiedShapes(theMakeShape, *anIter, GeomAPI_Shape::VERTEX, theNamePrefix);
52 theResultBody->loadModifiedShapes(theMakeShape, *anIter, GeomAPI_Shape::EDGE, theNamePrefix);
53 theResultBody->loadModifiedShapes(theMakeShape, *anIter, GeomAPI_Shape::FACE, theNamePrefix);
57 //==================================================================================================
58 void FeaturesPlugin_Tools::loadModifiedShapes(ResultBodyPtr theResultBody,
59 const GeomShapePtr& theBaseShape,
60 const GeomMakeShapePtr& theMakeShape,
61 const std::string theName)
63 switch(theBaseShape->shapeType()) {
64 case GeomAPI_Shape::COMPOUND: {
65 for(GeomAPI_ShapeIterator anIt(theBaseShape); anIt.more(); anIt.next())
67 loadModifiedShapes(theResultBody,
74 case GeomAPI_Shape::COMPSOLID:
75 case GeomAPI_Shape::SOLID:
76 case GeomAPI_Shape::SHELL: {
77 theResultBody->loadModifiedShapes(theMakeShape,
82 case GeomAPI_Shape::FACE:
83 case GeomAPI_Shape::WIRE: {
84 theResultBody->loadModifiedShapes(theMakeShape,
89 case GeomAPI_Shape::EDGE: {
90 theResultBody->loadModifiedShapes(theMakeShape,
92 GeomAPI_Shape::VERTEX,
98 //==================================================================================================
99 void FeaturesPlugin_Tools::loadDeletedShapes(ResultBodyPtr theResultBody,
100 const GeomShapePtr theBaseShape,
101 const ListOfShape& theTools,
102 const GeomMakeShapePtr& theMakeShape,
103 const GeomShapePtr theResultShapesCompound)
105 ListOfShape aShapes = theTools;
106 if (theBaseShape.get())
107 aShapes.push_front(theBaseShape);
109 for (ListOfShape::const_iterator anIter = aShapes.begin(); anIter != aShapes.end(); anIter++)
111 theResultBody->loadDeletedShapes(theMakeShape,
113 GeomAPI_Shape::VERTEX,
114 theResultShapesCompound);
115 theResultBody->loadDeletedShapes(theMakeShape,
118 theResultShapesCompound);
119 theResultBody->loadDeletedShapes(theMakeShape,
122 theResultShapesCompound);
123 // store information about deleted solids because of unittest TestBooleanCommon_SolidsHistory
124 // on OCCT 7.4.0 : common produces modified compsolid, so, move to the end for removed solids
125 // starts to produce whole compsolid
126 theResultBody->loadDeletedShapes(theMakeShape,
128 GeomAPI_Shape::SOLID,
129 theResultShapesCompound);
133 //==================================================================================================
134 void FeaturesPlugin_Tools::loadDeletedShapes(
135 std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
136 const ListOfShape& theTools,
137 const GeomShapePtr theResultShapesCompound)
139 for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
140 anIt != theResultBaseAlgoList.end();
143 ResultBaseAlgo& aRCA = *anIt;
144 loadDeletedShapes(aRCA.resultBody,
148 theResultShapesCompound);
152 //==================================================================================================
153 bool FeaturesPlugin_Tools::getShape(const AttributeSelectionListPtr theSelectionList,
154 const bool theShareTopology,
155 ListOfShape& theShapesList,
156 std::string& theError)
158 theShapesList.clear();
160 ListOfShape aBaseFacesList;
161 std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
162 if(!theSelectionList.get()) {
163 theError = "Error: Could not get base objects selection list.";
166 if(theSelectionList->size() == 0) {
167 theError = "Error: Base objects list is empty.";
170 for(int anIndex = 0; anIndex < theSelectionList->size(); anIndex++) {
171 AttributeSelectionPtr aBaseObjectSelection = theSelectionList->value(anIndex);
172 if(!aBaseObjectSelection.get()) {
173 theError = "Error: Selected base object is empty.";
176 GeomShapePtr aBaseShape = aBaseObjectSelection->value();
177 if(aBaseShape.get() && !aBaseShape->isNull()) {
178 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
179 if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
180 theError = "Error: Selected shapes has unsupported type.";
183 ResultConstructionPtr aConstruction =
184 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
185 if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
186 aST == GeomAPI_Shape::WIRE) {
187 // It is a wire on the sketch, store it to make face later.
188 aSketchWiresMap[aConstruction].push_back(aBaseShape);
191 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
192 theShapesList.push_back(aBaseShape);
195 // This may be the whole sketch result selected, check and get faces.
196 ResultConstructionPtr aConstruction =
197 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
198 if(!aConstruction.get()) {
199 theError = "Error: Selected sketches does not have results.";
202 int aFacesNum = aConstruction->facesNum();
204 // Probably it can be construction.
205 aBaseShape = aConstruction->shape();
206 if(aBaseShape.get() && !aBaseShape->isNull()) {
207 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
208 if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE &&
209 aST != GeomAPI_Shape::WIRE &&
210 aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
211 theError = "Error: Selected shapes has unsupported type.";
214 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
215 theShapesList.push_back(aBaseShape);
218 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
219 GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
220 if(!aBaseFace.get() || aBaseFace->isNull()) {
221 theError = "Error: One of the faces on selected sketch is null.";
224 aBaseFacesList.push_back(aBaseFace);
230 // Make faces from sketch wires.
231 for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
232 anIt != aSketchWiresMap.cend(); ++anIt) {
233 const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
234 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
235 const ListOfShape& aWiresList = (*anIt).second;
237 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
238 aSketchPlanarEdges->norm(),
241 aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
244 // Searching faces with common edges.
245 if(theShareTopology && aBaseFacesList.size() > 1) {
246 GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
247 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, theShapesList);
249 theShapesList.insert(theShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());
254 //==================================================================================================
255 bool FeaturesPlugin_Tools::shapesFromSelectionList(
256 const std::shared_ptr<ModelAPI_AttributeSelectionList> theSelectionList,
257 const bool theStoreFullHierarchy,
258 GeomAPI_ShapeHierarchy& theHierarchy,
259 std::list<ResultPtr>& theParts)
261 int aSize = theSelectionList->size();
262 for (int anObjectsIndex = 0; anObjectsIndex < aSize; anObjectsIndex++) {
263 AttributeSelectionPtr anObjectAttr = theSelectionList->value(anObjectsIndex);
264 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
265 if (!anObject.get()) { // may be for not-activated parts
268 ResultPtr aContext = anObjectAttr->context();
269 if (aContext && aContext->groupName() == ModelAPI_ResultPart::group())
270 theParts.push_back(aContext);
272 // store full shape hierarchy for the corresponding version only
273 theHierarchy.addObject(anObject);
274 if (theStoreFullHierarchy)
275 ModelAPI_Tools::fillShapeHierarchy(anObject, aContext, theHierarchy);