1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_Pipe.h"
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_Pipe.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_Tools.h>
35 #include <GeomAPI_PlanarEdges.h>
36 #include <GeomAPI_ShapeExplorer.h>
41 static void storeSubShape(ResultBodyPtr theResultBody,
42 const GeomShapePtr theShape,
43 const GeomAPI_Shape::ShapeType theType,
44 const std::string theName,
47 //==================================================================================================
48 FeaturesPlugin_Pipe::FeaturesPlugin_Pipe()
52 //==================================================================================================
53 void FeaturesPlugin_Pipe::initAttributes()
55 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
57 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
58 data()->addAttribute(PATH_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
60 data()->addAttribute(BINORMAL_ID(), ModelAPI_AttributeSelection::typeId());
62 data()->addAttribute(LOCATIONS_ID(), ModelAPI_AttributeSelectionList::typeId());
63 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATIONS_ID());
66 //==================================================================================================
67 void FeaturesPlugin_Pipe::execute()
69 // Getting creation method.
70 std::string aCreationMethod = string(CREATION_METHOD())->value();
72 // Getting base objects.
73 ListOfShape aBaseShapesList, aBaseFacesList;
74 std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
75 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
76 if(!aBaseObjectsSelectionList.get()) {
77 setError("Error: Could not get base objects selection list.");
80 if(aBaseObjectsSelectionList->size() == 0) {
81 setError("Error: Base objects list is empty.");
84 for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
85 AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
86 if(!aBaseObjectSelection.get()) {
87 setError("Error: One of the selected base objects is empty.");
90 std::shared_ptr<GeomAPI_Shape> aBaseShape = aBaseObjectSelection->value();
91 if(aBaseShape.get() && !aBaseShape->isNull()) {
92 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
93 ResultConstructionPtr aConstruction =
94 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
95 if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
96 aST == GeomAPI_Shape::WIRE) {
97 // It is a wire on the sketch, store it to make face later.
98 aSketchWiresMap[aConstruction].push_back(aBaseShape);
101 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
102 aBaseShapesList.push_back(aBaseShape);
105 // This may be the whole sketch result selected, check and get faces.
106 ResultConstructionPtr aConstruction =
107 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
108 if(!aConstruction.get()) {
109 setError("Error: One of selected sketches does not have results.");
112 int aFacesNum = aConstruction->facesNum();
114 // Probably it can be construction.
115 aBaseShape = aConstruction->shape();
116 if(aBaseShape.get() && !aBaseShape->isNull()) {
117 aBaseShape->shapeType() == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
118 aBaseShapesList.push_back(aBaseShape);
121 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
122 std::shared_ptr<GeomAPI_Shape> aBaseFace =
123 std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
124 if(!aBaseFace.get() || aBaseFace->isNull()) {
125 setError("Error: One of the faces on selected sketch is Null.");
128 aBaseFacesList.push_back(aBaseFace);
134 // Make faces from sketch wires.
135 for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
136 anIt != aSketchWiresMap.cend(); ++anIt) {
137 const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
138 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
139 const ListOfShape& aWiresList = (*anIt).second;
141 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
142 aSketchPlanarEdges->norm(),
145 aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
148 // Searching faces with common edges.
149 if(aCreationMethod == CREATION_METHOD_SIMPLE()) {
151 ListOfShape aFreeFaces;
152 std::shared_ptr<GeomAPI_Shape> aFacesCompound =
153 GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
154 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL,
155 aShells, aFreeFaces);
156 aBaseShapesList.insert(aBaseShapesList.end(), aFreeFaces.begin(), aFreeFaces.end());
157 aBaseShapesList.insert(aBaseShapesList.end(), aShells.begin(), aShells.end());
159 aBaseShapesList.insert(aBaseShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());
163 AttributeSelectionPtr aPathSelection = selection(PATH_OBJECT_ID());
164 if(!aPathSelection.get()) {
165 setError("Error: Path selection is empty.");
168 std::shared_ptr<GeomAPI_Shape> aPathShape =
169 std::dynamic_pointer_cast<GeomAPI_Shape>(aPathSelection->value());
170 if(!aPathShape.get() && aPathSelection->context().get()) {
171 // Probaply it is a construction.
172 aPathShape = aPathSelection->context()->shape();
174 if(!aPathShape.get() || aPathShape->isNull()) {
175 setError("Error: Path shape is null.");
180 std::shared_ptr<GeomAPI_Shape> aBiNormal;
181 if(aCreationMethod == CREATION_METHOD_BINORMAL()) {
182 AttributeSelectionPtr aBiNormalSelection = selection(BINORMAL_ID());
183 if(!aBiNormalSelection.get()) {
184 setError("Error: Bi-Normal selection is empty.");
187 aBiNormal = std::dynamic_pointer_cast<GeomAPI_Shape>(aBiNormalSelection->value());
188 if(!aBiNormal.get() && aBiNormalSelection->context().get()) {
189 // Probably it is a construction.
190 aBiNormal = aBiNormalSelection->context()->shape();
192 if(!aBiNormal.get() || aBiNormal->isNull()) {
193 setError("Error: Bi-Normal shape is null.");
198 // Getting locations.
199 ListOfShape aLocations;
200 if(aCreationMethod == CREATION_METHOD_LOCATIONS()) {
201 AttributeSelectionListPtr aLocationsSelectionList = selectionList(LOCATIONS_ID());
202 if(!aLocationsSelectionList.get()) {
203 setError("Error: Could not get locations selection list.");
206 for(int anIndex = 0; anIndex < aLocationsSelectionList->size(); anIndex++) {
207 AttributeSelectionPtr aLocationSelection = aLocationsSelectionList->value(anIndex);
208 if(!aLocationSelection.get()) {
209 setError("Error: One of the selected location is empty.");
212 std::shared_ptr<GeomAPI_Shape> aLocationShape = aLocationSelection->value();
213 if(!aLocationShape.get() && aLocationSelection->context().get()) {
214 // Probably it is a construction.
215 aLocationShape = aLocationSelection->context()->shape();
217 if(!aLocationShape.get() || aLocationShape->isNull()) {
218 setError("Error: One of the selected location shape is null.");
221 aLocations.push_back(aLocationShape);
225 // Generating result for each object.
226 int aResultIndex = 0;
228 if(aCreationMethod == CREATION_METHOD_SIMPLE() ||
229 aCreationMethod == CREATION_METHOD_BINORMAL()) {
230 for(ListOfShape::const_iterator
231 anIter = aBaseShapesList.cbegin(); anIter != aBaseShapesList.cend(); anIter++) {
232 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
234 std::shared_ptr<GeomAlgoAPI_Pipe> aPipeAlgo(
235 aCreationMethod == CREATION_METHOD_SIMPLE() ? new GeomAlgoAPI_Pipe(aBaseShape,
237 : new GeomAlgoAPI_Pipe(aBaseShape,
241 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPipeAlgo, getKind(), anError)) {
247 storeResult(aBaseShape, aPipeAlgo, aResultIndex++);
249 } else if(aCreationMethod == CREATION_METHOD_LOCATIONS()) {
250 std::shared_ptr<GeomAlgoAPI_Pipe> aPipeAlgo(new GeomAlgoAPI_Pipe(aBaseShapesList,
254 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPipeAlgo, getKind(), anError)) {
260 storeResult(aBaseShapesList, aPipeAlgo, aResultIndex++);
262 setError("Error: Wrong creation method.");
266 removeResults(aResultIndex);
269 //==================================================================================================
270 void FeaturesPlugin_Pipe::storeResult(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
271 const std::shared_ptr<GeomAlgoAPI_Pipe> thePipeAlgo,
272 const int theResultIndex)
274 // Create result body.
275 ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
277 // Store generated shape.
278 aResultBody->storeGenerated(theBaseShape, thePipeAlgo->shape());
280 // Store generated edges/faces.
281 GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
282 GeomAPI_Shape::ShapeType aShapeTypeToExplode;
284 switch(aBaseShapeType) {
285 case GeomAPI_Shape::VERTEX: {
286 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
289 case GeomAPI_Shape::EDGE:
290 case GeomAPI_Shape::WIRE: {
291 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
292 GeomAlgoAPI_ShapeTools::findBounds(theBaseShape, aV1, aV2);
293 ListOfShape aV1History, aV2History;
294 thePipeAlgo->generated(aV1, aV1History);
295 thePipeAlgo->generated(aV2, aV2History);
296 if(!aV1History.empty()) {
297 aResultBody->generated(aV1, aV1History.front());
299 if(!aV2History.empty()) {
300 aResultBody->generated(aV2, aV2History.front());
303 case GeomAPI_Shape::FACE:
304 case GeomAPI_Shape::SHELL: {
305 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
308 case GeomAPI_Shape::COMPOUND: {
309 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
313 if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
314 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
315 aResultBody->loadGeneratedShapes(thePipeAlgo, theBaseShape, GeomAPI_Shape::VERTEX);
317 if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
318 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
319 aResultBody->loadGeneratedShapes(thePipeAlgo, theBaseShape, GeomAPI_Shape::EDGE);
322 // Store from shapes.
323 storeShapes(aResultBody, aBaseShapeType, thePipeAlgo->fromShapes(), "From_");
326 storeShapes(aResultBody, aBaseShapeType, thePipeAlgo->toShapes(), "To_");
328 setResult(aResultBody, theResultIndex);
331 //==================================================================================================
332 void FeaturesPlugin_Pipe::storeResult(const ListOfShape& theBaseShapes,
333 const std::shared_ptr<GeomAlgoAPI_Pipe> thePipeAlgo,
334 const int theResultIndex)
336 // Create result body.
337 ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
339 // Store generated shape.
340 aResultBody->storeGenerated(theBaseShapes.front(), thePipeAlgo->shape());
342 // Store generated edges/faces.
343 for(ListOfShape::const_iterator anIter = theBaseShapes.cbegin();
344 anIter != theBaseShapes.cend();
347 GeomShapePtr aBaseShape = *anIter;
348 GeomAPI_Shape::ShapeType aBaseShapeType = aBaseShape->shapeType();
349 GeomAPI_Shape::ShapeType aShapeTypeToExplode;
350 switch(aBaseShapeType) {
351 case GeomAPI_Shape::VERTEX: {
352 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
355 case GeomAPI_Shape::EDGE:
356 case GeomAPI_Shape::WIRE: {
357 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
358 GeomAlgoAPI_ShapeTools::findBounds(aBaseShape, aV1, aV2);
359 ListOfShape aV1History, aV2History;
360 thePipeAlgo->generated(aV1, aV1History);
361 thePipeAlgo->generated(aV2, aV2History);
362 aResultBody->generated(aV1, aV1History.front());
363 aResultBody->generated(aV2, aV2History.front());
365 case GeomAPI_Shape::FACE:
366 case GeomAPI_Shape::SHELL: {
367 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
371 aResultBody->loadGeneratedShapes(thePipeAlgo, aBaseShape, aShapeTypeToExplode);
374 // Store from shapes.
375 storeShapes(aResultBody, theBaseShapes.front()->shapeType(), thePipeAlgo->fromShapes(), "From_");
378 storeShapes(aResultBody, theBaseShapes.back()->shapeType(), thePipeAlgo->toShapes(), "To_");
381 setResult(aResultBody, theResultIndex);
384 //==================================================================================================
385 void FeaturesPlugin_Pipe::storeShapes(ResultBodyPtr theResultBody,
386 const GeomAPI_Shape::ShapeType theBaseShapeType,
387 const ListOfShape& theShapes,
388 const std::string theName)
390 GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
391 std::string aShapeTypeStr = "Face";
392 switch(theBaseShapeType) {
393 case GeomAPI_Shape::VERTEX: {
394 aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
395 aShapeTypeStr = "Vertex";
398 case GeomAPI_Shape::EDGE:
399 case GeomAPI_Shape::WIRE: {
400 aShapeTypeToExplore = GeomAPI_Shape::EDGE;
401 aShapeTypeStr = "Edge";
404 case GeomAPI_Shape::FACE:
405 case GeomAPI_Shape::SHELL: {
406 aShapeTypeToExplore = GeomAPI_Shape::FACE;
407 aShapeTypeStr = "Face";
410 case GeomAPI_Shape::COMPOUND: {
411 aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
419 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
420 GeomShapePtr aShape = *anIt;
422 if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
423 std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
424 storeSubShape(theResultBody, aShape,
427 aShape->shapeType() == GeomAPI_Shape::EDGE ? aShapeIndex : aFaceIndex);
429 std::string aName = theName + aShapeTypeStr;
430 storeSubShape(theResultBody, aShape, aShapeTypeToExplore, aName, aShapeIndex);
435 //==================================================================================================
436 void storeSubShape(ResultBodyPtr theResultBody,
437 const GeomShapePtr theShape,
438 const GeomAPI_Shape::ShapeType theType,
439 const std::string theName,
442 for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
443 GeomShapePtr aSubShape = anExp.current();
444 std::ostringstream aStr;
445 aStr << theName << "_" << theShapeIndex++;
446 theResultBody->generated(aSubShape, aStr.str());