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