Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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 Engines::TMPFile * SuperV_Impl::DumpPython( CORBA::Object_ptr theStudy ,
93                                             CORBA::Boolean isPublished ,
94                                             CORBA::Boolean & isValidScript ) {
95   beginService( "Super_Impl::DumpPython" );
96   bool success = true ;
97   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( theStudy ) ;
98 //  typedef sequence<octet> TMPFile;  
99   Engines::TMPFile_var aStreamPythonvar ;
100   SALOMEDS::SObject_var aStudyObject ;
101   SALOMEDS::SObject_var aStudyObjectValue ;
102   SUPERV::StreamGraph_var aStreamGraph ;
103   char * aGraph ;
104   string aStringGraph ;
105   string aStringRebuildData ;
106   aStringGraph =
107     "### This file is generated by SALOME automatically by dump python funcitonality\n"
108       "### of Supervisor component\n\n";
109   aStringRebuildData = "def RebuildData( theStudy ) :\n" ;
110   if ( CORBA::is_nil( aStudy ) ) {
111     MESSAGE( "aStudy is_nil" ) ;
112     success = false ;
113   }
114   else {
115     SALOMEDS::SObject_var aStudyObject = aStudy->FindComponent( ComponentDataType() ) ;
116     if ( CORBA::is_nil( aStudyObject ) ) {
117       MESSAGE( "aStudyObject is_nil" ) ;
118       success = false ;
119     }
120     else {
121       SALOMEDS::ChildIterator_var aStudyChildIterator = aStudy->NewChildIterator( aStudyObject ) ;
122       bool importSuperV = true ;
123       for ( aStudyChildIterator->InitEx( true ) ;  aStudyChildIterator->More() ;
124             aStudyChildIterator->Next() ) {
125         aStudyObjectValue = aStudyChildIterator->Value() ;
126         int aDepth = aStudyObjectValue->Depth() ;
127         char * aStudyObjectValueIOR = aStudyObjectValue->GetIOR() ;
128         MESSAGE( "Super_Impl::DumpPython aStudyObjectValueIOR " << aStudyObjectValueIOR
129                  << " Depth " << aDepth ) ;
130         if ( strlen( aStudyObjectValueIOR ) ) {
131           CORBA::Object_var obj = CORBA::Object::_nil() ;
132           try {
133             obj = _Orb->string_to_object( aStudyObjectValueIOR ) ;
134             if ( !CORBA::is_nil( obj ) ) {
135               aStreamGraph = SUPERV::StreamGraph::_narrow( obj ) ;
136               if ( !CORBA::is_nil( aStreamGraph ) ) {
137                 MESSAGE( "aStreamGraph " << aStreamGraph->Name() ) ;
138                 aStreamGraph->IsValid() ;
139                 aGraph = aStreamGraph->SavePY( importSuperV ) ;
140                 importSuperV = false ;
141                 aStringGraph += string( aGraph ) ;
142                 aStringRebuildData += "    " ;
143                 aStringRebuildData += aStreamGraph->Name() ;
144                 aStringRebuildData += " = Def" ;
145                 aStringRebuildData += aStreamGraph->Name() ;
146                 aStringRebuildData += "()\n" ;
147                 aStringRebuildData += "    aBuilder,aStudyObject" ;
148                 ostringstream astr ;
149                 astr << aDepth ;
150                 aStringRebuildData += astr.str() ;
151                 aStringRebuildData += " = addDataflowToStudy( theStudy , " ;
152                 aStringRebuildData += aStreamGraph->Name() ;
153                 aStringRebuildData += " )\n" ;
154                 isValidScript = true ;
155               }
156               else {
157                 MESSAGE( "Super_Impl::DumpPython aStreamGraph is_nil ERROR" ) ;
158                 success = false ;
159                 break ;
160               }
161             }
162           }
163           catch ( ... ) {
164             char * aStudyObjectName = aStudyObjectValue->GetName() ;
165             ostringstream newastr ;
166             ostringstream astr ;
167             if ( strlen( aStudyObjectName ) ) {
168               MESSAGE( "Super_Impl::DumpPython aStudyObjectName " << aStudyObjectName ) ;
169               aStringRebuildData += "    aStudyObject" ;
170               astr << aDepth ;
171               aStringRebuildData += astr.str() ;
172               aStringRebuildData += " = aBuilder.NewObject( aStudyObject" ;
173               newastr << (aDepth - 1 ) ;
174               aStringRebuildData += newastr.str() ;
175               aStringRebuildData += " )\n" ;
176               aStringRebuildData += "    anAttr = aBuilder.FindOrCreateAttribute( aStudyObject" ;
177               aStringRebuildData += astr.str() ;
178               aStringRebuildData += " , 'AttributeName' )\n" ;
179               aStringRebuildData += "    aName = anAttr._narrow( SALOMEDS.AttributeName )\n" ;
180               aStringRebuildData += "    aName.SetValue( '" ;
181               aStringRebuildData += aStudyObjectName ;
182               aStringRebuildData += "' )\n" ;
183             }
184             else {
185               MESSAGE( "Super_Impl::DumpPython strlen( aStudyObjectName ) == 0 ERROR" ) ;
186               success = false ;
187               break ;
188             }
189             MESSAGE( "Super_Impl::DumpPython aStudyObjectValueIOR " << aStudyObjectValueIOR ) ;
190 //String in fact
191             aStringRebuildData += "    anAttr = aBuilder.FindOrCreateAttribute( aStudyObject" ;
192             aStringRebuildData += astr.str() ;
193             aStringRebuildData += " , 'AttributeIOR')\n";
194             aStringRebuildData += "    anIORAttr = anAttr._narrow( SALOMEDS.AttributeIOR )\n";
195             aStringRebuildData += "    anIORAttr.SetValue( '" ;
196             aStringRebuildData += aStudyObjectValueIOR ;
197             aStringRebuildData += "' )\n" ;
198           }
199         }
200         else {
201           char * aStudyObjectName = aStudyObjectValue->GetName() ;
202           if ( strlen( aStudyObjectName ) ) {
203             MESSAGE( "Super_Impl::DumpPython aStudyObjectName " << aStudyObjectName << " Depth "
204                      << aDepth ) ;
205             aStringRebuildData += "    aStudyObject" ;
206             ostringstream astr ;
207             astr << aDepth ;
208             aStringRebuildData += astr.str() ;
209             aStringRebuildData += " = aBuilder.NewObject( aStudyObject" ;
210             ostringstream newastr ;
211             newastr << (aDepth - 1 ) ;
212             aStringRebuildData += newastr.str() ;
213             aStringRebuildData += " )\n" ;
214             aStringRebuildData += "    anAttr = aBuilder.FindOrCreateAttribute( aStudyObject" ;
215             aStringRebuildData += astr.str() ;
216             aStringRebuildData += " , 'AttributeName' )\n" ;
217             aStringRebuildData += "    aName = anAttr._narrow( SALOMEDS.AttributeName )\n" ;
218             aStringRebuildData += "    aName.SetValue( '" ;
219             aStringRebuildData += aStudyObjectName ;
220             aStringRebuildData += "' )\n" ;
221           }
222           else {
223             MESSAGE( "Super_Impl::DumpPython strlen( aStudyObjectName ) == 0 ERROR" ) ;
224             success = false ;
225             break ;
226           }
227         }
228       }
229     }
230   }
231   if ( !success ) {
232     isValidScript = false ;
233     endService( "Super_Impl::DumpPython ERROR" );
234     return new Engines::TMPFile( 0 ) ;
235   }
236   aStringRebuildData += "    sg.updateObjBrowser( 1 )\n" ;
237   aStringRebuildData += "    return 1\n" ;
238   aStringGraph += aStringRebuildData ;
239   int aLen = strlen( aStringGraph.c_str() ) ;
240   CORBA::Octet * anOctetBuffer = (CORBA::Octet * ) aStringGraph.c_str() ;
241   aStreamPythonvar = new Engines::TMPFile ( aLen+1 , aLen+1 , anOctetBuffer ,1 ) ;
242   endService( "Super_Impl::DumpPython" );
243   return aStreamPythonvar._retn() ;
244 }
245
246 //  void SuperV_Impl::Save(const char *IORSComponent, const char *aUrlOfFile) {
247 SALOMEDS::TMPFile* SuperV_Impl::Save(SALOMEDS::SComponent_ptr theComponent,
248                                      const char* theURL,
249                                      bool isMultiFile) {
250   SALOMEDS::TMPFile_var aStreamFile;
251   // Get a temporary directory to store a file
252   TCollection_AsciiString aTmpDir("");
253   // Create a list to store names of created files
254   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
255   
256   TCollection_AsciiString myStudyName("");
257   if (isMultiFile) myStudyName = (char*)SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()).c_str();
258
259   SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(theComponent);
260   TColStd_SequenceOfAsciiString aFileNames;
261   TCollection_AsciiString aName;
262   
263   for(; anIter->More(); anIter->Next()) {
264     SALOMEDS::SObject_var aSO = anIter->Value();
265     SALOMEDS::GenericAttribute_var anAttr;
266     if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
267       aName = myStudyName + 
268         TCollection_AsciiString("_SUPERVISION_") +
269         TCollection_AsciiString(aSO->Tag()) +
270         TCollection_AsciiString(".xml");
271       aFileNames.Append(aName);
272
273       // save to the file
274       CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()) ;
275       SUPERV::Graph_var graph = SUPERV::Graph::_narrow(anObj);
276       if (aTmpDir.Length() == 0)
277         aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):(char*)SALOMEDS_Tool::GetTmpDir().c_str();
278       graph->Export((aTmpDir + aName).ToCString());
279     }
280   }
281
282   if (aFileNames.Length() == 0) return new SALOMEDS::TMPFile(0);
283
284   aSeq->length(aFileNames.Length());
285   int aCounter;
286   for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
287     aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
288
289   // Conver a file to the byte stream
290   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
291   // Remove the created file and tmp directory
292   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
293   
294   // Add python files to the temporary files sequence
295   SALOMEDS::ListOfFileNames_var aPySeq = new SALOMEDS::ListOfFileNames;
296   aPySeq->length(aSeq->length());
297   for(aCounter = aSeq->length(); aCounter > 0; aCounter--) {
298     char* aCName = CORBA::string_dup(aSeq[aCounter-1]);
299     int a = strlen(aCName);
300     aCName[a-3] = 'p';aCName[a-2] = 'y';aCName[a-1] = 0;
301     aPySeq[aCounter-1] = aCName;
302   }
303   // Remove the created file and tmp directory
304   SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aPySeq.in(), true);
305   // Return the created byte stream
306   return aStreamFile._retn();
307 }
308
309 SALOMEDS::TMPFile* SuperV_Impl::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
310                                           const char* theURL,
311                                           bool isMultiFile) {
312   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
313   return aStreamFile._retn();
314 }
315
316 //  void SuperV_Impl::Load(const char* IORSComponent, const char* aUrlOfFile) {
317 CORBA::Boolean SuperV_Impl::Load(SALOMEDS::SComponent_ptr theComponent,
318                                  const SALOMEDS::TMPFile& theStream,
319                                  const char* theURL,
320                                  bool isMultiFile) {
321   //    MESSAGE("SuperV_Impl::Load : IOR = " << IORSComponent << " , Url = " << aUrlOfFile);
322   // Get a temporary directory for a file
323   if (theStream.length() != 0) {
324     TCollection_AsciiString aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):(char*)SALOMEDS_Tool::GetTmpDir().c_str();
325     myStrURL = strdup(aTmpDir.ToCString());
326     SALOMEDS_Tool::PutStreamToFiles(theStream, myStrURL, isMultiFile);
327   }
328   return true;
329 }
330
331 CORBA::Boolean SuperV_Impl::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
332                                       const SALOMEDS::TMPFile& theStream,
333                                       const char* theURL,
334                                       bool isMultiFile) {
335   return Load(theComponent, theStream, theURL, isMultiFile);
336 }
337
338 void SuperV_Impl::Close(SALOMEDS::SComponent_ptr theComponent) {
339 // mpv 06.03.2003: bug SAL1508 - graphs will be destructed at the GUI-level at the supervision frame closing
340
341 //    SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(theComponent);
342 //    for(; anIter->More(); anIter->Next()) {
343 //      SALOMEDS::SObject_var aSO = anIter->Value();
344 //      SALOMEDS::GenericAttribute_var anAttr;
345 //      if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
346 //        CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()) ;
347 //        SUPERV::Graph_var graph = SUPERV::Graph::_narrow(anObj);
348 //        graph->destroy();
349 //      }
350 //    } 
351 }
352
353 char* SuperV_Impl::ComponentDataType() {
354     return(strdup("SUPERV"));
355 }
356
357 char* SuperV_Impl::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
358                                           const char* IORString,
359                                           CORBA::Boolean isMultiFile,
360                                           CORBA::Boolean isASCII) {
361   MESSAGE("SuperV_Impl::IORToLocalPersistentID: IOR:"<<IORString);
362
363   try {
364     CORBA::Object_var anObj = _orb->string_to_object(IORString) ;
365     if (CORBA::is_nil(anObj)) return CORBA::string_dup(IORString);
366     SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(anObj);
367     if (aGraph->_is_nil()) return CORBA::string_dup(IORString);
368   } catch (...) {
369     return CORBA::string_dup(IORString);
370   }
371
372 //    sprintf(aName, "%s_SUPERVISION_tmp_%d.xml",myStudyName,myCounter);
373 //  CORBA::String_var myStudyName("");
374 //  if (isMultiFile) myStudyName = strdup(SALOMEDS_Tool::GetNameFromPath(theSObject->GetStudy()->URL()));
375   TCollection_AsciiString aName = //TCollection_AsciiString(myStudyName) + 
376     TCollection_AsciiString("_SUPERVISION_") +
377     TCollection_AsciiString(theSObject->Tag()) +
378     TCollection_AsciiString(".xml");
379   return CORBA::string_dup(aName.ToCString());
380 }
381
382 char* SuperV_Impl::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
383                                           const char* aLocalPersistentID,
384                                           CORBA::Boolean isMultiFile,
385                                           CORBA::Boolean isASCII)
386      throw(SALOME::SALOME_Exception)
387 {
388   MESSAGE("SuperV_Impl::LocalPersistentIDToIOR : IOR = " << aLocalPersistentID);
389   CORBA::String_var aFatherID = theSObject->GetFather()->GetID();
390   CORBA::String_var aComponentID = theSObject->GetFatherComponent()->GetID();
391   if (strcmp(aFatherID, aComponentID)) return CORBA::string_dup(aLocalPersistentID);
392
393   string myStudyName("");
394   if (isMultiFile) myStudyName = SALOMEDS_Tool::GetNameFromPath(theSObject->GetStudy()->URL());
395
396   string aStr(myStrURL);
397   aStr += myStudyName + string(aLocalPersistentID);
398   SUPERV::StreamGraph_ptr graph = StreamGraph(aStr.c_str());
399
400   if (graph->_is_nil()) 
401     THROW_SALOME_CORBA_EXCEPTION("Unable to find graph in the file",SALOME::BAD_PARAM);
402
403   // mpv: remove IORs input ports on loading
404   MESSAGE("Remove IORs input ports on loading");
405   SUPERV::ListOfNodes_var aNodes = graph->Nodes();
406   SUPERV::CNode_var aNode;
407   const char* aStrIOR = "";
408   CORBA::Any * anAny = new CORBA::Any();
409   *anAny <<=  aStrIOR ;
410   int i, j;
411   for(i = aNodes->CNodes.length() - 1; i >= 0; i--) {
412     aNode = aNodes->CNodes[i];
413     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
414     for(j = aPorts->length() - 1; j >= 0; j--) {
415       SUPERV::Port_var aPort = aPorts[j];
416       if (aPort->IsIOR()) {
417         aPort->Input(AnyValue(*anAny));
418       }
419     }
420   }
421   for(i = aNodes->FNodes.length() - 1; i >= 0; i--) {
422     aNode = aNodes->FNodes[i];
423     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
424     for(j = aPorts->length() - 1; j >= 0; j--) {
425       SUPERV::Port_var aPort = aPorts[j];
426       if (aPort->IsIOR()) {
427         //CORBA::Any * anAny = new CORBA::Any(CORBA::TypeCode::PR_null_tc(), (void*)NULL, 0) ;
428         aPort->Input(AnyValue(*anAny));
429       }
430     }
431   }
432   for(i = aNodes->INodes.length() - 1; i >= 0; i--) {
433     aNode = aNodes->INodes[i];
434     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
435     for(j = aPorts->length() - 1; j >= 0; j--) {
436       SUPERV::Port_var aPort = aPorts[j];
437       if (aPort->IsIOR()) {
438         aPort->Input(AnyValue(*anAny));
439       }
440     }
441   }
442   for(i = aNodes->GNodes.length() - 1; i >= 0; i--) {
443     aNode = aNodes->GNodes[i];
444     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
445     for(j = aPorts->length() - 1; j >= 0; j--) {
446       SUPERV::Port_var aPort = aPorts[j];
447       if (aPort->IsIOR()) {
448         aPort->Input(AnyValue(*anAny));
449       }
450     }
451   }
452   for(i = aNodes->GNodes.length() - 1; i >= 0; i--) {
453     aNode = aNodes->GNodes[i];
454     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
455     for(j = aPorts->length() - 1; j >= 0; j--) {
456       SUPERV::Port_var aPort = aPorts[j];
457       if (aPort->IsIOR()) {
458         aPort->Input(AnyValue(*anAny));
459       }
460     }
461   }
462   for(i = aNodes->LNodes.length() - 1; i >= 0; i--) {
463     aNode = aNodes->LNodes[i];
464     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
465     for(j = aPorts->length() - 1; j >= 0; j--) {
466       SUPERV::Port_var aPort = aPorts[j];
467       if (aPort->IsIOR()) {
468         aPort->Input(AnyValue(*anAny));
469       }
470     }
471   }
472   for(i = aNodes->ELNodes.length() - 1; i >= 0; i--) {
473     aNode = aNodes->ELNodes[i];
474     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
475     for(j = aPorts->length() - 1; j >= 0; j--) {
476       SUPERV::Port_var aPort = aPorts[j];
477       if (aPort->IsIOR()) {
478         aPort->Input(AnyValue(*anAny));
479       }
480     }
481   }
482   for(i = aNodes->SNodes.length() - 1; i >= 0; i--) {
483     aNode = aNodes->SNodes[i];
484     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
485     for(j = aPorts->length() - 1; j >= 0; j--) {
486       SUPERV::Port_var aPort = aPorts[j];
487       if (aPort->IsIOR()) {
488         aPort->Input(AnyValue(*anAny));
489       }
490     }
491   }
492   for(i = aNodes->ESNodes.length() - 1; i >= 0; i--) {
493     aNode = aNodes->ESNodes[i];
494     SUPERV::ListOfPorts_var aPorts = aNode->Ports();
495     for(j = aPorts->length() - 1; j >= 0; j--) {
496       SUPERV::Port_var aPort = aPorts[j];
497       if (aPort->IsIOR()) {
498         aPort->Input(AnyValue(*anAny));
499       }
500     }
501   }
502
503
504   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
505   aSeq->length(1);
506   aSeq[0]=aLocalPersistentID;
507   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(myStrURL, aSeq.in(), true);
508   
509   return _Orb->object_to_string(graph);
510 }
511
512 bool SuperV_Impl::CanPublishInStudy(CORBA::Object_ptr theIOR) {
513   SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(theIOR);
514   return !(aGraph->_is_nil());
515 }
516
517 SALOMEDS::SObject_ptr SuperV_Impl::PublishInStudy(SALOMEDS::Study_ptr theStudy,
518                                                   SALOMEDS::SObject_ptr theSObject,
519                                                   CORBA::Object_ptr theObject,
520                                                   const char* theName) throw (SALOME::SALOME_Exception) {
521   SALOMEDS::SObject_var aResultSO;
522
523   if(CORBA::is_nil(theObject)) return aResultSO._retn();;
524
525   if(theStudy->_is_nil()) return aResultSO._retn();;
526
527   SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
528   SALOMEDS::GenericAttribute_var anAttr;
529   SALOMEDS::AttributeName_var    aName;
530   SALOMEDS::AttributeIOR_var     anIOR;
531   SALOMEDS::AttributePixMap_var  aPixmap;
532   SALOMEDS::SComponent_var aFather = theStudy->FindComponent(STUDY_SUPERVISION);
533
534   if (aFather->_is_nil()) {
535     aFather = aBuilder->NewComponent(STUDY_SUPERVISION);
536     anAttr = aBuilder->FindOrCreateAttribute(aFather, "AttributeName");
537     aName = SALOMEDS::AttributeName::_narrow(anAttr);
538     //NRI    aName->SetValue(STUDY_SUPERVISION);
539     
540     SALOME_NamingService *NamingService = new SALOME_NamingService(_Orb);
541     CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
542     SALOME_ModuleCatalog::ModuleCatalog_var Catalogue  = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
543     SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( STUDY_SUPERVISION );
544     if ( !Comp->_is_nil() ) {
545       aName->SetValue( Comp->componentusername() );
546     }
547     anAttr = aBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
548     aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
549     aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
550
551     aBuilder->DefineComponentInstance(aFather, SuperG::_this());
552   }
553
554   SALOMEDS::SObject_var       dataflowI;
555   SALOMEDS::ChildIterator_var dataflowS = theStudy->NewChildIterator(aFather);
556
557   SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(theObject);
558   
559   const char* name = aGraph->Name();
560
561   if (CORBA::is_nil(theSObject)) { // if SObject is null, then searching for exist graph SObject
562     for (; dataflowS->More(); dataflowS->Next()) {
563       dataflowI = dataflowS->Value();
564       if (dataflowI->FindAttribute(anAttr, "AttributeName")) {
565         aName = SALOMEDS::AttributeName::_narrow(anAttr);
566         if (strcmp(aName->Value(), name) == 0) {
567           aResultSO = dataflowI;
568           break;
569         }
570       }
571     }
572   } else { // else searching for referenced SObject
573     if (!theSObject->ReferencedObject(aResultSO)) 
574       THROW_SALOME_CORBA_EXCEPTION("Publish in study supervision graph error",SALOME::BAD_PARAM);
575   }
576
577   if (strlen(theName) != 0) name = theName;
578   if (CORBA::is_nil(aResultSO)) aResultSO = aBuilder->NewObject(aFather);
579
580   aName  = SALOMEDS::AttributeName::_narrow(aBuilder->FindOrCreateAttribute(aResultSO, "AttributeName"));
581   aName->SetValue(name);
582   anIOR  = SALOMEDS::AttributeIOR::_narrow(aBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR"));
583   anIOR->SetValue(aGraph->getIOR());
584 //    aBuilder->Addreference(theSObject, aResultSO);
585   return aResultSO._retn();
586 }
587
588 CORBA::Boolean SuperV_Impl::CanCopy(SALOMEDS::SObject_ptr theObject) {
589   // Try to retrieve known by SUPERVISION component Graph by given IOR
590   SALOMEDS::GenericAttribute_var anAttr;
591   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
592   try {
593     CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
594     if (CORBA::is_nil(anObj)) return false;
595     else {
596       SUPERV::Graph_var aGraph = SUPERV::Graph::_narrow(anObj);
597       if (aGraph->_is_nil()) return false;
598     }
599   } catch (...) {
600     return false;
601   }
602   return true;
603 }
604
605 SALOMEDS::TMPFile* SuperV_Impl::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
606   // Declare a sequence of the byte to store the copied object
607   SALOMEDS::TMPFile_var aStreamFile;
608
609   // Try to get GEOM_Shape object by given SObject
610   SALOMEDS::GenericAttribute_var anAttr;
611   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return aStreamFile._retn();
612   CORBA::String_var anIOR = strdup(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
613
614   bool isGraph = true;
615   SUPERV::Graph_var aGraph;
616   try {
617     CORBA::Object_var anObj = _orb->string_to_object(anIOR) ;
618     if (CORBA::is_nil(anObj)) isGraph = false;
619     else {
620       aGraph = SUPERV::Graph::_narrow(anObj);
621       if (aGraph->_is_nil()) isGraph = false;
622     }
623   } catch (...) {
624     isGraph = false;
625   }
626
627   if (!isGraph) { // it's just a value, stored in the IOR attribute
628     int aLen = strlen(anIOR);
629     CORBA::Octet* anOctetBuf = (CORBA::Octet*)strdup(anIOR);
630     aStreamFile = SALOMEDS::TMPFile_var(new SALOMEDS::TMPFile(aLen, aLen, anOctetBuf, 1));
631     return aStreamFile._retn();
632   }
633
634   // Get a temporary directory to store a temporary file
635   CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir().c_str();
636   // Create a list to store names of created files
637   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
638   aSeq->length(1);
639   aSeq[0] = strdup("_SUPERVISION_tmp.xml");
640   char* aFullName = new char[strlen(aTmpDir)+strlen(aSeq[0])+1];
641   strcpy(aFullName, aTmpDir);
642   strcpy(aFullName+strlen(aTmpDir), aSeq[0]);
643   aGraph->Export(strdup(aFullName));
644   delete(aFullName);
645
646   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.in(), aSeq.in(), false);
647   SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.in(), aSeq.in(), true);
648
649   // Assign an ID = 1 the the type SUPERV::Graph
650   theObjectID = 1;
651  
652   return aStreamFile._retn();
653 }
654
655 CORBA::Boolean SuperV_Impl::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
656   // The Supervision component can paste only objects copied by Supervision component
657   // and with the object type = 1
658   if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
659   return true;
660 }
661
662 SALOMEDS::SObject_ptr SuperV_Impl::PasteInto(const SALOMEDS::TMPFile& theStream,
663                                              CORBA::Long theObjectID,
664                                              SALOMEDS::SObject_ptr theObject) {
665   SALOMEDS::SObject_var aResultSO;
666   if(CORBA::is_nil(theObject)) return aResultSO._retn();
667   SALOMEDS::StudyBuilder_var aBuilder = theObject->GetStudy()->NewBuilder();
668  
669   aResultSO = SALOMEDS::SObject::_narrow(CORBA::Object::_duplicate(theObject));
670
671   bool isGraph = (strcmp(theObject->GetFatherComponent()->GetID(),theObject->GetID()) == 0);
672
673
674   if (!isGraph) {
675     SALOMEDS::AttributeIOR_var anIOR =
676       SALOMEDS::AttributeIOR::_narrow(aBuilder->FindOrCreateAttribute(theObject, "AttributeIOR"));
677     int aLen = theStream.length();
678     char* aVal = new char[aLen+1];
679     aVal[aLen] = (char)0;
680     memcpy((void*)aVal, (void*)theStream.NP_data(), aLen);
681     anIOR->SetValue(aVal);
682     return aResultSO._retn();
683   }
684
685   TCollection_AsciiString aTmpDir = (char*)SALOMEDS_Tool::GetTmpDir().c_str();
686   SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(), false);
687   TCollection_AsciiString aFullName;
688   aFullName = aTmpDir+aSeq[0];
689   SUPERV::Graph_var aGraph = Graph(aFullName.ToCString());
690
691    SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
692   
693    if (isGraph) {
694      aResultSO = aBuilder->NewObject(theObject);
695    }
696   
697    SALOMEDS::AttributeIOR_var anIOR =
698      SALOMEDS::AttributeIOR::_narrow(aBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR"));
699    anIOR->SetValue(CORBA::string_dup(aGraph->getIOR()));
700
701   return aResultSO._retn();
702 }
703
704 SUPERV::Value_ptr SuperV_Impl::StringValue( const char * aStrIOR ) {
705 //  beginService( "Super_Impl::StringValue" );
706   CORBA::Any * anAny = new CORBA::Any() ;
707
708   try {
709     CORBA::Object_ptr obj = _Orb->string_to_object( aStrIOR ) ;
710     if ( obj ) {
711       *anAny <<= obj ;
712 //      return(anAny);
713     }
714   }
715   catch ( ... ) { // mkr : catch any exception to avoid crash after set incorrect
716                   //       value of 'objref' type for input port
717   //catch ( CORBA::BAD_PARAM ) {
718
719 // JR : objref or string only ==> the eventual convertion to long or double is done in
720 //      the SuperVisionEngine with the real type of the argument
721 #if 0
722     long l;
723     double d;
724     if ( strchr( aStrIOR , '.' ) != NULL &&
725          sscanf( aStrIOR , "%lf%*n", &d)) {
726       *anAny <<= d;
727 //        MESSAGE( "Value( " << aStrIOR << " ) --> double " << d );
728 //    return(anAny);
729     }
730     else if (sscanf( aStrIOR , "%ld%*n", &l)) {
731       *anAny <<= l;
732 //        MESSAGE( "Value( " << aStrIOR << " ) --> long " << l );
733 //    return(anAny);
734     }
735     else {
736 #endif
737       *anAny <<=  aStrIOR ;
738 //        MESSAGE( "Value( " << aStrIOR << " ) --> string " << aStrIOR );
739 #if 0
740     }
741 #endif
742   }
743
744 //  endService( "SuperV_Impl::StringValue" );
745   return Value( anAny ) ;
746 }
747
748 SUPERV::Value_ptr SuperV_Impl::AnyValue( const CORBA::Any & anAny ) {
749 //  beginService( "Super_Impl::AnyValue" );
750   CORBA::Any * aAny = new CORBA::Any( anAny ) ;
751 //  endService( "SuperV_Impl::AnyValue" );
752   return Value( aAny ) ;
753 }
754
755 SUPERV::Value_ptr SuperV_Impl::Value( CORBA::Any * anAny ) {
756 //  beginService( "Super_Impl::Value" );
757   Value_Impl * myValue = new Value_Impl( _Orb , _Poa , _ContId ,
758                                          instanceName() , interfaceName() ,
759                                          NULL , NULL , NULL , anAny , true ) ;
760   PortableServer::ObjectId * id = myValue->getId() ;
761   CORBA::Object_var obj = _poa->id_to_reference(*id);
762   SUPERV::Value_var iobject ;
763   iobject = SUPERV::Value::_narrow(obj) ;
764 //  endService( "SuperV_Impl::Value" );
765   return SUPERV::Value::_duplicate( iobject ) ;
766 }
767
768 CORBA::Object_ptr SuperV_Impl::ToObject( CORBA::Object_ptr anObjectRef ) {
769   beginService( "SuperV_Impl::ToObject" );
770   CORBA::Object_ptr anObject ;
771   anObject = CORBA::Object::_duplicate( anObjectRef ) ;
772   endService( "SuperV_Impl::ToObject" );
773   return anObject ;
774 }
775
776 SUPERV::Graph_ptr SuperV_Impl::Graph(const char * aGraphName ) {
777   beginService( "SuperV_Impl::Graph" );
778   bool created = true ;
779   SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
780   Graph_Impl * myGraph ;
781   myGraph = new Graph_Impl( _Orb , _Poa, _ContId, instanceName() , interfaceName() ,
782                             aGraphName , SUPERV::DataFlowGraph ) ;
783   int lenname = strlen( aGraphName ) ;
784   if ( lenname > 4 && !strcmp( &aGraphName[ lenname - 4 ] , ".xml" ) ) {
785     created = myGraph->Import( aGraphName ) ;
786   }
787   else {
788     myGraph->Import( (char * ) NULL ) ;
789   }
790   if ( !created && myGraph->IsStreamGraph() ) {
791     MESSAGE( "SuperV_Impl::Graph " << aGraphName << " IsStreamGraph !" ) ;
792     myGraph->destroy() ;
793     SUPERV::StreamGraph_var streamiobject = StreamGraph( aGraphName ) ;
794     iobject = SUPERV::Graph::_narrow( streamiobject ) ;
795     if ( CORBA::is_nil( iobject ) ) {
796       created = false ;
797     }
798   }
799   else if ( created ) {
800     PortableServer::ObjectId * id = myGraph->getId() ;
801     CORBA::Object_var obj = _poa->id_to_reference(*id);
802     iobject = SUPERV::Graph::_narrow(obj) ;
803     myGraph->SetObjRef( SUPERV::Graph::_duplicate( iobject ) ) ;
804   }
805   endService( "SuperV_Impl::Graph" );
806   return SUPERV::Graph::_duplicate(iobject) ;
807 }
808
809 SUPERV::Graph_ptr SuperV_Impl::GraphE(const char * aGraphName ) {
810   beginService( "SuperV_Impl::GraphE" );
811   bool created = true ;
812   SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
813   Graph_Impl * myGraph ;
814   myGraph = new Graph_Impl( _Orb , _Poa, _ContId, instanceName() , interfaceName() ,
815                             aGraphName , SUPERV::DataFlowGraph ) ;
816   int lenname = strlen( aGraphName ) ;
817   if ( lenname > 4 && !strcmp( &aGraphName[ lenname - 4 ] , ".xml" ) ) {
818     created = myGraph->Import( aGraphName ) ;
819   }
820   else {
821     myGraph->Import( (char * ) NULL ) ;
822   }
823   if ( !created && myGraph->IsStreamGraph() ) {
824     myGraph->destroy() ;
825     SUPERV::StreamGraph_var streamiobject = StreamGraphE( aGraphName ) ;
826     iobject = SUPERV::Graph::_narrow( streamiobject ) ;
827     if ( CORBA::is_nil( iobject ) ) {
828       created = false ;
829     }
830   }
831   else if ( created ) {
832     PortableServer::ObjectId * id = myGraph->getId() ;
833     CORBA::Object_var obj = _poa->id_to_reference(*id);
834     iobject = SUPERV::Graph::_narrow(obj) ;
835     myGraph->SetObjRef( SUPERV::Graph::_duplicate( iobject ) ) ;
836   }
837   myGraph->ReadOnly() ;
838   endService( "SuperV_Impl::GraphE" );
839   return SUPERV::Graph::_duplicate(iobject) ;
840 }
841
842 SUPERV::StreamGraph_ptr SuperV_Impl::StreamGraph(const char * aGraphName ) {
843   beginService( "SuperV_Impl::StreamGraph" );
844   bool created = true ;
845   StreamGraph_Impl * myStreamGraph ;
846   myStreamGraph = new StreamGraph_Impl( _Orb , _Poa, _ContId, instanceName() ,
847                                         interfaceName() , aGraphName ) ;
848   int lenname = strlen( aGraphName ) ;
849   if ( lenname > 4 && !strcmp( &aGraphName[ lenname - 4 ] , ".xml" ) ) {
850     created = myStreamGraph->Import( aGraphName ) ;
851   }
852   else {
853     myStreamGraph->Import( (char * ) NULL ) ;
854   }
855   SUPERV::StreamGraph_var iobject = SUPERV::StreamGraph::_nil() ;
856   if ( created ) {
857     PortableServer::ObjectId * id = myStreamGraph->getId() ;
858     CORBA::Object_var obj = _poa->id_to_reference(*id);
859     iobject = SUPERV::StreamGraph::_narrow(obj) ;
860     myStreamGraph->SetObjRef( SUPERV::StreamGraph::_duplicate( iobject ) ) ;
861   }
862   endService( "SuperV_Impl::StreamGraph" );
863   return SUPERV::StreamGraph::_duplicate(iobject) ;
864 }
865
866 SUPERV::StreamGraph_ptr SuperV_Impl::StreamGraphE(const char * aGraphName ) {
867   beginService( "SuperV_Impl::StreamGraphE" );
868   bool created = true ;
869   StreamGraph_Impl * myStreamGraph ;
870   myStreamGraph = new StreamGraph_Impl( _Orb , _Poa, _ContId, instanceName() ,
871                                         interfaceName() , aGraphName ) ;
872   int lenname = strlen( aGraphName ) ;
873   if ( lenname > 4 && !strcmp( &aGraphName[ lenname - 4 ] , ".xml" ) ) {
874     created = myStreamGraph->Import( aGraphName ) ;
875   }
876   else {
877     myStreamGraph->Import( (char * ) NULL ) ;
878   }
879   SUPERV::StreamGraph_var iobject = SUPERV::StreamGraph::_nil() ;
880   if ( created ) {
881     PortableServer::ObjectId * id = myStreamGraph->getId() ;
882     CORBA::Object_var obj = _poa->id_to_reference(*id);
883     iobject = SUPERV::StreamGraph::_narrow(obj) ;
884     myStreamGraph->SetObjRef( SUPERV::StreamGraph::_duplicate( iobject ) ) ;
885   }
886   myStreamGraph->ReadOnly() ;
887   endService( "SuperV_Impl::StreamGraphE" );
888   return SUPERV::StreamGraph::_duplicate(iobject) ;
889 }
890
891 SUPERV::Graph_ptr SuperV_Impl::getGraph(const char* ior) {
892     SUPERV::Graph_var iobject = SUPERV::Graph::_nil();
893     try {
894         CORBA::Object_ptr obj = _Orb->string_to_object(ior);
895         iobject = SUPERV::Graph::_narrow(obj);
896     } catch ( ... ) {
897       return SUPERV::Graph::_nil();
898       //         SUPERV::Graph_var iobject = (SUPERV::Graph_var)NULL;
899     };
900     return(SUPERV::Graph::_duplicate(iobject));
901 }
902
903 SUPERV::StreamGraph_ptr SuperV_Impl::getStreamGraph(const char* ior) {
904     SUPERV::StreamGraph_var iobject = SUPERV::StreamGraph::_nil();
905     try {
906         CORBA::Object_ptr obj = _Orb->string_to_object(ior);
907         iobject = SUPERV::StreamGraph::_narrow(obj);
908     } catch ( ... ) {
909       return SUPERV::StreamGraph::_nil();
910       //         SUPERV::Graph_var iobject = (SUPERV::Graph_var)NULL;
911     };
912     return(SUPERV::StreamGraph::_duplicate(iobject));
913 }
914
915 PortableServer::ObjectId * SUPERVEngine_factory (
916                                             CORBA::ORB_ptr orb ,
917                                             PortableServer::POA_ptr poa , 
918                                             PortableServer::ObjectId * contId ,
919                                             const char *instanceName ,
920                                             const char *interfaceName
921 //                                            , int argc ,
922 //                                            char ** argv 
923                                             ) {
924     MESSAGE("SuperV_factory SuperV ("
925             << instanceName << "," << interfaceName << ")");
926     SuperV_Impl * mySuperV = new SuperV_Impl( orb , poa , contId ,
927                                               instanceName, interfaceName
928 //                                              , argc , argv
929                                             );
930     return mySuperV->getId() ;
931 }