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