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