Salome HOME
Copyright update 2021
[modules/smesh.git] / src / SMESH_I / SMESH_Hypothesis_i.cxx
1 // Copyright (C) 2007-2021  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 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's classes
24 //  File   : SMESH_Hypothesis_i.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //
28 #include "SMESH_Hypothesis_i.hxx"
29 #include "SMESH_Gen_i.hxx"
30
31 #include <utilities.h>
32 #include <SALOMEDS_wrap.hxx>
33
34 #include <iostream>
35 #include <sstream>
36
37 #include <TopExp_Explorer.hxx>
38
39 using namespace std;
40
41 //=============================================================================
42 /*!
43  *  SMESH_Hypothesis_i::SMESH_Hypothesis_i
44  * 
45  *  Constructor
46  */
47 //=============================================================================
48
49 SMESH_Hypothesis_i::SMESH_Hypothesis_i( PortableServer::POA_ptr thePOA )
50   : SALOME::GenericObj_i( thePOA )
51 {
52   myBaseImpl = 0;
53 }
54
55 //=============================================================================
56 /*!
57  *  SMESH_Hypothesis_i::~SMESH_Hypothesis_i
58  *
59  *  Destructor
60  */
61 //=============================================================================
62
63 SMESH_Hypothesis_i::~SMESH_Hypothesis_i()
64 {
65   //MESSAGE( "SMESH_Hypothesis_i::~SMESH_Hypothesis_i" );
66   if ( myBaseImpl )
67     delete myBaseImpl;
68 }
69
70 //=============================================================================
71 /*!
72  *  SMESH_Hypothesis_i::GetName
73  *
74  *  Get type name of hypothesis
75  */
76 //=============================================================================
77
78 char* SMESH_Hypothesis_i::GetName()
79 {
80   return CORBA::string_dup( myBaseImpl->GetName() );
81 }
82
83 //=============================================================================
84 /*!
85  *  SMESH_Hypothesis_i::GetLibName
86  *
87  *  Get plugin library name of hypothesis (required by persistency mechanism)
88  */
89 //=============================================================================
90
91 char* SMESH_Hypothesis_i::GetLibName()
92 {
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   myBaseImpl->SetLibName( theLibName );
107 }
108
109 //=============================================================================
110 /*!
111  *  SMESH_Hypothesis_i::GetId
112  *
113  *  Get unique id of hypothesis
114  */
115 //=============================================================================
116
117 CORBA::Long SMESH_Hypothesis_i::GetId()
118 {
119   return myBaseImpl->GetID();
120 }
121
122 //=============================================================================
123 /*!
124  *  SMESH_Hypothesis_i::IsPublished()
125  *
126  */
127 //=============================================================================
128 bool SMESH_Hypothesis_i::IsPublished()
129 {
130   bool res = false;
131   if ( SMESH_Gen_i::GetSMESHGen() )
132   {
133     SALOMEDS::SObject_wrap SO = SMESH_Gen_i::GetSMESHGen()->ObjectToSObject( _this());
134     res = !SO->_is_nil();
135   }
136   return res;
137 }
138
139 //================================================================================
140 /*!
141  * \brief Set the pramIndex-th parameter
142  */
143 //================================================================================
144
145 void SMESH_Hypothesis_i::SetVarParameter (const char* theParameter,
146                                           const char* theMethod)
147 {
148   if ( SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen() )
149   {
150     SMESH::SMESH_Hypothesis_var varHolder;
151     if ( myHolder->_is_nil() ) varHolder = _this();
152     else                       varHolder = myHolder;
153     gen->UpdateParameters( varHolder, theParameter );
154
155     const std::vector< std::string >& pars = gen->GetLastParameters();
156     if ( !pars.empty() )
157       myMethod2VarParams[ theMethod ] = pars[0];
158   }
159 }
160
161 //================================================================================
162 /*!
163  * \brief Return the pramIndex-th variable parameter used for Hypothesis creation
164  */
165 //================================================================================
166
167 char* SMESH_Hypothesis_i::GetVarParameter (const char* theMethod)
168 {
169   if ( myMethod2VarParams.count("needs update by old study"))
170   {
171     // restore myMethod2VarParams by old study
172     myMethod2VarParams.clear();
173     if ( SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen() )
174     {
175       CORBA::String_var oldparVar = gen->GetParameters( _this() );
176       setOldParameters( oldparVar.in() );
177     }
178   }
179   std::map< std::string, std::string >::iterator meth_param = myMethod2VarParams.find( theMethod );
180   if ( meth_param != myMethod2VarParams.end() )
181     return CORBA::string_dup( meth_param->second.c_str() );
182
183   return CORBA::string_dup("");
184 }
185
186 //================================================================================
187 /*!
188  * \brief Store a hypothesis wrapping this not published one.
189  *
190  * This hyp, which has no own parameters but is published, is used to store variables
191  * defining parameters of this hypothesis.
192  */
193 //================================================================================
194
195 void SMESH_Hypothesis_i::SetHolderHypothesis(const SMESH::SMESH_Hypothesis_ptr hyp)
196 {
197   myHolder = SMESH::SMESH_Hypothesis::_duplicate( hyp );
198 }
199
200 //================================================================================
201 /*!
202  * \brief Restore myMethod2VarParams by parameters stored in an old study
203  */
204 //================================================================================
205
206 void SMESH_Hypothesis_i::setOldParameters (const char* theParameters)
207 {
208   if ( SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen() )
209   {
210     TCollection_AsciiString aOldParameters(theParameters);
211     int pos = aOldParameters.SearchFromEnd("|");
212     if ( pos >= 0 ) aOldParameters = aOldParameters.Split(pos);
213     pos = aOldParameters.SearchFromEnd(";*=");
214     if ( pos >= 0 ) aOldParameters.Split(pos-1);
215     gen->UpdateParameters( CORBA::Object_var( _this() ).in(), aOldParameters.ToCString() );
216
217     myMethod2VarParams.clear();
218     const std::vector< std::string >& pars = gen->GetLastParameters();
219     for ( size_t i = 0; i < pars.size(); ++i )
220     {
221       std::string meth = getMethodOfParameter( i, pars.size() );
222       myMethod2VarParams[ meth ] = pars[i];
223     }
224     gen->UpdateParameters( CORBA::Object_var( _this() ).in(), "" ); // clear params
225   }
226 }
227
228 //=============================================================================
229 /*!
230  *  SMESH_Hypothesis_i::GetImpl
231  *
232  *  Get implementation
233  */
234 //=============================================================================
235
236 ::SMESH_Hypothesis* SMESH_Hypothesis_i::GetImpl()
237 {
238   return myBaseImpl;
239 }
240
241 //================================================================================
242 /*!
243  * \brief Return true if a hypothesis has parameters
244  */
245 //================================================================================
246
247 CORBA::Boolean SMESH_Hypothesis_i::HasParameters()
248 {
249   std::ostringstream os;
250   myBaseImpl->SaveTo( os );
251   return ( !os.str().empty() );
252 }
253
254 //=============================================================================
255 /*!
256  *  SMESH_Hypothesis_i::SaveTo
257  *
258  *  Persistence: Dumps parameters to the string stream
259  */
260 //=============================================================================
261
262 char* SMESH_Hypothesis_i::SaveTo()
263 {
264   std::ostringstream os;
265
266   // assure that parameters are loaded from an old study
267   CORBA::String_var p = GetVarParameter("");
268
269   os << "VARS " << myMethod2VarParams.size()  << " ";
270   std::map< std::string, std::string >::iterator meth_param = myMethod2VarParams.begin();
271   for ( ; meth_param != myMethod2VarParams.end(); ++meth_param )
272     os << meth_param->first << " "
273        << meth_param->second.size() << " "
274        << meth_param->second << " ";
275
276   myBaseImpl->SaveTo( os );
277   return CORBA::string_dup( os.str().c_str() );
278 }
279
280 //=============================================================================
281 /*!
282 *  SMESH_Hypothesis_i::LoadFrom
283 *
284 *  Persistence: Restores parameters from string
285 */
286 //=============================================================================
287
288 void SMESH_Hypothesis_i::LoadFrom( const char* theStream )
289 {
290   std::istringstream is( theStream );
291   if ( strncmp( theStream, "VARS", 4 ) == 0 )
292   {
293     int nbVars, len;
294     char str[256];
295     std::string meth;
296     is >> str >> nbVars;
297     for ( int i = 0; i < nbVars; ++i )
298     {
299       is >> meth >> len;
300       if ( len < 256 )
301       {
302         is.get( str, len + 2 ); // 2 - to read at least 1 white space
303         if ( len > 0 )
304           myMethod2VarParams[ meth ] = std::string( str+1, len );
305       }
306     }
307   }
308   else
309   {
310     // we can't restore myMethod2VarParams by old study here because SObject
311     // isn't yet bound to _this()
312     myMethod2VarParams["needs update by old study"] = "yes";
313   }
314
315   myBaseImpl->LoadFrom( is );
316
317   // let listeners know about loading (issue 0020918)
318   myBaseImpl->NotifySubMeshesHypothesisModification();
319 }
320
321 //================================================================================
322 /*!
323  * \brief This mesthod is called after completion of loading a study 
324  */
325 //================================================================================
326
327 void SMESH_Hypothesis_i::UpdateAsMeshesRestored()
328 {
329   // for hyps needing full data restored
330 }
331
332 //================================================================================
333 /*!
334  * \brief Check if a shape includes sub-shapes of a given dimension
335  */
336 //================================================================================
337
338 bool GenericHypothesisCreator_i::IsShapeOfDim( const TopoDS_Shape & S,
339                                                int                  dim )
340 {
341   TopAbs_ShapeEnum shapeType;
342   switch ( dim )
343   {
344   case 0: shapeType = TopAbs_VERTEX; break;
345   case 1: shapeType = TopAbs_EDGE; break;
346   case 2: shapeType = TopAbs_FACE; break;
347   case 3: shapeType = TopAbs_SOLID; break;
348   default: return true;
349   }
350   return TopExp_Explorer( S, shapeType ).More();
351 }