]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Fillet.cpp
Salome HOME
Corrections and tests for the Fillet feature history: make generated faces present...
[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   std::list<std::pair<GeomShapePtr, ListOfShape> > aSolidsAndSubs;
108
109   // getting objects and sort them according 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     // searching this parent is already in the list aSolidsAndSubs
132     std::list<std::pair<GeomShapePtr, ListOfShape> >::iterator aSearch = aSolidsAndSubs.begin();
133     ListOfShape* aFound;
134     for(; aSearch != aSolidsAndSubs.end(); aSearch++) {
135       if (aSearch->first->isSame(aParent)) {
136         aFound = &(aSearch->second);
137         break;
138       }
139     }
140     if (aSearch == aSolidsAndSubs.end()) { // not found, so, add a new one
141       aSolidsAndSubs.push_back(std::pair<GeomShapePtr, ListOfShape>(aParent, ListOfShape()));
142       aFound = &(aSolidsAndSubs.back().second);
143     }
144
145     ListOfShape anEdgesAndVertices;
146     collectSubs(anObject, anEdgesAndVertices, GeomAPI_Shape::EDGE);
147     collectSubs(anObject, anEdgesAndVertices, GeomAPI_Shape::VERTEX);
148     for (ListOfShape::iterator aEIt = anEdgesAndVertices.begin();
149          aEIt != anEdgesAndVertices.end(); ++aEIt)
150       aFound->push_back(*aEIt);
151   }
152
153   bool isFixedRadius = true;
154   double aRadius1 = 0.0, aRadius2 = 0.0;
155   if (aCreationMethod->value() == CREATION_METHOD_SINGLE_RADIUS())
156     aRadius1 = real(RADIUS_ID())->value();
157   else {
158     aRadius1 = real(START_RADIUS_ID())->value();
159     aRadius2 = real(END_RADIUS_ID())->value();
160     isFixedRadius = false;
161   }
162
163   // Perform fillet operation
164   GeomAlgoAPI_MakeShapeList aMakeShapeList;
165   std::shared_ptr<GeomAlgoAPI_Fillet> aFilletBuilder;
166   int aResultIndex = 0;
167   std::string anError;
168
169   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
170   ListOfShape anOriginalShapesList, aResultShapesList;
171
172   std::list<std::pair<GeomShapePtr, ListOfShape> >::iterator anIt = aSolidsAndSubs.begin();
173   for (; anIt != aSolidsAndSubs.end(); ++anIt) {
174     GeomShapePtr aSolid = anIt->first;
175     ListOfShape aFilletEdgesAndVertices = anIt->second;
176
177     ListOfShape aFilletEdges = selectEdges(aFilletEdgesAndVertices);
178     if (isFixedRadius)
179       aFilletBuilder.reset(new GeomAlgoAPI_Fillet(aSolid, aFilletEdges, aRadius1));
180     else
181       aFilletBuilder.reset(new GeomAlgoAPI_Fillet(aSolid, aFilletEdges, aRadius1, aRadius2));
182
183     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilletBuilder, getKind(), anError)) {
184       setError(anError);
185       return;
186     }
187
188     GeomShapePtr aResult = unwrapCompound(aFilletBuilder->shape());
189     std::shared_ptr<ModelAPI_ResultBody> aResultBody =
190         document()->createBody(data(), aResultIndex);
191
192     ListOfShape aBaseShapes;
193     aBaseShapes.push_back(aSolid);
194     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
195                                              aFilletBuilder, aResult, "Fillet");
196
197     setResult(aResultBody, aResultIndex);
198     aResultIndex++;
199
200     FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
201     aRBA.resultBody = aResultBody;
202     aRBA.baseShape = aSolid;
203     aRBA.makeShape = aFilletBuilder;
204     aResultBaseAlgoList.push_back(aRBA);
205     aResultShapesList.push_back(aResult);
206     anOriginalShapesList.push_back(aSolid);
207
208     const std::string aFilletFaceName = "Fillet";
209     ListOfShape::iterator aSelectedBase = aFilletEdges.begin();
210     for(; aSelectedBase != aFilletEdges.end(); aSelectedBase++) {
211       GeomShapePtr aBase = *aSelectedBase;
212       // Store new faces generated from edges and vertices
213       aResultBody->loadGeneratedShapes(
214         aFilletBuilder, aBase, GeomAPI_Shape::EDGE, aFilletFaceName, true);
215     }
216   }
217
218   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
219   // result shape has been deleted, but in another it was modified or stayed.
220   GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
221   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
222       anOriginalShapesList, aResultShapesCompound);
223
224   removeResults(aResultIndex);
225 }