Salome HOME
Merge from V6_main 11/02/2013
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_LayerDistribution_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 classes
24 //  File   : StdMeshers_LayerDistribution_i.cxx
25 //  Author : Edward AGAPOV
26 //  Module : SMESH
27 //
28 #include "StdMeshers_LayerDistribution_i.hxx"
29 #include "SMESH_Gen_i.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_PythonDump.hxx"
32
33 #include "Utils_CorbaException.hxx"
34 #include "utilities.h"
35
36 #include <TCollection_AsciiString.hxx>
37
38 using namespace std;
39
40 //=============================================================================
41 /*!
42  *  StdMeshers_LayerDistribution_i::StdMeshers_LayerDistribution_i
43  *
44  *  Constructor
45  */
46 //=============================================================================
47
48 StdMeshers_LayerDistribution_i::StdMeshers_LayerDistribution_i( PortableServer::POA_ptr thePOA,
49                                                           int                     theStudyId,
50                                                           ::SMESH_Gen*            theGenImpl )
51   : SALOME::GenericObj_i( thePOA ), 
52     SMESH_Hypothesis_i( thePOA )
53 {
54   MESSAGE( "StdMeshers_LayerDistribution_i::StdMeshers_LayerDistribution_i" );
55   myBaseImpl = new ::StdMeshers_LayerDistribution( theGenImpl->GetANewId(),
56                                                    theStudyId,
57                                                    theGenImpl );
58 }
59
60 //=============================================================================
61 /*!
62  *  StdMeshers_LayerDistribution_i::~StdMeshers_LayerDistribution_i
63  *
64  *  Destructor
65  */
66 //=============================================================================
67
68 StdMeshers_LayerDistribution_i::~StdMeshers_LayerDistribution_i()
69 {
70   MESSAGE( "StdMeshers_LayerDistribution_i::~StdMeshers_LayerDistribution_i" );
71 }
72
73 //=============================================================================
74 /*!
75  *  StdMeshers_LayerDistribution_i::SetLayerDistribution
76  *
77  
78  */
79 //=============================================================================
80
81 void StdMeshers_LayerDistribution_i::SetLayerDistribution(SMESH::SMESH_Hypothesis_ptr hyp1D)
82      throw ( SALOME::SALOME_Exception )
83 {
84   ASSERT( myBaseImpl );
85   try {
86     SMESH_Hypothesis_i * hyp_i = SMESH::DownCast< SMESH_Hypothesis_i*>( hyp1D );
87     bool isNewHyp = ( hyp_i->GetImpl() != this->GetImpl()->GetLayerDistribution() );
88     this->GetImpl()->SetLayerDistribution( hyp_i->GetImpl() );
89     myHyp = SMESH::SMESH_Hypothesis::_duplicate( hyp1D );
90     // Remove SO of 1D hypothesis if it was published
91     if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen())
92     {
93       SALOMEDS::Study_var study = gen->GetCurrentStudy();
94       SALOMEDS::SObject_var  SO = gen->ObjectToSObject( study, hyp1D );
95       if ( ! SO->_is_nil() )
96       {
97         SALOMEDS::StudyBuilder_var builder = study->NewBuilder();
98         builder->RemoveObjectWithChildren( SO );
99         SO->UnRegister();
100       }
101     }
102     // Update Python script: write creation of 1D hyp as it is not published and
103     // for this, SMESH_Gen does not write it's creation
104     if ( isNewHyp )
105       SMESH::TPythonDump() << hyp1D << " = "
106                            << SMESH_Gen_i::GetSMESHGen() << ".CreateHypothesis('"
107                            << hyp_i->GetName() << "', '" << hyp_i->GetLibName() << "')";
108   }
109   catch ( SALOME_Exception& S_ex ) {
110     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
111   }
112   // Update Python script
113   SMESH::TPythonDump() << _this() << ".SetLayerDistribution( " << hyp1D << " )";
114 }
115
116 //=============================================================================
117 /*!
118  *  StdMeshers_LayerDistribution_i::GetLayerDistribution
119  *
120  *  Returns <number of layers> parameter value
121  */
122 //=============================================================================
123
124 SMESH::SMESH_Hypothesis_ptr StdMeshers_LayerDistribution_i::GetLayerDistribution()
125 {
126   SMESH::SMESH_Hypothesis_var hyp = myHyp;
127   return hyp._retn();
128 }
129
130 //=============================================================================
131 /*!
132  *  StdMeshers_LayerDistribution_i::GetImpl
133  *
134  *  Get implementation
135  */
136 //=============================================================================
137
138 ::StdMeshers_LayerDistribution* StdMeshers_LayerDistribution_i::GetImpl()
139 {
140   return ( ::StdMeshers_LayerDistribution* )myBaseImpl;
141 }
142
143 //================================================================================
144 /*!
145  * \brief Verify whether hypothesis supports given entity type 
146   * \param type - dimension (see SMESH::Dimension enumeration)
147   * \retval CORBA::Boolean - TRUE if dimension is supported, FALSE otherwise
148  * 
149  * Verify whether hypothesis supports given entity type (see SMESH::Dimension enumeration)
150  */
151 //================================================================================  
152 CORBA::Boolean StdMeshers_LayerDistribution_i::IsDimSupported( SMESH::Dimension type )
153 {
154   return type == SMESH::DIM_3D;
155 }
156
157 //================================================================================
158 /*!
159  * \brief Write parameters in a string
160   * \retval char* - resulting string
161  */
162 //================================================================================
163
164 char* StdMeshers_LayerDistribution_i::SaveTo()
165 {
166   ASSERT( myBaseImpl );
167   std::ostringstream os;
168
169   ::SMESH_Hypothesis* hyp1D = GetImpl()->GetLayerDistribution();
170   SMESH_Hypothesis_i* hyp1D_i = SMESH::DownCast< SMESH_Hypothesis_i*>( myHyp );
171   if ( !hyp1D || !hyp1D_i )
172     os << "NULL_HYPO ";
173   else {
174     os << hyp1D->GetName() << " "
175        << hyp1D->GetLibName() << " "
176        << hyp1D_i->SaveTo() << " ";
177   }
178   os << SMESH_Hypothesis_i::SaveTo();  // to have a mark of storage version ("VARS...")
179
180   return CORBA::string_dup( os.str().c_str() );
181 }
182
183 //================================================================================
184 /*!
185  * \brief Retrieve parameters from the string
186   * \param theStream - the input string
187  */
188 //================================================================================
189
190 void StdMeshers_LayerDistribution_i::LoadFrom( const char* theStream )
191 {
192   ASSERT( myBaseImpl );
193   std::istringstream is( theStream );
194
195   string typeName, libName;
196   if ( is >> typeName &&
197        is >> libName )
198   {
199     SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen();
200     SALOMEDS::Study_var curStudy = gen->GetCurrentStudy();
201     gen->SetCurrentStudy( SALOMEDS::Study::_nil() ); // prevent hypo publishing
202
203     try {
204       SMESH::SMESH_Hypothesis_var hyp1D =
205         gen->CreateHypothesis( typeName.c_str(), libName.c_str() );
206       SMESH_Hypothesis_i* hyp1D_i = SMESH::DownCast< SMESH_Hypothesis_i*>( hyp1D );
207       if ( hyp1D_i ) {
208         hyp1D_i->LoadFrom( & theStream[ (streamoff) is.tellg()+1 ]);
209         this->GetImpl()->SetLayerDistribution( hyp1D_i->GetImpl() );
210         myHyp = hyp1D;
211         // as hyp1D is not published, its ID changes
212         //SMESH::TPythonDump() << _this() << ".SetLayerDistribution( " << hyp1D << " )";
213
214         // restore a mark of storage version ("VARS...")
215         SMESH_Hypothesis_i::LoadFrom( & theStream[ (streamoff)is.tellg()+1 ]);
216       }
217     }
218     catch (...) {
219     }
220     gen->SetCurrentStudy( curStudy );  // enable hypo publishing
221   }
222 }
223
224 //================================================================================
225 /*!
226  * \brief Restore myMethod2VarParams by parameters stored in an old study
227  */
228 //================================================================================
229
230 void StdMeshers_LayerDistribution_i::setOldParameters (const char* theParameters)
231 {
232   if ( SMESH_Hypothesis_i* hyp1D_i = SMESH::DownCast< SMESH_Hypothesis_i*>( myHyp ))
233     hyp1D_i->setOldParameters( theParameters );
234 }