Salome HOME
support fuzzy parameter in all boolean operations
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Intersection.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_Intersection.h"
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_BodyBuilder.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_Tools.h>
29
30 #include <GeomAlgoAPI_Intersection.h>
31 #include <GeomAlgoAPI_MakeShapeList.h>
32 #include <GeomAlgoAPI_Tools.h>
33 #include <GeomAPI_ShapeExplorer.h>
34
35 #include <sstream>
36
37
38 static const std::string INTERSECTION_VERSION_1("v9.5");
39
40 //=================================================================================================
41 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
42 {
43 }
44
45 //=================================================================================================
46 void FeaturesPlugin_Intersection::initAttributes()
47 {
48   AttributePtr anObjectsAttr = data()->addAttribute(OBJECT_LIST_ID(),
49                        ModelAPI_AttributeSelectionList::typeId());
50
51   data()->addAttribute(FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
52   // Initialize the fuzzy parameter with a value below Precision::Confusion() to indicate,
53   // that the internal algorithms should use their default fuzzy value, if none was specified
54   // by the user.
55   real(FUZZY_PARAM_ID())->setValue(1.e-8);
56
57   initVersion(INTERSECTION_VERSION_1, anObjectsAttr, AttributePtr());
58 }
59
60 //=================================================================================================
61 void FeaturesPlugin_Intersection::execute()
62 {
63   GeomAPI_ShapeHierarchy anObjectsHierarchy;
64   ListOfShape aPlanes;
65   // Getting objects.
66   if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes)) {
67     setError("Error: Objects or tools are empty.");
68     return;
69   }
70
71   // Getting fuzzy parameter.
72   // Used as additional tolerance to eliminate tiny results.
73   double aFuzzy = real(FUZZY_PARAM_ID())->value();
74
75   int aResultIndex = 0;
76
77   // Create result.
78   const ListOfShape& anObjects = anObjectsHierarchy.objects();
79   GeomMakeShapePtr anIntersectionAlgo(new GeomAlgoAPI_Intersection(anObjects, aFuzzy));
80
81   // Checking that the algorithm worked properly.
82   std::string anError;
83   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anIntersectionAlgo, getKind(), anError)) {
84     setError(anError);
85     return;
86   }
87
88   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
89   aMakeShapeList->appendAlgo(anIntersectionAlgo);
90
91   GeomShapePtr aResShape = anIntersectionAlgo->shape();
92   if (data()->version() == INTERSECTION_VERSION_1) {
93     // merge hierarchies of compounds containing objects and tools
94     // and append the result of the FUSE operation
95     aResShape = keepUnusedSubsOfCompound(aResShape, anObjectsHierarchy,
96                                          GeomAPI_ShapeHierarchy(), aMakeShapeList);
97   }
98
99   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
100   ModelAPI_Tools::loadModifiedShapes(aResultBody,
101                                      anObjects,
102                                      ListOfShape(),
103                                      aMakeShapeList,
104                                      aResShape);
105   setResult(aResultBody, aResultIndex);
106   aResultIndex++;
107
108   ModelAPI_Tools::loadDeletedShapes(aResultBody,
109                                     GeomShapePtr(),
110                                     anObjects,
111                                     aMakeShapeList,
112                                     aResShape);
113
114   // remove the rest results if there were produced in the previous pass
115   removeResults(aResultIndex);
116 }