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_Chamfer.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_Chamfer.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>
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 a single sub-shape, return this sub-shape
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_Chamfer::FeaturesPlugin_Chamfer()
88 void FeaturesPlugin_Chamfer::initAttributes()
90 data()->addAttribute(FeaturesPlugin_Chamfer::CREATION_METHOD(),
91 ModelAPI_AttributeString::typeId());
92 data()->addAttribute(FeaturesPlugin_Chamfer::OBJECT_LIST_ID(),
93 ModelAPI_AttributeSelectionList::typeId());
94 data()->addAttribute(FeaturesPlugin_Chamfer::D1_ID(), ModelAPI_AttributeDouble::typeId());
95 data()->addAttribute(FeaturesPlugin_Chamfer::D2_ID(), ModelAPI_AttributeDouble::typeId());
96 data()->addAttribute(FeaturesPlugin_Chamfer::D_ID(), ModelAPI_AttributeDouble::typeId());
97 data()->addAttribute(FeaturesPlugin_Chamfer::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
101 void FeaturesPlugin_Chamfer::execute()
103 AttributeStringPtr aCreationMethod = string(CREATION_METHOD());
104 if (!aCreationMethod)
107 std::list<std::pair<GeomShapePtr, ListOfShape> > aSolidsAndSubs;
108 std::map<GeomShapePtr, GeomShapePtr> aMapEdgeFace;
110 // getting objects and sort them according to parent solids
111 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
112 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); ++anObjectsIndex) {
113 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
114 GeomShapePtr anObject = anObjectAttr->value();
118 ResultPtr aContext = anObjectAttr->context();
119 GeomShapePtr aParent;
120 if (aContext.get()) {
121 ResultBodyPtr aCtxOwner = ModelAPI_Tools::bodyOwner(aContext);
122 aParent = aCtxOwner ? aCtxOwner->shape() : aContext->shape();
123 } else { // get it from a feature
124 FeaturePtr aFeature = anObjectAttr->contextFeature();
125 if (aFeature.get()) {
126 aParent = aFeature->firstResult()->shape();
132 // searching this parent is already in the list aSolidsAndSubs
133 std::list<std::pair<GeomShapePtr, ListOfShape> >::iterator aSearch = aSolidsAndSubs.begin();
135 for(; aSearch != aSolidsAndSubs.end(); aSearch++) {
136 if (aSearch->first->isSame(aParent)) {
137 aFound = &(aSearch->second);
141 if (aSearch == aSolidsAndSubs.end()) { // not found, so, add a new one
142 aSolidsAndSubs.push_back(std::pair<GeomShapePtr, ListOfShape>(aParent, ListOfShape()));
143 aFound = &(aSolidsAndSubs.back().second);
146 ListOfShape anEdgesAndVertices;
147 collectSubs(anObject, anEdgesAndVertices, GeomAPI_Shape::EDGE);
148 collectSubs(anObject, anEdgesAndVertices, GeomAPI_Shape::VERTEX);
149 for (ListOfShape::iterator aEIt = anEdgesAndVertices.begin();
150 aEIt != anEdgesAndVertices.end(); ++aEIt)
151 aFound->push_back(*aEIt);
153 if (anObject->isFace()) {
154 for (ListOfShape::iterator aEIt = anEdgesAndVertices.begin();
155 aEIt != anEdgesAndVertices.end(); ++aEIt) {
156 if ((*aEIt)->isEdge()) {
157 aMapEdgeFace[(*aEIt)] = anObject;
163 //bool isOption1 = true;
164 double aD1 = 0.0, aD2 = 0.0, aD = 0.0, anAngle = 0.0;
165 if (aCreationMethod->value() == CREATION_METHOD_DISTANCE_DISTANCE()) {
166 aD1 = real(FeaturesPlugin_Chamfer::D1_ID())->value();
167 aD2 = real(FeaturesPlugin_Chamfer::D2_ID())->value();
169 aD = real(FeaturesPlugin_Chamfer::D_ID())->value();
170 anAngle = real(FeaturesPlugin_Chamfer::ANGLE_ID())->value();
173 // Perform chamfer operation
174 GeomAlgoAPI_MakeShapeList aMakeShapeList;
175 std::shared_ptr<GeomAlgoAPI_Chamfer> aChamferBuilder;
176 int aResultIndex = 0;
180 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
181 ListOfShape anOriginalShapesList, aResultShapesList;
183 std::list<std::pair<GeomShapePtr, ListOfShape> >::iterator anIt = aSolidsAndSubs.begin();
184 for (; anIt != aSolidsAndSubs.end(); ++anIt) {
185 GeomShapePtr aSolid = anIt->first;
186 ListOfShape aFilletEdgesAndVertices = anIt->second;
188 ListOfShape aFilletEdges = selectEdges(aFilletEdgesAndVertices);
189 if (aCreationMethod->value() == CREATION_METHOD_DISTANCE_DISTANCE()) {
190 aChamferBuilder.reset(new GeomAlgoAPI_Chamfer(
191 aSolid, aFilletEdges, aMapEdgeFace, true, aD1, aD2));
193 aChamferBuilder.reset(new GeomAlgoAPI_Chamfer(
194 aSolid, aFilletEdges, aMapEdgeFace, false, aD, anAngle));
197 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aChamferBuilder, getKind(), anError)) {
202 GeomShapePtr aResult = unwrapCompound(aChamferBuilder->shape());
203 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
204 document()->createBody(data(), aResultIndex);
206 ListOfShape aBaseShapes;
207 aBaseShapes.push_back(aSolid);
208 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
209 aChamferBuilder, aResult, "Chamfer");
211 setResult(aResultBody, aResultIndex);
214 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
215 aRBA.resultBody = aResultBody;
216 aRBA.baseShape = aSolid;
217 aRBA.makeShape = aChamferBuilder;
218 aResultBaseAlgoList.push_back(aRBA);
219 aResultShapesList.push_back(aResult);
220 anOriginalShapesList.push_back(aSolid);
222 const std::string aFilletFaceName = "Chamfer";
223 ListOfShape::iterator aSelectedBase = aFilletEdges.begin();
224 for(; aSelectedBase != aFilletEdges.end(); aSelectedBase++) {
225 GeomShapePtr aBase = *aSelectedBase;
226 // Store new faces generated from edges and vertices
227 aResultBody->loadGeneratedShapes(
228 aChamferBuilder, aBase, GeomAPI_Shape::EDGE, aFilletFaceName, true);
232 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
233 // result shape has been deleted, but in another it was modified or stayed.
234 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
235 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
236 anOriginalShapesList, aResultShapesCompound);
238 removeResults(aResultIndex);