2 // File : VISU_Gen_i.cc file
4 // Author : Alexey Petrov
6 // Copyright : OPEN CASCADE
9 #include "VISU_Gen_i.hh"
10 #include "VISU_Result_i.hh"
11 #include "VISU_Convertor.hxx"
12 #include "VISU_PrsObject_i.hh"
13 #include "VISU_ViewManager_i.hh"
14 #include "VISU_Table_i.hh"
16 #include "QAD_Application.h"
17 #include "QAD_Desktop.h"
18 #include "QAD_Study.h"
19 #include "SALOMEDS_Tool.hxx"
21 #include <TCollection_AsciiString.hxx>
22 #include <TColStd_SequenceOfAsciiString.hxx>
23 #include <vtkUnstructuredGridReader.h>
24 #include <vtkUnstructuredGridWriter.h>
26 #include <omnithread.h>
27 #include CORBA_SERVER_HEADER(SALOME_Session)
30 #include <qfileinfo.h>
31 static QFileInfo aFileInfo;
34 static int MYDEBUG = 1;
36 static int MYDEBUG = 0;
39 #include <utilities.h>
41 //static int IS_LOADED = (cout<<"\n----------------- VisuEngine_factory is loaded -----------------\n", 1);
43 PortableServer::ObjectId *
44 VISUEngine_factory(CORBA::ORB_ptr orb,
45 PortableServer::POA_ptr poa,
46 PortableServer::ObjectId * contId,
47 const char *instanceName,
48 const char *interfaceName)
50 if(MYDEBUG) MESSAGE("VisuEngine_factory : "<<interfaceName);
51 VISU::VISU_Gen_i * pVISU_Gen = new VISU::VISU_Gen_i(orb, poa, contId, instanceName, interfaceName);
52 return pVISU_Gen->getId() ;
56 // GetVisuGen(CORBA::ORB_var orb, PortableServer::POA_var poa, QMutex* theMutex){
57 // if(MYDEBUG) MESSAGE("extern \"C\" GetVisuGen");
58 // VISU::VISU_Gen_i * aVISU_Gen = new VISU::VISU_Gen_i(orb,poa,theMutex);
59 // return VISU::VISU_Gen::_duplicate(aVISU_Gen->_this());
62 Engines::Component_ptr
63 GetVisuGen(CORBA::ORB_var orb, PortableServer::POA_var poa, QMutex* theMutex){
64 if(MYDEBUG) MESSAGE("extern \"C\" GetVisuGen");
65 VISU::VISU_Gen_i * aVISU_Gen = new VISU::VISU_Gen_i(orb,poa,theMutex);
66 return Engines::Component::_narrow( VISU::VISU_Gen::_duplicate(aVISU_Gen->_this()) );
71 //===========================================================================
72 QMutex* Base_i::myMutex = NULL;
73 CORBA::ORB_var Base_i::myOrb;
74 PortableServer::POA_var Base_i::myPOA;
76 char* Base_i::GetID(){
77 //CORBA::Object_var anObject = _this();
78 //return CORBA::string_dup(myOrb->object_to_string(anObject));
79 return CORBA::string_dup(myOrb->object_to_string(_this()));
81 //===========================================================================
82 static int mySCnt = 0;
83 static int myQCnt = 0;
84 Mutex::Mutex(QMutex* theMutex, QApplication* theQApp) :
85 myQApp(theQApp), isQAppLocked(theQApp->locked()),
86 myMutex(theMutex), isSessionLocked(theMutex->locked())
88 if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<!isQAppLocked<<" "<<!isSessionLocked);
89 if(!isSessionLocked) { myMutex->lock(); mySCnt++; }
90 if(!isQAppLocked) { myQApp->lock(); myQCnt++; }
92 //if(MYDEBUG) MESSAGE("Mutex::Mutex - "<<myQCnt<<" "<<mySCnt);
95 if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<!isQAppLocked<<" "<<!isSessionLocked);
97 if(!isQAppLocked) { myQApp->unlock(); myQCnt--; }
98 if(!isSessionLocked) { myMutex->unlock(); mySCnt--; }
99 //if(MYDEBUG) MESSAGE("Mutex::~Mutex - "<<myQCnt<<" "<<mySCnt);
100 // myQApp->processEvents(); myQApp->processEvents(); myQApp->processEvents();
102 //===========================================================================
103 static Storable::TCallbackMap VisuStoreMap;
104 const char* Storable::ToString(){
106 Storable::DataToStream( strOut, "myComment", GetComment() );
109 if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
112 void Storable::Registry(const char* theComment, TStorableEngine theEngine)
113 throw(std::logic_error&)
115 if(!VisuStoreMap.insert(TCallbackMap::value_type(theComment,theEngine)).second){
116 if(MYDEBUG) MESSAGE("Storable::Registry >> dupliacte registring !!!");
117 throw std::logic_error("Storable::Registry >> dupliacte registring !!!");
120 void RegistryStorable() throw(std::logic_error&) {
121 //Storable::Registry(View3D_i::myComment.c_str(),&View3DRestore);
122 Storable::Registry(Result_i::myComment.c_str(),&ResultRestore);
123 Storable::Registry(Mesh_i::myComment.c_str(),&MeshRestore);
124 Storable::Registry(ScalarMap_i::myComment.c_str(),&ScalarMapRestore);
125 Storable::Registry(DeformedShape_i::myComment.c_str(),&DeformedShapeRestore);
126 Storable::Registry(CutPlanes_i::myComment.c_str(),&CutPlanesRestore);
127 Storable::Registry(IsoSurfaces_i::myComment.c_str(),&IsoSurfacesRestore);
128 Storable::Registry(StreamLines_i::myComment.c_str(),&StreamLinesRestore);
129 Storable::Registry(Vectors_i::myComment.c_str(),&VectorsRestore);
130 Storable::Registry(Table_i::myComment.c_str(),&TableRestore);
131 Storable::Registry(Curve_i::myComment.c_str(),&CurveRestore);
132 Storable::Registry(Container_i::myComment.c_str(),&ContainerRestore);
135 SALOMEDS::SObject_ptr Storable::GetResultSO(SALOMEDS::SObject_ptr theSObject) {
136 CORBA::String_var aComponentID(theSObject->GetFatherComponent()->GetID());
137 SALOMEDS::SObject_var aResult = SALOMEDS::SObject::_duplicate(theSObject);
138 CORBA::String_var aFatherID(aResult->GetFather()->GetID());
139 while(strcmp(aComponentID, aFatherID)) {
140 aResult = aResult->GetFather();
141 aFatherID = aResult->GetFather()->GetID();
142 if (strlen(aFatherID) < 3) break;
144 return aResult._retn();
147 void Storable::StrToMap(const QString& theStr, VISU::Storable::TRestoringMap& theMap){
148 if(MYDEBUG) MESSAGE("Storable::StrToMap : string="<<theStr);
149 QStringList strList = QStringList::split( ";", theStr, false );
150 for ( int i = 0; i < strList.count(); i++ ) {
151 QString next = strList[ i ];
152 int pos = next.find("=");
153 QString aName, aValue;
155 aName = next.stripWhiteSpace();
159 aName = next.left( pos ).stripWhiteSpace();
160 aValue = next.mid( pos+1 );
161 if ( aValue.isNull() )
164 if ( !aName.isEmpty() )
165 theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );
168 void Storable::DataToStream(ostrstream& theStr, const QString& theName, const QString& theVal) {
169 QString output = ( !theName.isNull() ? theName : QString("") )
171 + ( !theVal.isNull() ? theVal : QString("") );
172 theStr<<output.latin1()<<";";
174 void Storable::DataToStream(ostrstream& theStr, const QString& theName, const int theVal) {
175 QString output = ( !theName.isNull() ? theName : QString("") )
177 + QString::number( theVal );
178 theStr<<output.latin1()<<";";
180 void Storable::DataToStream(ostrstream& theStr, const QString& theName, const double theVal) {
181 QString output = ( !theName.isNull() ? theName : QString("") )
183 + QString::number( theVal );
184 theStr<<output.latin1()<<";";
186 Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
187 const string& thePrefix, const string& theLocalPersistentID)
188 throw (std::runtime_error&)
191 QString strIn( theLocalPersistentID.c_str() );
193 StrToMap(strIn,aMap);
194 //CORBA::String_var aResultID(GetResultSO(theSObject)->GetID());
195 //aMap.insert(TRestoringMap::value_type("ResultID", strdup(aResultID)));
197 QString aComment = VISU::Storable::FindValue(aMap,"myComment",&isExist);
199 TCallbackMap::const_iterator i = VisuStoreMap.find(aComment.latin1());
200 if(MYDEBUG) MESSAGE("Storable::Create - "<<aComment.latin1()<<" "<<(i != VisuStoreMap.end()));
201 if(i == VisuStoreMap.end()) return NULL;
202 return (i->second)(theSObject,thePrefix,aMap);
204 }catch(std::logic_error& exc){
205 MESSAGE("Follow exception was accured :\n"<<exc.what());
207 MESSAGE("Unknown exception was accured!");
211 const QString& Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
212 throw(std::logic_error&)
214 TRestoringMap::const_iterator i = theMap.find(theArg);
215 if(i == theMap.end()) {
216 if(MYDEBUG) MESSAGE("Storable::Init >> there is no value for "<<theArg);
217 if(isFind != NULL) *isFind = false;
218 //throw std::logic_error(string("Storable::Init >> there is no value for ") + theArg);
219 static QString BAD_VALUE("NULL");
222 if(isFind != NULL) *isFind = true;
226 //===========================================================================
227 PortableServer::Servant GetServant(CORBA::Object_ptr theObject){
228 PortableServer::POA_var aPOA = GetPOA();
229 if(CORBA::is_nil(theObject))
230 if(MYDEBUG) MESSAGE("GetServant - CORBA::is_nil(theObject)");
231 return aPOA->reference_to_servant(theObject);
233 //===========================================================================
234 CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
235 SALOMEDS::GenericAttribute_var anAttr;
236 CORBA::Object_var anObj;
237 if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
238 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
239 CORBA::String_var aValue = anIOR->Value();
240 CORBA::ORB_var anORB = GetORB();
241 if(strcmp(aValue,"") != 0)
242 anObj = anORB->string_to_object(aValue);
246 //===========================================================================
247 SALOMEDS::SComponent_var FindOrCreateVisuComponent(SALOMEDS::Study_ptr theStudyDocument){
248 SALOMEDS::SComponent_var aSComponent = theStudyDocument->FindComponent("VISU");
249 if (aSComponent->_is_nil()) {
250 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
251 aStudyBuilder->NewCommand();
252 int aLocked = theStudyDocument->GetProperties()->IsLocked();
253 if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
254 aSComponent = aStudyBuilder->NewComponent("VISU");
255 SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
256 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
257 aName->SetValue("Visu");
259 anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
260 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
261 aPixmap->SetPixMap( "ICON_OBJBROWSER_Visu" );
263 VISU_Gen_var aVisuGen = GetVisu();
264 aStudyBuilder->DefineComponentInstance(aSComponent,aVisuGen);
265 if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
266 aStudyBuilder->CommitCommand();
270 //===========================================================================
271 string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
272 const char* theComment, int IsAllLevels)
274 if(MYDEBUG) MESSAGE("FindEntryWithComment - '"<<theComment<<"'");
275 SALOMEDS::ChildIterator_var anIter =
276 theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
277 anIter->InitEx(IsAllLevels);
278 SALOMEDS::SObject_var aFieldSO;
279 for(;anIter->More();anIter->Next()) {
280 SALOMEDS::GenericAttribute_var anAttr;
281 if (anIter->Value()->FindAttribute(anAttr,"AttributeComment")) {
282 SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
283 CORBA::String_var aString = aCmnt->Value();
284 string aValue(aString);
285 if (aValue == theComment) {
286 aFieldSO = anIter->Value();
287 aString = aFieldSO->GetID();
295 //===========================================================================
296 string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument,
297 const char* theFatherEntry, const char* theRefFatherEntry,
298 const char* theIOR, const char* theName,
299 const char* thePersistentRef, const char* theComment,
300 CORBA::Boolean theCreateNew)
303 MESSAGE("CreateAttributes - theName = "<<theName<<"; theComment = '"<<theComment<<
304 "'; theFatherEntry = "<<theFatherEntry);
305 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
306 SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry);
307 SALOMEDS::SObject_var newObj;
308 if (theCreateNew) newObj = aStudyBuilder->NewObject(aFather);
309 else newObj = aFather;
310 SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
311 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
312 anIOR->SetValue(theIOR);
313 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
314 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
315 aName->SetValue(theName);
316 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
317 SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
318 aPRef->SetValue(thePersistentRef);
319 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
320 SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
321 aCmnt->SetValue(theComment);
322 if(strcmp(theRefFatherEntry,"") != 0){
323 SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
324 SALOMEDS::SObject_var anObj = aStudyBuilder->NewObject(aRefFather);
325 aStudyBuilder->Addreference(anObj,newObj);
327 CORBA::String_var anEntry = newObj->GetID();
328 string aRet(anEntry);
329 if(MYDEBUG) MESSAGE("CreateAttributes - anEntry = "<<aRet<<"; IOR = "<<theIOR);
332 //===========================================================================
333 static CORBA::ORB_var VisuORB;
334 static PortableServer::POA_var VisuPOA;
335 static SALOMEDS::SComponent_var VisuSComponent;
336 static string VisuTmpDir;
337 static VISU_Gen_i* VisuGen;
338 static CORBA::Boolean myIsMultiFile;
339 CORBA::ORB_var GetORB() { return VisuORB;}
340 PortableServer::POA_var GetPOA() { return VisuPOA;}
341 VISU_Gen_var GetVisu() {
342 return VISU_Gen::_duplicate(VisuGen->_this());
343 //return VisuGen->_this();
345 const CORBA::Boolean IsMultifile() { return myIsMultiFile;}
346 //===========================================================================
347 bool VISU_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
348 const SALOMEDS::TMPFile & theStream,
352 if(MYDEBUG) MESSAGE("VISU_Gen_i::Load - myMutex = "<<myMutex);
354 Mutex mt(myMutex,qApp);
355 SALOMEDS::Study_var aStudy = theComponent->GetStudy();
356 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
357 TCollection_AsciiString aTmpDir =
358 isMultiFile?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
359 VisuSComponent = SALOMEDS::SComponent::_duplicate(theComponent);
360 VisuTmpDir = aTmpDir.ToCString();
361 SALOMEDS::ListOfFileNames_var aSeq =
362 SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(),isMultiFile);
363 myIsMultiFile = isMultiFile;
366 return myVisuGen->Load(theComponent,theStream,theURL,isMultiFile);
369 char* VISU_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
370 const char* aLocalPersistentID,
371 CORBA::Boolean isMultiFile) {
373 Mutex mt(myMutex,qApp);
374 CORBA::String_var aString("");
375 if(strcmp(aLocalPersistentID,"") != 0) {
376 Storable* aStorable =
377 Storable::Create(theSObject,VisuTmpDir.c_str(),aLocalPersistentID);
378 if(aStorable != NULL) aString = aStorable->GetID();
380 return aString._retn();
382 return myVisuGen->LocalPersistentIDToIOR(theSObject, aLocalPersistentID, isMultiFile);
384 //===========================================================================
385 SALOMEDS::TMPFile* VISU_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
389 if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - myMutex = "<<myMutex);
391 Mutex mt(myMutex,qApp);
392 TCollection_AsciiString aTmpDir =
393 (isMultiFile)?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
394 if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aTmpDir);
396 TColStd_SequenceOfAsciiString aFileNames;
397 SALOMEDS::Study_var aStudy = theComponent->GetStudy();
398 SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
399 for (; itBig->More(); itBig->Next()) {
400 SALOMEDS::SObject_var gotBranch = itBig->Value();
401 CORBA::Object_var anObj = SObjectToObject(gotBranch);
402 if(CORBA::is_nil(anObj)) continue;
403 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
405 const QFileInfo& aFileInfo = pResult->GetFileInfo();
406 QString aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL());
407 QString aFileName = aPrefix + "_" + pResult->GetName();
408 static QString aCommand;
409 aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
410 if(system(aCommand) == -1){
411 if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Cann't execute the command :"<<aCommand);
414 if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aCommand);
415 TCollection_AsciiString aString(strdup(aFileName.latin1()));
416 aFileNames.Append(aString);
419 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
420 if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
421 if(aFileNames.Length() > 0){
422 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
423 aSeq->length(aFileNames.Length());
424 for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
425 aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
426 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
428 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
430 return aStreamFile._retn();
432 return myVisuGen->Save(theComponent,theURL,isMultiFile);
435 char* VISU_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
436 const char* IORString,
437 CORBA::Boolean isMultiFile) {
438 if(MYDEBUG) MESSAGE("VISU_Gen_i::IORToLocalPersistentID - myMutex = "<<myMutex);
440 Mutex mt(myMutex,qApp);
441 CORBA::ORB_var anORB = GetORB();
442 CORBA::String_var aString(IORString);
443 if(strcmp(IORString,"") != 0){
444 CORBA::Object_var anObj = anORB->string_to_object(aString);
445 if(!CORBA::is_nil(anObj)){
446 Storable* pStorable = dynamic_cast<Storable*>(GetServant(anObj));
447 if(pStorable != NULL){
448 aString = pStorable->ToString();
449 return aString._retn();
453 return aString._retn();
455 return myVisuGen->IORToLocalPersistentID(theSObject, IORString, isMultiFile);
458 //===========================================================================
459 VISU_Gen_i::VISU_Gen_i(CORBA::ORB_ptr orb,
460 PortableServer::POA_ptr poa,
461 PortableServer::ObjectId * contId,
462 const char *instanceName,
463 const char *interfaceName) :
464 Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
466 if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<myMutex);
468 _id = _poa->activate_object(_thisObj);
469 SALOME_NamingService aNamingService(orb);
470 SALOME::Session_var aSession(SALOME::Session::_narrow(aNamingService.Resolve("/Kernel/Session")));
471 //aSession->GetInterface();
472 //myVisuGen = aSession->GetVisuGen();
473 myVisuGen = VISU::VISU_Gen::_narrow( aSession->GetVisuComponent() );
475 VISU_Gen_i::VISU_Gen_i(CORBA::ORB_var orb, PortableServer::POA_var poa, QMutex* theMutex) :
476 Engines_Component_i()
478 if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<theMutex);
479 Base_i::myMutex = theMutex;
480 Base_i::myOrb = CORBA::ORB::_duplicate(orb);
481 Base_i::myPOA = PortableServer::POA::_duplicate(poa);
482 Mutex mt(myMutex,qApp);
486 VisuPOA->activate_object(this);
488 if(!QAD_Application::getDesktop()->getActiveStudy())
489 QAD_Application::getDesktop()->createStudy();
491 QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
493 VISU_Gen_i::~VISU_Gen_i(){
494 if(MYDEBUG) MESSAGE("VISU_Gen_i::~VISU_Gen_i");
496 void VISU_Gen_i::SetCurrentStudy(SALOMEDS::Study_ptr theStudy){
497 //if(MYDEBUG) MESSAGE("VISU_Gen_i::SetCurrentStudy : "<<(!theStudy->_is_nil()));
499 myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
501 myVisuGen->SetCurrentStudy(theStudy);
503 SALOMEDS::Study_ptr VISU_Gen_i::GetCurrentStudy(){
504 if(MYDEBUG) MESSAGE("VISU_Gen_i::GetCurrentStudy : "<<myMutex);
506 return SALOMEDS::Study::_duplicate(myStudyDocument);
507 return myVisuGen->GetCurrentStudy();
509 ViewManager_ptr VISU_Gen_i::GetViewManager(){
511 //omni_mutex_lock aMutexLock(aMutex);
512 if(MYDEBUG) MESSAGE("VISU_Gen_i::GetViewManager : "<<myMutex);
514 Mutex mt(myMutex,qApp);
515 ViewManager_i * aViewManager = new ViewManager_i(myStudyDocument);
516 return ViewManager::_duplicate(aViewManager->_this());
518 return myVisuGen->GetViewManager();
521 SALOMEDS::SObject_ptr VISU_Gen_i::ImportTables(const char* theFileName){
523 if(myStudyDocument->GetProperties()->IsLocked()) return SALOMEDS::SObject::_nil();
524 Mutex mt(myMutex,qApp);
525 SALOMEDS::SObject_var aRes = VISU::ImportTables(theFileName,myStudyDocument);
528 return myVisuGen->ImportTables(theFileName);
531 Result_ptr VISU_Gen_i::ImportFile(const char* theFileName){
533 if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
534 Mutex mt(myMutex,qApp);
535 aFileInfo.setFile(theFileName);
536 Result_i* pResult = new Result_i(myStudyDocument);
537 if(pResult->Create(theFileName) != NULL)
538 return Result::_duplicate(pResult->_this());
539 return Result::_nil();
541 return myVisuGen->ImportFile(theFileName);
543 Result_ptr VISU_Gen_i::ImportMed(SALOME_MED::FIELD_ptr theField){
544 if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMed : "<<myMutex);
546 if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
547 Mutex mt(myMutex,qApp);
548 Result_i* pResult = new Result_i(myStudyDocument);
549 if(pResult->Create(theField) != NULL)
550 return Result::_duplicate(pResult->_this());
551 return Result::_nil();
553 return myVisuGen->ImportMed(theField);
555 Result_ptr VISU_Gen_i::ImportMedMesh(SALOME_MED::MESH_ptr theMesh){
556 if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMedMesh : "<<myMutex);
558 if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
559 Mutex mt(myMutex,qApp);
560 Result_i* pResult = new Result_i(myStudyDocument);
561 if(pResult->Create(theMesh) != NULL)
562 return Result::_duplicate(pResult->_this());
563 return Result::_nil();
565 return myVisuGen->ImportMedMesh(theMesh);
568 Result_ptr VISU_Gen_i::ImportMedSupport(SALOME_MED::SUPPORT_ptr theSupport){
569 if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMedSupport : "<<myMutex);
571 if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
572 Mutex mt(myMutex,qApp);
573 Result_i* pResult = new Result_i(myStudyDocument);
574 if(pResult->Create(theSupport) != NULL)
575 return Result::_duplicate(pResult->_this());
576 return Result::_nil();
578 return myVisuGen->ImportMedSupport(theSupport);
581 Mesh_ptr VISU_Gen_i::MeshOnEntity(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity){
582 if(MYDEBUG) MESSAGE("VISU_Gen_i::MeshOnEntity : "<<myMutex);
584 if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
585 Mutex mt(myMutex,qApp);
586 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
587 Mesh_i* aPresent = new Mesh_i(pResult);
588 if(aPresent->Create(theMeshName,theEntity) != NULL)
589 return Mesh::_duplicate(aPresent->_this());
592 return myVisuGen->MeshOnEntity(theResult,theMeshName,theEntity);
595 Mesh_ptr VISU_Gen_i::FamilyMeshOnEntity(Result_ptr theResult, const char* theMeshName,
596 VISU::Entity theEntity,
597 const char* theFamilyName){
598 if(MYDEBUG) MESSAGE("VISU_Gen_i::FamilyMeshOnEntity : "<<myMutex);
600 if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
601 Mutex mt(myMutex,qApp);
602 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
603 Mesh_i* aPresent = new Mesh_i(pResult);
604 if(aPresent->Create(theMeshName,theEntity,theFamilyName) != NULL)
605 return Mesh::_duplicate(aPresent->_this());
608 return myVisuGen->FamilyMeshOnEntity(theResult,theMeshName,theEntity,theFamilyName);
611 Mesh_ptr VISU_Gen_i::GroupMesh(Result_ptr theResult, const char* theMeshName,
612 const char* theGroupName){
613 if(MYDEBUG) MESSAGE("VISU_Gen_i::GroupMesh : "<<myMutex);
615 if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
616 Mutex mt(myMutex,qApp);
617 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
618 Mesh_i* aPresent = new Mesh_i(pResult);
619 if(aPresent->Create(theMeshName,theGroupName) != NULL)
620 return Mesh::_duplicate(aPresent->_this());
623 return myVisuGen->GroupMesh(theResult,theMeshName,theGroupName);
626 ScalarMap_ptr VISU_Gen_i::ScalarMapOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
627 const char* theFieldName, CORBA::Double theIteration){
628 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
630 if(myStudyDocument->GetProperties()->IsLocked()) return ScalarMap::_nil();
631 Mutex mt(myMutex,qApp);
632 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
633 if(ScalarMap_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
634 ScalarMap_i* aPresent = new ScalarMap_i(pResult);
635 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
636 return ScalarMap::_duplicate(aPresent->_this());
638 return ScalarMap::_nil();
640 return myVisuGen->ScalarMapOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
643 DeformedShape_ptr VISU_Gen_i::DeformedShapeOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
644 const char* theFieldName, CORBA::Double theIteration){
645 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
647 if(myStudyDocument->GetProperties()->IsLocked()) return DeformedShape::_nil();
648 Mutex mt(myMutex,qApp);
649 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
650 if(DeformedShape_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
651 DeformedShape_i* aPresent = new DeformedShape_i(pResult);
652 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
653 return DeformedShape::_duplicate(aPresent->_this());
655 return DeformedShape::_nil();
657 return myVisuGen->DeformedShapeOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
660 Vectors_ptr VISU_Gen_i::VectorsOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
661 const char* theFieldName, CORBA::Double theIteration){
662 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
664 if(myStudyDocument->GetProperties()->IsLocked()) return Vectors::_nil();
665 Mutex mt(myMutex,qApp);
666 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
667 if(Vectors_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
668 Vectors_i* aPresent = new Vectors_i(pResult);
669 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
670 return Vectors::_duplicate(aPresent->_this());
672 return Vectors::_nil();
674 return myVisuGen->VectorsOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
677 IsoSurfaces_ptr VISU_Gen_i::IsoSurfacesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
678 const char* theFieldName, CORBA::Double theIteration){
679 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
681 if(myStudyDocument->GetProperties()->IsLocked()) return IsoSurfaces::_nil();
682 Mutex mt(myMutex,qApp);
683 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
684 if(IsoSurfaces_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
685 IsoSurfaces_i* aPresent = new IsoSurfaces_i(pResult);
686 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
687 return IsoSurfaces::_duplicate(aPresent->_this());
689 return IsoSurfaces::_nil();
691 return myVisuGen->IsoSurfacesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
694 StreamLines_ptr VISU_Gen_i::StreamLinesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
695 const char* theFieldName, CORBA::Double theIteration){
696 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
698 if(myStudyDocument->GetProperties()->IsLocked()) return StreamLines::_nil();
699 Mutex mt(myMutex,qApp);
700 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
701 if(StreamLines_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
702 StreamLines_i* aPresent = new StreamLines_i(pResult);
703 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
704 return StreamLines::_duplicate(aPresent->_this());
706 return StreamLines::_nil();
708 return myVisuGen->StreamLinesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
711 CutPlanes_ptr VISU_Gen_i::CutPlanesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
712 const char* theFieldName, CORBA::Double theIteration){
713 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
715 if(myStudyDocument->GetProperties()->IsLocked()) return CutPlanes::_nil();
716 Mutex mt(myMutex,qApp);
717 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
718 if(CutPlanes_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
719 CutPlanes_i* aPresent = new CutPlanes_i(pResult);
720 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
721 return CutPlanes::_duplicate(aPresent->_this());
723 return CutPlanes::_nil();
725 return myVisuGen->CutPlanesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
728 Table_ptr VISU_Gen_i::CreateTable(const char* theTableEntry){
729 if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateTable : "<<myMutex);
731 if(myStudyDocument->GetProperties()->IsLocked()) return Table::_nil();
732 Mutex mt(myMutex,qApp);
733 Table_i* aPresent = new Table_i(myStudyDocument,theTableEntry);
734 if(aPresent->Create() != NULL)
735 return Table::_duplicate(aPresent->_this());
736 return Table::_nil();
738 return myVisuGen->CreateTable(theTableEntry);
741 Curve_ptr VISU_Gen_i::CreateCurve(Table_ptr theTable, CORBA::Long theHRow, CORBA::Long theVRow){
742 if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateCurve : "<<myMutex);
744 if(myStudyDocument->GetProperties()->IsLocked()) return Curve::_nil();
745 Mutex mt(myMutex,qApp);
746 PortableServer::POA_var aPOA = GetPOA();
747 Table_i* pTable = dynamic_cast<Table_i*>(aPOA->reference_to_servant(theTable));
748 Curve_i* aPresent = new Curve_i(myStudyDocument,pTable,theHRow,theVRow);
749 if(aPresent->Create() != NULL) {
750 return Curve::_duplicate(aPresent->_this());
752 return Curve::_nil();
754 return myVisuGen->CreateCurve(theTable,theHRow,theVRow);
756 Container_ptr VISU_Gen_i::CreateContainer(){
757 if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateContainer : "<<myMutex);
759 if(myStudyDocument->GetProperties()->IsLocked()) return Container::_nil();
760 Mutex mt(myMutex,qApp);
761 Container_i* aPresent = new Container_i(myStudyDocument);
762 if(aPresent->Create() != NULL) {
763 return Container::_duplicate(aPresent->_this());
765 return Container::_nil();
767 return myVisuGen->CreateContainer();
770 void VISU_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent){
771 if(MYDEBUG) MESSAGE("VISU_Gen_i::Close : "<<myMutex);
773 Mutex mt(myMutex,qApp);
774 SALOMEDS::Study_var aStudy = theComponent->GetStudy();
775 if(!aStudy->_is_nil()){
776 SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
777 for (int i = 0; itBig->More(); itBig->Next(),i++) {
778 SALOMEDS::SObject_var gotBranch = itBig->Value();
779 if(MYDEBUG) MESSAGE("VISU_Gen_i::Close : itBig->Next() = "<<i);
780 CORBA::Object_var anObj = SObjectToObject(gotBranch);
781 if(CORBA::is_nil(anObj)) continue;
782 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
783 if(pResult->IsRestored()){ //Try remove its file and directory
784 const QFileInfo& aFileInfo = pResult->GetFileInfo();
785 static QString aCommand;
786 aCommand.sprintf("rm %s",aFileInfo.filePath().latin1());
787 if(system(aCommand) != -1 && MYDEBUG) MESSAGE("VISU_Gen_i::Close - "<<aCommand);
788 aCommand.sprintf("rmdir --ignore-fail-on-non-empty %s",aFileInfo.dirPath().latin1());
789 if(system(aCommand) != -1 && MYDEBUG) MESSAGE("VISU_Gen_i::Close - "<<aCommand);
795 myVisuGen->Close(theComponent);
798 char* VISU_Gen_i::ComponentDataType(){
799 return CORBA::string_dup("VISU");
802 bool VISU_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
803 Result_var aResultObj = Result::_narrow(theIOR);
804 return !(aResultObj->_is_nil());
807 SALOMEDS::SObject_ptr VISU_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
808 SALOMEDS::SObject_ptr theSObject,
809 CORBA::Object_ptr theObject,
811 throw (SALOME::SALOME_Exception)
813 if(MYDEBUG) MESSAGE("VISU_Gen_i::PublishInStudy : "<<myMutex);
815 Mutex mt(myMutex,qApp);
816 SALOMEDS::SObject_var aResultSO;
817 PortableServer::POA_var aPOA = GetPOA();
818 Result_i* aResultObj = dynamic_cast<Result_i*>(aPOA->reference_to_servant(theObject));
819 if (!aResultObj) return aResultSO._retn();
821 CORBA::String_var anEntry;
822 const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
823 anEntry = strdup(aResultObj->Create(aFileInfo.filePath().latin1())->GetID());
824 // if (CORBA::is_nil(theSObject)) {
825 // anEntry = strdup(aResultObj->Create(SALOMEDS::SObject::_nil(), theName));
827 // if (!theSObject->ReferencedObject(aResultSO))
828 // THROW_SALOME_CORBA_EXCEPTION("Publish error: bad referenced SObject",SALOME::BAD_PARAM);
829 // anEntry = strdup(aResultObj->Create(aResultSO, (const char *)theName));
831 aResultSO = theStudy->FindObjectID(anEntry);
832 return aResultSO._retn();
834 return myVisuGen->PublishInStudy(theStudy, theSObject, theObject, theName);
837 CORBA::Boolean VISU_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
838 if(MYDEBUG) MESSAGE("VISU_Gen_i::CanCopy : "<<myMutex);
840 //Mutex mt(myMutex,qApp);
841 SALOMEDS::GenericAttribute_var anAttr;
842 if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
844 CORBA::Object_var anObj = GetORB()->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
845 if (CORBA::is_nil(anObj)) return false;
847 Result_var aResultObj = Result::_narrow(anObj);
848 if (aResultObj->_is_nil()) return false;
855 return myVisuGen->CanCopy(theObject);
858 SALOMEDS::TMPFile* VISU_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
859 if(MYDEBUG) MESSAGE("VISU_Gen_i::CopyFrom : "<<myMutex);
861 Mutex mt(myMutex,qApp);
864 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
865 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
867 PortableServer::POA_var aPOA = GetPOA();
869 SALOMEDS::GenericAttribute_var anAttr;
870 if (!theObject->FindAttribute(anAttr,"AttributeIOR")) return NULL;
871 SALOMEDS::AttributeIOR_var anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
872 CORBA::Object_var aCorbaObj;
874 aCorbaObj = GetORB()->string_to_object(anIORAttr->Value());
876 aStreamFile->length(1);
877 aStreamFile[0] = strdup("E")[0];
878 return aStreamFile._retn();
880 if (CORBA::is_nil(aCorbaObj)) {
884 Storable* pStorable = dynamic_cast<Storable*>(GetServant(aCorbaObj));
888 CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir();
890 char* aStr = strdup(pStorable->ToString());
891 ofstream stmOut2((string(aTmpDir) + string("copy_persistent")).c_str(),ios::out);
895 Result_i* aResultObj = dynamic_cast<Result_i*>(aPOA->reference_to_servant(aCorbaObj));
897 string aStudyPrefix = string(SALOMEDS_Tool::GetNameFromPath(theObject->GetStudy()->URL())) + string("_");
899 const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
900 static QString aCommand;
901 aCommand.sprintf("cp %s %s",
902 aFileInfo.filePath().latin1(),
903 (string(aTmpDir)+aStudyPrefix + string(aResultObj->GetName())).c_str());
904 if(system(aCommand) == -1) {
905 if(MYDEBUG) MESSAGE("VISU_Gen_i::Copy - Cann't execute the command :"<<aCommand);
910 aSeq[0] = "copy_persistent";
911 aSeq[1] = (aStudyPrefix + string(aResultObj->GetName())).c_str();
914 aSeq[0] = "copy_persistent";
916 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir, aSeq.in(), false);
917 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeq.in(), true);
919 // Assign an ID = 1 the the type VISU::Result
921 return aStreamFile._retn();
923 return myVisuGen->CopyFrom(theObject, theObjectID);
926 CORBA::Boolean VISU_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
927 // The VISU component can paste only objects copied by VISU component
928 // and with the object type = 1
929 if(MYDEBUG) MESSAGE("VISU_Gen_i::CanPaste : "<<myMutex);
930 if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
934 SALOMEDS::SObject_ptr VISU_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
935 CORBA::Long theObjectID,
936 SALOMEDS::SObject_ptr theObject) {
937 if(MYDEBUG) MESSAGE("VISU_Gen_i::PasteInto : "<<myMutex);
939 Mutex mt(myMutex,qApp);
940 SALOMEDS::SObject_var aResultSO;
941 if (theObjectID != 1) return aResultSO._retn();
943 CORBA::String_var aTmpDir = strdup(SALOMEDS_Tool::GetTmpDir());
944 SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
946 SALOMEDS::SComponent_var aComponent = theObject->GetFatherComponent();
947 SALOMEDS::Study_var aStudy = theObject->GetStudy();
948 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
949 CORBA::ORB_var anORB = GetORB();
951 ifstream stmIn((string(aTmpDir) + string("copy_persistent")).c_str());
952 // ifstream stmIn((string(aTmpDir) + string("copy_persistent")).c_str(),ios::in);
953 stmIn.seekg(0, ios::end);
954 int aLength = stmIn.tellg();
955 stmIn.seekg(0, ios::beg);
956 char* aString = new char[aLength+1];
957 stmIn.read(aString, aLength);
958 aString[aLength] = 0;
959 myIsMultiFile = true;
960 Storable* aStorable = Storable::Create(theObject,aTmpDir.in(),aString);
962 SALOMEDS::ListOfFileNames_var aSeqToRm = new SALOMEDS::ListOfFileNames;
964 aSeqToRm[0] = "copy_persistent";
965 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeqToRm.in(), true);
967 CORBA::String_var aComponentIDString(aComponent->GetID());
968 if (strcmp(aComponentIDString, theObject->GetID()) == 0) {
969 // create the new result SObject
970 aResultSO = aStudyBuilder->NewObject(aComponent);
972 aResultSO = SALOMEDS::SObject::_duplicate(theObject);
974 SALOMEDS::AttributeIOR_var anIOR =
975 SALOMEDS::AttributeIOR::_narrow(aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR"));
976 anIOR->SetValue(aStorable->GetID());
977 return aResultSO._retn();
979 return myVisuGen->PasteInto(theStream,theObjectID,theObject);