1 // VISU OBJECT : interactive object for VISU entities implementation
3 // Copyright (C) 2003 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.
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
23 // File : VISUConfig.cc
24 // Author : Alexey PETROV
27 #include "VISUConfig.hh"
32 static int MYDEBUG = 0;
34 static int MYDEBUG = 0;
38 //===========================================================================
39 QMutex* Base_i::myMutex = NULL; //apo - &VISUMutex;
40 CORBA::ORB_var Base_i::myOrb;
41 PortableServer::POA_var Base_i::myPoa;
42 SALOME_NamingService* Base_i::myNamingService;
43 SALOME_LifeCycleCORBA* Base_i::myEnginesLifeCycle;
44 VISU_Gen_i* Base_i::myVisuGenImpl;
50 char* Base_i::GetID(){
52 CORBA::Object_var anObject = _this();
53 CORBA::String_var anIOR = myOrb->object_to_string(anObject);
56 return CORBA::string_dup(myID.c_str());
59 //===========================================================================
60 static int mySCnt = 0;
61 static int myQCnt = 0;
62 static int myIsBatchMode = 0;
64 Mutex::Mutex(QMutex* theMutex, QApplication* theQApp, int theDelay) :
65 myQApp(theQApp), isQAppLocked(theQApp->locked()), myDelay(theDelay),
66 myMutex(theMutex), isSessionLocked(theMutex->locked())
68 if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<(!isQAppLocked && !myQCnt)<<" "<<(!isSessionLocked && !mySCnt));
69 if(!myIsBatchMode && isQAppLocked) myIsBatchMode++;
70 if(!isSessionLocked && !mySCnt) { myMutex->lock();}; mySCnt++;
71 if(!isQAppLocked && !myQCnt) {
79 if(!isQAppLocked && !myQCnt) {
82 myQApp->processEvents(myDelay+3);
85 mySCnt--; if(!isSessionLocked && !mySCnt) { myMutex->unlock();}
86 if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<(!isQAppLocked && !myQCnt)<<" "<<(!isSessionLocked && !mySCnt));
90 //===========================================================================
91 static Storable::TCallbackMap VisuStoreMap;
92 string Storable::ToString(){
94 Storable::DataToStream( strOut, "myComment", GetComment() );
97 if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
101 void Storable::Registry(const char* theComment, TStorableEngine theEngine)
102 throw(std::logic_error&)
104 if(!VisuStoreMap.insert(TCallbackMap::value_type(theComment,theEngine)).second){
105 if(MYDEBUG) MESSAGE("Storable::Registry >> dupliacte registring !!!");
106 throw std::logic_error("Storable::Registry >> dupliacte registring !!!");
110 void Storable::StrToMap(const QString& theStr, VISU::Storable::TRestoringMap& theMap){
111 if(0 && MYDEBUG) MESSAGE("Storable::StrToMap : string="<<theStr);
112 QStringList strList = QStringList::split( ";", theStr, false );
113 for ( int i = 0; i < strList.count(); i++ ) {
114 QString next = strList[ i ];
115 int pos = next.find("=");
116 QString aName, aValue;
118 aName = next.stripWhiteSpace();
122 aName = next.left( pos ).stripWhiteSpace();
123 aValue = next.mid( pos+1 );
124 if ( aValue.isNull() )
127 if ( !aName.isEmpty() )
128 theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );
132 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const QString& theVal) {
133 QString output = ( !theName.isNull() ? theName : QString("") )
135 + ( !theVal.isNull() ? theVal : QString("") );
136 theStr<<output.latin1()<<";";
139 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const int theVal) {
140 QString output = ( !theName.isNull() ? theName : QString("") )
142 + QString::number( theVal );
143 theStr<<output.latin1()<<";";
146 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const double theVal) {
147 QString output = ( !theName.isNull() ? theName : QString("") )
149 + QString::number( theVal );
150 theStr<<output.latin1()<<";";
152 Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
153 const string& thePrefix, const string& theLocalPersistentID)
154 throw (std::runtime_error&)
157 QString strIn( theLocalPersistentID.c_str() );
159 StrToMap(strIn,aMap);
161 QString aComment = VISU::Storable::FindValue(aMap,"myComment",&isExist);
163 TCallbackMap::const_iterator i = VisuStoreMap.find(aComment.latin1());
164 if(MYDEBUG) MESSAGE("Storable::Create - "<<aComment.latin1()<<" "<<(i != VisuStoreMap.end()));
165 if(i == VisuStoreMap.end()) return NULL;
166 return (i->second)(theSObject,thePrefix,aMap);
168 }catch(std::logic_error& exc){
169 INFOS("Follow exception was accured :\n"<<exc.what());
171 INFOS("Unknown exception was accured!");
176 const QString& Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
177 throw(std::logic_error&)
179 TRestoringMap::const_iterator i = theMap.find(theArg);
180 if(i == theMap.end()) {
181 if(MYDEBUG) MESSAGE("Storable::Init >> there is no value for "<<theArg);
182 if(isFind != NULL) *isFind = false;
183 //throw std::logic_error(string("Storable::Init >> there is no value for ") + theArg);
184 static QString BAD_VALUE("NULL");
187 if(isFind != NULL) *isFind = true;
192 //===========================================================================
193 PortableServer::ServantBase_var GetServant(CORBA::Object_ptr theObject){
194 if(CORBA::is_nil(theObject)) return NULL;
196 PortableServer::POA_ptr aPOA = Base_i::GetPOA();
197 PortableServer::Servant aServant = aPOA->reference_to_servant(theObject);
200 INFOS("GetServant - Unknown exception was accured!!!");
206 //===========================================================================
207 CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
208 SALOMEDS::GenericAttribute_var anAttr;
209 CORBA::Object_var anObj;
211 if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
212 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
213 CORBA::String_var aValue = anIOR->Value();
214 CORBA::ORB_ptr anORB = Base_i::GetORB();
215 if(strcmp(aValue,"") != 0)
216 anObj = anORB->string_to_object(aValue);
219 INFOS("SObjectToObject - Unknown exception was accured!!!");
225 //===========================================================================
226 string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
227 const char* theComment, int IsAllLevels)
229 SALOMEDS::ChildIterator_var anIter =
230 theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
231 anIter->InitEx(IsAllLevels);
232 SALOMEDS::SObject_var aFieldSO;
233 for(;anIter->More();anIter->Next()) {
234 SALOMEDS::GenericAttribute_var anAttr;
235 if (anIter->Value()->FindAttribute(anAttr,"AttributeComment")) {
236 SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
237 CORBA::String_var aString = aCmnt->Value();
238 string aValue(aString);
239 if (aValue == theComment) {
240 aFieldSO = anIter->Value();
241 aString = aFieldSO->GetID();
249 //===========================================================================
250 string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument,
251 const char* theFatherEntry, const char* theRefFatherEntry,
252 const char* theIOR, const char* theName,
253 const char* thePersistentRef, const char* theComment,
254 CORBA::Boolean theCreateNew)
256 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
257 SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry);
258 SALOMEDS::SObject_var newObj;
259 if (theCreateNew) newObj = aStudyBuilder->NewObject(aFather);
260 else newObj = aFather;
261 SALOMEDS::GenericAttribute_var anAttr;
262 if(strcmp(theIOR,"") != 0){
263 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
264 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
265 anIOR->SetValue(theIOR);
267 if(strcmp(theName,"") != 0){
268 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
269 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
270 aName->SetValue(theName);
272 if(strcmp(thePersistentRef,"") != 0){
273 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
274 SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
275 aPRef->SetValue(thePersistentRef);
277 if(strcmp(theComment,"") != 0){
278 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
279 SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
280 aCmnt->SetValue(theComment);
281 INFOS("CreateAttributes - Comment = "<<theComment);
283 if(strcmp(theRefFatherEntry,"") != 0){
284 SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
285 SALOMEDS::SObject_var anObj = aStudyBuilder->NewObject(aRefFather);
286 aStudyBuilder->Addreference(anObj,newObj);
288 CORBA::String_var anEntry = newObj->GetID();
289 string aRet(anEntry);
291 INFOS("CreateAttributes - StudyId = "<<theStudyDocument->StudyId()<<"; anEntry = "<<aRet<<"; IOR = '"<<theIOR<<"'");
297 QString VISU::GenerateName(const string& theFmt, int theId){
298 static QString aName;
300 aName.sprintf("%s:%d",theFmt.c_str(),theId);
302 aName.sprintf("%s",theFmt.c_str());