2 //=============================================================================
3 // File : SuperV_Impl.cxx
5 // Author : Jean Rahuel
8 //=============================================================================
20 #include "utilities.h"
22 #include "SALOME_Container_i.hxx"
24 #include "SuperV_Impl.hxx"
26 #include "SALOMEDS_Attributes.hh"
27 #include <TCollection_AsciiString.hxx>
28 #include <TColStd_SequenceOfAsciiString.hxx>
29 #include "SALOMEDS_Tool.hxx"
31 #include "Utils_CorbaException.hxx"
33 #define STUDY_SUPERVISION "SUPERV"
35 SuperV_Impl::SuperV_Impl( CORBA::ORB_ptr orb ,
36 PortableServer::POA_ptr poa ,
37 PortableServer::ObjectId * contId ,
38 const char *instanceName ,
39 const char *interfaceName
43 Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
44 MESSAGE("SuperV_Impl::SuperV_Impl activate object instanceName("
45 << instanceName << ") interfaceName(" << interfaceName << ") --> "
46 << hex << (void *) this << dec )
48 _id = _poa->activate_object(_thisObj);
49 _Orb = CORBA::ORB::_duplicate(orb);
54 SuperV_Impl::SuperV_Impl() {
57 SuperV_Impl::~SuperV_Impl() {
58 beginService( "SuperV_Impl::~SuperV_Impl" );
59 endService( "SuperV_Impl::~SuperV_Impl" );
62 void SuperV_Impl::Destroy() {
63 beginService( "SuperV_Impl::Destroy" );
64 _poa->deactivate_object(*_id) ;
65 CORBA::release(_poa) ;
67 _thisObj->_remove_ref();
69 endService( "SuperV_Impl::Destroy" );
72 // void SuperV_Impl::Save(const char *IORSComponent, const char *aUrlOfFile) {
73 SALOMEDS::TMPFile* SuperV_Impl::Save(SALOMEDS::SComponent_ptr theComponent,
76 SALOMEDS::TMPFile_var aStreamFile;
77 // Get a temporary directory to store a file
78 TCollection_AsciiString aTmpDir("");
79 // Create a list to store names of created files
80 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
82 CORBA::String_var myStudyName = strdup(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()));
84 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(theComponent);
85 TColStd_SequenceOfAsciiString aFileNames;
86 TCollection_AsciiString aName;
88 for(; anIter->More(); anIter->Next()) {
89 SALOMEDS::SObject_var aSO = anIter->Value();
90 SALOMEDS::GenericAttribute_var anAttr;
91 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
92 aName = TCollection_AsciiString(myStudyName) +
93 TCollection_AsciiString("_SUPERVISION_") +
94 TCollection_AsciiString(aSO->Tag()) +
95 TCollection_AsciiString(".xml");
96 aFileNames.Append(aName);
99 CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()) ;
100 SUPERV::Graph_var graph = SUPERV::Graph::_narrow(anObj);
101 if (aTmpDir.Length() == 0)
102 aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
103 graph->Export((aTmpDir + aName).ToCString());
107 if (aFileNames.Length() == 0) return new SALOMEDS::TMPFile(0);
109 aSeq->length(aFileNames.Length());
111 for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
112 aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
114 // Conver a file to the byte stream
115 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
116 // Remove the created file and tmp directory
117 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
119 // Add python files to the temporary files sequence
120 SALOMEDS::ListOfFileNames_var aPySeq = new SALOMEDS::ListOfFileNames;
121 aPySeq->length(aSeq->length());
122 for(aCounter = aSeq->length(); aCounter > 0; aCounter--) {
123 char* aCName = CORBA::string_dup(aSeq[aCounter-1]);
124 int a = strlen(aCName);
125 aCName[a-3] = 'p';aCName[a-2] = 'y';aCName[a-1] = 0;
126 aPySeq[aCounter-1] = aCName;
128 // Remove the created file and tmp directory
129 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aPySeq.in(), true);
130 // Return the created byte stream
131 return aStreamFile._retn();
134 // void SuperV_Impl::Load(const char* IORSComponent, const char* aUrlOfFile) {
135 CORBA::Boolean SuperV_Impl::Load(SALOMEDS::SComponent_ptr theComponent,
136 const SALOMEDS::TMPFile& theStream,
139 // MESSAGE("SuperV_Impl::Load : IOR = " << IORSComponent << " , Url = " << aUrlOfFile);
140 // Get a temporary directory for a file
141 if (theStream.length() != 0) {
142 TCollection_AsciiString aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
143 myStrURL = strdup(aTmpDir.ToCString());
144 SALOMEDS_Tool::PutStreamToFiles(theStream, myStrURL, isMultiFile);
149 void SuperV_Impl::Close(SALOMEDS::SComponent_ptr theComponent) {
150 // mpv 06.03.2003: bug SAL1508 - graphs will be destructed at the GUI-level at the supervision frame closing
152 // SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(theComponent);
153 // for(; anIter->More(); anIter->Next()) {
154 // SALOMEDS::SObject_var aSO = anIter->Value();
155 // SALOMEDS::GenericAttribute_var anAttr;
156 // if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
157 // CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()) ;
158 // SUPERV::Graph_var graph = SUPERV::Graph::_narrow(anObj);
164 char* SuperV_Impl::ComponentDataType() {
165 return(strdup("SUPERV"));
168 char* SuperV_Impl::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
169 const char* IORString,
170 CORBA::Boolean isMultiFile) {
171 MESSAGE("SuperV_Impl::IORToLocalPersistentID: IOR:"<<IORString);
174 CORBA::Object_var anObj = _orb->string_to_object(IORString) ;
175 if (CORBA::is_nil(anObj)) return CORBA::string_dup(IORString);
176 SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(anObj);
177 if (aGraph->_is_nil()) return CORBA::string_dup(IORString);
179 return CORBA::string_dup(IORString);
182 // sprintf(aName, "%s_SUPERVISION_tmp_%d.xml",myStudyName,myCounter);
183 CORBA::String_var myStudyName = strdup(SALOMEDS_Tool::GetNameFromPath(theSObject->GetStudy()->URL()));
184 TCollection_AsciiString aName = TCollection_AsciiString(myStudyName) +
185 TCollection_AsciiString("_SUPERVISION_") +
186 TCollection_AsciiString(theSObject->Tag()) +
187 TCollection_AsciiString(".xml");
188 return CORBA::string_dup(aName.ToCString());
191 char* SuperV_Impl::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
192 const char* aLocalPersistentID,
193 CORBA::Boolean isMultiFile)
194 throw(SALOME::SALOME_Exception)
196 MESSAGE("SuperV_Impl::LocalPersistentIDToIOR : IOR = " << aLocalPersistentID);
197 CORBA::String_var aFatherID = theSObject->GetFather()->GetID();
198 CORBA::String_var aComponentID = theSObject->GetFatherComponent()->GetID();
199 if (strcmp(aFatherID, aComponentID)) return CORBA::string_dup(aLocalPersistentID);
201 string aStr(myStrURL);
202 aStr += string(aLocalPersistentID);
203 SUPERV::Graph_ptr graph = Graph(aStr.c_str());
205 if (graph->_is_nil())
206 THROW_SALOME_CORBA_EXCEPTION("Unable to find graph in the file",SALOME::BAD_PARAM);
208 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
210 aSeq[0]=aLocalPersistentID;
211 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(myStrURL, aSeq.in(), true);
213 return _Orb->object_to_string(graph);
216 bool SuperV_Impl::CanPublishInStudy(CORBA::Object_ptr theIOR) {
217 SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(theIOR);
218 return !(aGraph->_is_nil());
221 SALOMEDS::SObject_ptr SuperV_Impl::PublishInStudy(SALOMEDS::Study_ptr theStudy,
222 SALOMEDS::SObject_ptr theSObject,
223 CORBA::Object_ptr theObject,
224 const char* theName) throw (SALOME::SALOME_Exception) {
225 SALOMEDS::SObject_var aResultSO;
227 if(CORBA::is_nil(theObject)) return aResultSO._retn();;
229 if(theStudy->_is_nil()) return aResultSO._retn();;
231 SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
232 SALOMEDS::GenericAttribute_var anAttr;
233 SALOMEDS::AttributeName_var aName;
234 SALOMEDS::AttributeIOR_var anIOR;
235 SALOMEDS::AttributePixMap_var aPixmap;
236 SALOMEDS::SComponent_var aFather = theStudy->FindComponent(STUDY_SUPERVISION);
238 if (aFather->_is_nil()) {
239 aFather = aBuilder->NewComponent(STUDY_SUPERVISION);
240 anAttr = aBuilder->FindOrCreateAttribute(aFather, "AttributeName");
241 aName = SALOMEDS::AttributeName::_narrow(anAttr);
242 //NRI aName->SetValue(STUDY_SUPERVISION);
244 SALOME_NamingService *NamingService = new SALOME_NamingService(_Orb);
245 CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
246 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
247 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( STUDY_SUPERVISION );
248 if ( !Comp->_is_nil() ) {
249 aName->SetValue( Comp->componentusername() );
252 anAttr = aBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
253 aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
254 aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
256 aBuilder->DefineComponentInstance(aFather, SuperG::_this());
259 SALOMEDS::SObject_var dataflowI;
260 SALOMEDS::ChildIterator_var dataflowS = theStudy->NewChildIterator(aFather);
262 SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(theObject);
264 const char* name = aGraph->Name();
266 if (CORBA::is_nil(theSObject)) { // if SObject is null, then searching for exist graph SObject
267 for (; dataflowS->More(); dataflowS->Next()) {
268 dataflowI = dataflowS->Value();
269 if (dataflowI->FindAttribute(anAttr, "AttributeName")) {
270 aName = SALOMEDS::AttributeName::_narrow(anAttr);
271 if (strcmp(aName->Value(), name) == 0) {
272 aResultSO = dataflowI;
277 } else { // else searching for referenced SObject
278 if (!theSObject->ReferencedObject(aResultSO))
279 THROW_SALOME_CORBA_EXCEPTION("Publish in study supervision graph error",SALOME::BAD_PARAM);
282 if (strlen(theName) != 0) name = theName;
283 if (CORBA::is_nil(aResultSO)) aResultSO = aBuilder->NewObject(aFather);
285 aName = SALOMEDS::AttributeName::_narrow(aBuilder->FindOrCreateAttribute(aResultSO, "AttributeName"));
286 aName->SetValue(name);
287 anIOR = SALOMEDS::AttributeIOR::_narrow(aBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR"));
288 anIOR->SetValue(aGraph->getIOR());
289 // aBuilder->Addreference(theSObject, aResultSO);
290 return aResultSO._retn();
293 CORBA::Boolean SuperV_Impl::CanCopy(SALOMEDS::SObject_ptr theObject) {
294 // Try to retrieve known by SUPERVISION component Graph by given IOR
295 SALOMEDS::GenericAttribute_var anAttr;
296 if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
298 CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
299 if (CORBA::is_nil(anObj)) return false;
301 SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(anObj);
302 if (aGraph->_is_nil()) return false;
310 SALOMEDS::TMPFile* SuperV_Impl::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
311 // Declare a sequence of the byte to store the copied object
312 SALOMEDS::TMPFile_var aStreamFile;
314 // Try to get GEOM_Shape object by given SObject
315 SALOMEDS::GenericAttribute_var anAttr;
316 if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return aStreamFile._retn();
317 CORBA::String_var anIOR = strdup(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
320 SUPERV::Graph_var aGraph;
322 CORBA::Object_var anObj = _orb->string_to_object(anIOR) ;
323 if (CORBA::is_nil(anObj)) isGraph = false;
325 aGraph = SUPERV::Graph::_narrow(anObj);
326 if (aGraph->_is_nil()) isGraph = false;
332 if (!isGraph) { // it's just a value, stored in the IOR attribute
333 int aLen = strlen(anIOR);
334 CORBA::Octet* anOctetBuf = (CORBA::Octet*)strdup(anIOR);
335 aStreamFile = SALOMEDS::TMPFile_var(new SALOMEDS::TMPFile(aLen, aLen, anOctetBuf, 1));
336 return aStreamFile._retn();
339 // Get a temporary directory to store a temporary file
340 CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir();
341 // Create a list to store names of created files
342 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
344 aSeq[0] = strdup("_SUPERVISION_tmp.xml");
345 char* aFullName = new char[strlen(aTmpDir)+strlen(aSeq[0])+1];
346 strcpy(aFullName, aTmpDir);
347 strcpy(aFullName+strlen(aTmpDir), aSeq[0]);
348 aGraph->Export(strdup(aFullName));
351 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir, aSeq.in(), false);
352 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeq.in(), true);
354 // Assign an ID = 1 the the type SUPERV::Graph
357 return aStreamFile._retn();
360 CORBA::Boolean SuperV_Impl::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
361 // The Supervision component can paste only objects copied by Supervision component
362 // and with the object type = 1
363 if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
367 SALOMEDS::SObject_ptr SuperV_Impl::PasteInto(const SALOMEDS::TMPFile& theStream,
368 CORBA::Long theObjectID,
369 SALOMEDS::SObject_ptr theObject) {
370 SALOMEDS::SObject_var aResultSO;
372 SALOMEDS::StudyBuilder_var aBuilder = theObject->GetStudy()->NewBuilder();
374 bool isGraph = (strcmp(theObject->GetFatherComponent()->GetID(),theObject->GetID()) == 0);
377 SALOMEDS::AttributeIOR_var anIOR =
378 SALOMEDS::AttributeIOR::_narrow(aBuilder->FindOrCreateAttribute(theObject, "AttributeIOR"));
379 anIOR->SetValue((char*)theStream.NP_data());
380 return SALOMEDS::SObject_var(theObject)._retn();
382 if(CORBA::is_nil(theObject)) return aResultSO._retn();
384 CORBA::String_var aTmpDir = strdup(SALOMEDS_Tool::GetTmpDir());
385 SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
386 char* aFullName = new char[strlen(aTmpDir)+strlen(aSeq[0])+1];
387 strcpy(aFullName, aTmpDir);
388 strcpy(aFullName+strlen(aTmpDir), aSeq[0]);
389 SUPERV::Graph_ptr aGraph = Graph(strdup(aFullName));
392 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeq.in(), true);
395 aResultSO = aBuilder->NewObject(theObject);
397 aResultSO = theObject;
400 SALOMEDS::AttributeIOR_var anIOR =
401 SALOMEDS::AttributeIOR::_narrow(aBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR"));
402 anIOR->SetValue(aGraph->getIOR());
404 return aResultSO._retn();
407 SUPERV::Value_ptr SuperV_Impl::StringValue( const char * aStrIOR ) {
408 // beginService( "Super_Impl::StringValue" );
409 CORBA::Any * anAny = new CORBA::Any() ;
412 CORBA::Object_ptr obj = _Orb->string_to_object( aStrIOR ) ;
418 catch ( CORBA::BAD_PARAM ) {
422 if ( strchr( aStrIOR , '.' ) != NULL &&
423 sscanf( aStrIOR , "%lf%*n", &d)) {
425 // MESSAGE( "Value( " << aStrIOR << " ) --> double " << d );
428 else if (sscanf( aStrIOR , "%ld%*n", &l)) {
430 // MESSAGE( "Value( " << aStrIOR << " ) --> long " << l );
435 // MESSAGE( "Value( " << aStrIOR << " ) --> string " << aStrIOR );
439 // endService( "SuperV_Impl::StringValue" );
440 return Value( anAny ) ;
443 SUPERV::Value_ptr SuperV_Impl::AnyValue( const CORBA::Any & anAny ) {
444 // beginService( "Super_Impl::AnyValue" );
445 CORBA::Any * aAny = new CORBA::Any( anAny ) ;
446 // endService( "SuperV_Impl::AnyValue" );
447 return Value( aAny ) ;
450 SUPERV::Value_ptr SuperV_Impl::Value( CORBA::Any * anAny ) {
451 // beginService( "Super_Impl::Value" );
452 Value_Impl * myValue = new Value_Impl( _Orb , _Poa , _ContId ,
453 instanceName() , interfaceName() ,
454 NULL , NULL , NULL , anAny , true ) ;
455 PortableServer::ObjectId * id = myValue->getId() ;
456 CORBA::Object_var obj = _poa->id_to_reference(*id);
457 SUPERV::Value_var iobject ;
458 iobject = SUPERV::Value::_narrow(obj) ;
459 // endService( "SuperV_Impl::Value" );
460 return SUPERV::Value::_duplicate( iobject ) ;
463 SUPERV::Graph_ptr SuperV_Impl::Graph(const char * aGraphName ) {
464 beginService( "SuperV_Impl::Graph" );
465 bool created = true ;
466 Graph_Impl * myGraph ;
467 myGraph = new Graph_Impl( _Orb , _Poa, _ContId,
468 instanceName() , interfaceName() , aGraphName ) ;
469 int lenname = strlen( aGraphName ) ;
470 if ( lenname > 4 && !strcmp( &aGraphName[ lenname - 4 ] , ".xml" ) ) {
471 created = myGraph->Import( aGraphName ) ;
474 myGraph->Import( (char * ) NULL ) ;
476 SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
478 PortableServer::ObjectId * id = myGraph->getId() ;
479 CORBA::Object_var obj = _poa->id_to_reference(*id);
480 iobject = SUPERV::Graph::_narrow(obj) ;
481 myGraph->SetObjRef( SUPERV::Graph::_duplicate( iobject ) ) ;
483 endService( "SuperV_Impl::Graph" );
484 return SUPERV::Graph::_duplicate(iobject) ;
487 SUPERV::Graph_ptr SuperV_Impl::GraphE(const char * aGraphName ) {
488 beginService( "SuperV_Impl::GraphE" );
489 bool created = true ;
490 Graph_Impl * myGraph ;
491 myGraph = new Graph_Impl( _Orb , _Poa, _ContId,
492 instanceName() , interfaceName() , aGraphName ) ;
493 int lenname = strlen( aGraphName ) ;
494 if ( lenname > 4 && !strcmp( &aGraphName[ lenname - 4 ] , ".xml" ) ) {
495 created = myGraph->Import( aGraphName ) ;
498 myGraph->Import( (char * ) NULL ) ;
500 SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
502 PortableServer::ObjectId * id = myGraph->getId() ;
503 CORBA::Object_var obj = _poa->id_to_reference(*id);
504 iobject = SUPERV::Graph::_narrow(obj) ;
505 myGraph->SetObjRef( SUPERV::Graph::_duplicate( iobject ) ) ;
507 myGraph->ReadOnly() ;
508 endService( "SuperV_Impl::GraphE" );
509 return SUPERV::Graph::_duplicate(iobject) ;
512 SUPERV::Graph_ptr SuperV_Impl::getGraph(const char* ior) {
513 SUPERV::Graph_var iobject = (SUPERV::Graph_var)NULL;
515 CORBA::Object_ptr obj = _Orb->string_to_object(ior);
516 iobject = SUPERV::Graph::_narrow(obj);
518 SUPERV::Graph_var iobject = (SUPERV::Graph_var)NULL;
520 return(SUPERV::Graph::_duplicate(iobject));
523 PortableServer::ObjectId * SUPERVEngine_factory (
525 PortableServer::POA_ptr poa ,
526 PortableServer::ObjectId * contId ,
527 const char *instanceName ,
528 const char *interfaceName
532 MESSAGE("SuperV_factory SuperV ("
533 << instanceName << "," << interfaceName << ")");
534 SuperV_Impl * mySuperV = new SuperV_Impl( orb , poa , contId ,
535 instanceName, interfaceName
538 return mySuperV->getId() ;