Salome HOME
Typo-fix by Kunda + fix user doc generation
[modules/smesh.git] / src / SMESH_I / SMESH_Hypothesis_i.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
24 //  File   : SMESH_Hypothesis_i.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //
28 #include "SMESH_Hypothesis_i.hxx"
29 #include "SMESH_Gen_i.hxx"
30
31 #include <utilities.h>
32 #include <SALOMEDS_wrap.hxx>
33
34 #include <iostream>
35 #include <sstream>
36
37 using namespace std;
38
39 //=============================================================================
40 /*!
41  *  SMESH_Hypothesis_i::SMESH_Hypothesis_i
42  * 
43  *  Constructor
44  */
45 //=============================================================================
46
47 SMESH_Hypothesis_i::SMESH_Hypothesis_i( PortableServer::POA_ptr thePOA )
48   : SALOME::GenericObj_i( thePOA )
49 {
50   myBaseImpl = 0;
51 };
52
53 //=============================================================================
54 /*!
55  *  SMESH_Hypothesis_i::~SMESH_Hypothesis_i
56  *
57  *  Destructor
58  */
59 //=============================================================================
60
61 SMESH_Hypothesis_i::~SMESH_Hypothesis_i()
62 {
63   //MESSAGE( "SMESH_Hypothesis_i::~SMESH_Hypothesis_i" );
64   if ( myBaseImpl )
65     delete myBaseImpl;
66 };
67
68 //=============================================================================
69 /*!
70  *  SMESH_Hypothesis_i::GetName
71  *
72  *  Get type name of hypothesis
73  */
74 //=============================================================================
75
76 char* SMESH_Hypothesis_i::GetName()
77 {
78   return CORBA::string_dup( myBaseImpl->GetName() );
79 };
80
81 //=============================================================================
82 /*!
83  *  SMESH_Hypothesis_i::GetLibName
84  *
85  *  Get plugin library name of hypothesis (required by persistency mechanism)
86  */
87 //=============================================================================
88
89 char* SMESH_Hypothesis_i::GetLibName()
90 {
91   return CORBA::string_dup( myBaseImpl->GetLibName() );
92 };
93
94 //=============================================================================
95 /*!
96  *  SMESH_Hypothesis_i::SetLibName
97  *
98  *  Set plugin library name of hypothesis (required by persistency mechanism)
99  */
100 //=============================================================================
101
102 void SMESH_Hypothesis_i::SetLibName(const char* theLibName)
103 {
104   myBaseImpl->SetLibName( theLibName );
105 };
106
107 //=============================================================================
108 /*!
109  *  SMESH_Hypothesis_i::GetId
110  *
111  *  Get unique id of hypothesis
112  */
113 //=============================================================================
114
115 CORBA::Long SMESH_Hypothesis_i::GetId()
116 {
117   return myBaseImpl->GetID();
118 }
119
120 //=============================================================================
121 /*!
122  *  SMESH_Hypothesis_i::IsPublished()
123  *
124  */
125 //=============================================================================
126 bool SMESH_Hypothesis_i::IsPublished()
127 {
128   bool res = false;
129   if ( SMESH_Gen_i::GetSMESHGen() )
130   {
131     SALOMEDS::SObject_wrap SO = SMESH_Gen_i::ObjectToSObject( _this());
132     res = !SO->_is_nil();
133   }
134   return res;
135 }
136
137 //================================================================================
138 /*!
139  * \brief Set the pramIndex-th parameter
140  */
141 //================================================================================
142
143 void SMESH_Hypothesis_i::SetVarParameter (const char* theParameter,
144                                           const char* theMethod)
145 {
146   if ( SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen() )
147   {
148     SMESH::SMESH_Hypothesis_var varHolder;
149     if ( myHolder->_is_nil() ) varHolder = _this();
150     else                       varHolder = myHolder;
151     gen->UpdateParameters( varHolder, theParameter );
152
153     const std::vector< std::string >& pars = gen->GetLastParameters();
154     if ( !pars.empty() )
155       myMethod2VarParams[ theMethod ] = pars[0];
156   }
157 }
158
159 //================================================================================
160 /*!
161  * \brief Return the pramIndex-th variable parameter used for Hypothesis creation
162  */
163 //================================================================================
164
165 char* SMESH_Hypothesis_i::GetVarParameter (const char* theMethod)
166 {
167   if ( myMethod2VarParams.count("needs update by old study"))
168   {
169     // restore myMethod2VarParams by old study
170     myMethod2VarParams.clear();
171     if ( SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen() )
172     {
173       CORBA::String_var oldparVar = gen->GetParameters( _this() );
174       setOldParameters( oldparVar.in() );
175     }
176   }
177   std::map< std::string, std::string >::iterator meth_param = myMethod2VarParams.find( theMethod );
178   if ( meth_param != myMethod2VarParams.end() )
179     return CORBA::string_dup( meth_param->second.c_str() );
180
181   return CORBA::string_dup("");
182 }
183
184 //================================================================================
185 /*!
186  * \brief Store a hypothesis wrapping this not published one.
187  *
188  * This hyp, which has no own parameters but is published, is used to store variables
189  * defining parameters of this hypothesis.
190  */
191 //================================================================================
192
193 void SMESH_Hypothesis_i::SetHolderHypothesis(const SMESH::SMESH_Hypothesis_ptr hyp)
194 {
195   myHolder = SMESH::SMESH_Hypothesis::_duplicate( hyp );
196 }
197
198 //================================================================================
199 /*!
200  * \brief Restore myMethod2VarParams by parameters stored in an old study
201  */
202 //================================================================================
203
204 void SMESH_Hypothesis_i::setOldParameters (const char* theParameters)
205 {
206   if ( SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen() )
207   {
208     TCollection_AsciiString aOldParameters(theParameters);
209     int pos = aOldParameters.SearchFromEnd("|");
210     if ( pos >= 0 ) aOldParameters = aOldParameters.Split(pos);
211     pos = aOldParameters.SearchFromEnd(";*=");
212     if ( pos >= 0 ) aOldParameters.Split(pos-1);
213     gen->UpdateParameters( CORBA::Object_var( _this() ).in(), aOldParameters.ToCString() );
214
215     myMethod2VarParams.clear();
216     const std::vector< std::string >& pars = gen->GetLastParameters();
217     for ( size_t i = 0; i < pars.size(); ++i )
218     {
219       std::string meth = getMethodOfParameter( i, pars.size() );
220       myMethod2VarParams[ meth ] = pars[i];
221     }
222     gen->UpdateParameters( CORBA::Object_var( _this() ).in(), "" ); // clear params
223   }
224 }
225
226 //=============================================================================
227 /*!
228  *  SMESH_Hypothesis_i::GetImpl
229  *
230  *  Get implementation
231  */
232 //=============================================================================
233
234 ::SMESH_Hypothesis* SMESH_Hypothesis_i::GetImpl()
235 {
236   return myBaseImpl;
237 }
238
239 //================================================================================
240 /*!
241  * \brief Return true if a hypothesis has parameters
242  */
243 //================================================================================
244
245 CORBA::Boolean SMESH_Hypothesis_i::HasParameters()
246 {
247   std::ostringstream os;
248   myBaseImpl->SaveTo( os );
249   return ( !os.str().empty() );
250 }
251
252 //=============================================================================
253 /*!
254  *  SMESH_Hypothesis_i::SaveTo
255  *
256  *  Persistence: Dumps parameters to the string stream
257  */
258 //=============================================================================
259
260 char* SMESH_Hypothesis_i::SaveTo()
261 {
262   std::ostringstream os;
263
264   // assure that parameters are loaded from an old study
265   CORBA::String_var p = GetVarParameter("");
266
267   os << "VARS " << myMethod2VarParams.size()  << " ";
268   std::map< std::string, std::string >::iterator meth_param = myMethod2VarParams.begin();
269   for ( ; meth_param != myMethod2VarParams.end(); ++meth_param )
270     os << meth_param->first << " "
271        << meth_param->second.size() << " "
272        << meth_param->second << " ";
273
274   myBaseImpl->SaveTo( os );
275   return CORBA::string_dup( os.str().c_str() );
276 }
277
278 //=============================================================================
279 /*!
280 *  SMESH_Hypothesis_i::LoadFrom
281 *
282 *  Persistence: Restores parameters from string
283 */
284 //=============================================================================
285
286 void SMESH_Hypothesis_i::LoadFrom( const char* theStream )
287 {
288   std::istringstream is( theStream );
289   if ( strncmp( theStream, "VARS", 4 ) == 0 )
290   {
291     int nbVars, len;
292     char str[256];
293     std::string meth;
294     is >> str >> nbVars;
295     for ( int i = 0; i < nbVars; ++i )
296     {
297       is >> meth >> len;
298       if ( len < 256 )
299       {
300         is.get( str, len + 2 ); // 2 - to read at least 1 white space
301         if ( len > 0 )
302           myMethod2VarParams[ meth ] = std::string( str+1, len );
303       }
304     }
305   }
306   else
307   {
308     // we can't restore myMethod2VarParams by old study here because SObject
309     // isn't yet bound to _this()
310     myMethod2VarParams["needs update by old study"] = "yes";
311   }
312
313   myBaseImpl->LoadFrom( is );
314
315   // let listeners know about loading (issue 0020918)
316   myBaseImpl->NotifySubMeshesHypothesisModification();
317 }
318
319 //================================================================================
320 /*!
321  * \brief This mesthod is called after completion of loading a study 
322  */
323 //================================================================================
324
325 void SMESH_Hypothesis_i::UpdateAsMeshesRestored()
326 {
327   // for hyps needing full data restored
328 }