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