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