Salome HOME
Fix for the Bug IPAL22045 TC5.1.5: Dump python doesn't restore GEOM-012 result
[modules/geom.git] / src / GEOM_I / GEOM_Gen_i.cc
1 //  Copyright (C) 2007-2010  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 <sstream>
33
34 #include "Utils_CorbaException.hxx"
35 #include "OpUtil.hxx"
36 #include "Utils_ExceptHandlers.hxx"
37 #include "utilities.h"
38
39 #include "GEOM_Object_i.hh"
40 #include "GEOM_Object.hxx"
41 #include "GEOM_Function.hxx"
42 #include "GEOM_ISubShape.hxx"
43 #include <GEOM_PythonDump.hxx>
44 #include "GEOMImpl_Types.hxx"
45 #include "GEOMImpl_CopyDriver.hxx"
46
47 // Cascade headers
48 #include <BRep_Builder.hxx>
49 #include <BRepTools.hxx>
50 #include <TDF_Label.hxx>
51 #include <TDF_Tool.hxx>
52 #include <TDF_ChildIDIterator.hxx>
53 #include <TNaming_NamedShape.hxx>
54 #include <TDataStd_Name.hxx>
55 #include <TCollection_AsciiString.hxx>
56 #include <TColStd_HArray1OfInteger.hxx>
57 #include <TopAbs_ShapeEnum.hxx>
58 //#include <TopTools_IndexedMapOfShape.hxx>
59 #include <TopExp.hxx>
60 #include <OSD.hxx>
61
62 #include "SALOMEDS_Tool.hxx"
63
64 //============================================================================
65 // function : GEOM_Gen_i()
66 // purpose  : constructor to be called for servant creation.
67 //============================================================================
68 GEOM_Gen_i::GEOM_Gen_i(CORBA::ORB_ptr            orb,
69                        PortableServer::POA_ptr   poa,
70                        PortableServer::ObjectId* contId,
71                        const char*               instanceName,
72                        const char*               interfaceName) :
73   Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
74 {
75   _thisObj = this;
76   _id = _poa->activate_object(_thisObj);
77   name_service = new SALOME_NamingService(_orb);
78
79   _impl = new ::GEOMImpl_Gen;
80
81   //PAL10867: disable signals catching with "noexcepthandler" option
82   char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
83   if (!envNoCatchSignals || !atoi(envNoCatchSignals))
84   {
85     //work around PAL12004, PAL12628
86     //OSD::SetSignal( true );
87     bool raiseFPE;
88 #ifdef _DEBUG_
89     raiseFPE = true;
90     char* envDisableFPE = getenv("DISABLE_FPE");
91     if (envDisableFPE && atoi(envDisableFPE))
92       raiseFPE = false;
93 #else
94     raiseFPE = false;
95 #endif
96     OSD::SetSignal( raiseFPE );
97   }
98 }
99
100 //============================================================================
101 // function : ~GEOM_Gen_i()
102 // purpose  : destructor
103 //============================================================================
104 GEOM_Gen_i::~GEOM_Gen_i() {
105   delete name_service;
106   delete _impl;
107 }
108
109
110 //============================================================================
111 // function : IORToLocalPersistentID()
112 // purpose  :
113 //============================================================================
114 char* GEOM_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
115                                          const char* IORString,
116                                          CORBA::Boolean isMultiFile,
117                                          CORBA::Boolean isASCII)
118 {
119   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(_orb->string_to_object(IORString));
120   if (!CORBA::is_nil(anObject)) {
121     return CORBA::string_dup(anObject->GetEntry());
122   }
123   return 0;
124 }
125
126
127 //============================================================================
128 // function : LocalPersistentIDToIOR()
129 // purpose  : Create/Load CORBA object from a persistent ref (an entry)
130 //          : Used when a study is loaded
131 //          : The IOR (IORName) of object created is returned
132 //============================================================================
133 char* GEOM_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
134                                          const char* aLocalPersistentID,
135                                          CORBA::Boolean isMultiFile,
136                                          CORBA::Boolean isASCII)
137 {
138   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
139
140   Handle(GEOM_Object) anObject = _impl->GetObject(aStudy->StudyId(), const_cast<char*>(aLocalPersistentID));
141   TCollection_AsciiString anEntry;
142   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
143   GEOM::GEOM_Object_var obj = GetObject(anObject->GetDocID(), anEntry.ToCString());
144
145   CORBA::String_var aPersRefString = _orb->object_to_string(obj);
146   return CORBA::string_dup(aPersRefString);
147 }
148
149 //============================================================================
150 // function : CanPublishInStudy
151 // purpose  :
152 //============================================================================
153 bool GEOM_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
154 {
155   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(theIOR);
156   return !(anObject->_is_nil());
157 }
158
159
160 //============================================================================
161 // function : PublishInStudy
162 // purpose  :
163 //============================================================================
164 SALOMEDS::SObject_ptr GEOM_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
165                                                  SALOMEDS::SObject_ptr theSObject,
166                                                  CORBA::Object_ptr theObject,
167                                                  const char* theName) throw (SALOME::SALOME_Exception)
168 {
169   Unexpect aCatch(SALOME_SalomeException);
170   SALOMEDS::SObject_var aResultSO;
171   if(CORBA::is_nil(theObject) || theStudy->_is_nil()) return aResultSO;
172   GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow(theObject);
173   if(aShape->_is_nil()) return aResultSO;
174
175   SALOMEDS::GenericAttribute_var anAttr;
176   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder();
177
178   SALOMEDS::SComponent_var       aFather = theStudy->FindComponent("GEOM");
179   if (aFather->_is_nil()) {
180     aFather = aStudyBuilder->NewComponent("GEOM");
181     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributeName");
182     SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
183     aName->SetValue("Geometry");
184     aName->Destroy();
185     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
186     SALOMEDS::AttributePixMap_var aPixMap=SALOMEDS::AttributePixMap::_narrow(anAttr);
187     aPixMap->SetPixMap("ICON_OBJBROWSER_Geometry");
188     aPixMap->Destroy();
189     aStudyBuilder->DefineComponentInstance(aFather, (GEOM::GEOM_Gen_var)GEOM_Gen::_this());
190   }
191   if (aFather->_is_nil()) return aResultSO;
192
193   if (CORBA::is_nil(theSObject)) {
194     aResultSO = aStudyBuilder->NewObject(aFather);
195   } else {
196     if (!theSObject->ReferencedObject(aResultSO))
197       aResultSO = SALOMEDS::SObject::_duplicate(theSObject); //SRN: Added Aug 24,2004 : for  the method AddInStudy with theFather argumenet != NULL
198       //THROW_SALOME_CORBA_EXCEPTION("Publish in study supervision graph error",SALOME::BAD_PARAM);
199   }
200   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
201   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
202   CORBA::String_var aGeomObjIOR = _orb->object_to_string(theObject);
203   anIOR->SetValue(aGeomObjIOR);
204   anIOR->Destroy();
205
206   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributePixMap");
207   SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
208   TCollection_AsciiString aShapeName("Shape_");
209
210   if ( aShape->GetType() == GEOM_GROUP ) {
211     GEOM::GEOM_IGroupOperations_var anOp = GetIGroupOperations( theStudy->StudyId() );
212     switch ( (TopAbs_ShapeEnum)anOp->GetType( aShape ) ) {
213     case TopAbs_VERTEX:
214       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_PNT" );
215       aShapeName = "Group_Of_Vertices_";
216       break;
217     case TopAbs_EDGE:
218       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_EDGE" );
219       aShapeName = "Group_Of_Edges_";
220       break;
221     case TopAbs_FACE:
222       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_FACE" );
223       aShapeName = "Group_Of_Faces_";
224       break;
225     case TopAbs_SOLID:
226       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_SOLID" );
227       aShapeName = "Group_Of_Solids_";
228       break;
229     }
230   } else if ( aShape->GetType() == GEOM_MARKER ) {
231     aPixmap->SetPixMap( "ICON_OBJBROWSER_LCS" );
232     aShapeName = "LocalCS_";
233   } else if ( aShape->GetType() > ADVANCED_BASE ) {
234     char buf[20];
235     sprintf( buf, "%d", aShape->GetType() );
236     std::string advId = "ICON_OBJBROWSER_ADVANCED_"; advId += buf;
237     aPixmap->SetPixMap( advId.c_str() );
238     aShapeName = "Advanced_";
239   } else if ( aShape->GetShapeType() == GEOM::COMPOUND ) {
240     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPOUND" );
241     aShapeName = "Compound_";
242   } else if ( aShape->GetShapeType() == GEOM::COMPSOLID ) {
243     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPSOLID" );
244     aShapeName = "Compsolid_";
245   } else if ( aShape->GetShapeType() == GEOM::SOLID ) {
246     aPixmap->SetPixMap( "ICON_OBJBROWSER_SOLID" );
247     aShapeName = "Solid_";
248   } else if ( aShape->GetShapeType() == GEOM::SHELL ) {
249     aPixmap->SetPixMap( "ICON_OBJBROWSER_SHELL" );
250     aShapeName = "Shell_";
251   } else if ( aShape->GetShapeType() == GEOM::FACE ) {
252     aPixmap->SetPixMap( "ICON_OBJBROWSER_FACE" );
253     aShapeName = "Face_";
254   } else if ( aShape->GetShapeType() == GEOM::WIRE ) {
255     aPixmap->SetPixMap( "ICON_OBJBROWSER_WIRE" );
256     aShapeName = "Wire_";
257   } else if ( aShape->GetShapeType() == GEOM::EDGE ) {
258     aPixmap->SetPixMap( "ICON_OBJBROWSER_EDGE" );
259     aShapeName = "Edge_";
260   } else if ( aShape->GetShapeType() == GEOM::VERTEX ) {
261     aPixmap->SetPixMap( "ICON_OBJBROWSER_VERTEX" );
262     aShapeName = "Vertex_";
263   }
264   aPixmap->Destroy();
265   //if (strlen(theName) == 0) aShapeName += TCollection_AsciiString(aResultSO->Tag());
266   //else aShapeName = TCollection_AsciiString(CORBA::string_dup(theName));
267
268   // BEGIN: try to find existed name for current shape
269   bool HasName = false;
270   // recieve current TopoDS shape
271   CORBA::String_var entry = aShape->GetEntry();
272   Handle(GEOM_Object) aGShape = _impl->GetObject(aShape->GetStudyID(), entry);
273   TopoDS_Shape TopoSh = aGShape->GetValue();
274   // find label of main shape
275   GEOM::GEOM_Object_var aMainSh = aShape;
276   while( !aMainSh->IsMainShape() ) {
277     aMainSh = aMainSh->GetMainShape();
278   }
279   entry = aMainSh->GetEntry();
280   Handle(GEOM_Object) anObj = _impl->GetObject(aMainSh->GetStudyID(), entry);
281   TDF_Label aMainLbl = anObj->GetFunction(1)->GetNamingEntry();
282
283   // check all named shapes using iterator
284   TDF_ChildIDIterator anIt (aMainLbl, TNaming_NamedShape::GetID(), Standard_True);
285
286   for (; anIt.More() && !HasName; anIt.Next()) {
287     Handle(TNaming_NamedShape) anAttr =
288       Handle(TNaming_NamedShape)::DownCast(anIt.Value());
289     if (anAttr.IsNull()) continue;
290     TopoDS_Shape S = anAttr->Get();
291     if (S.IsEqual(TopoSh)) {
292       TDF_Label L = anAttr->Label();
293       Handle(TDataStd_Name) aName;
294       if (L.FindAttribute(TDataStd_Name::GetID(), aName)) {
295         aShapeName = aName->Get();
296         HasName = true;
297       }
298     }
299   }
300   // END: try to find existed name for current shape
301
302   if (!HasName) {
303     // asv : 11.11.04 Introducing a more sofisticated method of name creation, just as
304     //       it is done in GUI in GEOMBase::GetDefaultName() - not just add a Tag() == number
305     //       of objects in the study, but compute a number of objects with the same prefix
306     //       and build a new name as Prefix_N+1
307     if ( strlen( theName ) == 0 ) { // MOST PROBABLY CALLED FROM BATCHMODE OR SUPERVISOR
308       int i = 0;                    // (WITH EMPTY NEW NAME)
309       SALOMEDS::SObject_var obj;
310       TCollection_AsciiString aNewShapeName;
311       do {
312         aNewShapeName = aShapeName + TCollection_AsciiString(++i);
313         obj = theStudy->FindObject( aNewShapeName.ToCString() );
314       }
315       while ( !obj->_is_nil() );
316       aShapeName = aNewShapeName;
317     }
318     else // MOST PROBABLY CALLED FROM GEOM GUI (ALREADY WITH VALID NAME)
319       aShapeName = TCollection_AsciiString((char*)theName);
320   }
321
322   //Set the study entry as a name of  the published GEOM_Object
323   CORBA::String_var anID =aResultSO->GetID();
324   aShape->SetStudyEntry(anID.in());
325
326   //Set a name of the added shape
327   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeName");
328   SALOMEDS::AttributeName_var aNameAttrib = SALOMEDS::AttributeName::_narrow(anAttr);
329   aNameAttrib->SetValue(aShapeName.ToCString());
330   aNameAttrib->Destroy();
331
332   //Set NoteBook variables used in the object creation
333   TCollection_AsciiString aVars;
334   CORBA::String_var aString=aShape->GetParameters();
335   SALOMEDS::ListOfListOfStrings_var aSections = theStudy->ParseVariables(aString);
336   for(int i = 0, n = aSections->length(); i < n; i++) {
337     SALOMEDS::ListOfStrings aListOfVars = aSections[i];
338     for(int j = 0, m = aListOfVars.length(); j < m; j++) {
339       if(theStudy->IsVariable(aListOfVars[j].in()))
340         aVars += TCollection_AsciiString(aListOfVars[j].in());
341       if(j != m-1)
342         aVars += ":";
343     }
344     if(i != n-1)
345       aVars += "|";
346   }
347   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeString");
348   SALOMEDS::AttributeString_var aStringAttrib = SALOMEDS::AttributeString::_narrow(anAttr);
349   aStringAttrib->SetValue(aVars.ToCString());
350   aStringAttrib->Destroy();
351
352   aFather->Destroy();
353
354   //Set a name of the GEOM object
355   aShape->SetName(theName);
356
357   return aResultSO._retn();
358 }
359
360
361 //============================================================================
362 // function : CreateAndPublishGroup
363 // purpose  : auxilary for PublishNamedShapesInStudy
364 //============================================================================
365 void GEOM_Gen_i::CreateAndPublishGroup(SALOMEDS::Study_ptr theStudy,
366                                        GEOM::GEOM_Object_var theMainShape,
367                                        const TopTools_IndexedMapOfShape& anIndices,
368                                        const TopTools_SequenceOfShape& SeqS,
369                                        const TColStd_SequenceOfAsciiString& SeqN,
370                                        const Standard_CString& GrName,
371                                        GEOM::ListOfGO_var aResList)
372 {
373   CORBA::String_var entry = theMainShape->GetEntry();
374   Handle(GEOM_Object) aMainShape = _impl->GetObject(theMainShape->GetStudyID(), entry);
375   Handle(TColStd_HArray1OfInteger) anArray;
376   if(SeqS.Length()>0) {
377     // create a group
378     GEOM::GEOM_IGroupOperations_var GOp = GetIGroupOperations(theStudy->StudyId());
379     GEOM::GEOM_Object_ptr GrObj =
380       GOp->CreateGroup( theMainShape, SeqS.Value(1).ShapeType() );
381     AddInStudy(theStudy, GrObj, GrName, theMainShape._retn());
382     CORBA::String_var GrEntry = GrObj->GetEntry();
383     Handle(GEOM_Object) HGrObj = _impl->GetObject(GrObj->GetStudyID(), GrEntry);
384     // add named objects
385     //Handle(GEOM_Object) anObj;
386     for(int i=1; i<=SeqS.Length(); i++) {
387       TopoDS_Shape aValue = SeqS.Value(i);
388       //anArray = new TColStd_HArray1OfInteger(1,1);
389       Standard_Integer anIndex = anIndices.FindIndex(aValue);
390       //anArray->SetValue(1, anIndex);
391       GOp->AddObject(GrObj,anIndex);
392       //anObj = GEOM_Engine::GetEngine()->AddObject(aMainShape->GetDocID(), GEOM_SUBSHAPE);
393       //if (anObj.IsNull()) continue;
394       //Handle(GEOM_Function) aFunction = anObj->AddFunction(GEOM_Object::GetSubShapeID(), 1);
395       //if (aFunction.IsNull()) continue;
396       //GEOM_ISubShape aSSI(aFunction);
397       //aSSI.SetMainShape(aMainShape->GetLastFunction());
398       //aSSI.SetIndices(anArray);
399       //aFunction->SetValue(aValue);
400       //GOp->UnionIDs(GrObj, anIndex);
401       //SALOMEDS::SObject_var aResultSO;
402       //TCollection_AsciiString anEntry;
403       //TDF_Tool::Entry(anObj->GetEntry(),anEntry);
404       //GEOM::GEOM_Object_var aGObj = GetObject(anObj->GetDocID(), anEntry.ToCString());
405       //AddInStudy(theStudy, aGObj._retn(), SeqN.Value(i).ToCString(), GrObj);
406     }
407   }
408 }
409
410
411 //============================================================================
412 // function : PublishNamedShapesInStudy
413 // purpose  :
414 //============================================================================
415 GEOM::ListOfGO* GEOM_Gen_i::
416             PublishNamedShapesInStudy(SALOMEDS::Study_ptr theStudy,
417                                       //SALOMEDS::SObject_ptr theSObject,
418                                       CORBA::Object_ptr theObject)
419 {
420   //Unexpect aCatch(SALOME_SalomeException);
421   GEOM::ListOfGO_var aResList = new GEOM::ListOfGO;
422
423   //CORBA::Object_var theObject = theSObject->GetObject();
424   GEOM::GEOM_Object_var theMainShape = GEOM::GEOM_Object::_narrow(theObject);
425   if(theMainShape->_is_nil()) return aResList._retn();
426
427   CORBA::String_var entry = theMainShape->GetEntry();
428   Handle(GEOM_Object) aMainShape = _impl->GetObject(theMainShape->GetStudyID(), entry);
429   if (aMainShape.IsNull()) return aResList._retn();
430   TopoDS_Shape MainSh = aMainShape->GetValue();
431
432   TDF_Label aMainLbl = aMainShape->GetEntry();
433   TopTools_SequenceOfShape SolidSeqS, FaceSeqS, EdgeSeqS, VertSeqS;
434   TColStd_SequenceOfAsciiString SolidSeqN, FaceSeqN, EdgeSeqN, VertSeqN;
435   TDF_ChildIDIterator anIt(aMainLbl, TNaming_NamedShape::GetID(), Standard_True);
436   for(; anIt.More(); anIt.Next()) {
437     Handle(TNaming_NamedShape) anAttr =
438       Handle(TNaming_NamedShape)::DownCast(anIt.Value());
439     if(anAttr.IsNull()) continue;
440     TopoDS_Shape S = anAttr->Get();
441     TDF_Label L = anAttr->Label();
442     //if(S.IsEqual(MainSh)) continue;
443     Handle(TDataStd_Name) aName;
444     if(L.FindAttribute(TDataStd_Name::GetID(),aName)) {
445       TCollection_ExtendedString EName = aName->Get();
446       if(S.ShapeType()==TopAbs_SOLID) {
447         SolidSeqS.Append(S);
448         SolidSeqN.Append(aName->Get());
449       }
450       else if(S.ShapeType()==TopAbs_FACE) {
451         FaceSeqS.Append(S);
452         FaceSeqN.Append(aName->Get());
453       }
454       else if(S.ShapeType()==TopAbs_EDGE) {
455         EdgeSeqS.Append(S);
456         EdgeSeqN.Append(aName->Get());
457       }
458       else if(S.ShapeType()==TopAbs_VERTEX) {
459         VertSeqS.Append(S);
460         VertSeqN.Append(aName->Get());
461       }
462     }
463   }
464
465   TopTools_IndexedMapOfShape anIndices;
466   TopExp::MapShapes(MainSh, anIndices);
467
468   CreateAndPublishGroup(theStudy, theMainShape, anIndices, SolidSeqS, SolidSeqN,
469                         "Group_Of_Named_Solids", aResList);
470
471   CreateAndPublishGroup(theStudy, theMainShape, anIndices, FaceSeqS, FaceSeqN,
472                         "Group_Of_Named_Faces", aResList);
473
474   CreateAndPublishGroup(theStudy, theMainShape, anIndices, EdgeSeqS, EdgeSeqN,
475                         "Group_Of_Named_Edges", aResList);
476
477   CreateAndPublishGroup(theStudy, theMainShape, anIndices, VertSeqS, VertSeqN,
478                         "Group_Of_Named_Vertices", aResList);
479
480   return aResList._retn();
481 }
482
483
484 //============================================================================
485 // function : Save()
486 // purpose  : save OCAF/Geom document
487 //============================================================================
488 SALOMEDS::TMPFile* GEOM_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
489                                     const char* theURL,
490                                     bool isMultiFile) {
491   SALOMEDS::TMPFile_var aStreamFile;
492   // Get a temporary directory to store a file
493   std::string aTmpDir = (isMultiFile)?theURL:SALOMEDS_Tool::GetTmpDir();
494
495   // OCCT BUG: cannot save a document (in current folder)
496   // if directory name is empty
497   if (aTmpDir.size() == 0) {
498 #ifdef WNT
499     aTmpDir = ".\\";
500 #else
501     aTmpDir = "./";
502 #endif
503   }
504
505   // Create a list to store names of created files
506   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
507   aSeq->length(1);
508   // Prepare a file name to open
509   TCollection_AsciiString aNameWithExt("");
510   if (isMultiFile)
511     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath
512                                                    (theComponent->GetStudy()->URL())).c_str());
513   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
514   aSeq[0] = CORBA::string_dup(aNameWithExt.ToCString());
515   // Build a full file name of temporary file
516   TCollection_AsciiString aFullName = TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt;
517   // Save GEOM component in this file
518   _impl->Save(theComponent->GetStudy()->StudyId(),(char*) aFullName.ToCString());
519   // Conver a file to the byte stream
520   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
521   // Remove the created file and tmp directory
522   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
523
524   // Return the created byte stream
525   return aStreamFile._retn();
526 }
527
528
529 //============================================================================
530 // function : SaveASCII()
531 // purpose  :
532 //============================================================================
533 SALOMEDS::TMPFile* GEOM_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
534                                          const char* theURL,
535                                          bool isMultiFile) {
536   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
537   return aStreamFile._retn();
538 }
539
540
541 //============================================================================
542 // function : Load()
543 // purpose  :
544 //============================================================================
545 CORBA::Boolean GEOM_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
546                                 const SALOMEDS::TMPFile& theStream,
547                                 const char* theURL,
548                                 bool isMultiFile) {
549
550   if (theStream.length() <= 9) {
551     MESSAGE("The TMPFile is too short : " << theStream.length() << " bytes ");
552     return false;
553   }
554
555   // Get a temporary directory for a file
556   std::string aTmpDir = isMultiFile?theURL:SALOMEDS_Tool::GetTmpDir();
557
558   // OCCT BUG: cannot load a document (from current folder)
559   // if directory name is empty
560   if (aTmpDir.size() == 0) {
561 #ifdef WNT
562     aTmpDir = ".\\";
563 #else
564     aTmpDir = "./";
565 #endif
566   }
567
568   // Conver the byte stream theStream to a file and place it in tmp directory
569   SALOMEDS::ListOfFileNames_var aSeq =
570     SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.c_str(), isMultiFile);
571
572   // Prepare a file name to open
573   TCollection_AsciiString aNameWithExt("");
574   if (isMultiFile)
575     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath
576                                                    (theComponent->GetStudy()->URL())).c_str());
577   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
578   TCollection_AsciiString aFullName = (TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt);
579
580   // Open document
581   if (!_impl->Load(theComponent->GetStudy()->StudyId(),(char*) aFullName.ToCString())) return false;
582
583   // Remove the created file and tmp directory
584   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
585
586   SALOMEDS::Study_var Study = theComponent->GetStudy();
587   TCollection_AsciiString name (Study->Name());
588
589   return true;
590 }
591
592
593 //============================================================================
594 // function : LoadASCII()
595 // purpose  :
596 //============================================================================
597 CORBA::Boolean GEOM_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
598                                      const SALOMEDS::TMPFile& theStream,
599                                      const char* theURL,
600                                      bool isMultiFile) {
601   return Load(theComponent, theStream, theURL, isMultiFile);
602 }
603
604
605 //============================================================================
606 // function : Close()
607 // purpose  :
608 //============================================================================
609 void GEOM_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
610 {
611   SALOMEDS::Study_var aStudy= theComponent->GetStudy();
612   _impl->Close(aStudy->StudyId());
613 }
614
615 //============================================================================
616 // function : CanCopy()
617 // purpose  :
618 //============================================================================
619 CORBA::Boolean GEOM_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
620   // Try to retrieve known by Geometry component GEOM_Object by given IOR
621   SALOMEDS::GenericAttribute_var anAttr;
622   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
623
624   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
625
626   CORBA::String_var aString=anIOR->Value();
627   anIOR->Destroy();
628   CORBA::Object_var anObj = _orb->string_to_object(aString);
629   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(anObj);
630   // If the object is null one it can't be copied: return false
631   if (anObject->_is_nil()) return false;
632   return true;
633 }
634
635 //============================================================================
636 // function : CopyFrom()
637 // purpose  :
638 //============================================================================
639 SALOMEDS::TMPFile* GEOM_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID)
640 {
641   // Declare a sequence of the byte to store the copied object
642   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
643
644   // Try to get GEOM_Object object by given SObject
645   SALOMEDS::GenericAttribute_var anAttr;
646   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return aStreamFile._retn();
647   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow
648     (_orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()));
649   if (anObject->_is_nil()) return aStreamFile._retn();
650
651   aStreamFile = anObject->GetShapeStream();
652
653   // Assign an ID  the type of  GEOM_Object
654   theObjectID = anObject->GetType();
655
656   // Return created TMPFile
657   return aStreamFile._retn();
658 }
659
660 //============================================================================
661 // function : CanPaste()
662 // purpose  :
663 //============================================================================
664 CORBA::Boolean GEOM_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
665   // The Geometry component can paste only objects copied by Geometry component
666   // and with the object type = 1
667   if (strcmp(theComponentName, ComponentDataType()) != 0) return false;
668   return true;
669 }
670
671 //============================================================================
672 // function : PasteInto()
673 // purpose  :
674 //============================================================================
675 SALOMEDS::SObject_ptr GEOM_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
676                                             CORBA::Long theObjectID,
677                                             SALOMEDS::SObject_ptr theObject) {
678   // Find the current Study and StudyBuilder
679   SALOMEDS::Study_var aStudy = theObject->GetStudy();
680   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
681
682   // Retrieve a TopoDS_Shape from byte stream
683   TopoDS_Shape aTopology;
684   std::istringstream aStreamedBrep((char*) &theStream[0]);
685   BRep_Builder aBuilder;
686   try {
687     BRepTools::Read(aTopology, aStreamedBrep, aBuilder);
688   } catch (Standard_Failure) {
689     return false;
690   }
691
692   // SObject of the created shape is theObject or new Child of Component if theObject == geom component
693   SALOMEDS::SObject_var aNewSO;
694   if (strcmp(theObject->GetFatherComponent()->GetID(),theObject->GetID()) == 0) {
695     aNewSO = aStudyBuilder->NewObject(theObject);
696   } else aNewSO = SALOMEDS::SObject::_duplicate(theObject);
697
698
699   //Create a new GEOM_Object
700   Handle(GEOM_Object) anObj = _impl->AddObject(aNewSO->GetStudy()->StudyId(), theObjectID);
701   Handle(GEOM_Function) aFunction = anObj->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
702   aFunction->SetValue(aTopology);
703
704   TCollection_AsciiString anEntry;
705   TDF_Tool::Entry(anObj->GetEntry(), anEntry);
706   GEOM::GEOM_Object_var obj = GetObject(anObj->GetDocID(), anEntry.ToCString());
707
708   //Set the study entry of the published GEOM_Object
709   obj->SetStudyEntry(aNewSO->GetID());
710
711   // Add IORAttribute to the Study and set IOR of the created GEOM_Object to it
712   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSO, "AttributeIOR");
713   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
714   CORBA::String_var objStr = _orb->object_to_string(obj);
715   anIOR->SetValue(objStr.in());
716   anIOR->Destroy();
717
718   // Return the created in the Study SObject
719   return aNewSO._retn();
720 }
721
722 //============================================================================
723 // function : ComponentDataType()
724 // purpose  :
725 //============================================================================
726 char* GEOM_Gen_i::ComponentDataType()
727 {
728   return CORBA::string_dup("GEOM");
729 }
730
731 //============================================================================
732 // function : AddInStudy
733 // purpose  :
734 //============================================================================
735 SALOMEDS::SObject_ptr GEOM_Gen_i::AddInStudy (SALOMEDS::Study_ptr theStudy,
736                                               GEOM::GEOM_Object_ptr theObject,
737                                               const char* theName,
738                                               GEOM::GEOM_Object_ptr theFather)
739 {
740   SALOMEDS::SObject_var aResultSO;
741   if(theObject->_is_nil() || theStudy->_is_nil()) return aResultSO;
742
743   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
744   CORBA::String_var IOR;
745
746   if(!theFather->_is_nil()) {
747     IOR = _orb->object_to_string(theFather);
748     SALOMEDS::SObject_var aFatherSO = theStudy->FindObjectIOR(IOR.in());
749     if(aFatherSO->_is_nil()) return aResultSO._retn();
750     aResultSO = aStudyBuilder->NewObject(aFatherSO);
751     aFatherSO->Destroy();
752     //aStudyBuilder->Addreference(aResultSO, aResultSO);
753   }
754
755   aResultSO = PublishInStudy(theStudy, aResultSO, theObject, theName);
756   if(aResultSO->_is_nil()) return aResultSO._retn();
757
758   GEOM::ListOfGO_var aList = theObject->GetDependency();
759   Standard_Integer aLength = aList->length();
760   if(aLength < 1) return aResultSO._retn();
761
762   //Publish the arguments
763   for(Standard_Integer i = 0; i< aLength; i++) {
764     GEOM::GEOM_Object_var anObject = aList[i];
765     if(anObject->_is_nil()) continue;
766     IOR = _orb->object_to_string(anObject);
767     SALOMEDS::SObject_var aSO =  theStudy->FindObjectIOR(IOR.in());
768     if(aSO->_is_nil()) continue;
769     SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(aResultSO);
770     aStudyBuilder->Addreference(aSubSO, aSO);
771     aSO->Destroy();
772     aSubSO->Destroy();
773   }
774
775   return aResultSO._retn();
776 }
777
778 //============================================================================
779 // function : RestoreSubShapesO
780 // purpose  : Publish sub-shapes, standing for arguments and sub-shapes of arguments.
781 //            To be used from python scripts out of geompy.addToStudy (non-default usage)
782 //============================================================================
783 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapesO (SALOMEDS::Study_ptr     theStudy,
784                                                GEOM::GEOM_Object_ptr   theObject,
785                                                const GEOM::ListOfGO&   theArgs,
786                                                GEOM::find_shape_method theFindMethod,
787                                                CORBA::Boolean          theInheritFirstArg,
788                                                CORBA::Boolean          theAddPrefix)
789 {
790   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
791   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject))
792     return aParts._retn();
793
794   // find SObject in the study if it is already published
795   CORBA::String_var anIORo = _orb->object_to_string(theObject);
796   SALOMEDS::SObject_var aSO = theStudy->FindObjectIOR(anIORo.in());
797   //PTv, IMP 0020001, The salome object <aSO>
798   // is not obligatory in case of invokation from script
799   // if (CORBA::is_nil(aSO))
800   //  return aParts._retn();
801
802   aParts = RestoreSubShapes(theStudy, theObject, aSO, theArgs,
803                             theFindMethod, theInheritFirstArg, theAddPrefix);
804   if (!CORBA::is_nil(aSO)) aSO->Destroy();
805   return aParts._retn();
806 }
807
808 //============================================================================
809 // function : RestoreGivenSubShapesO
810 // purpose  : Publish sub-shapes, standing for arguments and sub-shapes of arguments.
811 //            To be used from python scripts, generated by Dump Python.
812 //============================================================================
813 GEOM::ListOfGO* GEOM_Gen_i::RestoreGivenSubShapesO (SALOMEDS::Study_ptr     theStudy,
814                                                     GEOM::GEOM_Object_ptr   theObject,
815                                                     const GEOM::ListOfGO&   theArgs,
816                                                     GEOM::find_shape_method theFindMethod,
817                                                     CORBA::Boolean          theInheritFirstArg,
818                                                     CORBA::Boolean          theAddPrefix)
819 {
820   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
821   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject))
822     return aParts._retn();
823
824   // find SObject in the study if it is already published
825   CORBA::String_var anIORo = _orb->object_to_string(theObject);
826   SALOMEDS::SObject_var aSO = theStudy->FindObjectIOR(anIORo.in());
827   //PTv, IMP 0020001, The salome object <aSO>
828   // is not obligatory in case of invokation from script
829   // if (CORBA::is_nil(aSO))
830   //  return aParts._retn();
831
832   aParts = RestoreGivenSubShapes(theStudy, theObject, aSO, theArgs,
833                                  theFindMethod, theInheritFirstArg, theAddPrefix);
834   if (!CORBA::is_nil(aSO)) aSO->Destroy();
835   return aParts._retn();
836 }
837
838 //============================================================================
839 // function : RestoreSubShapesSO
840 // purpose  : Publish sub-shapes, standing for arguments and sub-shapes of arguments.
841 //            To be used from GUI and from geompy.addToStudy
842 //============================================================================
843 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapesSO (SALOMEDS::Study_ptr     theStudy,
844                                                 SALOMEDS::SObject_ptr   theSObject,
845                                                 const GEOM::ListOfGO&   theArgs,
846                                                 GEOM::find_shape_method theFindMethod,
847                                                 CORBA::Boolean          theInheritFirstArg,
848                                                 CORBA::Boolean          theAddPrefix)
849 {
850   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
851   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theSObject))
852     return aParts._retn();
853
854   SALOMEDS::GenericAttribute_var anAttr;
855   if (!theSObject->FindAttribute(anAttr, "AttributeIOR"))
856     return aParts._retn();
857
858   SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
859   CORBA::String_var anIORso = anAttrIOR->Value();
860
861   // get Object from SObject
862   GEOM::GEOM_Object_var anO = GEOM::GEOM_Object::_narrow(_orb->string_to_object(anIORso));
863   if (CORBA::is_nil(anO))
864     return aParts._retn();
865
866   aParts = RestoreSubShapes(theStudy, anO, theSObject, theArgs,
867                             theFindMethod, theInheritFirstArg, theAddPrefix);
868   return aParts._retn();
869 }
870
871 //============================================================================
872 // function : addToListOfGO
873 // purpose  : static local function
874 //============================================================================
875 static void addToListOfGO( GEOM::GEOM_Object_ptr theObject,
876                            GEOM::ListOfGO& theList )
877 {
878   const int oldLen = theList.length();
879   theList.length(oldLen + 1);
880   theList[ oldLen ] = GEOM::GEOM_Object::_duplicate( theObject );
881 }
882
883 //============================================================================
884 // function : addToListOfGO
885 // purpose  : static local function
886 //============================================================================
887 static void addToListOfGO( const GEOM::ListOfGO& theSrcList,
888                            GEOM::ListOfGO& theTrgList )
889 {
890   const int oldLen = theTrgList.length();
891   const int srcLen = theSrcList.length();
892   theTrgList.length(oldLen + srcLen);
893   for( int i = 0; i < srcLen; i++ )
894     theTrgList[ oldLen + i ] = GEOM::GEOM_Object::_duplicate( theSrcList[ i ] );
895 }
896
897 //============================================================================
898 // function : RestoreSubShapes
899 // purpose  : Private method. Works only if both theObject and theSObject
900 //            are defined, and does not check, if they correspond to each other.
901 //============================================================================
902 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapes(SALOMEDS::Study_ptr     theStudy,
903                                              GEOM::GEOM_Object_ptr   theObject,
904                                              SALOMEDS::SObject_ptr   theSObject,
905                                              const GEOM::ListOfGO&   theArgs,
906                                              GEOM::find_shape_method theFindMethod,
907                                              CORBA::Boolean          theInheritFirstArg,
908                                              CORBA::Boolean          theAddPrefix)
909 {
910   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
911   //PTv, IMP 0020001, The salome object <theSObject>
912   //     is not obligatory in case of invokation from script
913   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject) /*|| CORBA::is_nil(theSObject)*/)
914     return aParts._retn();
915
916   // For Dump Python (mantis issue 0020768)
917   GEOM::ListOfGO_var anOutArgs = new GEOM::ListOfGO;
918
919   // Arguments to be published
920   GEOM::ListOfGO_var aList;
921
922   // If theArgs list is empty, we try to publish all arguments,
923   // otherwise publish only passed args
924   Standard_Integer nbArgsActual = -1; // -1 means unknown
925   Standard_Integer aLength = theArgs.length();
926   if (aLength > 0) {
927     aList = new GEOM::ListOfGO;
928     aList->length(aLength);
929     for (int i = 0; i < aLength; i++) {
930       aList[i] = GEOM::GEOM_Object::_duplicate( theArgs[i] );
931     }
932   }
933   else {
934     // Get all arguments
935     aList = theObject->GetDependency();
936     aLength = aList->length();
937     nbArgsActual = aLength;
938   }
939
940   if (aLength < 1)
941     return aParts._retn();
942
943   if (theInheritFirstArg || (nbArgsActual == 1)) {
944     // Do not publish argument's reflection,
945     // but only reconstruct its published sub-shapes
946
947     CORBA::String_var anIOR = _orb->object_to_string(aList[0]);
948     SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
949
950     // remember restored objects for Python Dump
951     addToListOfGO(aList[0], anOutArgs);
952
953     aParts = RestoreSubShapesOneLevel(theStudy, anArgSO, theSObject, theObject,
954                                       anOutArgs, theFindMethod, theAddPrefix);
955
956     // set the color of the transformed shape to the color of initial shape
957     theObject->SetColor(aList[0]->GetColor());
958     // set the texture
959     if (theObject->GetShapeType() == GEOM::VERTEX) {
960       theObject->SetMarkerStd(aList[0]->GetMarkerType(), aList[0]->GetMarkerSize());
961       if (aList[0]->GetMarkerType() == GEOM::MT_USER)
962         theObject->SetMarkerTexture(aList[0]->GetMarkerTexture());
963     }
964
965     anArgSO->Destroy();
966   }
967   else {
968     // Get interface, containing method, which we will use to reconstruct sub-shapes
969     GEOM::GEOM_IShapesOperations_var aShapesOp = GetIShapesOperations(theStudy->StudyId());
970     GEOM::GEOM_IGroupOperations_var  aGroupOp  = GetIGroupOperations(theStudy->StudyId());
971
972     // Reconstruct arguments and tree of sub-shapes of the arguments
973     CORBA::String_var anIOR;
974     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
975     for (Standard_Integer i = 0; i < aLength; i++)
976     {
977       GEOM::GEOM_Object_var anArgO = aList[i];
978       if (!CORBA::is_nil(anArgO)) {
979         anIOR = _orb->object_to_string(anArgO);
980         SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
981         TCollection_AsciiString anArgName;
982         if (CORBA::is_nil(anArgSO)) {
983           anArgName = "arg_";
984           anArgName += TCollection_AsciiString(i);
985         }
986         else {
987           anArgName = anArgSO->GetName();
988         }
989
990         // Find a sub-shape of theObject in place of the argument
991         GEOM::GEOM_Object_var aSubO;
992         switch (theFindMethod) {
993         case GEOM::FSM_GetInPlace:
994           {
995             // Use GetInPlace
996             aSubO = aShapesOp->GetInPlace(theObject, anArgO);
997           }
998           break;
999         case GEOM::FSM_Transformed:
1000           {
1001             // transformation, cannot use GetInPlace, operate with indices
1002             GEOM::ListOfLong_var anIDs = anArgO->GetSubShapeIndices();
1003             if (anIDs->length() > 1) {
1004               // group
1005               aSubO = aGroupOp->CreateGroup(theObject, aGroupOp->GetType(anArgO));
1006               if (!CORBA::is_nil(aSubO))
1007                 aGroupOp->UnionIDs(aSubO, anIDs);
1008             }
1009             else if (anIDs->length() > 0) {
1010               // single sub-shape
1011               aSubO = aShapesOp->GetSubShape(theObject, anIDs[0]);
1012             }
1013           }
1014           break;
1015         case GEOM::FSM_GetSame:
1016           {
1017             // Use GetSame
1018             aSubO = aShapesOp->GetSame(theObject, anArgO);
1019           }
1020           break;
1021         case GEOM::FSM_GetShapesOnShape:
1022           {
1023             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
1024             aSubO = aShapesOp->GetShapesOnShapeAsCompound(anArgO, theObject,
1025               (short)GEOM::SOLID, GEOM::ST_ONIN);
1026           }
1027           break;
1028         case GEOM::FSM_GetInPlaceByHistory:
1029           {
1030             // Use GetInPlaceByHistory
1031             aSubO = aShapesOp->GetInPlaceByHistory(theObject, anArgO);
1032           }
1033           break;
1034         default:
1035           {}
1036         }
1037
1038         if (!CORBA::is_nil(aSubO)) {
1039           // remember restored objects for Python Dump
1040           addToListOfGO(anArgO, anOutArgs);
1041
1042           // add to parts list
1043           addToListOfGO( aSubO, aParts );
1044
1045           // Publish the sub-shape
1046           SALOMEDS::SObject_var aSubSO;
1047           if (!CORBA::is_nil(theSObject)) {
1048             TCollection_AsciiString aSubName;
1049             if (theAddPrefix) {
1050               aSubName = "from_";
1051             }
1052             aSubName += anArgName;
1053             aSubSO = aStudyBuilder->NewObject(theSObject);
1054             aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
1055             // Restore color
1056             aSubO->SetColor(anArgO->GetColor());
1057             // set the texture
1058             if (aSubO->GetShapeType() == GEOM::VERTEX) {
1059               aSubO->SetMarkerStd(anArgO->GetMarkerType(), anArgO->GetMarkerSize());
1060               if (anArgO->GetMarkerType() == GEOM::MT_USER)
1061                 aSubO->SetMarkerTexture(anArgO->GetMarkerTexture());
1062             }
1063           }
1064
1065           if (!CORBA::is_nil(anArgSO)) {
1066             // Restore published sub-shapes of the argument
1067             GEOM::ListOfGO_var aSubParts;
1068             if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
1069               // pass theObject, because only it has the history
1070               aSubParts = RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1071                                                    theObject, anOutArgs, theFindMethod, theAddPrefix);
1072             else
1073               aSubParts = RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1074                                                    aSubO, anOutArgs, theFindMethod, theAddPrefix);
1075             // add to parts list
1076             addToListOfGO( aSubParts, aParts );
1077           }
1078         }
1079         else { // GetInPlace failed, try to build from published parts
1080           if (!CORBA::is_nil(anArgSO)) {
1081             SALOMEDS::SObject_var aSubSO;
1082             if (!CORBA::is_nil(theSObject))
1083               aSubSO = aStudyBuilder->NewObject(theSObject);
1084
1085             // Restore published sub-shapes of the argument
1086             GEOM::ListOfGO_var aSubParts =
1087               RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1088                                        theObject, anOutArgs, theFindMethod, theAddPrefix);
1089
1090             // add to parts list
1091             addToListOfGO( aSubParts, aParts );
1092
1093             if (aSubParts->length() > 0) {
1094               // remember restored objects for Python Dump
1095               addToListOfGO(anArgO, anOutArgs);
1096
1097               // try to build an argument from a set of its sub-shapes,
1098               // that published and will be reconstructed
1099               if (aSubParts->length() > 1) {
1100                 aSubO = aShapesOp->MakeCompound(aSubParts);
1101                 // add to parts list
1102                 addToListOfGO( aSubO, aParts );
1103               }
1104               else {
1105                 aSubO = aSubParts[0];
1106               }
1107               if (!CORBA::is_nil(aSubO) && !CORBA::is_nil(aSubSO)) {
1108                 // Publish the sub-shape
1109                 TCollection_AsciiString aSubName;
1110                 if (theAddPrefix) {
1111                   aSubName = "from_parts_of_";
1112                 }
1113                 aSubName += anArgName;
1114                 aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
1115                 // Restore color
1116                 aSubO->SetColor(anArgO->GetColor());
1117                 // set the texture
1118                 if (aSubO->GetShapeType() == GEOM::VERTEX) {
1119                   aSubO->SetMarkerStd(anArgO->GetMarkerType(), anArgO->GetMarkerSize());
1120                   if (anArgO->GetMarkerType() == GEOM::MT_USER)
1121                     aSubO->SetMarkerTexture(anArgO->GetMarkerTexture());
1122                 }
1123               }
1124             }
1125             else if (!CORBA::is_nil(aSubSO)) {
1126               // remove created aSubSO, because no parts have been found
1127               aStudyBuilder->RemoveObject(aSubSO);
1128             }
1129           }
1130         } // try to build from published parts
1131         anArgSO->Destroy();
1132       }
1133     } // process arguments
1134   }
1135   std::set<std::string> anObjEntryMap;
1136   GEOM::ListOfGO_var aResParts = new GEOM::ListOfGO;
1137   int nbRes = 0;
1138   int nb = aParts->length();
1139   aResParts->length(nb);
1140   if (nb > 0)
1141   {
1142     Handle(GEOM_Object) aMainObj = _impl->GetObject(theObject->GetStudyID(), theObject->GetEntry());
1143     Handle(GEOM_Function) aFunction = aMainObj->GetLastFunction();
1144     GEOM::TPythonDump pd (aFunction, true);
1145     pd <<"[";
1146     int i = 0, j = 0;
1147     for ( ; i < nb; i++ )
1148     {
1149       GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_duplicate( aParts[ i ] );
1150       if (CORBA::is_nil(anObj))
1151         continue;
1152       char* anEntry = anObj->GetEntry();
1153       if (anObjEntryMap.count(anEntry))
1154         continue; // already treated
1155       anObjEntryMap.insert(anEntry);
1156       aResParts[nbRes++] = anObj;
1157       // clear python dump of object
1158       Handle(GEOM_Object) aGeomObj = _impl->GetObject(anObj->GetStudyID(), anEntry);
1159       Handle(GEOM_Function) anObjFun = aGeomObj->GetLastFunction();
1160       if ( !anObjFun.IsNull() )
1161         anObjFun->SetDescription( "" );
1162       if ( j > 0 )
1163         pd << ", ";
1164       pd << aGeomObj;
1165       j++;
1166     }
1167     pd <<"]" << " = geompy.RestoreGivenSubShapes(" << aMainObj << ", " << "[";
1168     //i = 0; nb = theArgs.length(); j = 0;
1169     i = 0; nb = anOutArgs->length(); j = 0;
1170     for ( ; i < nb; i++ )
1171     {
1172       //GEOM::GEOM_Object_var anObj = theArgs[ i ];
1173       GEOM::GEOM_Object_var anObj = anOutArgs[ i ];
1174       if (CORBA::is_nil(anObj))
1175         continue;
1176       Handle(GEOM_Object) aGeomObj = _impl->GetObject(anObj->GetStudyID(), anObj->GetEntry());
1177       if ( j > 0 )
1178         pd << ", ";
1179       pd << aGeomObj;
1180       j++;
1181     }
1182     pd <<"]" << ", " <<"geompy.GEOM.";
1183     switch (theFindMethod) {
1184     case GEOM::FSM_GetInPlace:
1185       pd << "FSM_GetInPlace"; break;
1186     case GEOM::FSM_Transformed:
1187       pd << "FSM_Transformed"; break;
1188     case GEOM::FSM_GetSame:
1189       pd << "FSM_GetSame"; break;
1190     case GEOM::FSM_GetShapesOnShape:
1191       pd << "FSM_GetShapesOnShape"; break;
1192     case GEOM::FSM_GetInPlaceByHistory:
1193     default:
1194       pd << "FSM_GetInPlaceByHistory"; break;
1195     }
1196     pd << ", " << theInheritFirstArg << ", " << theAddPrefix << ")";
1197   }
1198   aResParts->length(nbRes);
1199   return aResParts._retn();
1200 }
1201
1202 //============================================================================
1203 // function : RestoreSubShapesOneLevel
1204 // purpose  : Private method
1205 //============================================================================
1206 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapesOneLevel (SALOMEDS::Study_ptr     theStudy,
1207                                                       SALOMEDS::SObject_ptr   theOldSO,
1208                                                       SALOMEDS::SObject_ptr   theNewSO,
1209                                                       GEOM::GEOM_Object_ptr   theNewO,
1210                                                       GEOM::ListOfGO&         theOutArgs,
1211                                                       GEOM::find_shape_method theFindMethod,
1212                                                       CORBA::Boolean          theAddPrefix)
1213 {
1214   int i = 0;
1215   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
1216   GEOM::ListOfGO_var aNewParts = new GEOM::ListOfGO;
1217   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theOldSO) ||
1218       CORBA::is_nil(theNewO) /*|| CORBA::is_nil(theNewSO)*/)
1219     return aParts._retn();
1220
1221   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
1222
1223   // Get interface, containing method, which we will use to reconstruct sub-shapes
1224   GEOM::GEOM_IShapesOperations_var aShapesOp = GetIShapesOperations(theStudy->StudyId());
1225   GEOM::GEOM_IGroupOperations_var  aGroupOp  = GetIGroupOperations(theStudy->StudyId());
1226
1227   // Reconstruct published sub-shapes
1228   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator(theOldSO);
1229
1230   int aLen = 0;
1231   for (it->Init(); it->More(); it->Next()) {
1232     aLen++;
1233   }
1234   aParts->length(aLen);
1235
1236   for (it->Init(); it->More(); it->Next()) {
1237     SALOMEDS::SObject_var anOldSubSO = it->Value();
1238
1239     TCollection_AsciiString anArgName = anOldSubSO->GetName();
1240
1241     SALOMEDS::GenericAttribute_var anAttr;
1242     if (anOldSubSO->FindAttribute(anAttr, "AttributeIOR")) {
1243       SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1244       GEOM::GEOM_Object_var anOldSubO =
1245         GEOM::GEOM_Object::_narrow(_orb->string_to_object(anAttrIOR->Value()));
1246       if (!CORBA::is_nil(anOldSubO)) {
1247         // Find a sub-shape of theNewO in place of anOldSubO
1248         GEOM::GEOM_Object_var aNewSubO;
1249         switch (theFindMethod) {
1250         case GEOM::FSM_GetInPlace:
1251           {
1252             // Use GetInPlace
1253             aNewSubO = aShapesOp->GetInPlace(theNewO, anOldSubO);
1254           }
1255           break;
1256         case GEOM::FSM_Transformed:
1257           {
1258             // transformation, cannot use GetInPlace, operate with indices
1259             GEOM::ListOfLong_var anIDs = anOldSubO->GetSubShapeIndices();
1260             if (anIDs->length() > 1) {
1261               // group
1262               aNewSubO = aGroupOp->CreateGroup(theNewO, aGroupOp->GetType(anOldSubO));
1263               if (!CORBA::is_nil(aNewSubO))
1264                 aGroupOp->UnionIDs(aNewSubO, anIDs);
1265             }
1266             else {
1267               // single sub-shape
1268               aNewSubO = aShapesOp->GetSubShape(theNewO, anIDs[0]);
1269             }
1270           }
1271           break;
1272         case GEOM::FSM_GetSame:
1273           {
1274             // Use GetSame
1275             aNewSubO = aShapesOp->GetSame(theNewO, anOldSubO);
1276           }
1277           break;
1278         case GEOM::FSM_GetShapesOnShape:
1279           {
1280             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
1281             aNewSubO = aShapesOp->GetShapesOnShapeAsCompound(anOldSubO, theNewO,
1282                                                              (short)GEOM::SOLID, GEOM::ST_ONIN);
1283           }
1284           break;
1285         case GEOM::FSM_GetInPlaceByHistory:
1286           {
1287             // Use GetInPlaceByHistory
1288             aNewSubO = aShapesOp->GetInPlaceByHistory(theNewO, anOldSubO);
1289           }
1290           break;
1291         default:
1292           {}
1293         }
1294
1295         if (!CORBA::is_nil(aNewSubO)) {
1296           // remember restored objects for Python Dump
1297           addToListOfGO(anOldSubO, theOutArgs);
1298
1299           // add the part to the list
1300           aParts[i] = aNewSubO;
1301           i++;
1302           // add to parts list
1303           addToListOfGO( aNewSubO, aNewParts );
1304
1305           SALOMEDS::SObject_var aNewSubSO;
1306           if (!CORBA::is_nil(theNewSO)) {
1307               // Publish the sub-shape
1308             TCollection_AsciiString aSubName;
1309             if (theAddPrefix) {
1310               aSubName = "from_";
1311             }
1312             aSubName += anArgName;
1313             aNewSubSO = aStudyBuilder->NewObject(theNewSO);
1314             aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
1315             // Restore color
1316             aNewSubO->SetColor(anOldSubO->GetColor());
1317             // set the texture
1318             if (aNewSubO->GetShapeType() == GEOM::VERTEX) {
1319               aNewSubO->SetMarkerStd(anOldSubO->GetMarkerType(), anOldSubO->GetMarkerSize());
1320               if (anOldSubO->GetMarkerType() == GEOM::MT_USER)
1321                 aNewSubO->SetMarkerTexture(anOldSubO->GetMarkerTexture());
1322             }
1323           }
1324           // Restore published sub-shapes of the argument
1325           GEOM::ListOfGO_var aSubParts;
1326           if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
1327             // pass the main shape as Object, because only it has the history
1328             aSubParts = RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1329                                                  theNewO, theOutArgs, theFindMethod, theAddPrefix);
1330           else
1331             aSubParts = RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1332                                                  aNewSubO, theOutArgs, theFindMethod, theAddPrefix);
1333           // add to parts list
1334           addToListOfGO( aSubParts, aNewParts );
1335         }
1336         else { // GetInPlace failed, try to build from published parts
1337           SALOMEDS::SObject_var aNewSubSO;
1338           if (!CORBA::is_nil(theNewSO))
1339             aNewSubSO = aStudyBuilder->NewObject(theNewSO);
1340
1341           // Restore published sub-shapes of the argument
1342           GEOM::ListOfGO_var aSubParts =
1343             RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1344                                      theNewO, theOutArgs, theFindMethod, theAddPrefix);
1345           // add to parts list
1346           addToListOfGO( aSubParts, aNewParts );
1347
1348           if (aSubParts->length() > 0) {
1349             // remember restored objects for Python Dump
1350             addToListOfGO(anOldSubO, theOutArgs);
1351
1352             // try to build an object from a set of its sub-shapes,
1353             // that published and will be reconstructed
1354             if (aSubParts->length() > 1) {
1355               aNewSubO = aShapesOp->MakeCompound(aSubParts);
1356               // add to parts list
1357               addToListOfGO( aNewSubO, aNewParts );
1358             }
1359             else {
1360               aNewSubO = aSubParts[0];
1361             }
1362
1363             if (!CORBA::is_nil(aNewSubO)) {
1364               // add the part to the list
1365               aSubParts[i] = aNewSubO;
1366               i++;
1367
1368               // Publish the sub-shape
1369               if (!CORBA::is_nil(aNewSubSO)) {
1370                 TCollection_AsciiString aSubName;
1371                 if (theAddPrefix) {
1372                   aSubName = "from_parts_of_";
1373                 }
1374                 aSubName += anArgName;
1375                 aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
1376                 // Restore color
1377                 aNewSubO->SetColor(anOldSubO->GetColor());
1378                 // set the texture
1379                 if (aNewSubO->GetShapeType() == GEOM::VERTEX) {
1380                   aNewSubO->SetMarkerStd(anOldSubO->GetMarkerType(), anOldSubO->GetMarkerSize());
1381                   if (anOldSubO->GetMarkerType() == GEOM::MT_USER)
1382                     aNewSubO->SetMarkerTexture(anOldSubO->GetMarkerTexture());
1383                 }
1384               }
1385             }
1386           }
1387           else if (!CORBA::is_nil(aNewSubSO)) {
1388             // remove created aSubSO, because no parts have been found
1389             aStudyBuilder->RemoveObject(aNewSubSO);
1390           }
1391         } // try to build from published parts
1392       }
1393     }
1394   } // iterate on published sub-shapes
1395
1396   aParts->length(i);
1397   // add to parts list
1398   addToListOfGO( aNewParts, aParts );
1399   return aParts._retn();
1400 }
1401
1402 //============================================================================
1403 // function : RestoreGivenSubShapes
1404 // purpose  : Private method. Works only if both theObject and theSObject
1405 //            are defined, and does not check, if they correspond to each other.
1406 //            List theArgs in this case contains not only operation arguments,
1407 //            but also all subshapes, which must be published.
1408 //============================================================================
1409 GEOM::ListOfGO* GEOM_Gen_i::RestoreGivenSubShapes(SALOMEDS::Study_ptr     theStudy,
1410                                                   GEOM::GEOM_Object_ptr   theObject,
1411                                                   SALOMEDS::SObject_ptr   theSObject,
1412                                                   const GEOM::ListOfGO&   theArgs,
1413                                                   GEOM::find_shape_method theFindMethod,
1414                                                   CORBA::Boolean          theInheritFirstArg,
1415                                                   CORBA::Boolean          theAddPrefix)
1416 {
1417   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
1418   //PTv, IMP 0020001, The salome object <theSObject>
1419   //     is not obligatory in case of invokation from script
1420   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject) /*|| CORBA::is_nil(theSObject)*/)
1421     return aParts._retn();
1422
1423   // If theArgs list is empty, nothing to do
1424   Standard_Integer aLength = theArgs.length();
1425   if (aLength == 0)
1426     return aParts._retn();
1427
1428   // Get all arguments
1429   GEOM::ListOfGO_var anOpArgsList = theObject->GetDependency();
1430   Standard_Integer nbArgsActual = anOpArgsList->length();
1431
1432   // If anOpArgsList list is empty, nothing to do
1433   if (nbArgsActual == 0)
1434     return aParts._retn();
1435
1436   // Entries of arguments and subshapes
1437   std::set<std::string> anArgs;
1438   for (int i = 0; i < aLength; i++) {
1439     CORBA::String_var anEntry = theArgs[i]->GetEntry();
1440     anArgs.insert(anEntry.in());
1441   }
1442
1443   // Arguments to be published
1444   // We try to publish all arguments, that are in theArgs list
1445   GEOM::ListOfGO_var aList = new GEOM::ListOfGO;
1446   aList->length(nbArgsActual);
1447
1448   int k = 0;
1449   for (int j = 0; j < nbArgsActual; j++) {
1450     CORBA::String_var anEntry = anOpArgsList[j]->GetEntry();
1451     if (anArgs.count(anEntry.in())) {
1452       aList[k] = GEOM::GEOM_Object::_duplicate(anOpArgsList[j]);
1453       k++;
1454     }
1455   }
1456   nbArgsActual = k;
1457   //aList->length(nbArgsActual);
1458
1459   if (nbArgsActual < 1)
1460     return aParts._retn();
1461
1462   if (theInheritFirstArg || (nbArgsActual == 1)) {
1463     // Do not publish argument's reflection,
1464     // but only reconstruct its published sub-shapes
1465
1466     CORBA::String_var anIOR = _orb->object_to_string(aList[0]);
1467     SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
1468
1469     aParts = RestoreGivenSubShapesOneLevel(theStudy, anArgSO, theSObject, theObject,
1470                                            anArgs, theFindMethod, theAddPrefix);
1471
1472     // set the color of the transformed shape to the color of initial shape
1473     theObject->SetColor(aList[0]->GetColor());
1474     // set the texture
1475     if (theObject->GetShapeType() == GEOM::VERTEX) {
1476       theObject->SetMarkerStd(aList[0]->GetMarkerType(), aList[0]->GetMarkerSize());
1477       if (aList[0]->GetMarkerType() == GEOM::MT_USER)
1478         theObject->SetMarkerTexture(aList[0]->GetMarkerTexture());
1479     }
1480
1481     anArgSO->Destroy();
1482   }
1483   else {
1484     // Get interface, containing method, which we will use to reconstruct sub-shapes
1485     GEOM::GEOM_IShapesOperations_var aShapesOp = GetIShapesOperations(theStudy->StudyId());
1486     GEOM::GEOM_IGroupOperations_var  aGroupOp  = GetIGroupOperations(theStudy->StudyId());
1487
1488     // Reconstruct arguments and tree of sub-shapes of the arguments
1489     CORBA::String_var anIOR;
1490     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
1491     for (Standard_Integer i = 0; i < nbArgsActual; i++)
1492     {
1493       GEOM::GEOM_Object_var anArgO = aList[i];
1494       if (!CORBA::is_nil(anArgO)) {
1495         anIOR = _orb->object_to_string(anArgO);
1496         SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
1497         TCollection_AsciiString anArgName;
1498         if (CORBA::is_nil(anArgSO)) {
1499           anArgName = "arg_";
1500           anArgName += TCollection_AsciiString(i);
1501         }
1502         else {
1503           anArgName = anArgSO->GetName();
1504         }
1505
1506         // Find a sub-shape of theObject in place of the argument
1507         GEOM::GEOM_Object_var aSubO;
1508         switch (theFindMethod) {
1509         case GEOM::FSM_GetInPlace:
1510           {
1511             // Use GetInPlace
1512             aSubO = aShapesOp->GetInPlace(theObject, anArgO);
1513           }
1514           break;
1515         case GEOM::FSM_Transformed:
1516           {
1517             // transformation, cannot use GetInPlace, operate with indices
1518             GEOM::ListOfLong_var anIDs = anArgO->GetSubShapeIndices();
1519             if (anIDs->length() > 1) {
1520               // group
1521               aSubO = aGroupOp->CreateGroup(theObject, aGroupOp->GetType(anArgO));
1522               if (!CORBA::is_nil(aSubO))
1523                 aGroupOp->UnionIDs(aSubO, anIDs);
1524             }
1525             else if (anIDs->length() > 0) {
1526               // single sub-shape
1527               aSubO = aShapesOp->GetSubShape(theObject, anIDs[0]);
1528             }
1529           }
1530           break;
1531         case GEOM::FSM_GetSame:
1532           {
1533             // Use GetSame
1534             aSubO = aShapesOp->GetSame(theObject, anArgO);
1535           }
1536           break;
1537         case GEOM::FSM_GetShapesOnShape:
1538           {
1539             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
1540             aSubO = aShapesOp->GetShapesOnShapeAsCompound(anArgO, theObject,
1541               (short)GEOM::SOLID, GEOM::ST_ONIN);
1542           }
1543           break;
1544         case GEOM::FSM_GetInPlaceByHistory:
1545           {
1546             // Use GetInPlaceByHistory
1547             aSubO = aShapesOp->GetInPlaceByHistory(theObject, anArgO);
1548           }
1549           break;
1550         default:
1551           {}
1552         }
1553
1554         if (!CORBA::is_nil(aSubO)) {
1555           // add to parts list
1556           addToListOfGO( aSubO, aParts );
1557
1558           // Publish the sub-shape
1559           SALOMEDS::SObject_var aSubSO;
1560           if (!CORBA::is_nil(theSObject)) {
1561             TCollection_AsciiString aSubName;
1562             if (theAddPrefix) {
1563               aSubName = "from_";
1564             }
1565             aSubName += anArgName;
1566             aSubSO = aStudyBuilder->NewObject(theSObject);
1567             aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
1568             // Restore color
1569             aSubO->SetColor(anArgO->GetColor());
1570             // set the texture
1571             if (aSubO->GetShapeType() == GEOM::VERTEX) {
1572               aSubO->SetMarkerStd(anArgO->GetMarkerType(), anArgO->GetMarkerSize());
1573               if (anArgO->GetMarkerType() == GEOM::MT_USER)
1574                 aSubO->SetMarkerTexture(anArgO->GetMarkerTexture());
1575             }
1576           }
1577
1578           if (!CORBA::is_nil(anArgSO)) {
1579             // Restore published sub-shapes of the argument
1580             GEOM::ListOfGO_var aSubParts;
1581             if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
1582               // pass theObject, because only it has the history
1583               aSubParts = RestoreGivenSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1584                                                         theObject, anArgs, theFindMethod, theAddPrefix);
1585             else
1586               aSubParts = RestoreGivenSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1587                                                         aSubO, anArgs, theFindMethod, theAddPrefix);
1588             // add to parts list
1589             addToListOfGO( aSubParts, aParts );
1590           }
1591         }
1592         else { // GetInPlace failed, try to build from published parts
1593           if (!CORBA::is_nil(anArgSO)) {
1594             SALOMEDS::SObject_var aSubSO;
1595             if (!CORBA::is_nil(theSObject))
1596               aSubSO = aStudyBuilder->NewObject(theSObject);
1597
1598             // Restore published sub-shapes of the argument
1599             GEOM::ListOfGO_var aSubParts =
1600               RestoreGivenSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1601                                             theObject, anArgs, theFindMethod, theAddPrefix);
1602
1603             // add to parts list
1604             addToListOfGO( aSubParts, aParts );
1605
1606             if (aSubParts->length() > 0) {
1607               // try to build an argument from a set of its sub-shapes,
1608               // that published and will be reconstructed
1609               if (aSubParts->length() > 1) {
1610                 aSubO = aShapesOp->MakeCompound(aSubParts);
1611                 // add to parts list
1612                 addToListOfGO( aSubO, aParts );
1613               }
1614               else {
1615                 aSubO = aSubParts[0];
1616               }
1617               if (!CORBA::is_nil(aSubO) && !CORBA::is_nil(aSubSO)) {
1618                 // Publish the sub-shape
1619                 TCollection_AsciiString aSubName;
1620                 if (theAddPrefix) {
1621                   aSubName = "from_parts_of_";
1622                 }
1623                 aSubName += anArgName;
1624                 aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
1625                 // Restore color
1626                 aSubO->SetColor(anArgO->GetColor());
1627                 // set the texture
1628                 if (aSubO->GetShapeType() == GEOM::VERTEX) {
1629                   aSubO->SetMarkerStd(anArgO->GetMarkerType(), anArgO->GetMarkerSize());
1630                   if (anArgO->GetMarkerType() == GEOM::MT_USER)
1631                     aSubO->SetMarkerTexture(anArgO->GetMarkerTexture());
1632                 }
1633               }
1634             }
1635             else if (!CORBA::is_nil(aSubSO)) {
1636               // remove created aSubSO, because no parts have been found
1637               aStudyBuilder->RemoveObject(aSubSO);
1638             }
1639           }
1640         } // try to build from published parts
1641         anArgSO->Destroy();
1642       }
1643     } // process arguments
1644   }
1645   std::set<std::string> anObjEntryMap;
1646   GEOM::ListOfGO_var aResParts = new GEOM::ListOfGO;
1647   int nbRes = 0;
1648   int nb = aParts->length();
1649   aResParts->length(nb);
1650   if (nb > 0)
1651   {
1652     Handle(GEOM_Object) aMainObj = _impl->GetObject(theObject->GetStudyID(), theObject->GetEntry());
1653     Handle(GEOM_Function) aFunction = aMainObj->GetLastFunction();
1654     GEOM::TPythonDump pd (aFunction, true);
1655     pd <<"[";
1656     int i = 0, j = 0;
1657     for ( ; i < nb; i++ )
1658     {
1659       GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_duplicate( aParts[ i ] );
1660       if (CORBA::is_nil(anObj))
1661         continue;
1662       char* anEntry = anObj->GetEntry();
1663       if (anObjEntryMap.count(anEntry))
1664         continue; // already treated
1665       anObjEntryMap.insert(anEntry);
1666       aResParts[nbRes++] = anObj;
1667       // clear python dump of object
1668       Handle(GEOM_Object) aGeomObj = _impl->GetObject(anObj->GetStudyID(), anEntry);
1669       Handle(GEOM_Function) anObjFun = aGeomObj->GetLastFunction();
1670       if ( !anObjFun.IsNull() )
1671         anObjFun->SetDescription( "" );
1672       if ( j > 0 )
1673         pd << ", ";
1674       pd << aGeomObj;
1675       j++;
1676     }
1677     pd <<"]" << " = geompy.RestoreGivenSubShapes(" << aMainObj << ", " << "[";
1678     i = 0; nb = theArgs.length(); j = 0;
1679     for ( ; i < nb; i++ )
1680     {
1681       GEOM::GEOM_Object_var anObj = theArgs[ i ];
1682       if (CORBA::is_nil(anObj))
1683         continue;
1684       Handle(GEOM_Object) aGeomObj = _impl->GetObject(anObj->GetStudyID(), anObj->GetEntry());
1685       if ( j > 0 )
1686         pd << ", ";
1687       pd << aGeomObj;
1688       j++;
1689     }
1690     pd <<"]" << ", " <<"geompy.GEOM.";
1691     switch (theFindMethod) {
1692     case GEOM::FSM_GetInPlace:
1693       pd << "FSM_GetInPlace"; break;
1694     case GEOM::FSM_Transformed:
1695       pd << "FSM_Transformed"; break;
1696     case GEOM::FSM_GetSame:
1697       pd << "FSM_GetSame"; break;
1698     case GEOM::FSM_GetShapesOnShape:
1699       pd << "FSM_GetShapesOnShape"; break;
1700     case GEOM::FSM_GetInPlaceByHistory:
1701     default:
1702       pd << "FSM_GetInPlaceByHistory"; break;
1703     }
1704     pd << ", " << theInheritFirstArg << ", " << theAddPrefix << ")";
1705   }
1706   aResParts->length(nbRes);
1707   return aResParts._retn();
1708 }
1709
1710 //============================================================================
1711 // function : RestoreGivenSubShapesOneLevel
1712 // purpose  : Private method
1713 //============================================================================
1714 GEOM::ListOfGO* GEOM_Gen_i::RestoreGivenSubShapesOneLevel (SALOMEDS::Study_ptr     theStudy,
1715                                                            SALOMEDS::SObject_ptr   theOldSO,
1716                                                            SALOMEDS::SObject_ptr   theNewSO,
1717                                                            GEOM::GEOM_Object_ptr   theNewO,
1718                                                            std::set<std::string>   theArgs,
1719                                                            GEOM::find_shape_method theFindMethod,
1720                                                            CORBA::Boolean          theAddPrefix)
1721 {
1722   int i = 0;
1723   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
1724   GEOM::ListOfGO_var aNewParts = new GEOM::ListOfGO;
1725   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theOldSO) ||
1726       CORBA::is_nil(theNewO) /*|| CORBA::is_nil(theNewSO)*/)
1727     return aParts._retn();
1728
1729   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
1730
1731   // Get interface, containing method, which we will use to reconstruct sub-shapes
1732   GEOM::GEOM_IShapesOperations_var aShapesOp = GetIShapesOperations(theStudy->StudyId());
1733   GEOM::GEOM_IGroupOperations_var  aGroupOp  = GetIGroupOperations(theStudy->StudyId());
1734
1735   // Reconstruct published sub-shapes
1736   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator(theOldSO);
1737
1738   int aLen = 0;
1739   for (it->Init(); it->More(); it->Next()) {
1740     aLen++;
1741   }
1742   aParts->length(aLen);
1743
1744   for (it->Init(); it->More(); it->Next()) {
1745     SALOMEDS::SObject_var anOldSubSO = it->Value();
1746
1747     TCollection_AsciiString anArgName = anOldSubSO->GetName();
1748
1749     SALOMEDS::GenericAttribute_var anAttr;
1750     if (anOldSubSO->FindAttribute(anAttr, "AttributeIOR")) {
1751       SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1752       GEOM::GEOM_Object_var anOldSubO =
1753         GEOM::GEOM_Object::_narrow(_orb->string_to_object(anAttrIOR->Value()));
1754
1755       bool okToContinue = false;
1756
1757       if (!CORBA::is_nil(anOldSubO)) {
1758         CORBA::String_var anEntry = anOldSubO->GetEntry();
1759         okToContinue = theArgs.count(anEntry.in());
1760       }
1761
1762       if (okToContinue) {
1763         // Find a sub-shape of theNewO in place of anOldSubO
1764         GEOM::GEOM_Object_var aNewSubO;
1765         switch (theFindMethod) {
1766         case GEOM::FSM_GetInPlace:
1767           {
1768             // Use GetInPlace
1769             aNewSubO = aShapesOp->GetInPlace(theNewO, anOldSubO);
1770           }
1771           break;
1772         case GEOM::FSM_Transformed:
1773           {
1774             // transformation, cannot use GetInPlace, operate with indices
1775             GEOM::ListOfLong_var anIDs = anOldSubO->GetSubShapeIndices();
1776             if (anIDs->length() > 1) {
1777               // group
1778               aNewSubO = aGroupOp->CreateGroup(theNewO, aGroupOp->GetType(anOldSubO));
1779               if (!CORBA::is_nil(aNewSubO))
1780                 aGroupOp->UnionIDs(aNewSubO, anIDs);
1781             }
1782             else {
1783               // single sub-shape
1784               aNewSubO = aShapesOp->GetSubShape(theNewO, anIDs[0]);
1785             }
1786           }
1787           break;
1788         case GEOM::FSM_GetSame:
1789           {
1790             // Use GetSame
1791             aNewSubO = aShapesOp->GetSame(theNewO, anOldSubO);
1792           }
1793           break;
1794         case GEOM::FSM_GetShapesOnShape:
1795           {
1796             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
1797             aNewSubO = aShapesOp->GetShapesOnShapeAsCompound(anOldSubO, theNewO,
1798                                                              (short)GEOM::SOLID, GEOM::ST_ONIN);
1799           }
1800           break;
1801         case GEOM::FSM_GetInPlaceByHistory:
1802           {
1803             // Use GetInPlaceByHistory
1804             aNewSubO = aShapesOp->GetInPlaceByHistory(theNewO, anOldSubO);
1805           }
1806           break;
1807         default:
1808           {}
1809         }
1810
1811         if (!CORBA::is_nil(aNewSubO)) {
1812           // add the part to the list
1813           aParts[i] = aNewSubO;
1814           i++;
1815           // add to parts list
1816           addToListOfGO( aNewSubO, aNewParts );
1817
1818           SALOMEDS::SObject_var aNewSubSO;
1819           if (!CORBA::is_nil(theNewSO)) {
1820               // Publish the sub-shape
1821             TCollection_AsciiString aSubName;
1822             if (theAddPrefix) {
1823               aSubName = "from_";
1824             }
1825             aSubName += anArgName;
1826             aNewSubSO = aStudyBuilder->NewObject(theNewSO);
1827             aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
1828             // Restore color
1829             aNewSubO->SetColor(anOldSubO->GetColor());
1830             // set the texture
1831             if (aNewSubO->GetShapeType() == GEOM::VERTEX) {
1832               aNewSubO->SetMarkerStd(anOldSubO->GetMarkerType(), anOldSubO->GetMarkerSize());
1833               if (anOldSubO->GetMarkerType() == GEOM::MT_USER)
1834                 aNewSubO->SetMarkerTexture(anOldSubO->GetMarkerTexture());
1835             }
1836           }
1837           // Restore published sub-shapes of the argument
1838           GEOM::ListOfGO_var aSubParts;
1839           if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
1840             // pass the main shape as Object, because only it has the history
1841             aSubParts = RestoreGivenSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1842                                                       theNewO, theArgs, theFindMethod, theAddPrefix);
1843           else
1844             aSubParts = RestoreGivenSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1845                                                       aNewSubO, theArgs, theFindMethod, theAddPrefix);
1846           // add to parts list
1847           addToListOfGO( aSubParts, aNewParts );
1848         }
1849         else { // GetInPlace failed, try to build from published parts
1850           SALOMEDS::SObject_var aNewSubSO;
1851           if (!CORBA::is_nil(theNewSO))
1852             aNewSubSO = aStudyBuilder->NewObject(theNewSO);
1853
1854           // Restore published sub-shapes of the argument
1855           GEOM::ListOfGO_var aSubParts =
1856             RestoreGivenSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1857                                           theNewO, theArgs, theFindMethod, theAddPrefix);
1858           // add to parts list
1859           addToListOfGO( aSubParts, aNewParts );
1860
1861           if (aSubParts->length() > 0) {
1862             // try to build an object from a set of its sub-shapes,
1863             // that published and will be reconstructed
1864             if (aSubParts->length() > 1) {
1865               aNewSubO = aShapesOp->MakeCompound(aSubParts);
1866               // add to parts list
1867               addToListOfGO( aNewSubO, aNewParts );
1868             }
1869             else {
1870               aNewSubO = aSubParts[0];
1871             }
1872
1873             if (!CORBA::is_nil(aNewSubO)) {
1874               // add the part to the list
1875               aSubParts[i] = aNewSubO;
1876               i++;
1877
1878               // Publish the sub-shape
1879               if (!CORBA::is_nil(aNewSubSO)) {
1880                 TCollection_AsciiString aSubName;
1881                 if (theAddPrefix) {
1882                   aSubName = "from_parts_of_";
1883                 }
1884                 aSubName += anArgName;
1885                 aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
1886                 // Restore color
1887                 aNewSubO->SetColor(anOldSubO->GetColor());
1888                 // set the texture
1889                 if (aNewSubO->GetShapeType() == GEOM::VERTEX) {
1890                   aNewSubO->SetMarkerStd(anOldSubO->GetMarkerType(), anOldSubO->GetMarkerSize());
1891                   if (anOldSubO->GetMarkerType() == GEOM::MT_USER)
1892                     aNewSubO->SetMarkerTexture(anOldSubO->GetMarkerTexture());
1893                 }
1894               }
1895             }
1896           }
1897           else if (!CORBA::is_nil(aNewSubSO)) {
1898             // remove created aSubSO, because no parts have been found
1899             aStudyBuilder->RemoveObject(aNewSubSO);
1900           }
1901         } // try to build from published parts
1902       }
1903     }
1904   } // iterate on published sub-shapes
1905
1906   aParts->length(i);
1907   // add to parts list
1908   addToListOfGO( aNewParts, aParts );
1909   return aParts._retn();
1910 }
1911
1912 //============================================================================
1913 // function : register()
1914 // purpose  : register 'name' in 'name_service'
1915 //============================================================================
1916 void GEOM_Gen_i::register_name(char * name)
1917 {
1918   GEOM::GEOM_Gen_var g = _this();
1919   name_service->Register(g, name);
1920 }
1921
1922 //============================================================================
1923 // function : Undo
1924 // purpose  :
1925 //============================================================================
1926 void GEOM_Gen_i::Undo(CORBA::Long theStudyID)
1927 {
1928   _impl->Undo(theStudyID);
1929 }
1930
1931 //============================================================================
1932 // function : Redo
1933 // purpose  :
1934 //============================================================================
1935 void GEOM_Gen_i::Redo(CORBA::Long theStudyID)
1936 {
1937   _impl->Redo(theStudyID);
1938 }
1939
1940 //============================================================================
1941 // function : GetIBasicOperations
1942 // purpose  :
1943 //============================================================================
1944 GEOM::GEOM_IBasicOperations_ptr GEOM_Gen_i::GetIBasicOperations(CORBA::Long theStudyID)
1945      throw ( SALOME::SALOME_Exception )
1946 {
1947   Unexpect aCatch(SALOME_SalomeException);
1948   MESSAGE( "GEOM_Gen_i::GetIBasicOperations" );
1949
1950   GEOM::GEOM_Gen_ptr engine = _this();
1951
1952   //transfer reference on engine
1953   GEOM_IBasicOperations_i* aServant =
1954     new GEOM_IBasicOperations_i(_poa, engine, _impl->GetIBasicOperations(theStudyID));
1955
1956   PortableServer::ObjectId_var id = _poa->activate_object(aServant);
1957   // activate the CORBA servant
1958   GEOM::GEOM_IBasicOperations_var operations = aServant->_this();
1959   return operations._retn();
1960 }
1961
1962 //============================================================================
1963 // function : GetITransformOperations
1964 // purpose  :
1965 //============================================================================
1966 GEOM::GEOM_ITransformOperations_ptr GEOM_Gen_i::GetITransformOperations(CORBA::Long theStudyID)
1967      throw ( SALOME::SALOME_Exception )
1968 {
1969   Unexpect aCatch(SALOME_SalomeException);
1970   MESSAGE( "GEOM_Gen_i::GetITransformOperations" );
1971
1972   GEOM::GEOM_Gen_ptr engine = _this();
1973
1974   GEOM_ITransformOperations_i* aServant =
1975     new GEOM_ITransformOperations_i(_poa, engine, _impl->GetITransformOperations(theStudyID));
1976
1977   // activate the CORBA servant
1978   GEOM::GEOM_ITransformOperations_var operations = aServant->_this();
1979   return operations._retn();
1980 }
1981
1982 //============================================================================
1983 // function : GetI3DPrimOperations
1984 // purpose  :
1985 //============================================================================
1986 GEOM::GEOM_I3DPrimOperations_ptr GEOM_Gen_i::GetI3DPrimOperations(CORBA::Long theStudyID)
1987      throw ( SALOME::SALOME_Exception )
1988 {
1989   Unexpect aCatch(SALOME_SalomeException);
1990   MESSAGE( "GEOM_Gen_i::GetI3DPrimOperations" );
1991
1992   GEOM::GEOM_Gen_ptr engine = _this();
1993
1994   GEOM_I3DPrimOperations_i* aServant =
1995     new GEOM_I3DPrimOperations_i(_poa, engine, _impl->GetI3DPrimOperations(theStudyID));
1996   PortableServer::ObjectId_var id = _poa->activate_object(aServant);
1997
1998   // activate the CORBA servant
1999   GEOM::GEOM_I3DPrimOperations_var operations = aServant->_this();
2000   return operations._retn();
2001 }
2002
2003 //============================================================================
2004 // function : GetIShapesOperations
2005 // purpose  :
2006 //============================================================================
2007 GEOM::GEOM_IShapesOperations_ptr GEOM_Gen_i::GetIShapesOperations(CORBA::Long theStudyID)
2008      throw ( SALOME::SALOME_Exception )
2009 {
2010   Unexpect aCatch(SALOME_SalomeException);
2011   MESSAGE( "GEOM_Gen_i::GetIShapesOperations" );
2012
2013   GEOM::GEOM_Gen_ptr engine = _this();
2014
2015   GEOM_IShapesOperations_i* aServant =
2016     new GEOM_IShapesOperations_i(_poa, engine, _impl->GetIShapesOperations(theStudyID));
2017
2018   // activate the CORBA servant
2019   GEOM::GEOM_IShapesOperations_var operations = aServant->_this();
2020   return operations._retn();
2021 }
2022
2023 //============================================================================
2024 // function : GetIBlocksOperations
2025 // purpose  :
2026 //============================================================================
2027 GEOM::GEOM_IBlocksOperations_ptr GEOM_Gen_i::GetIBlocksOperations(CORBA::Long theStudyID)
2028      throw ( SALOME::SALOME_Exception )
2029 {
2030   Unexpect aCatch(SALOME_SalomeException);
2031   MESSAGE( "GEOM_Gen_i::GetIBlocksOperations" );
2032
2033   GEOM::GEOM_Gen_ptr engine = _this();
2034
2035   GEOM_IBlocksOperations_i* aServant =
2036     new GEOM_IBlocksOperations_i(_poa, engine, _impl->GetIBlocksOperations(theStudyID));
2037
2038   // activate the CORBA servant
2039   GEOM::GEOM_IBlocksOperations_var operations = aServant->_this();
2040   return operations._retn();
2041 }
2042
2043 //============================================================================
2044 // function : GetIBooleanOperations
2045 // purpose  :
2046 //============================================================================
2047 GEOM::GEOM_IBooleanOperations_ptr GEOM_Gen_i::GetIBooleanOperations(CORBA::Long theStudyID)
2048      throw ( SALOME::SALOME_Exception )
2049 {
2050   Unexpect aCatch(SALOME_SalomeException);
2051   MESSAGE( "GEOM_Gen_i::GetIBooleanOperations" );
2052
2053   GEOM::GEOM_Gen_ptr engine = _this();
2054
2055   GEOM_IBooleanOperations_i* aServant =
2056     new GEOM_IBooleanOperations_i(_poa, engine, _impl->GetIBooleanOperations(theStudyID));
2057
2058   // activate the CORBA servant
2059   GEOM::GEOM_IBooleanOperations_var operations = aServant->_this();
2060   return operations._retn();
2061 }
2062
2063 //============================================================================
2064 // function : GetICurvesOperations
2065 // purpose  :
2066 //============================================================================
2067 GEOM::GEOM_ICurvesOperations_ptr GEOM_Gen_i::GetICurvesOperations(CORBA::Long theStudyID)
2068      throw ( SALOME::SALOME_Exception )
2069 {
2070   Unexpect aCatch(SALOME_SalomeException);
2071   MESSAGE( "GEOM_Gen_i::GetICurvesOperations" );
2072
2073   GEOM::GEOM_Gen_ptr engine = _this();
2074
2075   GEOM_ICurvesOperations_i* aServant =
2076     new GEOM_ICurvesOperations_i(_poa, engine, _impl->GetICurvesOperations(theStudyID));
2077
2078   // activate the CORBA servant
2079   GEOM::GEOM_ICurvesOperations_var operations = aServant->_this();
2080   return operations._retn();
2081 }
2082
2083 //============================================================================
2084 // function : GetILocalOperations
2085 // purpose  :
2086 //============================================================================
2087 GEOM::GEOM_ILocalOperations_ptr GEOM_Gen_i::GetILocalOperations(CORBA::Long theStudyID)
2088      throw ( SALOME::SALOME_Exception )
2089 {
2090   Unexpect aCatch(SALOME_SalomeException);
2091   MESSAGE( "GEOM_Gen_i::GetILocalOperations" );
2092
2093   GEOM::GEOM_Gen_ptr engine = _this();
2094
2095   GEOM_ILocalOperations_i* aServant =
2096     new GEOM_ILocalOperations_i(_poa, engine, _impl->GetILocalOperations(theStudyID));
2097
2098   // activate the CORBA servant
2099   GEOM::GEOM_ILocalOperations_var operations = aServant->_this();
2100   return operations._retn();
2101 }
2102
2103 //============================================================================
2104 // function : GetIHealingOperations
2105 // purpose  :
2106 //============================================================================
2107 GEOM::GEOM_IHealingOperations_ptr GEOM_Gen_i::GetIHealingOperations(CORBA::Long theStudyID)
2108      throw ( SALOME::SALOME_Exception )
2109 {
2110   Unexpect aCatch(SALOME_SalomeException);
2111   MESSAGE( "GEOM_Gen_i::IHealingOperations" );
2112
2113   GEOM::GEOM_Gen_ptr engine = _this();
2114
2115   GEOM_IHealingOperations_i* aServant =
2116     new GEOM_IHealingOperations_i(_poa, engine, _impl->GetIHealingOperations(theStudyID));
2117
2118   // activate the CORBA servant
2119   GEOM::GEOM_IHealingOperations_var operations = aServant->_this();
2120   return operations._retn();
2121 }
2122
2123 //============================================================================
2124 // function : GetIInsertOperations
2125 // purpose  :
2126 //============================================================================
2127 GEOM::GEOM_IInsertOperations_ptr GEOM_Gen_i::GetIInsertOperations(CORBA::Long theStudyID)
2128      throw ( SALOME::SALOME_Exception )
2129 {
2130   Unexpect aCatch(SALOME_SalomeException);
2131   MESSAGE( "GEOM_Gen_i::GetIInsertOperations" );
2132
2133   GEOM::GEOM_Gen_ptr engine = _this();
2134
2135   GEOM_IInsertOperations_i* aServant =
2136     new GEOM_IInsertOperations_i(_poa, engine, _impl->GetIInsertOperations(theStudyID));
2137
2138   // activate the CORBA servant
2139   GEOM::GEOM_IInsertOperations_var operations = aServant->_this();
2140   return operations._retn();
2141 }
2142
2143 //============================================================================
2144 // function : GetIMeasureOperations
2145 // purpose  :
2146 //============================================================================
2147 GEOM::GEOM_IMeasureOperations_ptr GEOM_Gen_i::GetIMeasureOperations(CORBA::Long theStudyID)
2148      throw ( SALOME::SALOME_Exception )
2149 {
2150   Unexpect aCatch(SALOME_SalomeException);
2151   MESSAGE( "GEOM_Gen_i::GetIMeasureOperations" );
2152
2153   GEOM::GEOM_Gen_ptr engine = _this();
2154
2155   GEOM_IMeasureOperations_i* aServant =
2156     new GEOM_IMeasureOperations_i(_poa, engine, _impl->GetIMeasureOperations(theStudyID));
2157
2158   // activate the CORBA servant
2159   GEOM::GEOM_IMeasureOperations_var operations = aServant->_this();
2160   return operations._retn();
2161 }
2162
2163 //============================================================================
2164 // function : GetIGroupOperations
2165 // purpose  :
2166 //============================================================================
2167 GEOM::GEOM_IGroupOperations_ptr GEOM_Gen_i::GetIGroupOperations(CORBA::Long theStudyID)
2168      throw ( SALOME::SALOME_Exception )
2169 {
2170   Unexpect aCatch(SALOME_SalomeException);
2171   MESSAGE( "GEOM_Gen_i::GetIGroupOperations" );
2172
2173   GEOM::GEOM_Gen_ptr engine = _this();
2174
2175   GEOM_IGroupOperations_i* aServant =
2176     new GEOM_IGroupOperations_i(_poa, engine, _impl->GetIGroupOperations(theStudyID));
2177
2178   // activate the CORBA servant
2179   GEOM::GEOM_IGroupOperations_var operations = aServant->_this();
2180   return operations._retn();
2181 }
2182
2183 //============================================================================
2184 // function : GetIAdvancedOperations
2185 // purpose  :
2186 //============================================================================
2187 GEOM::GEOM_IAdvancedOperations_ptr GEOM_Gen_i::GetIAdvancedOperations(CORBA::Long theStudyID)
2188      throw ( SALOME::SALOME_Exception )
2189 {
2190   Unexpect aCatch(SALOME_SalomeException);
2191   MESSAGE( "GEOM_Gen_i::GetIAdvancedOperations" );
2192
2193   GEOM::GEOM_Gen_ptr engine = _this();
2194
2195   GEOM_IAdvancedOperations_i* aServant =
2196     new GEOM_IAdvancedOperations_i(_poa, engine, _impl->GetIAdvancedOperations(theStudyID));
2197
2198   // activate the CORBA servant
2199   GEOM::GEOM_IAdvancedOperations_var operations = aServant->_this();
2200   return operations._retn();
2201 }
2202
2203 //=============================================================================
2204 /*!
2205  *  AddSubShape
2206  */
2207 //=============================================================================
2208 GEOM::GEOM_Object_ptr GEOM_Gen_i::AddSubShape (GEOM::GEOM_Object_ptr theMainShape,
2209                                                const GEOM::ListOfLong& theIndices)
2210 {
2211   if (CORBA::is_nil(theMainShape) || theIndices.length() < 1)
2212     return GEOM::GEOM_Object::_nil();
2213   CORBA::String_var entry = theMainShape->GetEntry();
2214   Handle(GEOM_Object) aMainsShape = _impl->GetObject(theMainShape->GetStudyID(), entry);
2215   if (aMainsShape.IsNull()) return GEOM::GEOM_Object::_nil();
2216
2217   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1, theIndices.length());
2218   for(Standard_Integer i = 0; i<theIndices.length(); i++) anArray->SetValue(i+1, theIndices[i]);
2219
2220   Handle(GEOM_Object) anObject = _impl->AddSubShape(aMainsShape, anArray, true);
2221   if(anObject.IsNull()) return GEOM::GEOM_Object::_nil();
2222
2223   TCollection_AsciiString anEntry;
2224   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
2225   return GetObject(anObject->GetDocID(), anEntry.ToCString());
2226 }
2227
2228 //=============================================================================
2229 /*!
2230  *  RemoveObject
2231  */
2232 //=============================================================================
2233 void GEOM_Gen_i::RemoveObject(GEOM::GEOM_Object_ptr theObject)
2234 {
2235   CORBA::String_var anEntry = theObject->GetEntry();
2236   Handle(GEOM_Object) anObject = _impl->GetObject(theObject->GetStudyID(), anEntry, false);
2237   if (anObject.IsNull()) return;
2238   _impl->RemoveObject(anObject);
2239   return;
2240 }
2241
2242
2243 //=================================================================================
2244 // function : GetStringFromIOR()
2245 // purpose  : returns a string that represents  a 'GEOM::GEOM_Object_var'
2246 //=================================================================================
2247 char* GEOM_Gen_i::GetStringFromIOR(GEOM::GEOM_Object_ptr theObject) {
2248   return _orb->object_to_string(theObject);
2249 }
2250
2251
2252 //=================================================================================
2253 // function : GetIORFromString()
2254 // purpose  : returns a 'GEOM::GEOM_Object_var' from a string representing it
2255 //=================================================================================
2256 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetIORFromString(const char* stringIOR) {
2257   GEOM::GEOM_Object_var aGeomObject;
2258   if(strcmp(stringIOR,"") != 0){
2259     CORBA::Object_var anObject = _orb->string_to_object(stringIOR);
2260     if(!CORBA::is_nil(anObject))
2261       aGeomObject =  GEOM::GEOM_Object::_narrow(anObject.in());
2262   }
2263   return aGeomObject._retn();
2264 }
2265
2266 //=================================================================================
2267 // function : GetObject()
2268 // purpose  :
2269 //=================================================================================
2270 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetObject (CORBA::Long theStudyID, const char* theEntry)
2271 {
2272   GEOM::GEOM_Object_var obj;
2273   Handle(GEOM_Object) handle_object = _impl->GetObject(theStudyID, (char*)theEntry);
2274   if (handle_object.IsNull()) return obj._retn();
2275
2276   TCollection_AsciiString stringIOR = handle_object->GetIOR();
2277   if (stringIOR.Length() > 1) {
2278     CORBA::Object_var corba_object = _orb->string_to_object(stringIOR.ToCString());
2279     if (!CORBA::is_nil(corba_object)) obj = GEOM::GEOM_Object::_narrow(corba_object);
2280     return obj._retn();
2281   }
2282
2283   GEOM::GEOM_Gen_ptr engine = _this();
2284   //transfer the reference to GEOM_Object_i
2285   GEOM_Object_i* servant = new GEOM_Object_i (_poa, engine, handle_object);
2286   PortableServer::ObjectId_var id = _poa->activate_object(servant);
2287
2288   obj = servant->_this();
2289   CORBA::String_var objStr = _orb->object_to_string(obj);
2290   TCollection_AsciiString anAscii( (char *)objStr.in() );
2291   handle_object->SetIOR( anAscii );
2292   return obj._retn();
2293 }
2294
2295 //=================================================================================
2296 // function : hasObjectInfo()
2297 // purpose  : shows if module provides information for its objects
2298 //=================================================================================
2299 bool GEOM_Gen_i::hasObjectInfo()
2300 {
2301   return true;
2302 }
2303
2304 //=================================================================================
2305 // function : getObjectInfo()
2306 // purpose  : returns an information for a given object by its entry
2307 //=================================================================================
2308 char* GEOM_Gen_i::getObjectInfo(CORBA::Long studyId, const char* entry)
2309 {
2310   GEOM::GEOM_Object_var aGeomObject;
2311
2312   CORBA::Object_var aSMObject = name_service->Resolve( "/myStudyManager" );
2313   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow( aSMObject );
2314   SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID( studyId );
2315   SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( entry );
2316   SALOMEDS::SObject_var aResultSObj;
2317   if (aSObj->ReferencedObject(aResultSObj))
2318     aSObj = aResultSObj;
2319
2320   SALOMEDS::GenericAttribute_var anAttr;
2321   if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
2322     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
2323     CORBA::String_var aVal = anIOR->Value();
2324     anIOR->Destroy();
2325     CORBA::Object_var anObject = aStudy->ConvertIORToObject(aVal);
2326     aGeomObject = GEOM::GEOM_Object::_narrow(anObject);
2327   }
2328   if (!aSObj->_is_nil() )
2329     aSObj->Destroy();
2330
2331   const char* aTypeInfo = "Object";
2332   if ( !aGeomObject->_is_nil() ) {
2333     GEOM::GEOM_IKindOfShape::shape_kind aKind;
2334     GEOM::ListOfLong_var anInts;
2335     GEOM::ListOfDouble_var aDbls;
2336
2337     GEOM::GEOM_IMeasureOperations_var anOp = GetIMeasureOperations( studyId );
2338     aKind = anOp->KindOfShape( aGeomObject, anInts, aDbls );
2339
2340     if ( anOp->IsDone() ) {
2341       switch ( aKind ) {
2342       case GEOM::GEOM_IKindOfShape::COMPOUND:
2343         aTypeInfo = "Compound";
2344         break;
2345       case GEOM::GEOM_IKindOfShape::COMPSOLID:
2346         aTypeInfo = "CompSolid";
2347         break;
2348       case GEOM::GEOM_IKindOfShape::SHELL:
2349         aTypeInfo = "Shell";
2350         break;
2351       case GEOM::GEOM_IKindOfShape::WIRE:
2352         if ( anInts[0] == 1 )
2353           aTypeInfo = "Closed Wire";
2354         else if ( anInts[0] == 2 )
2355           aTypeInfo = "Opened Wire";
2356         else
2357           aTypeInfo = "Wire";
2358         break;
2359         // SOLIDs
2360       case GEOM::GEOM_IKindOfShape::SPHERE:
2361         aTypeInfo = "Sphere";
2362         break;
2363       case GEOM::GEOM_IKindOfShape::CYLINDER:
2364         aTypeInfo = "Cylinder";
2365         break;
2366       case GEOM::GEOM_IKindOfShape::BOX:
2367       case GEOM::GEOM_IKindOfShape::ROTATED_BOX:
2368         aTypeInfo = "Box";
2369         break;
2370       case GEOM::GEOM_IKindOfShape::TORUS:
2371         aTypeInfo = "Torus";
2372         break;
2373       case GEOM::GEOM_IKindOfShape::CONE:
2374         aTypeInfo = "Cone";
2375         break;
2376       case GEOM::GEOM_IKindOfShape::POLYHEDRON:
2377         aTypeInfo = "Polyhedron";
2378         break;
2379       case GEOM::GEOM_IKindOfShape::SOLID:
2380         aTypeInfo = "Solid";
2381         break;
2382         // FACEs
2383       case GEOM::GEOM_IKindOfShape::SPHERE2D:
2384         aTypeInfo = "Spherical Face";
2385         break;
2386       case GEOM::GEOM_IKindOfShape::CYLINDER2D:
2387         aTypeInfo = "Cylindrical Face";
2388         break;
2389       case GEOM::GEOM_IKindOfShape::TORUS2D:
2390         aTypeInfo = "Toroidal Face";
2391         break;
2392       case GEOM::GEOM_IKindOfShape::CONE2D:
2393         aTypeInfo = "Conical Face";
2394         break;
2395       case GEOM::GEOM_IKindOfShape::DISK_CIRCLE:
2396         aTypeInfo = "Disk";
2397         break;
2398       case GEOM::GEOM_IKindOfShape::DISK_ELLIPSE:
2399         aTypeInfo = "Elliptical Face";
2400         break;
2401       case GEOM::GEOM_IKindOfShape::POLYGON:
2402         aTypeInfo = "Polygon";
2403         break;
2404       case GEOM::GEOM_IKindOfShape::PLANE:
2405         aTypeInfo = "Plane";
2406         break;
2407       case GEOM::GEOM_IKindOfShape::PLANAR:
2408         aTypeInfo = "Planar Face";
2409         break;
2410       case GEOM::GEOM_IKindOfShape::FACE:
2411         aTypeInfo = "Face";
2412         break;
2413         // EDGEs
2414       case GEOM::GEOM_IKindOfShape::CIRCLE:
2415         aTypeInfo = "Circle";
2416         break;
2417       case GEOM::GEOM_IKindOfShape::ARC_CIRCLE:
2418         aTypeInfo = "Arc Circle";
2419         break;
2420       case GEOM::GEOM_IKindOfShape::ELLIPSE:
2421         aTypeInfo = "Ellipse";
2422         break;
2423       case GEOM::GEOM_IKindOfShape::ARC_ELLIPSE:
2424         aTypeInfo = "Arc Ellipse";
2425         break;
2426       case GEOM::GEOM_IKindOfShape::LINE:
2427         aTypeInfo = "Line";
2428         break;
2429       case GEOM::GEOM_IKindOfShape::SEGMENT:
2430         aTypeInfo = "Segment";
2431         break;
2432       case GEOM::GEOM_IKindOfShape::EDGE:
2433         aTypeInfo = "Edge";
2434         break;
2435       case GEOM::GEOM_IKindOfShape::VERTEX:
2436         aTypeInfo = "Vertex";
2437         break;
2438       default:
2439         break;
2440       }
2441     }
2442   }
2443
2444   char* anInfo = new char[strlen("Module ") + strlen(ComponentDataType()) + strlen(", ") + strlen(aTypeInfo) + 3];
2445   sprintf(anInfo, "Module %s, %s", ComponentDataType(), aTypeInfo);
2446
2447   char* ret = CORBA::string_dup(anInfo);
2448   delete [] anInfo;
2449   return ret;
2450 }
2451
2452 //=====================================================================================
2453 // EXPORTED METHODS
2454 //=====================================================================================
2455 extern "C"
2456 {
2457   /*
2458   GEOM_I_EXPORT
2459   PortableServer::ObjectId* GEOMEngine_factory(CORBA::ORB*, PortableServer::POA*, PortableServer::ObjectId*, const char*, const char*);
2460   */
2461
2462   GEOM_I_EXPORT
2463   PortableServer::ObjectId* GEOMEngine_factory(CORBA::ORB_ptr            orb,
2464                                                PortableServer::POA_ptr   poa,
2465                                                PortableServer::ObjectId* contId,
2466                                                const char*               instanceName,
2467                                                const char*               interfaceName)
2468   {
2469     GEOM_Gen_i* myGEOM_Gen_i = new GEOM_Gen_i(orb, poa, contId, instanceName, interfaceName);
2470     return myGEOM_Gen_i->getId();
2471   }
2472 }