Salome HOME
Updated copyright comment
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Union.cpp
1 // Copyright (C) 2014-2024  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_Union.h"
21
22 #include <GeomAlgoAPI_Boolean.h>
23 #include <GeomAlgoAPI_CompoundBuilder.h>
24 #include <GeomAlgoAPI_MakeShapeList.h>
25 #include <GeomAlgoAPI_PaveFiller.h>
26 #include <GeomAlgoAPI_ShapeBuilder.h>
27 #include <GeomAlgoAPI_Tools.h>
28
29 #include <GeomAPI_ShapeExplorer.h>
30 #include <GeomAPI_ShapeIterator.h>
31
32 #include <ModelAPI_AttributeBoolean.h>>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_AttributeSelectionList.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_Tools.h>
37
38
39 static const double DEFAULT_FUZZY = 1.e-5;
40
41
42 //=================================================================================================
43 FeaturesPlugin_Union::FeaturesPlugin_Union()
44 {
45 }
46
47 //=================================================================================================
48 void FeaturesPlugin_Union::initAttributes()
49 {
50   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
51
52   data()->addAttribute(USE_FUZZY_ID(), ModelAPI_AttributeBoolean::typeId());
53   data()->addAttribute(FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
54   boolean(USE_FUZZY_ID())->setValue(false); // Do NOT use the fuzzy parameter by default.
55   real(FUZZY_PARAM_ID())->setValue(DEFAULT_FUZZY);
56
57   initVersion(BOP_VERSION_9_4(), selectionList(BASE_OBJECTS_ID()));
58 }
59
60 //=================================================================================================
61 void FeaturesPlugin_Union::execute()
62 {
63   GeomAPI_ShapeHierarchy anObjects;
64   ListOfShape anEmptyList;
65
66   // Getting objects.
67   if (!processAttribute(BASE_OBJECTS_ID(), anObjects, anEmptyList))
68     return;
69
70   if(anObjects.objects().size() < 2) {
71     setError("Error: Not enough objects for operation. Should be at least 2.");
72     return;
73   }
74
75   // Getting fuzzy parameter.
76   // Used as additional tolerance to eliminate tiny results.
77   // Using -1 as fuzzy value in the GeomAlgoAPI means to ignore it during the boolean operation!
78   bool aUseFuzzy = boolean(USE_FUZZY_ID())->value();
79   double aFuzzy = (aUseFuzzy ? real(FUZZY_PARAM_ID())->value() : -1);
80
81   std::string anError;
82   int aResultIndex = 0;
83   std::vector<ModelAPI_Tools::ResultBaseAlgo> aResultBaseAlgoList;
84   ListOfShape aResultShapesList;
85
86   GeomShapePtr aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(ListOfShape());
87
88   // Fuse objects.
89   bool isOk = true;
90   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
91        anObjectsIt != anObjects.end() && isOk;
92        ++anObjectsIt) {
93     GeomShapePtr anObject = *anObjectsIt;
94     GeomShapePtr aParent = anObjects.parent(anObject, false);
95
96     if (aParent && aParent->shapeType() <= GeomAPI_Shape::COMPSOLID) {
97       // get parent once again to mark it and the subs as processed
98       aParent = anObjects.parent(anObject);
99       // compsolid handling
100       isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_FUSE,
101                               anObjects, aParent, anEmptyList, anEmptyList,
102                               aFuzzy,
103                               aResultIndex, aResultBaseAlgoList, aResultShapesList,
104                               aResultCompound);
105     } else {
106       // process object as is
107       isOk = processObject(GeomAlgoAPI_Tools::BOOL_FUSE,
108                            anObject, anEmptyList, anEmptyList,
109                            aFuzzy,
110                            aResultIndex, aResultBaseAlgoList, aResultShapesList,
111                            aResultCompound);
112     }
113   }
114
115   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
116   for (std::vector<ModelAPI_Tools::ResultBaseAlgo>::iterator
117        aRBAIt = aResultBaseAlgoList.begin();
118        aRBAIt != aResultBaseAlgoList.end(); ++aRBAIt) {
119     aMakeShapeList->appendAlgo(aRBAIt->makeShape);
120   }
121
122   GeomShapePtr aShape;
123   GeomAPI_ShapeIterator aCIt(aResultCompound);
124   if (data()->version().empty()) {
125     // if the compound consists of a single sub-shape, take it,
126     // otherwise, take the full compound
127     aShape = aCIt.current();
128     aCIt.next();
129     if (aCIt.more())
130       aShape = aResultCompound;
131   }
132   else {
133     // merge hierarchies of compounds containing objects and tools
134     aShape = keepUnusedSubsOfCompound(aCIt.current(), anObjects, GeomAPI_ShapeHierarchy(),
135                                       aMakeShapeList);
136     for (aCIt.next(); aCIt.more(); aCIt.next()) {
137       std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
138       aBuilder->add(aShape, aCIt.current());
139       aMakeShapeList->appendAlgo(aBuilder);
140     }
141   }
142
143   // Store result and naming.
144   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
145   ListOfShape anObjectsList = anObjects.objects();
146   aResultBody->storeModified(anObjectsList.front(), aShape);
147
148   for(ListOfShape::const_iterator anIter = anObjectsList.begin();
149       anIter != anObjectsList.end(); ++anIter) {
150     aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::EDGE);
151     aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::FACE);
152     //aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
153   }
154
155   setResult(aResultBody);
156 }