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