Salome HOME
Fix to avoid dependence of mesh on itself (it leaded to cycle in 'SetRemovedFromStudy').
[modules/smesh.git] / src / SMESH_I / SMESH_Hypothesis_i.cxx
1 // Copyright (C) 2007-2013  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 "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   MESSAGE( "SMESH_Hypothesis_i::SMESH_Hypothesis_i / Début" );
51   myBaseImpl = 0;
52   
53   MESSAGE( "SMESH_Hypothesis_i::SMESH_Hypothesis_i / Fin" );
54 };
55
56 //=============================================================================
57 /*!
58  *  SMESH_Hypothesis_i::~SMESH_Hypothesis_i
59  *
60  *  Destructor
61  */
62 //=============================================================================
63
64 SMESH_Hypothesis_i::~SMESH_Hypothesis_i()
65 {
66   MESSAGE( "SMESH_Hypothesis_i::~SMESH_Hypothesis_i" );
67   if ( myBaseImpl )
68     delete myBaseImpl;
69 };
70
71 //=============================================================================
72 /*!
73  *  SMESH_Hypothesis_i::GetName
74  *
75  *  Get type name of hypothesis
76  */
77 //=============================================================================
78
79 char* SMESH_Hypothesis_i::GetName()
80 {
81   //MESSAGE( "SMESH_Hypothesis_i::GetName" );
82   return CORBA::string_dup( myBaseImpl->GetName() );
83 };
84
85 //=============================================================================
86 /*!
87  *  SMESH_Hypothesis_i::GetLibName
88  *
89  *  Get plugin library name of hypothesis (required by persistency mechanism)
90  */
91 //=============================================================================
92
93 char* SMESH_Hypothesis_i::GetLibName()
94 {
95   MESSAGE( "SMESH_Hypothesis_i::GetLibName" );
96   return CORBA::string_dup( myBaseImpl->GetLibName() );
97 };
98
99 //=============================================================================
100 /*!
101  *  SMESH_Hypothesis_i::SetLibName
102  *
103  *  Set plugin library name of hypothesis (required by persistency mechanism)
104  */
105 //=============================================================================
106
107 void SMESH_Hypothesis_i::SetLibName(const char* theLibName)
108 {
109   MESSAGE( "SMESH_Hypothesis_i::SetLibName" );
110   myBaseImpl->SetLibName( theLibName );
111 };
112
113 //=============================================================================
114 /*!
115  *  SMESH_Hypothesis_i::GetId
116  *
117  *  Get unique id of hypothesis
118  */
119 //=============================================================================
120
121 CORBA::Long SMESH_Hypothesis_i::GetId()
122 {
123   MESSAGE( "SMESH_Hypothesis_i::GetId" );
124   return myBaseImpl->GetID();
125 }
126
127 //=============================================================================
128 /*!
129  *  SMESH_Hypothesis_i::IsPublished()
130  *
131  */
132 //=============================================================================
133 bool SMESH_Hypothesis_i::IsPublished()
134 {
135   bool res = false;
136   if ( SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen())
137   {
138     SALOMEDS::Study_var study = gen->GetCurrentStudy();
139     SALOMEDS::SObject_wrap SO = SMESH_Gen_i::ObjectToSObject( study, _this());
140     res = !SO->_is_nil();
141   }
142   return res;
143 }
144
145 //================================================================================
146 /*!
147  * \brief Set the pramIndex-th parameter
148  */
149 //================================================================================
150
151 void SMESH_Hypothesis_i::SetVarParameter (const char* theParameter,
152                                           const char* theMethod)
153 {
154   if ( SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen() )
155   {
156     SMESH::SMESH_Hypothesis_var varHolder;
157     if ( myHolder->_is_nil() ) varHolder = _this();
158     else                       varHolder = myHolder;
159     gen->UpdateParameters( varHolder, theParameter );
160
161     const std::vector< std::string >& pars = gen->GetLastParameters();
162     if ( !pars.empty() )
163       myMethod2VarParams[ theMethod ] = pars[0];
164   }
165 }
166
167 //================================================================================
168 /*!
169  * \brief Return the pramIndex-th variable parameter used for Hypothesis creation
170  */
171 //================================================================================
172
173 char* SMESH_Hypothesis_i::GetVarParameter (const char* theMethod)
174 {
175   if ( myMethod2VarParams.count("needs update by old study"))
176   {
177     // restore myMethod2VarParams by old study
178     myMethod2VarParams.clear();
179     if ( SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen() )
180     {
181       CORBA::String_var oldparVar = gen->GetParameters( _this() );
182       setOldParameters( oldparVar.in() );
183     }
184   }
185   std::map< std::string, std::string >::iterator meth_param = myMethod2VarParams.find( theMethod );
186   if ( meth_param != myMethod2VarParams.end() )
187     return CORBA::string_dup( meth_param->second.c_str() );
188
189   return CORBA::string_dup("");
190 }
191
192 //================================================================================
193 /*!
194  * \brief Store a hypothesis wrapping this not published one.
195  *
196  * This hyp, which has no own parameters but is published, is used to store variables
197  * defining parameters of this hypothesis.
198  */
199 //================================================================================
200
201 void SMESH_Hypothesis_i::SetHolderHypothesis(const SMESH::SMESH_Hypothesis_ptr hyp)
202 {
203   myHolder = SMESH::SMESH_Hypothesis::_duplicate( hyp );
204 }
205
206 //================================================================================
207 /*!
208  * \brief Restore myMethod2VarParams by parameters stored in an old study
209  */
210 //================================================================================
211
212 void SMESH_Hypothesis_i::setOldParameters (const char* theParameters)
213 {
214   if ( SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen() )
215   {
216     TCollection_AsciiString aOldParameters(theParameters);
217     int pos = aOldParameters.SearchFromEnd("|");
218     if ( pos >= 0 ) aOldParameters = aOldParameters.Split(pos);
219     pos = aOldParameters.SearchFromEnd(";*=");
220     if ( pos >= 0 ) aOldParameters.Split(pos-1);
221     gen->UpdateParameters( CORBA::Object_var( _this() ).in(), aOldParameters.ToCString() );
222
223     myMethod2VarParams.clear();
224     const std::vector< std::string >& pars = gen->GetLastParameters();
225     for ( size_t i = 0; i < pars.size(); ++i )
226     {
227       std::string meth = getMethodOfParameter( i, pars.size() );
228       myMethod2VarParams[ meth ] = pars[i];
229     }
230     gen->UpdateParameters( CORBA::Object_var( _this() ).in(), "" ); // clear params
231   }
232 }
233
234 //=============================================================================
235 /*!
236  *  SMESH_Hypothesis_i::GetImpl
237  *
238  *  Get implementation
239  */
240 //=============================================================================
241
242 ::SMESH_Hypothesis* SMESH_Hypothesis_i::GetImpl()
243 {
244   return myBaseImpl;
245 }
246
247 //=============================================================================
248 /*!
249  *  SMESH_Hypothesis_i::SaveTo
250  *
251  *  Persistence: Dumps parameters to the string stream
252  */
253 //=============================================================================
254
255 char* SMESH_Hypothesis_i::SaveTo()
256 {
257   std::ostringstream os;
258
259   // assure that parameters are loaded from an old study
260   CORBA::String_var p = GetVarParameter("");
261
262   os << "VARS " << myMethod2VarParams.size()  << " ";
263   std::map< std::string, std::string >::iterator meth_param = myMethod2VarParams.begin();
264   for ( ; meth_param != myMethod2VarParams.end(); ++meth_param )
265     os << meth_param->first << " "
266        << meth_param->second.size() << " "
267        << meth_param->second << " ";
268
269   myBaseImpl->SaveTo( os );
270   return CORBA::string_dup( os.str().c_str() );
271 }
272
273 //=============================================================================
274 /*!
275 *  SMESH_Hypothesis_i::LoadFrom
276 *
277 *  Persistence: Restores parameters from string
278 */
279 //=============================================================================
280
281 void SMESH_Hypothesis_i::LoadFrom( const char* theStream )
282 {
283   std::istringstream is( theStream );
284   if ( strncmp( theStream, "VARS", 4 ) == 0 )
285   {
286     int nbVars, len;
287     char str[256];
288     std::string meth;
289     is >> str >> nbVars;
290     for ( int i = 0; i < nbVars; ++i )
291     {
292       is >> meth >> len;
293       if ( len < 256 )
294       {
295         is.get( str, len + 2 ); // 2 - to read at least 1 white space
296         if ( len > 0 )
297           myMethod2VarParams[ meth ] = std::string( str+1, len );
298       }
299     }
300   }
301   else
302   {
303     // we can't restore myMethod2VarParams by old study here because SObject
304     // isn't yet bound to _this()
305     myMethod2VarParams["needs update by old study"] = "yes";
306   }
307
308   myBaseImpl->LoadFrom( is );
309
310   // let listeners know about loading (issue 0020918)
311   myBaseImpl->NotifySubMeshesHypothesisModification();
312 }
313
314 //================================================================================
315 /*!
316  * \brief This mesthod is called after completion of loading a study 
317  */
318 //================================================================================
319
320 void SMESH_Hypothesis_i::UpdateAsMeshesRestored()
321 {
322   // for hyps needing full data restored
323 }