]> SALOME platform Git repositories - modules/superv.git/blob - src/Supervision/SuperV_Impl.cxx
Salome HOME
Merge with OCC_development_01
[modules/superv.git] / src / Supervision / SuperV_Impl.cxx
1 //  SUPERV Supervision : contains the implementation of interfaces of SuperVision described in SUPERV.idl
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SuperV_Impl.cxx
25 //  Author : Jean Rahuel
26 //  Module : SUPERV
27 //  $Header$
28
29 using namespace std;
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33
34 #include <fstream>
35 //#include <sstream>
36 #include <string>
37
38 #include "Python.h"
39
40 //#include "utilities.h"
41
42 #include "SALOME_Container_i.hxx"
43
44 #include "SuperV_Impl.hxx"
45
46 #include "SALOMEDS_Attributes.hh"
47 #include <TCollection_AsciiString.hxx>
48 #include <TColStd_SequenceOfAsciiString.hxx>
49 #include "SALOMEDS_Tool.hxx"
50
51 #include "Utils_CorbaException.hxx"
52
53 #define STUDY_SUPERVISION "SUPERV"
54
55 SuperV_Impl::SuperV_Impl( CORBA::ORB_ptr orb ,
56                           PortableServer::POA_ptr poa ,
57                           PortableServer::ObjectId * contId , 
58                           const char *instanceName ,
59                           const char *interfaceName 
60 //                          , int argc ,
61 //                          char ** argv
62                           ) :
63   Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
64   MESSAGE("SuperV_Impl::SuperV_Impl activate object instanceName("
65           << instanceName << ") interfaceName(" << interfaceName << ") --> "
66           << hex << (void *) this << dec )
67   _thisObj = this ;
68   _id = _poa->activate_object(_thisObj);
69   _Orb = CORBA::ORB::_duplicate(orb);
70   _Poa = poa ;
71   _ContId = contId ;
72 }
73
74 SuperV_Impl::SuperV_Impl() {
75 }
76
77 SuperV_Impl::~SuperV_Impl() {
78   beginService( "SuperV_Impl::~SuperV_Impl" );
79   endService( "SuperV_Impl::~SuperV_Impl" );
80 }
81
82 void SuperV_Impl::Destroy() {
83   beginService( "SuperV_Impl::Destroy" );
84   _poa->deactivate_object(*_id) ;
85   CORBA::release(_poa) ;
86   delete(_id) ;
87   _thisObj->_remove_ref();
88   Py_Finalize();
89   endService( "SuperV_Impl::Destroy" );
90 }
91
92 //  void SuperV_Impl::Save(const char *IORSComponent, const char *aUrlOfFile) {
93 SALOMEDS::TMPFile* SuperV_Impl::Save(SALOMEDS::SComponent_ptr theComponent,
94                                      const char* theURL,
95                                      bool isMultiFile) {
96   SALOMEDS::TMPFile_var aStreamFile;
97   // Get a temporary directory to store a file
98   TCollection_AsciiString aTmpDir("");
99   // Create a list to store names of created files
100   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
101   
102   TCollection_AsciiString myStudyName("");
103   if (isMultiFile) myStudyName = (char*)SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()).c_str();
104
105   SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(theComponent);
106   TColStd_SequenceOfAsciiString aFileNames;
107   TCollection_AsciiString aName;
108   
109   for(; anIter->More(); anIter->Next()) {
110     SALOMEDS::SObject_var aSO = anIter->Value();
111     SALOMEDS::GenericAttribute_var anAttr;
112     if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
113       aName = myStudyName + 
114         TCollection_AsciiString("_SUPERVISION_") +
115         TCollection_AsciiString(aSO->Tag()) +
116         TCollection_AsciiString(".xml");
117       aFileNames.Append(aName);
118
119       // save to the file
120       CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()) ;
121       SUPERV::Graph_var graph = SUPERV::Graph::_narrow(anObj);
122       if (aTmpDir.Length() == 0)
123         aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):(char*)SALOMEDS_Tool::GetTmpDir().c_str();
124       graph->Export((aTmpDir + aName).ToCString());
125     }
126   }
127
128   if (aFileNames.Length() == 0) return new SALOMEDS::TMPFile(0);
129
130   aSeq->length(aFileNames.Length());
131   int aCounter;
132   for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
133     aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
134
135   // Conver a file to the byte stream
136   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
137   // Remove the created file and tmp directory
138   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
139   
140   // Add python files to the temporary files sequence
141   SALOMEDS::ListOfFileNames_var aPySeq = new SALOMEDS::ListOfFileNames;
142   aPySeq->length(aSeq->length());
143   for(aCounter = aSeq->length(); aCounter > 0; aCounter--) {
144     char* aCName = CORBA::string_dup(aSeq[aCounter-1]);
145     int a = strlen(aCName);
146     aCName[a-3] = 'p';aCName[a-2] = 'y';aCName[a-1] = 0;
147     aPySeq[aCounter-1] = aCName;
148   }
149   // Remove the created file and tmp directory
150   SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aPySeq.in(), true);
151   // Return the created byte stream
152   return aStreamFile._retn();
153 }
154
155 SALOMEDS::TMPFile* SuperV_Impl::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
156                                           const char* theURL,
157                                           bool isMultiFile) {
158   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
159   return aStreamFile._retn();
160 }
161
162 //  void SuperV_Impl::Load(const char* IORSComponent, const char* aUrlOfFile) {
163 CORBA::Boolean SuperV_Impl::Load(SALOMEDS::SComponent_ptr theComponent,
164                                  const SALOMEDS::TMPFile& theStream,
165                                  const char* theURL,
166                                  bool isMultiFile) {
167   //    MESSAGE("SuperV_Impl::Load : IOR = " << IORSComponent << " , Url = " << aUrlOfFile);
168   // Get a temporary directory for a file
169   if (theStream.length() != 0) {
170     TCollection_AsciiString aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):(char*)SALOMEDS_Tool::GetTmpDir().c_str();
171     myStrURL = strdup(aTmpDir.ToCString());
172     SALOMEDS_Tool::PutStreamToFiles(theStream, myStrURL, isMultiFile);
173   }
174   return true;
175 }
176
177 CORBA::Boolean SuperV_Impl::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
178                                       const SALOMEDS::TMPFile& theStream,
179                                       const char* theURL,
180                                       bool isMultiFile) {
181   return Load(theComponent, theStream, theURL, isMultiFile);
182 }
183
184 void SuperV_Impl::Close(SALOMEDS::SComponent_ptr theComponent) {
185 // mpv 06.03.2003: bug SAL1508 - graphs will be destructed at the GUI-level at the supervision frame closing
186
187 //    SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(theComponent);
188 //    for(; anIter->More(); anIter->Next()) {
189 //      SALOMEDS::SObject_var aSO = anIter->Value();
190 //      SALOMEDS::GenericAttribute_var anAttr;
191 //      if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
192 //        CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()) ;
193 //        SUPERV::Graph_var graph = SUPERV::Graph::_narrow(anObj);
194 //        graph->destroy();
195 //      }
196 //    } 
197 }
198
199 char* SuperV_Impl::ComponentDataType() {
200     return(strdup("SUPERV"));
201 }
202
203 char* SuperV_Impl::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
204                                           const char* IORString,
205                                           CORBA::Boolean isMultiFile,
206                                           CORBA::Boolean isASCII) {
207   MESSAGE("SuperV_Impl::IORToLocalPersistentID: IOR:"<<IORString);
208
209   try {
210     CORBA::Object_var anObj = _orb->string_to_object(IORString) ;
211     if (CORBA::is_nil(anObj)) return CORBA::string_dup(IORString);
212     SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(anObj);
213     if (aGraph->_is_nil()) return CORBA::string_dup(IORString);
214   } catch (...) {
215     return CORBA::string_dup(IORString);
216   }
217
218 //    sprintf(aName, "%s_SUPERVISION_tmp_%d.xml",myStudyName,myCounter);
219 //  CORBA::String_var myStudyName("");
220 //  if (isMultiFile) myStudyName = strdup(SALOMEDS_Tool::GetNameFromPath(theSObject->GetStudy()->URL()));
221   TCollection_AsciiString aName = //TCollection_AsciiString(myStudyName) + 
222     TCollection_AsciiString("_SUPERVISION_") +
223     TCollection_AsciiString(theSObject->Tag()) +
224     TCollection_AsciiString(".xml");
225   return CORBA::string_dup(aName.ToCString());
226 }
227
228 char* SuperV_Impl::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
229                                           const char* aLocalPersistentID,
230                                           CORBA::Boolean isMultiFile,
231                                           CORBA::Boolean isASCII)
232      throw(SALOME::SALOME_Exception)
233 {
234   MESSAGE("SuperV_Impl::LocalPersistentIDToIOR : IOR = " << aLocalPersistentID);
235   CORBA::String_var aFatherID = theSObject->GetFather()->GetID();
236   CORBA::String_var aComponentID = theSObject->GetFatherComponent()->GetID();
237   if (strcmp(aFatherID, aComponentID)) return CORBA::string_dup(aLocalPersistentID);
238
239   string myStudyName("");
240   if (isMultiFile) myStudyName = SALOMEDS_Tool::GetNameFromPath(theSObject->GetStudy()->URL());
241
242   string aStr(myStrURL);
243   aStr += myStudyName + string(aLocalPersistentID);
244   SUPERV::StreamGraph_ptr graph = StreamGraph(aStr.c_str());
245
246   if (graph->_is_nil()) 
247     THROW_SALOME_CORBA_EXCEPTION("Unable to find graph in the file",SALOME::BAD_PARAM);
248
249   // mpv: remove IORs input ports on loading
250   MESSAGE("Remove IORs input ports on loading");
251   SUPERV::ListOfNodes_var aNodes = graph->Nodes();
252   SUPERV::CNode_var aNode;
253   const char* aStrIOR = "";
254   CORBA::Any * anAny = new CORBA::Any();
255   *anAny <<=  aStrIOR ;
256   int i, j;
257   for(i = aNodes->CNodes.length() - 1; i >= 0; i--) {
258     aNode = aNodes->CNodes[i];
259     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
260     for(j = aPorts->length() - 1; j >= 0; j--) {
261       SUPERV::Port_var aPort = aPorts[j];
262       if (aPort->IsIOR()) {
263         aPort->Input(AnyValue(*anAny));
264       }
265     }
266   }
267   for(i = aNodes->FNodes.length() - 1; i >= 0; i--) {
268     aNode = aNodes->FNodes[i];
269     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
270     for(j = aPorts->length() - 1; j >= 0; j--) {
271       SUPERV::Port_var aPort = aPorts[j];
272       if (aPort->IsIOR()) {
273         //CORBA::Any * anAny = new CORBA::Any(CORBA::TypeCode::PR_null_tc(), (void*)NULL, 0) ;
274         aPort->Input(AnyValue(*anAny));
275       }
276     }
277   }
278   for(i = aNodes->INodes.length() - 1; i >= 0; i--) {
279     aNode = aNodes->INodes[i];
280     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
281     for(j = aPorts->length() - 1; j >= 0; j--) {
282       SUPERV::Port_var aPort = aPorts[j];
283       if (aPort->IsIOR()) {
284         aPort->Input(AnyValue(*anAny));
285       }
286     }
287   }
288   for(i = aNodes->GNodes.length() - 1; i >= 0; i--) {
289     aNode = aNodes->GNodes[i];
290     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
291     for(j = aPorts->length() - 1; j >= 0; j--) {
292       SUPERV::Port_var aPort = aPorts[j];
293       if (aPort->IsIOR()) {
294         aPort->Input(AnyValue(*anAny));
295       }
296     }
297   }
298   for(i = aNodes->GNodes.length() - 1; i >= 0; i--) {
299     aNode = aNodes->GNodes[i];
300     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
301     for(j = aPorts->length() - 1; j >= 0; j--) {
302       SUPERV::Port_var aPort = aPorts[j];
303       if (aPort->IsIOR()) {
304         aPort->Input(AnyValue(*anAny));
305       }
306     }
307   }
308   for(i = aNodes->LNodes.length() - 1; i >= 0; i--) {
309     aNode = aNodes->LNodes[i];
310     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
311     for(j = aPorts->length() - 1; j >= 0; j--) {
312       SUPERV::Port_var aPort = aPorts[j];
313       if (aPort->IsIOR()) {
314         aPort->Input(AnyValue(*anAny));
315       }
316     }
317   }
318   for(i = aNodes->ELNodes.length() - 1; i >= 0; i--) {
319     aNode = aNodes->ELNodes[i];
320     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
321     for(j = aPorts->length() - 1; j >= 0; j--) {
322       SUPERV::Port_var aPort = aPorts[j];
323       if (aPort->IsIOR()) {
324         aPort->Input(AnyValue(*anAny));
325       }
326     }
327   }
328   for(i = aNodes->SNodes.length() - 1; i >= 0; i--) {
329     aNode = aNodes->SNodes[i];
330     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
331     for(j = aPorts->length() - 1; j >= 0; j--) {
332       SUPERV::Port_var aPort = aPorts[j];
333       if (aPort->IsIOR()) {
334         aPort->Input(AnyValue(*anAny));
335       }
336     }
337   }
338   for(i = aNodes->ESNodes.length() - 1; i >= 0; i--) {
339     aNode = aNodes->ESNodes[i];
340     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
341     for(j = aPorts->length() - 1; j >= 0; j--) {
342       SUPERV::Port_var aPort = aPorts[j];
343       if (aPort->IsIOR()) {
344         aPort->Input(AnyValue(*anAny));
345       }
346     }
347   }
348
349
350   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
351   aSeq->length(1);
352   aSeq[0]=aLocalPersistentID;
353   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(myStrURL, aSeq.in(), true);
354   
355   return _Orb->object_to_string(graph);
356 }
357
358 bool SuperV_Impl::CanPublishInStudy(CORBA::Object_ptr theIOR) {
359   SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(theIOR);
360   return !(aGraph->_is_nil());
361 }
362
363 SALOMEDS::SObject_ptr SuperV_Impl::PublishInStudy(SALOMEDS::Study_ptr theStudy,
364                                                   SALOMEDS::SObject_ptr theSObject,
365                                                   CORBA::Object_ptr theObject,
366                                                   const char* theName) throw (SALOME::SALOME_Exception) {
367   SALOMEDS::SObject_var aResultSO;
368
369   if(CORBA::is_nil(theObject)) return aResultSO._retn();;
370
371   if(theStudy->_is_nil()) return aResultSO._retn();;
372
373   SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
374   SALOMEDS::GenericAttribute_var anAttr;
375   SALOMEDS::AttributeName_var    aName;
376   SALOMEDS::AttributeIOR_var     anIOR;
377   SALOMEDS::AttributePixMap_var  aPixmap;
378   SALOMEDS::SComponent_var aFather = theStudy->FindComponent(STUDY_SUPERVISION);
379
380   if (aFather->_is_nil()) {
381     aFather = aBuilder->NewComponent(STUDY_SUPERVISION);
382     anAttr = aBuilder->FindOrCreateAttribute(aFather, "AttributeName");
383     aName = SALOMEDS::AttributeName::_narrow(anAttr);
384     //NRI    aName->SetValue(STUDY_SUPERVISION);
385     
386     SALOME_NamingService *NamingService = new SALOME_NamingService(_Orb);
387     CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
388     SALOME_ModuleCatalog::ModuleCatalog_var Catalogue  = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
389     SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( STUDY_SUPERVISION );
390     if ( !Comp->_is_nil() ) {
391       aName->SetValue( Comp->componentusername() );
392     }
393     anAttr = aBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
394     aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
395     aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
396
397     aBuilder->DefineComponentInstance(aFather, SuperG::_this());
398   }
399
400   SALOMEDS::SObject_var       dataflowI;
401   SALOMEDS::ChildIterator_var dataflowS = theStudy->NewChildIterator(aFather);
402
403   SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(theObject);
404   
405   const char* name = aGraph->Name();
406
407   if (CORBA::is_nil(theSObject)) { // if SObject is null, then searching for exist graph SObject
408     for (; dataflowS->More(); dataflowS->Next()) {
409       dataflowI = dataflowS->Value();
410       if (dataflowI->FindAttribute(anAttr, "AttributeName")) {
411         aName = SALOMEDS::AttributeName::_narrow(anAttr);
412         if (strcmp(aName->Value(), name) == 0) {
413           aResultSO = dataflowI;
414           break;
415         }
416       }
417     }
418   } else { // else searching for referenced SObject
419     if (!theSObject->ReferencedObject(aResultSO)) 
420       THROW_SALOME_CORBA_EXCEPTION("Publish in study supervision graph error",SALOME::BAD_PARAM);
421   }
422
423   if (strlen(theName) != 0) name = theName;
424   if (CORBA::is_nil(aResultSO)) aResultSO = aBuilder->NewObject(aFather);
425
426   aName  = SALOMEDS::AttributeName::_narrow(aBuilder->FindOrCreateAttribute(aResultSO, "AttributeName"));
427   aName->SetValue(name);
428   anIOR  = SALOMEDS::AttributeIOR::_narrow(aBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR"));
429   anIOR->SetValue(aGraph->getIOR());
430 //    aBuilder->Addreference(theSObject, aResultSO);
431   return aResultSO._retn();
432 }
433
434 CORBA::Boolean SuperV_Impl::CanCopy(SALOMEDS::SObject_ptr theObject) {
435   // Try to retrieve known by SUPERVISION component Graph by given IOR
436   SALOMEDS::GenericAttribute_var anAttr;
437   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
438   try {
439     CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
440     if (CORBA::is_nil(anObj)) return false;
441     else {
442       SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(anObj);
443       if (aGraph->_is_nil()) return false;
444     }
445   } catch (...) {
446     return false;
447   }
448   return true;
449 }
450
451 SALOMEDS::TMPFile* SuperV_Impl::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
452   // Declare a sequence of the byte to store the copied object
453   SALOMEDS::TMPFile_var aStreamFile;
454
455   // Try to get GEOM_Shape object by given SObject
456   SALOMEDS::GenericAttribute_var anAttr;
457   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return aStreamFile._retn();
458   CORBA::String_var anIOR = strdup(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
459
460   bool isGraph = true;
461   SUPERV::Graph_var aGraph;
462   try {
463     CORBA::Object_var anObj = _orb->string_to_object(anIOR) ;
464     if (CORBA::is_nil(anObj)) isGraph = false;
465     else {
466       aGraph = SUPERV::Graph::_narrow(anObj);
467       if (aGraph->_is_nil()) isGraph = false;
468     }
469   } catch (...) {
470     isGraph = false;
471   }
472
473   if (!isGraph) { // it's just a value, stored in the IOR attribute
474     int aLen = strlen(anIOR);
475     CORBA::Octet* anOctetBuf = (CORBA::Octet*)strdup(anIOR);
476     aStreamFile = SALOMEDS::TMPFile_var(new SALOMEDS::TMPFile(aLen, aLen, anOctetBuf, 1));
477     return aStreamFile._retn();
478   }
479
480   // Get a temporary directory to store a temporary file
481   CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir().c_str();
482   // Create a list to store names of created files
483   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
484   aSeq->length(1);
485   aSeq[0] = strdup("_SUPERVISION_tmp.xml");
486   char* aFullName = new char[strlen(aTmpDir)+strlen(aSeq[0])+1];
487   strcpy(aFullName, aTmpDir);
488   strcpy(aFullName+strlen(aTmpDir), aSeq[0]);
489   aGraph->Export(strdup(aFullName));
490   delete(aFullName);
491
492   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.in(), aSeq.in(), false);
493   SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.in(), aSeq.in(), true);
494
495   // Assign an ID = 1 the the type SUPERV::Graph
496   theObjectID = 1;
497  
498   return aStreamFile._retn();
499 }
500
501 CORBA::Boolean SuperV_Impl::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
502   // The Supervision component can paste only objects copied by Supervision component
503   // and with the object type = 1
504   if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
505   return true;
506 }
507
508 SALOMEDS::SObject_ptr SuperV_Impl::PasteInto(const SALOMEDS::TMPFile& theStream,
509                                              CORBA::Long theObjectID,
510                                              SALOMEDS::SObject_ptr theObject) {
511   SALOMEDS::SObject_var aResultSO;
512   if(CORBA::is_nil(theObject)) return aResultSO._retn();
513   SALOMEDS::StudyBuilder_var aBuilder = theObject->GetStudy()->NewBuilder();
514  
515   aResultSO = SALOMEDS::SObject::_narrow(CORBA::Object::_duplicate(theObject));
516
517   bool isGraph = (strcmp(theObject->GetFatherComponent()->GetID(),theObject->GetID()) == 0);
518
519
520   if (!isGraph) {
521     SALOMEDS::AttributeIOR_var anIOR =
522       SALOMEDS::AttributeIOR::_narrow(aBuilder->FindOrCreateAttribute(theObject, "AttributeIOR"));
523     int aLen = theStream.length();
524     char* aVal = new char[aLen+1];
525     aVal[aLen] = (char)0;
526     memcpy((void*)aVal, (void*)theStream.NP_data(), aLen);
527     anIOR->SetValue(aVal);
528     return aResultSO._retn();
529   }
530
531   TCollection_AsciiString aTmpDir = (char*)SALOMEDS_Tool::GetTmpDir().c_str();
532   SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(), false);
533   TCollection_AsciiString aFullName;
534   aFullName = aTmpDir+aSeq[0];
535   SUPERV::Graph_var aGraph = Graph(aFullName.ToCString());
536
537    SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
538   
539    if (isGraph) {
540      aResultSO = aBuilder->NewObject(theObject);
541    }
542   
543    SALOMEDS::AttributeIOR_var anIOR =
544      SALOMEDS::AttributeIOR::_narrow(aBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR"));
545    anIOR->SetValue(CORBA::string_dup(aGraph->getIOR()));
546
547   return aResultSO._retn();
548 }
549
550 SUPERV::Value_ptr SuperV_Impl::StringValue( const char * aStrIOR ) {
551 //  beginService( "Super_Impl::StringValue" );
552   CORBA::Any * anAny = new CORBA::Any() ;
553
554   try {
555     CORBA::Object_ptr obj = _Orb->string_to_object( aStrIOR ) ;
556     if ( obj ) {
557       *anAny <<= obj ;
558 //      return(anAny);
559     }
560   }
561   catch ( CORBA::BAD_PARAM ) {
562
563 // JR : objref or string only ==> the eventual convertion to long or double is done in
564 //      the SuperVisionEngine with the real type of the argument
565 #if 0
566     long l;
567     double d;
568     if ( strchr( aStrIOR , '.' ) != NULL &&
569          sscanf( aStrIOR , "%lf%*n", &d)) {
570       *anAny <<= d;
571 //        MESSAGE( "Value( " << aStrIOR << " ) --> double " << d );
572 //    return(anAny);
573     }
574     else if (sscanf( aStrIOR , "%ld%*n", &l)) {
575       *anAny <<= l;
576 //        MESSAGE( "Value( " << aStrIOR << " ) --> long " << l );
577 //    return(anAny);
578     }
579     else {
580 #endif
581       *anAny <<=  aStrIOR ;
582 //        MESSAGE( "Value( " << aStrIOR << " ) --> string " << aStrIOR );
583 #if 0
584     }
585 #endif
586   }
587
588 //  endService( "SuperV_Impl::StringValue" );
589   return Value( anAny ) ;
590 }
591
592 SUPERV::Value_ptr SuperV_Impl::AnyValue( const CORBA::Any & anAny ) {
593 //  beginService( "Super_Impl::AnyValue" );
594   CORBA::Any * aAny = new CORBA::Any( anAny ) ;
595 //  endService( "SuperV_Impl::AnyValue" );
596   return Value( aAny ) ;
597 }
598
599 SUPERV::Value_ptr SuperV_Impl::Value( CORBA::Any * anAny ) {
600 //  beginService( "Super_Impl::Value" );
601   Value_Impl * myValue = new Value_Impl( _Orb , _Poa , _ContId ,
602                                          instanceName() , interfaceName() ,
603                                          NULL , NULL , NULL , anAny , true ) ;
604   PortableServer::ObjectId * id = myValue->getId() ;
605   CORBA::Object_var obj = _poa->id_to_reference(*id);
606   SUPERV::Value_var iobject ;
607   iobject = SUPERV::Value::_narrow(obj) ;
608 //  endService( "SuperV_Impl::Value" );
609   return SUPERV::Value::_duplicate( iobject ) ;
610 }
611
612 SUPERV::Graph_ptr SuperV_Impl::Graph(const char * aGraphName ) {
613   beginService( "SuperV_Impl::Graph" );
614   bool created = true ;
615   SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
616   Graph_Impl * myGraph ;
617   myGraph = new Graph_Impl( _Orb , _Poa, _ContId, instanceName() , interfaceName() ,
618                             aGraphName , SUPERV::DataFlowGraph ) ;
619   int lenname = strlen( aGraphName ) ;
620   if ( lenname > 4 && !strcmp( &aGraphName[ lenname - 4 ] , ".xml" ) ) {
621     created = myGraph->Import( aGraphName ) ;
622   }
623   else {
624     myGraph->Import( (char * ) NULL ) ;
625   }
626   if ( !created && myGraph->IsStreamGraph() ) {
627     MESSAGE( "SuperV_Impl::Graph " << aGraphName << " IsStreamGraph !" ) ;
628     myGraph->destroy() ;
629     SUPERV::StreamGraph_var streamiobject = StreamGraph( aGraphName ) ;
630     iobject = SUPERV::Graph::_narrow( streamiobject ) ;
631     if ( CORBA::is_nil( iobject ) ) {
632       created = false ;
633     }
634   }
635   else if ( created ) {
636     PortableServer::ObjectId * id = myGraph->getId() ;
637     CORBA::Object_var obj = _poa->id_to_reference(*id);
638     iobject = SUPERV::Graph::_narrow(obj) ;
639     myGraph->SetObjRef( SUPERV::Graph::_duplicate( iobject ) ) ;
640   }
641   endService( "SuperV_Impl::Graph" );
642   return SUPERV::Graph::_duplicate(iobject) ;
643 }
644
645 SUPERV::Graph_ptr SuperV_Impl::GraphE(const char * aGraphName ) {
646   beginService( "SuperV_Impl::GraphE" );
647   bool created = true ;
648   SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
649   Graph_Impl * myGraph ;
650   myGraph = new Graph_Impl( _Orb , _Poa, _ContId, instanceName() , interfaceName() ,
651                             aGraphName , SUPERV::DataFlowGraph ) ;
652   int lenname = strlen( aGraphName ) ;
653   if ( lenname > 4 && !strcmp( &aGraphName[ lenname - 4 ] , ".xml" ) ) {
654     created = myGraph->Import( aGraphName ) ;
655   }
656   else {
657     myGraph->Import( (char * ) NULL ) ;
658   }
659   if ( !created && myGraph->IsStreamGraph() ) {
660     myGraph->destroy() ;
661     SUPERV::StreamGraph_var streamiobject = StreamGraphE( aGraphName ) ;
662     iobject = SUPERV::Graph::_narrow( streamiobject ) ;
663     if ( CORBA::is_nil( iobject ) ) {
664       created = false ;
665     }
666   }
667   else if ( created ) {
668     PortableServer::ObjectId * id = myGraph->getId() ;
669     CORBA::Object_var obj = _poa->id_to_reference(*id);
670     iobject = SUPERV::Graph::_narrow(obj) ;
671     myGraph->SetObjRef( SUPERV::Graph::_duplicate( iobject ) ) ;
672   }
673   myGraph->ReadOnly() ;
674   endService( "SuperV_Impl::GraphE" );
675   return SUPERV::Graph::_duplicate(iobject) ;
676 }
677
678 SUPERV::StreamGraph_ptr SuperV_Impl::StreamGraph(const char * aGraphName ) {
679   beginService( "SuperV_Impl::StreamGraph" );
680   bool created = true ;
681   StreamGraph_Impl * myStreamGraph ;
682   myStreamGraph = new StreamGraph_Impl( _Orb , _Poa, _ContId, instanceName() ,
683                                         interfaceName() , aGraphName ) ;
684   int lenname = strlen( aGraphName ) ;
685   if ( lenname > 4 && !strcmp( &aGraphName[ lenname - 4 ] , ".xml" ) ) {
686     created = myStreamGraph->Import( aGraphName ) ;
687   }
688   else {
689     myStreamGraph->Import( (char * ) NULL ) ;
690   }
691   SUPERV::StreamGraph_var iobject = SUPERV::StreamGraph::_nil() ;
692   if ( created ) {
693     PortableServer::ObjectId * id = myStreamGraph->getId() ;
694     CORBA::Object_var obj = _poa->id_to_reference(*id);
695     iobject = SUPERV::StreamGraph::_narrow(obj) ;
696     myStreamGraph->SetObjRef( SUPERV::StreamGraph::_duplicate( iobject ) ) ;
697   }
698   endService( "SuperV_Impl::StreamGraph" );
699   return SUPERV::StreamGraph::_duplicate(iobject) ;
700 }
701
702 SUPERV::StreamGraph_ptr SuperV_Impl::StreamGraphE(const char * aGraphName ) {
703   beginService( "SuperV_Impl::StreamGraphE" );
704   bool created = true ;
705   StreamGraph_Impl * myStreamGraph ;
706   myStreamGraph = new StreamGraph_Impl( _Orb , _Poa, _ContId, instanceName() ,
707                                         interfaceName() , aGraphName ) ;
708   int lenname = strlen( aGraphName ) ;
709   if ( lenname > 4 && !strcmp( &aGraphName[ lenname - 4 ] , ".xml" ) ) {
710     created = myStreamGraph->Import( aGraphName ) ;
711   }
712   else {
713     myStreamGraph->Import( (char * ) NULL ) ;
714   }
715   SUPERV::StreamGraph_var iobject = SUPERV::StreamGraph::_nil() ;
716   if ( created ) {
717     PortableServer::ObjectId * id = myStreamGraph->getId() ;
718     CORBA::Object_var obj = _poa->id_to_reference(*id);
719     iobject = SUPERV::StreamGraph::_narrow(obj) ;
720     myStreamGraph->SetObjRef( SUPERV::StreamGraph::_duplicate( iobject ) ) ;
721   }
722   myStreamGraph->ReadOnly() ;
723   endService( "SuperV_Impl::StreamGraphE" );
724   return SUPERV::StreamGraph::_duplicate(iobject) ;
725 }
726
727 SUPERV::Graph_ptr SuperV_Impl::getGraph(const char* ior) {
728     SUPERV::Graph_var iobject = SUPERV::Graph::_nil();
729     try {
730         CORBA::Object_ptr obj = _Orb->string_to_object(ior);
731         iobject = SUPERV::Graph::_narrow(obj);
732     } catch ( ... ) {
733       return SUPERV::Graph::_nil();
734       //         SUPERV::Graph_var iobject = (SUPERV::Graph_var)NULL;
735     };
736     return(SUPERV::Graph::_duplicate(iobject));
737 }
738
739 SUPERV::StreamGraph_ptr SuperV_Impl::getStreamGraph(const char* ior) {
740     SUPERV::StreamGraph_var iobject = SUPERV::StreamGraph::_nil();
741     try {
742         CORBA::Object_ptr obj = _Orb->string_to_object(ior);
743         iobject = SUPERV::StreamGraph::_narrow(obj);
744     } catch ( ... ) {
745       return SUPERV::StreamGraph::_nil();
746       //         SUPERV::Graph_var iobject = (SUPERV::Graph_var)NULL;
747     };
748     return(SUPERV::StreamGraph::_duplicate(iobject));
749 }
750
751 PortableServer::ObjectId * SUPERVEngine_factory (
752                                             CORBA::ORB_ptr orb ,
753                                             PortableServer::POA_ptr poa , 
754                                             PortableServer::ObjectId * contId ,
755                                             const char *instanceName ,
756                                             const char *interfaceName
757 //                                            , int argc ,
758 //                                            char ** argv 
759                                             ) {
760     MESSAGE("SuperV_factory SuperV ("
761             << instanceName << "," << interfaceName << ")");
762     SuperV_Impl * mySuperV = new SuperV_Impl( orb , poa , contId ,
763                                               instanceName, interfaceName
764 //                                              , argc , argv
765                                             );
766     return mySuperV->getId() ;
767 }