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