Salome HOME
Copyright update 2021
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Boolean.cpp
1 // Copyright (C) 2014-2021  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "FeaturesPlugin_Boolean.h"
21
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>
31
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 <GeomAPI_Face.h>
42 #include <GeomAPI_ShapeExplorer.h>
43 #include <GeomAPI_ShapeIterator.h>
44
45 #include <algorithm>
46 #include <map>
47
48 //=================================================================================================
49 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean(const OperationType theOperationType)
50 : myOperationType(theOperationType)
51 {
52 }
53
54 //=================================================================================================
55 void FeaturesPlugin_Boolean::initAttributes()
56 {
57   AttributeSelectionListPtr aSelection =
58     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
59     FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
60
61   aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
62     FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
63
64   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
65   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
66 }
67
68 //=================================================================================================
69 FeaturesPlugin_Boolean::OperationType FeaturesPlugin_Boolean::operationType()
70 {
71   return myOperationType;
72 }
73
74 //=================================================================================================
75 void FeaturesPlugin_Boolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
76                                           const std::shared_ptr<GeomAPI_Shape> theBaseShape,
77                                           const ListOfShape& theTools,
78                                           const std::shared_ptr<GeomAPI_Shape> theResultShape,
79                                           const GeomMakeShapePtr& theMakeShape)
80 {
81   //load result
82   if(theBaseShape->isEqual(theResultShape)) {
83     theResultBody->store(theResultShape, false);
84     return;
85   }
86
87   theResultBody->storeModified(theBaseShape, theResultShape);
88
89   theResultBody->loadModifiedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
90   theResultBody->loadModifiedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::FACE);
91
92   theResultBody->loadDeletedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::FACE);
93
94   for (ListOfShape::const_iterator anIter = theTools.begin();
95        anIter != theTools.end();
96        ++anIter)
97   {
98     GeomAPI_Shape::ShapeType aShapeType =
99       (*anIter)->shapeType() <= GeomAPI_Shape::FACE ? GeomAPI_Shape::FACE
100                                                     : GeomAPI_Shape::EDGE;
101     theResultBody->loadModifiedShapes(theMakeShape, *anIter, aShapeType);
102
103     theResultBody->loadDeletedShapes(theMakeShape, *anIter, GeomAPI_Shape::FACE);
104   }
105 }
106
107 //=================================================================================================
108 void FeaturesPlugin_Boolean::storeResult(
109     const ListOfShape& theObjects,
110     const ListOfShape& theTools,
111     const GeomShapePtr theResultShape,
112     int& theResultIndex,
113     std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList,
114     std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList)
115 {
116   if (!theResultShape)
117     return;
118
119   std::shared_ptr<ModelAPI_ResultBody> aResultBody =
120       document()->createBody(data(), theResultIndex);
121
122   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
123                                            theObjects,
124                                            theTools,
125                                            theMakeShapeList,
126                                            theResultShape);
127   setResult(aResultBody, theResultIndex++);
128
129   // merge algorithms
130   FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
131   aRBA.resultBody = aResultBody;
132   aRBA.baseShape = theObjects.front();
133   for (std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>::iterator
134        aRBAIt = theResultBaseAlgoList.begin();
135        aRBAIt != theResultBaseAlgoList.end(); ++aRBAIt) {
136     theMakeShapeList->appendAlgo(aRBAIt->makeShape);
137   }
138   aRBA.makeShape = theMakeShapeList;
139   theResultBaseAlgoList.clear();
140   theResultBaseAlgoList.push_back(aRBA);
141 }