Salome HOME
Merge from V6_main (04/10/2012)
[modules/smesh.git] / src / SMESH_I / SMESH_Hypothesis_i.cxx
1 // Copyright (C) 2007-2012  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.
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 <iostream>
29 #include <sstream>
30 #include "SMESH_Hypothesis_i.hxx"
31 #include "SMESH_Gen_i.hxx"
32 #include "utilities.h"
33
34 using namespace std;
35
36 //=============================================================================
37 /*!
38  *  SMESH_Hypothesis_i::SMESH_Hypothesis_i
39  * 
40  *  Constructor
41  */
42 //=============================================================================
43
44 SMESH_Hypothesis_i::SMESH_Hypothesis_i( PortableServer::POA_ptr thePOA )
45      : SALOME::GenericObj_i( thePOA )
46 {
47   MESSAGE( "SMESH_Hypothesis_i::SMESH_Hypothesis_i / Début" );
48   myBaseImpl = 0;
49   
50   MESSAGE( "SMESH_Hypothesis_i::SMESH_Hypothesis_i / Fin" );
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   //MESSAGE( "SMESH_Hypothesis_i::GetName" );
79   return CORBA::string_dup( myBaseImpl->GetName() );
80 };
81
82 //=============================================================================
83 /*!
84  *  SMESH_Hypothesis_i::GetLibName
85  *
86  *  Get plugin library name of hypothesis (required by persistency mechanism)
87  */
88 //=============================================================================
89
90 char* SMESH_Hypothesis_i::GetLibName()
91 {
92   MESSAGE( "SMESH_Hypothesis_i::GetLibName" );
93   return CORBA::string_dup( myBaseImpl->GetLibName() );
94 };
95
96 //=============================================================================
97 /*!
98  *  SMESH_Hypothesis_i::SetLibName
99  *
100  *  Set plugin library name of hypothesis (required by persistency mechanism)
101  */
102 //=============================================================================
103
104 void SMESH_Hypothesis_i::SetLibName(const char* theLibName)
105 {
106   MESSAGE( "SMESH_Hypothesis_i::SetLibName" );
107   myBaseImpl->SetLibName( theLibName );
108 };
109
110 //=============================================================================
111 /*!
112  *  SMESH_Hypothesis_i::GetId
113  *
114  *  Get unique id of hypothesis
115  */
116 //=============================================================================
117
118 CORBA::Long SMESH_Hypothesis_i::GetId()
119 {
120   MESSAGE( "SMESH_Hypothesis_i::GetId" );
121   return myBaseImpl->GetID();
122 }
123
124 //=============================================================================
125 /*!
126  *  SMESH_Hypothesis_i::IsPublished()
127  *
128  */
129 //=============================================================================
130 bool SMESH_Hypothesis_i::IsPublished(){
131   bool res = false;
132   SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen();
133   if(gen){
134     SALOMEDS::SObject_var SO = 
135       SMESH_Gen_i::ObjectToSObject(gen->GetCurrentStudy() , SMESH::SMESH_Hypothesis::_narrow(_this()));
136     res = !SO->_is_nil();
137   }
138   return res;
139 }
140
141 //================================================================================
142 /*!
143  * \brief Set the pramIndex-th parameter
144  */
145 //================================================================================
146
147 void SMESH_Hypothesis_i::SetVarParameter (const char* theParameter,
148                                           const char* theMethod)
149 {
150   if ( SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen() )
151   {
152     gen->UpdateParameters(theParameter);
153
154     const std::vector< std::string >& pars = gen->GetLastParameters();
155     if ( !pars.empty() )
156       myMethod2VarParams[ theMethod ] = pars[0];
157   }
158 }
159
160 //================================================================================
161 /*!
162  * \brief Return the pramIndex-th variable parameter used for Hypothesis creation
163  */
164 //================================================================================
165
166 char* SMESH_Hypothesis_i::GetVarParameter (const char* theMethod)
167 {
168   if ( myMethod2VarParams.count("needs update by old study"))
169   {
170     // restore myMethod2VarParams by old study
171     myMethod2VarParams.clear();
172     if ( SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen() )
173     {
174       CORBA::String_var oldparVar = gen->GetParameters( _this() );
175       setOldParameters( oldparVar.in() );
176     }
177   }
178   std::map< std::string, std::string >::iterator meth_param = myMethod2VarParams.find( theMethod );
179   if ( meth_param != myMethod2VarParams.end() )
180     return CORBA::string_dup( meth_param->second.c_str() );
181
182   return CORBA::string_dup("");
183 }
184
185 //================================================================================
186 /*!
187  * \brief Restore myMethod2VarParams by parameters stored in an old study
188  */
189 //================================================================================
190
191 void SMESH_Hypothesis_i::setOldParameters (const char* theParameters)
192 {
193   if ( SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen() )
194   {
195     TCollection_AsciiString aOldParameters(theParameters);
196     int pos = aOldParameters.SearchFromEnd("|");
197     if ( pos >= 0 ) aOldParameters = aOldParameters.Split(pos);
198     pos = aOldParameters.SearchFromEnd(";*=");
199     if ( pos >= 0 ) aOldParameters.Split(pos-1);
200     gen->UpdateParameters( aOldParameters.ToCString() );
201
202     myMethod2VarParams.clear();
203     const std::vector< std::string >& pars = gen->GetLastParameters();
204     for ( size_t i = 0; i < pars.size(); ++i )
205     {
206       std::string meth = getMethodOfParameter( i, pars.size() );
207       myMethod2VarParams[ meth ] = pars[i];
208     }
209     gen->UpdateParameters(""); // clear params
210   }
211 }
212
213 //=============================================================================
214 /*!
215  *  SMESH_Hypothesis_i::SetParameters()
216  *
217  */
218 //=============================================================================
219 // void SMESH_Hypothesis_i::SetParameters(const char* theParameters)
220 // {
221 //   SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen();
222 //   //char * aParameters = CORBA::string_dup(theParameters);
223 //   if(gen){
224 //     gen->UpdateParameters(theParameters);
225 //     // if(IsPublished()) {
226 //     //   SMESH_Gen_i::GetSMESHGen()->UpdateParameters(SMESH::SMESH_Hypothesis::_narrow(_this()),aParameters);
227 //     // }
228 //     // else {
229 //     //   myBaseImpl->SetParameters(gen->ParseParameters(aParameters));
230 //     // }
231 //   }
232 // }
233
234 // //=============================================================================
235 // /*!
236 //  *  SMESH_Hypothesis_i::GetParameters()
237 //  *
238 //  */
239 // //=============================================================================
240 // char* SMESH_Hypothesis_i::GetParameters()
241 // {
242 //   SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen();
243 //   char* aResult;
244 //   if(IsPublished()) {
245 //     MESSAGE("SMESH_Hypothesis_i::GetParameters() : Get Parameters from SObject");
246 //     aResult = gen->GetParameters(SMESH::SMESH_Hypothesis::_narrow(_this()));
247 //   }
248 //   else {
249 //     MESSAGE("SMESH_Hypothesis_i::GetParameters() : Get local parameters");
250 //     aResult = myBaseImpl->GetParameters(); 
251 //   }
252 //   return CORBA::string_dup(aResult);
253 // }
254
255 // //=============================================================================
256 // /*!
257 //  *  SMESH_Hypothesis_i::GetLastParameters()
258 //  *
259 //  */
260 // //=============================================================================
261 // SMESH::ListOfParameters* SMESH_Hypothesis_i::GetLastParameters()
262 // {
263 //   SMESH::ListOfParameters_var aResult = new SMESH::ListOfParameters();
264 //   SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen();
265 //   if(gen) {
266 //     char *aParameters;
267 //     if(IsPublished())
268 //      aParameters = GetParameters();
269 //     else
270 //       aParameters = myBaseImpl->GetLastParameters();
271
272 //     SALOMEDS::Study_ptr aStudy = gen->GetCurrentStudy();
273 //     if(!aStudy->_is_nil()) {
274 //       SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters); 
275 //       if(aSections->length() > 0) {
276 //         SALOMEDS::ListOfStrings aVars = aSections[aSections->length()-1];
277 //         aResult->length(aVars.length());
278 //         for(int i = 0;i < aVars.length();i++)
279 //           aResult[i] = CORBA::string_dup( aVars[i]);
280 //       }
281 //     }
282 //   }
283 //   return aResult._retn();
284 // }
285
286 // //=============================================================================
287 // /*!
288 //  *  SMESH_Hypothesis_i::SetLastParameters()
289 //  *
290 //  */
291 // //=============================================================================
292 // void SMESH_Hypothesis_i::SetLastParameters(const char* theParameters)
293 // {
294 //   if(!IsPublished()) {
295 //     myBaseImpl->SetLastParameters(theParameters);
296 //   }
297 // }
298 // //=============================================================================
299 // /*!
300 //  *  SMESH_Hypothesis_i::ClearParameters()
301 //  *
302 //  */
303 // //=============================================================================
304 // void SMESH_Hypothesis_i::ClearParameters()
305 // {
306 //   myMethod2VarParams.clear();
307 //   // if(!IsPublished()) {
308 //   //   myBaseImpl->ClearParameters();
309 //   // }
310 // }
311
312 //=============================================================================
313 /*!
314  *  SMESH_Hypothesis_i::GetImpl
315  *
316  *  Get implementation
317  */
318 //=============================================================================
319
320 ::SMESH_Hypothesis* SMESH_Hypothesis_i::GetImpl()
321 {
322   return myBaseImpl;
323 }
324
325 //=============================================================================
326 /*!
327  *  SMESH_Hypothesis_i::SaveTo
328  *
329  *  Persistence: Dumps parameters to the string stream
330  */
331 //=============================================================================
332
333 char* SMESH_Hypothesis_i::SaveTo()
334 {
335   std::ostringstream os;
336
337   // assure that parameters are loaded from an old study
338   CORBA::String_var p = GetVarParameter("");
339
340   os << "VARS " << myMethod2VarParams.size()  << " ";
341   std::map< std::string, std::string >::iterator meth_param = myMethod2VarParams.begin();
342   for ( ; meth_param != myMethod2VarParams.end(); ++meth_param )
343     os << meth_param->first << " "
344        << meth_param->second.size() << " "
345        << meth_param->second << " ";
346
347   myBaseImpl->SaveTo( os );
348   return CORBA::string_dup( os.str().c_str() );
349 }
350
351 //=============================================================================
352 /*!
353 *  SMESH_Hypothesis_i::LoadFrom
354 *
355 *  Persistence: Restores parameters from string
356 */
357 //=============================================================================
358
359 void SMESH_Hypothesis_i::LoadFrom( const char* theStream )
360 {
361   std::istringstream is( theStream );
362   if ( strncmp( theStream, "VARS", 4 ) == 0 )
363   {
364     int nbVars, len;
365     char str[256];
366     std::string meth;
367     is >> str >> nbVars;
368     for ( int i = 0; i < nbVars; ++i )
369     {
370       is >> meth >> len;
371       if ( len < 256 )
372       {
373         is.get( str, len + 2 ); // 2 - to read at least 1 white space
374         if ( len > 0 )
375           myMethod2VarParams[ meth ] = std::string( str+1, len );
376       }
377     }
378   }
379   else
380   {
381     // we can't restore myMethod2VarParams by old study here because SObject
382     // isn't yet bound to _this()
383     myMethod2VarParams["needs update by old study"] = "yes";
384   }
385
386   myBaseImpl->LoadFrom( is );
387
388   // let listeners know about loading (issue 0020918)
389   myBaseImpl->NotifySubMeshesHypothesisModification();
390 }
391
392 //================================================================================
393 /*!
394  * \brief This mesthod is called after completion of loading a study 
395  */
396 //================================================================================
397
398 void SMESH_Hypothesis_i::UpdateAsMeshesRestored()
399 {
400   // for hyps needing full data restored
401 }