Salome HOME
Change the paradigm of versioning of Boolean Operations on the Python API level.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Union.cpp
1 // Copyright (C) 2014-2019  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_AttributeSelectionList.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_Tools.h>
35
36 //=================================================================================================
37 FeaturesPlugin_Union::FeaturesPlugin_Union()
38 {
39 }
40
41 //=================================================================================================
42 void FeaturesPlugin_Union::initAttributes()
43 {
44   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
45   initVersion(THE_VERSION_1, selectionList(BASE_OBJECTS_ID()));
46 }
47
48 //=================================================================================================
49 void FeaturesPlugin_Union::execute()
50 {
51   ObjectHierarchy anObjects;
52   ListOfShape anEmptyList;
53
54   // Getting objects.
55   if (!processAttribute(BASE_OBJECTS_ID(), anObjects, anEmptyList))
56     return;
57
58   if(anObjects.Objects().size() < 2) {
59     setError("Error: Not enough objects for operation. Should be at least 2.");
60     return;
61   }
62
63   std::string anError;
64   int aResultIndex = 0;
65   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
66   ListOfShape aResultShapesList;
67
68   int aUnionVersion = version();
69   GeomShapePtr aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(ListOfShape());
70
71   // Fuse objects.
72   bool isOk = true;
73   for (ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
74        anObjectsIt != anObjects.End() && isOk;
75        ++anObjectsIt) {
76     GeomShapePtr anObject = *anObjectsIt;
77     GeomShapePtr aParent = anObjects.Parent(anObject, false);
78
79     if (aParent && aParent->shapeType() <= GeomAPI_Shape::COMPSOLID) {
80       // get parent once again to mark it and the subs as processed
81       aParent = anObjects.Parent(anObject);
82       // compsolid handling
83       isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_FUSE,
84                               anObjects, aParent, anEmptyList, anEmptyList,
85                               aResultIndex, aResultBaseAlgoList, aResultShapesList,
86                               aResultCompound);
87     } else {
88       // process object as is
89       isOk = processObject(GeomAlgoAPI_Tools::BOOL_FUSE,
90                            anObject, anEmptyList, anEmptyList,
91                            aResultIndex, aResultBaseAlgoList, aResultShapesList,
92                            aResultCompound);
93     }
94   }
95
96   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
97   for (std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>::iterator
98        aRBAIt = aResultBaseAlgoList.begin();
99        aRBAIt != aResultBaseAlgoList.end(); ++aRBAIt) {
100     aMakeShapeList->appendAlgo(aRBAIt->makeShape);
101   }
102
103   GeomShapePtr aShape;
104   GeomAPI_ShapeIterator aCIt(aResultCompound);
105   if (aUnionVersion < THE_VERSION_1) {
106     // if the compound consists of a single sub-shape, take it,
107     // otherwise, take the full compound
108     aShape = aCIt.current();
109     aCIt.next();
110     if (aCIt.more())
111       aShape = aResultCompound;
112   }
113   else {
114     // merge hierarchies of compounds containing objects and tools
115     aShape = keepUnusedSubsOfCompound(aCIt.current(), anObjects, ObjectHierarchy(), aMakeShapeList);
116     for (aCIt.next(); aCIt.more(); aCIt.next()) {
117       std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
118       aBuilder->add(aShape, aCIt.current());
119       aMakeShapeList->appendAlgo(aBuilder);
120     }
121   }
122
123   // Store result and naming.
124   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
125   ListOfShape anObjectsList = anObjects.Objects();
126   aResultBody->storeModified(anObjectsList.front(), aShape);
127
128   for(ListOfShape::const_iterator anIter = anObjectsList.begin();
129       anIter != anObjectsList.end(); ++anIter) {
130     aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::EDGE);
131     aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::FACE);
132     //aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
133   }
134
135   setResult(aResultBody);
136 }