Salome HOME
To show notebook variables in the Object Browser.
[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   //PAL10867: disable signals catching with "noexcepthandler" option
72   char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
73   if (!envNoCatchSignals || !atoi(envNoCatchSignals))
74   {
75     //work around PAL12004, PAL12628
76     //OSD::SetSignal( true );
77     bool raiseFPE;
78 #ifdef _DEBUG_
79     raiseFPE = true;
80     char* envDisableFPE = getenv("DISABLE_FPE");
81     if (envDisableFPE && atoi(envDisableFPE))
82       raiseFPE = false;
83 #else
84     raiseFPE = false;
85 #endif
86     OSD::SetSignal( raiseFPE );
87   }
88 }
89
90 //============================================================================
91 // function : ~GEOM_Gen_i()
92 // purpose  : destructor
93 //============================================================================
94 GEOM_Gen_i::~GEOM_Gen_i() {
95   delete name_service;
96   delete _impl;
97 }
98
99
100 //============================================================================
101 // function : IORToLocalPersistentID()
102 // purpose  :
103 //============================================================================
104 char* GEOM_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
105                                          const char* IORString,
106                                          CORBA::Boolean isMultiFile,
107                                          CORBA::Boolean isASCII)
108 {
109   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(_orb->string_to_object(IORString));
110   if (!CORBA::is_nil(anObject)) {
111     return CORBA::string_dup(anObject->GetEntry());
112   }
113   return 0;
114 }
115
116
117 //============================================================================
118 // function : LocalPersistentIDToIOR()
119 // purpose  : Create/Load CORBA object from a persistent ref (an entry)
120 //          : Used when a study is loaded
121 //          : The IOR (IORName) of object created is returned
122 //============================================================================
123 char* GEOM_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
124                                          const char* aLocalPersistentID,
125                                          CORBA::Boolean isMultiFile,
126                                          CORBA::Boolean isASCII)
127 {
128   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
129
130   Handle(GEOM_Object) anObject = _impl->GetObject(aStudy->StudyId(), const_cast<char*>(aLocalPersistentID));
131   TCollection_AsciiString anEntry;
132   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
133   GEOM::GEOM_Object_var obj = GetObject(anObject->GetDocID(), anEntry.ToCString());
134
135   CORBA::String_var aPersRefString = _orb->object_to_string(obj);
136   return CORBA::string_dup(aPersRefString);
137 }
138
139 //============================================================================
140 // function : CanPublishInStudy
141 // purpose  :
142 //============================================================================
143 bool GEOM_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
144 {
145   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(theIOR);
146   return !(anObject->_is_nil());
147 }
148
149
150 //============================================================================
151 // function : PublishInStudy
152 // purpose  :
153 //============================================================================
154 SALOMEDS::SObject_ptr GEOM_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
155                                                  SALOMEDS::SObject_ptr theSObject,
156                                                  CORBA::Object_ptr theObject,
157                                                  const char* theName) throw (SALOME::SALOME_Exception)
158 {
159   Unexpect aCatch(SALOME_SalomeException);
160   SALOMEDS::SObject_var aResultSO;
161   if(CORBA::is_nil(theObject) || theStudy->_is_nil()) return aResultSO;
162
163   GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow(theObject);
164   if(aShape->_is_nil()) return aResultSO;
165
166   SALOMEDS::GenericAttribute_var anAttr;
167   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder();
168
169   SALOMEDS::SComponent_var       aFather = theStudy->FindComponent("GEOM");
170   if (aFather->_is_nil()) {
171     aFather = aStudyBuilder->NewComponent("GEOM");
172     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributeName");
173     SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
174     aName->SetValue("Geometry");
175     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
176     SALOMEDS::AttributePixMap::_narrow(anAttr)->SetPixMap("ICON_OBJBROWSER_Geometry");
177     aStudyBuilder->DefineComponentInstance(aFather, GEOM_Gen::_this());
178   }
179   if (aFather->_is_nil()) return aResultSO;
180
181   if (CORBA::is_nil(theSObject)) {
182     aResultSO = aStudyBuilder->NewObject(aFather);
183   } else {
184     if (!theSObject->ReferencedObject(aResultSO))
185       aResultSO = SALOMEDS::SObject::_duplicate(theSObject); //SRN: Added Aug 24,2004 : for  the method AddInStudy with theFather argumenet != NULL
186       //THROW_SALOME_CORBA_EXCEPTION("Publish in study supervision graph error",SALOME::BAD_PARAM);
187   }
188   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
189   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
190   //char *aGeomObjIOR = _orb->object_to_string(theObject);
191    CORBA::String_var aGeomObjIOR = _orb->object_to_string(theObject);
192   //anIOR->SetValue(CORBA::string_dup(aGeomObjIOR));
193   anIOR->SetValue(aGeomObjIOR);
194
195   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributePixMap");
196   SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
197   TCollection_AsciiString aShapeName("Shape_");
198
199   if ( aShape->GetType() == GEOM_GROUP ) {
200     GEOM::GEOM_IGroupOperations_var anOp = GetIGroupOperations( theStudy->StudyId() );
201     switch ( (TopAbs_ShapeEnum)anOp->GetType( aShape ) ) {
202     case TopAbs_VERTEX:
203       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_PNT" );
204       aShapeName = "Group_Of_Vertices_";
205       break;
206     case TopAbs_EDGE:
207       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_EDGE" );
208       aShapeName = "Group_Of_Edges_";
209       break;
210     case TopAbs_FACE:
211       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_FACE" );
212       aShapeName = "Group_Of_Faces_";
213       break;
214     case TopAbs_SOLID:
215       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_SOLID" );
216       aShapeName = "Group_Of_Solids_";
217       break;
218     }
219   } else if ( aShape->GetType() == GEOM_MARKER ) {
220     aPixmap->SetPixMap( "ICON_OBJBROWSER_LCS" );
221     aShapeName = "LocalCS_";
222   } else if ( aShape->GetShapeType() == GEOM::COMPOUND ) {
223     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPOUND" );
224     aShapeName = "Compound_";
225   } else if ( aShape->GetShapeType() == GEOM::COMPSOLID ) {
226     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPSOLID" );
227     aShapeName = "Compsolid_";
228   } else if ( aShape->GetShapeType() == GEOM::SOLID ) {
229     aPixmap->SetPixMap( "ICON_OBJBROWSER_SOLID" );
230     aShapeName = "Solid_";
231   } else if ( aShape->GetShapeType() == GEOM::SHELL ) {
232     aPixmap->SetPixMap( "ICON_OBJBROWSER_SHELL" );
233     aShapeName = "Shell_";
234   } else if ( aShape->GetShapeType() == GEOM::FACE ) {
235     aPixmap->SetPixMap( "ICON_OBJBROWSER_FACE" );
236     aShapeName = "Face_";
237   } else if ( aShape->GetShapeType() == GEOM::WIRE ) {
238     aPixmap->SetPixMap( "ICON_OBJBROWSER_WIRE" );
239     aShapeName = "Wire_";
240   } else if ( aShape->GetShapeType() == GEOM::EDGE ) {
241     aPixmap->SetPixMap( "ICON_OBJBROWSER_EDGE" );
242     aShapeName = "Edge_";
243   } else if ( aShape->GetShapeType() == GEOM::VERTEX ) {
244     aPixmap->SetPixMap( "ICON_OBJBROWSER_VERTEX" );
245     aShapeName = "Vertex_";
246   }
247   //if (strlen(theName) == 0) aShapeName += TCollection_AsciiString(aResultSO->Tag());
248   //else aShapeName = TCollection_AsciiString(CORBA::string_dup(theName));
249
250   // asv : 11.11.04 Introducing a more sofisticated method of name creation, just as
251   //       it is done in GUI in GEOMBase::GetDefaultName() - not just add a Tag() == number
252   //       of objects in the study, but compute a number of objects with the same prefix
253   //       and build a new name as Prefix_N+1
254   if ( strlen( theName ) == 0 ) { // MOST PROBABLY CALLED FROM BATCHMODE OR SUPERVISOR
255     int i = 0;                    // (WITH EMPTY NEW NAME)
256     SALOMEDS::SObject_var obj;
257     TCollection_AsciiString aNewShapeName;
258     do {
259       aNewShapeName = aShapeName + TCollection_AsciiString(++i);
260       obj = theStudy->FindObject( aNewShapeName.ToCString() );
261     }
262     while ( !obj->_is_nil() );
263     aShapeName = aNewShapeName;
264   }
265   else // MOST PROBABLY CALLED FROM GEOM GUI (ALREADY WITH VALID NAME)
266     aShapeName = TCollection_AsciiString((char*)theName);
267
268   //Set the study entry as a name of  the published GEOM_Object
269   aShape->SetStudyEntry(aResultSO->GetID());
270
271   //Set a name of the added shape
272   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeName");
273   SALOMEDS::AttributeName_var aNameAttrib = SALOMEDS::AttributeName::_narrow(anAttr);
274   aNameAttrib->SetValue(aShapeName.ToCString());
275
276   //Set NoteBook variables used in the object creation
277   TCollection_AsciiString aParams(aShape->GetParameters());
278   if(!aParams.IsEmpty()) {
279     TCollection_AsciiString aVars;
280     int nbVars = 0;
281     int n = 1;
282     TCollection_AsciiString aParam = aParams.Token(":",1);
283     while( aParam.Length() != 0 ) {
284       aParam = aParams.Token(":",n);
285       if(theStudy->IsVariable(aParam.ToCString())){
286         aVars+=aParam;
287         nbVars++;
288       }
289       if(aParam.Length() == 0)
290         break;
291       aVars+=":";
292       n++;
293     }
294     aVars.Remove(aVars.Length(),1);
295     if(nbVars > 0 ) {
296       anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeString");
297       SALOMEDS::AttributeString_var aStringAttrib = SALOMEDS::AttributeString::_narrow(anAttr);
298       aStringAttrib->SetValue(aVars.ToCString());
299     }
300   }
301   
302   //Set a name of the GEOM object
303   aShape->SetName(theName);
304
305   return aResultSO._retn();
306 }
307
308
309 //============================================================================
310 // function : Save()
311 // purpose  : save OCAF/Geom document
312 //============================================================================
313 SALOMEDS::TMPFile* GEOM_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
314                                     const char* theURL,
315                                     bool isMultiFile) {
316   SALOMEDS::TMPFile_var aStreamFile;
317   // Get a temporary directory to store a file
318   std::string aTmpDir = (isMultiFile)?theURL:SALOMEDS_Tool::GetTmpDir();
319
320   // OCCT BUG: cannot save a document (in current folder)
321   // if directory name is empty
322   if (aTmpDir.size() == 0) {
323 #ifdef WNT
324     aTmpDir = ".\\";
325 #else
326     aTmpDir = "./";
327 #endif
328   }
329
330   // Create a list to store names of created files
331   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
332   aSeq->length(1);
333   // Prepare a file name to open
334   TCollection_AsciiString aNameWithExt("");
335   if (isMultiFile)
336     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL())).c_str());
337   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
338   aSeq[0] = CORBA::string_dup(aNameWithExt.ToCString());
339   // Build a full file name of temporary file
340   TCollection_AsciiString aFullName = TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt;
341   // Save GEOM component in this file
342   _impl->Save(theComponent->GetStudy()->StudyId(),(char*) aFullName.ToCString());
343   // Conver a file to the byte stream
344   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
345   // Remove the created file and tmp directory
346   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
347
348   // Return the created byte stream
349   return aStreamFile._retn();
350 }
351
352
353 //============================================================================
354 // function : SaveASCII()
355 // purpose  :
356 //============================================================================
357 SALOMEDS::TMPFile* GEOM_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
358                                          const char* theURL,
359                                          bool isMultiFile) {
360   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
361   return aStreamFile._retn();
362 }
363
364
365 //============================================================================
366 // function : Load()
367 // purpose  :
368 //============================================================================
369 CORBA::Boolean GEOM_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
370                                 const SALOMEDS::TMPFile& theStream,
371                                 const char* theURL,
372                                 bool isMultiFile) {
373
374   if (theStream.length() <= 9) {
375     MESSAGE("The TMPFile is too short : " << theStream.length() << " bytes ");
376     return false;
377   }
378
379   // Get a temporary directory for a file
380   std::string aTmpDir = isMultiFile?theURL:SALOMEDS_Tool::GetTmpDir();
381
382   // OCCT BUG: cannot load a document (from current folder)
383   // if directory name is empty
384   if (aTmpDir.size() == 0) {
385 #ifdef WNT
386     aTmpDir = ".\\";
387 #else
388     aTmpDir = "./";
389 #endif
390   }
391
392   // Conver the byte stream theStream to a file and place it in tmp directory
393   SALOMEDS::ListOfFileNames_var aSeq =
394     SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.c_str(), isMultiFile);
395
396   // Prepare a file name to open
397   TCollection_AsciiString aNameWithExt("");
398   if (isMultiFile)
399     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL())).c_str());
400   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
401   TCollection_AsciiString aFullName = (TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt);
402
403   // Open document
404   if (!_impl->Load(theComponent->GetStudy()->StudyId(),(char*) aFullName.ToCString())) return false;
405
406   // Remove the created file and tmp directory
407   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
408
409   SALOMEDS::Study_var Study = theComponent->GetStudy();
410   TCollection_AsciiString name (Study->Name());
411
412   return true;
413 }
414
415
416 //============================================================================
417 // function : LoadASCII()
418 // purpose  :
419 //============================================================================
420 CORBA::Boolean GEOM_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
421                                      const SALOMEDS::TMPFile& theStream,
422                                      const char* theURL,
423                                      bool isMultiFile) {
424   return Load(theComponent, theStream, theURL, isMultiFile);
425 }
426
427
428 //============================================================================
429 // function : Close()
430 // purpose  :
431 //============================================================================
432 void GEOM_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
433 {
434   _impl->Close(theComponent->GetStudy()->StudyId());
435 }
436
437 //============================================================================
438 // function : CanCopy()
439 // purpose  :
440 //============================================================================
441 CORBA::Boolean GEOM_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
442   // Try to retrieve known by Geometry component GEOM_Object by given IOR
443   SALOMEDS::GenericAttribute_var anAttr;
444   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
445
446   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
447
448   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(_orb->string_to_object(anIOR->Value()));
449   // If the object is null one it can't be copied: return false
450   if (anObject->_is_nil()) return false;
451   return true;
452 }
453
454 //============================================================================
455 // function : CopyFrom()
456 // purpose  :
457 //============================================================================
458 SALOMEDS::TMPFile* GEOM_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
459   // Declare a sequence of the byte to store the copied object
460   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
461
462   // Try to get GEOM_Object object by given SObject
463   SALOMEDS::GenericAttribute_var anAttr;
464   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return aStreamFile._retn();
465   GEOM::GEOM_Object_var anObject =  GEOM::GEOM_Object::_narrow(_orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()));
466   if (anObject->_is_nil()) return aStreamFile._retn();
467
468   aStreamFile = anObject->GetShapeStream();
469
470   // Assign an ID  the type of  GEOM_Object
471   theObjectID = anObject->GetType();
472
473   // Return created TMPFile
474   return aStreamFile._retn();
475 }
476
477 //============================================================================
478 // function : CanPaste()
479 // purpose  :
480 //============================================================================
481 CORBA::Boolean GEOM_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
482   // The Geometry component can paste only objects copied by Geometry component
483   // and with the object type = 1
484   if (strcmp(theComponentName, ComponentDataType()) != 0) return false;
485   return true;
486 }
487
488 //============================================================================
489 // function : PasteInto()
490 // purpose  :
491 //============================================================================
492 SALOMEDS::SObject_ptr GEOM_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
493                                             CORBA::Long theObjectID,
494                                             SALOMEDS::SObject_ptr theObject) {
495   // Find the current Study and StudyBuilder
496   SALOMEDS::Study_var aStudy = theObject->GetStudy();
497   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
498
499   // Retrieve a TopoDS_Shape from byte stream
500   TopoDS_Shape aTopology;
501   istrstream aStreamedBrep((char*) &theStream[0], theStream.length());
502   BRep_Builder aBuilder;
503   try {
504     BRepTools::Read(aTopology, aStreamedBrep, aBuilder);
505   } catch (Standard_Failure) {
506     return false;
507   }
508
509   // SObject of the created shape is theObject or new Child of Component if theObject == geom component
510   SALOMEDS::SObject_var aNewSO;
511   if (strcmp(theObject->GetFatherComponent()->GetID(),theObject->GetID()) == 0) {
512     aNewSO = aStudyBuilder->NewObject(theObject);
513   } else aNewSO = SALOMEDS::SObject::_duplicate(theObject);
514
515
516   //Create a new GEOM_Object
517   Handle(GEOM_Object) anObj = _impl->AddObject(aNewSO->GetStudy()->StudyId(), theObjectID);
518   Handle(GEOM_Function) aFunction = anObj->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
519   aFunction->SetValue(aTopology);
520
521   TCollection_AsciiString anEntry;
522   TDF_Tool::Entry(anObj->GetEntry(), anEntry);
523   GEOM::GEOM_Object_var obj = GetObject(anObj->GetDocID(), anEntry.ToCString());
524
525   //Set the study entry of the published GEOM_Object
526   obj->SetStudyEntry(aNewSO->GetID());
527
528   // Add IORAttribute to the Study and set IOR of the created GEOM_Object to it
529   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSO, "AttributeIOR");
530   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
531   CORBA::String_var objStr = _orb->object_to_string(obj);
532   anIOR->SetValue(objStr.in());
533
534   // Return the created in the Study SObject
535   return aNewSO._retn();
536 }
537
538 //============================================================================
539 // function : ComponentDataType()
540 // purpose  :
541 //============================================================================
542 char* GEOM_Gen_i::ComponentDataType()
543 {
544   return CORBA::string_dup("GEOM");
545 }
546
547 //============================================================================
548 // function : AddInStudy
549 // purpose  :
550 //============================================================================
551 SALOMEDS::SObject_ptr GEOM_Gen_i::AddInStudy (SALOMEDS::Study_ptr theStudy,
552                                               GEOM::GEOM_Object_ptr theObject,
553                                               const char* theName,
554                                               GEOM::GEOM_Object_ptr theFather)
555 {
556   SALOMEDS::SObject_var aResultSO;
557   if(theObject->_is_nil() || theStudy->_is_nil()) return aResultSO;
558
559   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
560   CORBA::String_var IOR;
561
562   if(!theFather->_is_nil()) {
563     IOR = _orb->object_to_string(theFather);
564     SALOMEDS::SObject_var aFatherSO = theStudy->FindObjectIOR(IOR.in());
565     if(aFatherSO->_is_nil()) return aResultSO._retn();
566     aResultSO = aStudyBuilder->NewObject(aFatherSO);
567     //aStudyBuilder->Addreference(aResultSO, aResultSO);
568   }
569
570   aResultSO = PublishInStudy(theStudy, aResultSO, theObject, theName);
571   if(aResultSO->_is_nil()) return aResultSO._retn();
572
573   GEOM::ListOfGO_var aList = theObject->GetDependency();
574   Standard_Integer aLength = aList->length();
575   if(aLength < 1) return aResultSO._retn();
576
577   //Publish the arguments
578   for(Standard_Integer i = 0; i< aLength; i++) {
579     GEOM::GEOM_Object_var anObject = aList[i];
580     if(anObject->_is_nil()) continue;
581     IOR = _orb->object_to_string(anObject);
582     SALOMEDS::SObject_var aSO =  theStudy->FindObjectIOR(IOR.in());
583     if(aSO->_is_nil()) continue;
584     SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(aResultSO);
585     aStudyBuilder->Addreference(aSubSO, aSO);
586   }
587
588   return aResultSO._retn();
589 }
590
591 //============================================================================
592 // function : RestoreSubShapesO
593 // purpose  : Publish sub-shapes, standing for arguments and sub-shapes of arguments.
594 //            To be used from python scripts out of geompy.addToStudy (non-default usage)
595 //============================================================================
596 CORBA::Boolean GEOM_Gen_i::RestoreSubShapesO (SALOMEDS::Study_ptr     theStudy,
597                                               GEOM::GEOM_Object_ptr   theObject,
598                                               const GEOM::ListOfGO&   theArgs,
599                                               GEOM::find_shape_method theFindMethod,
600                                               CORBA::Boolean          theInheritFirstArg)
601 {
602   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject))
603     return false;
604
605   // find SObject in the study
606   CORBA::String_var anIORo = _orb->object_to_string(theObject);
607   SALOMEDS::SObject_var aSO = theStudy->FindObjectIOR(anIORo.in());
608   if (CORBA::is_nil(aSO))
609     return false;
610
611   return RestoreSubShapes(theStudy, theObject, aSO, theArgs, theFindMethod, theInheritFirstArg);
612 }
613
614 //============================================================================
615 // function : RestoreSubShapesSO
616 // purpose  : Publish sub-shapes, standing for arguments and sub-shapes of arguments.
617 //            To be used from GUI and from geompy.addToStudy
618 //============================================================================
619 CORBA::Boolean GEOM_Gen_i::RestoreSubShapesSO (SALOMEDS::Study_ptr     theStudy,
620                                                SALOMEDS::SObject_ptr   theSObject,
621                                                const GEOM::ListOfGO&   theArgs,
622                                                GEOM::find_shape_method theFindMethod,
623                                                CORBA::Boolean          theInheritFirstArg)
624 {
625   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theSObject))
626     return false;
627
628   SALOMEDS::GenericAttribute_var anAttr;
629   if (!theSObject->FindAttribute(anAttr, "AttributeIOR"))
630     return false;
631
632   SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
633   CORBA::String_var anIORso = anAttrIOR->Value();
634
635   // get Object from SObject
636   GEOM::GEOM_Object_var anO = GEOM::GEOM_Object::_narrow(_orb->string_to_object(anIORso));
637   if (CORBA::is_nil(anO))
638     return false;
639
640   return RestoreSubShapes(theStudy, anO, theSObject, theArgs, theFindMethod, theInheritFirstArg);
641 }
642
643 //============================================================================
644 // function : RestoreSubShapes
645 // purpose  : Private method. Works only if both theObject and theSObject
646 //            are defined, and does not check, if they correspond to each other.
647 //============================================================================
648 CORBA::Boolean GEOM_Gen_i::RestoreSubShapes (SALOMEDS::Study_ptr     theStudy,
649                                              GEOM::GEOM_Object_ptr   theObject,
650                                              SALOMEDS::SObject_ptr   theSObject,
651                                              const GEOM::ListOfGO&   theArgs,
652                                              GEOM::find_shape_method theFindMethod,
653                                              CORBA::Boolean          theInheritFirstArg)
654 {
655   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject) || CORBA::is_nil(theSObject))
656     return false;
657
658   // Arguments to be published
659   GEOM::ListOfGO_var aList;
660
661   // If theArgs list is empty, we try to publish all arguments,
662   // otherwise publish only passed args
663   Standard_Integer nbArgsActual = -1; // -1 means unknown
664   Standard_Integer aLength = theArgs.length();
665   if (aLength > 0) {
666     aList = new GEOM::ListOfGO;
667     aList->length(aLength);
668     for (int i = 0; i < aLength; i++) {
669       aList[i] = theArgs[i];
670     }
671   }
672   else {
673     // Get all arguments
674     aList = theObject->GetDependency();
675     aLength = aList->length();
676     nbArgsActual = aLength;
677   }
678
679   if (aLength < 1)
680     return false;
681
682   if (theInheritFirstArg || (nbArgsActual == 1)) {
683     // Do not publish argument's reflection,
684     // but only reconstruct its published sub-shapes
685
686     GEOM::GEOM_Object_var anArgO = aList[0];
687     CORBA::String_var anIOR = _orb->object_to_string(anArgO);
688     SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
689
690     GEOM::ListOfGO_var aParts =
691       RestoreSubShapesOneLevel(theStudy, anArgSO, theSObject, theObject, theFindMethod);
692
693     // set the color of the transformed shape to the color of initial shape
694     theObject->SetColor(aList[0]->GetColor());
695
696     return (aParts->length() > 0);
697   }
698
699   // Get interface, containing method, which we will use to reconstruct sub-shapes
700   GEOM::GEOM_IShapesOperations_var aShapesOp = GetIShapesOperations(theStudy->StudyId());
701   GEOM::GEOM_IGroupOperations_var  aGroupOp  = GetIGroupOperations(theStudy->StudyId());
702
703   // Reconstruct arguments and tree of sub-shapes of the arguments
704   CORBA::String_var anIOR;
705   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
706   for (Standard_Integer i = 0; i < aLength; i++)
707   {
708     GEOM::GEOM_Object_var anArgO = aList[i];
709     if (!CORBA::is_nil(anArgO)) {
710       anIOR = _orb->object_to_string(anArgO);
711       SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
712       TCollection_AsciiString anArgName;
713       if (CORBA::is_nil(anArgSO)) {
714         anArgName = "arg_";
715         anArgName += TCollection_AsciiString(i);
716       }
717       else {
718         anArgName = anArgSO->GetName();
719       }
720
721       // Find a sub-shape of theObject in place of the argument
722       GEOM::GEOM_Object_var aSubO;
723       switch (theFindMethod) {
724       case GEOM::FSM_GetInPlace:
725         {
726           // Use GetInPlace
727           aSubO = aShapesOp->GetInPlace(theObject, anArgO);
728         }
729         break;
730       case GEOM::FSM_Transformed:
731         {
732           // transformation, cannot use GetInPlace, operate with indices
733           GEOM::ListOfLong_var anIDs = anArgO->GetSubShapeIndices();
734           if (anIDs->length() > 1) {
735             // group
736             aSubO = aGroupOp->CreateGroup(theObject, aGroupOp->GetType(anArgO));
737             if (!CORBA::is_nil(aSubO))
738               aGroupOp->UnionIDs(aSubO, anIDs);
739           }
740           else {
741             // single sub-shape
742             aSubO = aShapesOp->GetSubShape(theObject, anIDs[0]);
743           }
744         }
745         break;
746       case GEOM::FSM_GetSame:
747         {
748           // Use GetSame
749           aSubO = aShapesOp->GetSame(theObject, anArgO);
750         }
751         break;
752       case GEOM::FSM_GetShapesOnShape:
753         {
754           // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
755           aSubO = aShapesOp->GetShapesOnShapeAsCompound(anArgO, theObject,
756                                                         (short)GEOM::SOLID, GEOM::ST_ONIN);
757         }
758         break;
759       case GEOM::FSM_GetInPlaceByHistory:
760         {
761           // Use GetInPlaceByHistory
762           aSubO = aShapesOp->GetInPlaceByHistory(theObject, anArgO);
763         }
764         break;
765       default:
766         {}
767       }
768
769       if (!CORBA::is_nil(aSubO)) {
770         // Publish the sub-shape
771         TCollection_AsciiString aSubName ("from_");
772         aSubName += anArgName;
773         SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(theSObject);
774         aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
775         // Restore color
776         aSubO->SetColor(anArgO->GetColor());
777
778         if (!CORBA::is_nil(anArgSO)) {
779           // Restore published sub-shapes of the argument
780           if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
781             // pass theObject, because only it has the history
782             RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO, theObject, theFindMethod);
783           else
784             RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO, aSubO, theFindMethod);
785         }
786       }
787       else { // GetInPlace failed, try to build from published parts
788         if (!CORBA::is_nil(anArgSO)) {
789           SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(theSObject);
790
791           // Restore published sub-shapes of the argument
792           GEOM::ListOfGO_var aParts =
793             RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO, theObject, theFindMethod);
794
795           if (aParts->length() > 0) {
796             // try to build an argument from a set of its sub-shapes,
797             // that published and will be reconstructed
798             if (aParts->length() > 1) {
799               aSubO = aShapesOp->MakeCompound(aParts);
800             }
801             else {
802               aSubO = aParts[0];
803             }
804             if (!CORBA::is_nil(aSubO)) {
805               // Publish the sub-shape
806               TCollection_AsciiString aSubName ("from_parts_of_");
807               aSubName += anArgName;
808               aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
809               // Restore color
810               aSubO->SetColor(anArgO->GetColor());
811             }
812           }
813           else {
814             // remove created aSubSO, because no parts have been found
815             aStudyBuilder->RemoveObject(aSubSO);
816           }
817         }
818       } // try to build from published parts
819     }
820   } // process arguments
821
822   return true;
823 }
824
825 //============================================================================
826 // function : RestoreSubShapesOneLevel
827 // purpose  : Private method
828 //============================================================================
829 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapesOneLevel (SALOMEDS::Study_ptr     theStudy,
830                                                       SALOMEDS::SObject_ptr   theOldSO,
831                                                       SALOMEDS::SObject_ptr   theNewSO,
832                                                       GEOM::GEOM_Object_ptr   theNewO,
833                                                       GEOM::find_shape_method theFindMethod)
834 {
835   int i = 0;
836   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
837   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theOldSO) ||
838       CORBA::is_nil(theNewO) || CORBA::is_nil(theNewSO))
839     return aParts._retn();
840
841   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
842
843   // Get interface, containing method, which we will use to reconstruct sub-shapes
844   GEOM::GEOM_IShapesOperations_var aShapesOp = GetIShapesOperations(theStudy->StudyId());
845   GEOM::GEOM_IGroupOperations_var  aGroupOp  = GetIGroupOperations(theStudy->StudyId());
846
847   // Reconstruct published sub-shapes
848   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator(theOldSO);
849
850   int aLen = 0;
851   for (it->Init(); it->More(); it->Next()) {
852     aLen++;
853   }
854   aParts->length(aLen);
855
856   for (it->Init(); it->More(); it->Next()) {
857     SALOMEDS::SObject_var anOldSubSO = it->Value();
858
859     TCollection_AsciiString anArgName = anOldSubSO->GetName();
860
861     SALOMEDS::GenericAttribute_var anAttr;
862     if (anOldSubSO->FindAttribute(anAttr, "AttributeIOR")) {
863       SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
864       GEOM::GEOM_Object_var anOldSubO =
865         GEOM::GEOM_Object::_narrow(_orb->string_to_object(anAttrIOR->Value()));
866       if (!CORBA::is_nil(anOldSubO)) {
867         // Find a sub-shape of theNewO in place of anOldSubO
868         GEOM::GEOM_Object_var aNewSubO;
869         switch (theFindMethod) {
870         case GEOM::FSM_GetInPlace:
871           {
872             // Use GetInPlace
873             aNewSubO = aShapesOp->GetInPlace(theNewO, anOldSubO);
874           }
875           break;
876         case GEOM::FSM_Transformed:
877           {
878             // transformation, cannot use GetInPlace, operate with indices
879             GEOM::ListOfLong_var anIDs = anOldSubO->GetSubShapeIndices();
880             if (anIDs->length() > 1) {
881               // group
882               aNewSubO = aGroupOp->CreateGroup(theNewO, aGroupOp->GetType(anOldSubO));
883               if (!CORBA::is_nil(aNewSubO))
884                 aGroupOp->UnionIDs(aNewSubO, anIDs);
885             }
886             else {
887               // single sub-shape
888               aNewSubO = aShapesOp->GetSubShape(theNewO, anIDs[0]);
889             }
890           }
891           break;
892         case GEOM::FSM_GetSame:
893           {
894             // Use GetSame
895             aNewSubO = aShapesOp->GetSame(theNewO, anOldSubO);
896           }
897           break;
898         case GEOM::FSM_GetShapesOnShape:
899           {
900             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
901             aNewSubO = aShapesOp->GetShapesOnShapeAsCompound(anOldSubO, theNewO,
902                                                              (short)GEOM::SOLID, GEOM::ST_ONIN);
903           }
904           break;
905         case GEOM::FSM_GetInPlaceByHistory:
906           {
907             // Use GetInPlaceByHistory
908             aNewSubO = aShapesOp->GetInPlaceByHistory(theNewO, anOldSubO);
909           }
910           break;
911         default:
912           {}
913         }
914
915         if (!CORBA::is_nil(aNewSubO)) {
916           // add the part to the list
917           aParts[i] = aNewSubO;
918           i++;
919
920           // Publish the sub-shape
921           TCollection_AsciiString aSubName ("from_");
922           aSubName += anArgName;
923           SALOMEDS::SObject_var aNewSubSO = aStudyBuilder->NewObject(theNewSO);
924           aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
925           // Restore color
926           aNewSubO->SetColor(anOldSubO->GetColor());
927
928           // Restore published sub-shapes of the argument
929           if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
930             // pass the main shape as Object, because only it has the history
931             RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO, theNewO, theFindMethod);
932           else
933             RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO, aNewSubO, theFindMethod);
934         }
935         else { // GetInPlace failed, try to build from published parts
936           SALOMEDS::SObject_var aNewSubSO = aStudyBuilder->NewObject(theNewSO);
937
938           // Restore published sub-shapes of the argument
939           GEOM::ListOfGO_var aParts =
940             RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO, theNewO, theFindMethod);
941
942           if (aParts->length() > 0) {
943             // try to build an object from a set of its sub-shapes,
944             // that published and will be reconstructed
945             if (aParts->length() > 1) {
946               aNewSubO = aShapesOp->MakeCompound(aParts);
947             }
948             else {
949               aNewSubO = aParts[0];
950             }
951
952             if (!CORBA::is_nil(aNewSubO)) {
953               // add the part to the list
954               aParts[i] = aNewSubO;
955               i++;
956
957               // Publish the sub-shape
958               TCollection_AsciiString aSubName = "from_parts_of_";
959               aSubName += anArgName;
960               aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
961               // Restore color
962               aNewSubO->SetColor(anOldSubO->GetColor());
963             }
964           }
965           else {
966             // remove created aSubSO, because no parts have been found
967             aStudyBuilder->RemoveObject(aNewSubSO);
968           }
969         } // try to build from published parts
970       }
971     }
972   } // iterate on published sub-shapes
973
974   aParts->length(i);
975   return aParts._retn();
976 }
977
978 //============================================================================
979 // function : register()
980 // purpose  : register 'name' in 'name_service'
981 //============================================================================
982 void GEOM_Gen_i::register_name(char * name)
983 {
984   GEOM::GEOM_Gen_var g = _this();
985   name_service->Register(g, name);
986 }
987
988 //============================================================================
989 // function : Undo
990 // purpose  :
991 //============================================================================
992 void GEOM_Gen_i::Undo(CORBA::Long theStudyID)
993 {
994   _impl->Undo(theStudyID);
995 }
996
997 //============================================================================
998 // function : Redo
999 // purpose  :
1000 //============================================================================
1001 void GEOM_Gen_i::Redo(CORBA::Long theStudyID)
1002 {
1003   _impl->Redo(theStudyID);
1004 }
1005
1006 //============================================================================
1007 // function : GetIBasicOperations
1008 // purpose  :
1009 //============================================================================
1010 GEOM::GEOM_IBasicOperations_ptr GEOM_Gen_i::GetIBasicOperations(CORBA::Long theStudyID)
1011      throw ( SALOME::SALOME_Exception )
1012 {
1013   Unexpect aCatch(SALOME_SalomeException);
1014   MESSAGE( "GEOM_Gen_i::GetIBasicOperations" );
1015
1016   GEOM::GEOM_Gen_ptr engine = _this();
1017
1018   //transfer reference on engine
1019   GEOM_IBasicOperations_i* aServant =
1020     new GEOM_IBasicOperations_i(_poa, engine, _impl->GetIBasicOperations(theStudyID));
1021
1022   // activate the CORBA servant
1023   GEOM::GEOM_IBasicOperations_var operations = aServant->_this();
1024   return operations._retn();
1025 }
1026
1027 //============================================================================
1028 // function : GetITransformOperations
1029 // purpose  :
1030 //============================================================================
1031 GEOM::GEOM_ITransformOperations_ptr GEOM_Gen_i::GetITransformOperations(CORBA::Long theStudyID)
1032      throw ( SALOME::SALOME_Exception )
1033 {
1034   Unexpect aCatch(SALOME_SalomeException);
1035   MESSAGE( "GEOM_Gen_i::GetITransformOperations" );
1036
1037   GEOM::GEOM_Gen_ptr engine = _this();
1038
1039   GEOM_ITransformOperations_i* aServant =
1040     new GEOM_ITransformOperations_i(_poa, engine, _impl->GetITransformOperations(theStudyID));
1041
1042   // activate the CORBA servant
1043   GEOM::GEOM_ITransformOperations_var operations = aServant->_this();
1044   return operations._retn();
1045 }
1046
1047 //============================================================================
1048 // function : GetI3DPrimOperations
1049 // purpose  :
1050 //============================================================================
1051 GEOM::GEOM_I3DPrimOperations_ptr GEOM_Gen_i::GetI3DPrimOperations(CORBA::Long theStudyID)
1052      throw ( SALOME::SALOME_Exception )
1053 {
1054   Unexpect aCatch(SALOME_SalomeException);
1055   MESSAGE( "GEOM_Gen_i::GetI3DPrimOperations" );
1056
1057   GEOM::GEOM_Gen_ptr engine = _this();
1058
1059   GEOM_I3DPrimOperations_i* aServant =
1060     new GEOM_I3DPrimOperations_i(_poa, engine, _impl->GetI3DPrimOperations(theStudyID));
1061
1062   // activate the CORBA servant
1063   GEOM::GEOM_I3DPrimOperations_var operations = aServant->_this();
1064   return operations._retn();
1065 }
1066
1067 //============================================================================
1068 // function : GetIShapesOperations
1069 // purpose  :
1070 //============================================================================
1071 GEOM::GEOM_IShapesOperations_ptr GEOM_Gen_i::GetIShapesOperations(CORBA::Long theStudyID)
1072      throw ( SALOME::SALOME_Exception )
1073 {
1074   Unexpect aCatch(SALOME_SalomeException);
1075   MESSAGE( "GEOM_Gen_i::GetIShapesOperations" );
1076
1077   GEOM::GEOM_Gen_ptr engine = _this();
1078
1079   GEOM_IShapesOperations_i* aServant =
1080     new GEOM_IShapesOperations_i(_poa, engine, _impl->GetIShapesOperations(theStudyID));
1081
1082   // activate the CORBA servant
1083   GEOM::GEOM_IShapesOperations_var operations = aServant->_this();
1084   return operations._retn();
1085 }
1086
1087 //============================================================================
1088 // function : GetIBlocksOperations
1089 // purpose  :
1090 //============================================================================
1091 GEOM::GEOM_IBlocksOperations_ptr GEOM_Gen_i::GetIBlocksOperations(CORBA::Long theStudyID)
1092      throw ( SALOME::SALOME_Exception )
1093 {
1094   Unexpect aCatch(SALOME_SalomeException);
1095   MESSAGE( "GEOM_Gen_i::GetIBlocksOperations" );
1096
1097   GEOM::GEOM_Gen_ptr engine = _this();
1098
1099   GEOM_IBlocksOperations_i* aServant =
1100     new GEOM_IBlocksOperations_i(_poa, engine, _impl->GetIBlocksOperations(theStudyID));
1101
1102   // activate the CORBA servant
1103   GEOM::GEOM_IBlocksOperations_var operations = aServant->_this();
1104   return operations._retn();
1105 }
1106
1107 //============================================================================
1108 // function : GetIBooleanOperations
1109 // purpose  :
1110 //============================================================================
1111 GEOM::GEOM_IBooleanOperations_ptr GEOM_Gen_i::GetIBooleanOperations(CORBA::Long theStudyID)
1112      throw ( SALOME::SALOME_Exception )
1113 {
1114   Unexpect aCatch(SALOME_SalomeException);
1115   MESSAGE( "GEOM_Gen_i::GetIBooleanOperations" );
1116
1117   GEOM::GEOM_Gen_ptr engine = _this();
1118
1119   GEOM_IBooleanOperations_i* aServant =
1120     new GEOM_IBooleanOperations_i(_poa, engine, _impl->GetIBooleanOperations(theStudyID));
1121
1122   // activate the CORBA servant
1123   GEOM::GEOM_IBooleanOperations_var operations = aServant->_this();
1124   return operations._retn();
1125 }
1126
1127 //============================================================================
1128 // function : GetICurvesOperations
1129 // purpose  :
1130 //============================================================================
1131 GEOM::GEOM_ICurvesOperations_ptr GEOM_Gen_i::GetICurvesOperations(CORBA::Long theStudyID)
1132      throw ( SALOME::SALOME_Exception )
1133 {
1134   Unexpect aCatch(SALOME_SalomeException);
1135   MESSAGE( "GEOM_Gen_i::GetICurvesOperations" );
1136
1137   GEOM::GEOM_Gen_ptr engine = _this();
1138
1139   GEOM_ICurvesOperations_i* aServant =
1140     new GEOM_ICurvesOperations_i(_poa, engine, _impl->GetICurvesOperations(theStudyID));
1141
1142   // activate the CORBA servant
1143   GEOM::GEOM_ICurvesOperations_var operations = aServant->_this();
1144   return operations._retn();
1145 }
1146
1147 //============================================================================
1148 // function : GetILocalOperations
1149 // purpose  :
1150 //============================================================================
1151 GEOM::GEOM_ILocalOperations_ptr GEOM_Gen_i::GetILocalOperations(CORBA::Long theStudyID)
1152      throw ( SALOME::SALOME_Exception )
1153 {
1154   Unexpect aCatch(SALOME_SalomeException);
1155   MESSAGE( "GEOM_Gen_i::GetILocalOperations" );
1156
1157   GEOM::GEOM_Gen_ptr engine = _this();
1158
1159   GEOM_ILocalOperations_i* aServant =
1160     new GEOM_ILocalOperations_i(_poa, engine, _impl->GetILocalOperations(theStudyID));
1161
1162   // activate the CORBA servant
1163   GEOM::GEOM_ILocalOperations_var operations = aServant->_this();
1164   return operations._retn();
1165 }
1166
1167 //============================================================================
1168 // function : GetIHealingOperations
1169 // purpose  :
1170 //============================================================================
1171 GEOM::GEOM_IHealingOperations_ptr GEOM_Gen_i::GetIHealingOperations(CORBA::Long theStudyID)
1172      throw ( SALOME::SALOME_Exception )
1173 {
1174   Unexpect aCatch(SALOME_SalomeException);
1175   MESSAGE( "GEOM_Gen_i::IHealingOperations" );
1176
1177   GEOM::GEOM_Gen_ptr engine = _this();
1178
1179   GEOM_IHealingOperations_i* aServant =
1180     new GEOM_IHealingOperations_i(_poa, engine, _impl->GetIHealingOperations(theStudyID));
1181
1182   // activate the CORBA servant
1183   GEOM::GEOM_IHealingOperations_var operations = aServant->_this();
1184   return operations._retn();
1185 }
1186
1187 //============================================================================
1188 // function : GetIInsertOperations
1189 // purpose  :
1190 //============================================================================
1191 GEOM::GEOM_IInsertOperations_ptr GEOM_Gen_i::GetIInsertOperations(CORBA::Long theStudyID)
1192      throw ( SALOME::SALOME_Exception )
1193 {
1194   Unexpect aCatch(SALOME_SalomeException);
1195   MESSAGE( "GEOM_Gen_i::GetIInsertOperations" );
1196
1197   GEOM::GEOM_Gen_ptr engine = _this();
1198
1199   GEOM_IInsertOperations_i* aServant =
1200     new GEOM_IInsertOperations_i(_poa, engine, _impl->GetIInsertOperations(theStudyID));
1201
1202   // activate the CORBA servant
1203   GEOM::GEOM_IInsertOperations_var operations = aServant->_this();
1204   return operations._retn();
1205 }
1206
1207 //============================================================================
1208 // function : GetIMeasureOperations
1209 // purpose  :
1210 //============================================================================
1211 GEOM::GEOM_IMeasureOperations_ptr GEOM_Gen_i::GetIMeasureOperations(CORBA::Long theStudyID)
1212      throw ( SALOME::SALOME_Exception )
1213 {
1214   Unexpect aCatch(SALOME_SalomeException);
1215   MESSAGE( "GEOM_Gen_i::GetIMeasureOperations" );
1216
1217   GEOM::GEOM_Gen_ptr engine = _this();
1218
1219   GEOM_IMeasureOperations_i* aServant =
1220     new GEOM_IMeasureOperations_i(_poa, engine, _impl->GetIMeasureOperations(theStudyID));
1221
1222   // activate the CORBA servant
1223   GEOM::GEOM_IMeasureOperations_var operations = aServant->_this();
1224   return operations._retn();
1225 }
1226
1227 //============================================================================
1228 // function : GetIGroupOperations
1229 // purpose  :
1230 //============================================================================
1231 GEOM::GEOM_IGroupOperations_ptr GEOM_Gen_i::GetIGroupOperations(CORBA::Long theStudyID)
1232      throw ( SALOME::SALOME_Exception )
1233 {
1234   Unexpect aCatch(SALOME_SalomeException);
1235   MESSAGE( "GEOM_Gen_i::GetIGroupOperations" );
1236
1237   GEOM::GEOM_Gen_ptr engine = _this();
1238
1239   GEOM_IGroupOperations_i* aServant =
1240     new GEOM_IGroupOperations_i(_poa, engine, _impl->GetIGroupOperations(theStudyID));
1241
1242   // activate the CORBA servant
1243   GEOM::GEOM_IGroupOperations_var operations = aServant->_this();
1244   return operations._retn();
1245 }
1246
1247 //=============================================================================
1248 /*!
1249  *  AddSubShape
1250  */
1251 //=============================================================================
1252 GEOM::GEOM_Object_ptr GEOM_Gen_i::AddSubShape (GEOM::GEOM_Object_ptr theMainShape,
1253                                                const GEOM::ListOfLong& theIndices)
1254 {
1255   if(theMainShape == NULL || theIndices.length() < 1) return GEOM::GEOM_Object::_nil();
1256   Handle(GEOM_Object) aMainsShape = _impl->GetObject(theMainShape->GetStudyID(), theMainShape->GetEntry());
1257   if (aMainsShape.IsNull()) return GEOM::GEOM_Object::_nil();
1258
1259   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1, theIndices.length());
1260   for(Standard_Integer i = 0; i<theIndices.length(); i++) anArray->SetValue(i+1, theIndices[i]);
1261
1262   Handle(GEOM_Object) anObject = _impl->AddSubShape(aMainsShape, anArray, true);
1263   if(anObject.IsNull()) return GEOM::GEOM_Object::_nil();
1264
1265   TCollection_AsciiString anEntry;
1266   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
1267   return GetObject(anObject->GetDocID(), anEntry.ToCString());
1268 }
1269
1270 //=============================================================================
1271 /*!
1272  *  RemoveObject
1273  */
1274 //=============================================================================
1275 void GEOM_Gen_i::RemoveObject(GEOM::GEOM_Object_ptr theObject)
1276 {
1277   CORBA::String_var anEntry = theObject->GetEntry();
1278   Handle(GEOM_Object) anObject = _impl->GetObject(theObject->GetStudyID(), anEntry);
1279   if (anObject.IsNull()) return;
1280   _impl->RemoveObject(anObject);
1281   return;
1282 }
1283
1284
1285 //=================================================================================
1286 // function : GetStringFromIOR()
1287 // purpose  : returns a string that represents  a 'GEOM::GEOM_Object_var'
1288 //=================================================================================
1289 char* GEOM_Gen_i::GetStringFromIOR(GEOM::GEOM_Object_ptr theObject) {
1290   return _orb->object_to_string(theObject);
1291 }
1292
1293
1294 //=================================================================================
1295 // function : GetIORFromString()
1296 // purpose  : returns a 'GEOM::GEOM_Object_var' from a string representing it
1297 //=================================================================================
1298 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetIORFromString(const char* stringIOR) {
1299   GEOM::GEOM_Object_var aGeomObject;
1300   if(strcmp(stringIOR,"") != 0){
1301     CORBA::Object_var anObject = _orb->string_to_object(stringIOR);
1302     if(!CORBA::is_nil(anObject))
1303       aGeomObject =  GEOM::GEOM_Object::_narrow(anObject.in());
1304   }
1305   return aGeomObject._retn();
1306 }
1307
1308 //=================================================================================
1309 // function : GetObject()
1310 // purpose  :
1311 //=================================================================================
1312 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetObject (CORBA::Long theStudyID, const char* theEntry)
1313 {
1314   GEOM::GEOM_Object_var obj;
1315   Handle(GEOM_Object) handle_object = _impl->GetObject(theStudyID, (char*)theEntry);
1316   if (handle_object.IsNull()) return NULL;
1317
1318   TCollection_AsciiString stringIOR = handle_object->GetIOR();
1319   if (stringIOR.Length() > 1) {
1320     CORBA::Object_var corba_object = _orb->string_to_object(stringIOR.ToCString());
1321     if (!CORBA::is_nil(corba_object)) obj = GEOM::GEOM_Object::_narrow(corba_object);
1322     return obj._retn();
1323   }
1324
1325   GEOM::GEOM_Gen_ptr engine = _this();
1326   GEOM_Object_i* servant = new GEOM_Object_i (_poa, engine, handle_object);
1327
1328   obj = servant->_this();
1329   CORBA::String_var objStr = _orb->object_to_string(obj);
1330   TCollection_AsciiString anAscii( (char *)objStr.in() );
1331   handle_object->SetIOR( anAscii );
1332   return obj._retn();
1333 }
1334
1335
1336 //=====================================================================================
1337 // EXPORTED METHODS
1338 //=====================================================================================
1339 extern "C"
1340 {
1341 GEOM_I_EXPORT
1342   PortableServer::ObjectId * GEOMEngine_factory(CORBA::ORB_ptr orb,
1343                                                 PortableServer::POA_ptr poa,
1344                                                 PortableServer::ObjectId * contId,
1345                                                 const char *instanceName,
1346                                                 const char * interfaceName)
1347   {
1348    GEOM_Gen_i * myGEOM_Gen_i = new GEOM_Gen_i(orb, poa, contId, instanceName, interfaceName);
1349    // Don't understand the reason of this register ????
1350 //   myGEOM_Gen_i->register_name("/myGEOM_Gen"); // NRI : 11/07/2002 : Add for Supervision example
1351    return myGEOM_Gen_i->getId();
1352   }
1353 }