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_ExtrusionFuse.h"
22 #include <ModelAPI_AttributeString.h>
23 #include <ModelAPI_AttributeSelectionList.h>
25 #include <GeomAlgoAPI_Boolean.h>
26 #include <GeomAlgoAPI_Prism.h>
27 #include <GeomAlgoAPI_ThroughAll.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
30 //=================================================================================================
31 FeaturesPlugin_ExtrusionFuse::FeaturesPlugin_ExtrusionFuse()
34 myOperationType = BOOL_FUSE;
37 //=================================================================================================
38 void FeaturesPlugin_ExtrusionFuse::execute()
40 if (string(CREATION_METHOD())->value() != CREATION_METHOD_THROUGH_ALL())
41 executeCompositeBoolean();
43 executeFuseThroughAll();
47 //=================================================================================================
48 void FeaturesPlugin_ExtrusionFuse::executeFuseThroughAll()
51 ListOfShape anObjects;
52 AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
53 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
54 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
55 GeomShapePtr anObject = anObjectAttr->value();
56 if (!anObject.get()) {
57 myFeature->setError("Error: Could not get object.");
60 anObjects.push_back(anObject);
64 ListOfShape aGenBaseShapes;
65 ListOfMakeShape aGenMakeShapes;
66 if (!makeGeneration(aGenBaseShapes, aGenMakeShapes)) {
71 ListOfShape aNewTools;
72 ListOfMakeShape aToolsMakeShapes;
73 for (ListOfMakeShape::const_iterator
74 anIt = aGenMakeShapes.cbegin(); anIt != aGenMakeShapes.cend(); ++anIt) {
75 GeomMakeShapePtr anAlgo = (*anIt);
76 std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo =
77 std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(anAlgo);
79 // Cut the prism by all objects and throw away end pieces
80 std::shared_ptr<GeomAlgoAPI_ThroughAll> aToolAlgo (
81 new GeomAlgoAPI_ThroughAll(aPrismAlgo, anObjects));
83 // Checking that the algorithm worked properly
84 if (!aToolAlgo->isDone() || aToolAlgo->shape()->isNull() || !aToolAlgo->isValid()) {
85 myFeature->setError("Error: ThroughAll algorithm failed.");
87 GeomShapePtr aCuttedTool = aToolAlgo->shape();
88 aNewTools.push_back(aCuttedTool);
89 aToolsMakeShapes.push_back(aToolAlgo);
93 // Perform FeaturesPlugin_CompositeBoolean::makeBoolean() with new (cutted) tools
94 ListOfShape aBooleanObjects;
95 ListOfMakeShape aBooleanMakeShapes;
96 if (!makeBoolean(aNewTools, aBooleanObjects, aBooleanMakeShapes)) {
100 if (myOperationType == BOOL_FUSE) {
101 aNewTools.splice(aNewTools.begin(), aBooleanObjects);
102 aBooleanObjects.splice(aBooleanObjects.begin(), aNewTools, aNewTools.begin());
105 // 4. Store result (like in FeaturesPlugin_CompositeBoolean::executeCompositeBoolean())
106 int aResultIndex = 0;
107 std::vector<ResultBaseAlgo> aResultBaseAlgoList;
108 ListOfShape aResultShapesList;
109 ListOfShape::const_iterator aBoolObjIt = aBooleanObjects.cbegin();
110 ListOfMakeShape::const_iterator aBoolMSIt = aBooleanMakeShapes.cbegin();
111 for(; aBoolObjIt != aBooleanObjects.cend() && aBoolMSIt != aBooleanMakeShapes.cend();
112 ++aBoolObjIt, ++aBoolMSIt) {
114 ResultBodyPtr aResultBody = myFeature->document()->createBody(myFeature->data(), aResultIndex);
116 if((*aBoolObjIt)->isEqual((*aBoolMSIt)->shape())) {
117 aResultBody->store((*aBoolMSIt)->shape(), false);
121 aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape());
123 // Store generation history.
124 ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
125 ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
126 for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
127 ++aGenBaseIt, ++aGenMSIt) {
130 ListOfMakeShape::const_iterator aToolsMSIt = aToolsMakeShapes.cbegin();
131 for(; aToolsMSIt != aToolsMakeShapes.cend(); ++aToolsMSIt) {
132 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMSList(new GeomAlgoAPI_MakeShapeList());
135 aMSList->appendAlgo(*aGenMSIt);
137 // tool modification (cut by objects)
138 aMSList->appendAlgo(*aToolsMSIt);
141 aMSList->appendAlgo(*aBoolMSIt);
142 storeGenerationHistory(aResultBody, *aGenBaseIt, aMSList);
146 storeModificationHistory(aResultBody, *aBoolObjIt, aNewTools, *aBoolMSIt);
149 aRBA.resultBody = aResultBody;
150 aRBA.baseShape = *aBoolObjIt;
151 aRBA.makeShape = *aBoolMSIt;
152 aResultBaseAlgoList.push_back(aRBA);
153 aResultShapesList.push_back((*aBoolMSIt)->shape());
156 myFeature->setResult(aResultBody, aResultIndex++);
159 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
160 // result shape has been deleted, but in another it was modified or stayed.
161 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
162 storeDeletedShapes(aResultBaseAlgoList, aNewTools, aResultShapesCompound);
164 myFeature->removeResults(aResultIndex);