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