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>
26 #include <GeomAlgoAPI_CompoundBuilder.h>
27 #include <GeomAlgoAPI_ShapeTools.h>
29 #include <GeomAPI_PlanarEdges.h>
30 #include <GeomAPI_ShapeIterator.h>
32 //==================================================================================================
33 void FeaturesPlugin_Tools::loadModifiedShapes(ResultBodyPtr theResultBody,
34 const ListOfShape& theBaseShapes,
35 const ListOfShape& theTools,
36 const GeomMakeShapePtr& theMakeShape,
37 const GeomShapePtr theResultShape,
38 const std::string& theNamePrefix)
40 theResultBody->storeModified(theBaseShapes, theResultShape, theMakeShape);
42 ListOfShape aShapes = theBaseShapes;
43 ListOfShape::const_iterator aToolIter = theTools.cbegin();
44 for(; aToolIter != theTools.cend(); aToolIter++)
45 aShapes.push_back(*aToolIter);
47 for (ListOfShape::const_iterator anIter = aShapes.begin(); anIter != aShapes.end(); ++anIter)
49 theResultBody->loadModifiedShapes(theMakeShape, *anIter, GeomAPI_Shape::VERTEX, theNamePrefix);
50 theResultBody->loadModifiedShapes(theMakeShape, *anIter, GeomAPI_Shape::EDGE, theNamePrefix);
51 theResultBody->loadModifiedShapes(theMakeShape, *anIter, GeomAPI_Shape::FACE, theNamePrefix);
55 //==================================================================================================
56 void FeaturesPlugin_Tools::loadModifiedShapes(ResultBodyPtr theResultBody,
57 const GeomShapePtr& theBaseShape,
58 const GeomMakeShapePtr& theMakeShape,
59 const std::string theName)
61 switch(theBaseShape->shapeType()) {
62 case GeomAPI_Shape::COMPOUND: {
63 for(GeomAPI_ShapeIterator anIt(theBaseShape); anIt.more(); anIt.next())
65 loadModifiedShapes(theResultBody,
72 case GeomAPI_Shape::COMPSOLID:
73 case GeomAPI_Shape::SOLID:
74 case GeomAPI_Shape::SHELL: {
75 theResultBody->loadModifiedShapes(theMakeShape,
80 case GeomAPI_Shape::FACE:
81 case GeomAPI_Shape::WIRE: {
82 theResultBody->loadModifiedShapes(theMakeShape,
87 case GeomAPI_Shape::EDGE: {
88 theResultBody->loadModifiedShapes(theMakeShape,
90 GeomAPI_Shape::VERTEX,
96 //==================================================================================================
97 void FeaturesPlugin_Tools::loadDeletedShapes(ResultBodyPtr theResultBody,
98 const GeomShapePtr theBaseShape,
99 const ListOfShape& theTools,
100 const GeomMakeShapePtr& theMakeShape,
101 const GeomShapePtr theResultShapesCompound)
103 ListOfShape aShapes = theTools;
104 if (theBaseShape.get())
105 aShapes.push_front(theBaseShape);
107 for (ListOfShape::const_iterator anIter = aShapes.begin(); anIter != aShapes.end(); anIter++)
109 theResultBody->loadDeletedShapes(theMakeShape,
111 GeomAPI_Shape::VERTEX,
112 theResultShapesCompound);
113 theResultBody->loadDeletedShapes(theMakeShape,
116 theResultShapesCompound);
117 theResultBody->loadDeletedShapes(theMakeShape,
120 theResultShapesCompound);
124 //==================================================================================================
125 void FeaturesPlugin_Tools::loadDeletedShapes(
126 std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
127 const ListOfShape& theTools,
128 const GeomShapePtr theResultShapesCompound)
130 for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
131 anIt != theResultBaseAlgoList.end();
134 ResultBaseAlgo& aRCA = *anIt;
135 loadDeletedShapes(aRCA.resultBody,
139 theResultShapesCompound);
143 //==================================================================================================
144 bool FeaturesPlugin_Tools::getShape(const AttributeSelectionListPtr theSelectionList,
145 const bool theShareTopology,
146 ListOfShape& theShapesList,
147 std::string& theError)
149 theShapesList.clear();
151 ListOfShape aBaseFacesList;
152 std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
153 if(!theSelectionList.get()) {
154 theError = "Error: Could not get base objects selection list.";
157 if(theSelectionList->size() == 0) {
158 theError = "Error: Base objects list is empty.";
161 for(int anIndex = 0; anIndex < theSelectionList->size(); anIndex++) {
162 AttributeSelectionPtr aBaseObjectSelection = theSelectionList->value(anIndex);
163 if(!aBaseObjectSelection.get()) {
164 theError = "Error: Selected base object is empty.";
167 GeomShapePtr aBaseShape = aBaseObjectSelection->value();
168 if(aBaseShape.get() && !aBaseShape->isNull()) {
169 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
170 if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
171 theError = "Error: Selected shapes has unsupported type.";
174 ResultConstructionPtr aConstruction =
175 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
176 if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
177 aST == GeomAPI_Shape::WIRE) {
178 // It is a wire on the sketch, store it to make face later.
179 aSketchWiresMap[aConstruction].push_back(aBaseShape);
182 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
183 theShapesList.push_back(aBaseShape);
186 // This may be the whole sketch result selected, check and get faces.
187 ResultConstructionPtr aConstruction =
188 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
189 if(!aConstruction.get()) {
190 theError = "Error: Selected sketches does not have results.";
193 int aFacesNum = aConstruction->facesNum();
195 // Probably it can be construction.
196 aBaseShape = aConstruction->shape();
197 if(aBaseShape.get() && !aBaseShape->isNull()) {
198 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
199 if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE &&
200 aST != GeomAPI_Shape::WIRE &&
201 aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
202 theError = "Error: Selected shapes has unsupported type.";
205 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
206 theShapesList.push_back(aBaseShape);
209 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
210 GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
211 if(!aBaseFace.get() || aBaseFace->isNull()) {
212 theError = "Error: One of the faces on selected sketch is null.";
215 aBaseFacesList.push_back(aBaseFace);
221 // Make faces from sketch wires.
222 for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
223 anIt != aSketchWiresMap.cend(); ++anIt) {
224 const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
225 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
226 const ListOfShape& aWiresList = (*anIt).second;
228 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
229 aSketchPlanarEdges->norm(),
232 aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
235 // Searching faces with common edges.
236 if(theShareTopology && aBaseFacesList.size() > 1) {
237 GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
238 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, theShapesList);
240 theShapesList.insert(theShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());