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