]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_Gen_i.cc
Salome HOME
Merging with WPdev
[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   return aResultSO._retn();
277 }
278
279
280 //============================================================================
281 // function : Save()
282 // purpose  : save OCAF/Geom document
283 //============================================================================
284 SALOMEDS::TMPFile* GEOM_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
285                                     const char* theURL,
286                                     bool isMultiFile) {
287   SALOMEDS::TMPFile_var aStreamFile;
288   // Get a temporary directory to store a file
289   std::string aTmpDir = (isMultiFile)?theURL:SALOMEDS_Tool::GetTmpDir();
290   // Create a list to store names of created files
291   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
292   aSeq->length(1);
293   // Prepare a file name to open
294   TCollection_AsciiString aNameWithExt("");
295   if (isMultiFile)
296     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL())).c_str());
297   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
298   aSeq[0] = CORBA::string_dup(aNameWithExt.ToCString());
299   // Build a full file name of temporary file
300   TCollection_AsciiString aFullName = TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt;
301   // Save GEOM component in this file
302   _impl->Save(theComponent->GetStudy()->StudyId(), aFullName.ToCString());
303   // Conver a file to the byte stream
304   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
305   // Remove the created file and tmp directory
306   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
307
308   // Return the created byte stream
309   return aStreamFile._retn();
310 }
311
312
313 //============================================================================
314 // function : SaveASCII()
315 // purpose  :
316 //============================================================================
317 SALOMEDS::TMPFile* GEOM_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
318                                          const char* theURL,
319                                          bool isMultiFile) {
320   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
321   return aStreamFile._retn();
322 }
323
324
325 //============================================================================
326 // function : Load()
327 // purpose  :
328 //============================================================================
329 CORBA::Boolean GEOM_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
330                                 const SALOMEDS::TMPFile& theStream,
331                                 const char* theURL,
332                                 bool isMultiFile) {
333
334   if (theStream.length() <= 9) {
335     MESSAGE("The TMPFile is too short : " << theStream.length() << " bytes ");
336     return false;
337   }
338
339   // Get a temporary directory for a file
340   std::string aTmpDir = isMultiFile?theURL:SALOMEDS_Tool::GetTmpDir();
341   // Conver the byte stream theStream to a file and place it in tmp directory
342   SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.c_str(), isMultiFile);
343
344   // Prepare a file name to open
345   TCollection_AsciiString aNameWithExt("");
346   if (isMultiFile)
347     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL())).c_str());
348   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
349   TCollection_AsciiString aFullName = (TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt);
350
351   // Open document
352   if (!_impl->Load(theComponent->GetStudy()->StudyId(), aFullName.ToCString())) return false;
353
354   // Remove the created file and tmp directory
355   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
356
357   SALOMEDS::Study_var Study = theComponent->GetStudy();
358   TCollection_AsciiString name (Study->Name());
359
360   return true;
361 }
362
363
364 //============================================================================
365 // function : LoadASCII()
366 // purpose  :
367 //============================================================================
368 CORBA::Boolean GEOM_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
369                                      const SALOMEDS::TMPFile& theStream,
370                                      const char* theURL,
371                                      bool isMultiFile) {
372   return Load(theComponent, theStream, theURL, isMultiFile);
373 }
374
375
376 //============================================================================
377 // function : Close()
378 // purpose  :
379 //============================================================================
380 void GEOM_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
381 {
382   _impl->Close(theComponent->GetStudy()->StudyId());
383 }
384
385 //============================================================================
386 // function : CanCopy()
387 // purpose  :
388 //============================================================================
389 CORBA::Boolean GEOM_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
390   // Try to retrieve known by Geometry component GEOM_Object by given IOR
391   SALOMEDS::GenericAttribute_var anAttr;
392   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
393
394   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
395
396   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(_orb->string_to_object(anIOR->Value()));
397   // If the object is null one it can't be copied: return false
398   if (anObject->_is_nil()) return false;
399   return true;
400 }
401
402 //============================================================================
403 // function : CopyFrom()
404 // purpose  :
405 //============================================================================
406 SALOMEDS::TMPFile* GEOM_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
407   // Declare a sequence of the byte to store the copied object
408   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
409
410   // Try to get GEOM_Object object by given SObject
411   SALOMEDS::GenericAttribute_var anAttr;
412   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return aStreamFile._retn();
413   GEOM::GEOM_Object_var anObject =  GEOM::GEOM_Object::_narrow(_orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()));
414   if (anObject->_is_nil()) return aStreamFile._retn();
415
416   aStreamFile = anObject->GetShapeStream();
417
418   // Assign an ID  the type of  GEOM_Object
419   theObjectID = anObject->GetType();
420
421   // Return created TMPFile
422   return aStreamFile._retn();
423 }
424
425 //============================================================================
426 // function : CanPaste()
427 // purpose  :
428 //============================================================================
429 CORBA::Boolean GEOM_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
430   // The Geometry component can paste only objects copied by Geometry component
431   // and with the object type = 1
432   if (strcmp(theComponentName, ComponentDataType()) != 0) return false;
433   return true;
434 }
435
436 //============================================================================
437 // function : PasteInto()
438 // purpose  :
439 //============================================================================
440 SALOMEDS::SObject_ptr GEOM_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
441                                             CORBA::Long theObjectID,
442                                             SALOMEDS::SObject_ptr theObject) {
443   // Find the current Study and StudyBuilder
444   SALOMEDS::Study_var aStudy = theObject->GetStudy();
445   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
446
447   // Retrieve a TopoDS_Shape from byte stream
448   TopoDS_Shape aTopology;
449   istrstream aStreamedBrep((char*) &theStream[0], theStream.length());
450   BRep_Builder aBuilder;
451   try {
452     BRepTools::Read(aTopology, aStreamedBrep, aBuilder);
453   } catch (Standard_Failure) {
454     return false;
455   }
456
457   // SObject of the created shape is theObject or new Child of Component if theObject == geom component
458   SALOMEDS::SObject_var aNewSO;
459   if (strcmp(theObject->GetFatherComponent()->GetID(),theObject->GetID()) == 0) {
460     aNewSO = aStudyBuilder->NewObject(theObject);
461   } else aNewSO = SALOMEDS::SObject::_duplicate(theObject);
462
463
464   //Create a new GEOM_Object
465   Handle(GEOM_Object) anObj = _impl->AddObject(aNewSO->GetStudy()->StudyId(), theObjectID);
466   Handle(GEOM_Function) aFunction = anObj->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
467   aFunction->SetValue(aTopology);
468
469   TCollection_AsciiString anEntry;
470   TDF_Tool::Entry(anObj->GetEntry(), anEntry);
471   GEOM::GEOM_Object_var obj = GetObject(anObj->GetDocID(), anEntry.ToCString());
472
473   //Set the study entry of the published GEOM_Object
474   obj->SetStudyEntry(aNewSO->GetID());
475
476   // Add IORAttribute to the Study and set IOR of the created GEOM_Object to it
477   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSO, "AttributeIOR");
478   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
479   CORBA::String_var objStr = _orb->object_to_string(obj);
480   anIOR->SetValue(objStr.in());
481
482   // Return the created in the Study SObject
483   return aNewSO._retn();
484 }
485
486 //============================================================================
487 // function : ComponentDataType()
488 // purpose  :
489 //============================================================================
490 char* GEOM_Gen_i::ComponentDataType()
491 {
492   return CORBA::string_dup("GEOM");
493 }
494
495 //============================================================================
496 // function : AddInStudy
497 // purpose  :
498 //============================================================================
499 SALOMEDS::SObject_ptr GEOM_Gen_i::AddInStudy(SALOMEDS::Study_ptr theStudy, GEOM::GEOM_Object_ptr theObject, const char* theName, GEOM::GEOM_Object_ptr theFather)
500 {
501   SALOMEDS::SObject_var aResultSO;
502   if(theObject->_is_nil() || theStudy->_is_nil()) return aResultSO;
503
504   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
505   CORBA::String_var IOR;
506
507   if(!theFather->_is_nil()) {
508     IOR = _orb->object_to_string(theFather);
509     SALOMEDS::SObject_var aFatherSO = theStudy->FindObjectIOR(IOR.in());
510     if(aFatherSO->_is_nil()) return aResultSO._retn();
511     aResultSO = aStudyBuilder->NewObject(aFatherSO);
512     //aStudyBuilder->Addreference(aResultSO, aResultSO);
513   }
514
515   aResultSO = PublishInStudy(theStudy, aResultSO, theObject, theName);
516   if(aResultSO->_is_nil()) return aResultSO._retn();
517
518   GEOM::ListOfGO_var aList = theObject->GetDependency();
519   Standard_Integer aLength = aList->length();
520   if(aLength < 1) return aResultSO._retn();
521
522   //Publish the arguments
523   for(Standard_Integer i = 0; i< aLength; i++) {
524     GEOM::GEOM_Object_var anObject = aList[i];
525     if(anObject->_is_nil()) continue;
526     IOR = _orb->object_to_string(anObject);
527     SALOMEDS::SObject_var aSO =  theStudy->FindObjectIOR(IOR.in());
528     if(aSO->_is_nil()) continue;
529     SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(aResultSO);
530     aStudyBuilder->Addreference(aSubSO, aSO);
531   }
532
533   return aResultSO._retn();
534 }
535
536 //============================================================================
537 // function : register()
538 // purpose  : register 'name' in 'name_service'
539 //============================================================================
540 void GEOM_Gen_i::register_name(char * name)
541 {
542   GEOM::GEOM_Gen_ptr g = GEOM::GEOM_Gen::_narrow(_this());
543   name_service->Register(g, CORBA::string_dup(name));
544 }
545
546 //============================================================================
547 // function : Undo
548 // purpose  :
549 //============================================================================
550 void GEOM_Gen_i::Undo(CORBA::Long theStudyID)
551 {
552   _impl->Undo(theStudyID);
553 }
554
555 //============================================================================
556 // function : Redo
557 // purpose  :
558 //============================================================================
559 void GEOM_Gen_i::Redo(CORBA::Long theStudyID)
560 {
561   _impl->Redo(theStudyID);
562 }
563
564 //============================================================================
565 // function : GetIBasicOperations
566 // purpose  :
567 //============================================================================
568 GEOM::GEOM_IBasicOperations_ptr GEOM_Gen_i::GetIBasicOperations(CORBA::Long theStudyID)
569      throw ( SALOME::SALOME_Exception )
570 {
571   Unexpect aCatch(SALOME_SalomeException);
572   MESSAGE( "GEOM_Gen_i::GetIBasicOperations" );
573
574   GEOM::GEOM_Gen_ptr engine = _this();
575
576   GEOM_IBasicOperations_i* aServant =
577     new GEOM_IBasicOperations_i(_poa, engine, _impl->GetIBasicOperations(theStudyID));
578
579   // activate the CORBA servant
580   GEOM::GEOM_IBasicOperations_var operations = aServant->_this();
581   return operations._retn();
582 }
583
584 //============================================================================
585 // function : GetITransformOperations
586 // purpose  :
587 //============================================================================
588 GEOM::GEOM_ITransformOperations_ptr GEOM_Gen_i::GetITransformOperations(CORBA::Long theStudyID)
589      throw ( SALOME::SALOME_Exception )
590 {
591   Unexpect aCatch(SALOME_SalomeException);
592   MESSAGE( "GEOM_Gen_i::GetITransformOperations" );
593
594   GEOM::GEOM_Gen_ptr engine = _this();
595
596   GEOM_ITransformOperations_i* aServant =
597     new GEOM_ITransformOperations_i(_poa, engine, _impl->GetITransformOperations(theStudyID));
598
599   // activate the CORBA servant
600   GEOM::GEOM_ITransformOperations_var operations = aServant->_this();
601   return operations._retn();
602 }
603
604 //============================================================================
605 // function : GetI3DPrimOperations
606 // purpose  :
607 //============================================================================
608 GEOM::GEOM_I3DPrimOperations_ptr GEOM_Gen_i::GetI3DPrimOperations(CORBA::Long theStudyID)
609      throw ( SALOME::SALOME_Exception )
610 {
611   Unexpect aCatch(SALOME_SalomeException);
612   MESSAGE( "GEOM_Gen_i::GetI3DPrimOperations" );
613
614   GEOM::GEOM_Gen_ptr engine = _this();
615
616   GEOM_I3DPrimOperations_i* aServant =
617     new GEOM_I3DPrimOperations_i(_poa, engine, _impl->GetI3DPrimOperations(theStudyID));
618
619   // activate the CORBA servant
620   GEOM::GEOM_I3DPrimOperations_var operations = aServant->_this();
621   return operations._retn();
622 }
623
624 //============================================================================
625 // function : GetIShapesOperations
626 // purpose  :
627 //============================================================================
628 GEOM::GEOM_IShapesOperations_ptr GEOM_Gen_i::GetIShapesOperations(CORBA::Long theStudyID)
629      throw ( SALOME::SALOME_Exception )
630 {
631   Unexpect aCatch(SALOME_SalomeException);
632   MESSAGE( "GEOM_Gen_i::GetIShapesOperations" );
633
634   GEOM::GEOM_Gen_ptr engine = _this();
635
636   GEOM_IShapesOperations_i* aServant =
637     new GEOM_IShapesOperations_i(_poa, engine, _impl->GetIShapesOperations(theStudyID));
638
639   // activate the CORBA servant
640   GEOM::GEOM_IShapesOperations_var operations = aServant->_this();
641   return operations._retn();
642 }
643
644 //============================================================================
645 // function : GetIBlocksOperations
646 // purpose  :
647 //============================================================================
648 GEOM::GEOM_IBlocksOperations_ptr GEOM_Gen_i::GetIBlocksOperations(CORBA::Long theStudyID)
649      throw ( SALOME::SALOME_Exception )
650 {
651   Unexpect aCatch(SALOME_SalomeException);
652   MESSAGE( "GEOM_Gen_i::GetIBlocksOperations" );
653
654   GEOM::GEOM_Gen_ptr engine = _this();
655
656   GEOM_IBlocksOperations_i* aServant =
657     new GEOM_IBlocksOperations_i(_poa, engine, _impl->GetIBlocksOperations(theStudyID));
658
659   // activate the CORBA servant
660   GEOM::GEOM_IBlocksOperations_var operations = aServant->_this();
661   return operations._retn();
662 }
663
664 //============================================================================
665 // function : GetIBooleanOperations
666 // purpose  :
667 //============================================================================
668 GEOM::GEOM_IBooleanOperations_ptr GEOM_Gen_i::GetIBooleanOperations(CORBA::Long theStudyID)
669      throw ( SALOME::SALOME_Exception )
670 {
671   Unexpect aCatch(SALOME_SalomeException);
672   MESSAGE( "GEOM_Gen_i::GetIBooleanOperations" );
673
674   GEOM::GEOM_Gen_ptr engine = _this();
675
676   GEOM_IBooleanOperations_i* aServant =
677     new GEOM_IBooleanOperations_i(_poa, engine, _impl->GetIBooleanOperations(theStudyID));
678
679   // activate the CORBA servant
680   GEOM::GEOM_IBooleanOperations_var operations = aServant->_this();
681   return operations._retn();
682 }
683
684 //============================================================================
685 // function : GetICurvesOperations
686 // purpose  :
687 //============================================================================
688 GEOM::GEOM_ICurvesOperations_ptr GEOM_Gen_i::GetICurvesOperations(CORBA::Long theStudyID)
689      throw ( SALOME::SALOME_Exception )
690 {
691   Unexpect aCatch(SALOME_SalomeException);
692   MESSAGE( "GEOM_Gen_i::GetICurvesOperations" );
693
694   GEOM::GEOM_Gen_ptr engine = _this();
695
696   GEOM_ICurvesOperations_i* aServant =
697     new GEOM_ICurvesOperations_i(_poa, engine, _impl->GetICurvesOperations(theStudyID));
698
699   // activate the CORBA servant
700   GEOM::GEOM_ICurvesOperations_var operations = aServant->_this();
701   return operations._retn();
702 }
703
704 //============================================================================
705 // function : GetILocalOperations
706 // purpose  :
707 //============================================================================
708 GEOM::GEOM_ILocalOperations_ptr GEOM_Gen_i::GetILocalOperations(CORBA::Long theStudyID)
709      throw ( SALOME::SALOME_Exception )
710 {
711   Unexpect aCatch(SALOME_SalomeException);
712   MESSAGE( "GEOM_Gen_i::GetILocalOperations" );
713
714   GEOM::GEOM_Gen_ptr engine = _this();
715
716   GEOM_ILocalOperations_i* aServant =
717     new GEOM_ILocalOperations_i(_poa, engine, _impl->GetILocalOperations(theStudyID));
718
719   // activate the CORBA servant
720   GEOM::GEOM_ILocalOperations_var operations = aServant->_this();
721   return operations._retn();
722 }
723
724 //============================================================================
725 // function : GetIHealingOperations
726 // purpose  :
727 //============================================================================
728 GEOM::GEOM_IHealingOperations_ptr GEOM_Gen_i::GetIHealingOperations(CORBA::Long theStudyID)
729      throw ( SALOME::SALOME_Exception )
730 {
731   Unexpect aCatch(SALOME_SalomeException);
732   MESSAGE( "GEOM_Gen_i::IHealingOperations" );
733
734   GEOM::GEOM_Gen_ptr engine = _this();
735
736   GEOM_IHealingOperations_i* aServant =
737     new GEOM_IHealingOperations_i(_poa, engine, _impl->GetIHealingOperations(theStudyID));
738
739   // activate the CORBA servant
740   GEOM::GEOM_IHealingOperations_var operations = aServant->_this();
741   return operations._retn();
742 }
743
744 //============================================================================
745 // function : GetIInsertOperations
746 // purpose  :
747 //============================================================================
748 GEOM::GEOM_IInsertOperations_ptr GEOM_Gen_i::GetIInsertOperations(CORBA::Long theStudyID)
749      throw ( SALOME::SALOME_Exception )
750 {
751   Unexpect aCatch(SALOME_SalomeException);
752   MESSAGE( "GEOM_Gen_i::GetIInsertOperations" );
753
754   GEOM::GEOM_Gen_ptr engine = _this();
755
756   GEOM_IInsertOperations_i* aServant =
757     new GEOM_IInsertOperations_i(_poa, engine, _impl->GetIInsertOperations(theStudyID));
758
759   // activate the CORBA servant
760   GEOM::GEOM_IInsertOperations_var operations = aServant->_this();
761   return operations._retn();
762 }
763
764 //============================================================================
765 // function : GetIMeasureOperations
766 // purpose  :
767 //============================================================================
768 GEOM::GEOM_IMeasureOperations_ptr GEOM_Gen_i::GetIMeasureOperations(CORBA::Long theStudyID)
769      throw ( SALOME::SALOME_Exception )
770 {
771   Unexpect aCatch(SALOME_SalomeException);
772   MESSAGE( "GEOM_Gen_i::GetIMeasureOperations" );
773
774   GEOM::GEOM_Gen_ptr engine = _this();
775
776   GEOM_IMeasureOperations_i* aServant =
777     new GEOM_IMeasureOperations_i(_poa, engine, _impl->GetIMeasureOperations(theStudyID));
778
779   // activate the CORBA servant
780   GEOM::GEOM_IMeasureOperations_var operations = aServant->_this();
781   return operations._retn();
782 }
783
784 //============================================================================
785 // function : GetIGroupOperations
786 // purpose  :
787 //============================================================================
788 GEOM::GEOM_IGroupOperations_ptr GEOM_Gen_i::GetIGroupOperations(CORBA::Long theStudyID)
789      throw ( SALOME::SALOME_Exception )
790 {
791   Unexpect aCatch(SALOME_SalomeException);
792   MESSAGE( "GEOM_Gen_i::GetIGroupOperations" );
793
794   GEOM::GEOM_Gen_ptr engine = _this();
795
796   GEOM_IGroupOperations_i* aServant =
797     new GEOM_IGroupOperations_i(_poa, engine, _impl->GetIGroupOperations(theStudyID));
798
799   // activate the CORBA servant
800   GEOM::GEOM_IGroupOperations_var operations = aServant->_this();
801   return operations._retn();
802 }
803
804 //=============================================================================
805 /*!
806  *  AddSubShape
807  */
808 //=============================================================================
809 GEOM::GEOM_Object_ptr GEOM_Gen_i::AddSubShape (GEOM::GEOM_Object_ptr theMainShape,
810                                                const GEOM::ListOfLong& theIndices)
811 {
812   if(theMainShape == NULL || theIndices.length() < 1) return GEOM::GEOM_Object::_nil();
813   Handle(GEOM_Object) aMainsShape = _impl->GetObject(theMainShape->GetStudyID(), theMainShape->GetEntry());
814   if (aMainsShape.IsNull()) return GEOM::GEOM_Object::_nil();
815
816   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1, theIndices.length());
817   for(Standard_Integer i = 0; i<theIndices.length(); i++) anArray->SetValue(i+1, theIndices[i]);
818
819   Handle(GEOM_Object) anObject = _impl->AddSubShape(aMainsShape, anArray, true);
820   if(anObject.IsNull()) return GEOM::GEOM_Object::_nil();
821
822   TCollection_AsciiString anEntry;
823   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
824   return GetObject(anObject->GetDocID(), anEntry.ToCString());
825 }
826
827 //=============================================================================
828 /*!
829  *  RemoveObject
830  */
831 //=============================================================================
832 void GEOM_Gen_i::RemoveObject(GEOM::GEOM_Object_ptr theObject)
833 {
834   Handle(GEOM_Object) anObject = _impl->GetObject(theObject->GetStudyID(), theObject->GetEntry());
835   if (anObject.IsNull()) return;
836   _impl->RemoveObject(anObject);
837   return;
838 }
839
840
841 //=================================================================================
842 // function : GetStringFromIOR()
843 // purpose  : returns a string that represents  a 'GEOM::GEOM_Object_var'
844 //=================================================================================
845 char* GEOM_Gen_i::GetStringFromIOR(GEOM::GEOM_Object_ptr theObject) {
846   return _orb->object_to_string(theObject);
847 }
848
849
850 //=================================================================================
851 // function : GetIORFromString()
852 // purpose  : returns a 'GEOM::GEOM_Object_var' from a string representing it
853 //=================================================================================
854 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetIORFromString(const char* stringIOR) {
855   GEOM::GEOM_Object_var aGeomObject;
856   if(strcmp(stringIOR,"") != 0){
857     CORBA::Object_var anObject = _orb->string_to_object(stringIOR);
858     if(!CORBA::is_nil(anObject))
859       aGeomObject =  GEOM::GEOM_Object::_narrow(anObject.in());
860   }
861   return aGeomObject._retn();
862 }
863
864 //=================================================================================
865 // function : GetObject()
866 // purpose  :
867 //=================================================================================
868 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetObject (CORBA::Long theStudyID, const char* theEntry)
869 {
870   GEOM::GEOM_Object_var obj;
871   Handle(GEOM_Object) handle_object = _impl->GetObject(theStudyID, (char*)theEntry);
872   if (handle_object.IsNull()) return NULL;
873
874   TCollection_AsciiString stringIOR = handle_object->GetIOR();
875   if (stringIOR.Length() > 1) {
876     CORBA::Object_var corba_object = _orb->string_to_object(stringIOR.ToCString());
877     if (!CORBA::is_nil(corba_object)) obj = GEOM::GEOM_Object::_narrow(corba_object);
878     return obj._retn();
879   }
880
881   GEOM::GEOM_Gen_ptr engine = _this();
882   GEOM_Object_i* servant = new GEOM_Object_i (_poa, engine, handle_object);
883
884   obj = servant->_this();
885   CORBA::String_var objStr = _orb->object_to_string(obj);
886   TCollection_AsciiString anAscii( (char *)objStr.in() );
887   handle_object->SetIOR( anAscii );
888   return obj._retn();
889 }
890
891
892 //=====================================================================================
893 // EXPORTED METHODS
894 //=====================================================================================
895 extern "C"
896 {
897 GEOM_I_EXPORT
898   PortableServer::ObjectId * GEOMEngine_factory(CORBA::ORB_ptr orb,
899                                                 PortableServer::POA_ptr poa,
900                                                 PortableServer::ObjectId * contId,
901                                                 const char *instanceName,
902                                                 const char * interfaceName)
903   {
904    GEOM_Gen_i * myGEOM_Gen_i = new GEOM_Gen_i(orb, poa, contId, instanceName, interfaceName);
905    myGEOM_Gen_i->register_name("/myGEOM_Gen"); // NRI : 11/07/2002 : Add for Supervision example
906    return myGEOM_Gen_i->getId();
907   }
908 }