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_VersionedBoolean.h"
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_AttributeInteger.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Tools.h>
32 #include <GeomAlgoAPI_Boolean.h>
33 #include <GeomAlgoAPI_CompoundBuilder.h>
34 #include <GeomAlgoAPI_MakeShapeCustom.h>
35 #include <GeomAlgoAPI_MakeShapeList.h>
36 #include <GeomAlgoAPI_Partition.h>
37 #include <GeomAlgoAPI_PaveFiller.h>
38 #include <GeomAlgoAPI_ShapeBuilder.h>
39 #include <GeomAlgoAPI_ShapeTools.h>
40 #include <GeomAlgoAPI_Tools.h>
41 #include <GeomAlgoAPI_UnifySameDomain.h>
42 #include <GeomAPI_Face.h>
43 #include <GeomAPI_ShapeExplorer.h>
44 #include <GeomAPI_ShapeIterator.h>
49 static void performBoolean(const GeomAlgoAPI_Tools::BOPType theBooleanType,
50 GeomMakeShapePtr& theBooleanAlgo,
51 const ListOfShape& theObjects,
52 const ListOfShape& theTools)
54 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
55 theBooleanAlgo.reset(new GeomAlgoAPI_Partition(theObjects, theTools));
57 // separate processing of FUSE, if only objects are given
58 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_FUSE && theTools.empty()) {
59 if (theObjects.front()->shapeType() == GeomAPI_Shape::FACE)
60 theBooleanAlgo.reset(new GeomAlgoAPI_UnifySameDomain(theObjects));
62 ListOfShape anObjects = theObjects;
64 aTools.splice(aTools.begin(), anObjects, anObjects.begin());
65 theBooleanAlgo.reset(new GeomAlgoAPI_Boolean(anObjects, aTools, theBooleanType));
69 theBooleanAlgo.reset(new GeomAlgoAPI_Boolean(theObjects, theTools, theBooleanType));
73 //=================================================================================================
74 void FeaturesPlugin_VersionedBoolean::initVersion(const std::string& theVersion,
75 const AttributePtr theObjectsAttr,
76 const AttributePtr theToolsAttr)
78 AttributeIntegerPtr anOldVersionAttr = integer("version");
79 if (anOldVersionAttr && anOldVersionAttr->isInitialized() && data()->version().empty()) {
80 // move value to the common version interface in ModelAPI_Data
81 data()->setVersion(BOP_VERSION_9_4());
83 else if ((!theObjectsAttr || !theObjectsAttr->isInitialized()) &&
84 (!theToolsAttr || !theToolsAttr->isInitialized())) {
85 // this is a newly created feature (not read from file),
86 // so, initialize the latest version
87 data()->setVersion(theVersion);
91 //=================================================================================================
92 bool FeaturesPlugin_VersionedBoolean::processAttribute(const std::string& theAttributeName,
93 GeomAPI_ShapeHierarchy& theObjects,
94 ListOfShape& thePlanesList)
96 AttributeSelectionListPtr anObjectsSelList = selectionList(theAttributeName);
97 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
98 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
99 GeomShapePtr anObject = anObjectAttr->value();
100 if (!anObject.get()) {
101 // It could be a construction plane.
102 ResultPtr aContext = anObjectAttr->context();
103 anObject = anObjectAttr->context()->shape();
104 if (anObject.get()) {
105 thePlanesList.push_back(anObject);
111 theObjects.addObject(anObject);
113 ResultPtr aContext = anObjectAttr->context();
114 ModelAPI_Tools::fillShapeHierarchy(anObject, aContext, theObjects);
119 //=================================================================================================
120 bool FeaturesPlugin_VersionedBoolean::processObject(
121 const GeomAlgoAPI_Tools::BOPType theBooleanType,
122 const GeomShapePtr& theObject,
123 const ListOfShape& theTools,
124 const ListOfShape& thePlanes,
126 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
127 ListOfShape& theResultShapesList,
128 GeomShapePtr theResultCompound)
130 ListOfShape aListWithObject;
131 aListWithObject.push_back(theObject);
132 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
133 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
134 GeomShapePtr aResShape;
137 ListOfShape aToolsWithPlanes = theTools;
138 ListOfShape aPlanesCopy = thePlanes;
139 resizePlanes(aListWithObject, aPlanesCopy, aMakeShapeList);
140 aToolsWithPlanes.insert(aToolsWithPlanes.end(), aPlanesCopy.begin(), aPlanesCopy.end());
142 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
143 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes));
145 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
149 // Checking that the algorithm worked properly.
151 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
156 aResShape = aBoolAlgo->shape();
157 if (aResShape.get() && aResShape->shapeType() == GeomAPI_Shape::COMPOUND) {
158 int aSubResultsNb = 0;
159 GeomAPI_ShapeIterator anIt(aResShape);
160 for (; anIt.more(); anIt.next())
163 if (aSubResultsNb == 1) {
164 anIt.init(aResShape);
166 aResShape = anIt.current();
170 aMakeShapeList->appendAlgo(aBoolAlgo);
172 GeomAPI_ShapeIterator aShapeIt(aResShape);
173 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
174 std::shared_ptr<ModelAPI_ResultBody> aResultBody;
176 if (theResultCompound) { // store BOP result to the compound
177 std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
178 aBuilder->add(theResultCompound, aResShape);
179 aMakeShapeList->appendAlgo(aBuilder);
181 else { // create a separate ResultBody
182 aResultBody = document()->createBody(data(), theResultIndex);
184 // tools should be added to the list to fulfill the correct history of modification
185 aListWithObject.insert(aListWithObject.end(), theTools.begin(), theTools.end());
187 ListOfShape aUsedTools = theTools;
188 aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
190 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
195 setResult(aResultBody, theResultIndex);
200 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
201 aRBA.resultBody = aResultBody;
202 aRBA.baseShape = theObject;
203 aRBA.makeShape = aMakeShapeList;
204 theResultBaseAlgoList.push_back(aRBA);
205 theResultShapesList.push_back(aResShape);
210 //=================================================================================================
211 bool FeaturesPlugin_VersionedBoolean::processCompsolid(
212 const GeomAlgoAPI_Tools::BOPType theBooleanType,
213 GeomAPI_ShapeHierarchy& theCompsolidHierarchy,
214 const GeomShapePtr& theCompsolid,
215 const ListOfShape& theTools,
216 const ListOfShape& thePlanes,
218 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
219 ListOfShape& theResultShapesList,
220 GeomShapePtr theResultCompound)
222 ListOfShape aUsedInOperationSolids;
223 ListOfShape aNotUsedSolids;
224 theCompsolidHierarchy.splitCompound(theCompsolid, aUsedInOperationSolids, aNotUsedSolids);
226 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
229 ListOfShape aToolsWithPlanes = theTools;
230 ListOfShape aPlanesCopy = thePlanes;
231 resizePlanes(aUsedInOperationSolids, aPlanesCopy, aMakeShapeList);
232 aToolsWithPlanes.insert(aToolsWithPlanes.end(), aPlanesCopy.begin(), aPlanesCopy.end());
234 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
235 performBoolean(theBooleanType, aBoolAlgo, aUsedInOperationSolids, aToolsWithPlanes);
237 // Checking that the algorithm worked properly.
239 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
244 aMakeShapeList->appendAlgo(aBoolAlgo);
245 GeomShapePtr aResultShape = aBoolAlgo->shape();
247 // Add result to not used solids from compsolid.
248 if (!aNotUsedSolids.empty()) {
249 theCompsolidHierarchy.markProcessed(aNotUsedSolids);
251 ListOfShape aShapesToAdd = aNotUsedSolids;
252 aShapesToAdd.push_back(aBoolAlgo->shape());
253 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
254 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
255 if (!aFillerAlgo->isDone()) {
256 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
257 setError(aFeatureError);
261 aMakeShapeList->appendAlgo(aFillerAlgo);
262 aResultShape = aFillerAlgo->shape();
265 GeomAPI_ShapeIterator aShapeIt(aResultShape);
266 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
268 std::shared_ptr<ModelAPI_ResultBody> aResultBody;
270 if (theResultCompound) { // store BOP result to the compound
271 std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
272 aBuilder->add(theResultCompound, aResultShape);
273 aMakeShapeList->appendAlgo(aBuilder);
275 else { // create a separate ResultBody
276 aResultBody = document()->createBody(data(), theResultIndex);
278 ListOfShape aCompSolidList;
279 aCompSolidList.push_back(theCompsolid);
280 // tools should be added to the list to fulfill the correct history of modification
281 aCompSolidList.insert(aCompSolidList.end(), theTools.begin(), theTools.end());
283 ListOfShape aUsedTools = theTools;
284 aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
286 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
291 setResult(aResultBody, theResultIndex);
295 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
296 aRBA.resultBody = aResultBody;
297 aRBA.baseShape = theCompsolid;
298 aRBA.makeShape = aMakeShapeList;
299 theResultBaseAlgoList.push_back(aRBA);
300 theResultShapesList.push_back(aResultShape);
305 //=================================================================================================
306 bool FeaturesPlugin_VersionedBoolean::processCompound(
307 const GeomAlgoAPI_Tools::BOPType theBooleanType,
308 GeomAPI_ShapeHierarchy& theCompoundHierarchy,
309 const GeomShapePtr& theCompound,
310 const ListOfShape& theTools,
312 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
313 ListOfShape& theResultShapesList,
314 GeomShapePtr theResultCompound)
316 ListOfShape aUsedInOperationShapes;
317 ListOfShape aNotUsedShapes;
318 theCompoundHierarchy.splitCompound(theCompound, aUsedInOperationShapes, aNotUsedShapes);
319 if (theResultCompound) {
320 // Not necessary to keep all subs of the current compound,
321 // all unused solids are already stored in the result compound.
322 aNotUsedShapes.clear();
325 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
326 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
327 performBoolean(theBooleanType, aBoolAlgo, aUsedInOperationShapes, theTools);
329 // Checking that the algorithm worked properly.
331 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
336 aMakeShapeList->appendAlgo(aBoolAlgo);
337 GeomShapePtr aResultShape = aBoolAlgo->shape();
339 // Add result to not used shape from compound.
340 if (!aNotUsedShapes.empty()) {
341 theCompoundHierarchy.markProcessed(aNotUsedShapes);
343 ListOfShape aShapesForResult = aNotUsedShapes;
344 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
345 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
346 aShapesForResult.push_back(aResultIt.current());
350 aShapesForResult.push_back(aResultShape);
353 if (aShapesForResult.size() == 1) {
354 aResultShape = aShapesForResult.front();
357 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
361 GeomAPI_ShapeIterator aShapeIt(aResultShape);
362 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
363 std::shared_ptr<ModelAPI_ResultBody> aResultBody;
365 if (theResultCompound) { // store BOP result to the compound
366 std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
367 aBuilder->add(theResultCompound, aResultShape);
368 aMakeShapeList->appendAlgo(aBuilder);
370 else { // create a separate ResultBody
371 aResultBody = document()->createBody(data(), theResultIndex);
373 ListOfShape aCompoundList;
374 aCompoundList.push_back(theCompound);
375 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
380 setResult(aResultBody, theResultIndex);
384 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
385 aRBA.resultBody = aResultBody;
386 aRBA.baseShape = theCompound;
387 aRBA.makeShape = aMakeShapeList;
388 theResultBaseAlgoList.push_back(aRBA);
389 theResultShapesList.push_back(aResultShape);
394 //==================================================================================================
395 GeomShapePtr FeaturesPlugin_VersionedBoolean::keepUnusedSubsOfCompound(
396 const GeomShapePtr& theResult,
397 const GeomAPI_ShapeHierarchy& theObjectsHierarchy,
398 const GeomAPI_ShapeHierarchy& theToolsHierarchy,
399 std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList)
401 ListOfShape aCompounds;
402 theObjectsHierarchy.compoundsOfUnusedObjects(aCompounds);
403 theToolsHierarchy.compoundsOfUnusedObjects(aCompounds);
405 GeomShapePtr aResultShape = theResult;
406 if (!aCompounds.empty()) {
407 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aCompounds);
409 std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
410 aBuilder->add(aResultShape, theResult);
411 theMakeShapeList->appendAlgo(aBuilder);
417 //=================================================================================================
418 void FeaturesPlugin_VersionedBoolean::resizePlanes(
419 const ListOfShape& theObjects,
420 ListOfShape& thePlanes,
421 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList)
423 if (thePlanes.empty())
426 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
427 GeomAlgoAPI_ShapeTools::getBoundingBox(theObjects, 1.0);
429 // Resize planes to fit in bounding box
430 for (ListOfShape::iterator anIt = thePlanes.begin(); anIt != thePlanes.end(); ++anIt) {
431 GeomShapePtr aPlane = *anIt;
432 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
433 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
434 aMkShCustom->addModified(aPlane, aTool);
435 theMakeShapeList->appendAlgo(aMkShCustom);