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