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