Salome HOME
219c1e12699fe2a141673a6e6ae12639bf2459dd
[modules/smesh.git] / src / SMESH_I / SMESH_Hypothesis_i.cxx
1 //  Copyright (C) 2007-2008  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 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
23 //  File   : SMESH_Hypothesis_i.cxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : SMESH
26 //  $Header$
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  *  SMESH_Hypothesis_i::GetEntry()
144  */
145 //=============================================================================
146 char* SMESH_Hypothesis_i::GetEntry()
147 {
148   char aBuf[100];
149   sprintf( aBuf, "%i", GetId() );
150   return CORBA::string_dup( aBuf );
151 }
152
153 //=============================================================================
154 /*!
155  *  SMESH_Hypothesis_i::GetComponent()
156  */
157 //=============================================================================
158 char* SMESH_Hypothesis_i::GetComponent()
159 {
160   return CORBA::string_dup( "SMESH" );
161 }
162
163 //=============================================================================
164 /*!
165  *  SMESH_Hypothesis_i::IsValid()
166  */
167 //=============================================================================
168 CORBA::Boolean SMESH_Hypothesis_i::IsValid()
169 {
170   return true;
171 }
172
173 //=============================================================================
174 /*!
175  *  SMESH_Hypothesis_i::SetParameters()
176  *
177  */
178 //=============================================================================
179 void SMESH_Hypothesis_i::SetParameters( SALOME::Notebook_ptr theNotebook, const SALOME::StringArray& theParameters )
180 {
181   theNotebook->ClearDependencies( _this(), SALOME::Parameters );
182   std::list<std::string> aParams;
183   int n = theParameters.length();
184   for( int i=0; i<n; i++ )
185   {
186     std::string aParam = CORBA::string_dup( theParameters[i] );
187     aParams.push_back( aParam );
188     
189     SALOME::Parameter_ptr aParamPtr = theNotebook->GetParameter( aParam.c_str() );
190     if( !CORBA::is_nil( aParamPtr ) )
191       theNotebook->AddDependency( _this(), aParamPtr );
192   }
193   myBaseImpl->SetParameters( aParams );
194
195   UpdateStringAttribute();
196 }
197
198 //=============================================================================
199 /*!
200  *  SMESH_Hypothesis_i::GetParameters()
201  *
202  */
203 //=============================================================================
204 SALOME::StringArray* SMESH_Hypothesis_i::GetParameters()
205 {
206   std::list<std::string> aParams = myBaseImpl->GetParameters();
207   SALOME::StringArray_var aRes = new SALOME::StringArray();
208   aRes->length( aParams.size() );
209   std::list<std::string>::const_iterator it = aParams.begin(), last = aParams.end();
210   for( int i=0; it!=last; it++, i++ )
211     aRes[i] = CORBA::string_dup( it->c_str() );
212   return aRes._retn();
213 }
214
215 //=============================================================================
216 /*!
217  * SMESH_Hypothesis_i::Update()
218  *
219  */
220 //=============================================================================
221 void SMESH_Hypothesis_i::Update( SALOME::Notebook_ptr theNotebook )
222 {
223 }
224
225 //=============================================================================
226 /*!
227  * SMESH_Hypothesis_i::UpdateStringAttribute()
228  *
229  */
230 //=============================================================================
231 void SMESH_Hypothesis_i::UpdateStringAttribute()
232 {
233   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
234
235   SALOME::Notebook_ptr aNotebook = aSMESHGen->GetNotebook( myBaseImpl->GetStudyId() );
236
237   SALOME::StringArray* anObjectParameters = aNotebook->GetObjectParameters( GetComponent(), GetEntry() );
238   int aParametersLength = anObjectParameters ? anObjectParameters->length() : 0;
239   if( aParametersLength == 0 )
240     return;
241
242   SALOMEDS::Study_ptr aStudy = aSMESHGen->GetStudy( myBaseImpl->GetStudyId() );
243   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
244   SALOMEDS::SObject_var aSObject = SMESH_Gen_i::ObjectToSObject( aStudy, SMESH::SMESH_Hypothesis::_narrow( _this() ) );
245   if( CORBA::is_nil( aSObject ) )
246     return;
247
248   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute( aSObject, "AttributeString" );
249   SALOMEDS::AttributeString_var aStringAttrib = SALOMEDS::AttributeString::_narrow( anAttr );
250
251   std::string aString;
252   for( int i = 0, n = anObjectParameters->length(); i < n; i++ ) {
253     std::string aParameter = anObjectParameters->operator[](i).in();
254     if( aParameter != "" )
255     {
256       if( aString != "" )
257         aString += ", ";
258       aString += aParameter;
259     }
260   }
261   aStringAttrib->SetValue( aString.c_str() );
262   aStringAttrib->Destroy();
263 }
264
265 //=============================================================================
266 /*!
267  *  SMESH_Hypothesis_i::GetImpl
268  *
269  *  Get implementation
270  */
271 //=============================================================================
272
273 ::SMESH_Hypothesis* SMESH_Hypothesis_i::GetImpl()
274 {
275   //MESSAGE( "SMESH_Hypothesis_i::GetImpl" );
276   return myBaseImpl;
277 }
278
279 //=============================================================================
280 /*!
281  *  SMESH_Hypothesis_i::SaveTo
282  *
283  *  Persistence: Dumps parameters to the string stream
284  */
285 //=============================================================================
286
287 char* SMESH_Hypothesis_i::SaveTo()
288 {
289   MESSAGE( "SMESH_Hypothesis_i::SaveTo" );
290   std::ostringstream os;
291   myBaseImpl->SaveTo( os );
292   return CORBA::string_dup( os.str().c_str() );
293 }
294
295 //=============================================================================
296 /*!
297 *  SMESH_Hypothesis_i::LoadFrom
298 *
299 *  Persistence: Restores parameters from string
300 */
301 //=============================================================================
302
303 void SMESH_Hypothesis_i::LoadFrom( const char* theStream )
304 {
305   MESSAGE( "SMESH_Hypothesis_i::LoadFrom" );
306   std::istringstream is( theStream );
307   myBaseImpl->LoadFrom( is );
308 }