Salome HOME
7f9f5cab49ba11e64d867dc1e0e6d548276c57b9
[modules/geom.git] / src / GEOM_I / GEOM_Gen_i.cc
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 #ifdef WNT
23 #pragma warning( disable:4786 )
24 #endif
25
26 #include <Standard_Stream.hxx>
27
28 #include "GEOM_Gen_i.hh"
29 #include "GEOM_Object_i.hh"
30
31 #include <strstream>
32 //#include <sstream>
33
34 #include "Utils_CorbaException.hxx"
35 #include "OpUtil.hxx"
36 #include "Utils_ExceptHandlers.hxx"
37 #include "utilities.h"
38
39 #include "GEOM_Object_i.hh"
40 #include "GEOM_Object.hxx"
41 #include "GEOM_Function.hxx"
42 #include "GEOMImpl_Types.hxx"
43 #include "GEOMImpl_CopyDriver.hxx"
44
45 // Cascade headers
46 #include <BRep_Builder.hxx>
47 #include <BRepTools.hxx>
48 #include <TDF_Label.hxx>
49 #include <TDF_Tool.hxx>
50 #include <TCollection_AsciiString.hxx>
51 #include <TColStd_HArray1OfInteger.hxx>
52 #include <TopAbs_ShapeEnum.hxx>
53 #include <OSD.hxx>
54
55 #include "SALOMEDS_Tool.hxx"
56
57 //============================================================================
58 // function : GEOM_Gen_i()
59 // purpose  : constructor to be called for servant creation.
60 //============================================================================
61 GEOM_Gen_i::GEOM_Gen_i(CORBA::ORB_ptr orb,
62                        PortableServer::POA_ptr poa,
63                        PortableServer::ObjectId * contId,
64                        const char *instanceName,
65                        const char *interfaceName) :
66   Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
67 {
68   _thisObj = this;
69   _id = _poa->activate_object(_thisObj);
70   name_service = new SALOME_NamingService(_orb);
71
72   _impl = new ::GEOMImpl_Gen;
73
74   //PAL10867: disable signals catching with "noexcepthandler" option
75   char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
76   if (!envNoCatchSignals || !atoi(envNoCatchSignals))
77   {
78     //work around PAL12004, PAL12628
79     //OSD::SetSignal( true );
80     bool raiseFPE;
81 #ifdef _DEBUG_
82     raiseFPE = true;
83     char* envDisableFPE = getenv("DISABLE_FPE");
84     if (envDisableFPE && atoi(envDisableFPE))
85       raiseFPE = false;
86 #else
87     raiseFPE = false;
88 #endif
89     OSD::SetSignal( raiseFPE );
90   }
91 }
92
93 //============================================================================
94 // function : ~GEOM_Gen_i()
95 // purpose  : destructor
96 //============================================================================
97 GEOM_Gen_i::~GEOM_Gen_i() {
98   delete name_service;
99   delete _impl;
100 }
101
102
103 //============================================================================
104 // function : IORToLocalPersistentID()
105 // purpose  :
106 //============================================================================
107 char* GEOM_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
108                                          const char* IORString,
109                                          CORBA::Boolean isMultiFile,
110                                          CORBA::Boolean isASCII)
111 {
112   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(_orb->string_to_object(IORString));
113   if (!CORBA::is_nil(anObject)) {
114     return CORBA::string_dup(anObject->GetEntry());
115   }
116   return 0;
117 }
118
119
120 //============================================================================
121 // function : LocalPersistentIDToIOR()
122 // purpose  : Create/Load CORBA object from a persistent ref (an entry)
123 //          : Used when a study is loaded
124 //          : The IOR (IORName) of object created is returned
125 //============================================================================
126 char* GEOM_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
127                                          const char* aLocalPersistentID,
128                                          CORBA::Boolean isMultiFile,
129                                          CORBA::Boolean isASCII)
130 {
131   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
132
133   Handle(GEOM_Object) anObject = _impl->GetObject(aStudy->StudyId(), const_cast<char*>(aLocalPersistentID));
134   TCollection_AsciiString anEntry;
135   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
136   GEOM::GEOM_Object_var obj = GetObject(anObject->GetDocID(), anEntry.ToCString());
137
138   CORBA::String_var aPersRefString = _orb->object_to_string(obj);
139   return CORBA::string_dup(aPersRefString);
140 }
141
142 //============================================================================
143 // function : CanPublishInStudy
144 // purpose  :
145 //============================================================================
146 bool GEOM_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
147 {
148   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(theIOR);
149   return !(anObject->_is_nil());
150 }
151
152
153 //============================================================================
154 // function : PublishInStudy
155 // purpose  :
156 //============================================================================
157 SALOMEDS::SObject_ptr GEOM_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
158                                                  SALOMEDS::SObject_ptr theSObject,
159                                                  CORBA::Object_ptr theObject,
160                                                  const char* theName) throw (SALOME::SALOME_Exception)
161 {
162   Unexpect aCatch(SALOME_SalomeException);
163   SALOMEDS::SObject_var aResultSO;
164   if(CORBA::is_nil(theObject) || theStudy->_is_nil()) return aResultSO;
165   GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow(theObject);
166   if(aShape->_is_nil()) return aResultSO;
167
168   SALOMEDS::GenericAttribute_var anAttr;
169   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder();
170
171   SALOMEDS::SComponent_var       aFather = theStudy->FindComponent("GEOM");
172   if (aFather->_is_nil()) {
173     aFather = aStudyBuilder->NewComponent("GEOM");
174     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributeName");
175     SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
176     aName->SetValue("Geometry");
177     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
178     SALOMEDS::AttributePixMap::_narrow(anAttr)->SetPixMap("ICON_OBJBROWSER_Geometry");
179     aStudyBuilder->DefineComponentInstance(aFather, GEOM_Gen::_this());
180   }
181   if (aFather->_is_nil()) return aResultSO;
182
183   if (CORBA::is_nil(theSObject)) {
184     aResultSO = aStudyBuilder->NewObject(aFather);
185   } else {
186     if (!theSObject->ReferencedObject(aResultSO))
187       aResultSO = SALOMEDS::SObject::_duplicate(theSObject); //SRN: Added Aug 24,2004 : for  the method AddInStudy with theFather argumenet != NULL
188       //THROW_SALOME_CORBA_EXCEPTION("Publish in study supervision graph error",SALOME::BAD_PARAM);
189   }
190   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
191   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
192   //char *aGeomObjIOR = _orb->object_to_string(theObject);
193    CORBA::String_var aGeomObjIOR = _orb->object_to_string(theObject);
194   //anIOR->SetValue(CORBA::string_dup(aGeomObjIOR));
195   anIOR->SetValue(aGeomObjIOR);
196
197   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributePixMap");
198   SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
199   TCollection_AsciiString aShapeName("Shape_");
200
201   if ( aShape->GetType() == GEOM_GROUP ) {
202     GEOM::GEOM_IGroupOperations_var anOp = GetIGroupOperations( theStudy->StudyId() );
203     switch ( (TopAbs_ShapeEnum)anOp->GetType( aShape ) ) {
204     case TopAbs_VERTEX:
205       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_PNT" );
206       aShapeName = "Group_Of_Vertices_";
207       break;
208     case TopAbs_EDGE:
209       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_EDGE" );
210       aShapeName = "Group_Of_Edges_";
211       break;
212     case TopAbs_FACE:
213       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_FACE" );
214       aShapeName = "Group_Of_Faces_";
215       break;
216     case TopAbs_SOLID:
217       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_SOLID" );
218       aShapeName = "Group_Of_Solids_";
219       break;
220     }
221   } else if ( aShape->GetType() == GEOM_MARKER ) {
222     aPixmap->SetPixMap( "ICON_OBJBROWSER_LCS" );
223     aShapeName = "LocalCS_";
224   } else if ( aShape->GetShapeType() == GEOM::COMPOUND ) {
225     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPOUND" );
226     aShapeName = "Compound_";
227   } else if ( aShape->GetShapeType() == GEOM::COMPSOLID ) {
228     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPSOLID" );
229     aShapeName = "Compsolid_";
230   } else if ( aShape->GetShapeType() == GEOM::SOLID ) {
231     aPixmap->SetPixMap( "ICON_OBJBROWSER_SOLID" );
232     aShapeName = "Solid_";
233   } else if ( aShape->GetShapeType() == GEOM::SHELL ) {
234     aPixmap->SetPixMap( "ICON_OBJBROWSER_SHELL" );
235     aShapeName = "Shell_";
236   } else if ( aShape->GetShapeType() == GEOM::FACE ) {
237     aPixmap->SetPixMap( "ICON_OBJBROWSER_FACE" );
238     aShapeName = "Face_";
239   } else if ( aShape->GetShapeType() == GEOM::WIRE ) {
240     aPixmap->SetPixMap( "ICON_OBJBROWSER_WIRE" );
241     aShapeName = "Wire_";
242   } else if ( aShape->GetShapeType() == GEOM::EDGE ) {
243     aPixmap->SetPixMap( "ICON_OBJBROWSER_EDGE" );
244     aShapeName = "Edge_";
245   } else if ( aShape->GetShapeType() == GEOM::VERTEX ) {
246     aPixmap->SetPixMap( "ICON_OBJBROWSER_VERTEX" );
247     aShapeName = "Vertex_";
248   }
249   //if (strlen(theName) == 0) aShapeName += TCollection_AsciiString(aResultSO->Tag());
250   //else aShapeName = TCollection_AsciiString(CORBA::string_dup(theName));
251
252   // asv : 11.11.04 Introducing a more sofisticated method of name creation, just as
253   //       it is done in GUI in GEOMBase::GetDefaultName() - not just add a Tag() == number
254   //       of objects in the study, but compute a number of objects with the same prefix
255   //       and build a new name as Prefix_N+1
256   if ( strlen( theName ) == 0 ) { // MOST PROBABLY CALLED FROM BATCHMODE OR SUPERVISOR
257     int i = 0;                    // (WITH EMPTY NEW NAME)
258     SALOMEDS::SObject_var obj;
259     TCollection_AsciiString aNewShapeName;
260     do {
261       aNewShapeName = aShapeName + TCollection_AsciiString(++i);
262       obj = theStudy->FindObject( aNewShapeName.ToCString() );
263     }
264     while ( !obj->_is_nil() );
265     aShapeName = aNewShapeName;
266   }
267   else // MOST PROBABLY CALLED FROM GEOM GUI (ALREADY WITH VALID NAME)
268     aShapeName = TCollection_AsciiString((char*)theName);
269
270   //Set the study entry as a name of  the published GEOM_Object
271   aShape->SetStudyEntry(aResultSO->GetID());
272
273   //Set a name of the added shape
274   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeName");
275   SALOMEDS::AttributeName_var aNameAttrib = SALOMEDS::AttributeName::_narrow(anAttr);
276   aNameAttrib->SetValue(aShapeName.ToCString());
277
278   //Set NoteBook variables used in the object creation
279   TCollection_AsciiString aVars;
280   SALOMEDS::ListOfListOfStrings_var aSections = theStudy->ParseVariables(aShape->GetParameters());
281   for(int i = 0, n = aSections->length(); i < n; i++) {
282     SALOMEDS::ListOfStrings aListOfVars = aSections[i];
283     for(int j = 0, m = aListOfVars.length(); j < m; j++) {
284       if(theStudy->IsVariable(aListOfVars[j].in()))
285         aVars += TCollection_AsciiString(aListOfVars[j].in());
286       if(j != m-1)
287         aVars += ":";
288     }
289     if(i != n-1)
290       aVars += "|";
291   }
292   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeString");
293   SALOMEDS::AttributeString_var aStringAttrib = SALOMEDS::AttributeString::_narrow(anAttr);
294   aStringAttrib->SetValue(aVars.ToCString());
295
296   //Set a name of the GEOM object
297   aShape->SetName(theName);
298
299   return aResultSO._retn();
300 }
301
302
303 //============================================================================
304 // function : Save()
305 // purpose  : save OCAF/Geom document
306 //============================================================================
307 SALOMEDS::TMPFile* GEOM_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
308                                     const char* theURL,
309                                     bool isMultiFile) {
310   SALOMEDS::TMPFile_var aStreamFile;
311   // Get a temporary directory to store a file
312   std::string aTmpDir = (isMultiFile)?theURL:SALOMEDS_Tool::GetTmpDir();
313
314   // OCCT BUG: cannot save a document (in current folder)
315   // if directory name is empty
316   if (aTmpDir.size() == 0) {
317 #ifdef WNT
318     aTmpDir = ".\\";
319 #else
320     aTmpDir = "./";
321 #endif
322   }
323
324   // Create a list to store names of created files
325   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
326   aSeq->length(1);
327   // Prepare a file name to open
328   TCollection_AsciiString aNameWithExt("");
329   if (isMultiFile)
330     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath
331                                                    (theComponent->GetStudy()->URL())).c_str());
332   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
333   aSeq[0] = CORBA::string_dup(aNameWithExt.ToCString());
334   // Build a full file name of temporary file
335   TCollection_AsciiString aFullName = TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt;
336   // Save GEOM component in this file
337   _impl->Save(theComponent->GetStudy()->StudyId(),(char*) aFullName.ToCString());
338   // Conver a file to the byte stream
339   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
340   // Remove the created file and tmp directory
341   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
342
343   // Return the created byte stream
344   return aStreamFile._retn();
345 }
346
347
348 //============================================================================
349 // function : SaveASCII()
350 // purpose  :
351 //============================================================================
352 SALOMEDS::TMPFile* GEOM_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
353                                          const char* theURL,
354                                          bool isMultiFile) {
355   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
356   return aStreamFile._retn();
357 }
358
359
360 //============================================================================
361 // function : Load()
362 // purpose  :
363 //============================================================================
364 CORBA::Boolean GEOM_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
365                                 const SALOMEDS::TMPFile& theStream,
366                                 const char* theURL,
367                                 bool isMultiFile) {
368
369   if (theStream.length() <= 9) {
370     MESSAGE("The TMPFile is too short : " << theStream.length() << " bytes ");
371     return false;
372   }
373
374   // Get a temporary directory for a file
375   std::string aTmpDir = isMultiFile?theURL:SALOMEDS_Tool::GetTmpDir();
376
377   // OCCT BUG: cannot load a document (from current folder)
378   // if directory name is empty
379   if (aTmpDir.size() == 0) {
380 #ifdef WNT
381     aTmpDir = ".\\";
382 #else
383     aTmpDir = "./";
384 #endif
385   }
386
387   // Conver the byte stream theStream to a file and place it in tmp directory
388   SALOMEDS::ListOfFileNames_var aSeq =
389     SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.c_str(), isMultiFile);
390
391   // Prepare a file name to open
392   TCollection_AsciiString aNameWithExt("");
393   if (isMultiFile)
394     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath
395                                                    (theComponent->GetStudy()->URL())).c_str());
396   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
397   TCollection_AsciiString aFullName = (TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt);
398
399   // Open document
400   if (!_impl->Load(theComponent->GetStudy()->StudyId(),(char*) aFullName.ToCString())) return false;
401
402   // Remove the created file and tmp directory
403   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
404
405   SALOMEDS::Study_var Study = theComponent->GetStudy();
406   TCollection_AsciiString name (Study->Name());
407
408   return true;
409 }
410
411
412 //============================================================================
413 // function : LoadASCII()
414 // purpose  :
415 //============================================================================
416 CORBA::Boolean GEOM_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
417                                      const SALOMEDS::TMPFile& theStream,
418                                      const char* theURL,
419                                      bool isMultiFile) {
420   return Load(theComponent, theStream, theURL, isMultiFile);
421 }
422
423
424 //============================================================================
425 // function : Close()
426 // purpose  :
427 //============================================================================
428 void GEOM_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
429 {
430   _impl->Close(theComponent->GetStudy()->StudyId());
431 }
432
433 //============================================================================
434 // function : CanCopy()
435 // purpose  :
436 //============================================================================
437 CORBA::Boolean GEOM_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
438   // Try to retrieve known by Geometry component GEOM_Object by given IOR
439   SALOMEDS::GenericAttribute_var anAttr;
440   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
441
442   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
443
444   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(_orb->string_to_object(anIOR->Value()));
445   // If the object is null one it can't be copied: return false
446   if (anObject->_is_nil()) return false;
447   return true;
448 }
449
450 //============================================================================
451 // function : CopyFrom()
452 // purpose  :
453 //============================================================================
454 SALOMEDS::TMPFile* GEOM_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID)
455 {
456   // Declare a sequence of the byte to store the copied object
457   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
458
459   // Try to get GEOM_Object object by given SObject
460   SALOMEDS::GenericAttribute_var anAttr;
461   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return aStreamFile._retn();
462   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow
463     (_orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()));
464   if (anObject->_is_nil()) return aStreamFile._retn();
465
466   aStreamFile = anObject->GetShapeStream();
467
468   // Assign an ID  the type of  GEOM_Object
469   theObjectID = anObject->GetType();
470
471   // Return created TMPFile
472   return aStreamFile._retn();
473 }
474
475 //============================================================================
476 // function : CanPaste()
477 // purpose  :
478 //============================================================================
479 CORBA::Boolean GEOM_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
480   // The Geometry component can paste only objects copied by Geometry component
481   // and with the object type = 1
482   if (strcmp(theComponentName, ComponentDataType()) != 0) return false;
483   return true;
484 }
485
486 //============================================================================
487 // function : PasteInto()
488 // purpose  :
489 //============================================================================
490 SALOMEDS::SObject_ptr GEOM_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
491                                             CORBA::Long theObjectID,
492                                             SALOMEDS::SObject_ptr theObject) {
493   // Find the current Study and StudyBuilder
494   SALOMEDS::Study_var aStudy = theObject->GetStudy();
495   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
496
497   // Retrieve a TopoDS_Shape from byte stream
498   TopoDS_Shape aTopology;
499   istrstream aStreamedBrep((char*) &theStream[0], theStream.length());
500   BRep_Builder aBuilder;
501   try {
502     BRepTools::Read(aTopology, aStreamedBrep, aBuilder);
503   } catch (Standard_Failure) {
504     return false;
505   }
506
507   // SObject of the created shape is theObject or new Child of Component if theObject == geom component
508   SALOMEDS::SObject_var aNewSO;
509   if (strcmp(theObject->GetFatherComponent()->GetID(),theObject->GetID()) == 0) {
510     aNewSO = aStudyBuilder->NewObject(theObject);
511   } else aNewSO = SALOMEDS::SObject::_duplicate(theObject);
512
513
514   //Create a new GEOM_Object
515   Handle(GEOM_Object) anObj = _impl->AddObject(aNewSO->GetStudy()->StudyId(), theObjectID);
516   Handle(GEOM_Function) aFunction = anObj->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
517   aFunction->SetValue(aTopology);
518
519   TCollection_AsciiString anEntry;
520   TDF_Tool::Entry(anObj->GetEntry(), anEntry);
521   GEOM::GEOM_Object_var obj = GetObject(anObj->GetDocID(), anEntry.ToCString());
522
523   //Set the study entry of the published GEOM_Object
524   obj->SetStudyEntry(aNewSO->GetID());
525
526   // Add IORAttribute to the Study and set IOR of the created GEOM_Object to it
527   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSO, "AttributeIOR");
528   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
529   CORBA::String_var objStr = _orb->object_to_string(obj);
530   anIOR->SetValue(objStr.in());
531
532   // Return the created in the Study SObject
533   return aNewSO._retn();
534 }
535
536 //============================================================================
537 // function : ComponentDataType()
538 // purpose  :
539 //============================================================================
540 char* GEOM_Gen_i::ComponentDataType()
541 {
542   return CORBA::string_dup("GEOM");
543 }
544
545 //============================================================================
546 // function : AddInStudy
547 // purpose  :
548 //============================================================================
549 SALOMEDS::SObject_ptr GEOM_Gen_i::AddInStudy (SALOMEDS::Study_ptr theStudy,
550                                               GEOM::GEOM_Object_ptr theObject,
551                                               const char* theName,
552                                               GEOM::GEOM_Object_ptr theFather)
553 {
554   SALOMEDS::SObject_var aResultSO;
555   if(theObject->_is_nil() || theStudy->_is_nil()) return aResultSO;
556
557   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
558   CORBA::String_var IOR;
559
560   if(!theFather->_is_nil()) {
561     IOR = _orb->object_to_string(theFather);
562     SALOMEDS::SObject_var aFatherSO = theStudy->FindObjectIOR(IOR.in());
563     if(aFatherSO->_is_nil()) return aResultSO._retn();
564     aResultSO = aStudyBuilder->NewObject(aFatherSO);
565     //aStudyBuilder->Addreference(aResultSO, aResultSO);
566   }
567
568   aResultSO = PublishInStudy(theStudy, aResultSO, theObject, theName);
569   if(aResultSO->_is_nil()) return aResultSO._retn();
570
571   GEOM::ListOfGO_var aList = theObject->GetDependency();
572   Standard_Integer aLength = aList->length();
573   if(aLength < 1) return aResultSO._retn();
574
575   //Publish the arguments
576   for(Standard_Integer i = 0; i< aLength; i++) {
577     GEOM::GEOM_Object_var anObject = aList[i];
578     if(anObject->_is_nil()) continue;
579     IOR = _orb->object_to_string(anObject);
580     SALOMEDS::SObject_var aSO =  theStudy->FindObjectIOR(IOR.in());
581     if(aSO->_is_nil()) continue;
582     SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(aResultSO);
583     aStudyBuilder->Addreference(aSubSO, aSO);
584   }
585
586   return aResultSO._retn();
587 }
588
589 //============================================================================
590 // function : RestoreSubShapesO
591 // purpose  : Publish sub-shapes, standing for arguments and sub-shapes of arguments.
592 //            To be used from python scripts out of geompy.addToStudy (non-default usage)
593 //============================================================================
594 CORBA::Boolean GEOM_Gen_i::RestoreSubShapesO (SALOMEDS::Study_ptr     theStudy,
595                                               GEOM::GEOM_Object_ptr   theObject,
596                                               const GEOM::ListOfGO&   theArgs,
597                                               GEOM::find_shape_method theFindMethod,
598                                               CORBA::Boolean          theInheritFirstArg)
599 {
600   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject))
601     return false;
602
603   // find SObject in the study
604   CORBA::String_var anIORo = _orb->object_to_string(theObject);
605   SALOMEDS::SObject_var aSO = theStudy->FindObjectIOR(anIORo.in());
606   if (CORBA::is_nil(aSO))
607     return false;
608
609   return RestoreSubShapes(theStudy, theObject, aSO, theArgs, theFindMethod, theInheritFirstArg);
610 }
611
612 //============================================================================
613 // function : RestoreSubShapesSO
614 // purpose  : Publish sub-shapes, standing for arguments and sub-shapes of arguments.
615 //            To be used from GUI and from geompy.addToStudy
616 //============================================================================
617 CORBA::Boolean GEOM_Gen_i::RestoreSubShapesSO (SALOMEDS::Study_ptr     theStudy,
618                                                SALOMEDS::SObject_ptr   theSObject,
619                                                const GEOM::ListOfGO&   theArgs,
620                                                GEOM::find_shape_method theFindMethod,
621                                                CORBA::Boolean          theInheritFirstArg)
622 {
623   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theSObject))
624     return false;
625
626   SALOMEDS::GenericAttribute_var anAttr;
627   if (!theSObject->FindAttribute(anAttr, "AttributeIOR"))
628     return false;
629
630   SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
631   CORBA::String_var anIORso = anAttrIOR->Value();
632
633   // get Object from SObject
634   GEOM::GEOM_Object_var anO = GEOM::GEOM_Object::_narrow(_orb->string_to_object(anIORso));
635   if (CORBA::is_nil(anO))
636     return false;
637
638   return RestoreSubShapes(theStudy, anO, theSObject, theArgs, theFindMethod, theInheritFirstArg);
639 }
640
641 //============================================================================
642 // function : RestoreSubShapes
643 // purpose  : Private method. Works only if both theObject and theSObject
644 //            are defined, and does not check, if they correspond to each other.
645 //============================================================================
646 CORBA::Boolean GEOM_Gen_i::RestoreSubShapes (SALOMEDS::Study_ptr     theStudy,
647                                              GEOM::GEOM_Object_ptr   theObject,
648                                              SALOMEDS::SObject_ptr   theSObject,
649                                              const GEOM::ListOfGO&   theArgs,
650                                              GEOM::find_shape_method theFindMethod,
651                                              CORBA::Boolean          theInheritFirstArg)
652 {
653   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject) || CORBA::is_nil(theSObject))
654     return false;
655
656   // Arguments to be published
657   GEOM::ListOfGO_var aList;
658
659   // If theArgs list is empty, we try to publish all arguments,
660   // otherwise publish only passed args
661   Standard_Integer nbArgsActual = -1; // -1 means unknown
662   Standard_Integer aLength = theArgs.length();
663   if (aLength > 0) {
664     aList = new GEOM::ListOfGO;
665     aList->length(aLength);
666     for (int i = 0; i < aLength; i++) {
667       aList[i] = theArgs[i];
668     }
669   }
670   else {
671     // Get all arguments
672     aList = theObject->GetDependency();
673     aLength = aList->length();
674     nbArgsActual = aLength;
675   }
676
677   if (aLength < 1)
678     return false;
679
680   if (theInheritFirstArg || (nbArgsActual == 1)) {
681     // Do not publish argument's reflection,
682     // but only reconstruct its published sub-shapes
683
684     GEOM::GEOM_Object_var anArgO = aList[0];
685     CORBA::String_var anIOR = _orb->object_to_string(anArgO);
686     SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
687
688     GEOM::ListOfGO_var aParts =
689       RestoreSubShapesOneLevel(theStudy, anArgSO, theSObject, theObject, theFindMethod);
690
691     // set the color of the transformed shape to the color of initial shape
692     theObject->SetColor(aList[0]->GetColor());
693
694     return (aParts->length() > 0);
695   }
696
697   // Get interface, containing method, which we will use to reconstruct sub-shapes
698   GEOM::GEOM_IShapesOperations_var aShapesOp = GetIShapesOperations(theStudy->StudyId());
699   GEOM::GEOM_IGroupOperations_var  aGroupOp  = GetIGroupOperations(theStudy->StudyId());
700
701   // Reconstruct arguments and tree of sub-shapes of the arguments
702   CORBA::String_var anIOR;
703   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
704   for (Standard_Integer i = 0; i < aLength; i++)
705   {
706     GEOM::GEOM_Object_var anArgO = aList[i];
707     if (!CORBA::is_nil(anArgO)) {
708       anIOR = _orb->object_to_string(anArgO);
709       SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
710       TCollection_AsciiString anArgName;
711       if (CORBA::is_nil(anArgSO)) {
712         anArgName = "arg_";
713         anArgName += TCollection_AsciiString(i);
714       }
715       else {
716         anArgName = anArgSO->GetName();
717       }
718
719       // Find a sub-shape of theObject in place of the argument
720       GEOM::GEOM_Object_var aSubO;
721       switch (theFindMethod) {
722       case GEOM::FSM_GetInPlace:
723         {
724           // Use GetInPlace
725           aSubO = aShapesOp->GetInPlace(theObject, anArgO);
726         }
727         break;
728       case GEOM::FSM_Transformed:
729         {
730           // transformation, cannot use GetInPlace, operate with indices
731           GEOM::ListOfLong_var anIDs = anArgO->GetSubShapeIndices();
732           if (anIDs->length() > 1) {
733             // group
734             aSubO = aGroupOp->CreateGroup(theObject, aGroupOp->GetType(anArgO));
735             if (!CORBA::is_nil(aSubO))
736               aGroupOp->UnionIDs(aSubO, anIDs);
737           }
738           else {
739             // single sub-shape
740             aSubO = aShapesOp->GetSubShape(theObject, anIDs[0]);
741           }
742         }
743         break;
744       case GEOM::FSM_GetSame:
745         {
746           // Use GetSame
747           aSubO = aShapesOp->GetSame(theObject, anArgO);
748         }
749         break;
750       case GEOM::FSM_GetShapesOnShape:
751         {
752           // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
753           aSubO = aShapesOp->GetShapesOnShapeAsCompound(anArgO, theObject,
754                                                         (short)GEOM::SOLID, GEOM::ST_ONIN);
755         }
756         break;
757       case GEOM::FSM_GetInPlaceByHistory:
758         {
759           // Use GetInPlaceByHistory
760           aSubO = aShapesOp->GetInPlaceByHistory(theObject, anArgO);
761         }
762         break;
763       default:
764         {}
765       }
766
767       if (!CORBA::is_nil(aSubO)) {
768         // Publish the sub-shape
769         TCollection_AsciiString aSubName ("from_");
770         aSubName += anArgName;
771         SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(theSObject);
772         aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
773         // Restore color
774         aSubO->SetColor(anArgO->GetColor());
775
776         if (!CORBA::is_nil(anArgSO)) {
777           // Restore published sub-shapes of the argument
778           if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
779             // pass theObject, because only it has the history
780             RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO, theObject, theFindMethod);
781           else
782             RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO, aSubO, theFindMethod);
783         }
784       }
785       else { // GetInPlace failed, try to build from published parts
786         if (!CORBA::is_nil(anArgSO)) {
787           SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(theSObject);
788
789           // Restore published sub-shapes of the argument
790           GEOM::ListOfGO_var aParts =
791             RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO, theObject, theFindMethod);
792
793           if (aParts->length() > 0) {
794             // try to build an argument from a set of its sub-shapes,
795             // that published and will be reconstructed
796             if (aParts->length() > 1) {
797               aSubO = aShapesOp->MakeCompound(aParts);
798             }
799             else {
800               aSubO = aParts[0];
801             }
802             if (!CORBA::is_nil(aSubO)) {
803               // Publish the sub-shape
804               TCollection_AsciiString aSubName ("from_parts_of_");
805               aSubName += anArgName;
806               aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
807               // Restore color
808               aSubO->SetColor(anArgO->GetColor());
809             }
810           }
811           else {
812             // remove created aSubSO, because no parts have been found
813             aStudyBuilder->RemoveObject(aSubSO);
814           }
815         }
816       } // try to build from published parts
817     }
818   } // process arguments
819
820   return true;
821 }
822
823 //============================================================================
824 // function : RestoreSubShapesOneLevel
825 // purpose  : Private method
826 //============================================================================
827 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapesOneLevel (SALOMEDS::Study_ptr     theStudy,
828                                                       SALOMEDS::SObject_ptr   theOldSO,
829                                                       SALOMEDS::SObject_ptr   theNewSO,
830                                                       GEOM::GEOM_Object_ptr   theNewO,
831                                                       GEOM::find_shape_method theFindMethod)
832 {
833   int i = 0;
834   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
835   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theOldSO) ||
836       CORBA::is_nil(theNewO) || CORBA::is_nil(theNewSO))
837     return aParts._retn();
838
839   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
840
841   // Get interface, containing method, which we will use to reconstruct sub-shapes
842   GEOM::GEOM_IShapesOperations_var aShapesOp = GetIShapesOperations(theStudy->StudyId());
843   GEOM::GEOM_IGroupOperations_var  aGroupOp  = GetIGroupOperations(theStudy->StudyId());
844
845   // Reconstruct published sub-shapes
846   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator(theOldSO);
847
848   int aLen = 0;
849   for (it->Init(); it->More(); it->Next()) {
850     aLen++;
851   }
852   aParts->length(aLen);
853
854   for (it->Init(); it->More(); it->Next()) {
855     SALOMEDS::SObject_var anOldSubSO = it->Value();
856
857     TCollection_AsciiString anArgName = anOldSubSO->GetName();
858
859     SALOMEDS::GenericAttribute_var anAttr;
860     if (anOldSubSO->FindAttribute(anAttr, "AttributeIOR")) {
861       SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
862       GEOM::GEOM_Object_var anOldSubO =
863         GEOM::GEOM_Object::_narrow(_orb->string_to_object(anAttrIOR->Value()));
864       if (!CORBA::is_nil(anOldSubO)) {
865         // Find a sub-shape of theNewO in place of anOldSubO
866         GEOM::GEOM_Object_var aNewSubO;
867         switch (theFindMethod) {
868         case GEOM::FSM_GetInPlace:
869           {
870             // Use GetInPlace
871             aNewSubO = aShapesOp->GetInPlace(theNewO, anOldSubO);
872           }
873           break;
874         case GEOM::FSM_Transformed:
875           {
876             // transformation, cannot use GetInPlace, operate with indices
877             GEOM::ListOfLong_var anIDs = anOldSubO->GetSubShapeIndices();
878             if (anIDs->length() > 1) {
879               // group
880               aNewSubO = aGroupOp->CreateGroup(theNewO, aGroupOp->GetType(anOldSubO));
881               if (!CORBA::is_nil(aNewSubO))
882                 aGroupOp->UnionIDs(aNewSubO, anIDs);
883             }
884             else {
885               // single sub-shape
886               aNewSubO = aShapesOp->GetSubShape(theNewO, anIDs[0]);
887             }
888           }
889           break;
890         case GEOM::FSM_GetSame:
891           {
892             // Use GetSame
893             aNewSubO = aShapesOp->GetSame(theNewO, anOldSubO);
894           }
895           break;
896         case GEOM::FSM_GetShapesOnShape:
897           {
898             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
899             aNewSubO = aShapesOp->GetShapesOnShapeAsCompound(anOldSubO, theNewO,
900                                                              (short)GEOM::SOLID, GEOM::ST_ONIN);
901           }
902           break;
903         case GEOM::FSM_GetInPlaceByHistory:
904           {
905             // Use GetInPlaceByHistory
906             aNewSubO = aShapesOp->GetInPlaceByHistory(theNewO, anOldSubO);
907           }
908           break;
909         default:
910           {}
911         }
912
913         if (!CORBA::is_nil(aNewSubO)) {
914           // add the part to the list
915           aParts[i] = aNewSubO;
916           i++;
917
918           // Publish the sub-shape
919           TCollection_AsciiString aSubName ("from_");
920           aSubName += anArgName;
921           SALOMEDS::SObject_var aNewSubSO = aStudyBuilder->NewObject(theNewSO);
922           aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
923           // Restore color
924           aNewSubO->SetColor(anOldSubO->GetColor());
925
926           // Restore published sub-shapes of the argument
927           if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
928             // pass the main shape as Object, because only it has the history
929             RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO, theNewO, theFindMethod);
930           else
931             RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO, aNewSubO, theFindMethod);
932         }
933         else { // GetInPlace failed, try to build from published parts
934           SALOMEDS::SObject_var aNewSubSO = aStudyBuilder->NewObject(theNewSO);
935
936           // Restore published sub-shapes of the argument
937           GEOM::ListOfGO_var aParts =
938             RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO, theNewO, theFindMethod);
939
940           if (aParts->length() > 0) {
941             // try to build an object from a set of its sub-shapes,
942             // that published and will be reconstructed
943             if (aParts->length() > 1) {
944               aNewSubO = aShapesOp->MakeCompound(aParts);
945             }
946             else {
947               aNewSubO = aParts[0];
948             }
949
950             if (!CORBA::is_nil(aNewSubO)) {
951               // add the part to the list
952               aParts[i] = aNewSubO;
953               i++;
954
955               // Publish the sub-shape
956               TCollection_AsciiString aSubName = "from_parts_of_";
957               aSubName += anArgName;
958               aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
959               // Restore color
960               aNewSubO->SetColor(anOldSubO->GetColor());
961             }
962           }
963           else {
964             // remove created aSubSO, because no parts have been found
965             aStudyBuilder->RemoveObject(aNewSubSO);
966           }
967         } // try to build from published parts
968       }
969     }
970   } // iterate on published sub-shapes
971
972   aParts->length(i);
973   return aParts._retn();
974 }
975
976 //============================================================================
977 // function : register()
978 // purpose  : register 'name' in 'name_service'
979 //============================================================================
980 void GEOM_Gen_i::register_name(char * name)
981 {
982   GEOM::GEOM_Gen_var g = _this();
983   name_service->Register(g, name);
984 }
985
986 //============================================================================
987 // function : Undo
988 // purpose  :
989 //============================================================================
990 void GEOM_Gen_i::Undo(CORBA::Long theStudyID)
991 {
992   _impl->Undo(theStudyID);
993 }
994
995 //============================================================================
996 // function : Redo
997 // purpose  :
998 //============================================================================
999 void GEOM_Gen_i::Redo(CORBA::Long theStudyID)
1000 {
1001   _impl->Redo(theStudyID);
1002 }
1003
1004 //============================================================================
1005 // function : GetIBasicOperations
1006 // purpose  :
1007 //============================================================================
1008 GEOM::GEOM_IBasicOperations_ptr GEOM_Gen_i::GetIBasicOperations(CORBA::Long theStudyID)
1009      throw ( SALOME::SALOME_Exception )
1010 {
1011   Unexpect aCatch(SALOME_SalomeException);
1012   MESSAGE( "GEOM_Gen_i::GetIBasicOperations" );
1013
1014   GEOM::GEOM_Gen_ptr engine = _this();
1015
1016   //transfer reference on engine
1017   GEOM_IBasicOperations_i* aServant =
1018     new GEOM_IBasicOperations_i(_poa, engine, _impl->GetIBasicOperations(theStudyID));
1019
1020   // activate the CORBA servant
1021   GEOM::GEOM_IBasicOperations_var operations = aServant->_this();
1022   return operations._retn();
1023 }
1024
1025 //============================================================================
1026 // function : GetITransformOperations
1027 // purpose  :
1028 //============================================================================
1029 GEOM::GEOM_ITransformOperations_ptr GEOM_Gen_i::GetITransformOperations(CORBA::Long theStudyID)
1030      throw ( SALOME::SALOME_Exception )
1031 {
1032   Unexpect aCatch(SALOME_SalomeException);
1033   MESSAGE( "GEOM_Gen_i::GetITransformOperations" );
1034
1035   GEOM::GEOM_Gen_ptr engine = _this();
1036
1037   GEOM_ITransformOperations_i* aServant =
1038     new GEOM_ITransformOperations_i(_poa, engine, _impl->GetITransformOperations(theStudyID));
1039
1040   // activate the CORBA servant
1041   GEOM::GEOM_ITransformOperations_var operations = aServant->_this();
1042   return operations._retn();
1043 }
1044
1045 //============================================================================
1046 // function : GetI3DPrimOperations
1047 // purpose  :
1048 //============================================================================
1049 GEOM::GEOM_I3DPrimOperations_ptr GEOM_Gen_i::GetI3DPrimOperations(CORBA::Long theStudyID)
1050      throw ( SALOME::SALOME_Exception )
1051 {
1052   Unexpect aCatch(SALOME_SalomeException);
1053   MESSAGE( "GEOM_Gen_i::GetI3DPrimOperations" );
1054
1055   GEOM::GEOM_Gen_ptr engine = _this();
1056
1057   GEOM_I3DPrimOperations_i* aServant =
1058     new GEOM_I3DPrimOperations_i(_poa, engine, _impl->GetI3DPrimOperations(theStudyID));
1059
1060   // activate the CORBA servant
1061   GEOM::GEOM_I3DPrimOperations_var operations = aServant->_this();
1062   return operations._retn();
1063 }
1064
1065 //============================================================================
1066 // function : GetIShapesOperations
1067 // purpose  :
1068 //============================================================================
1069 GEOM::GEOM_IShapesOperations_ptr GEOM_Gen_i::GetIShapesOperations(CORBA::Long theStudyID)
1070      throw ( SALOME::SALOME_Exception )
1071 {
1072   Unexpect aCatch(SALOME_SalomeException);
1073   MESSAGE( "GEOM_Gen_i::GetIShapesOperations" );
1074
1075   GEOM::GEOM_Gen_ptr engine = _this();
1076
1077   GEOM_IShapesOperations_i* aServant =
1078     new GEOM_IShapesOperations_i(_poa, engine, _impl->GetIShapesOperations(theStudyID));
1079
1080   // activate the CORBA servant
1081   GEOM::GEOM_IShapesOperations_var operations = aServant->_this();
1082   return operations._retn();
1083 }
1084
1085 //============================================================================
1086 // function : GetIBlocksOperations
1087 // purpose  :
1088 //============================================================================
1089 GEOM::GEOM_IBlocksOperations_ptr GEOM_Gen_i::GetIBlocksOperations(CORBA::Long theStudyID)
1090      throw ( SALOME::SALOME_Exception )
1091 {
1092   Unexpect aCatch(SALOME_SalomeException);
1093   MESSAGE( "GEOM_Gen_i::GetIBlocksOperations" );
1094
1095   GEOM::GEOM_Gen_ptr engine = _this();
1096
1097   GEOM_IBlocksOperations_i* aServant =
1098     new GEOM_IBlocksOperations_i(_poa, engine, _impl->GetIBlocksOperations(theStudyID));
1099
1100   // activate the CORBA servant
1101   GEOM::GEOM_IBlocksOperations_var operations = aServant->_this();
1102   return operations._retn();
1103 }
1104
1105 //============================================================================
1106 // function : GetIBooleanOperations
1107 // purpose  :
1108 //============================================================================
1109 GEOM::GEOM_IBooleanOperations_ptr GEOM_Gen_i::GetIBooleanOperations(CORBA::Long theStudyID)
1110      throw ( SALOME::SALOME_Exception )
1111 {
1112   Unexpect aCatch(SALOME_SalomeException);
1113   MESSAGE( "GEOM_Gen_i::GetIBooleanOperations" );
1114
1115   GEOM::GEOM_Gen_ptr engine = _this();
1116
1117   GEOM_IBooleanOperations_i* aServant =
1118     new GEOM_IBooleanOperations_i(_poa, engine, _impl->GetIBooleanOperations(theStudyID));
1119
1120   // activate the CORBA servant
1121   GEOM::GEOM_IBooleanOperations_var operations = aServant->_this();
1122   return operations._retn();
1123 }
1124
1125 //============================================================================
1126 // function : GetICurvesOperations
1127 // purpose  :
1128 //============================================================================
1129 GEOM::GEOM_ICurvesOperations_ptr GEOM_Gen_i::GetICurvesOperations(CORBA::Long theStudyID)
1130      throw ( SALOME::SALOME_Exception )
1131 {
1132   Unexpect aCatch(SALOME_SalomeException);
1133   MESSAGE( "GEOM_Gen_i::GetICurvesOperations" );
1134
1135   GEOM::GEOM_Gen_ptr engine = _this();
1136
1137   GEOM_ICurvesOperations_i* aServant =
1138     new GEOM_ICurvesOperations_i(_poa, engine, _impl->GetICurvesOperations(theStudyID));
1139
1140   // activate the CORBA servant
1141   GEOM::GEOM_ICurvesOperations_var operations = aServant->_this();
1142   return operations._retn();
1143 }
1144
1145 //============================================================================
1146 // function : GetILocalOperations
1147 // purpose  :
1148 //============================================================================
1149 GEOM::GEOM_ILocalOperations_ptr GEOM_Gen_i::GetILocalOperations(CORBA::Long theStudyID)
1150      throw ( SALOME::SALOME_Exception )
1151 {
1152   Unexpect aCatch(SALOME_SalomeException);
1153   MESSAGE( "GEOM_Gen_i::GetILocalOperations" );
1154
1155   GEOM::GEOM_Gen_ptr engine = _this();
1156
1157   GEOM_ILocalOperations_i* aServant =
1158     new GEOM_ILocalOperations_i(_poa, engine, _impl->GetILocalOperations(theStudyID));
1159
1160   // activate the CORBA servant
1161   GEOM::GEOM_ILocalOperations_var operations = aServant->_this();
1162   return operations._retn();
1163 }
1164
1165 //============================================================================
1166 // function : GetIHealingOperations
1167 // purpose  :
1168 //============================================================================
1169 GEOM::GEOM_IHealingOperations_ptr GEOM_Gen_i::GetIHealingOperations(CORBA::Long theStudyID)
1170      throw ( SALOME::SALOME_Exception )
1171 {
1172   Unexpect aCatch(SALOME_SalomeException);
1173   MESSAGE( "GEOM_Gen_i::IHealingOperations" );
1174
1175   GEOM::GEOM_Gen_ptr engine = _this();
1176
1177   GEOM_IHealingOperations_i* aServant =
1178     new GEOM_IHealingOperations_i(_poa, engine, _impl->GetIHealingOperations(theStudyID));
1179
1180   // activate the CORBA servant
1181   GEOM::GEOM_IHealingOperations_var operations = aServant->_this();
1182   return operations._retn();
1183 }
1184
1185 //============================================================================
1186 // function : GetIInsertOperations
1187 // purpose  :
1188 //============================================================================
1189 GEOM::GEOM_IInsertOperations_ptr GEOM_Gen_i::GetIInsertOperations(CORBA::Long theStudyID)
1190      throw ( SALOME::SALOME_Exception )
1191 {
1192   Unexpect aCatch(SALOME_SalomeException);
1193   MESSAGE( "GEOM_Gen_i::GetIInsertOperations" );
1194
1195   GEOM::GEOM_Gen_ptr engine = _this();
1196
1197   GEOM_IInsertOperations_i* aServant =
1198     new GEOM_IInsertOperations_i(_poa, engine, _impl->GetIInsertOperations(theStudyID));
1199
1200   // activate the CORBA servant
1201   GEOM::GEOM_IInsertOperations_var operations = aServant->_this();
1202   return operations._retn();
1203 }
1204
1205 //============================================================================
1206 // function : GetIMeasureOperations
1207 // purpose  :
1208 //============================================================================
1209 GEOM::GEOM_IMeasureOperations_ptr GEOM_Gen_i::GetIMeasureOperations(CORBA::Long theStudyID)
1210      throw ( SALOME::SALOME_Exception )
1211 {
1212   Unexpect aCatch(SALOME_SalomeException);
1213   MESSAGE( "GEOM_Gen_i::GetIMeasureOperations" );
1214
1215   GEOM::GEOM_Gen_ptr engine = _this();
1216
1217   GEOM_IMeasureOperations_i* aServant =
1218     new GEOM_IMeasureOperations_i(_poa, engine, _impl->GetIMeasureOperations(theStudyID));
1219
1220   // activate the CORBA servant
1221   GEOM::GEOM_IMeasureOperations_var operations = aServant->_this();
1222   return operations._retn();
1223 }
1224
1225 //============================================================================
1226 // function : GetIGroupOperations
1227 // purpose  :
1228 //============================================================================
1229 GEOM::GEOM_IGroupOperations_ptr GEOM_Gen_i::GetIGroupOperations(CORBA::Long theStudyID)
1230      throw ( SALOME::SALOME_Exception )
1231 {
1232   Unexpect aCatch(SALOME_SalomeException);
1233   MESSAGE( "GEOM_Gen_i::GetIGroupOperations" );
1234
1235   GEOM::GEOM_Gen_ptr engine = _this();
1236
1237   GEOM_IGroupOperations_i* aServant =
1238     new GEOM_IGroupOperations_i(_poa, engine, _impl->GetIGroupOperations(theStudyID));
1239
1240   // activate the CORBA servant
1241   GEOM::GEOM_IGroupOperations_var operations = aServant->_this();
1242   return operations._retn();
1243 }
1244
1245 //=============================================================================
1246 /*!
1247  *  AddSubShape
1248  */
1249 //=============================================================================
1250 GEOM::GEOM_Object_ptr GEOM_Gen_i::AddSubShape (GEOM::GEOM_Object_ptr theMainShape,
1251                                                const GEOM::ListOfLong& theIndices)
1252 {
1253   if (CORBA::is_nil(theMainShape) || theIndices.length() < 1)
1254     return GEOM::GEOM_Object::_nil();
1255   CORBA::String_var entry = theMainShape->GetEntry();
1256   Handle(GEOM_Object) aMainsShape = _impl->GetObject(theMainShape->GetStudyID(), entry);
1257   if (aMainsShape.IsNull()) return GEOM::GEOM_Object::_nil();
1258
1259   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1, theIndices.length());
1260   for(Standard_Integer i = 0; i<theIndices.length(); i++) anArray->SetValue(i+1, theIndices[i]);
1261
1262   Handle(GEOM_Object) anObject = _impl->AddSubShape(aMainsShape, anArray, true);
1263   if(anObject.IsNull()) return GEOM::GEOM_Object::_nil();
1264
1265   TCollection_AsciiString anEntry;
1266   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
1267   return GetObject(anObject->GetDocID(), anEntry.ToCString());
1268 }
1269
1270 //=============================================================================
1271 /*!
1272  *  RemoveObject
1273  */
1274 //=============================================================================
1275 void GEOM_Gen_i::RemoveObject(GEOM::GEOM_Object_ptr theObject)
1276 {
1277   CORBA::String_var anEntry = theObject->GetEntry();
1278   Handle(GEOM_Object) anObject = _impl->GetObject(theObject->GetStudyID(), anEntry);
1279   if (anObject.IsNull()) return;
1280   _impl->RemoveObject(anObject);
1281   return;
1282 }
1283
1284
1285 //=================================================================================
1286 // function : GetStringFromIOR()
1287 // purpose  : returns a string that represents  a 'GEOM::GEOM_Object_var'
1288 //=================================================================================
1289 char* GEOM_Gen_i::GetStringFromIOR(GEOM::GEOM_Object_ptr theObject) {
1290   return _orb->object_to_string(theObject);
1291 }
1292
1293
1294 //=================================================================================
1295 // function : GetIORFromString()
1296 // purpose  : returns a 'GEOM::GEOM_Object_var' from a string representing it
1297 //=================================================================================
1298 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetIORFromString(const char* stringIOR) {
1299   GEOM::GEOM_Object_var aGeomObject;
1300   if(strcmp(stringIOR,"") != 0){
1301     CORBA::Object_var anObject = _orb->string_to_object(stringIOR);
1302     if(!CORBA::is_nil(anObject))
1303       aGeomObject =  GEOM::GEOM_Object::_narrow(anObject.in());
1304   }
1305   return aGeomObject._retn();
1306 }
1307
1308 //=================================================================================
1309 // function : GetObject()
1310 // purpose  :
1311 //=================================================================================
1312 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetObject (CORBA::Long theStudyID, const char* theEntry)
1313 {
1314   GEOM::GEOM_Object_var obj;
1315   Handle(GEOM_Object) handle_object = _impl->GetObject(theStudyID, (char*)theEntry);
1316   if (handle_object.IsNull()) return obj._retn();
1317
1318   TCollection_AsciiString stringIOR = handle_object->GetIOR();
1319   if (stringIOR.Length() > 1) {
1320     CORBA::Object_var corba_object = _orb->string_to_object(stringIOR.ToCString());
1321     if (!CORBA::is_nil(corba_object)) obj = GEOM::GEOM_Object::_narrow(corba_object);
1322     return obj._retn();
1323   }
1324
1325   GEOM::GEOM_Gen_ptr engine = _this();
1326   GEOM_Object_i* servant = new GEOM_Object_i (_poa, engine, handle_object);
1327
1328   obj = servant->_this();
1329   CORBA::String_var objStr = _orb->object_to_string(obj);
1330   TCollection_AsciiString anAscii( (char *)objStr.in() );
1331   handle_object->SetIOR( anAscii );
1332   return obj._retn();
1333 }
1334
1335 //=================================================================================
1336 // function : hasObjectInfo()
1337 // purpose  : shows if module provides information for its objects
1338 //=================================================================================
1339 bool GEOM_Gen_i::hasObjectInfo()
1340 {
1341   return true;
1342 }
1343
1344 //=================================================================================
1345 // function : getObjectInfo()
1346 // purpose  : returns an information for a given object by its entry
1347 //=================================================================================
1348 char* GEOM_Gen_i::getObjectInfo(CORBA::Long studyId, const char* entry)
1349 {
1350   GEOM::GEOM_Object_var aGeomObject;
1351  
1352   CORBA::Object_var aSMObject = name_service->Resolve( "/myStudyManager" );
1353   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow( aSMObject );
1354   SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID( studyId );
1355   SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( entry );
1356   SALOMEDS::SObject_var aResultSObj;
1357   if (aSObj->ReferencedObject(aResultSObj))
1358     aSObj = aResultSObj;
1359
1360   SALOMEDS::GenericAttribute_var anAttr;
1361   if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
1362     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1363     CORBA::String_var aVal = anIOR->Value();
1364     CORBA::Object_var anObject = aStudy->ConvertIORToObject(aVal);
1365     aGeomObject = GEOM::GEOM_Object::_narrow(anObject);
1366   }
1367   
1368   const char* aTypeInfo = "Object";
1369   if ( !aGeomObject->_is_nil() ) {
1370     GEOM::GEOM_IKindOfShape::shape_kind aKind;
1371     GEOM::ListOfLong_var anInts;
1372     GEOM::ListOfDouble_var aDbls;
1373
1374     GEOM::GEOM_IMeasureOperations_var anOp = GetIMeasureOperations( studyId );
1375     aKind = anOp->KindOfShape( aGeomObject, anInts, aDbls );
1376
1377     if ( anOp->IsDone() ) {
1378       switch ( aKind ) {
1379       case GEOM::GEOM_IKindOfShape::COMPOUND:
1380         aTypeInfo = "Compound";
1381         break;
1382       case GEOM::GEOM_IKindOfShape::COMPSOLID:
1383         aTypeInfo = "CompSolid";
1384         break;
1385       case GEOM::GEOM_IKindOfShape::SHELL:
1386         aTypeInfo = "Shell";
1387         break;
1388       case GEOM::GEOM_IKindOfShape::WIRE:
1389         if ( anInts[0] == 1 )
1390           aTypeInfo = "Closed Wire";
1391         else if ( anInts[0] == 2 )
1392           aTypeInfo = "Opened Wire";
1393         else
1394           aTypeInfo = "Wire";
1395         break;
1396         // SOLIDs
1397       case GEOM::GEOM_IKindOfShape::SPHERE:
1398         aTypeInfo = "Sphere";
1399         break;
1400       case GEOM::GEOM_IKindOfShape::CYLINDER:
1401         aTypeInfo = "Cylinder";
1402         break;
1403       case GEOM::GEOM_IKindOfShape::BOX:
1404       case GEOM::GEOM_IKindOfShape::ROTATED_BOX:
1405         aTypeInfo = "Box";
1406         break;
1407       case GEOM::GEOM_IKindOfShape::TORUS:
1408         aTypeInfo = "Torus";
1409         break;
1410       case GEOM::GEOM_IKindOfShape::CONE:
1411         aTypeInfo = "Cone";
1412         break;
1413       case GEOM::GEOM_IKindOfShape::POLYHEDRON:
1414         aTypeInfo = "Polyhedron";
1415         break;
1416       case GEOM::GEOM_IKindOfShape::SOLID:
1417         aTypeInfo = "Solid";
1418         break;
1419         // FACEs
1420       case GEOM::GEOM_IKindOfShape::SPHERE2D:
1421         aTypeInfo = "Spherical Face";
1422         break;
1423       case GEOM::GEOM_IKindOfShape::CYLINDER2D:
1424         aTypeInfo = "Cylindrical Face";
1425         break;
1426       case GEOM::GEOM_IKindOfShape::TORUS2D:
1427         aTypeInfo = "Toroidal Face";
1428         break;
1429       case GEOM::GEOM_IKindOfShape::CONE2D:
1430         aTypeInfo = "Conical Face";
1431         break;
1432       case GEOM::GEOM_IKindOfShape::DISK_CIRCLE:
1433         aTypeInfo = "Disk";
1434         break;
1435       case GEOM::GEOM_IKindOfShape::DISK_ELLIPSE:
1436         aTypeInfo = "Elliptical Face";
1437         break;
1438       case GEOM::GEOM_IKindOfShape::POLYGON:
1439         aTypeInfo = "Polygon";
1440         break;
1441       case GEOM::GEOM_IKindOfShape::PLANE:
1442         aTypeInfo = "Plane";
1443         break;
1444       case GEOM::GEOM_IKindOfShape::PLANAR:
1445         aTypeInfo = "Planar Face";
1446         break;
1447       case GEOM::GEOM_IKindOfShape::FACE:
1448         aTypeInfo = "Face";
1449         break;
1450         // EDGEs
1451       case GEOM::GEOM_IKindOfShape::CIRCLE:
1452         aTypeInfo = "Circle";
1453         break;
1454       case GEOM::GEOM_IKindOfShape::ARC_CIRCLE:
1455         aTypeInfo = "Ark";
1456         break;
1457       case GEOM::GEOM_IKindOfShape::ELLIPSE:
1458         aTypeInfo = "Ellipse";
1459         break;
1460       case GEOM::GEOM_IKindOfShape::ARC_ELLIPSE:
1461         aTypeInfo = "Arc Ellipse";
1462         break;
1463       case GEOM::GEOM_IKindOfShape::LINE:
1464         aTypeInfo = "Line";
1465         break;
1466       case GEOM::GEOM_IKindOfShape::SEGMENT:
1467         aTypeInfo = "Segment";
1468         break;
1469       case GEOM::GEOM_IKindOfShape::EDGE:
1470         aTypeInfo = "Edge";
1471         break;
1472       case GEOM::GEOM_IKindOfShape::VERTEX:
1473         aTypeInfo = "Vertex";
1474         break;
1475       default:
1476         break;
1477       }
1478     }
1479   }
1480     
1481   char* anInfo = new char[strlen("Module ") + strlen(ComponentDataType()) + strlen(", ") + strlen(aTypeInfo) + 3];
1482   sprintf(anInfo, "Module %s, %s", ComponentDataType(), aTypeInfo);
1483   
1484   char* ret = CORBA::string_dup(anInfo);
1485   delete [] anInfo;
1486   return ret;
1487 }
1488
1489 //=====================================================================================
1490 // EXPORTED METHODS
1491 //=====================================================================================
1492 extern "C"
1493 {
1494 GEOM_I_EXPORT
1495   PortableServer::ObjectId * GEOMEngine_factory(CORBA::ORB_ptr orb,
1496                                                 PortableServer::POA_ptr poa,
1497                                                 PortableServer::ObjectId * contId,
1498                                                 const char *instanceName,
1499                                                 const char * interfaceName)
1500   {
1501    GEOM_Gen_i * myGEOM_Gen_i = new GEOM_Gen_i(orb, poa, contId, instanceName, interfaceName);
1502    // Don't understand the reason of this register ????
1503 //   myGEOM_Gen_i->register_name("/myGEOM_Gen"); // NRI : 11/07/2002 : Add for Supervision example
1504    return myGEOM_Gen_i->getId();
1505   }
1506 }