Salome HOME
59c1c8e29a287fee81f4e857d59f8e147256734e
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_Fillet.cpp
1 // Copyright (C) 2017-2023  CEA, EDF
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 "FeaturesAPI_Fillet.h"
21
22 #include <ModelHighAPI_Double.h>
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Selection.h>
25 #include <ModelHighAPI_Tools.h>
26
27 static GeomAPI_Shape::ShapeType typeOfSelection(
28     const std::list<ModelHighAPI_Selection>& theBaseObjects)
29 {
30   std::string aType = theBaseObjects.empty() ? "SHAPE" : theBaseObjects.front().shapeType();
31   return GeomAPI_Shape::shapeTypeByStr(aType);
32 }
33
34 //==================================================================================================
35
36 FeaturesAPI_Fillet::FeaturesAPI_Fillet(const std::shared_ptr<ModelAPI_Feature>& theFeature)
37   : ModelHighAPI_Interface(theFeature)
38 {
39 }
40
41 //==================================================================================================
42
43 FeaturesAPI_Fillet1D::FeaturesAPI_Fillet1D(const std::shared_ptr<ModelAPI_Feature>& theFeature)
44   : FeaturesAPI_Fillet(theFeature)
45 {
46   initialize();
47 }
48
49 FeaturesAPI_Fillet1D::FeaturesAPI_Fillet1D(const std::shared_ptr<ModelAPI_Feature>& theFeature,
50                                            const std::list<ModelHighAPI_Selection>& theBaseObjects,
51                                            const ModelHighAPI_Double& theRadius)
52   : FeaturesAPI_Fillet(theFeature)
53 {
54   if (initialize()) {
55     setBase(theBaseObjects);
56     fillAttribute(theRadius, myradius);
57
58     execIfBaseNotEmpty();
59   }
60 }
61
62 FeaturesAPI_Fillet1D::~FeaturesAPI_Fillet1D()
63 {
64 }
65
66 void FeaturesAPI_Fillet1D::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
67 {
68   mybaseWires->clear();
69   mybaseVertices->clear();
70
71   if (typeOfSelection(theBaseObjects) == GeomAPI_Shape::WIRE) {
72     fillAttribute(FeaturesPlugin_Fillet1D::CREATION_BY_WIRES(), mycreationMethod);
73     fillAttribute(theBaseObjects, mybaseWires);
74   }
75   else {
76     fillAttribute(FeaturesPlugin_Fillet1D::CREATION_BY_VERTICES(), mycreationMethod);
77     fillAttribute(theBaseObjects, mybaseVertices);
78   }
79
80   execIfBaseNotEmpty();
81 }
82
83 void FeaturesAPI_Fillet1D::setRadius(const ModelHighAPI_Double& theRadius)
84 {
85   fillAttribute(theRadius, myradius);
86   execIfBaseNotEmpty();
87 }
88
89 void FeaturesAPI_Fillet1D::execIfBaseNotEmpty()
90 {
91   if (mybaseWires->size() > 0 || mybaseVertices->size() > 0)
92     execute();
93 }
94
95 void FeaturesAPI_Fillet1D::dump(ModelHighAPI_Dumper& theDumper) const
96 {
97   FeaturePtr aBase = feature();
98   const std::string& aDocName = theDumper.name(aBase->document());
99
100   AttributeSelectionListPtr anAttrObjects;
101   if (creationMethod()->value() == FeaturesPlugin_Fillet1D::CREATION_BY_WIRES())
102     anAttrObjects = aBase->selectionList(FeaturesPlugin_Fillet1D::WIRE_LIST_ID());
103   else if (creationMethod()->value() == FeaturesPlugin_Fillet1D::CREATION_BY_VERTICES())
104     anAttrObjects = aBase->selectionList(FeaturesPlugin_Fillet1D::VERTEX_LIST_ID());
105
106   AttributeDoublePtr anAttrRadius = aBase->real(FeaturesPlugin_Fillet1D::RADIUS_ID());
107
108   theDumper << aBase << " = model.addFillet(" << aDocName << ", " << anAttrObjects
109                                                           << ", " << anAttrRadius;
110
111   if (!aBase->data()->version().empty())
112     theDumper << ", keepSubResults = True";
113
114   theDumper << ")" << std::endl;
115 }
116
117 //==================================================================================================
118
119 FeaturesAPI_Fillet2D::FeaturesAPI_Fillet2D(const std::shared_ptr<ModelAPI_Feature>& theFeature)
120   : FeaturesAPI_Fillet(theFeature)
121 {
122   initialize();
123 }
124
125 FeaturesAPI_Fillet2D::FeaturesAPI_Fillet2D(const std::shared_ptr<ModelAPI_Feature>& theFeature,
126                                            const std::list<ModelHighAPI_Selection>& theBaseObjects,
127                                            const ModelHighAPI_Double& theRadius)
128   : FeaturesAPI_Fillet(theFeature)
129 {
130   if (initialize()) {
131     fillAttribute(FeaturesPlugin_Fillet::CREATION_METHOD_SINGLE_RADIUS(), mycreationMethod);
132     fillAttribute(theBaseObjects, mybaseObjects);
133     fillAttribute(theRadius, myradius);
134
135     execIfBaseNotEmpty();
136   }
137 }
138
139 FeaturesAPI_Fillet2D::FeaturesAPI_Fillet2D(const std::shared_ptr<ModelAPI_Feature>& theFeature,
140                                            const std::list<ModelHighAPI_Selection>& theBaseObjects,
141                                            const ModelHighAPI_Double& theRadius1,
142                                            const ModelHighAPI_Double& theRadius2)
143   : FeaturesAPI_Fillet(theFeature)
144 {
145   if (initialize()) {
146     fillAttribute(FeaturesPlugin_Fillet::CREATION_METHOD_VARYING_RADIUS(), mycreationMethod);
147     fillAttribute(theBaseObjects, mybaseObjects);
148     fillAttribute(theRadius1, mystartRadius);
149     fillAttribute(theRadius2, myendRadius);
150
151     execIfBaseNotEmpty();
152   }
153 }
154
155 FeaturesAPI_Fillet2D::~FeaturesAPI_Fillet2D()
156 {
157 }
158
159 void FeaturesAPI_Fillet2D::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
160 {
161   mybaseObjects->clear();
162   fillAttribute(theBaseObjects, mybaseObjects);
163
164   execIfBaseNotEmpty();
165 }
166
167 void FeaturesAPI_Fillet2D::setRadius(const ModelHighAPI_Double& theRadius)
168 {
169   fillAttribute(FeaturesPlugin_Fillet::CREATION_METHOD_SINGLE_RADIUS(), mycreationMethod);
170   fillAttribute(theRadius, myradius);
171
172   execIfBaseNotEmpty();
173 }
174
175 void FeaturesAPI_Fillet2D::setRadius(const ModelHighAPI_Double& theRadius1,
176                                      const ModelHighAPI_Double& theRadius2)
177 {
178   fillAttribute(FeaturesPlugin_Fillet::CREATION_METHOD_VARYING_RADIUS(), mycreationMethod);
179   fillAttribute(theRadius1, mystartRadius);
180   fillAttribute(theRadius2, myendRadius);
181
182   execIfBaseNotEmpty();
183 }
184
185 void FeaturesAPI_Fillet2D::dump(ModelHighAPI_Dumper& theDumper) const
186 {
187   FeaturePtr aBase = feature();
188   const std::string& aDocName = theDumper.name(aBase->document());
189
190   AttributeSelectionListPtr anAttrObjects =
191     aBase->selectionList(FeaturesPlugin_Fillet::OBJECT_LIST_ID());
192
193   theDumper << aBase << " = model.addFillet(" << aDocName << ", " << anAttrObjects;
194
195   std::string aCreationMethod = aBase->string(FeaturesPlugin_Fillet::CREATION_METHOD())->value();
196
197   if(aCreationMethod == FeaturesPlugin_Fillet::CREATION_METHOD_SINGLE_RADIUS()) {
198     AttributeDoublePtr anAttrRadius = aBase->real(FeaturesPlugin_Fillet::RADIUS_ID());
199     theDumper << ", " << anAttrRadius;
200   } else if(aCreationMethod == FeaturesPlugin_Fillet::CREATION_METHOD_VARYING_RADIUS()) {
201     AttributeDoublePtr anAttrRadius1 = aBase->real(FeaturesPlugin_Fillet::START_RADIUS_ID());
202     AttributeDoublePtr anAttrRadius2 = aBase->real(FeaturesPlugin_Fillet::END_RADIUS_ID());
203     theDumper << ", " << anAttrRadius1 << ", " << anAttrRadius2;
204   }
205
206   if (!aBase->data()->version().empty())
207     theDumper << ", keepSubResults = True";
208
209   theDumper << ")" << std::endl;
210 }
211
212 void FeaturesAPI_Fillet2D::execIfBaseNotEmpty()
213 {
214   if (mybaseObjects->size() > 0)
215     execute();
216 }
217
218
219 //==================================================================================================
220
221 FilletPtr addFillet(const std::shared_ptr<ModelAPI_Document>& thePart,
222                     const std::list<ModelHighAPI_Selection>& theBaseObjects,
223                     const ModelHighAPI_Double& theRadius1,
224                     const ModelHighAPI_Double& theRadius2,
225                     const bool keepSubResults)
226 {
227   GeomAPI_Shape::ShapeType aType = typeOfSelection(theBaseObjects);
228   bool is1D = aType == GeomAPI_Shape::WIRE || aType == GeomAPI_Shape::VERTEX;
229
230   FeaturePtr aFeature =
231       thePart->addFeature(is1D ? FeaturesAPI_Fillet1D::ID() : FeaturesAPI_Fillet2D::ID());
232   if (!keepSubResults)
233     aFeature->data()->setVersion("");
234
235   FilletPtr aFillet;
236   if (is1D)
237     aFillet.reset(new FeaturesAPI_Fillet1D(aFeature, theBaseObjects, theRadius1));
238   else if (theRadius2.value() < 0.0)
239     aFillet.reset(new FeaturesAPI_Fillet2D(aFeature, theBaseObjects, theRadius1));
240   else
241     aFillet.reset(new FeaturesAPI_Fillet2D(aFeature, theBaseObjects, theRadius1, theRadius2));
242   return aFillet;
243 }