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