Salome HOME
PAL12628: Enabling FPE signal in GEOM leads to Salome crash with some video card...
[modules/geom.git] / src / GEOM_I / GEOM_Gen_i.cc
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #ifdef WNT
21 #pragma warning( disable:4786 )
22 #endif
23
24 #include <Standard_Stream.hxx>
25
26 #include "GEOM_Gen_i.hh"
27 #include "GEOM_Object_i.hh"
28
29 #include <strstream>
30
31 #include "Utils_CorbaException.hxx"
32 #include "OpUtil.hxx"
33 #include "Utils_ExceptHandlers.hxx"
34 #include "utilities.h"
35
36 #include "GEOM_Object_i.hh"
37 #include "GEOM_Object.hxx"
38 #include "GEOM_Function.hxx"
39 #include "GEOMImpl_Types.hxx"
40 #include "GEOMImpl_CopyDriver.hxx"
41
42 // Cascade headers
43 #include <BRep_Builder.hxx>
44 #include <BRepTools.hxx>
45 #include <TDF_Label.hxx>
46 #include <TDF_Tool.hxx>
47 #include <TCollection_AsciiString.hxx>
48 #include <TColStd_HArray1OfInteger.hxx>
49 #include <TopAbs_ShapeEnum.hxx>
50 #include <OSD.hxx>
51
52 #include "SALOMEDS_Tool.hxx"
53
54 //============================================================================
55 // function : GEOM_Gen_i()
56 // purpose  : constructor to be called for servant creation.
57 //============================================================================
58 GEOM_Gen_i::GEOM_Gen_i(CORBA::ORB_ptr orb,
59                        PortableServer::POA_ptr poa,
60                        PortableServer::ObjectId * contId,
61                        const char *instanceName,
62                        const char *interfaceName) :
63   Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
64 {
65   _thisObj = this;
66   _id = _poa->activate_object(_thisObj);
67   name_service = new SALOME_NamingService(_orb);
68
69   _impl = new ::GEOMImpl_Gen;
70
71   //work around PAL12004, PAL12628
72   //OSD::SetSignal( true );
73   bool raiseFPE;
74 #ifdef _DEBUG_
75   raiseFPE = true;
76   char* envDisableFPE = getenv("DISABLE_FPE");
77   if (envDisableFPE && atoi(envDisableFPE))
78     raiseFPE = false;
79 #else
80   raiseFPE = false;
81 #endif
82   OSD::SetSignal( raiseFPE );
83 }
84
85 //============================================================================
86 // function : ~GEOM_Gen_i()
87 // purpose  : destructor
88 //============================================================================
89 GEOM_Gen_i::~GEOM_Gen_i() {
90   delete name_service;
91   delete _impl;
92 }
93
94
95 //============================================================================
96 // function : IORToLocalPersistentID()
97 // purpose  :
98 //============================================================================
99 char* GEOM_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
100                                          const char* IORString,
101                                          CORBA::Boolean isMultiFile,
102                                          CORBA::Boolean isASCII)
103 {
104   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(_orb->string_to_object(IORString));
105   if (!CORBA::is_nil(anObject)) {
106     return strdup(anObject->GetEntry());
107   }
108   return 0;
109 }
110
111
112 //============================================================================
113 // function : LocalPersistentIDToIOR()
114 // purpose  : Create/Load CORBA object from a persistent ref (an entry)
115 //          : Used when a study is loaded
116 //          : The IOR (IORName) of object created is returned
117 //============================================================================
118 char* GEOM_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
119                                          const char* aLocalPersistentID,
120                                          CORBA::Boolean isMultiFile,
121                                          CORBA::Boolean isASCII)
122 {
123   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
124
125   Handle(GEOM_Object) anObject = _impl->GetObject(aStudy->StudyId(), const_cast<char*>(aLocalPersistentID));
126   TCollection_AsciiString anEntry;
127   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
128   GEOM::GEOM_Object_var obj = GetObject(anObject->GetDocID(), anEntry.ToCString());
129
130   CORBA::String_var aPersRefString = _orb->object_to_string(obj);
131   return strdup(aPersRefString);
132 }
133
134 //============================================================================
135 // function : CanPublishInStudy
136 // purpose  :
137 //============================================================================
138 bool GEOM_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
139 {
140   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(theIOR);
141   return !(anObject->_is_nil());
142 }
143
144
145 //============================================================================
146 // function : PublishInStudy
147 // purpose  :
148 //============================================================================
149 SALOMEDS::SObject_ptr GEOM_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
150                                                  SALOMEDS::SObject_ptr theSObject,
151                                                  CORBA::Object_ptr theObject,
152                                                  const char* theName) throw (SALOME::SALOME_Exception)
153 {
154   Unexpect aCatch(SALOME_SalomeException);
155   SALOMEDS::SObject_var aResultSO;
156   if(CORBA::is_nil(theObject) || theStudy->_is_nil()) return aResultSO;
157
158   GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow(theObject);
159   if(aShape->_is_nil()) return aResultSO;
160
161   SALOMEDS::GenericAttribute_var anAttr;
162   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder();
163
164   SALOMEDS::SComponent_var       aFather = theStudy->FindComponent("GEOM");
165   if (aFather->_is_nil()) {
166     aFather = aStudyBuilder->NewComponent("GEOM");
167     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributeName");
168     SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
169     aName->SetValue("Geometry");
170     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
171     SALOMEDS::AttributePixMap::_narrow(anAttr)->SetPixMap("ICON_OBJBROWSER_Geometry");
172     aStudyBuilder->DefineComponentInstance(aFather, GEOM_Gen::_this());
173   }
174   if (aFather->_is_nil()) return aResultSO;
175
176   if (CORBA::is_nil(theSObject)) {
177     aResultSO = aStudyBuilder->NewObject(aFather);
178   } else {
179     if (!theSObject->ReferencedObject(aResultSO))
180       aResultSO = SALOMEDS::SObject::_duplicate(theSObject); //SRN: Added Aug 24,2004 : for  the method AddInStudy with theFather argumenet != NULL
181       //THROW_SALOME_CORBA_EXCEPTION("Publish in study supervision graph error",SALOME::BAD_PARAM);
182   }
183   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
184   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
185   char *aGeomObjIOR = _orb->object_to_string(theObject);
186   anIOR->SetValue(strdup(aGeomObjIOR));
187
188   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributePixMap");
189   SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
190   TCollection_AsciiString aShapeName("Shape_");
191
192   if ( aShape->GetType() == GEOM_GROUP ) {
193     GEOM::GEOM_IGroupOperations_var anOp = GetIGroupOperations( theStudy->StudyId() );
194     switch ( (TopAbs_ShapeEnum)anOp->GetType( aShape ) ) {
195     case TopAbs_VERTEX:
196       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_PNT" );
197       aShapeName = "Group_Of_Vertices_";
198       break;
199     case TopAbs_EDGE:
200       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_EDGE" );
201       aShapeName = "Group_Of_Edges_";
202       break;
203     case TopAbs_FACE:
204       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_FACE" );
205       aShapeName = "Group_Of_Faces_";
206       break;
207     case TopAbs_SOLID:
208       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_SOLID" );
209       aShapeName = "Group_Of_Solids_";
210       break;
211     }
212   } else if ( aShape->GetType() == GEOM_MARKER ) {
213     aPixmap->SetPixMap( "ICON_OBJBROWSER_LCS" );
214     aShapeName = "LocalCS_";
215   } else if ( aShape->GetShapeType() == GEOM::COMPOUND ) {
216     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPOUND" );
217     aShapeName = "Compound_";
218   } else if ( aShape->GetShapeType() == GEOM::COMPSOLID ) {
219     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPSOLID" );
220     aShapeName = "Compsolid_";
221   } else if ( aShape->GetShapeType() == GEOM::SOLID ) {
222     aPixmap->SetPixMap( "ICON_OBJBROWSER_SOLID" );
223     aShapeName = "Solid_";
224   } else if ( aShape->GetShapeType() == GEOM::SHELL ) {
225     aPixmap->SetPixMap( "ICON_OBJBROWSER_SHELL" );
226     aShapeName = "Shell_";
227   } else if ( aShape->GetShapeType() == GEOM::FACE ) {
228     aPixmap->SetPixMap( "ICON_OBJBROWSER_FACE" );
229     aShapeName = "Face_";
230   } else if ( aShape->GetShapeType() == GEOM::WIRE ) {
231     aPixmap->SetPixMap( "ICON_OBJBROWSER_WIRE" );
232     aShapeName = "Wire_";
233   } else if ( aShape->GetShapeType() == GEOM::EDGE ) {
234     aPixmap->SetPixMap( "ICON_OBJBROWSER_EDGE" );
235     aShapeName = "Edge_";
236   } else if ( aShape->GetShapeType() == GEOM::VERTEX ) {
237     aPixmap->SetPixMap( "ICON_OBJBROWSER_VERTEX" );
238     aShapeName = "Vertex_";
239   }
240   //if (strlen(theName) == 0) aShapeName += TCollection_AsciiString(aResultSO->Tag());
241   //else aShapeName = TCollection_AsciiString(strdup(theName));
242
243   // asv : 11.11.04 Introducing a more sofisticated method of name creation, just as
244   //       it is done in GUI in GEOMBase::GetDefaultName() - not just add a Tag() == number
245   //       of objects in the study, but compute a number of objects with the same prefix
246   //       and build a new name as Prefix_N+1
247   if ( strlen( theName ) == 0 ) { // MOST PROBABLY CALLED FROM BATCHMODE OR SUPERVISOR
248     int i = 0;                    // (WITH EMPTY NEW NAME)
249     SALOMEDS::SObject_var obj;
250     TCollection_AsciiString aNewShapeName;
251     do {
252       aNewShapeName = aShapeName + TCollection_AsciiString(++i);
253       obj = theStudy->FindObject( aNewShapeName.ToCString() );
254     }
255     while ( !obj->_is_nil() );
256     aShapeName = aNewShapeName;
257   }
258   else // MOST PROBABLY CALLED FROM GEOM GUI (ALREADY WITH VALID NAME)
259     aShapeName = TCollection_AsciiString(strdup(theName));
260
261   //Set the study entry as a name of  the published GEOM_Object
262   aShape->SetStudyEntry(aResultSO->GetID());
263
264   //Set a name of the added shape
265   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeName");
266   SALOMEDS::AttributeName_var aNameAttrib = SALOMEDS::AttributeName::_narrow(anAttr);
267   aNameAttrib->SetValue(aShapeName.ToCString());
268
269   return aResultSO._retn();
270 }
271
272
273 //============================================================================
274 // function : Save()
275 // purpose  : save OCAF/Geom document
276 //============================================================================
277 SALOMEDS::TMPFile* GEOM_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
278                                     const char* theURL,
279                                     bool isMultiFile) {
280   SALOMEDS::TMPFile_var aStreamFile;
281   // Get a temporary directory to store a file
282   std::string aTmpDir = (isMultiFile)?theURL:SALOMEDS_Tool::GetTmpDir();
283   // Create a list to store names of created files
284   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
285   aSeq->length(1);
286   // Prepare a file name to open
287   TCollection_AsciiString aNameWithExt("");
288   if (isMultiFile)
289     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL())).c_str());
290   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
291   aSeq[0] = CORBA::string_dup(aNameWithExt.ToCString());
292   // Build a full file name of temporary file
293   TCollection_AsciiString aFullName = TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt;
294   // Save GEOM component in this file
295   _impl->Save(theComponent->GetStudy()->StudyId(), aFullName.ToCString());
296   // Conver a file to the byte stream
297   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
298   // Remove the created file and tmp directory
299   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
300
301   // Return the created byte stream
302   return aStreamFile._retn();
303 }
304
305
306 //============================================================================
307 // function : SaveASCII()
308 // purpose  :
309 //============================================================================
310 SALOMEDS::TMPFile* GEOM_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
311                                          const char* theURL,
312                                          bool isMultiFile) {
313   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
314   return aStreamFile._retn();
315 }
316
317
318 //============================================================================
319 // function : Load()
320 // purpose  :
321 //============================================================================
322 CORBA::Boolean GEOM_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
323                                 const SALOMEDS::TMPFile& theStream,
324                                 const char* theURL,
325                                 bool isMultiFile) {
326
327   if (theStream.length() <= 9) {
328     MESSAGE("The TMPFile is too short : " << theStream.length() << " bytes ");
329     return false;
330   }
331
332   // Get a temporary directory for a file
333   std::string aTmpDir = isMultiFile?theURL:SALOMEDS_Tool::GetTmpDir();
334   // Conver the byte stream theStream to a file and place it in tmp directory
335   SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.c_str(), isMultiFile);
336
337   // Prepare a file name to open
338   TCollection_AsciiString aNameWithExt("");
339   if (isMultiFile)
340     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL())).c_str());
341   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
342   TCollection_AsciiString aFullName = (TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt);
343
344   // Open document
345   if (!_impl->Load(theComponent->GetStudy()->StudyId(), aFullName.ToCString())) return false;
346
347   // Remove the created file and tmp directory
348   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
349
350   SALOMEDS::Study_var Study = theComponent->GetStudy();
351   TCollection_AsciiString name( strdup(Study->Name()) );
352
353   return true;
354 }
355
356
357 //============================================================================
358 // function : LoadASCII()
359 // purpose  :
360 //============================================================================
361 CORBA::Boolean GEOM_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
362                                      const SALOMEDS::TMPFile& theStream,
363                                      const char* theURL,
364                                      bool isMultiFile) {
365   return Load(theComponent, theStream, theURL, isMultiFile);
366 }
367
368
369 //============================================================================
370 // function : Close()
371 // purpose  :
372 //============================================================================
373 void GEOM_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
374 {
375   _impl->Close(theComponent->GetStudy()->StudyId());
376 }
377
378 //============================================================================
379 // function : CanCopy()
380 // purpose  :
381 //============================================================================
382 CORBA::Boolean GEOM_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
383   // Try to retrieve known by Geometry component GEOM_Object by given IOR
384   SALOMEDS::GenericAttribute_var anAttr;
385   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
386
387   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
388
389   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(_orb->string_to_object(anIOR->Value()));
390   // If the object is null one it can't be copied: return false
391   if (anObject->_is_nil()) return false;
392   return true;
393 }
394
395 //============================================================================
396 // function : CopyFrom()
397 // purpose  :
398 //============================================================================
399 SALOMEDS::TMPFile* GEOM_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
400   // Declare a sequence of the byte to store the copied object
401   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
402
403   // Try to get GEOM_Object object by given SObject
404   SALOMEDS::GenericAttribute_var anAttr;
405   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return aStreamFile._retn();
406   GEOM::GEOM_Object_var anObject =  GEOM::GEOM_Object::_narrow(_orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()));
407   if (anObject->_is_nil()) return aStreamFile._retn();
408
409   aStreamFile = anObject->GetShapeStream();
410
411   // Assign an ID  the type of  GEOM_Object
412   theObjectID = anObject->GetType();
413
414   // Return created TMPFile
415   return aStreamFile._retn();
416 }
417
418 //============================================================================
419 // function : CanPaste()
420 // purpose  :
421 //============================================================================
422 CORBA::Boolean GEOM_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
423   // The Geometry component can paste only objects copied by Geometry component
424   // and with the object type = 1
425   if (strcmp(theComponentName, ComponentDataType()) != 0) return false;
426   return true;
427 }
428
429 //============================================================================
430 // function : PasteInto()
431 // purpose  :
432 //============================================================================
433 SALOMEDS::SObject_ptr GEOM_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
434                                             CORBA::Long theObjectID,
435                                             SALOMEDS::SObject_ptr theObject) {
436   // Find the current Study and StudyBuilder
437   SALOMEDS::Study_var aStudy = theObject->GetStudy();
438   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
439
440   // Retrieve a TopoDS_Shape from byte stream
441   TopoDS_Shape aTopology;
442   istrstream aStreamedBrep((char*) &theStream[0], theStream.length());
443   BRep_Builder aBuilder;
444   try {
445     BRepTools::Read(aTopology, aStreamedBrep, aBuilder);
446   } catch (Standard_Failure) {
447     return false;
448   }
449
450   // SObject of the created shape is theObject or new Child of Component if theObject == geom component
451   SALOMEDS::SObject_var aNewSO;
452   if (strcmp(theObject->GetFatherComponent()->GetID(),theObject->GetID()) == 0) {
453     aNewSO = aStudyBuilder->NewObject(theObject);
454   } else aNewSO = SALOMEDS::SObject::_duplicate(theObject);
455
456
457   //Create a new GEOM_Object
458   Handle(GEOM_Object) anObj = _impl->AddObject(aNewSO->GetStudy()->StudyId(), theObjectID);
459   Handle(GEOM_Function) aFunction = anObj->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
460   aFunction->SetValue(aTopology);
461
462   TCollection_AsciiString anEntry;
463   TDF_Tool::Entry(anObj->GetEntry(), anEntry);
464   GEOM::GEOM_Object_var obj = GetObject(anObj->GetDocID(), anEntry.ToCString());
465
466   // Add IORAttribute to the Study and set IOR of the created GEOM_Object to it
467   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSO, "AttributeIOR");
468   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
469   anIOR->SetValue(_orb->object_to_string(obj));
470
471   // Return the created in the Study SObject
472   return aNewSO._retn();
473 }
474
475 //============================================================================
476 // function : ComponentDataType()
477 // purpose  :
478 //============================================================================
479 char* GEOM_Gen_i::ComponentDataType()
480 {
481   return strdup("GEOM");
482 }
483
484 //============================================================================
485 // function : AddInStudy
486 // purpose  :
487 //============================================================================
488 SALOMEDS::SObject_ptr GEOM_Gen_i::AddInStudy(SALOMEDS::Study_ptr theStudy, GEOM::GEOM_Object_ptr theObject, const char* theName, GEOM::GEOM_Object_ptr theFather)
489 {
490   SALOMEDS::SObject_var aResultSO;
491   if(theObject->_is_nil() || theStudy->_is_nil()) return aResultSO;
492
493   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
494   char* IOR;
495
496   if(!theFather->_is_nil()) {
497     IOR = _orb->object_to_string(theFather);
498     SALOMEDS::SObject_var aFatherSO = theStudy->FindObjectIOR(IOR);
499     if(aFatherSO->_is_nil()) return aResultSO._retn();
500     aResultSO = aStudyBuilder->NewObject(aFatherSO);
501     //aStudyBuilder->Addreference(aResultSO, aResultSO);
502   }
503
504   aResultSO = PublishInStudy(theStudy, aResultSO, theObject, theName);
505   if(aResultSO->_is_nil()) return aResultSO._retn();
506
507   GEOM::ListOfGO_var aList = theObject->GetDependency();
508   Standard_Integer aLength = aList->length();
509   if(aLength < 1) return aResultSO._retn();
510
511   //Publish the arguments
512   for(Standard_Integer i = 0; i< aLength; i++) {
513     GEOM::GEOM_Object_var anObject = aList[i];
514     if(anObject->_is_nil()) continue;
515     IOR = _orb->object_to_string(anObject);
516     SALOMEDS::SObject_var aSO =  theStudy->FindObjectIOR(IOR);
517     if(aSO->_is_nil()) continue;
518     SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(aResultSO);
519     aStudyBuilder->Addreference(aSubSO, aSO);
520   }
521
522   return aResultSO._retn();
523 }
524
525 //============================================================================
526 // function : register()
527 // purpose  : register 'name' in 'name_service'
528 //============================================================================
529 void GEOM_Gen_i::register_name(char * name)
530 {
531   GEOM::GEOM_Gen_ptr g = GEOM::GEOM_Gen::_narrow(_this());
532   name_service->Register(g, strdup(name));
533 }
534
535 //============================================================================
536 // function : Undo
537 // purpose  :
538 //============================================================================
539 void GEOM_Gen_i::Undo(CORBA::Long theStudyID)
540 {
541   _impl->Undo(theStudyID);
542 }
543
544 //============================================================================
545 // function : Redo
546 // purpose  :
547 //============================================================================
548 void GEOM_Gen_i::Redo(CORBA::Long theStudyID)
549 {
550   _impl->Redo(theStudyID);
551 }
552
553 //============================================================================
554 // function : GetIBasicOperations
555 // purpose  :
556 //============================================================================
557 GEOM::GEOM_IBasicOperations_ptr GEOM_Gen_i::GetIBasicOperations(CORBA::Long theStudyID)
558      throw ( SALOME::SALOME_Exception )
559 {
560   Unexpect aCatch(SALOME_SalomeException);
561   MESSAGE( "GEOM_Gen_i::GetIBasicOperations" );
562
563   GEOM::GEOM_Gen_ptr engine = _this();
564
565   GEOM_IBasicOperations_i* aServant =
566     new GEOM_IBasicOperations_i(_poa, engine, _impl->GetIBasicOperations(theStudyID));
567
568   // activate the CORBA servant
569   GEOM::GEOM_IBasicOperations_var operations = aServant->_this();
570   return operations._retn();
571 }
572
573 //============================================================================
574 // function : GetITransformOperations
575 // purpose  :
576 //============================================================================
577 GEOM::GEOM_ITransformOperations_ptr GEOM_Gen_i::GetITransformOperations(CORBA::Long theStudyID)
578      throw ( SALOME::SALOME_Exception )
579 {
580   Unexpect aCatch(SALOME_SalomeException);
581   MESSAGE( "GEOM_Gen_i::GetITransformOperations" );
582
583   GEOM::GEOM_Gen_ptr engine = _this();
584
585   GEOM_ITransformOperations_i* aServant =
586     new GEOM_ITransformOperations_i(_poa, engine, _impl->GetITransformOperations(theStudyID));
587
588   // activate the CORBA servant
589   GEOM::GEOM_ITransformOperations_var operations = aServant->_this();
590   return operations._retn();
591 }
592
593 //============================================================================
594 // function : GetI3DPrimOperations
595 // purpose  :
596 //============================================================================
597 GEOM::GEOM_I3DPrimOperations_ptr GEOM_Gen_i::GetI3DPrimOperations(CORBA::Long theStudyID)
598      throw ( SALOME::SALOME_Exception )
599 {
600   Unexpect aCatch(SALOME_SalomeException);
601   MESSAGE( "GEOM_Gen_i::GetI3DPrimOperations" );
602
603   GEOM::GEOM_Gen_ptr engine = _this();
604
605   GEOM_I3DPrimOperations_i* aServant =
606     new GEOM_I3DPrimOperations_i(_poa, engine, _impl->GetI3DPrimOperations(theStudyID));
607
608   // activate the CORBA servant
609   GEOM::GEOM_I3DPrimOperations_var operations = aServant->_this();
610   return operations._retn();
611 }
612
613 //============================================================================
614 // function : GetIShapesOperations
615 // purpose  :
616 //============================================================================
617 GEOM::GEOM_IShapesOperations_ptr GEOM_Gen_i::GetIShapesOperations(CORBA::Long theStudyID)
618      throw ( SALOME::SALOME_Exception )
619 {
620   Unexpect aCatch(SALOME_SalomeException);
621   MESSAGE( "GEOM_Gen_i::GetIShapesOperations" );
622
623   GEOM::GEOM_Gen_ptr engine = _this();
624
625   GEOM_IShapesOperations_i* aServant =
626     new GEOM_IShapesOperations_i(_poa, engine, _impl->GetIShapesOperations(theStudyID));
627
628   // activate the CORBA servant
629   GEOM::GEOM_IShapesOperations_var operations = aServant->_this();
630   return operations._retn();
631 }
632
633 //============================================================================
634 // function : GetIBlocksOperations
635 // purpose  :
636 //============================================================================
637 GEOM::GEOM_IBlocksOperations_ptr GEOM_Gen_i::GetIBlocksOperations(CORBA::Long theStudyID)
638      throw ( SALOME::SALOME_Exception )
639 {
640   Unexpect aCatch(SALOME_SalomeException);
641   MESSAGE( "GEOM_Gen_i::GetIBlocksOperations" );
642
643   GEOM::GEOM_Gen_ptr engine = _this();
644
645   GEOM_IBlocksOperations_i* aServant =
646     new GEOM_IBlocksOperations_i(_poa, engine, _impl->GetIBlocksOperations(theStudyID));
647
648   // activate the CORBA servant
649   GEOM::GEOM_IBlocksOperations_var operations = aServant->_this();
650   return operations._retn();
651 }
652
653 //============================================================================
654 // function : GetIBooleanOperations
655 // purpose  :
656 //============================================================================
657 GEOM::GEOM_IBooleanOperations_ptr GEOM_Gen_i::GetIBooleanOperations(CORBA::Long theStudyID)
658      throw ( SALOME::SALOME_Exception )
659 {
660   Unexpect aCatch(SALOME_SalomeException);
661   MESSAGE( "GEOM_Gen_i::GetIBooleanOperations" );
662
663   GEOM::GEOM_Gen_ptr engine = _this();
664
665   GEOM_IBooleanOperations_i* aServant =
666     new GEOM_IBooleanOperations_i(_poa, engine, _impl->GetIBooleanOperations(theStudyID));
667
668   // activate the CORBA servant
669   GEOM::GEOM_IBooleanOperations_var operations = aServant->_this();
670   return operations._retn();
671 }
672
673 //============================================================================
674 // function : GetICurvesOperations
675 // purpose  :
676 //============================================================================
677 GEOM::GEOM_ICurvesOperations_ptr GEOM_Gen_i::GetICurvesOperations(CORBA::Long theStudyID)
678      throw ( SALOME::SALOME_Exception )
679 {
680   Unexpect aCatch(SALOME_SalomeException);
681   MESSAGE( "GEOM_Gen_i::GetICurvesOperations" );
682
683   GEOM::GEOM_Gen_ptr engine = _this();
684
685   GEOM_ICurvesOperations_i* aServant =
686     new GEOM_ICurvesOperations_i(_poa, engine, _impl->GetICurvesOperations(theStudyID));
687
688   // activate the CORBA servant
689   GEOM::GEOM_ICurvesOperations_var operations = aServant->_this();
690   return operations._retn();
691 }
692
693 //============================================================================
694 // function : GetILocalOperations
695 // purpose  :
696 //============================================================================
697 GEOM::GEOM_ILocalOperations_ptr GEOM_Gen_i::GetILocalOperations(CORBA::Long theStudyID)
698      throw ( SALOME::SALOME_Exception )
699 {
700   Unexpect aCatch(SALOME_SalomeException);
701   MESSAGE( "GEOM_Gen_i::GetILocalOperations" );
702
703   GEOM::GEOM_Gen_ptr engine = _this();
704
705   GEOM_ILocalOperations_i* aServant =
706     new GEOM_ILocalOperations_i(_poa, engine, _impl->GetILocalOperations(theStudyID));
707
708   // activate the CORBA servant
709   GEOM::GEOM_ILocalOperations_var operations = aServant->_this();
710   return operations._retn();
711 }
712
713 //============================================================================
714 // function : GetIHealingOperations
715 // purpose  :
716 //============================================================================
717 GEOM::GEOM_IHealingOperations_ptr GEOM_Gen_i::GetIHealingOperations(CORBA::Long theStudyID)
718      throw ( SALOME::SALOME_Exception )
719 {
720   Unexpect aCatch(SALOME_SalomeException);
721   MESSAGE( "GEOM_Gen_i::IHealingOperations" );
722
723   GEOM::GEOM_Gen_ptr engine = _this();
724
725   GEOM_IHealingOperations_i* aServant =
726     new GEOM_IHealingOperations_i(_poa, engine, _impl->GetIHealingOperations(theStudyID));
727
728   // activate the CORBA servant
729   GEOM::GEOM_IHealingOperations_var operations = aServant->_this();
730   return operations._retn();
731 }
732
733 //============================================================================
734 // function : GetIInsertOperations
735 // purpose  :
736 //============================================================================
737 GEOM::GEOM_IInsertOperations_ptr GEOM_Gen_i::GetIInsertOperations(CORBA::Long theStudyID)
738      throw ( SALOME::SALOME_Exception )
739 {
740   Unexpect aCatch(SALOME_SalomeException);
741   MESSAGE( "GEOM_Gen_i::GetIInsertOperations" );
742
743   GEOM::GEOM_Gen_ptr engine = _this();
744
745   GEOM_IInsertOperations_i* aServant =
746     new GEOM_IInsertOperations_i(_poa, engine, _impl->GetIInsertOperations(theStudyID));
747
748   // activate the CORBA servant
749   GEOM::GEOM_IInsertOperations_var operations = aServant->_this();
750   return operations._retn();
751 }
752
753 //============================================================================
754 // function : GetIMeasureOperations
755 // purpose  :
756 //============================================================================
757 GEOM::GEOM_IMeasureOperations_ptr GEOM_Gen_i::GetIMeasureOperations(CORBA::Long theStudyID)
758      throw ( SALOME::SALOME_Exception )
759 {
760   Unexpect aCatch(SALOME_SalomeException);
761   MESSAGE( "GEOM_Gen_i::GetIMeasureOperations" );
762
763   GEOM::GEOM_Gen_ptr engine = _this();
764
765   GEOM_IMeasureOperations_i* aServant =
766     new GEOM_IMeasureOperations_i(_poa, engine, _impl->GetIMeasureOperations(theStudyID));
767
768   // activate the CORBA servant
769   GEOM::GEOM_IMeasureOperations_var operations = aServant->_this();
770   return operations._retn();
771 }
772
773 //============================================================================
774 // function : GetIGroupOperations
775 // purpose  :
776 //============================================================================
777 GEOM::GEOM_IGroupOperations_ptr GEOM_Gen_i::GetIGroupOperations(CORBA::Long theStudyID)
778      throw ( SALOME::SALOME_Exception )
779 {
780   Unexpect aCatch(SALOME_SalomeException);
781   MESSAGE( "GEOM_Gen_i::GetIGroupOperations" );
782
783   GEOM::GEOM_Gen_ptr engine = _this();
784
785   GEOM_IGroupOperations_i* aServant =
786     new GEOM_IGroupOperations_i(_poa, engine, _impl->GetIGroupOperations(theStudyID));
787
788   // activate the CORBA servant
789   GEOM::GEOM_IGroupOperations_var operations = aServant->_this();
790   return operations._retn();
791 }
792
793 //=============================================================================
794 /*!
795  *  AddSubShape
796  */
797 //=============================================================================
798 GEOM::GEOM_Object_ptr GEOM_Gen_i::AddSubShape (GEOM::GEOM_Object_ptr theMainShape,
799                                                const GEOM::ListOfLong& theIndices)
800 {
801   if(theMainShape == NULL || theIndices.length() < 1) return GEOM::GEOM_Object::_nil();
802   Handle(GEOM_Object) aMainsShape = _impl->GetObject(theMainShape->GetStudyID(), theMainShape->GetEntry());
803   if (aMainsShape.IsNull()) return GEOM::GEOM_Object::_nil();
804
805   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1, theIndices.length());
806   for(Standard_Integer i = 0; i<theIndices.length(); i++) anArray->SetValue(i+1, theIndices[i]);
807
808   Handle(GEOM_Object) anObject = _impl->AddSubShape(aMainsShape, anArray, true);
809   if(anObject.IsNull()) return GEOM::GEOM_Object::_nil();
810
811   TCollection_AsciiString anEntry;
812   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
813   return GetObject(anObject->GetDocID(), anEntry.ToCString());
814 }
815
816 //=============================================================================
817 /*!
818  *  RemoveObject
819  */
820 //=============================================================================
821 void GEOM_Gen_i::RemoveObject(GEOM::GEOM_Object_ptr theObject)
822 {
823   Handle(GEOM_Object) anObject = _impl->GetObject(theObject->GetStudyID(), theObject->GetEntry());
824   if (anObject.IsNull()) return;
825   _impl->RemoveObject(anObject);
826   return;
827 }
828
829
830 //=================================================================================
831 // function : GetStringFromIOR()
832 // purpose  : returns a string that represents  a 'GEOM::GEOM_Object_var'
833 //=================================================================================
834 char* GEOM_Gen_i::GetStringFromIOR(GEOM::GEOM_Object_ptr theObject) {
835   return _orb->object_to_string(theObject);
836 }
837
838
839 //=================================================================================
840 // function : GetIORFromString()
841 // purpose  : returns a 'GEOM::GEOM_Object_var' from a string representing it
842 //=================================================================================
843 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetIORFromString(const char* stringIOR) {
844   GEOM::GEOM_Object_var aGeomObject;
845   if(strcmp(stringIOR,"") != 0){
846     CORBA::Object_var anObject = _orb->string_to_object(stringIOR);
847     if(!CORBA::is_nil(anObject))
848       aGeomObject =  GEOM::GEOM_Object::_narrow(anObject.in());
849   }
850   return aGeomObject._retn();
851 }
852
853 //=================================================================================
854 // function : GetObject()
855 // purpose  :
856 //=================================================================================
857 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetObject (CORBA::Long theStudyID, const char* theEntry)
858 {
859   GEOM::GEOM_Object_var obj;
860   Handle(GEOM_Object) handle_object = _impl->GetObject(theStudyID, (char*)theEntry);
861   if (handle_object.IsNull()) return NULL;
862
863   TCollection_AsciiString stringIOR = handle_object->GetIOR();
864   if (stringIOR.Length() > 1) {
865     CORBA::Object_var corba_object = _orb->string_to_object(stringIOR.ToCString());
866     if (!CORBA::is_nil(corba_object)) obj = GEOM::GEOM_Object::_narrow(corba_object);
867     return obj._retn();
868   }
869
870   GEOM::GEOM_Gen_ptr engine = _this();
871   GEOM_Object_i* servant = new GEOM_Object_i (_poa, engine, handle_object);
872
873   obj = servant->_this();
874   stringIOR = _orb->object_to_string(obj);
875   handle_object->SetIOR(stringIOR);
876   return obj._retn();
877 }
878
879
880 //=====================================================================================
881 // EXPORTED METHODS
882 //=====================================================================================
883 extern "C"
884 {
885 #ifdef WNT
886         __declspec( dllexport )
887 #endif
888   PortableServer::ObjectId * GEOMEngine_factory(CORBA::ORB_ptr orb,
889                                                 PortableServer::POA_ptr poa,
890                                                 PortableServer::ObjectId * contId,
891                                                 const char *instanceName,
892                                                 const char * interfaceName)
893   {
894    GEOM_Gen_i * myGEOM_Gen_i = new GEOM_Gen_i(orb, poa, contId, instanceName, interfaceName);
895    myGEOM_Gen_i->register_name("/myGEOM_Gen"); // NRI : 11/07/2002 : Add for Supervision example
896    return myGEOM_Gen_i->getId();
897   }
898 }