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