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