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