Salome HOME
bf28f3e1e9f7806055b724a4c2c3f56bc0bff948
[modules/shaper.git] / src / BuildAPI / BuildAPI_Interpolation.cpp
1 // Copyright (C) 2014-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 "BuildAPI_Interpolation.h"
21
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24
25 //==================================================================================================
26 BuildAPI_Interpolation::BuildAPI_Interpolation(const std::shared_ptr<ModelAPI_Feature>& theFeature)
27 : ModelHighAPI_Interface(theFeature)
28 {
29   initialize();
30 }
31
32 //==================================================================================================
33 BuildAPI_Interpolation::BuildAPI_Interpolation(const FeaturePtr& theFeature,
34                     const std::list<ModelHighAPI_Selection>& theBaseObjects,
35                     const ModelHighAPI_Selection& theStartTangent,
36                     const ModelHighAPI_Selection& theEndTangent,
37                     const bool theIsClosed,
38                     const bool theIsToReorder)
39 : ModelHighAPI_Interface(theFeature)
40 {
41   if (initialize()) {
42     fillAttribute(BuildPlugin_Interpolation::CREATION_METHOD_BY_SELECTION_ID(),mycreationmethod);
43     setUseTangents(true);
44     setTangents(theStartTangent, theEndTangent);
45     setClosed(theIsClosed);
46     setReorder(theIsToReorder);
47     setBase(theBaseObjects);
48   }
49 }
50
51 //==================================================================================================
52 BuildAPI_Interpolation::BuildAPI_Interpolation(const FeaturePtr& theFeature,
53   const std::list<ModelHighAPI_Selection>& theBaseObjects,
54   const bool theIsClosed,
55   const bool theIsToReorder)
56   : ModelHighAPI_Interface(theFeature)
57 {
58   if (initialize()) {
59     fillAttribute(BuildPlugin_Interpolation::CREATION_METHOD_BY_SELECTION_ID(),mycreationmethod);
60     setClosed(theIsClosed);
61     setReorder(theIsToReorder);
62     setUseTangents(false);
63     setBase(theBaseObjects);
64   }
65 }
66
67 //==================================================================================================
68 BuildAPI_Interpolation::BuildAPI_Interpolation(const FeaturePtr& theFeature,
69                                                const std::string & theXTexpression,
70                                                const std::string & theYTexpression,
71                                                const std::string & theZTexpression,
72                                                const ModelHighAPI_Double& theMinT,
73                                                const ModelHighAPI_Double& theMaxT,
74                                                const ModelHighAPI_Integer& theNbStep)
75 : ModelHighAPI_Interface(theFeature)
76 {
77   if (initialize()) {
78     fillAttribute(BuildPlugin_Interpolation::CREATION_METHOD_ANALYTICAL_ID(),mycreationmethod);
79     fillAttribute(theXTexpression, myxt);
80     fillAttribute(theYTexpression, myyt);
81     fillAttribute(theZTexpression, myzt);
82     fillAttribute(theMinT, mymint);
83     fillAttribute(theMaxT, mymaxt);
84     fillAttribute(theNbStep, mynumstep);
85
86     execute();
87   }
88 }
89
90 //==================================================================================================
91 BuildAPI_Interpolation::~BuildAPI_Interpolation()
92 {
93 }
94
95 //==================================================================================================
96 void BuildAPI_Interpolation::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
97 {
98   fillAttribute(theBaseObjects, mybaseObjects);
99
100   execute();
101 }
102
103 //==================================================================================================
104 void BuildAPI_Interpolation::setClosed(const bool theIsClosed)
105 {
106   fillAttribute(theIsClosed, myclosed);
107
108   execIfBaseNotEmpty();
109 }
110
111 void BuildAPI_Interpolation::setReorder(const bool theIsToReorder)
112 {
113   fillAttribute(theIsToReorder, myreorder);
114
115   execIfBaseNotEmpty();
116 }
117
118 void BuildAPI_Interpolation::setUseTangents(const bool theIsToUseTangents)
119 {
120   fillAttribute(theIsToUseTangents ? "true" : "", myuseTangents);
121
122   execIfBaseNotEmpty();
123 }
124
125 void BuildAPI_Interpolation::setTangents(const ModelHighAPI_Selection& theStartTangent,
126                                          const ModelHighAPI_Selection& theEndTangent)
127 {
128   fillAttribute(theStartTangent, mystartTangent);
129   fillAttribute(theEndTangent, myendTangent);
130
131   execIfBaseNotEmpty();
132 }
133
134 //==================================================================================================
135 void BuildAPI_Interpolation::dump(ModelHighAPI_Dumper& theDumper) const
136 {
137   FeaturePtr aBase = feature();
138   std::string aPartName = theDumper.name(aBase->document());
139
140   if (aBase->string(BuildPlugin_Interpolation::CREATION_METHOD_ID())->value() ==
141             BuildPlugin_Interpolation::CREATION_METHOD_BY_SELECTION_ID())
142   {
143     AttributeSelectionListPtr anAttrBaseObjects =
144       aBase->selectionList(BuildPlugin_Interpolation::BASE_OBJECTS_ID());
145
146     theDumper << aBase << " = model.addInterpolation(" << aPartName << ", "
147               << anAttrBaseObjects << ", ";
148
149     AttributeStringPtr useTangentsAttr = useTangents();
150     std::string useTangents = useTangentsAttr->value();
151     if (!useTangents.empty()) {
152       AttributeSelectionPtr anAttrStartTangent =
153         aBase->selection(BuildPlugin_Interpolation::TANGENT_START_ID());
154       AttributeSelectionPtr anAttrEndTangent =
155         aBase->selection(BuildPlugin_Interpolation::TANGENT_END_ID());
156
157       theDumper << anAttrStartTangent << ", " << anAttrEndTangent << ", ";
158     }
159
160     theDumper << closed() << ", " << reorder() << ")" << std::endl;
161   } else {
162     theDumper << aBase << " = model.addInterpolation(" << aPartName ;
163     AttributeStringPtr XtAttr = xt();
164     std::string xt = XtAttr->value();
165     AttributeStringPtr YtAttr = yt();
166     std::string yt = YtAttr->value();
167     AttributeStringPtr ZtAttr = zt();
168     std::string zt = ZtAttr->value();
169     AttributeDoublePtr minTAttr = mint();
170     double mint = minTAttr->value();
171     AttributeDoublePtr maxTAttr = maxt();
172     double maxt = maxTAttr->value();
173     AttributeIntegerPtr nbStepAttr = numstep();
174     int nbStep = nbStepAttr->value();
175
176     theDumper<< ", \""  << xt << "\", \"" << yt << "\", \""<< zt<< "\", " ;
177     theDumper << mint << ", " << maxt << ", "<< nbStep<< ")"<<std::endl;
178   }
179 }
180
181 //==================================================================================================
182 InterpolationPtr addInterpolation(const std::shared_ptr<ModelAPI_Document>& thePart,
183                                   const std::list<ModelHighAPI_Selection>& theBaseObjects,
184                                   const bool theIsClosed,
185                                   const bool theIsToReorder)
186 {
187   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(BuildAPI_Interpolation::ID());
188   return InterpolationPtr(new BuildAPI_Interpolation(aFeature,
189                                                      theBaseObjects,
190                                                      theIsClosed,
191                                                      theIsToReorder));
192 }
193
194 //==================================================================================================
195 InterpolationPtr addInterpolation(const std::shared_ptr<ModelAPI_Document>& thePart,
196                                   const std::list<ModelHighAPI_Selection>& theBaseObjects,
197                                   const ModelHighAPI_Selection& theStartTangent,
198                                   const ModelHighAPI_Selection& theEndTangent,
199                                   const bool theIsClosed,
200                                   const bool theIsToReorder)
201 {
202   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(BuildAPI_Interpolation::ID());
203   return InterpolationPtr(new BuildAPI_Interpolation(aFeature,
204                                                      theBaseObjects,
205                                                      theStartTangent,
206                                                      theEndTangent,
207                                                      theIsClosed,
208                                                      theIsToReorder));
209 }
210
211 InterpolationPtr addInterpolation(const std::shared_ptr<ModelAPI_Document>& thePart,
212                                   const std::string & theXTexpression,
213                                   const std::string & theYTexpression,
214                                   const std::string & theZTexpression,
215                                   const ModelHighAPI_Double& theMinT,
216                                   const ModelHighAPI_Double& theMaxT,
217                                   const ModelHighAPI_Integer& theNbStep)
218 {
219   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(BuildAPI_Interpolation::ID());
220   return InterpolationPtr(new BuildAPI_Interpolation(aFeature,
221                                                      theXTexpression,
222                                                      theYTexpression,
223                                                      theZTexpression,
224                                                      theMinT,
225                                                      theMaxT,
226                                                      theNbStep));
227 }
228
229 //==================================================================================================
230 void BuildAPI_Interpolation::execIfBaseNotEmpty()
231 {
232   if (baseObjects()->size() > 0)
233     execute();
234 }