1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's classes
24 // File : SMESH_Hypothesis_i.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_Hypothesis_i.hxx"
29 #include "SMESH_Gen_i.hxx"
31 #include <utilities.h>
32 #include <SALOMEDS_wrap.hxx>
37 #include <TopExp_Explorer.hxx>
41 //=============================================================================
43 * SMESH_Hypothesis_i::SMESH_Hypothesis_i
47 //=============================================================================
49 SMESH_Hypothesis_i::SMESH_Hypothesis_i( PortableServer::POA_ptr thePOA )
50 : SALOME::GenericObj_i( thePOA )
55 //=============================================================================
57 * SMESH_Hypothesis_i::~SMESH_Hypothesis_i
61 //=============================================================================
63 SMESH_Hypothesis_i::~SMESH_Hypothesis_i()
65 //MESSAGE( "SMESH_Hypothesis_i::~SMESH_Hypothesis_i" );
70 //=============================================================================
72 * SMESH_Hypothesis_i::GetName
74 * Get type name of hypothesis
76 //=============================================================================
78 char* SMESH_Hypothesis_i::GetName()
80 return CORBA::string_dup( myBaseImpl->GetName() );
83 //=============================================================================
85 * SMESH_Hypothesis_i::GetLibName
87 * Get plugin library name of hypothesis (required by persistency mechanism)
89 //=============================================================================
91 char* SMESH_Hypothesis_i::GetLibName()
93 return CORBA::string_dup( myBaseImpl->GetLibName() );
96 //=============================================================================
98 * SMESH_Hypothesis_i::SetLibName
100 * Set plugin library name of hypothesis (required by persistency mechanism)
102 //=============================================================================
104 void SMESH_Hypothesis_i::SetLibName(const char* theLibName)
106 myBaseImpl->SetLibName( theLibName );
109 //=============================================================================
111 * SMESH_Hypothesis_i::GetId
113 * Get unique id of hypothesis
115 //=============================================================================
117 CORBA::Short SMESH_Hypothesis_i::GetId()
119 return myBaseImpl->GetID();
122 //=============================================================================
124 * SMESH_Hypothesis_i::IsPublished()
127 //=============================================================================
128 bool SMESH_Hypothesis_i::IsPublished()
131 if ( SMESH_Gen_i::GetSMESHGen() )
133 SALOMEDS::SObject_wrap SO = SMESH_Gen_i::GetSMESHGen()->ObjectToSObject( _this());
134 res = !SO->_is_nil();
139 //================================================================================
141 * \brief Set the pramIndex-th parameter
143 //================================================================================
145 void SMESH_Hypothesis_i::SetVarParameter (const char* theParameter,
146 const char* theMethod)
148 if ( SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen() )
150 SMESH::SMESH_Hypothesis_var varHolder;
151 if ( myHolder->_is_nil() ) varHolder = _this();
152 else varHolder = myHolder;
153 gen->UpdateParameters( varHolder, theParameter );
155 const std::vector< std::string >& pars = gen->GetLastParameters();
157 myMethod2VarParams[ theMethod ] = pars[0];
161 //================================================================================
163 * \brief Return the pramIndex-th variable parameter used for Hypothesis creation
165 //================================================================================
167 char* SMESH_Hypothesis_i::GetVarParameter (const char* theMethod)
169 if ( myMethod2VarParams.count("needs update by old study"))
171 // restore myMethod2VarParams by old study
172 myMethod2VarParams.clear();
173 if ( SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen() )
175 CORBA::String_var oldparVar = gen->GetParameters( _this() );
176 setOldParameters( oldparVar.in() );
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() );
183 return CORBA::string_dup("");
186 //================================================================================
188 * \brief Store a hypothesis wrapping this not published one.
190 * This hyp, which has no own parameters but is published, is used to store variables
191 * defining parameters of this hypothesis.
193 //================================================================================
195 void SMESH_Hypothesis_i::SetHolderHypothesis(const SMESH::SMESH_Hypothesis_ptr hyp)
197 myHolder = SMESH::SMESH_Hypothesis::_duplicate( hyp );
200 //================================================================================
202 * \brief Restore myMethod2VarParams by parameters stored in an old study
204 //================================================================================
206 void SMESH_Hypothesis_i::setOldParameters (const char* theParameters)
208 if ( SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen() )
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() );
217 myMethod2VarParams.clear();
218 const std::vector< std::string >& pars = gen->GetLastParameters();
219 for ( size_t i = 0; i < pars.size(); ++i )
221 std::string meth = getMethodOfParameter( i, pars.size() );
222 myMethod2VarParams[ meth ] = pars[i];
224 gen->UpdateParameters( CORBA::Object_var( _this() ).in(), "" ); // clear params
228 //=============================================================================
230 * SMESH_Hypothesis_i::GetImpl
234 //=============================================================================
236 ::SMESH_Hypothesis* SMESH_Hypothesis_i::GetImpl()
241 //================================================================================
243 * \brief Return true if a hypothesis has parameters
245 //================================================================================
247 CORBA::Boolean SMESH_Hypothesis_i::HasParameters()
249 std::ostringstream os;
250 myBaseImpl->SaveTo( os );
251 return ( !os.str().empty() );
254 //=============================================================================
256 * SMESH_Hypothesis_i::SaveTo
258 * Persistence: Dumps parameters to the string stream
260 //=============================================================================
262 char* SMESH_Hypothesis_i::SaveTo()
264 std::ostringstream os;
266 // assure that parameters are loaded from an old study
267 CORBA::String_var p = GetVarParameter("");
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 << " ";
276 myBaseImpl->SaveTo( os );
277 return CORBA::string_dup( os.str().c_str() );
280 //=============================================================================
282 * SMESH_Hypothesis_i::LoadFrom
284 * Persistence: Restores parameters from string
286 //=============================================================================
288 void SMESH_Hypothesis_i::LoadFrom( const char* theStream )
290 std::istringstream is( theStream );
291 if ( strncmp( theStream, "VARS", 4 ) == 0 )
297 for ( int i = 0; i < nbVars; ++i )
302 is.get( str, len + 2 ); // 2 - to read at least 1 white space
304 myMethod2VarParams[ meth ] = std::string( str+1, len );
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";
315 myBaseImpl->LoadFrom( is );
317 // let listeners know about loading (issue 0020918)
318 myBaseImpl->NotifySubMeshesHypothesisModification();
321 //================================================================================
323 * \brief This mesthod is called after completion of loading a study
325 //================================================================================
327 void SMESH_Hypothesis_i::UpdateAsMeshesRestored()
329 // for hyps needing full data restored
332 //================================================================================
334 * \brief Check if a shape includes sub-shapes of a given dimension
336 //================================================================================
338 bool GenericHypothesisCreator_i::IsShapeOfDim( const TopoDS_Shape & S,
341 TopAbs_ShapeEnum shapeType;
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;
350 return TopExp_Explorer( S, shapeType ).More();