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