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(), ModelAPI_AttributeString::typeId());
91 data()->addAttribute(FeaturesPlugin_Chamfer::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
92 data()->addAttribute(FeaturesPlugin_Chamfer::D1_ID(), ModelAPI_AttributeDouble::typeId());
93 data()->addAttribute(FeaturesPlugin_Chamfer::D2_ID(), ModelAPI_AttributeDouble::typeId());
94 data()->addAttribute(FeaturesPlugin_Chamfer::D_ID(), ModelAPI_AttributeDouble::typeId());
95 data()->addAttribute(FeaturesPlugin_Chamfer::ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
99 void FeaturesPlugin_Chamfer::execute()
101 AttributeStringPtr aCreationMethod = string(CREATION_METHOD());
102 if (!aCreationMethod)
105 std::list<std::pair<GeomShapePtr, ListOfShape> > aSolidsAndSubs;
106 std::map<GeomShapePtr, GeomShapePtr> aMapEdgeFace;
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);
151 if (anObject->isFace()) {
152 for (ListOfShape::iterator aEIt = anEdgesAndVertices.begin();
153 aEIt != anEdgesAndVertices.end(); ++aEIt) {
154 if ((*aEIt)->isEdge()) {
155 aMapEdgeFace[(*aEIt)] = anObject;
161 //bool isOption1 = true;
162 double aD1 = 0.0, aD2 = 0.0, aD = 0.0, anAngle = 0.0;
163 if (aCreationMethod->value() == CREATION_METHOD_DISTANCE_DISTANCE()) {
164 aD1 = real(FeaturesPlugin_Chamfer::D1_ID())->value();
165 aD2 = real(FeaturesPlugin_Chamfer::D2_ID())->value();
167 aD = real(FeaturesPlugin_Chamfer::D_ID())->value();
168 anAngle = real(FeaturesPlugin_Chamfer::ANGLE_ID())->value();
171 // Perform chamfer operation
172 GeomAlgoAPI_MakeShapeList aMakeShapeList;
173 std::shared_ptr<GeomAlgoAPI_Chamfer> aChamferBuilder;
174 int aResultIndex = 0;
178 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
179 ListOfShape anOriginalShapesList, aResultShapesList;
181 std::list<std::pair<GeomShapePtr, ListOfShape> >::iterator anIt = aSolidsAndSubs.begin();
182 for (; anIt != aSolidsAndSubs.end(); ++anIt) {
183 GeomShapePtr aSolid = anIt->first;
184 ListOfShape aFilletEdgesAndVertices = anIt->second;
186 ListOfShape aFilletEdges = selectEdges(aFilletEdgesAndVertices);
187 if (aCreationMethod->value() == CREATION_METHOD_DISTANCE_DISTANCE()) {
188 aChamferBuilder.reset(new GeomAlgoAPI_Chamfer(aSolid, aFilletEdges, aMapEdgeFace, true, aD1, aD2));
190 aChamferBuilder.reset(new GeomAlgoAPI_Chamfer(aSolid, aFilletEdges, aMapEdgeFace, false, aD, anAngle));
193 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aChamferBuilder, getKind(), anError)) {
198 GeomShapePtr aResult = unwrapCompound(aChamferBuilder->shape());
199 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
200 document()->createBody(data(), aResultIndex);
202 ListOfShape aBaseShapes;
203 aBaseShapes.push_back(aSolid);
204 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
205 aChamferBuilder, aResult, "Chamfer");
207 setResult(aResultBody, aResultIndex);
210 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
211 aRBA.resultBody = aResultBody;
212 aRBA.baseShape = aSolid;
213 aRBA.makeShape = aChamferBuilder;
214 aResultBaseAlgoList.push_back(aRBA);
215 aResultShapesList.push_back(aResult);
216 anOriginalShapesList.push_back(aSolid);
218 const std::string aFilletFaceName = "Chamfer";
219 ListOfShape::iterator aSelectedBase = aFilletEdges.begin();
220 for(; aSelectedBase != aFilletEdges.end(); aSelectedBase++) {
221 GeomShapePtr aBase = *aSelectedBase;
222 // Store new faces generated from edges and vertices
223 aResultBody->loadGeneratedShapes(
224 aChamferBuilder, aBase, GeomAPI_Shape::EDGE, aFilletFaceName, true);
228 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
229 // result shape has been deleted, but in another it was modified or stayed.
230 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
231 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
232 anOriginalShapesList, aResultShapesCompound);
234 removeResults(aResultIndex);