Salome HOME
Test case for Pipe naming (issue #17281)
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Fillet.cpp
1 // Copyright (C) 2017-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_Fillet.h"
21 #include "FeaturesPlugin_Tools.h"
22
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_Validator.h>
31
32 #include <GeomAlgoAPI_CompoundBuilder.h>
33 #include <GeomAlgoAPI_Fillet.h>
34 #include <GeomAlgoAPI_MakeShapeList.h>
35 #include <GeomAlgoAPI_Tools.h>
36
37 #include <GeomAPI_DataMapOfShapeMapOfShapes.h>
38 #include <GeomAPI_ShapeExplorer.h>
39 #include <GeomAPI_ShapeIterator.h>
40
41
42 // Obtain all sub-shapes from the shape and append them to the list
43 static void collectSubs(const GeomShapePtr& theShape,
44                               ListOfShape& theSubs,
45                         const GeomAPI_Shape::ShapeType theShapeType)
46 {
47   GeomAPI_ShapeExplorer anExp(theShape, theShapeType);
48   for (; anExp.more(); anExp.next()) {
49     GeomShapePtr aShape = anExp.current();
50     // Store all shapes with FORWARD orientation to avoid duplication of shared edges/vertices
51     aShape->setOrientation(GeomAPI_Shape::FORWARD);
52     theSubs.push_back(aShape);
53   }
54 }
55
56 // Extract edges from the list
57 static ListOfShape selectEdges(const ListOfShape& theShapes)
58 {
59   ListOfShape anEdges;
60   for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt)
61     if ((*anIt)->isEdge())
62       anEdges.push_back(*anIt);
63   return anEdges;
64 }
65
66 // If theShape is a compound of a single sub-shape, return this sub-shape
67 static GeomShapePtr unwrapCompound(const GeomShapePtr& theShape)
68 {
69   GeomShapePtr aShape = theShape;
70   if(aShape->shapeType() == GeomAPI_Shape::COMPOUND) {
71     int aSubResultsNb = 0;
72     GeomAPI_ShapeIterator anIt(aShape);
73     for(; anIt.more(); anIt.next())
74       ++aSubResultsNb;
75
76     if(aSubResultsNb == 1) {
77       anIt.init(aShape);
78       aShape = anIt.current();
79     }
80   }
81   return aShape;
82 }
83
84
85 FeaturesPlugin_Fillet::FeaturesPlugin_Fillet()
86 {
87 }
88
89 void FeaturesPlugin_Fillet::initAttributes()
90 {
91   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
92   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
93   data()->addAttribute(START_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
94   data()->addAttribute(END_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
95
96   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_RADIUS_ID());
97 }
98
99
100 void FeaturesPlugin_Fillet::execute()
101 {
102   AttributeStringPtr aCreationMethod = string(CREATION_METHOD());
103   if (!aCreationMethod)
104     return;
105
106   std::list<std::pair<GeomShapePtr, ListOfShape> > aSolidsAndSubs;
107
108   // getting objects and sort them according to parent solids
109   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
110   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); ++anObjectsIndex) {
111     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
112     GeomShapePtr anObject = anObjectAttr->value();
113     if (!anObject)
114       return;
115
116     ResultPtr aContext = anObjectAttr->context();
117     GeomShapePtr aParent;
118     if (aContext.get()) {
119       ResultBodyPtr aCtxOwner = ModelAPI_Tools::bodyOwner(aContext);
120       aParent = aCtxOwner ? aCtxOwner->shape() : aContext->shape();
121     } else { // get it from a feature
122       FeaturePtr aFeature = anObjectAttr->contextFeature();
123       if (aFeature.get()) {
124         aParent = aFeature->firstResult()->shape();
125       }
126     }
127     if (!aParent)
128       return;
129
130     // searching this parent is already in the list aSolidsAndSubs
131     std::list<std::pair<GeomShapePtr, ListOfShape> >::iterator aSearch = aSolidsAndSubs.begin();
132     ListOfShape* aFound;
133     for(; aSearch != aSolidsAndSubs.end(); aSearch++) {
134       if (aSearch->first->isSame(aParent)) {
135         aFound = &(aSearch->second);
136         break;
137       }
138     }
139     if (aSearch == aSolidsAndSubs.end()) { // not found, so, add a new one
140       aSolidsAndSubs.push_back(std::pair<GeomShapePtr, ListOfShape>(aParent, ListOfShape()));
141       aFound = &(aSolidsAndSubs.back().second);
142     }
143
144     ListOfShape anEdgesAndVertices;
145     collectSubs(anObject, anEdgesAndVertices, GeomAPI_Shape::EDGE);
146     collectSubs(anObject, anEdgesAndVertices, GeomAPI_Shape::VERTEX);
147     for (ListOfShape::iterator aEIt = anEdgesAndVertices.begin();
148          aEIt != anEdgesAndVertices.end(); ++aEIt)
149       aFound->push_back(*aEIt);
150   }
151
152   bool isFixedRadius = true;
153   double aRadius1 = 0.0, aRadius2 = 0.0;
154   if (aCreationMethod->value() == CREATION_METHOD_SINGLE_RADIUS())
155     aRadius1 = real(RADIUS_ID())->value();
156   else {
157     aRadius1 = real(START_RADIUS_ID())->value();
158     aRadius2 = real(END_RADIUS_ID())->value();
159     isFixedRadius = false;
160   }
161
162   // Perform fillet operation
163   GeomAlgoAPI_MakeShapeList aMakeShapeList;
164   std::shared_ptr<GeomAlgoAPI_Fillet> aFilletBuilder;
165   int aResultIndex = 0;
166   std::string anError;
167
168   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
169   ListOfShape anOriginalShapesList, aResultShapesList;
170
171   std::list<std::pair<GeomShapePtr, ListOfShape> >::iterator anIt = aSolidsAndSubs.begin();
172   for (; anIt != aSolidsAndSubs.end(); ++anIt) {
173     GeomShapePtr aSolid = anIt->first;
174     ListOfShape aFilletEdgesAndVertices = anIt->second;
175
176     ListOfShape aFilletEdges = selectEdges(aFilletEdgesAndVertices);
177     if (isFixedRadius)
178       aFilletBuilder.reset(new GeomAlgoAPI_Fillet(aSolid, aFilletEdges, aRadius1));
179     else
180       aFilletBuilder.reset(new GeomAlgoAPI_Fillet(aSolid, aFilletEdges, aRadius1, aRadius2));
181
182     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilletBuilder, getKind(), anError)) {
183       setError(anError);
184       return;
185     }
186
187     GeomShapePtr aResult = unwrapCompound(aFilletBuilder->shape());
188     std::shared_ptr<ModelAPI_ResultBody> aResultBody =
189         document()->createBody(data(), aResultIndex);
190
191     ListOfShape aBaseShapes;
192     aBaseShapes.push_back(aSolid);
193     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
194                                              aFilletBuilder, aResult, "Fillet");
195
196     setResult(aResultBody, aResultIndex);
197     aResultIndex++;
198
199     FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
200     aRBA.resultBody = aResultBody;
201     aRBA.baseShape = aSolid;
202     aRBA.makeShape = aFilletBuilder;
203     aResultBaseAlgoList.push_back(aRBA);
204     aResultShapesList.push_back(aResult);
205     anOriginalShapesList.push_back(aSolid);
206
207     const std::string aFilletFaceName = "Fillet";
208     ListOfShape::iterator aSelectedBase = aFilletEdges.begin();
209     for(; aSelectedBase != aFilletEdges.end(); aSelectedBase++) {
210       GeomShapePtr aBase = *aSelectedBase;
211       // Store new faces generated from edges and vertices
212       aResultBody->loadGeneratedShapes(
213         aFilletBuilder, aBase, GeomAPI_Shape::EDGE, aFilletFaceName, true);
214     }
215   }
216
217   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
218   // result shape has been deleted, but in another it was modified or stayed.
219   GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
220   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
221       anOriginalShapesList, aResultShapesCompound);
222
223   removeResults(aResultIndex);
224 }