+//================================================================================
+/*!
+ * \brief Stores names of variables that WILL be passes as parameters when calling
+ * some method of a given object.
+ * \param [in] theObject - the object whose a method WILL be called with \a theParameters.
+ * \param [in] theParameters - a string containing parameters separated by ':'.
+ */
+//================================================================================
+
+void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
+{
+ // find variable names within theParameters
+
+ myLastObj.clear();
+ myLastParameters.clear();
+ myLastParamIndex.clear(); /* vector holding indices of virables within the string
+ of all variables used for theObject */
+ int nbVars = 0;
+ int pos = 0, prevPos = 0, len = strlen( theParameters );
+ if ( len == 0 ) return;
+ while ( pos <= len )
+ {
+ if ( pos == len || theParameters[pos] == ':' )
+ {
+ if ( prevPos < pos )
+ {
+ string val( theParameters + prevPos, theParameters + pos );
+ if ( !getStudyServant()->IsVariable( val.c_str() ))
+ val.clear();
+ myLastParameters.push_back( val );
+ nbVars += (! myLastParameters.back().empty() );
+ }
+ else
+ {
+ myLastParameters.push_back("");
+ }
+ prevPos = pos+1;
+ }
+ ++pos;
+ }
+
+ if ( nbVars < 1 )
+ return;
+
+ // store
+ // (1) variable names in the string of all variables used for theObject and
+ // (2) indices of found variables in myLastParamIndex.
+
+ // remember theObject
+ SALOMEDS::SObject_wrap aSObj = ObjectToSObject(theObject);
+ if ( aSObj->_is_nil() )
+ return;
+ CORBA::String_var anObjEntry = aSObj->GetID();
+ myLastObj = anObjEntry.in();
+
+ // get a string of variable names
+ SALOMEDS::StudyBuilder_var aStudyBuilder = getStudyServant()->NewBuilder();
+ SALOMEDS::GenericAttribute_wrap anAttr =
+ aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
+ SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
+ CORBA::String_var oldVars = aStringAttr->Value();
+ std::string varStr = oldVars.in();
+
+ // add new variables and find indices of variables
+ for ( size_t i = 0; i < myLastParameters.size(); ++i )
+ {
+ int varIndex = -1;
+ if ( !myLastParameters[i].empty() )
+ {
+ // find index of myLastParameters[i] in varStr
+ int curIndex = 0;
+ bool varFound = false;
+ size_t pos = 0;
+ // varStr can be "A|B::C;*=2|D"
+ const std::string separators(":|;*=");
+ while ( pos < varStr.size() && !varFound )
+ {
+ // skip separators
+ while ( separators.find( varStr[ pos ]) != std::string::npos )
+ if ( ++pos >= varStr.size() )
+ break;
+ // skip repetition number following '='
+ if ( varStr[ pos-1 ] == '=' )
+ {
+ while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
+ ++pos;
+ continue; // to skip next separator
+ }
+ // compare variable name
+ if ( pos < varStr.size() )
+ {
+ varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
+ // same string beginning but is length same?
+ ( pos + myLastParameters[i].size() >= varStr.size() ||
+ separators.find( varStr[ pos+1 ]) != std::string::npos ));
+ if ( varFound )
+ varIndex = curIndex;
+ else
+ pos = varStr.find_first_of( separators, pos ); // goto the next separator
+ ++curIndex;
+ }
+ }
+ // add new variable
+ if ( !varFound )
+ {
+ varStr += ":" + myLastParameters[i];
+ varIndex = curIndex;
+ }
+ }
+ myLastParamIndex.push_back( varIndex );
+ }
+ aStringAttr->SetValue( varStr.c_str() );
+}
+
+//================================================================================
+/*!
+ * \brief Return all variables used to create an object
+ * \param [in] theObjectEntry - an object entry in the current study
+ * \return std::vector< std::string > - all variable names (or values of removed variables)
+ */
+//================================================================================
+
+std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
+{
+ std::vector< std::string > varNames;
+
+ SALOMEDS::SObject_wrap aSObj = getStudyServant()->FindObjectID( theObjectEntry.c_str() );
+
+ // get a string of variable names
+ SALOMEDS::StudyBuilder_var aStudyBuilder = getStudyServant()->NewBuilder();
+ SALOMEDS::GenericAttribute_wrap anAttr =
+ aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
+ SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
+ CORBA::String_var oldVars = aStringAttr->Value();
+ std::string varStr = oldVars.in();
+
+ // separate variables within varStr;
+ // varStr can be "A|B::C;*=2|D"
+ size_t pos = 0;
+ const std::string separators(":|;*=");
+ while ( pos < varStr.size() )
+ {
+ // skip separators
+ pos = varStr.find_first_not_of( separators, pos );
+ // while ( separators.find( varStr[ pos ]) != std::string::npos )
+ // if ( ++pos >= varStr.size() )
+ // break;
+ // skip repetition number following '='
+ if ( varStr[ pos-1 ] == '=' )
+ {
+ while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
+ ++pos;
+ continue; // to skip next separator
+ }
+ // store variable name
+ if ( pos < varStr.size() )
+ {
+ size_t pos2 = varStr.find_first_of( separators, pos );
+ varNames.push_back( varStr.substr( pos, pos2 - pos));
+ pos = pos2;
+ }
+ }
+ return varNames;
+}
+
+//=======================================================================
+//function : ParseParameters
+//purpose : Replace variables by their values
+//=======================================================================
+// char* SMESH_Gen_i::ParseParameters(const char* theParameters)
+// {
+// //const char* aParameters = theParameters;
+// // const char* aParameters = CORBA::string_dup(theParameters);
+// TCollection_AsciiString anInputParams;
+// SALOMEDS::Study_var aStudy = getStudyServant();
+// if( !aStudy->_is_nil() ) {
+// // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
+// // for(int j=0;j<aSections->length();j++) {
+// // SALOMEDS::ListOfStrings aVars= aSections[j];
+// // for(int i=0;i<aVars.length();i++ ) {
+// // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
+// // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
+// // if(i != aVars.length()-1)
+// // anInputParams+=":";
+// // }
+// // if(j!=aSections->length()-1)
+// // anInputParams+="|";
+// // }
+// TCollection_AsciiString paramStr( theParameters );
+// int beg = 0, end;
+// char sep, *pParams = (char*)paramStr.ToCString();
+// while ( beg < paramStr.Length() )
+// {
+// end = beg-1;
+// while ( ++end < paramStr.Length() )
+// if ( pParams[end] == ':' || pParams[end] == '|')
+// break;
+// if ( end < paramStr.Length())
+// {
+// sep = pParams[end];
+// pParams[end] = '\0';
+// }
+// if ( aStudy->IsVariable( pParams+beg ))
+// anInputParams += pParams+beg;
+// if ( end < paramStr.Length() )
+// anInputParams += sep;
+// else
+// break;
+// beg = end + 1;
+// }
+// }
+// return CORBA::string_dup(anInputParams.ToCString());
+// }
+
+//=======================================================================
+//function : GetParameters
+//purpose :
+//=======================================================================
+
+char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
+{
+ CORBA::String_var aResult("");
+
+ SALOMEDS::SObject_wrap aSObj = ObjectToSObject( theObject );
+ if ( !aSObj->_is_nil() )
+ {
+ SALOMEDS::GenericAttribute_wrap attr;
+ if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
+ {
+ SALOMEDS::AttributeString_wrap strAttr = attr;
+ aResult = strAttr->Value();
+ }
+ }
+ return aResult._retn();
+}
+
+
+
+// ==============
+// Study context
+// ==============
+
+//=======================================================================
+//function : addObject
+//purpose : register object in the internal map and return its id
+//=======================================================================
+
+int StudyContext::addObject( const std::string& theIOR )
+{
+ int nextId = getNextId();
+ mapIdToIOR.Bind( nextId, theIOR );
+ return nextId;
+}
+
+//=======================================================================
+//function : findId
+//purpose : find the object id in the internal map by the IOR
+//=======================================================================
+
+int StudyContext::findId( const std::string& theIOR )
+{
+ TInt2StringMap::iterator imap;
+ for ( imap = mapIdToIOR.begin(); imap != mapIdToIOR.end(); ++imap ) {
+ if ( *imap == theIOR )
+ return imap.Iterator().Key();
+ }
+ return 0;
+}
+
+//=======================================================================
+//function : getIORbyId
+//purpose : get object's IOR by id
+//=======================================================================
+
+std::string StudyContext::getIORbyId( const int theId )
+{
+ if ( mapIdToIOR.IsBound( theId ) )
+ return mapIdToIOR( theId );
+ return std::string();
+}
+
+//=======================================================================
+//function : getIORbyOldId
+//purpose : get object's IOR by old id
+//=======================================================================
+
+std::string StudyContext::getIORbyOldId( const int theOldId )
+{
+ if ( mapIdToId.IsBound( theOldId ) )
+ return getIORbyId( mapIdToId( theOldId ));
+ return std::string();
+}
+
+//=======================================================================
+//function : mapOldToNew
+//purpose : maps old object id to the new one (used when restoring data)
+//=======================================================================
+
+void StudyContext::mapOldToNew( const int oldId, const int newId )
+{
+ mapIdToId.Bind( oldId, newId );
+}
+
+//=======================================================================
+//function : getOldId
+//purpose : get old id by a new one
+//=======================================================================
+
+int StudyContext::getOldId( const int newId )
+{
+ TInt2IntMap::iterator imap;
+ for ( imap = mapIdToId.begin(); imap != mapIdToId.end(); ++imap ) {
+ if ( *imap == newId )
+ return imap.Iterator().Key();
+ }
+ return 0;
+}