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