1 // Copyright (C) 2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_Fillet.h"
22 #include "FeaturesPlugin_Tools.h"
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>
33 #include <GeomAlgoAPI_CompoundBuilder.h>
34 #include <GeomAlgoAPI_Fillet.h>
35 #include <GeomAlgoAPI_MakeShapeList.h>
36 #include <GeomAlgoAPI_Tools.h>
38 #include <GeomAPI_DataMapOfShapeMapOfShapes.h>
39 #include <GeomAPI_ShapeExplorer.h>
40 #include <GeomAPI_ShapeIterator.h>
43 // Obtain all sub-shapes from the shape and append them to the list
44 static void collectSubs(const GeomShapePtr& theShape,
46 const GeomAPI_Shape::ShapeType theShapeType)
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);
57 // Extract edges from the list
58 static ListOfShape selectEdges(const ListOfShape& theShapes)
61 for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt)
62 if ((*anIt)->isEdge())
63 anEdges.push_back(*anIt);
67 // If theShape is a compound of a single sub-shape, return this sub-shape
68 static GeomShapePtr unwrapCompound(const GeomShapePtr& theShape)
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())
77 if(aSubResultsNb == 1) {
79 aShape = anIt.current();
86 FeaturesPlugin_Fillet::FeaturesPlugin_Fillet()
90 void FeaturesPlugin_Fillet::initAttributes()
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());
97 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_RADIUS_ID());
101 void FeaturesPlugin_Fillet::execute()
103 AttributeStringPtr aCreationMethod = string(CREATION_METHOD());
104 if (!aCreationMethod)
107 std::list<std::pair<GeomShapePtr, ListOfShape> > aSolidsAndSubs;
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();
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();
131 // searching this parent is already in the list aSolidsAndSubs
132 std::list<std::pair<GeomShapePtr, ListOfShape> >::iterator aSearch = aSolidsAndSubs.begin();
134 for(; aSearch != aSolidsAndSubs.end(); aSearch++) {
135 if (aSearch->first->isSame(aParent)) {
136 aFound = &(aSearch->second);
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);
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);
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();
158 aRadius1 = real(START_RADIUS_ID())->value();
159 aRadius2 = real(END_RADIUS_ID())->value();
160 isFixedRadius = false;
163 // Perform fillet operation
164 GeomAlgoAPI_MakeShapeList aMakeShapeList;
165 std::shared_ptr<GeomAlgoAPI_Fillet> aFilletBuilder;
166 int aResultIndex = 0;
169 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
170 ListOfShape anOriginalShapesList, aResultShapesList;
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;
177 ListOfShape aFilletEdges = selectEdges(aFilletEdgesAndVertices);
179 aFilletBuilder.reset(new GeomAlgoAPI_Fillet(aSolid, aFilletEdges, aRadius1));
181 aFilletBuilder.reset(new GeomAlgoAPI_Fillet(aSolid, aFilletEdges, aRadius1, aRadius2));
183 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilletBuilder, getKind(), anError)) {
188 GeomShapePtr aResult = unwrapCompound(aFilletBuilder->shape());
189 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
190 document()->createBody(data(), aResultIndex);
192 ListOfShape aBaseShapes;
193 aBaseShapes.push_back(aSolid);
194 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
195 aFilletBuilder, aResult, "Fillet");
197 setResult(aResultBody, aResultIndex);
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);
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);
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);
224 removeResults(aResultIndex);