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