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 QMutex aMutex(TRUE);
63 Mutex::Mutex(QMutex* theMutex): myMutex(&aMutex){
64 if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<!mySCnt);
72 if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<!mySCnt);
76 //===========================================================================
77 static Storable::TCallbackMap VisuStoreMap;
78 string Storable::ToString(){
80 Storable::DataToStream( strOut, "myComment", GetComment() );
83 if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
87 void Storable::Registry(const char* theComment, TStorableEngine theEngine)
89 if(!VisuStoreMap.insert(TCallbackMap::value_type(theComment,theEngine)).second){
90 if(MYDEBUG) MESSAGE("Storable::Registry >> dupliacte registring !!!");
91 throw std::logic_error("Storable::Registry >> dupliacte registring !!!");
95 void Storable::StrToMap(const QString& theStr, VISU::Storable::TRestoringMap& theMap){
96 if(0 && MYDEBUG) MESSAGE("Storable::StrToMap : string="<<theStr);
97 QStringList strList = QStringList::split( ";", theStr, false );
98 for ( int i = 0; i < strList.count(); i++ ) {
99 QString next = strList[ i ];
100 int pos = next.find("=");
101 QString aName, aValue;
103 aName = next.stripWhiteSpace();
107 aName = next.left( pos ).stripWhiteSpace();
108 aValue = next.mid( pos+1 );
109 if ( aValue.isNull() )
112 if ( !aName.isEmpty() )
113 theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );
117 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const QString& theVal) {
118 QString output = ( !theName.isNull() ? theName : QString("") )
120 + ( !theVal.isNull() ? theVal : QString("") );
121 theStr<<output.latin1()<<";";
124 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const int theVal) {
125 QString output = ( !theName.isNull() ? theName : QString("") )
127 + QString::number( theVal );
128 theStr<<output.latin1()<<";";
131 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const double theVal) {
132 QString output = ( !theName.isNull() ? theName : QString("") )
134 + QString::number( theVal );
135 theStr<<output.latin1()<<";";
137 Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
138 const string& thePrefix, const string& theLocalPersistentID)
141 QString strIn( theLocalPersistentID.c_str() );
143 StrToMap(strIn,aMap);
145 QString aComment = VISU::Storable::FindValue(aMap,"myComment",&isExist);
147 TCallbackMap::const_iterator i = VisuStoreMap.find(aComment.latin1());
148 if(MYDEBUG) MESSAGE("Storable::Create - "<<aComment.latin1()<<" "<<(i != VisuStoreMap.end()));
149 if(i == VisuStoreMap.end()) return NULL;
150 return (i->second)(theSObject,thePrefix,aMap);
152 }catch(std::logic_error& exc){
153 INFOS("Follow exception was occured :\n"<<exc.what());
155 INFOS("Unknown exception was occured!");
160 const QString& Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
162 TRestoringMap::const_iterator i = theMap.find(theArg);
163 if(i == theMap.end()) {
164 if(MYDEBUG) MESSAGE("Storable::Init >> there is no value for "<<theArg);
165 if(isFind != NULL) *isFind = false;
166 //throw std::logic_error(string("Storable::Init >> there is no value for ") + theArg);
167 static QString BAD_VALUE("NULL");
170 if(isFind != NULL) *isFind = true;
175 //===========================================================================
176 PortableServer::ServantBase_var GetServant(CORBA::Object_ptr theObject){
177 if(CORBA::is_nil(theObject)) return NULL;
179 PortableServer::POA_ptr aPOA = Base_i::GetPOA();
180 PortableServer::Servant aServant = aPOA->reference_to_servant(theObject);
183 INFOS("GetServant - Unknown exception was occured!!!");
189 //===========================================================================
190 CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
191 SALOMEDS::GenericAttribute_var anAttr;
192 CORBA::Object_var anObj;
194 if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
195 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
196 CORBA::String_var aValue = anIOR->Value();
197 CORBA::ORB_ptr anORB = Base_i::GetORB();
198 if(strcmp(aValue,"") != 0)
199 anObj = anORB->string_to_object(aValue);
202 INFOS("SObjectToObject - Unknown exception was occured!!!");
208 //===========================================================================
209 string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
210 const char* theComment, int IsAllLevels)
212 SALOMEDS::ChildIterator_var anIter =
213 theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
214 anIter->InitEx(IsAllLevels);
215 SALOMEDS::SObject_var aFieldSO;
216 for(;anIter->More();anIter->Next()) {
217 SALOMEDS::GenericAttribute_var anAttr;
218 if (anIter->Value()->FindAttribute(anAttr,"AttributeComment")) {
219 SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
220 CORBA::String_var aString = aCmnt->Value();
221 string aValue(aString);
222 if (aValue == theComment) {
223 aFieldSO = anIter->Value();
224 aString = aFieldSO->GetID();
232 //===========================================================================
233 string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument,
234 const char* theFatherEntry, const char* theRefFatherEntry,
235 const char* theIOR, const char* theName,
236 const char* thePersistentRef, const char* theComment,
237 CORBA::Boolean theCreateNew)
239 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
240 SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry);
241 SALOMEDS::SObject_var newObj;
242 if (theCreateNew) newObj = aStudyBuilder->NewObject(aFather);
243 else newObj = aFather;
244 SALOMEDS::GenericAttribute_var anAttr;
245 if(strcmp(theIOR,"") != 0){
246 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
247 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
248 anIOR->SetValue(theIOR);
250 if(strcmp(theName,"") != 0){
251 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
252 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
253 aName->SetValue(theName);
255 if(strcmp(thePersistentRef,"") != 0){
256 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
257 SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
258 aPRef->SetValue(thePersistentRef);
260 if(strcmp(theComment,"") != 0){
261 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
262 SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
263 aCmnt->SetValue(theComment);
264 if(MYDEBUG) INFOS("CreateAttributes - Comment = "<<theComment);
266 if(strcmp(theRefFatherEntry,"") != 0){
267 SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
268 SALOMEDS::SObject_var anObj = aStudyBuilder->NewObject(aRefFather);
269 aStudyBuilder->Addreference(anObj,newObj);
271 CORBA::String_var anEntry = newObj->GetID();
272 string aRet(anEntry);
274 INFOS("CreateAttributes - StudyId = "<<theStudyDocument->StudyId()<<"; anEntry = "<<aRet<<"; IOR = '"<<theIOR<<"'");
280 QString VISU::GenerateName(const string& theFmt, int theId){
281 static QString aName;
283 aName.sprintf("%s:%d",theFmt.c_str(),theId);
285 aName.sprintf("%s",theFmt.c_str());