]> SALOME platform Git repositories - modules/hexablock.git/blob - src/HEXABLOCK_I/HEXABLOCK.cxx
Salome HOME
Merge from V6_main 01/04/2013
[modules/hexablock.git] / src / HEXABLOCK_I / HEXABLOCK.cxx
1 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 using namespace std;
20
21
22 static bool db = false;
23
24 // #include <Basics_Utils.hxx>
25 // 
26 // #include <TDF_Tool.hxx>
27 // #include <TDF_Data.hxx>
28 // #include <TDF_Reference.hxx>
29 // #include <TDF_LabelSequence.hxx>
30 // #include <TDataStd_Integer.hxx>
31 // #include <TDataStd_ChildNodeIterator.hxx>
32 // #include <TFunction_Driver.hxx>
33 // #include <TFunction_DriverTable.hxx>
34 // #include <TDataStd_HArray1OfByte.hxx>
35 // #include <TDataStd_ByteArray.hxx>
36 // #include <TDataStd_UAttribute.hxx>
37 // #include <TDF_ChildIterator.hxx>
38 // #include <TDataStd_Comment.hxx>
39 // #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
40
41 ////////////
42
43 #include <TCollection_AsciiString.hxx>
44 #include <TopAbs_ShapeEnum.hxx>
45 #include <TopoDS.hxx>
46 #include <TopoDS_Vertex.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Face.hxx>
49
50
51 #include <Geom_Curve.hxx>
52
53 #include <gp_Pnt.hxx>
54 #include <BRep_Builder.hxx>
55 #include <BRep_Tool.hxx>
56 #include <BRepTools.hxx>
57
58 #include "SALOME_NamingService.hxx"
59 #include "SALOME_LifeCycleCORBA.hxx"
60 #include "Utils_SINGLETON.hxx"
61 #include "Utils_ExceptHandlers.hxx"
62 #include "HexElements_grid.cxx"
63
64
65 #include "HEXABLOCK.hxx"
66 #include "HEXABLOCK_version.h"
67 #include "utilities.h"
68
69 #include "HexDocument_impl.hxx"
70 #include <string>
71
72 #include "SALOMEDS_Tool.hxx"
73 // #include "SALOMEDSImpl_TMPFile.hxx"
74
75 using namespace HEXABLOCK_ORB;
76
77
78 // Static variables definition
79 PortableServer::POA_var HEXABLOCK_Gen_i::_poa;
80 HEXABLOCK_Gen_i*        HEXABLOCK_Gen_i::_HEXABLOCKGen = NULL;
81 SALOME_LifeCycleCORBA*  HEXABLOCK_Gen_i::_lcc = NULL;
82 GEOM::GEOM_Gen_var      HEXABLOCK_Gen_i::_geomGen = GEOM::GEOM_Gen::_nil();
83 // GEOM_Client*            HEXABLOCK_Gen_i::_geomClient = NULL;
84 CORBA::ORB_var          HEXABLOCK_Gen_i::_orb;
85 SALOME_NamingService*   HEXABLOCK_Gen_i::_ns = NULL;
86
87 //=============================================================================
88 /*!
89  *  standard constructor
90  */
91 //=============================================================================
92
93 HEXABLOCK_Gen_i::HEXABLOCK_Gen_i(CORBA::ORB_ptr orb,
94         PortableServer::POA_ptr poa,
95         PortableServer::ObjectId * contId, 
96         const char *instanceName, 
97         const char *interfaceName) :
98   Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
99 {
100   _thisObj = this ;
101   _poa = PortableServer::POA::_duplicate(poa);
102   _orb = CORBA::ORB::_duplicate(orb);
103   _id = _poa->activate_object(_thisObj);
104 //_engine_cpp = new HEXA_NS::Hex;
105   _engine_cpp = HEXA_NS::Hex::getInstance ();
106   _geomClient = NULL;
107   _HEXABLOCKGen = this;
108 }
109
110 //=============================================================================
111 /*!
112  *  GetServant [ static ]
113  *
114  *  Get servant of the CORBA object
115  */
116 //=============================================================================
117
118 PortableServer::ServantBase_var HEXABLOCK_Gen_i::GetServant( CORBA::Object_ptr theObject )
119 {
120   if( CORBA::is_nil( theObject ) || CORBA::is_nil( GetPOA() ) )
121     return NULL;
122   try {
123     PortableServer::Servant aServant = GetPOA()->reference_to_servant( theObject );
124     return aServant;
125   } 
126   catch (...) {
127     INFOS( "GetServant - Unknown exception was caught!!!" ); 
128     return NULL;
129   }
130 }
131
132
133
134 //=============================================================================
135 //  destructor
136 //=============================================================================
137 HEXABLOCK_Gen_i::~HEXABLOCK_Gen_i()
138 {
139 }
140
141 //=============================================================================
142 /*!
143  *  DumpPython
144  */
145 //=============================================================================
146 Engines::TMPFile* HEXABLOCK_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
147                                               CORBA::Boolean /*isPublished*/,
148                                               CORBA::Boolean /*isMultiFile*/,
149                                               CORBA::Boolean& isValidScript)
150 {
151   std::cout << "DumpPython DumpPython DumpPython DumpPython" << std::endl;
152   isValidScript = true;  // to avoid dump python operation failing
153   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(0);
154   return aStreamFile._retn(); 
155 }
156 //   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
157 //   if(CORBA::is_nil(aStudy))
158 //     return new Engines::TMPFile(0);   
159 // 
160 //   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
161 //   if(CORBA::is_nil(aSO))
162 //     return new Engines::TMPFile(0);  
163 // 
164 //   TObjectData objData;
165 //   std::vector<TObjectData> objectDataVec;
166 // 
167 //   TVariablesList aVariableMap;
168 // 
169 //   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
170 //   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
171 //     SALOMEDS::SObject_var aValue = Itr->Value();
172 //     CORBA::String_var IOR = aValue->GetIOR();
173 //     if(strlen(IOR.in()) > 0) {
174 //       CORBA::Object_var obj = _orb->string_to_object(IOR);
175 //       GEOM::GEOM_Object_var GO = GEOM::GEOM_Object::_narrow(obj);
176 //       if(!CORBA::is_nil(GO)) {
177 //         CORBA::String_var aName       = aValue->GetName();
178 //         CORBA::String_var anEntry     = GO->GetEntry();
179 //         CORBA::String_var aStudyEntry = aValue->GetID();
180 //         objData._name       = aName.in();
181 //         objData._entry      = anEntry.in();
182 //         objData._studyEntry = aStudyEntry.in();
183 //      objectDataVec.push_back( objData );
184 // 
185 //      //Find attribute with list of used notebook variables
186 //      SALOMEDS::GenericAttribute_var anAttr;
187 //      SALOMEDS::AttributeString_var anAttrStr;
188 //      if(aValue->FindAttribute(anAttr,"AttributeString")){
189 //        anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
190 //        SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
191 //        ObjectStates* aStates = new ObjectStates();
192 //        for(int i = 0; i < aSections->length(); i++) {
193 //          TState aState;
194 //          SALOMEDS::ListOfStrings aListOfVars = aSections[i];
195 //          for(int j = 0; j < aListOfVars.length(); j++) {
196 //            bool isVar = aStudy->IsVariable(aListOfVars[j].in());
197 //            TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
198 //            aState.push_back(aVar);
199 //          }
200 //          aStates->AddState(aState);
201 //        }
202 //        aVariableMap.insert(std::make_pair(TCollection_AsciiString(anEntry),aStates));
203 //      }
204 //       }
205 //     }
206 //   }
207 //   
208 //   TCollection_AsciiString aScript;
209 //   aScript += _impl->DumpPython(aStudy->StudyId(), objectDataVec, aVariableMap, isPublished, isValidScript);
210 // 
211 //   if (isPublished)
212 //   {
213 //     //Output the script that sets up the visual parameters.
214 //     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
215 //     if (script && strlen(script) > 0) {
216 //       aScript += "\n\t### Store presentation parameters of displayed objects\n";
217 //       aScript += script;
218 //       CORBA::string_free(script);
219 //     }
220 //   }
221 // 
222 //   aScript += "\n\tpass\n";
223 // 
224 //   int aLen = aScript.Length(); 
225 //   unsigned char* aBuffer = new unsigned char[aLen+1];
226 //   strcpy((char*)aBuffer, aScript.ToCString());
227 // 
228 //   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
229 //   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
230 // 
231 //   return aStreamFile._retn(); 
232 // }
233
234
235
236
237 void HEXABLOCK_Gen_i::test()
238 {
239 }
240
241 CORBA::Long HEXABLOCK_Gen_i::countDocument()
242 {
243     CORBA::Long nbDocument = _engine_cpp->countDocument();
244 //     CORBA::Long nbDocument = 333;
245     return nbDocument;
246 }
247
248 Document_ptr HEXABLOCK_Gen_i::getDocument(CORBA::Long i)
249 {
250     HEXA_NS::Document *doc=_engine_cpp->getDocument(i);
251     if ( doc == NULL ) 
252         return Document::_nil();
253
254     Document_impl *servantCorba = new Document_impl( GetPOA(), doc );
255     return servantCorba->_this();
256 }
257
258 void HEXABLOCK_Gen_i::removeDocument(HEXABLOCK_ORB::Document_ptr docIn)
259 {
260   Document_impl* docServant = ::DownCast<Document_impl*>( docIn );
261   ASSERT( docServant );
262   if ( docServant ) {
263     HEXA_NS::Document* doc = docServant->GetImpl();
264     _engine_cpp->removeDocument( doc );
265   }
266 }
267
268 Document_ptr HEXABLOCK_Gen_i::findDocument(const char* name)
269 {
270    HEXA_NS::Document *doc=_engine_cpp->findDocument (name);
271
272    if (doc==NULL)
273       return Document::_nil();
274
275    Document_impl *servantCorba=new Document_impl( GetPOA(), doc);
276    return servantCorba->_this();
277 }
278
279 Document_ptr HEXABLOCK_Gen_i::addDocument(const char* name)
280 {
281    // HEXA_NS::Document *doc=_engine_cpp->addDocument (name);
282    // Document_impl *servantCorba=new Document_impl( GetPOA(), doc);
283    // return servantCorba->_this();
284
285    return createDocInStudy(name);
286 }
287
288
289
290 Document_ptr HEXABLOCK_Gen_i::loadDocument(const char* xmlFilename)
291 {
292     HEXA_NS::Document *doc=_engine_cpp->loadDocument(xmlFilename);
293     Document_impl *servantCorba=new Document_impl( GetPOA(), doc );
294     return servantCorba->_this();
295 }
296
297 GEOM_Client* HEXABLOCK_Gen_i::getGeomClient()
298 {
299   // create shape reader if necessary
300   if ( _geomClient == NULL ) 
301     _geomClient = new GEOM_Client(GetContainerRef());
302   ASSERT( _geomClient);
303   return _geomClient;
304 }
305
306
307 TopoDS_Shape HEXABLOCK_Gen_i::geomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
308 {
309   TopoDS_Shape S;
310   if ( !theGeomObject->_is_nil() ){
311     _geomClient = getGeomClient();
312
313     if(CORBA::is_nil(_geomGen)) {
314       Engines::EngineComponent_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
315       _geomGen=GEOM::GEOM_Gen::_narrow(temp);
316     }
317     if ( _geomClient != NULL  && !_geomGen->_is_nil () )
318       S = _geomClient->GetShape( _geomGen, theGeomObject );
319   }
320   return S;
321 }
322
323
324
325
326
327 // HEXABLOCK_ORB::GEOM_Object_ptr HEXABLOCK_Gen_i::shapeToGeomObject (const TopoDS_Shape& theShape )
328 // {
329 //   HEXABLOCK_ORB::GEOM_Object_var aShapeObj;
330 //   if ( !theShape.IsNull() ) {
331 //     _geomClient = getGeomClient();//new GEOM_Client(GetContainerRef());
332 //     TCollection_AsciiString IOR;
333 //     bool found = _geomClient->Find(theShape, IOR);
334 //     if (found){
335 //       CORBA::Object_var obj = _orb->string_to_object( IOR.ToCString() );
336 //       aShapeObj = HEXABLOCK_ORB::GEOM_Object::_narrow ( obj );
337 //     }
338 //   }
339 //   return aShapeObj._retn();
340 // }
341
342
343
344 // GEOM::GEOM_Object_ptr HEXABLOCK_Gen_i::_makeFace( const TopoDS_Shape& theShape )
345 // {
346 //   ASSERT ( theShape.ShapeType() == TopAbs_FACE );
347 // 
348 //   int theStudyID = 0;
349 //   GEOM::GEOM_Object_var result; 
350 //   
351 // // PrimOp   = self.GetI3DPrimOperations 
352 // // GEOM_IShapesOperations
353 //   return result._retn();
354 // }
355
356 // GEOM::GEOM_Object_ptr HEXABLOCK_Gen_i::_makeEdge( const TopoDS_Shape& theShape )
357 // {
358 //   ASSERT ( theShape.ShapeType() == TopAbs_EDGE );
359 //   int theStudyID = 0;
360 //   GEOM::GEOM_Object_var result;
361 //   GEOM::GEOM_Object_var p1;
362 //   GEOM::GEOM_Object_var p2;
363 // 
364 //   if ( CORBA::is_nil(_geomGen) ) { //_geomGen->_is_nil() 
365 //     Engines::EngineComponent_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
366 //     _geomGen=GEOM::GEOM_Gen::_narrow(temp);
367 //   }
368 //   GEOM::GEOM_IBasicOperations_var     aBasicOp  = _geomGen->GetIBasicOperations(theStudyID);
369 //   GEOM::GEOM_IShapesOperations_var    aShapesOp = _geomGen->GetIShapesOperations(theStudyID);
370 // 
371 //   TopoDS_Edge anEdge = TopoDS::Edge( theShape );
372 //   double f, l;
373 //   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, f, l);
374 // 
375 //   gp_Pnt aCurveStart = aCurve->Value( f );
376 //   gp_Pnt aCurveEnd   = aCurve->Value( l );
377 // 
378 //   double x1, y1, z1;
379 //   double x2, y2, z2;
380 // 
381 //   x1 = aCurveStart.X(); y1 = aCurveStart.Y(); z1 = aCurveStart.Z();
382 //   x2 = aCurveEnd.X();   y2 = aCurveEnd.Y(); z2 = aCurveEnd.Z();
383 //   p1= aBasicOp->MakePointXYZ(x1, y1, z1);
384 //   p2= aBasicOp->MakePointXYZ(x2, y2, z2);
385 //   result = aShapesOp->MakeEdge(p1, p2);
386 // 
387 //   return result._retn();
388 // }
389
390 // GEOM::GEOM_Object_ptr HEXABLOCK_Gen_i::_makeVertex( const TopoDS_Shape& theShape )
391 // {
392 //   ASSERT ( theShape.ShapeType() == TopAbs_VERTEX );
393 // 
394 //   int theStudyID = 0;
395 //   GEOM::GEOM_Object_var result;
396 // 
397 //   TopoDS_Vertex aPoint;
398 //   aPoint      = TopoDS::Vertex( theShape );
399 //   gp_Pnt aPnt = BRep_Tool::Pnt( aPoint );
400 //   double x = aPnt.X();
401 //   double y = aPnt.Y();
402 //   double z = aPnt.Z();
403 // 
404 //   GEOM::GEOM_IBasicOperations_var aBasicOp = _geomGen->GetIBasicOperations(theStudyID);
405 //   result = aBasicOp->MakePointXYZ(x, y, z);
406 // 
407 //   return result._retn();
408 // }
409
410
411 // GEOM::GEOM_Object_ptr HEXABLOCK_Gen_i::shapeToGeomObject (const TopoDS_Shape& theShape )
412 // {
413 //   std::cout << "XXXXXXXX shapeToGeomObject "<< std::endl;
414 //   GEOM::GEOM_Object_var aShapeObj;
415 //   if ( !theShape.IsNull() ) {
416 //     switch ( theShape.ShapeType() ){
417 //       case TopAbs_VERTEX: aShapeObj = _makeVertex( theShape ); break;
418 // //       case TopAbs_FACE:   aShapeObj = _makeFace( theShape ); break; //CS_TODO
419 //       case TopAbs_EDGE:   aShapeObj = _makeEdge( theShape ); break;
420 //       default: throw (SALOME_Exception(LOCALIZED("shapeToGeomObject : shape type not supported")));
421 //     }
422 //   }
423 //   return aShapeObj._retn();
424 // }
425
426
427
428
429 GEOM::GEOM_Object_ptr HEXABLOCK_Gen_i::brepToGeomObject(const std::string& theBrep)
430 {
431   int theStudyID = 0; //CS_TODO
432   GEOM::GEOM_Object_var aShapeObj;
433
434   if ( CORBA::is_nil(_geomGen) ){
435     Engines::EngineComponent_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
436     _geomGen=GEOM::GEOM_Gen::_narrow(temp);
437   }
438   GEOM::GEOM_IInsertOperations_var aInsOp = _geomGen->GetIInsertOperations(theStudyID);
439
440   // char *tmpname = strdup("HexaAssocXXXXXX");   // killed by Abu 
441   char tmpname [32];                              // Abu 11/07
442   sprintf (tmpname, "/tmp/tmpHexaAsso%dXXXXXX", getpid()); // Abu 11/07
443   // strcpy (tmpname, "HexaAssocXXXXXX");   // killed by Abu 
444
445   int fd = mkstemp( tmpname );
446   if ( fd != -1 )
447      {
448      ofstream tmpfile( tmpname );
449      tmpfile << theBrep;
450      tmpfile.close();
451      aShapeObj = aInsOp->ImportFile( tmpname, "BREP" );
452      close(fd);
453      unlink(tmpname);
454      }
455
456   return aShapeObj._retn();
457 }
458
459
460
461
462
463
464 // GEOM::GEOM_Object_ptr HEXABLOCK_Gen_i::shapeToGeomObject (const TopoDS_Shape& theShape )
465 // {
466 //   GEOM::GEOM_Object_var aShapeObj;
467 //   std::cout<<"shapeToGeomObject "<<std::endl;
468 //   if ( !theShape.IsNull() ) {
469 //     std::cout<<"shapeToGeomObject !theShape.IsNull()"<<std::endl;
470 //     _geomClient = getGeomClient();//new GEOM_Client(GetContainerRef());
471 //     TCollection_AsciiString IOR;
472 //     bool found = _geomClient->Find(theShape, IOR);
473 //     std::cout<<"shapeToGeomObject found"<<found<<std::endl;
474 //     if (found){
475 //       CORBA::Object_var obj = _orb->string_to_object( IOR.ToCString() );
476 //       aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
477 //     }
478 //   }
479 //   return aShapeObj._retn();
480 // }
481
482
483 StudyContext* HEXABLOCK_Gen_i::GetCurrentStudyContext()
484 {
485   if ( !CORBA::is_nil( myCurrentStudy ) &&
486       myStudyContextMap.find( GetCurrentStudyID() ) != myStudyContextMap.end() )
487     return myStudyContextMap[ myCurrentStudy->StudyId() ];
488   else
489     return 0;
490 }
491
492 int HEXABLOCK_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
493 {
494   StudyContext* myStudyContext = GetCurrentStudyContext();
495   if ( myStudyContext && !CORBA::is_nil( theObject )) {
496     CORBA::String_var iorString = GetORB()->object_to_string( theObject );
497     return myStudyContext->addObject( string( iorString.in() ) );
498   }
499   return 0;
500 }
501
502
503 char* HEXABLOCK_Gen_i::ComponentDataType()
504 {
505   return CORBA::string_dup( "HEXABLOCK" );
506 }
507
508
509 Document_ptr HEXABLOCK_Gen_i::createDoc (const char* name)
510                               throw ( SALOME::SALOME_Exception )
511 {
512   Unexpect aCatch(SALOME_SalomeException);
513
514   // Get or create the GEOM_Client instance
515   try {
516       HEXA_NS::Document *doc  = NULL;
517       doc = _engine_cpp->addDocument (name);
518       Document_impl *docImpl = new Document_impl( GetPOA(), doc );
519
520     // activate the CORBA servant of Mesh
521     Document_var docServant = Document::_narrow( docImpl->_this() );
522     /* int nextId = */    RegisterObject( docServant );
523
524     return docServant._retn();
525   }
526   catch (SALOME_Exception& S_ex) {
527   }
528   return Document::_nil();
529 }
530
531 Document_ptr HEXABLOCK_Gen_i::createDocInStudy (const char* name)
532                               throw ( SALOME::SALOME_Exception )
533 {
534   Unexpect aCatch(SALOME_SalomeException);
535   // create mesh
536   Document_var doc = this->createDoc (name);
537
538   // publish mesh in the study
539   if ( CanPublishInStudy( doc ) ) {
540     SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
541     aStudyBuilder->NewCommand();  // There is a transaction
542     SALOMEDS::SObject_var aSO = PublishDoc( myCurrentStudy, doc.in() );
543     aStudyBuilder->CommitCommand();
544   }
545
546   return doc._retn();
547 }
548
549
550
551
552 SALOME_LifeCycleCORBA*  HEXABLOCK_Gen_i::GetLCC() {
553   if ( _lcc == NULL ) {
554     _lcc = new SALOME_LifeCycleCORBA( GetNS() );
555   }
556   return _lcc;
557 }
558
559 SALOME_NamingService* HEXABLOCK_Gen_i::GetNS()
560 {
561   if ( _ns == NULL ) {
562     _ns = SINGLETON_<SALOME_NamingService>::Instance();
563     ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
564     _ns->init_orb( _orb );
565   }
566   return _ns;
567 }
568
569
570
571 int HEXABLOCK_Gen_i::GetCurrentStudyID()
572 {
573   return myCurrentStudy->_is_nil() || myCurrentStudy->_non_existent() ? -1 : myCurrentStudy->StudyId();
574 }
575
576
577 void HEXABLOCK_Gen_i::SetCurrentStudy( SALOMEDS::Study_ptr theStudy )
578 {
579   /*int curStudyId = */     GetCurrentStudyID();
580   myCurrentStudy = SALOMEDS::Study::_duplicate( theStudy );
581   // create study context, if it doesn't exist and set current study
582   int studyId = GetCurrentStudyID();
583   if ( myStudyContextMap.find( studyId ) == myStudyContextMap.end() ) {
584     myStudyContextMap[ studyId ] = new StudyContext;
585   }
586
587 //   // myCurrentStudy may be nil
588 //   if ( !CORBA::is_nil( myCurrentStudy ) ) {
589 //     SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
590 //     if( !myCurrentStudy->FindComponent( "GEOM" )->_is_nil() )
591 //       aStudyBuilder->LoadWith( myCurrentStudy->FindComponent( "GEOM" ), GetGeomEngine() );
592 // 
593 //     // NPAL16168, issue 0020210
594 //     // Let meshes update their data depending on GEOM groups that could change
595 //     if ( curStudyId != studyId )
596 //     {
597 //       //SALOMEDS::SComponent_var me =  PublishComponent( myCurrentStudy );
598 //       SALOMEDS::SComponent_var me = SALOMEDS::SComponent::_narrow
599 //         ( myCurrentStudy->FindComponent( ComponentDataType() ) );
600 //       if ( !me->_is_nil() ) {
601 //         SALOMEDS::ChildIterator_var anIter = myCurrentStudy->NewChildIterator( me );
602 //         for ( ; anIter->More(); anIter->Next() ) {
603 //           SALOMEDS::SObject_var so = anIter->Value();
604 //           CORBA::Object_var    ior = SObjectToObject( so );
605 //           if ( SMESH_Mesh_i*  mesh = SMESH::DownCast<SMESH_Mesh_i*>( ior ))
606 //             mesh->CheckGeomGroupModif();
607 //         }
608 //       }
609 //     }
610 //   }
611 }
612
613
614 TopoDS_Shape string2shape( const string& brep )
615 {
616   TopoDS_Shape shape;
617
618   istringstream streamBrep(brep);
619
620   BRep_Builder aBuilder;
621   BRepTools::Read(shape, streamBrep, aBuilder);
622   return shape;
623 }
624
625 // TopoDS_Shape STRING_TO_SHAPE( char* buf, int sizebuf )
626 // {
627 //   TopoDS_Shape shape;
628 //   std::istrstream streamBrep(buf,sizebuf);
629 //   BRep_Builder aBuilder;
630 //   BRepTools::Read(shape, streamBrep, aBuilder);
631 //   return(shape);
632 // }
633
634 // ===================================================== shape2string
635 string shape2string( const TopoDS_Shape& aShape )
636 {
637   ostringstream streamShape;
638   BRepTools::Write(aShape, streamShape);
639
640   return streamShape.str();
641 }
642 // ===================================================== HEXABLOCKEngine_factory
643 extern "C"
644 {
645   PortableServer::ObjectId * HEXABLOCKEngine_factory(
646                                CORBA::ORB_ptr orb,
647                                PortableServer::POA_ptr poa, 
648                                PortableServer::ObjectId * contId,
649                                const char *instanceName, 
650                                const char *interfaceName)
651   {
652     SCRUTE(interfaceName);
653     HEXABLOCK_Gen_i *myHEXABLOCK 
654       = new HEXABLOCK_Gen_i(orb, poa, contId, instanceName, interfaceName);
655     return myHEXABLOCK->getId() ;
656   }
657 }
658 // ------------------------------------------------------------------------
659 // 
660 //                 Heritage de  SALOMEDSImpl_Driver
661 //
662 // ------------------------------------------------------------------------
663 //
664 /* ***********************************************************************
665 // ========================================================= GetIOR
666 std::string HEXABLOCK_Gen_i::GetIOR()
667 {
668    return "";
669 }
670    *********************************************************************** */
671 // =========================================================== SaveASCII
672 SALOMEDS::TMPFile* HEXABLOCK_Gen_i::SaveASCII (
673                                           const SALOMEDS::SComponent_ptr compo,
674                                           const char* theURL,
675                                           bool isMultiFile)
676 {
677    SALOMEDS::TMPFile* fic =  Save (compo, theURL, isMultiFile);
678    return fic;
679 }
680 // =========================================================== LoadASCII
681 CORBA::Boolean HEXABLOCK_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr compo,
682                            const SALOMEDS::TMPFile& theStream,
683                            const char* theURL,
684                            bool isMultiFile)
685 {
686    bool rep = Load (compo, theStream, theURL, isMultiFile);
687    return rep;
688 }
689 // =========================================================== Close
690 void HEXABLOCK_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
691 {
692    if (db) cout << " +++++++ Je suis passe par  HEXABLOCK_Gen_i::"
693         << "Close" << endl;
694 }
695 // =================================================== IORToLocalPersistentID
696 char* HEXABLOCK_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
697                                const char* IORString,
698                                CORBA::Boolean isMultiFile,
699                                CORBA::Boolean isASCII)
700 {
701    if (db) cout << " +++++++ Je suis passe par  HEXABLOCK_Gen_i::"
702         << "IORToLocalPersistentID" << endl;
703    const char* empty = "";
704    return CORBA::string_dup (empty);
705 }
706 // =================================================== LocalPersistentIDToIOR
707 char* HEXABLOCK_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
708                                const char* aLocalPersistentID,
709                                CORBA::Boolean isMultiFile,
710                                CORBA::Boolean isASCII)
711 {
712    if (db) cout << " +++++++ Je suis passe par  HEXABLOCK_Gen_i::"
713         << "LocalPersistentIDToIOR" << endl;
714
715    const char* empty = "";
716    return CORBA::string_dup (empty);
717 }
718 //   
719 // =========================================================== CanCopy
720 CORBA::Boolean HEXABLOCK_Gen_i::CanCopy (SALOMEDS::SObject_ptr theObject)
721 {
722    if (db) cout << " +++++++ Je suis passe par  HEXABLOCK_Gen_i::"
723         << "CanCopy" << endl;
724    return false;
725 }
726 // =========================================================== CopyFrom
727 SALOMEDS::TMPFile* HEXABLOCK_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, 
728                                              CORBA::Long& theObjectID)
729 {
730    if (db) cout << " +++++++ Je suis passe par  HEXABLOCK_Gen_i::"
731         << "CopyFrom" << endl;
732    return NULL;
733 }
734 // =========================================================== CanPaste
735 CORBA::Boolean HEXABLOCK_Gen_i:: CanPaste (const char* theComponentName, 
736                                            CORBA::Long theObjectID)
737 {
738    if (db) cout << " +++++++ Je suis passe par  HEXABLOCK_Gen_i::"
739         << "CanPaste" << endl;
740    return false;
741 }
742 // =========================================================== PasteInto
743 SALOMEDS::SObject_ptr HEXABLOCK_Gen_i::PasteInto(const SALOMEDS::TMPFile& fic,
744                                   CORBA::Long theObjectID,
745                                   SALOMEDS::SObject_ptr theObject)
746 {
747    if (db) cout << " +++++++ Je suis passe par  HEXABLOCK_Gen_i::"
748         << "PasteInto" << endl;
749    return NULL;
750 }
751 // =========================================================== CanPaste
752 SALOMEDS::SObject_ptr HEXABLOCK_Gen_i::PublishInStudy (SALOMEDS::Study_ptr stud,
753                                        SALOMEDS::SObject_ptr theSObject,
754                                        CORBA::Object_ptr theObject,
755                                        const char* theName) 
756         throw (SALOME::SALOME_Exception) 
757 {
758    if (db) cout << " +++++++ Je suis passe par  HEXABLOCK_Gen_i::"
759         << "PublishInStudy" << endl;
760    return NULL;
761 }
762 // =========================================================== Save
763 SALOMEDS::TMPFile* HEXABLOCK_Gen_i::Save(const SALOMEDS::SComponent_ptr compo,
764                                          const char* theURL,
765                                          bool isMultiFile)
766 {
767    if (db) cout << " +++++++ Je suis passe par  HEXABLOCK_Gen_i::"
768         << "Save" << endl;
769
770    string tmp_fic = SALOMEDS_Tool::GetTmpDir();
771 // tmp_fic += "hexablock.xml";
772    tmp_fic  = "hexablock.xml";
773    cpchar  fic_study = tmp_fic.c_str();
774
775    int   len_buffer = 4;
776    char* ch_buffer  = NULL;
777    int ier = _engine_cpp->saveAllDocs (fic_study);
778    if (ier==HOK)
779       ch_buffer  = Hex::read_file (fic_study, len_buffer);
780    else
781       ch_buffer  = strdup ("<0>");
782
783    CORBA::Octet*      oct_buffer = (CORBA::Octet*) ch_buffer;
784    SALOMEDS::TMPFile* oct_stream = new SALOMEDS::TMPFile (len_buffer, 
785                                               len_buffer, oct_buffer, 1);
786    return oct_stream;
787 }
788 // =========================================================== Load
789 CORBA::Boolean HEXABLOCK_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
790                       const SALOMEDS::TMPFile& stream,
791                       const char* theURL,
792                       bool isMultiFile)
793 {
794    if (db) cout << " +++++++ Je suis passe par  HEXABLOCK_Gen_i::"
795         << "Load" << endl;
796
797    char *ch_buffer = (char*) stream.NP_data();
798
799    int ier = _engine_cpp->loadAllDocs (ch_buffer);
800    return ier==HOK;
801 }
802
803 // =========================================================== addDocInStudy
804 CORBA::Long HEXABLOCK_Gen_i::addDocInStudy (Document_ptr doc_in)
805                              throw ( SALOME::SALOME_Exception )
806 {
807    if (CORBA::is_nil(myCurrentStudy))
808       return 986;
809
810    Document_impl* doc_servant = ::DownCast <Document_impl*> (doc_in);
811    if (doc_servant == NULL)
812       return 987;
813
814    HEXA_NS::Document* doc = doc_servant->GetImpl();
815    if (doc == NULL)
816       return 988;
817
818    cpchar name = doc->getName();
819    PutData (name);
820    
821    SALOMEDS::StudyBuilder_var study_builder = myCurrentStudy->NewBuilder();
822    // study_builder->NewCommand();  // There is a transaction ???
823
824
825    SALOMEDS::SObject_var obj = ObjectToSObject( myCurrentStudy, doc_in );
826    if ( obj->_is_nil() )
827       {
828       SALOMEDS::SComponent_var father = PublishComponent( myCurrentStudy );
829       if ( father->_is_nil() )
830           return 989;
831
832    // SALOMEDS::SObject_var 
833    // s_obj = HEXABLOCK_Gen_i::ObjectToSObject( my_current_study, doc_in ); ???
834    // if ( obj->_is_nil() ) --> C'est le cas
835
836       SALOMEDS::SObject_var s_obj = study_builder->NewObject (father);
837       obj = s_obj._retn();
838       if ( obj->_is_nil() )
839          return 981;
840       }
841
842   SALOMEDS::GenericAttribute_var attrib;
843   SALOMEDS::AttributeName_var    attname;
844
845   attrib  = study_builder->FindOrCreateAttribute( obj, "AttributeName" );
846   attname = SALOMEDS::AttributeName::_narrow( attrib );
847   attname->SetValue (name);
848                                   /// ----------- Fin de SetName
849   // study_builder->CommitCommand(); ???
850   return HOK;
851 }
852 // =========================================================== Version information
853
854 char* HEXABLOCK_Gen_i::getVersion()
855 {
856 #if HEXABLOCK_DEVELOPMENT
857   return CORBA::string_dup(HEXABLOCK_VERSION_STR"dev");
858 #else
859   return CORBA::string_dup(HEXABLOCK_VERSION_STR);
860 #endif
861 }