Salome HOME
added checkbox in UI for optional fuzzy parameter
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Boolean.cpp
1 // Copyright (C) 2014-2022  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_AttributeBoolean.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeReference.h>
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_AttributeSelectionList.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_Validator.h>
32 #include <ModelAPI_Tools.h>
33
34 #include <GeomAlgoAPI_Boolean.h>
35 #include <GeomAlgoAPI_CompoundBuilder.h>
36 #include <GeomAlgoAPI_MakeShapeCustom.h>
37 #include <GeomAlgoAPI_MakeShapeList.h>
38 #include <GeomAlgoAPI_Partition.h>
39 #include <GeomAlgoAPI_PaveFiller.h>
40 #include <GeomAlgoAPI_ShapeBuilder.h>
41 #include <GeomAlgoAPI_ShapeTools.h>
42 #include <GeomAlgoAPI_Tools.h>
43 #include <GeomAPI_Face.h>
44 #include <GeomAPI_ShapeExplorer.h>
45 #include <GeomAPI_ShapeIterator.h>
46
47 #include <algorithm>
48 #include <map>
49
50
51 static const double DEFAULT_FUZZY = 1.e-5;
52
53
54 //=================================================================================================
55 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean(const OperationType theOperationType)
56 : myOperationType(theOperationType)
57 {
58 }
59
60 //=================================================================================================
61 void FeaturesPlugin_Boolean::initAttributes()
62 {
63   data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
64   data()->addAttribute(FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
65
66   data()->addAttribute(FeaturesPlugin_Boolean::USE_FUZZY_ID(), ModelAPI_AttributeBoolean::typeId());
67   data()->addAttribute(FeaturesPlugin_Boolean::FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
68   boolean(USE_FUZZY_ID())->setValue(false); // Do NOT use the fuzzy parameter by default.
69   real(FUZZY_PARAM_ID())->setValue(DEFAULT_FUZZY);
70
71   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
72   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
73 }
74
75 //=================================================================================================
76 FeaturesPlugin_Boolean::OperationType FeaturesPlugin_Boolean::operationType()
77 {
78   return myOperationType;
79 }
80
81 //=================================================================================================
82 void FeaturesPlugin_Boolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
83                                           const std::shared_ptr<GeomAPI_Shape> theBaseShape,
84                                           const ListOfShape& theTools,
85                                           const std::shared_ptr<GeomAPI_Shape> theResultShape,
86                                           const GeomMakeShapePtr& theMakeShape)
87 {
88   //load result
89   if(theBaseShape->isEqual(theResultShape)) {
90     theResultBody->store(theResultShape, false);
91     return;
92   }
93
94   theResultBody->storeModified(theBaseShape, theResultShape);
95
96   theResultBody->loadModifiedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
97   theResultBody->loadModifiedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::FACE);
98
99   theResultBody->loadDeletedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::FACE);
100
101   for (ListOfShape::const_iterator anIter = theTools.begin();
102        anIter != theTools.end();
103        ++anIter)
104   {
105     GeomAPI_Shape::ShapeType aShapeType =
106       (*anIter)->shapeType() <= GeomAPI_Shape::FACE ? GeomAPI_Shape::FACE
107                                                     : GeomAPI_Shape::EDGE;
108     theResultBody->loadModifiedShapes(theMakeShape, *anIter, aShapeType);
109
110     theResultBody->loadDeletedShapes(theMakeShape, *anIter, GeomAPI_Shape::FACE);
111   }
112 }
113
114 //=================================================================================================
115 void FeaturesPlugin_Boolean::storeResult(
116     const ListOfShape& theObjects,
117     const ListOfShape& theTools,
118     const GeomShapePtr theResultShape,
119     int& theResultIndex,
120     std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList,
121     std::vector<ModelAPI_Tools::ResultBaseAlgo>& theResultBaseAlgoList)
122 {
123   if (!theResultShape)
124     return;
125
126   std::shared_ptr<ModelAPI_ResultBody> aResultBody =
127       document()->createBody(data(), theResultIndex);
128
129   ModelAPI_Tools::loadModifiedShapes(aResultBody,
130                                      theObjects,
131                                      theTools,
132                                      theMakeShapeList,
133                                      theResultShape);
134   setResult(aResultBody, theResultIndex++);
135
136   // merge algorithms
137   ModelAPI_Tools::ResultBaseAlgo aRBA;
138   aRBA.resultBody = aResultBody;
139   aRBA.baseShape = theObjects.front();
140   for (std::vector<ModelAPI_Tools::ResultBaseAlgo>::iterator
141        aRBAIt = theResultBaseAlgoList.begin();
142        aRBAIt != theResultBaseAlgoList.end(); ++aRBAIt) {
143     theMakeShapeList->appendAlgo(aRBAIt->makeShape);
144   }
145   aRBA.makeShape = theMakeShapeList;
146   theResultBaseAlgoList.clear();
147   theResultBaseAlgoList.push_back(aRBA);
148 }