1 // Copyright (C) 2017-2019 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 email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_Fillet.h"
21 #include "FeaturesPlugin_Tools.h"
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>
32 #include <GeomAlgoAPI_CompoundBuilder.h>
33 #include <GeomAlgoAPI_Fillet.h>
34 #include <GeomAlgoAPI_MakeShapeList.h>
35 #include <GeomAlgoAPI_Tools.h>
37 #include <GeomAPI_DataMapOfShapeMapOfShapes.h>
38 #include <GeomAPI_ShapeExplorer.h>
39 #include <GeomAPI_ShapeIterator.h>
42 // Obtain all sub-shapes from the shape and append them to the list
43 static void collectSubs(const GeomShapePtr& theShape,
45 const GeomAPI_Shape::ShapeType theShapeType)
47 GeomAPI_ShapeExplorer anExp(theShape, theShapeType);
48 for (; anExp.more(); anExp.next()) {
49 GeomShapePtr aShape = anExp.current();
50 // Store all shapes with FORWARD orientation to avoid duplication of shared edges/vertices
51 aShape->setOrientation(GeomAPI_Shape::FORWARD);
52 theSubs.push_back(aShape);
56 // Extract edges from the list
57 static ListOfShape selectEdges(const ListOfShape& theShapes)
60 for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt)
61 if ((*anIt)->isEdge())
62 anEdges.push_back(*anIt);
66 // If theShape is a compound of a single sub-shape, return this sub-shape
67 static GeomShapePtr unwrapCompound(const GeomShapePtr& theShape)
69 GeomShapePtr aShape = theShape;
70 if(aShape->shapeType() == GeomAPI_Shape::COMPOUND) {
71 int aSubResultsNb = 0;
72 GeomAPI_ShapeIterator anIt(aShape);
73 for(; anIt.more(); anIt.next())
76 if(aSubResultsNb == 1) {
78 aShape = anIt.current();
85 FeaturesPlugin_Fillet::FeaturesPlugin_Fillet()
89 void FeaturesPlugin_Fillet::initAttributes()
91 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
92 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
93 data()->addAttribute(START_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
94 data()->addAttribute(END_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
96 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_RADIUS_ID());
100 void FeaturesPlugin_Fillet::execute()
102 AttributeStringPtr aCreationMethod = string(CREATION_METHOD());
103 if (!aCreationMethod)
106 std::list<std::pair<GeomShapePtr, ListOfShape> > aSolidsAndSubs;
108 // getting objects and sort them according to parent solids
109 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
110 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); ++anObjectsIndex) {
111 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
112 GeomShapePtr anObject = anObjectAttr->value();
116 ResultPtr aContext = anObjectAttr->context();
117 GeomShapePtr aParent;
118 if (aContext.get()) {
119 ResultBodyPtr aCtxOwner = ModelAPI_Tools::bodyOwner(aContext);
120 aParent = aCtxOwner ? aCtxOwner->shape() : aContext->shape();
121 } else { // get it from a feature
122 FeaturePtr aFeature = anObjectAttr->contextFeature();
123 if (aFeature.get()) {
124 aParent = aFeature->firstResult()->shape();
130 // searching this parent is already in the list aSolidsAndSubs
131 std::list<std::pair<GeomShapePtr, ListOfShape> >::iterator aSearch = aSolidsAndSubs.begin();
133 for(; aSearch != aSolidsAndSubs.end(); aSearch++) {
134 if (aSearch->first->isSame(aParent)) {
135 aFound = &(aSearch->second);
139 if (aSearch == aSolidsAndSubs.end()) { // not found, so, add a new one
140 aSolidsAndSubs.push_back(std::pair<GeomShapePtr, ListOfShape>(aParent, ListOfShape()));
141 aFound = &(aSolidsAndSubs.back().second);
144 ListOfShape anEdgesAndVertices;
145 collectSubs(anObject, anEdgesAndVertices, GeomAPI_Shape::EDGE);
146 collectSubs(anObject, anEdgesAndVertices, GeomAPI_Shape::VERTEX);
147 for (ListOfShape::iterator aEIt = anEdgesAndVertices.begin();
148 aEIt != anEdgesAndVertices.end(); ++aEIt)
149 aFound->push_back(*aEIt);
152 bool isFixedRadius = true;
153 double aRadius1 = 0.0, aRadius2 = 0.0;
154 if (aCreationMethod->value() == CREATION_METHOD_SINGLE_RADIUS())
155 aRadius1 = real(RADIUS_ID())->value();
157 aRadius1 = real(START_RADIUS_ID())->value();
158 aRadius2 = real(END_RADIUS_ID())->value();
159 isFixedRadius = false;
162 // Perform fillet operation
163 GeomAlgoAPI_MakeShapeList aMakeShapeList;
164 std::shared_ptr<GeomAlgoAPI_Fillet> aFilletBuilder;
165 int aResultIndex = 0;
168 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
169 ListOfShape anOriginalShapesList, aResultShapesList;
171 std::list<std::pair<GeomShapePtr, ListOfShape> >::iterator anIt = aSolidsAndSubs.begin();
172 for (; anIt != aSolidsAndSubs.end(); ++anIt) {
173 GeomShapePtr aSolid = anIt->first;
174 ListOfShape aFilletEdgesAndVertices = anIt->second;
176 ListOfShape aFilletEdges = selectEdges(aFilletEdgesAndVertices);
178 aFilletBuilder.reset(new GeomAlgoAPI_Fillet(aSolid, aFilletEdges, aRadius1));
180 aFilletBuilder.reset(new GeomAlgoAPI_Fillet(aSolid, aFilletEdges, aRadius1, aRadius2));
182 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilletBuilder, getKind(), anError)) {
187 GeomShapePtr aResult = unwrapCompound(aFilletBuilder->shape());
188 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
189 document()->createBody(data(), aResultIndex);
191 ListOfShape aBaseShapes;
192 aBaseShapes.push_back(aSolid);
193 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
194 aFilletBuilder, aResult, "Fillet");
196 setResult(aResultBody, aResultIndex);
199 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
200 aRBA.resultBody = aResultBody;
201 aRBA.baseShape = aSolid;
202 aRBA.makeShape = aFilletBuilder;
203 aResultBaseAlgoList.push_back(aRBA);
204 aResultShapesList.push_back(aResult);
205 anOriginalShapesList.push_back(aSolid);
207 const std::string aFilletFaceName = "Fillet";
208 ListOfShape::iterator aSelectedBase = aFilletEdges.begin();
209 for(; aSelectedBase != aFilletEdges.end(); aSelectedBase++) {
210 GeomShapePtr aBase = *aSelectedBase;
211 // Store new faces generated from edges and vertices
212 aResultBody->loadGeneratedShapes(
213 aFilletBuilder, aBase, GeomAPI_Shape::EDGE, aFilletFaceName, true);
217 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
218 // result shape has been deleted, but in another it was modified or stayed.
219 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
220 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
221 anOriginalShapesList, aResultShapesCompound);
223 removeResults(aResultIndex);