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