Salome HOME
ddee74a75da2537490153226c09d9130b705d355
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Fillet.cpp
1 // Copyright (C) 2017-2021  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "FeaturesPlugin_Fillet.h"
21
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Validator.h>
27
28 #include <GeomAlgoAPI_Fillet.h>
29 #include <GeomAlgoAPI_MakeShape.h>
30 #include <GeomAlgoAPI_Tools.h>
31
32 #include <GeomAPI_ShapeExplorer.h>
33
34 // Extract edges from the list
35 static ListOfShape extractEdges(const ListOfShape& theShapes)
36 {
37   ListOfShape anEdges;
38   for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt)
39     for (GeomAPI_ShapeExplorer anExp(*anIt, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
40       anEdges.push_back(anExp.current());
41   return anEdges;
42 }
43
44
45 FeaturesPlugin_Fillet::FeaturesPlugin_Fillet()
46 {
47 }
48
49 void FeaturesPlugin_Fillet::initAttributes()
50 {
51   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
52   AttributePtr aSelectionList =
53       data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
54   data()->addAttribute(START_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
55   data()->addAttribute(END_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
56
57   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_RADIUS_ID());
58
59   initVersion(aSelectionList);
60 }
61
62 AttributePtr FeaturesPlugin_Fillet::objectsAttribute()
63 {
64   return attribute(OBJECT_LIST_ID());
65 }
66
67 const std::string& FeaturesPlugin_Fillet::modifiedShapePrefix() const
68 {
69   static const std::string& THE_PREFIX("Fillet");
70   return THE_PREFIX;
71 }
72
73 GeomMakeShapePtr FeaturesPlugin_Fillet::performOperation(const GeomShapePtr& theSolid,
74                                                          const ListOfShape& theEdges)
75 {
76   AttributeStringPtr aCreationMethod = string(CREATION_METHOD());
77   if (!aCreationMethod)
78     return GeomMakeShapePtr();
79
80   bool isFixedRadius = aCreationMethod->value() == CREATION_METHOD_SINGLE_RADIUS();
81   double aRadius1 = 0.0, aRadius2 = 0.0;
82   if (isFixedRadius)
83     aRadius1 = real(RADIUS_ID())->value();
84   else {
85     aRadius1 = real(START_RADIUS_ID())->value();
86     aRadius2 = real(END_RADIUS_ID())->value();
87   }
88
89   // Perform fillet operation
90   std::shared_ptr<GeomAlgoAPI_Fillet> aFilletBuilder;
91   std::string anError;
92
93   ListOfShape aFilletEdges = extractEdges(theEdges);
94   if (isFixedRadius)
95     aFilletBuilder.reset(new GeomAlgoAPI_Fillet(theSolid, aFilletEdges, aRadius1));
96   else
97     aFilletBuilder.reset(new GeomAlgoAPI_Fillet(theSolid, aFilletEdges, aRadius1, aRadius2));
98
99   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilletBuilder, getKind(), anError)) {
100     setError(anError);
101     return GeomMakeShapePtr();
102   }
103   return aFilletBuilder;
104 }