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"
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_ResultCompSolid.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
31 #include <ModelAPI_Validator.h>
33 #include <GeomAlgoAPI_Fillet.h>
34 #include <GeomAlgoAPI_MakeShapeList.h>
36 #include <GeomAPI_DataMapOfShapeMapOfShapes.h>
37 #include <GeomAPI_ShapeExplorer.h>
38 #include <GeomAPI_ShapeIterator.h>
41 // Obtain all sub-shapes from the shape and append them to the list
42 static void collectSubs(const GeomShapePtr& theShape,
44 const GeomAPI_Shape::ShapeType theShapeType)
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);
55 // Extract edges from the list
56 static ListOfShape selectEdges(const ListOfShape& theShapes)
59 for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt)
60 if ((*anIt)->isEdge())
61 anEdges.push_back(*anIt);
65 // If theShape is a compound of single shape, return it
66 static GeomShapePtr unwrapCompound(const GeomShapePtr& theShape)
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())
75 if(aSubResultsNb == 1) {
77 aShape = anIt.current();
84 FeaturesPlugin_Fillet::FeaturesPlugin_Fillet()
88 void FeaturesPlugin_Fillet::initAttributes()
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());
95 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_RADIUS_ID());
99 void FeaturesPlugin_Fillet::execute()
101 AttributeStringPtr aCreationMethod = string(CREATION_METHOD());
102 if (!aCreationMethod)
105 GeomAPI_DataMapOfShapeMapOfShapes aSolidsAndSubs;
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();
115 ResultPtr aContext = anObjectAttr->context();
116 ResultCompSolidPtr aCtxOwner = ModelAPI_Tools::compSolidOwner(aContext);
117 GeomShapePtr aParent = aCtxOwner ? aCtxOwner->shape() : aContext->shape();
121 ListOfShape anEdgesAndVertices;
122 collectSubs(anObject, anEdgesAndVertices, GeomAPI_Shape::EDGE);
123 collectSubs(anObject, anEdgesAndVertices, GeomAPI_Shape::VERTEX);
124 for (ListOfShape::iterator aEIt = anEdgesAndVertices.begin();
125 aEIt != anEdgesAndVertices.end(); ++aEIt)
126 aSolidsAndSubs.add(aParent, *aEIt);
129 bool isFixedRadius = true;
130 double aRadius1 = 0.0, aRadius2 = 0.0;
131 if (aCreationMethod->value() == CREATION_METHOD_SINGLE_RADIUS())
132 aRadius1 = real(RADIUS_ID())->value();
134 aRadius1 = real(START_RADIUS_ID())->value();
135 aRadius2 = real(END_RADIUS_ID())->value();
136 isFixedRadius = false;
139 // Perform fillet operation
140 GeomAlgoAPI_MakeShapeList aMakeShapeList;
141 std::shared_ptr<GeomAlgoAPI_Fillet> aFilletBuilder;
142 int aResultIndex = 0;
144 GeomAPI_DataMapOfShapeMapOfShapes::iterator anIt = aSolidsAndSubs.begin();
145 for (; anIt != aSolidsAndSubs.end(); ++anIt) {
146 GeomShapePtr aSolid = anIt.first();
147 ListOfShape aFilletEdgesAndVertices = anIt.second();
149 ListOfShape aFilletEdges = selectEdges(aFilletEdgesAndVertices);
151 aFilletBuilder.reset(new GeomAlgoAPI_Fillet(aSolid, aFilletEdges, aRadius1));
153 aFilletBuilder.reset(new GeomAlgoAPI_Fillet(aSolid, aFilletEdges, aRadius1, aRadius2));
154 if (isFailed(aFilletBuilder))
157 GeomShapePtr aResult = unwrapCompound(aFilletBuilder->shape());
158 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
159 document()->createBody(data(), aResultIndex);
161 loadNamingDS(aResultBody, aSolid, aFilletEdgesAndVertices, aResult, aFilletBuilder);
162 setResult(aResultBody, aResultIndex);
165 removeResults(aResultIndex);
168 bool FeaturesPlugin_Fillet::isFailed(
169 const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgorithm)
171 if (!theAlgorithm->isDone()) {
172 static const std::string aFeatureError = "Error: fillet algorithm failed.";
173 setError(aFeatureError);
176 if (theAlgorithm->shape()->isNull()) {
177 static const std::string aShapeError = "Error: Resulting shape of fillet is Null.";
178 setError(aShapeError);
181 if (!theAlgorithm->isValid()) {
182 std::string aFeatureError = "Error: Resulting shape of fillet is not valid.";
183 setError(aFeatureError);
189 void FeaturesPlugin_Fillet::loadNamingDS(
190 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
191 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
192 const ListOfShape& theFilletShapes,
193 const std::shared_ptr<GeomAPI_Shape> theResultShape,
194 const std::shared_ptr<GeomAlgoAPI_MakeShape>& theMakeShape)
197 if(theBaseShape->isEqual(theResultShape)) {
198 theResultBody->store(theResultShape, false);
202 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfShapes = theMakeShape->mapOfSubShapes();
204 const int aDeletedTag = 1;
205 const int aModifyTag = 2;
206 const int aGeneratedTag = 3;
207 /// sub solids will be placed at labels 4, 5, etc. if result is compound of solids
208 const int aSubsolidsTag = 4;
210 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
211 theResultBody->storeGenerated(theBaseShape, theResultShape);
213 const std::string aModFaceName = "Modified_Face";
214 const std::string aFilletFaceName = "Fillet_Face";
216 // Store modified faces
217 theResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), theBaseShape,
218 GeomAPI_Shape::FACE, aModifyTag, aModFaceName, *aMapOfShapes);
220 // Store new faces generated from edges and vertices
221 theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape,
222 GeomAPI_Shape::EDGE, aGeneratedTag, aFilletFaceName, *aMapOfShapes);
223 theResultBody->loadAndOrientGeneratedShapes(theMakeShape.get(), theBaseShape,
224 GeomAPI_Shape::VERTEX, aGeneratedTag, aFilletFaceName, *aMapOfShapes);
227 theResultBody->loadDeletedShapes(theMakeShape.get(), theBaseShape,
228 GeomAPI_Shape::EDGE, aDeletedTag);
229 theResultBody->loadDeletedShapes(theMakeShape.get(), theBaseShape,
230 GeomAPI_Shape::FACE, aDeletedTag);