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