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