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