]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_Gen_i.cc
Salome HOME
5606d8bfc91367a31b5456de295bb9438ce81e66
[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     GEOM::GEOM_ITransformOperations_var aTrsfOp = GetITransformOperations(theStudy->StudyId());
972
973     // Reconstruct arguments and tree of sub-shapes of the arguments
974     CORBA::String_var anIOR;
975     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
976     for (Standard_Integer i = 0; i < aLength; i++)
977     {
978       GEOM::GEOM_Object_var anArgO = aList[i];
979       if (!CORBA::is_nil(anArgO)) {
980         anIOR = _orb->object_to_string(anArgO);
981         SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
982         TCollection_AsciiString anArgName;
983         if (CORBA::is_nil(anArgSO)) {
984           anArgName = "arg_";
985           anArgName += TCollection_AsciiString(i);
986         }
987         else {
988           anArgName = anArgSO->GetName();
989         }
990
991         // Find a sub-shape of theObject in place of the argument
992         GEOM::GEOM_Object_var aSubO;
993         switch (theFindMethod) {
994         case GEOM::FSM_GetInPlace:
995           {
996             // Use GetInPlace
997             aSubO = aShapesOp->GetInPlace(theObject, anArgO);
998           }
999           break;
1000         case GEOM::FSM_MultiTransformed:
1001           {
1002             // Only for Multi-transformations
1003             GEOM::GEOM_Object_var anArgOTrsf = aTrsfOp->TransformLikeOtherCopy(anArgO, theObject);
1004             if (!CORBA::is_nil(anArgOTrsf)) {
1005               CORBA::String_var anArgOTrsfEntry = anArgOTrsf->GetEntry();
1006               Handle(GEOM_Object) anArgOTrsfImpl = _impl->GetObject(anArgOTrsf->GetStudyID(), anArgOTrsfEntry);
1007               Handle(GEOM_Function) anArgOTrsfFun = anArgOTrsfImpl->GetLastFunction();
1008               anArgOTrsfFun->SetDescription("");
1009               aSubO = aShapesOp->GetInPlace(theObject, anArgOTrsf);
1010             }
1011             /*
1012             Handle(GEOM_Function) anOFun = theObject->GetLastFunction();
1013             if (!anOFun.IsNull()) {
1014               CORBA::String_var entryArg = anArgO->GetEntry();
1015               Handle(GEOM_Object) anArgOImpl = _impl->GetObject(anArgO->GetStudyID(), entryArg);
1016               if (!anArgOImpl.IsNull()) {
1017                 TopoDS_Shape anArgOShape = anArgOImpl->GetValue();
1018                 TopoDS_Shape aMultiArgShape;
1019                 //GEOM::GEOM_Object_var anArgOMulti; // ???
1020                 switch (anOFun->GetType()) {
1021                 case TRANSLATE_1D:
1022                   {
1023                     GEOMImpl_ITranslate aTI (anOFun);
1024                     aMultiArgShape = GEOMImpl_ITransformOperations::TranslateShape1D(anArgOShape, &aTI);
1025                     //anArgOMulti = aTrsfOp->Translate1D(anArgO, , , );
1026                   }
1027                   break;
1028                 case TRANSLATE_2D:
1029                   {
1030                     GEOMImpl_ITranslate aTI (anOFun);
1031                     aMultiArgShape = GEOMImpl_ITransformOperations::TranslateShape2D(anArgOShape, &aTI);
1032                   }
1033                   break;
1034                 case ROTATE_1D:
1035                   {
1036                     GEOMImpl_IRotate aTI (anOFun);
1037                     //aMultiArgShape = GEOMImpl_ITransformOperations::TranslateShape2D(anArgOShape, &aTI);
1038                   }
1039                   break;
1040                 case ROTATE_2D:
1041                   {
1042                     GEOMImpl_IRotate aTI (anOFun);
1043                     //aMultiArgShape = GEOMImpl_ITransformOperations::TranslateShape2D(anArgOShape, &aTI);
1044                   }
1045                   break;
1046                 default:
1047                   {}
1048                 }
1049                 GEOM::GEOM_Object_var anArgOMulti = (aMultiArgShape); // TODO
1050                 Handle(GEOM_Function) anArgOMultiFun = anArgOMulti->GetLastFunction();
1051                 anArgOMultiFun->SetDescription("");
1052                 aSubO = aShapesOp->GetInPlace(theObject, anArgOMulti);
1053               }
1054             }
1055             */
1056           }
1057           break;
1058         case GEOM::FSM_Transformed:
1059           {
1060             // transformation, cannot use GetInPlace, operate with indices
1061             GEOM::ListOfLong_var anIDs = anArgO->GetSubShapeIndices();
1062             if (anIDs->length() > 1) {
1063               // group
1064               aSubO = aGroupOp->CreateGroup(theObject, aGroupOp->GetType(anArgO));
1065               if (!CORBA::is_nil(aSubO))
1066                 aGroupOp->UnionIDs(aSubO, anIDs);
1067             }
1068             else if (anIDs->length() > 0) {
1069               // single sub-shape
1070               aSubO = aShapesOp->GetSubShape(theObject, anIDs[0]);
1071             }
1072           }
1073           break;
1074         case GEOM::FSM_GetSame:
1075           {
1076             // Use GetSame
1077             aSubO = aShapesOp->GetSame(theObject, anArgO);
1078           }
1079           break;
1080         case GEOM::FSM_GetShapesOnShape:
1081           {
1082             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
1083             aSubO = aShapesOp->GetShapesOnShapeAsCompound(anArgO, theObject,
1084               (short)GEOM::SOLID, GEOM::ST_ONIN);
1085           }
1086           break;
1087         case GEOM::FSM_GetInPlaceByHistory:
1088           {
1089             // Use GetInPlaceByHistory
1090             aSubO = aShapesOp->GetInPlaceByHistory(theObject, anArgO);
1091           }
1092           break;
1093         default:
1094           {}
1095         }
1096
1097         if (!CORBA::is_nil(aSubO)) {
1098           // remember restored objects for Python Dump
1099           addToListOfGO(anArgO, anOutArgs);
1100
1101           // add to parts list
1102           addToListOfGO( aSubO, aParts );
1103
1104           // Publish the sub-shape
1105           SALOMEDS::SObject_var aSubSO;
1106           if (!CORBA::is_nil(theSObject)) {
1107             TCollection_AsciiString aSubName;
1108             if (theAddPrefix) {
1109               aSubName = "from_";
1110             }
1111             aSubName += anArgName;
1112             aSubSO = aStudyBuilder->NewObject(theSObject);
1113             aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
1114             // Restore color
1115             aSubO->SetColor(anArgO->GetColor());
1116             // set the texture
1117             if (aSubO->GetShapeType() == GEOM::VERTEX) {
1118               aSubO->SetMarkerStd(anArgO->GetMarkerType(), anArgO->GetMarkerSize());
1119               if (anArgO->GetMarkerType() == GEOM::MT_USER)
1120                 aSubO->SetMarkerTexture(anArgO->GetMarkerTexture());
1121             }
1122           }
1123
1124           if (!CORBA::is_nil(anArgSO)) {
1125             // Restore published sub-shapes of the argument
1126             GEOM::ListOfGO_var aSubParts;
1127             if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
1128               // pass theObject, because only it has the history
1129               aSubParts = RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1130                                                    theObject, anOutArgs, theFindMethod, theAddPrefix);
1131             else
1132               aSubParts = RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1133                                                    aSubO, anOutArgs, theFindMethod, theAddPrefix);
1134             // add to parts list
1135             addToListOfGO( aSubParts, aParts );
1136           }
1137         }
1138         else { // GetInPlace failed, try to build from published parts
1139           if (!CORBA::is_nil(anArgSO)) {
1140             SALOMEDS::SObject_var aSubSO;
1141             if (!CORBA::is_nil(theSObject))
1142               aSubSO = aStudyBuilder->NewObject(theSObject);
1143
1144             // Restore published sub-shapes of the argument
1145             GEOM::ListOfGO_var aSubParts =
1146               RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1147                                        theObject, anOutArgs, theFindMethod, theAddPrefix);
1148
1149             // add to parts list
1150             addToListOfGO( aSubParts, aParts );
1151
1152             if (aSubParts->length() > 0) {
1153               // remember restored objects for Python Dump
1154               addToListOfGO(anArgO, anOutArgs);
1155
1156               // try to build an argument from a set of its sub-shapes,
1157               // that published and will be reconstructed
1158               if (aSubParts->length() > 1) {
1159                 aSubO = aShapesOp->MakeCompound(aSubParts);
1160                 // add to parts list
1161                 addToListOfGO( aSubO, aParts );
1162               }
1163               else {
1164                 aSubO = aSubParts[0];
1165               }
1166               if (!CORBA::is_nil(aSubO) && !CORBA::is_nil(aSubSO)) {
1167                 // Publish the sub-shape
1168                 TCollection_AsciiString aSubName;
1169                 if (theAddPrefix) {
1170                   aSubName = "from_parts_of_";
1171                 }
1172                 aSubName += anArgName;
1173                 aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
1174                 // Restore color
1175                 aSubO->SetColor(anArgO->GetColor());
1176                 // set the texture
1177                 if (aSubO->GetShapeType() == GEOM::VERTEX) {
1178                   aSubO->SetMarkerStd(anArgO->GetMarkerType(), anArgO->GetMarkerSize());
1179                   if (anArgO->GetMarkerType() == GEOM::MT_USER)
1180                     aSubO->SetMarkerTexture(anArgO->GetMarkerTexture());
1181                 }
1182               }
1183             }
1184             else if (!CORBA::is_nil(aSubSO)) {
1185               // remove created aSubSO, because no parts have been found
1186               aStudyBuilder->RemoveObject(aSubSO);
1187             }
1188           }
1189         } // try to build from published parts
1190         anArgSO->Destroy();
1191       }
1192     } // process arguments
1193   }
1194   std::set<std::string> anObjEntryMap;
1195   GEOM::ListOfGO_var aResParts = new GEOM::ListOfGO;
1196   int nbRes = 0;
1197   int nb = aParts->length();
1198   aResParts->length(nb);
1199   if (nb > 0)
1200   {
1201     Handle(GEOM_Object) aMainObj = _impl->GetObject(theObject->GetStudyID(), theObject->GetEntry());
1202     Handle(GEOM_Function) aFunction = aMainObj->GetLastFunction();
1203     GEOM::TPythonDump pd (aFunction, true);
1204     pd <<"[";
1205     int i = 0, j = 0;
1206     for ( ; i < nb; i++ )
1207     {
1208       GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_duplicate( aParts[ i ] );
1209       if (CORBA::is_nil(anObj))
1210         continue;
1211       char* anEntry = anObj->GetEntry();
1212       if (anObjEntryMap.count(anEntry))
1213         continue; // already treated
1214       anObjEntryMap.insert(anEntry);
1215       aResParts[nbRes++] = anObj;
1216       // clear python dump of object
1217       Handle(GEOM_Object) aGeomObj = _impl->GetObject(anObj->GetStudyID(), anEntry);
1218       Handle(GEOM_Function) anObjFun = aGeomObj->GetLastFunction();
1219       if ( !anObjFun.IsNull() )
1220         anObjFun->SetDescription( "" );
1221       if ( j > 0 )
1222         pd << ", ";
1223       pd << aGeomObj;
1224       j++;
1225     }
1226     pd <<"]" << " = geompy.RestoreGivenSubShapes(" << aMainObj << ", " << "[";
1227     //i = 0; nb = theArgs.length(); j = 0;
1228     i = 0; nb = anOutArgs->length(); j = 0;
1229     for ( ; i < nb; i++ )
1230     {
1231       //GEOM::GEOM_Object_var anObj = theArgs[ i ];
1232       GEOM::GEOM_Object_var anObj = anOutArgs[ i ];
1233       if (CORBA::is_nil(anObj))
1234         continue;
1235       Handle(GEOM_Object) aGeomObj = _impl->GetObject(anObj->GetStudyID(), anObj->GetEntry());
1236       if ( j > 0 )
1237         pd << ", ";
1238       pd << aGeomObj;
1239       j++;
1240     }
1241     pd <<"]" << ", " <<"geompy.GEOM.";
1242     switch (theFindMethod) {
1243     case GEOM::FSM_GetInPlace:
1244       pd << "FSM_GetInPlace"; break;
1245     case GEOM::FSM_MultiTransformed:
1246       pd << "FSM_MultiTransformed"; break;
1247     case GEOM::FSM_Transformed:
1248       pd << "FSM_Transformed"; break;
1249     case GEOM::FSM_GetSame:
1250       pd << "FSM_GetSame"; break;
1251     case GEOM::FSM_GetShapesOnShape:
1252       pd << "FSM_GetShapesOnShape"; break;
1253     case GEOM::FSM_GetInPlaceByHistory:
1254     default:
1255       pd << "FSM_GetInPlaceByHistory"; break;
1256     }
1257     pd << ", " << theInheritFirstArg << ", " << theAddPrefix << ")";
1258   }
1259   aResParts->length(nbRes);
1260   return aResParts._retn();
1261 }
1262
1263 //============================================================================
1264 // function : RestoreSubShapesOneLevel
1265 // purpose  : Private method
1266 //============================================================================
1267 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapesOneLevel (SALOMEDS::Study_ptr     theStudy,
1268                                                       SALOMEDS::SObject_ptr   theOldSO,
1269                                                       SALOMEDS::SObject_ptr   theNewSO,
1270                                                       GEOM::GEOM_Object_ptr   theNewO,
1271                                                       GEOM::ListOfGO&         theOutArgs,
1272                                                       GEOM::find_shape_method theFindMethod,
1273                                                       CORBA::Boolean          theAddPrefix)
1274 {
1275   int i = 0;
1276   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
1277   GEOM::ListOfGO_var aNewParts = new GEOM::ListOfGO;
1278   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theOldSO) ||
1279       CORBA::is_nil(theNewO) /*|| CORBA::is_nil(theNewSO)*/)
1280     return aParts._retn();
1281
1282   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
1283
1284   // Get interface, containing method, which we will use to reconstruct sub-shapes
1285   GEOM::GEOM_IShapesOperations_var  aShapesOp = GetIShapesOperations(theStudy->StudyId());
1286   GEOM::GEOM_IGroupOperations_var    aGroupOp = GetIGroupOperations(theStudy->StudyId());
1287   GEOM::GEOM_ITransformOperations_var aTrsfOp = GetITransformOperations(theStudy->StudyId());
1288
1289   // Reconstruct published sub-shapes
1290   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator(theOldSO);
1291
1292   int aLen = 0;
1293   for (it->Init(); it->More(); it->Next()) {
1294     aLen++;
1295   }
1296   aParts->length(aLen);
1297
1298   for (it->Init(); it->More(); it->Next()) {
1299     SALOMEDS::SObject_var anOldSubSO = it->Value();
1300
1301     TCollection_AsciiString anArgName = anOldSubSO->GetName();
1302
1303     SALOMEDS::GenericAttribute_var anAttr;
1304     if (anOldSubSO->FindAttribute(anAttr, "AttributeIOR")) {
1305       SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1306       GEOM::GEOM_Object_var anOldSubO =
1307         GEOM::GEOM_Object::_narrow(_orb->string_to_object(anAttrIOR->Value()));
1308       if (!CORBA::is_nil(anOldSubO)) {
1309         // Find a sub-shape of theNewO in place of anOldSubO
1310         GEOM::GEOM_Object_var aNewSubO;
1311         switch (theFindMethod) {
1312         case GEOM::FSM_GetInPlace:
1313           {
1314             // Use GetInPlace
1315             aNewSubO = aShapesOp->GetInPlace(theNewO, anOldSubO);
1316           }
1317           break;
1318         case GEOM::FSM_MultiTransformed:
1319           {
1320             // Only for Multi-transformations
1321             GEOM::GEOM_Object_var anArgOTrsf = aTrsfOp->TransformLikeOtherCopy(anOldSubO, theNewO);
1322             if (!CORBA::is_nil(anArgOTrsf)) {
1323               CORBA::String_var anArgOTrsfEntry = anArgOTrsf->GetEntry();
1324               Handle(GEOM_Object) anArgOTrsfImpl = _impl->GetObject(anArgOTrsf->GetStudyID(), anArgOTrsfEntry);
1325               Handle(GEOM_Function) anArgOTrsfFun = anArgOTrsfImpl->GetLastFunction();
1326               anArgOTrsfFun->SetDescription("");
1327               aNewSubO = aShapesOp->GetInPlace(theNewO, anArgOTrsf);
1328             }
1329           }
1330           break;
1331         case GEOM::FSM_Transformed:
1332           {
1333             // transformation, cannot use GetInPlace, operate with indices
1334             GEOM::ListOfLong_var anIDs = anOldSubO->GetSubShapeIndices();
1335             if (anIDs->length() > 1) {
1336               // group
1337               aNewSubO = aGroupOp->CreateGroup(theNewO, aGroupOp->GetType(anOldSubO));
1338               if (!CORBA::is_nil(aNewSubO))
1339                 aGroupOp->UnionIDs(aNewSubO, anIDs);
1340             }
1341             else {
1342               // single sub-shape
1343               aNewSubO = aShapesOp->GetSubShape(theNewO, anIDs[0]);
1344             }
1345           }
1346           break;
1347         case GEOM::FSM_GetSame:
1348           {
1349             // Use GetSame
1350             aNewSubO = aShapesOp->GetSame(theNewO, anOldSubO);
1351           }
1352           break;
1353         case GEOM::FSM_GetShapesOnShape:
1354           {
1355             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
1356             aNewSubO = aShapesOp->GetShapesOnShapeAsCompound(anOldSubO, theNewO,
1357                                                              (short)GEOM::SOLID, GEOM::ST_ONIN);
1358           }
1359           break;
1360         case GEOM::FSM_GetInPlaceByHistory:
1361           {
1362             // Use GetInPlaceByHistory
1363             aNewSubO = aShapesOp->GetInPlaceByHistory(theNewO, anOldSubO);
1364           }
1365           break;
1366         default:
1367           {}
1368         }
1369
1370         if (!CORBA::is_nil(aNewSubO)) {
1371           // remember restored objects for Python Dump
1372           addToListOfGO(anOldSubO, theOutArgs);
1373
1374           // add the part to the list
1375           aParts[i] = aNewSubO;
1376           i++;
1377           // add to parts list
1378           addToListOfGO( aNewSubO, aNewParts );
1379
1380           SALOMEDS::SObject_var aNewSubSO;
1381           if (!CORBA::is_nil(theNewSO)) {
1382               // Publish the sub-shape
1383             TCollection_AsciiString aSubName;
1384             if (theAddPrefix) {
1385               aSubName = "from_";
1386             }
1387             aSubName += anArgName;
1388             aNewSubSO = aStudyBuilder->NewObject(theNewSO);
1389             aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
1390             // Restore color
1391             aNewSubO->SetColor(anOldSubO->GetColor());
1392             // set the texture
1393             if (aNewSubO->GetShapeType() == GEOM::VERTEX) {
1394               aNewSubO->SetMarkerStd(anOldSubO->GetMarkerType(), anOldSubO->GetMarkerSize());
1395               if (anOldSubO->GetMarkerType() == GEOM::MT_USER)
1396                 aNewSubO->SetMarkerTexture(anOldSubO->GetMarkerTexture());
1397             }
1398           }
1399           // Restore published sub-shapes of the argument
1400           GEOM::ListOfGO_var aSubParts;
1401           if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
1402             // pass the main shape as Object, because only it has the history
1403             aSubParts = RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1404                                                  theNewO, theOutArgs, theFindMethod, theAddPrefix);
1405           else
1406             aSubParts = RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1407                                                  aNewSubO, theOutArgs, theFindMethod, theAddPrefix);
1408           // add to parts list
1409           addToListOfGO( aSubParts, aNewParts );
1410         }
1411         else { // GetInPlace failed, try to build from published parts
1412           SALOMEDS::SObject_var aNewSubSO;
1413           if (!CORBA::is_nil(theNewSO))
1414             aNewSubSO = aStudyBuilder->NewObject(theNewSO);
1415
1416           // Restore published sub-shapes of the argument
1417           GEOM::ListOfGO_var aSubParts =
1418             RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1419                                      theNewO, theOutArgs, theFindMethod, theAddPrefix);
1420           // add to parts list
1421           addToListOfGO( aSubParts, aNewParts );
1422
1423           if (aSubParts->length() > 0) {
1424             // remember restored objects for Python Dump
1425             addToListOfGO(anOldSubO, theOutArgs);
1426
1427             // try to build an object from a set of its sub-shapes,
1428             // that published and will be reconstructed
1429             if (aSubParts->length() > 1) {
1430               aNewSubO = aShapesOp->MakeCompound(aSubParts);
1431               // add to parts list
1432               addToListOfGO( aNewSubO, aNewParts );
1433             }
1434             else {
1435               aNewSubO = aSubParts[0];
1436             }
1437
1438             if (!CORBA::is_nil(aNewSubO)) {
1439               // add the part to the list
1440               aSubParts[i] = aNewSubO;
1441               i++;
1442
1443               // Publish the sub-shape
1444               if (!CORBA::is_nil(aNewSubSO)) {
1445                 TCollection_AsciiString aSubName;
1446                 if (theAddPrefix) {
1447                   aSubName = "from_parts_of_";
1448                 }
1449                 aSubName += anArgName;
1450                 aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
1451                 // Restore color
1452                 aNewSubO->SetColor(anOldSubO->GetColor());
1453                 // set the texture
1454                 if (aNewSubO->GetShapeType() == GEOM::VERTEX) {
1455                   aNewSubO->SetMarkerStd(anOldSubO->GetMarkerType(), anOldSubO->GetMarkerSize());
1456                   if (anOldSubO->GetMarkerType() == GEOM::MT_USER)
1457                     aNewSubO->SetMarkerTexture(anOldSubO->GetMarkerTexture());
1458                 }
1459               }
1460             }
1461           }
1462           else if (!CORBA::is_nil(aNewSubSO)) {
1463             // remove created aSubSO, because no parts have been found
1464             aStudyBuilder->RemoveObject(aNewSubSO);
1465           }
1466         } // try to build from published parts
1467       }
1468     }
1469   } // iterate on published sub-shapes
1470
1471   aParts->length(i);
1472   // add to parts list
1473   addToListOfGO( aNewParts, aParts );
1474   return aParts._retn();
1475 }
1476
1477 //============================================================================
1478 // function : RestoreGivenSubShapes
1479 // purpose  : Private method. Works only if both theObject and theSObject
1480 //            are defined, and does not check, if they correspond to each other.
1481 //            List theArgs in this case contains not only operation arguments,
1482 //            but also all subshapes, which must be published.
1483 //============================================================================
1484 GEOM::ListOfGO* GEOM_Gen_i::RestoreGivenSubShapes(SALOMEDS::Study_ptr     theStudy,
1485                                                   GEOM::GEOM_Object_ptr   theObject,
1486                                                   SALOMEDS::SObject_ptr   theSObject,
1487                                                   const GEOM::ListOfGO&   theArgs,
1488                                                   GEOM::find_shape_method theFindMethod,
1489                                                   CORBA::Boolean          theInheritFirstArg,
1490                                                   CORBA::Boolean          theAddPrefix)
1491 {
1492   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
1493   //PTv, IMP 0020001, The salome object <theSObject>
1494   //     is not obligatory in case of invokation from script
1495   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject) /*|| CORBA::is_nil(theSObject)*/)
1496     return aParts._retn();
1497
1498   // If theArgs list is empty, nothing to do
1499   Standard_Integer aLength = theArgs.length();
1500   if (aLength == 0)
1501     return aParts._retn();
1502
1503   // Get all arguments
1504   GEOM::ListOfGO_var anOpArgsList = theObject->GetDependency();
1505   Standard_Integer nbArgsActual = anOpArgsList->length();
1506
1507   // If anOpArgsList list is empty, nothing to do
1508   if (nbArgsActual == 0)
1509     return aParts._retn();
1510
1511   // Entries of arguments and subshapes
1512   std::set<std::string> anArgs;
1513   for (int i = 0; i < aLength; i++) {
1514     CORBA::String_var anEntry = theArgs[i]->GetEntry();
1515     anArgs.insert(anEntry.in());
1516   }
1517
1518   // Arguments to be published
1519   // We try to publish all arguments, that are in theArgs list
1520   GEOM::ListOfGO_var aList = new GEOM::ListOfGO;
1521   aList->length(nbArgsActual);
1522
1523   int k = 0;
1524   for (int j = 0; j < nbArgsActual; j++) {
1525     CORBA::String_var anEntry = anOpArgsList[j]->GetEntry();
1526     if (anArgs.count(anEntry.in())) {
1527       aList[k] = GEOM::GEOM_Object::_duplicate(anOpArgsList[j]);
1528       k++;
1529     }
1530   }
1531   nbArgsActual = k;
1532   //aList->length(nbArgsActual);
1533
1534   if (nbArgsActual < 1)
1535     return aParts._retn();
1536
1537   if (theInheritFirstArg || (nbArgsActual == 1)) {
1538     // Do not publish argument's reflection,
1539     // but only reconstruct its published sub-shapes
1540
1541     CORBA::String_var anIOR = _orb->object_to_string(aList[0]);
1542     SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
1543
1544     aParts = RestoreGivenSubShapesOneLevel(theStudy, anArgSO, theSObject, theObject,
1545                                            anArgs, theFindMethod, theAddPrefix);
1546
1547     // set the color of the transformed shape to the color of initial shape
1548     theObject->SetColor(aList[0]->GetColor());
1549     // set the texture
1550     if (theObject->GetShapeType() == GEOM::VERTEX) {
1551       theObject->SetMarkerStd(aList[0]->GetMarkerType(), aList[0]->GetMarkerSize());
1552       if (aList[0]->GetMarkerType() == GEOM::MT_USER)
1553         theObject->SetMarkerTexture(aList[0]->GetMarkerTexture());
1554     }
1555
1556     anArgSO->Destroy();
1557   }
1558   else {
1559     // Get interface, containing method, which we will use to reconstruct sub-shapes
1560     GEOM::GEOM_IShapesOperations_var  aShapesOp = GetIShapesOperations(theStudy->StudyId());
1561     GEOM::GEOM_IGroupOperations_var    aGroupOp = GetIGroupOperations(theStudy->StudyId());
1562     GEOM::GEOM_ITransformOperations_var aTrsfOp = GetITransformOperations(theStudy->StudyId());
1563
1564     // Reconstruct arguments and tree of sub-shapes of the arguments
1565     CORBA::String_var anIOR;
1566     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
1567     for (Standard_Integer i = 0; i < nbArgsActual; i++)
1568     {
1569       GEOM::GEOM_Object_var anArgO = aList[i];
1570       if (!CORBA::is_nil(anArgO)) {
1571         anIOR = _orb->object_to_string(anArgO);
1572         SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
1573         TCollection_AsciiString anArgName;
1574         if (CORBA::is_nil(anArgSO)) {
1575           anArgName = "arg_";
1576           anArgName += TCollection_AsciiString(i);
1577         }
1578         else {
1579           anArgName = anArgSO->GetName();
1580         }
1581
1582         // Find a sub-shape of theObject in place of the argument
1583         GEOM::GEOM_Object_var aSubO;
1584         switch (theFindMethod) {
1585         case GEOM::FSM_GetInPlace:
1586           {
1587             // Use GetInPlace
1588             aSubO = aShapesOp->GetInPlace(theObject, anArgO);
1589           }
1590           break;
1591         case GEOM::FSM_MultiTransformed:
1592           {
1593             // Only for Multi-transformations
1594             GEOM::GEOM_Object_var anArgOTrsf = aTrsfOp->TransformLikeOtherCopy(anArgO, theObject);
1595             if (!CORBA::is_nil(anArgOTrsf)) {
1596               CORBA::String_var anArgOTrsfEntry = anArgOTrsf->GetEntry();
1597               Handle(GEOM_Object) anArgOTrsfImpl = _impl->GetObject(anArgOTrsf->GetStudyID(), anArgOTrsfEntry);
1598               Handle(GEOM_Function) anArgOTrsfFun = anArgOTrsfImpl->GetLastFunction();
1599               anArgOTrsfFun->SetDescription("");
1600               aSubO = aShapesOp->GetInPlace(theObject, anArgOTrsf);
1601             }
1602           }
1603           break;
1604         case GEOM::FSM_Transformed:
1605           {
1606             // transformation, cannot use GetInPlace, operate with indices
1607             GEOM::ListOfLong_var anIDs = anArgO->GetSubShapeIndices();
1608             if (anIDs->length() > 1) {
1609               // group
1610               aSubO = aGroupOp->CreateGroup(theObject, aGroupOp->GetType(anArgO));
1611               if (!CORBA::is_nil(aSubO))
1612                 aGroupOp->UnionIDs(aSubO, anIDs);
1613             }
1614             else if (anIDs->length() > 0) {
1615               // single sub-shape
1616               aSubO = aShapesOp->GetSubShape(theObject, anIDs[0]);
1617             }
1618           }
1619           break;
1620         case GEOM::FSM_GetSame:
1621           {
1622             // Use GetSame
1623             aSubO = aShapesOp->GetSame(theObject, anArgO);
1624           }
1625           break;
1626         case GEOM::FSM_GetShapesOnShape:
1627           {
1628             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
1629             aSubO = aShapesOp->GetShapesOnShapeAsCompound(anArgO, theObject,
1630               (short)GEOM::SOLID, GEOM::ST_ONIN);
1631           }
1632           break;
1633         case GEOM::FSM_GetInPlaceByHistory:
1634           {
1635             // Use GetInPlaceByHistory
1636             aSubO = aShapesOp->GetInPlaceByHistory(theObject, anArgO);
1637           }
1638           break;
1639         default:
1640           {}
1641         }
1642
1643         if (!CORBA::is_nil(aSubO)) {
1644           // add to parts list
1645           addToListOfGO( aSubO, aParts );
1646
1647           // Publish the sub-shape
1648           SALOMEDS::SObject_var aSubSO;
1649           if (!CORBA::is_nil(theSObject)) {
1650             TCollection_AsciiString aSubName;
1651             if (theAddPrefix) {
1652               aSubName = "from_";
1653             }
1654             aSubName += anArgName;
1655             aSubSO = aStudyBuilder->NewObject(theSObject);
1656             aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
1657             // Restore color
1658             aSubO->SetColor(anArgO->GetColor());
1659             // set the texture
1660             if (aSubO->GetShapeType() == GEOM::VERTEX) {
1661               aSubO->SetMarkerStd(anArgO->GetMarkerType(), anArgO->GetMarkerSize());
1662               if (anArgO->GetMarkerType() == GEOM::MT_USER)
1663                 aSubO->SetMarkerTexture(anArgO->GetMarkerTexture());
1664             }
1665           }
1666
1667           if (!CORBA::is_nil(anArgSO)) {
1668             // Restore published sub-shapes of the argument
1669             GEOM::ListOfGO_var aSubParts;
1670             if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
1671               // pass theObject, because only it has the history
1672               aSubParts = RestoreGivenSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1673                                                         theObject, anArgs, theFindMethod, theAddPrefix);
1674             else
1675               aSubParts = RestoreGivenSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1676                                                         aSubO, anArgs, theFindMethod, theAddPrefix);
1677             // add to parts list
1678             addToListOfGO( aSubParts, aParts );
1679           }
1680         }
1681         else { // GetInPlace failed, try to build from published parts
1682           if (!CORBA::is_nil(anArgSO)) {
1683             SALOMEDS::SObject_var aSubSO;
1684             if (!CORBA::is_nil(theSObject))
1685               aSubSO = aStudyBuilder->NewObject(theSObject);
1686
1687             // Restore published sub-shapes of the argument
1688             GEOM::ListOfGO_var aSubParts =
1689               RestoreGivenSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1690                                             theObject, anArgs, theFindMethod, theAddPrefix);
1691
1692             // add to parts list
1693             addToListOfGO( aSubParts, aParts );
1694
1695             if (aSubParts->length() > 0) {
1696               // try to build an argument from a set of its sub-shapes,
1697               // that published and will be reconstructed
1698               if (aSubParts->length() > 1) {
1699                 aSubO = aShapesOp->MakeCompound(aSubParts);
1700                 // add to parts list
1701                 addToListOfGO( aSubO, aParts );
1702               }
1703               else {
1704                 aSubO = aSubParts[0];
1705               }
1706               if (!CORBA::is_nil(aSubO) && !CORBA::is_nil(aSubSO)) {
1707                 // Publish the sub-shape
1708                 TCollection_AsciiString aSubName;
1709                 if (theAddPrefix) {
1710                   aSubName = "from_parts_of_";
1711                 }
1712                 aSubName += anArgName;
1713                 aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
1714                 // Restore color
1715                 aSubO->SetColor(anArgO->GetColor());
1716                 // set the texture
1717                 if (aSubO->GetShapeType() == GEOM::VERTEX) {
1718                   aSubO->SetMarkerStd(anArgO->GetMarkerType(), anArgO->GetMarkerSize());
1719                   if (anArgO->GetMarkerType() == GEOM::MT_USER)
1720                     aSubO->SetMarkerTexture(anArgO->GetMarkerTexture());
1721                 }
1722               }
1723             }
1724             else if (!CORBA::is_nil(aSubSO)) {
1725               // remove created aSubSO, because no parts have been found
1726               aStudyBuilder->RemoveObject(aSubSO);
1727             }
1728           }
1729         } // try to build from published parts
1730         anArgSO->Destroy();
1731       }
1732     } // process arguments
1733   }
1734   std::set<std::string> anObjEntryMap;
1735   GEOM::ListOfGO_var aResParts = new GEOM::ListOfGO;
1736   int nbRes = 0;
1737   int nb = aParts->length();
1738   aResParts->length(nb);
1739   if (nb > 0)
1740   {
1741     Handle(GEOM_Object) aMainObj = _impl->GetObject(theObject->GetStudyID(), theObject->GetEntry());
1742     Handle(GEOM_Function) aFunction = aMainObj->GetLastFunction();
1743     GEOM::TPythonDump pd (aFunction, true);
1744     pd <<"[";
1745     int i = 0, j = 0;
1746     for ( ; i < nb; i++ )
1747     {
1748       GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_duplicate( aParts[ i ] );
1749       if (CORBA::is_nil(anObj))
1750         continue;
1751       char* anEntry = anObj->GetEntry();
1752       if (anObjEntryMap.count(anEntry))
1753         continue; // already treated
1754       anObjEntryMap.insert(anEntry);
1755       aResParts[nbRes++] = anObj;
1756       // clear python dump of object
1757       Handle(GEOM_Object) aGeomObj = _impl->GetObject(anObj->GetStudyID(), anEntry);
1758       Handle(GEOM_Function) anObjFun = aGeomObj->GetLastFunction();
1759       if ( !anObjFun.IsNull() )
1760         anObjFun->SetDescription( "" );
1761       if ( j > 0 )
1762         pd << ", ";
1763       pd << aGeomObj;
1764       j++;
1765     }
1766     pd <<"]" << " = geompy.RestoreGivenSubShapes(" << aMainObj << ", " << "[";
1767     i = 0; nb = theArgs.length(); j = 0;
1768     for ( ; i < nb; i++ )
1769     {
1770       GEOM::GEOM_Object_var anObj = theArgs[ i ];
1771       if (CORBA::is_nil(anObj))
1772         continue;
1773       Handle(GEOM_Object) aGeomObj = _impl->GetObject(anObj->GetStudyID(), anObj->GetEntry());
1774       if ( j > 0 )
1775         pd << ", ";
1776       pd << aGeomObj;
1777       j++;
1778     }
1779     pd <<"]" << ", " <<"geompy.GEOM.";
1780     switch (theFindMethod) {
1781     case GEOM::FSM_GetInPlace:
1782       pd << "FSM_GetInPlace"; break;
1783     case GEOM::FSM_MultiTransformed:
1784       pd << "FSM_MultiTransformed"; break;
1785     case GEOM::FSM_Transformed:
1786       pd << "FSM_Transformed"; break;
1787     case GEOM::FSM_GetSame:
1788       pd << "FSM_GetSame"; break;
1789     case GEOM::FSM_GetShapesOnShape:
1790       pd << "FSM_GetShapesOnShape"; break;
1791     case GEOM::FSM_GetInPlaceByHistory:
1792     default:
1793       pd << "FSM_GetInPlaceByHistory"; break;
1794     }
1795     pd << ", " << theInheritFirstArg << ", " << theAddPrefix << ")";
1796   }
1797   aResParts->length(nbRes);
1798   return aResParts._retn();
1799 }
1800
1801 //============================================================================
1802 // function : RestoreGivenSubShapesOneLevel
1803 // purpose  : Private method
1804 //============================================================================
1805 GEOM::ListOfGO* GEOM_Gen_i::RestoreGivenSubShapesOneLevel (SALOMEDS::Study_ptr     theStudy,
1806                                                            SALOMEDS::SObject_ptr   theOldSO,
1807                                                            SALOMEDS::SObject_ptr   theNewSO,
1808                                                            GEOM::GEOM_Object_ptr   theNewO,
1809                                                            std::set<std::string>   theArgs,
1810                                                            GEOM::find_shape_method theFindMethod,
1811                                                            CORBA::Boolean          theAddPrefix)
1812 {
1813   int i = 0;
1814   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
1815   GEOM::ListOfGO_var aNewParts = new GEOM::ListOfGO;
1816   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theOldSO) ||
1817       CORBA::is_nil(theNewO) /*|| CORBA::is_nil(theNewSO)*/)
1818     return aParts._retn();
1819
1820   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
1821
1822   // Get interface, containing method, which we will use to reconstruct sub-shapes
1823   GEOM::GEOM_IShapesOperations_var  aShapesOp = GetIShapesOperations(theStudy->StudyId());
1824   GEOM::GEOM_IGroupOperations_var    aGroupOp = GetIGroupOperations(theStudy->StudyId());
1825   GEOM::GEOM_ITransformOperations_var aTrsfOp = GetITransformOperations(theStudy->StudyId());
1826
1827   // Reconstruct published sub-shapes
1828   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator(theOldSO);
1829
1830   int aLen = 0;
1831   for (it->Init(); it->More(); it->Next()) {
1832     aLen++;
1833   }
1834   aParts->length(aLen);
1835
1836   for (it->Init(); it->More(); it->Next()) {
1837     SALOMEDS::SObject_var anOldSubSO = it->Value();
1838
1839     TCollection_AsciiString anArgName = anOldSubSO->GetName();
1840
1841     SALOMEDS::GenericAttribute_var anAttr;
1842     if (anOldSubSO->FindAttribute(anAttr, "AttributeIOR")) {
1843       SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1844       GEOM::GEOM_Object_var anOldSubO =
1845         GEOM::GEOM_Object::_narrow(_orb->string_to_object(anAttrIOR->Value()));
1846
1847       bool okToContinue = false;
1848
1849       if (!CORBA::is_nil(anOldSubO)) {
1850         CORBA::String_var anEntry = anOldSubO->GetEntry();
1851         okToContinue = theArgs.count(anEntry.in());
1852       }
1853
1854       if (okToContinue) {
1855         // Find a sub-shape of theNewO in place of anOldSubO
1856         GEOM::GEOM_Object_var aNewSubO;
1857         switch (theFindMethod) {
1858         case GEOM::FSM_GetInPlace:
1859           {
1860             // Use GetInPlace
1861             aNewSubO = aShapesOp->GetInPlace(theNewO, anOldSubO);
1862           }
1863           break;
1864         case GEOM::FSM_MultiTransformed:
1865           {
1866             // Only for Multi-transformations
1867             GEOM::GEOM_Object_var anArgOTrsf = aTrsfOp->TransformLikeOtherCopy(anOldSubO, theNewO);
1868             if (!CORBA::is_nil(anArgOTrsf)) {
1869               CORBA::String_var anArgOTrsfEntry = anArgOTrsf->GetEntry();
1870               Handle(GEOM_Object) anArgOTrsfImpl = _impl->GetObject(anArgOTrsf->GetStudyID(), anArgOTrsfEntry);
1871               Handle(GEOM_Function) anArgOTrsfFun = anArgOTrsfImpl->GetLastFunction();
1872               anArgOTrsfFun->SetDescription("");
1873               aNewSubO = aShapesOp->GetInPlace(theNewO, anArgOTrsf);
1874             }
1875           }
1876           break;
1877         case GEOM::FSM_Transformed:
1878           {
1879             // transformation, cannot use GetInPlace, operate with indices
1880             GEOM::ListOfLong_var anIDs = anOldSubO->GetSubShapeIndices();
1881             if (anIDs->length() > 1) {
1882               // group
1883               aNewSubO = aGroupOp->CreateGroup(theNewO, aGroupOp->GetType(anOldSubO));
1884               if (!CORBA::is_nil(aNewSubO))
1885                 aGroupOp->UnionIDs(aNewSubO, anIDs);
1886             }
1887             else {
1888               // single sub-shape
1889               aNewSubO = aShapesOp->GetSubShape(theNewO, anIDs[0]);
1890             }
1891           }
1892           break;
1893         case GEOM::FSM_GetSame:
1894           {
1895             // Use GetSame
1896             aNewSubO = aShapesOp->GetSame(theNewO, anOldSubO);
1897           }
1898           break;
1899         case GEOM::FSM_GetShapesOnShape:
1900           {
1901             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
1902             aNewSubO = aShapesOp->GetShapesOnShapeAsCompound(anOldSubO, theNewO,
1903                                                              (short)GEOM::SOLID, GEOM::ST_ONIN);
1904           }
1905           break;
1906         case GEOM::FSM_GetInPlaceByHistory:
1907           {
1908             // Use GetInPlaceByHistory
1909             aNewSubO = aShapesOp->GetInPlaceByHistory(theNewO, anOldSubO);
1910           }
1911           break;
1912         default:
1913           {}
1914         }
1915
1916         if (!CORBA::is_nil(aNewSubO)) {
1917           // add the part to the list
1918           aParts[i] = aNewSubO;
1919           i++;
1920           // add to parts list
1921           addToListOfGO( aNewSubO, aNewParts );
1922
1923           SALOMEDS::SObject_var aNewSubSO;
1924           if (!CORBA::is_nil(theNewSO)) {
1925               // Publish the sub-shape
1926             TCollection_AsciiString aSubName;
1927             if (theAddPrefix) {
1928               aSubName = "from_";
1929             }
1930             aSubName += anArgName;
1931             aNewSubSO = aStudyBuilder->NewObject(theNewSO);
1932             aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
1933             // Restore color
1934             aNewSubO->SetColor(anOldSubO->GetColor());
1935             // set the texture
1936             if (aNewSubO->GetShapeType() == GEOM::VERTEX) {
1937               aNewSubO->SetMarkerStd(anOldSubO->GetMarkerType(), anOldSubO->GetMarkerSize());
1938               if (anOldSubO->GetMarkerType() == GEOM::MT_USER)
1939                 aNewSubO->SetMarkerTexture(anOldSubO->GetMarkerTexture());
1940             }
1941           }
1942           // Restore published sub-shapes of the argument
1943           GEOM::ListOfGO_var aSubParts;
1944           if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
1945             // pass the main shape as Object, because only it has the history
1946             aSubParts = RestoreGivenSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1947                                                       theNewO, theArgs, theFindMethod, theAddPrefix);
1948           else
1949             aSubParts = RestoreGivenSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1950                                                       aNewSubO, theArgs, theFindMethod, theAddPrefix);
1951           // add to parts list
1952           addToListOfGO( aSubParts, aNewParts );
1953         }
1954         else { // GetInPlace failed, try to build from published parts
1955           SALOMEDS::SObject_var aNewSubSO;
1956           if (!CORBA::is_nil(theNewSO))
1957             aNewSubSO = aStudyBuilder->NewObject(theNewSO);
1958
1959           // Restore published sub-shapes of the argument
1960           GEOM::ListOfGO_var aSubParts =
1961             RestoreGivenSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1962                                           theNewO, theArgs, theFindMethod, theAddPrefix);
1963           // add to parts list
1964           addToListOfGO( aSubParts, aNewParts );
1965
1966           if (aSubParts->length() > 0) {
1967             // try to build an object from a set of its sub-shapes,
1968             // that published and will be reconstructed
1969             if (aSubParts->length() > 1) {
1970               aNewSubO = aShapesOp->MakeCompound(aSubParts);
1971               // add to parts list
1972               addToListOfGO( aNewSubO, aNewParts );
1973             }
1974             else {
1975               aNewSubO = aSubParts[0];
1976             }
1977
1978             if (!CORBA::is_nil(aNewSubO)) {
1979               // add the part to the list
1980               aSubParts[i] = aNewSubO;
1981               i++;
1982
1983               // Publish the sub-shape
1984               if (!CORBA::is_nil(aNewSubSO)) {
1985                 TCollection_AsciiString aSubName;
1986                 if (theAddPrefix) {
1987                   aSubName = "from_parts_of_";
1988                 }
1989                 aSubName += anArgName;
1990                 aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
1991                 // Restore color
1992                 aNewSubO->SetColor(anOldSubO->GetColor());
1993                 // set the texture
1994                 if (aNewSubO->GetShapeType() == GEOM::VERTEX) {
1995                   aNewSubO->SetMarkerStd(anOldSubO->GetMarkerType(), anOldSubO->GetMarkerSize());
1996                   if (anOldSubO->GetMarkerType() == GEOM::MT_USER)
1997                     aNewSubO->SetMarkerTexture(anOldSubO->GetMarkerTexture());
1998                 }
1999               }
2000             }
2001           }
2002           else if (!CORBA::is_nil(aNewSubSO)) {
2003             // remove created aSubSO, because no parts have been found
2004             aStudyBuilder->RemoveObject(aNewSubSO);
2005           }
2006         } // try to build from published parts
2007       }
2008     }
2009   } // iterate on published sub-shapes
2010
2011   aParts->length(i);
2012   // add to parts list
2013   addToListOfGO( aNewParts, aParts );
2014   return aParts._retn();
2015 }
2016
2017 //============================================================================
2018 // function : register()
2019 // purpose  : register 'name' in 'name_service'
2020 //============================================================================
2021 void GEOM_Gen_i::register_name(char * name)
2022 {
2023   GEOM::GEOM_Gen_var g = _this();
2024   name_service->Register(g, name);
2025 }
2026
2027 //============================================================================
2028 // function : Undo
2029 // purpose  :
2030 //============================================================================
2031 void GEOM_Gen_i::Undo(CORBA::Long theStudyID)
2032 {
2033   _impl->Undo(theStudyID);
2034 }
2035
2036 //============================================================================
2037 // function : Redo
2038 // purpose  :
2039 //============================================================================
2040 void GEOM_Gen_i::Redo(CORBA::Long theStudyID)
2041 {
2042   _impl->Redo(theStudyID);
2043 }
2044
2045 //============================================================================
2046 // function : GetIBasicOperations
2047 // purpose  :
2048 //============================================================================
2049 GEOM::GEOM_IBasicOperations_ptr GEOM_Gen_i::GetIBasicOperations(CORBA::Long theStudyID)
2050      throw ( SALOME::SALOME_Exception )
2051 {
2052   Unexpect aCatch(SALOME_SalomeException);
2053   MESSAGE( "GEOM_Gen_i::GetIBasicOperations" );
2054
2055   GEOM::GEOM_Gen_ptr engine = _this();
2056
2057   //transfer reference on engine
2058   GEOM_IBasicOperations_i* aServant =
2059     new GEOM_IBasicOperations_i(_poa, engine, _impl->GetIBasicOperations(theStudyID));
2060
2061   PortableServer::ObjectId_var id = _poa->activate_object(aServant);
2062   // activate the CORBA servant
2063   GEOM::GEOM_IBasicOperations_var operations = aServant->_this();
2064   return operations._retn();
2065 }
2066
2067 //============================================================================
2068 // function : GetITransformOperations
2069 // purpose  :
2070 //============================================================================
2071 GEOM::GEOM_ITransformOperations_ptr GEOM_Gen_i::GetITransformOperations(CORBA::Long theStudyID)
2072      throw ( SALOME::SALOME_Exception )
2073 {
2074   Unexpect aCatch(SALOME_SalomeException);
2075   MESSAGE( "GEOM_Gen_i::GetITransformOperations" );
2076
2077   GEOM::GEOM_Gen_ptr engine = _this();
2078
2079   GEOM_ITransformOperations_i* aServant =
2080     new GEOM_ITransformOperations_i(_poa, engine, _impl->GetITransformOperations(theStudyID));
2081
2082   // activate the CORBA servant
2083   GEOM::GEOM_ITransformOperations_var operations = aServant->_this();
2084   return operations._retn();
2085 }
2086
2087 //============================================================================
2088 // function : GetI3DPrimOperations
2089 // purpose  :
2090 //============================================================================
2091 GEOM::GEOM_I3DPrimOperations_ptr GEOM_Gen_i::GetI3DPrimOperations(CORBA::Long theStudyID)
2092      throw ( SALOME::SALOME_Exception )
2093 {
2094   Unexpect aCatch(SALOME_SalomeException);
2095   MESSAGE( "GEOM_Gen_i::GetI3DPrimOperations" );
2096
2097   GEOM::GEOM_Gen_ptr engine = _this();
2098
2099   GEOM_I3DPrimOperations_i* aServant =
2100     new GEOM_I3DPrimOperations_i(_poa, engine, _impl->GetI3DPrimOperations(theStudyID));
2101   PortableServer::ObjectId_var id = _poa->activate_object(aServant);
2102
2103   // activate the CORBA servant
2104   GEOM::GEOM_I3DPrimOperations_var operations = aServant->_this();
2105   return operations._retn();
2106 }
2107
2108 //============================================================================
2109 // function : GetIShapesOperations
2110 // purpose  :
2111 //============================================================================
2112 GEOM::GEOM_IShapesOperations_ptr GEOM_Gen_i::GetIShapesOperations(CORBA::Long theStudyID)
2113      throw ( SALOME::SALOME_Exception )
2114 {
2115   Unexpect aCatch(SALOME_SalomeException);
2116   MESSAGE( "GEOM_Gen_i::GetIShapesOperations" );
2117
2118   GEOM::GEOM_Gen_ptr engine = _this();
2119
2120   GEOM_IShapesOperations_i* aServant =
2121     new GEOM_IShapesOperations_i(_poa, engine, _impl->GetIShapesOperations(theStudyID));
2122
2123   // activate the CORBA servant
2124   GEOM::GEOM_IShapesOperations_var operations = aServant->_this();
2125   return operations._retn();
2126 }
2127
2128 //============================================================================
2129 // function : GetIBlocksOperations
2130 // purpose  :
2131 //============================================================================
2132 GEOM::GEOM_IBlocksOperations_ptr GEOM_Gen_i::GetIBlocksOperations(CORBA::Long theStudyID)
2133      throw ( SALOME::SALOME_Exception )
2134 {
2135   Unexpect aCatch(SALOME_SalomeException);
2136   MESSAGE( "GEOM_Gen_i::GetIBlocksOperations" );
2137
2138   GEOM::GEOM_Gen_ptr engine = _this();
2139
2140   GEOM_IBlocksOperations_i* aServant =
2141     new GEOM_IBlocksOperations_i(_poa, engine, _impl->GetIBlocksOperations(theStudyID));
2142
2143   // activate the CORBA servant
2144   GEOM::GEOM_IBlocksOperations_var operations = aServant->_this();
2145   return operations._retn();
2146 }
2147
2148 //============================================================================
2149 // function : GetIBooleanOperations
2150 // purpose  :
2151 //============================================================================
2152 GEOM::GEOM_IBooleanOperations_ptr GEOM_Gen_i::GetIBooleanOperations(CORBA::Long theStudyID)
2153      throw ( SALOME::SALOME_Exception )
2154 {
2155   Unexpect aCatch(SALOME_SalomeException);
2156   MESSAGE( "GEOM_Gen_i::GetIBooleanOperations" );
2157
2158   GEOM::GEOM_Gen_ptr engine = _this();
2159
2160   GEOM_IBooleanOperations_i* aServant =
2161     new GEOM_IBooleanOperations_i(_poa, engine, _impl->GetIBooleanOperations(theStudyID));
2162
2163   // activate the CORBA servant
2164   GEOM::GEOM_IBooleanOperations_var operations = aServant->_this();
2165   return operations._retn();
2166 }
2167
2168 //============================================================================
2169 // function : GetICurvesOperations
2170 // purpose  :
2171 //============================================================================
2172 GEOM::GEOM_ICurvesOperations_ptr GEOM_Gen_i::GetICurvesOperations(CORBA::Long theStudyID)
2173      throw ( SALOME::SALOME_Exception )
2174 {
2175   Unexpect aCatch(SALOME_SalomeException);
2176   MESSAGE( "GEOM_Gen_i::GetICurvesOperations" );
2177
2178   GEOM::GEOM_Gen_ptr engine = _this();
2179
2180   GEOM_ICurvesOperations_i* aServant =
2181     new GEOM_ICurvesOperations_i(_poa, engine, _impl->GetICurvesOperations(theStudyID));
2182
2183   // activate the CORBA servant
2184   GEOM::GEOM_ICurvesOperations_var operations = aServant->_this();
2185   return operations._retn();
2186 }
2187
2188 //============================================================================
2189 // function : GetILocalOperations
2190 // purpose  :
2191 //============================================================================
2192 GEOM::GEOM_ILocalOperations_ptr GEOM_Gen_i::GetILocalOperations(CORBA::Long theStudyID)
2193      throw ( SALOME::SALOME_Exception )
2194 {
2195   Unexpect aCatch(SALOME_SalomeException);
2196   MESSAGE( "GEOM_Gen_i::GetILocalOperations" );
2197
2198   GEOM::GEOM_Gen_ptr engine = _this();
2199
2200   GEOM_ILocalOperations_i* aServant =
2201     new GEOM_ILocalOperations_i(_poa, engine, _impl->GetILocalOperations(theStudyID));
2202
2203   // activate the CORBA servant
2204   GEOM::GEOM_ILocalOperations_var operations = aServant->_this();
2205   return operations._retn();
2206 }
2207
2208 //============================================================================
2209 // function : GetIHealingOperations
2210 // purpose  :
2211 //============================================================================
2212 GEOM::GEOM_IHealingOperations_ptr GEOM_Gen_i::GetIHealingOperations(CORBA::Long theStudyID)
2213      throw ( SALOME::SALOME_Exception )
2214 {
2215   Unexpect aCatch(SALOME_SalomeException);
2216   MESSAGE( "GEOM_Gen_i::IHealingOperations" );
2217
2218   GEOM::GEOM_Gen_ptr engine = _this();
2219
2220   GEOM_IHealingOperations_i* aServant =
2221     new GEOM_IHealingOperations_i(_poa, engine, _impl->GetIHealingOperations(theStudyID));
2222
2223   // activate the CORBA servant
2224   GEOM::GEOM_IHealingOperations_var operations = aServant->_this();
2225   return operations._retn();
2226 }
2227
2228 //============================================================================
2229 // function : GetIInsertOperations
2230 // purpose  :
2231 //============================================================================
2232 GEOM::GEOM_IInsertOperations_ptr GEOM_Gen_i::GetIInsertOperations(CORBA::Long theStudyID)
2233      throw ( SALOME::SALOME_Exception )
2234 {
2235   Unexpect aCatch(SALOME_SalomeException);
2236   MESSAGE( "GEOM_Gen_i::GetIInsertOperations" );
2237
2238   GEOM::GEOM_Gen_ptr engine = _this();
2239
2240   GEOM_IInsertOperations_i* aServant =
2241     new GEOM_IInsertOperations_i(_poa, engine, _impl->GetIInsertOperations(theStudyID));
2242
2243   // activate the CORBA servant
2244   GEOM::GEOM_IInsertOperations_var operations = aServant->_this();
2245   return operations._retn();
2246 }
2247
2248 //============================================================================
2249 // function : GetIMeasureOperations
2250 // purpose  :
2251 //============================================================================
2252 GEOM::GEOM_IMeasureOperations_ptr GEOM_Gen_i::GetIMeasureOperations(CORBA::Long theStudyID)
2253      throw ( SALOME::SALOME_Exception )
2254 {
2255   Unexpect aCatch(SALOME_SalomeException);
2256   MESSAGE( "GEOM_Gen_i::GetIMeasureOperations" );
2257
2258   GEOM::GEOM_Gen_ptr engine = _this();
2259
2260   GEOM_IMeasureOperations_i* aServant =
2261     new GEOM_IMeasureOperations_i(_poa, engine, _impl->GetIMeasureOperations(theStudyID));
2262
2263   // activate the CORBA servant
2264   GEOM::GEOM_IMeasureOperations_var operations = aServant->_this();
2265   return operations._retn();
2266 }
2267
2268 //============================================================================
2269 // function : GetIGroupOperations
2270 // purpose  :
2271 //============================================================================
2272 GEOM::GEOM_IGroupOperations_ptr GEOM_Gen_i::GetIGroupOperations(CORBA::Long theStudyID)
2273      throw ( SALOME::SALOME_Exception )
2274 {
2275   Unexpect aCatch(SALOME_SalomeException);
2276   MESSAGE( "GEOM_Gen_i::GetIGroupOperations" );
2277
2278   GEOM::GEOM_Gen_ptr engine = _this();
2279
2280   GEOM_IGroupOperations_i* aServant =
2281     new GEOM_IGroupOperations_i(_poa, engine, _impl->GetIGroupOperations(theStudyID));
2282
2283   // activate the CORBA servant
2284   GEOM::GEOM_IGroupOperations_var operations = aServant->_this();
2285   return operations._retn();
2286 }
2287
2288 //============================================================================
2289 // function : GetIAdvancedOperations
2290 // purpose  :
2291 //============================================================================
2292 GEOM::GEOM_IAdvancedOperations_ptr GEOM_Gen_i::GetIAdvancedOperations(CORBA::Long theStudyID)
2293      throw ( SALOME::SALOME_Exception )
2294 {
2295   Unexpect aCatch(SALOME_SalomeException);
2296   MESSAGE( "GEOM_Gen_i::GetIAdvancedOperations" );
2297
2298   GEOM::GEOM_Gen_ptr engine = _this();
2299
2300   GEOM_IAdvancedOperations_i* aServant =
2301     new GEOM_IAdvancedOperations_i(_poa, engine, _impl->GetIAdvancedOperations(theStudyID));
2302
2303   // activate the CORBA servant
2304   GEOM::GEOM_IAdvancedOperations_var operations = aServant->_this();
2305   return operations._retn();
2306 }
2307
2308 //=============================================================================
2309 /*!
2310  *  AddSubShape
2311  */
2312 //=============================================================================
2313 GEOM::GEOM_Object_ptr GEOM_Gen_i::AddSubShape (GEOM::GEOM_Object_ptr theMainShape,
2314                                                const GEOM::ListOfLong& theIndices)
2315 {
2316   if (CORBA::is_nil(theMainShape) || theIndices.length() < 1)
2317     return GEOM::GEOM_Object::_nil();
2318   CORBA::String_var entry = theMainShape->GetEntry();
2319   Handle(GEOM_Object) aMainsShape = _impl->GetObject(theMainShape->GetStudyID(), entry);
2320   if (aMainsShape.IsNull()) return GEOM::GEOM_Object::_nil();
2321
2322   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1, theIndices.length());
2323   for(Standard_Integer i = 0; i<theIndices.length(); i++) anArray->SetValue(i+1, theIndices[i]);
2324
2325   Handle(GEOM_Object) anObject = _impl->AddSubShape(aMainsShape, anArray, true);
2326   if(anObject.IsNull()) return GEOM::GEOM_Object::_nil();
2327
2328   TCollection_AsciiString anEntry;
2329   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
2330   return GetObject(anObject->GetDocID(), anEntry.ToCString());
2331 }
2332
2333 //=============================================================================
2334 /*!
2335  *  RemoveObject
2336  */
2337 //=============================================================================
2338 void GEOM_Gen_i::RemoveObject(GEOM::GEOM_Object_ptr theObject)
2339 {
2340   CORBA::String_var anEntry = theObject->GetEntry();
2341   Handle(GEOM_Object) anObject = _impl->GetObject(theObject->GetStudyID(), anEntry, false);
2342   if (anObject.IsNull()) return;
2343   _impl->RemoveObject(anObject);
2344   return;
2345 }
2346
2347
2348 //=================================================================================
2349 // function : GetStringFromIOR()
2350 // purpose  : returns a string that represents  a 'GEOM::GEOM_Object_var'
2351 //=================================================================================
2352 char* GEOM_Gen_i::GetStringFromIOR(GEOM::GEOM_Object_ptr theObject) {
2353   return _orb->object_to_string(theObject);
2354 }
2355
2356
2357 //=================================================================================
2358 // function : GetIORFromString()
2359 // purpose  : returns a 'GEOM::GEOM_Object_var' from a string representing it
2360 //=================================================================================
2361 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetIORFromString(const char* stringIOR) {
2362   GEOM::GEOM_Object_var aGeomObject;
2363   if(strcmp(stringIOR,"") != 0){
2364     CORBA::Object_var anObject = _orb->string_to_object(stringIOR);
2365     if(!CORBA::is_nil(anObject))
2366       aGeomObject =  GEOM::GEOM_Object::_narrow(anObject.in());
2367   }
2368   return aGeomObject._retn();
2369 }
2370
2371 //=================================================================================
2372 // function : GetObject()
2373 // purpose  :
2374 //=================================================================================
2375 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetObject (CORBA::Long theStudyID, const char* theEntry)
2376 {
2377   GEOM::GEOM_Object_var obj;
2378   Handle(GEOM_Object) handle_object = _impl->GetObject(theStudyID, (char*)theEntry);
2379   if (handle_object.IsNull()) return obj._retn();
2380
2381   TCollection_AsciiString stringIOR = handle_object->GetIOR();
2382   if (stringIOR.Length() > 1) {
2383     CORBA::Object_var corba_object = _orb->string_to_object(stringIOR.ToCString());
2384     if (!CORBA::is_nil(corba_object)) obj = GEOM::GEOM_Object::_narrow(corba_object);
2385     return obj._retn();
2386   }
2387
2388   GEOM::GEOM_Gen_ptr engine = _this();
2389   //transfer the reference to GEOM_Object_i
2390   GEOM_Object_i* servant = new GEOM_Object_i (_poa, engine, handle_object);
2391   PortableServer::ObjectId_var id = _poa->activate_object(servant);
2392
2393   obj = servant->_this();
2394   CORBA::String_var objStr = _orb->object_to_string(obj);
2395   TCollection_AsciiString anAscii( (char *)objStr.in() );
2396   handle_object->SetIOR( anAscii );
2397   return obj._retn();
2398 }
2399
2400 //=================================================================================
2401 // function : hasObjectInfo()
2402 // purpose  : shows if module provides information for its objects
2403 //=================================================================================
2404 bool GEOM_Gen_i::hasObjectInfo()
2405 {
2406   return true;
2407 }
2408
2409 //=================================================================================
2410 // function : getObjectInfo()
2411 // purpose  : returns an information for a given object by its entry
2412 //=================================================================================
2413 char* GEOM_Gen_i::getObjectInfo(CORBA::Long studyId, const char* entry)
2414 {
2415   GEOM::GEOM_Object_var aGeomObject;
2416
2417   CORBA::Object_var aSMObject = name_service->Resolve( "/myStudyManager" );
2418   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow( aSMObject );
2419   SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID( studyId );
2420   SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( entry );
2421   SALOMEDS::SObject_var aResultSObj;
2422   if (aSObj->ReferencedObject(aResultSObj))
2423     aSObj = aResultSObj;
2424
2425   SALOMEDS::GenericAttribute_var anAttr;
2426   if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
2427     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
2428     CORBA::String_var aVal = anIOR->Value();
2429     anIOR->Destroy();
2430     CORBA::Object_var anObject = aStudy->ConvertIORToObject(aVal);
2431     aGeomObject = GEOM::GEOM_Object::_narrow(anObject);
2432   }
2433   if (!aSObj->_is_nil() )
2434     aSObj->Destroy();
2435
2436   const char* aTypeInfo = "Object";
2437   if ( !aGeomObject->_is_nil() ) {
2438     GEOM::GEOM_IKindOfShape::shape_kind aKind;
2439     GEOM::ListOfLong_var anInts;
2440     GEOM::ListOfDouble_var aDbls;
2441
2442     GEOM::GEOM_IMeasureOperations_var anOp = GetIMeasureOperations( studyId );
2443     aKind = anOp->KindOfShape( aGeomObject, anInts, aDbls );
2444
2445     if ( anOp->IsDone() ) {
2446       switch ( aKind ) {
2447       case GEOM::GEOM_IKindOfShape::COMPOUND:
2448         aTypeInfo = "Compound";
2449         break;
2450       case GEOM::GEOM_IKindOfShape::COMPSOLID:
2451         aTypeInfo = "CompSolid";
2452         break;
2453       case GEOM::GEOM_IKindOfShape::SHELL:
2454         aTypeInfo = "Shell";
2455         break;
2456       case GEOM::GEOM_IKindOfShape::WIRE:
2457         if ( anInts[0] == 1 )
2458           aTypeInfo = "Closed Wire";
2459         else if ( anInts[0] == 2 )
2460           aTypeInfo = "Opened Wire";
2461         else
2462           aTypeInfo = "Wire";
2463         break;
2464         // SOLIDs
2465       case GEOM::GEOM_IKindOfShape::SPHERE:
2466         aTypeInfo = "Sphere";
2467         break;
2468       case GEOM::GEOM_IKindOfShape::CYLINDER:
2469         aTypeInfo = "Cylinder";
2470         break;
2471       case GEOM::GEOM_IKindOfShape::BOX:
2472       case GEOM::GEOM_IKindOfShape::ROTATED_BOX:
2473         aTypeInfo = "Box";
2474         break;
2475       case GEOM::GEOM_IKindOfShape::TORUS:
2476         aTypeInfo = "Torus";
2477         break;
2478       case GEOM::GEOM_IKindOfShape::CONE:
2479         aTypeInfo = "Cone";
2480         break;
2481       case GEOM::GEOM_IKindOfShape::POLYHEDRON:
2482         aTypeInfo = "Polyhedron";
2483         break;
2484       case GEOM::GEOM_IKindOfShape::SOLID:
2485         aTypeInfo = "Solid";
2486         break;
2487         // FACEs
2488       case GEOM::GEOM_IKindOfShape::SPHERE2D:
2489         aTypeInfo = "Spherical Face";
2490         break;
2491       case GEOM::GEOM_IKindOfShape::CYLINDER2D:
2492         aTypeInfo = "Cylindrical Face";
2493         break;
2494       case GEOM::GEOM_IKindOfShape::TORUS2D:
2495         aTypeInfo = "Toroidal Face";
2496         break;
2497       case GEOM::GEOM_IKindOfShape::CONE2D:
2498         aTypeInfo = "Conical Face";
2499         break;
2500       case GEOM::GEOM_IKindOfShape::DISK_CIRCLE:
2501         aTypeInfo = "Disk";
2502         break;
2503       case GEOM::GEOM_IKindOfShape::DISK_ELLIPSE:
2504         aTypeInfo = "Elliptical Face";
2505         break;
2506       case GEOM::GEOM_IKindOfShape::POLYGON:
2507         aTypeInfo = "Polygon";
2508         break;
2509       case GEOM::GEOM_IKindOfShape::PLANE:
2510         aTypeInfo = "Plane";
2511         break;
2512       case GEOM::GEOM_IKindOfShape::PLANAR:
2513         aTypeInfo = "Planar Face";
2514         break;
2515       case GEOM::GEOM_IKindOfShape::FACE:
2516         aTypeInfo = "Face";
2517         break;
2518         // EDGEs
2519       case GEOM::GEOM_IKindOfShape::CIRCLE:
2520         aTypeInfo = "Circle";
2521         break;
2522       case GEOM::GEOM_IKindOfShape::ARC_CIRCLE:
2523         aTypeInfo = "Arc Circle";
2524         break;
2525       case GEOM::GEOM_IKindOfShape::ELLIPSE:
2526         aTypeInfo = "Ellipse";
2527         break;
2528       case GEOM::GEOM_IKindOfShape::ARC_ELLIPSE:
2529         aTypeInfo = "Arc Ellipse";
2530         break;
2531       case GEOM::GEOM_IKindOfShape::LINE:
2532         aTypeInfo = "Line";
2533         break;
2534       case GEOM::GEOM_IKindOfShape::SEGMENT:
2535         aTypeInfo = "Segment";
2536         break;
2537       case GEOM::GEOM_IKindOfShape::EDGE:
2538         aTypeInfo = "Edge";
2539         break;
2540       case GEOM::GEOM_IKindOfShape::VERTEX:
2541         aTypeInfo = "Vertex";
2542         break;
2543       default:
2544         break;
2545       }
2546     }
2547   }
2548
2549   char* anInfo = new char[strlen("Module ") + strlen(ComponentDataType()) + strlen(", ") + strlen(aTypeInfo) + 3];
2550   sprintf(anInfo, "Module %s, %s", ComponentDataType(), aTypeInfo);
2551
2552   char* ret = CORBA::string_dup(anInfo);
2553   delete [] anInfo;
2554   return ret;
2555 }
2556
2557 //=====================================================================================
2558 // EXPORTED METHODS
2559 //=====================================================================================
2560 extern "C"
2561 {
2562   /*
2563   GEOM_I_EXPORT
2564   PortableServer::ObjectId* GEOMEngine_factory(CORBA::ORB*, PortableServer::POA*, PortableServer::ObjectId*, const char*, const char*);
2565   */
2566
2567   GEOM_I_EXPORT
2568   PortableServer::ObjectId* GEOMEngine_factory(CORBA::ORB_ptr            orb,
2569                                                PortableServer::POA_ptr   poa,
2570                                                PortableServer::ObjectId* contId,
2571                                                const char*               instanceName,
2572                                                const char*               interfaceName)
2573   {
2574     GEOM_Gen_i* myGEOM_Gen_i = new GEOM_Gen_i(orb, poa, contId, instanceName, interfaceName);
2575     return myGEOM_Gen_i->getId();
2576   }
2577 }