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