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