Salome HOME
[Code coverage]: Move checking the algorithm's result into separate function
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Union.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesPlugin_Union.h"
22
23 #include <GeomAlgoAPI_Boolean.h>
24 #include <GeomAlgoAPI_MakeShapeList.h>
25 #include <GeomAlgoAPI_PaveFiller.h>
26 #include <GeomAlgoAPI_Tools.h>
27 #include <GeomAlgoAPI_UnifySameDomain.h>
28
29 #include <GeomAPI_ShapeExplorer.h>
30 #include <GeomAPI_ShapeIterator.h>
31
32 #include <ModelAPI_AttributeSelectionList.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_Tools.h>
35
36 //=================================================================================================
37 FeaturesPlugin_Union::FeaturesPlugin_Union()
38 {
39 }
40
41 //=================================================================================================
42 void FeaturesPlugin_Union::initAttributes()
43 {
44   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
45 }
46
47 //=================================================================================================
48 void FeaturesPlugin_Union::execute()
49 {
50   ListOfShape anObjects;
51   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
52
53   // Getting objects.
54   AttributeSelectionListPtr anObjectsSelList =
55     selectionList(FeaturesPlugin_Union::BASE_OBJECTS_ID());
56   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
57     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
58     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
59     if(!anObject.get()) {
60       return;
61     }
62     ResultPtr aContext = anObjectAttr->context();
63     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
64     if(aResCompSolidPtr.get()) {
65       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
66       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
67         anIt = aCompSolidsObjects.begin();
68       for(; anIt != aCompSolidsObjects.end(); anIt++) {
69         if(anIt->first->isEqual(aContextShape)) {
70           aCompSolidsObjects[anIt->first].push_back(anObject);
71           break;
72         }
73       }
74       if(anIt == aCompSolidsObjects.end()) {
75         aCompSolidsObjects[aContextShape].push_back(anObject);
76       }
77     } else {
78       anObjects.push_back(anObject);
79     }
80   }
81
82   // Collecting solids from compsolids which will not be modified in
83   // boolean operation and will be added to result.
84   ListOfShape aShapesToAdd;
85   for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
86     anIt = aCompSolidsObjects.begin();
87     anIt != aCompSolidsObjects.end(); anIt++) {
88     std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
89     ListOfShape& aUsedInOperationSolids = anIt->second;
90     anObjects.insert(anObjects.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
91
92     // Collect solids from compsolid which will not be modified in boolean operation.
93     for(GeomAPI_ShapeIterator anExp(aCompSolid); anExp.more(); anExp.next()) {
94       std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
95       ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
96       for(; anIt != aUsedInOperationSolids.end(); anIt++) {
97         if(aSolidInCompSolid->isEqual(*anIt)) {
98           break;
99         }
100       }
101       if(anIt == aUsedInOperationSolids.end()) {
102         aShapesToAdd.push_back(aSolidInCompSolid);
103       }
104     }
105   }
106
107   if(anObjects.size() < 2) {
108     setError("Error: Not enough objects for operation. Should be at least 2.");
109     return;
110   }
111
112   // Fuse objects.
113   std::string anError;
114   std::shared_ptr<GeomAlgoAPI_MakeShape> anAlgo;
115   ListOfShape aTools;
116   if (anObjects.front()->shapeType() == GeomAPI_Shape::SOLID) {
117     aTools.splice(aTools.begin(), anObjects, anObjects.begin());
118     anAlgo.reset(new GeomAlgoAPI_Boolean(anObjects,
119                  aTools,
120                  GeomAlgoAPI_Boolean::BOOL_FUSE));
121   } else {
122     anAlgo.reset(new GeomAlgoAPI_UnifySameDomain(anObjects));
123   }
124
125   // Checking that the algorithm worked properly.
126   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
127   GeomAPI_DataMapOfShapeShape aMapOfShapes;
128   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgo, getKind(), anError)) {
129     setError(anError);
130     return;
131   }
132
133   GeomShapePtr aShape = anAlgo->shape();
134   aMakeShapeList->appendAlgo(anAlgo);
135   aMapOfShapes.merge(anAlgo->mapOfSubShapes());
136
137   // Store original shapes for naming.
138   anObjects.splice(anObjects.begin(), aTools);
139   anObjects.insert(anObjects.end(), aShapesToAdd.begin(), aShapesToAdd.end());
140
141   // Combine result with not used solids from compsolid.
142   if(aShapesToAdd.size() > 0) {
143     aShapesToAdd.push_back(aShape);
144     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
145       new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
146     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
147       setError(anError);
148       return;
149     }
150
151     aShape = aFillerAlgo->shape();
152     aMakeShapeList->appendAlgo(aFillerAlgo);
153     aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
154   }
155   // workaround: make copy to name edges correctly
156
157   // Store result and naming.
158
159   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
160   aResultBody->storeModified(anObjects.front(), aShape);
161
162   for(ListOfShape::const_iterator anIter = anObjects.begin(); anIter != anObjects.end(); ++anIter) {
163     aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::EDGE);
164     aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::FACE);
165     //aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
166   }
167
168   setResult(aResultBody);
169 }