]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_Gen_i.cc
Salome HOME
IMP 0020670: Be able to keep the same names for the 'Set ... subshapes arguments...
[modules/geom.git] / src / GEOM_I / GEOM_Gen_i.cc
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 #ifdef WNT
23 #pragma warning( disable:4786 )
24 #endif
25
26 #include <Standard_Stream.hxx>
27
28 #include "GEOM_Gen_i.hh"
29 #include "GEOM_Object_i.hh"
30
31 #include <set>
32 #include <strstream>
33 //#include <sstream>
34
35 #include "Utils_CorbaException.hxx"
36 #include "OpUtil.hxx"
37 #include "Utils_ExceptHandlers.hxx"
38 #include "utilities.h"
39
40 #include "GEOM_Object_i.hh"
41 #include "GEOM_Object.hxx"
42 #include "GEOM_Function.hxx"
43 #include "GEOM_ISubShape.hxx"
44 #include <GEOM_PythonDump.hxx>
45 #include "GEOMImpl_Types.hxx"
46 #include "GEOMImpl_CopyDriver.hxx"
47
48 // Cascade headers
49 #include <BRep_Builder.hxx>
50 #include <BRepTools.hxx>
51 #include <TDF_Label.hxx>
52 #include <TDF_Tool.hxx>
53 #include <TDF_ChildIDIterator.hxx>
54 #include <TNaming_NamedShape.hxx>
55 #include <TDataStd_Name.hxx>
56 #include <TCollection_AsciiString.hxx>
57 #include <TColStd_HArray1OfInteger.hxx>
58 #include <TopAbs_ShapeEnum.hxx>
59 //#include <TopTools_IndexedMapOfShape.hxx>
60 #include <TopExp.hxx>
61 #include <OSD.hxx>
62
63 #include "SALOMEDS_Tool.hxx"
64
65 //============================================================================
66 // function : GEOM_Gen_i()
67 // purpose  : constructor to be called for servant creation.
68 //============================================================================
69 GEOM_Gen_i::GEOM_Gen_i(CORBA::ORB_ptr            orb,
70                        PortableServer::POA_ptr   poa,
71                        PortableServer::ObjectId* contId,
72                        const char*               instanceName,
73                        const char*               interfaceName) :
74   Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
75 {
76   _thisObj = this;
77   _id = _poa->activate_object(_thisObj);
78   name_service = new SALOME_NamingService(_orb);
79
80   _impl = new ::GEOMImpl_Gen;
81
82   //PAL10867: disable signals catching with "noexcepthandler" option
83   char* envNoCatchSignals = getenv("NOT_INTERCEPT_SIGNALS");
84   if (!envNoCatchSignals || !atoi(envNoCatchSignals))
85   {
86     //work around PAL12004, PAL12628
87     //OSD::SetSignal( true );
88     bool raiseFPE;
89 #ifdef _DEBUG_
90     raiseFPE = true;
91     char* envDisableFPE = getenv("DISABLE_FPE");
92     if (envDisableFPE && atoi(envDisableFPE))
93       raiseFPE = false;
94 #else
95     raiseFPE = false;
96 #endif
97     OSD::SetSignal( raiseFPE );
98   }
99 }
100
101 //============================================================================
102 // function : ~GEOM_Gen_i()
103 // purpose  : destructor
104 //============================================================================
105 GEOM_Gen_i::~GEOM_Gen_i() {
106   delete name_service;
107   delete _impl;
108 }
109
110
111 //============================================================================
112 // function : IORToLocalPersistentID()
113 // purpose  :
114 //============================================================================
115 char* GEOM_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
116                                          const char* IORString,
117                                          CORBA::Boolean isMultiFile,
118                                          CORBA::Boolean isASCII)
119 {
120   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(_orb->string_to_object(IORString));
121   if (!CORBA::is_nil(anObject)) {
122     return CORBA::string_dup(anObject->GetEntry());
123   }
124   return 0;
125 }
126
127
128 //============================================================================
129 // function : LocalPersistentIDToIOR()
130 // purpose  : Create/Load CORBA object from a persistent ref (an entry)
131 //          : Used when a study is loaded
132 //          : The IOR (IORName) of object created is returned
133 //============================================================================
134 char* GEOM_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
135                                          const char* aLocalPersistentID,
136                                          CORBA::Boolean isMultiFile,
137                                          CORBA::Boolean isASCII)
138 {
139   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
140
141   Handle(GEOM_Object) anObject = _impl->GetObject(aStudy->StudyId(), const_cast<char*>(aLocalPersistentID));
142   TCollection_AsciiString anEntry;
143   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
144   GEOM::GEOM_Object_var obj = GetObject(anObject->GetDocID(), anEntry.ToCString());
145
146   CORBA::String_var aPersRefString = _orb->object_to_string(obj);
147   return CORBA::string_dup(aPersRefString);
148 }
149
150 //============================================================================
151 // function : CanPublishInStudy
152 // purpose  :
153 //============================================================================
154 bool GEOM_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
155 {
156   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(theIOR);
157   return !(anObject->_is_nil());
158 }
159
160
161 //============================================================================
162 // function : PublishInStudy
163 // purpose  :
164 //============================================================================
165 SALOMEDS::SObject_ptr GEOM_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
166                                                  SALOMEDS::SObject_ptr theSObject,
167                                                  CORBA::Object_ptr theObject,
168                                                  const char* theName) throw (SALOME::SALOME_Exception)
169 {
170   Unexpect aCatch(SALOME_SalomeException);
171   SALOMEDS::SObject_var aResultSO;
172   if(CORBA::is_nil(theObject) || theStudy->_is_nil()) return aResultSO;
173   GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow(theObject);
174   if(aShape->_is_nil()) return aResultSO;
175
176   SALOMEDS::GenericAttribute_var anAttr;
177   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder();
178
179   SALOMEDS::SComponent_var       aFather = theStudy->FindComponent("GEOM");
180   if (aFather->_is_nil()) {
181     aFather = aStudyBuilder->NewComponent("GEOM");
182     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributeName");
183     SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
184     aName->SetValue("Geometry");
185     aName->Destroy();
186     anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
187     SALOMEDS::AttributePixMap_var aPixMap=SALOMEDS::AttributePixMap::_narrow(anAttr);
188     aPixMap->SetPixMap("ICON_OBJBROWSER_Geometry");
189     aPixMap->Destroy();
190     aStudyBuilder->DefineComponentInstance(aFather, (GEOM::GEOM_Gen_var)GEOM_Gen::_this());
191   }
192   if (aFather->_is_nil()) return aResultSO;
193
194   if (CORBA::is_nil(theSObject)) {
195     aResultSO = aStudyBuilder->NewObject(aFather);
196   } else {
197     if (!theSObject->ReferencedObject(aResultSO))
198       aResultSO = SALOMEDS::SObject::_duplicate(theSObject); //SRN: Added Aug 24,2004 : for  the method AddInStudy with theFather argumenet != NULL
199       //THROW_SALOME_CORBA_EXCEPTION("Publish in study supervision graph error",SALOME::BAD_PARAM);
200   }
201   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
202   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
203   CORBA::String_var aGeomObjIOR = _orb->object_to_string(theObject);
204   anIOR->SetValue(aGeomObjIOR);
205   anIOR->Destroy();
206
207   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributePixMap");
208   SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
209   TCollection_AsciiString aShapeName("Shape_");
210
211   if ( aShape->GetType() == GEOM_GROUP ) {
212     GEOM::GEOM_IGroupOperations_var anOp = GetIGroupOperations( theStudy->StudyId() );
213     switch ( (TopAbs_ShapeEnum)anOp->GetType( aShape ) ) {
214     case TopAbs_VERTEX:
215       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_PNT" );
216       aShapeName = "Group_Of_Vertices_";
217       break;
218     case TopAbs_EDGE:
219       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_EDGE" );
220       aShapeName = "Group_Of_Edges_";
221       break;
222     case TopAbs_FACE:
223       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_FACE" );
224       aShapeName = "Group_Of_Faces_";
225       break;
226     case TopAbs_SOLID:
227       aPixmap->SetPixMap( "ICON_OBJBROWSER_GROUP_SOLID" );
228       aShapeName = "Group_Of_Solids_";
229       break;
230     }
231   } else if ( aShape->GetType() == GEOM_MARKER ) {
232     aPixmap->SetPixMap( "ICON_OBJBROWSER_LCS" );
233     aShapeName = "LocalCS_";
234   } else if ( aShape->GetShapeType() == GEOM::COMPOUND ) {
235     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPOUND" );
236     aShapeName = "Compound_";
237   } else if ( aShape->GetShapeType() == GEOM::COMPSOLID ) {
238     aPixmap->SetPixMap( "ICON_OBJBROWSER_COMPSOLID" );
239     aShapeName = "Compsolid_";
240   } else if ( aShape->GetShapeType() == GEOM::SOLID ) {
241     aPixmap->SetPixMap( "ICON_OBJBROWSER_SOLID" );
242     aShapeName = "Solid_";
243   } else if ( aShape->GetShapeType() == GEOM::SHELL ) {
244     aPixmap->SetPixMap( "ICON_OBJBROWSER_SHELL" );
245     aShapeName = "Shell_";
246   } else if ( aShape->GetShapeType() == GEOM::FACE ) {
247     aPixmap->SetPixMap( "ICON_OBJBROWSER_FACE" );
248     aShapeName = "Face_";
249   } else if ( aShape->GetShapeType() == GEOM::WIRE ) {
250     aPixmap->SetPixMap( "ICON_OBJBROWSER_WIRE" );
251     aShapeName = "Wire_";
252   } else if ( aShape->GetShapeType() == GEOM::EDGE ) {
253     aPixmap->SetPixMap( "ICON_OBJBROWSER_EDGE" );
254     aShapeName = "Edge_";
255   } else if ( aShape->GetShapeType() == GEOM::VERTEX ) {
256     aPixmap->SetPixMap( "ICON_OBJBROWSER_VERTEX" );
257     aShapeName = "Vertex_";
258   }
259   aPixmap->Destroy();
260   //if (strlen(theName) == 0) aShapeName += TCollection_AsciiString(aResultSO->Tag());
261   //else aShapeName = TCollection_AsciiString(CORBA::string_dup(theName));
262   
263   // try to find existed name for current shape
264   bool HasName = false;
265   // recieve current TopoDS shape
266   CORBA::String_var entry = aShape->GetEntry();
267   Handle(GEOM_Object) aGShape = _impl->GetObject(aShape->GetStudyID(), entry);
268   TopoDS_Shape TopoSh = aGShape->GetValue();
269   // find label of main shape
270   GEOM::GEOM_Object_var aMainSh = aShape;
271   while( !aMainSh->IsMainShape() ) {
272     aMainSh = aMainSh->GetMainShape();
273   }
274   entry = aMainSh->GetEntry();
275   Handle(GEOM_Object) anObj = _impl->GetObject(aMainSh->GetStudyID(), entry);
276   TDF_Label aMainLbl = anObj->GetEntry();
277   // check all named shapes using iterator
278   TDF_ChildIDIterator anIt(aMainLbl, TNaming_NamedShape::GetID(), Standard_True);
279   for(; anIt.More(); anIt.Next()) {
280     Handle(TNaming_NamedShape) anAttr =
281       Handle(TNaming_NamedShape)::DownCast(anIt.Value());
282     if(anAttr.IsNull()) continue;
283     TopoDS_Shape S = anAttr->Get();
284     if( !S.IsEqual(TopoSh) ) continue;
285     TDF_Label L = anAttr->Label();
286     Handle(TDataStd_Name) aName;
287     if(L.FindAttribute(TDataStd_Name::GetID(),aName)) {
288       aShapeName = aName->Get();
289       HasName = true;
290     }
291   }
292
293   if(!HasName) {
294     // asv : 11.11.04 Introducing a more sofisticated method of name creation, just as
295     //       it is done in GUI in GEOMBase::GetDefaultName() - not just add a Tag() == number
296     //       of objects in the study, but compute a number of objects with the same prefix
297     //       and build a new name as Prefix_N+1
298     if ( strlen( theName ) == 0 ) { // MOST PROBABLY CALLED FROM BATCHMODE OR SUPERVISOR
299       int i = 0;                    // (WITH EMPTY NEW NAME)
300       SALOMEDS::SObject_var obj;
301       TCollection_AsciiString aNewShapeName;
302       do {
303         aNewShapeName = aShapeName + TCollection_AsciiString(++i);
304         obj = theStudy->FindObject( aNewShapeName.ToCString() );
305       }
306       while ( !obj->_is_nil() );
307       aShapeName = aNewShapeName;
308     }
309     else // MOST PROBABLY CALLED FROM GEOM GUI (ALREADY WITH VALID NAME)
310       aShapeName = TCollection_AsciiString((char*)theName);
311   }
312
313   //Set the study entry as a name of  the published GEOM_Object
314   CORBA::String_var anID =aResultSO->GetID();
315   aShape->SetStudyEntry(anID.in());
316
317   //Set a name of the added shape
318   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeName");
319   SALOMEDS::AttributeName_var aNameAttrib = SALOMEDS::AttributeName::_narrow(anAttr);
320   aNameAttrib->SetValue(aShapeName.ToCString());
321   aNameAttrib->Destroy();
322
323   //Set NoteBook variables used in the object creation
324   TCollection_AsciiString aVars;
325   CORBA::String_var aString=aShape->GetParameters();
326   SALOMEDS::ListOfListOfStrings_var aSections = theStudy->ParseVariables(aString);
327   for(int i = 0, n = aSections->length(); i < n; i++) {
328     SALOMEDS::ListOfStrings aListOfVars = aSections[i];
329     for(int j = 0, m = aListOfVars.length(); j < m; j++) {
330       if(theStudy->IsVariable(aListOfVars[j].in()))
331         aVars += TCollection_AsciiString(aListOfVars[j].in());
332       if(j != m-1)
333         aVars += ":";
334     }
335     if(i != n-1)
336       aVars += "|";
337   }
338   anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeString");
339   SALOMEDS::AttributeString_var aStringAttrib = SALOMEDS::AttributeString::_narrow(anAttr);
340   aStringAttrib->SetValue(aVars.ToCString());
341   aStringAttrib->Destroy();
342
343   aFather->Destroy();
344
345   //Set a name of the GEOM object
346   aShape->SetName(theName);
347
348   return aResultSO._retn();
349 }
350
351
352 //============================================================================
353 // function : CreateAndPublishGroup
354 // purpose  : auxilary for PublishNamedShapesInStudy
355 //============================================================================
356 void GEOM_Gen_i::CreateAndPublishGroup(SALOMEDS::Study_ptr theStudy,
357                                        GEOM::GEOM_Object_var theMainShape,
358                                        const TopTools_IndexedMapOfShape& anIndices,
359                                        const TopTools_SequenceOfShape& SeqS,
360                                        const TColStd_SequenceOfAsciiString& SeqN,
361                                        const Standard_CString& GrName,
362                                        GEOM::ListOfGO_var aResList)
363 {
364   CORBA::String_var entry = theMainShape->GetEntry();
365   Handle(GEOM_Object) aMainShape = _impl->GetObject(theMainShape->GetStudyID(), entry);
366   Handle(TColStd_HArray1OfInteger) anArray;
367   if(SeqS.Length()>0) {
368     // create a group
369     GEOM::GEOM_IGroupOperations_var GOp = GetIGroupOperations(theStudy->StudyId());
370     GEOM::GEOM_Object_ptr GrObj =
371       GOp->CreateGroup( theMainShape, SeqS.Value(1).ShapeType() );
372     AddInStudy(theStudy, GrObj, GrName, theMainShape._retn());
373     CORBA::String_var GrEntry = GrObj->GetEntry();
374     Handle(GEOM_Object) HGrObj = _impl->GetObject(GrObj->GetStudyID(), GrEntry);
375     // add named objects
376     //Handle(GEOM_Object) anObj;
377     for(int i=1; i<=SeqS.Length(); i++) {
378       TopoDS_Shape aValue = SeqS.Value(i);
379       //anArray = new TColStd_HArray1OfInteger(1,1);
380       Standard_Integer anIndex = anIndices.FindIndex(aValue);
381       //anArray->SetValue(1, anIndex);
382       GOp->AddObject(GrObj,anIndex);
383       //anObj = GEOM_Engine::GetEngine()->AddObject(aMainShape->GetDocID(), GEOM_SUBSHAPE);
384       //if (anObj.IsNull()) continue;
385       //Handle(GEOM_Function) aFunction = anObj->AddFunction(GEOM_Object::GetSubShapeID(), 1);
386       //if (aFunction.IsNull()) continue;
387       //GEOM_ISubShape aSSI(aFunction);
388       //aSSI.SetMainShape(aMainShape->GetLastFunction());
389       //aSSI.SetIndices(anArray);
390       //aFunction->SetValue(aValue);
391       //GOp->UnionIDs(GrObj, anIndex);
392       //SALOMEDS::SObject_var aResultSO;
393       //TCollection_AsciiString anEntry;
394       //TDF_Tool::Entry(anObj->GetEntry(),anEntry);
395       //GEOM::GEOM_Object_var aGObj = GetObject(anObj->GetDocID(), anEntry.ToCString());
396       //AddInStudy(theStudy, aGObj._retn(), SeqN.Value(i).ToCString(), GrObj);
397     }
398   }
399 }
400
401
402 //============================================================================
403 // function : PublishNamedShapesInStudy
404 // purpose  :
405 //============================================================================
406 GEOM::ListOfGO* GEOM_Gen_i::
407             PublishNamedShapesInStudy(SALOMEDS::Study_ptr theStudy,
408                                       //SALOMEDS::SObject_ptr theSObject,
409                                       CORBA::Object_ptr theObject)
410 {
411   //Unexpect aCatch(SALOME_SalomeException);
412   GEOM::ListOfGO_var aResList = new GEOM::ListOfGO;
413
414   //CORBA::Object_var theObject = theSObject->GetObject();
415   GEOM::GEOM_Object_var theMainShape = GEOM::GEOM_Object::_narrow(theObject);
416   if(theMainShape->_is_nil()) return aResList._retn();
417
418   CORBA::String_var entry = theMainShape->GetEntry();
419   Handle(GEOM_Object) aMainShape = _impl->GetObject(theMainShape->GetStudyID(), entry);
420   if (aMainShape.IsNull()) return aResList._retn();
421   TopoDS_Shape MainSh = aMainShape->GetValue();
422
423   TDF_Label aMainLbl = aMainShape->GetEntry();
424   TopTools_SequenceOfShape SolidSeqS, FaceSeqS, EdgeSeqS, VertSeqS;
425   TColStd_SequenceOfAsciiString SolidSeqN, FaceSeqN, EdgeSeqN, VertSeqN;
426   TDF_ChildIDIterator anIt(aMainLbl, TNaming_NamedShape::GetID(), Standard_True);
427   for(; anIt.More(); anIt.Next()) {
428     Handle(TNaming_NamedShape) anAttr =
429       Handle(TNaming_NamedShape)::DownCast(anIt.Value());
430     if(anAttr.IsNull()) continue;
431     TopoDS_Shape S = anAttr->Get();
432     TDF_Label L = anAttr->Label();
433     //if(S.IsEqual(MainSh)) continue;
434     Handle(TDataStd_Name) aName;
435     if(L.FindAttribute(TDataStd_Name::GetID(),aName)) {
436       TCollection_ExtendedString EName = aName->Get();
437       if(S.ShapeType()==TopAbs_SOLID) {
438         SolidSeqS.Append(S);
439         SolidSeqN.Append(aName->Get());
440       }
441       else if(S.ShapeType()==TopAbs_FACE) {
442         FaceSeqS.Append(S);
443         FaceSeqN.Append(aName->Get());
444       }
445       else if(S.ShapeType()==TopAbs_EDGE) {
446         EdgeSeqS.Append(S);
447         EdgeSeqN.Append(aName->Get());
448       }
449       else if(S.ShapeType()==TopAbs_VERTEX) {
450         VertSeqS.Append(S);
451         VertSeqN.Append(aName->Get());
452       }
453     }
454   }
455
456   TopTools_IndexedMapOfShape anIndices;
457   TopExp::MapShapes(MainSh, anIndices);
458
459   CreateAndPublishGroup(theStudy, theMainShape, anIndices, SolidSeqS, SolidSeqN,
460                         "Group_Of_Named_Solids", aResList);
461
462   CreateAndPublishGroup(theStudy, theMainShape, anIndices, FaceSeqS, FaceSeqN,
463                         "Group_Of_Named_Faces", aResList);
464
465   CreateAndPublishGroup(theStudy, theMainShape, anIndices, EdgeSeqS, EdgeSeqN,
466                         "Group_Of_Named_Edges", aResList);
467
468   CreateAndPublishGroup(theStudy, theMainShape, anIndices, VertSeqS, VertSeqN,
469                         "Group_Of_Named_Vertices", aResList);
470
471   return aResList._retn();
472 }
473
474
475 //============================================================================
476 // function : Save()
477 // purpose  : save OCAF/Geom document
478 //============================================================================
479 SALOMEDS::TMPFile* GEOM_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
480                                     const char* theURL,
481                                     bool isMultiFile) {
482   SALOMEDS::TMPFile_var aStreamFile;
483   // Get a temporary directory to store a file
484   std::string aTmpDir = (isMultiFile)?theURL:SALOMEDS_Tool::GetTmpDir();
485
486   // OCCT BUG: cannot save a document (in current folder)
487   // if directory name is empty
488   if (aTmpDir.size() == 0) {
489 #ifdef WNT
490     aTmpDir = ".\\";
491 #else
492     aTmpDir = "./";
493 #endif
494   }
495
496   // Create a list to store names of created files
497   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
498   aSeq->length(1);
499   // Prepare a file name to open
500   TCollection_AsciiString aNameWithExt("");
501   if (isMultiFile)
502     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath
503                                                    (theComponent->GetStudy()->URL())).c_str());
504   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
505   aSeq[0] = CORBA::string_dup(aNameWithExt.ToCString());
506   // Build a full file name of temporary file
507   TCollection_AsciiString aFullName = TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt;
508   // Save GEOM component in this file
509   _impl->Save(theComponent->GetStudy()->StudyId(),(char*) aFullName.ToCString());
510   // Conver a file to the byte stream
511   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
512   // Remove the created file and tmp directory
513   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
514
515   // Return the created byte stream
516   return aStreamFile._retn();
517 }
518
519
520 //============================================================================
521 // function : SaveASCII()
522 // purpose  :
523 //============================================================================
524 SALOMEDS::TMPFile* GEOM_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
525                                          const char* theURL,
526                                          bool isMultiFile) {
527   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
528   return aStreamFile._retn();
529 }
530
531
532 //============================================================================
533 // function : Load()
534 // purpose  :
535 //============================================================================
536 CORBA::Boolean GEOM_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
537                                 const SALOMEDS::TMPFile& theStream,
538                                 const char* theURL,
539                                 bool isMultiFile) {
540
541   if (theStream.length() <= 9) {
542     MESSAGE("The TMPFile is too short : " << theStream.length() << " bytes ");
543     return false;
544   }
545
546   // Get a temporary directory for a file
547   std::string aTmpDir = isMultiFile?theURL:SALOMEDS_Tool::GetTmpDir();
548
549   // OCCT BUG: cannot load a document (from current folder)
550   // if directory name is empty
551   if (aTmpDir.size() == 0) {
552 #ifdef WNT
553     aTmpDir = ".\\";
554 #else
555     aTmpDir = "./";
556 #endif
557   }
558
559   // Conver the byte stream theStream to a file and place it in tmp directory
560   SALOMEDS::ListOfFileNames_var aSeq =
561     SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.c_str(), isMultiFile);
562
563   // Prepare a file name to open
564   TCollection_AsciiString aNameWithExt("");
565   if (isMultiFile)
566     aNameWithExt = TCollection_AsciiString((char*)(SALOMEDS_Tool::GetNameFromPath
567                                                    (theComponent->GetStudy()->URL())).c_str());
568   aNameWithExt += TCollection_AsciiString("_GEOM.sgd");
569   TCollection_AsciiString aFullName = (TCollection_AsciiString((char*)aTmpDir.c_str()) + aNameWithExt);
570
571   // Open document
572   if (!_impl->Load(theComponent->GetStudy()->StudyId(),(char*) aFullName.ToCString())) return false;
573
574   // Remove the created file and tmp directory
575   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
576
577   SALOMEDS::Study_var Study = theComponent->GetStudy();
578   TCollection_AsciiString name (Study->Name());
579
580   return true;
581 }
582
583
584 //============================================================================
585 // function : LoadASCII()
586 // purpose  :
587 //============================================================================
588 CORBA::Boolean GEOM_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
589                                      const SALOMEDS::TMPFile& theStream,
590                                      const char* theURL,
591                                      bool isMultiFile) {
592   return Load(theComponent, theStream, theURL, isMultiFile);
593 }
594
595
596 //============================================================================
597 // function : Close()
598 // purpose  :
599 //============================================================================
600 void GEOM_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
601 {
602   SALOMEDS::Study_var aStudy= theComponent->GetStudy();
603   _impl->Close(aStudy->StudyId());
604 }
605
606 //============================================================================
607 // function : CanCopy()
608 // purpose  :
609 //============================================================================
610 CORBA::Boolean GEOM_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
611   // Try to retrieve known by Geometry component GEOM_Object by given IOR
612   SALOMEDS::GenericAttribute_var anAttr;
613   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
614
615   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
616
617   CORBA::String_var aString=anIOR->Value();
618   anIOR->Destroy();
619   CORBA::Object_var anObj = _orb->string_to_object(aString);
620   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow(anObj);
621   // If the object is null one it can't be copied: return false
622   if (anObject->_is_nil()) return false;
623   return true;
624 }
625
626 //============================================================================
627 // function : CopyFrom()
628 // purpose  :
629 //============================================================================
630 SALOMEDS::TMPFile* GEOM_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID)
631 {
632   // Declare a sequence of the byte to store the copied object
633   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
634
635   // Try to get GEOM_Object object by given SObject
636   SALOMEDS::GenericAttribute_var anAttr;
637   if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return aStreamFile._retn();
638   GEOM::GEOM_Object_var anObject = GEOM::GEOM_Object::_narrow
639     (_orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value()));
640   if (anObject->_is_nil()) return aStreamFile._retn();
641
642   aStreamFile = anObject->GetShapeStream();
643
644   // Assign an ID  the type of  GEOM_Object
645   theObjectID = anObject->GetType();
646
647   // Return created TMPFile
648   return aStreamFile._retn();
649 }
650
651 //============================================================================
652 // function : CanPaste()
653 // purpose  :
654 //============================================================================
655 CORBA::Boolean GEOM_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
656   // The Geometry component can paste only objects copied by Geometry component
657   // and with the object type = 1
658   if (strcmp(theComponentName, ComponentDataType()) != 0) return false;
659   return true;
660 }
661
662 //============================================================================
663 // function : PasteInto()
664 // purpose  :
665 //============================================================================
666 SALOMEDS::SObject_ptr GEOM_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
667                                             CORBA::Long theObjectID,
668                                             SALOMEDS::SObject_ptr theObject) {
669   // Find the current Study and StudyBuilder
670   SALOMEDS::Study_var aStudy = theObject->GetStudy();
671   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
672
673   // Retrieve a TopoDS_Shape from byte stream
674   TopoDS_Shape aTopology;
675   istrstream aStreamedBrep((char*) &theStream[0], theStream.length());
676   BRep_Builder aBuilder;
677   try {
678     BRepTools::Read(aTopology, aStreamedBrep, aBuilder);
679   } catch (Standard_Failure) {
680     return false;
681   }
682
683   // SObject of the created shape is theObject or new Child of Component if theObject == geom component
684   SALOMEDS::SObject_var aNewSO;
685   if (strcmp(theObject->GetFatherComponent()->GetID(),theObject->GetID()) == 0) {
686     aNewSO = aStudyBuilder->NewObject(theObject);
687   } else aNewSO = SALOMEDS::SObject::_duplicate(theObject);
688
689
690   //Create a new GEOM_Object
691   Handle(GEOM_Object) anObj = _impl->AddObject(aNewSO->GetStudy()->StudyId(), theObjectID);
692   Handle(GEOM_Function) aFunction = anObj->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
693   aFunction->SetValue(aTopology);
694
695   TCollection_AsciiString anEntry;
696   TDF_Tool::Entry(anObj->GetEntry(), anEntry);
697   GEOM::GEOM_Object_var obj = GetObject(anObj->GetDocID(), anEntry.ToCString());
698
699   //Set the study entry of the published GEOM_Object
700   obj->SetStudyEntry(aNewSO->GetID());
701
702   // Add IORAttribute to the Study and set IOR of the created GEOM_Object to it
703   SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSO, "AttributeIOR");
704   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
705   CORBA::String_var objStr = _orb->object_to_string(obj);
706   anIOR->SetValue(objStr.in());
707   anIOR->Destroy();
708
709   // Return the created in the Study SObject
710   return aNewSO._retn();
711 }
712
713 //============================================================================
714 // function : ComponentDataType()
715 // purpose  :
716 //============================================================================
717 char* GEOM_Gen_i::ComponentDataType()
718 {
719   return CORBA::string_dup("GEOM");
720 }
721
722 //============================================================================
723 // function : AddInStudy
724 // purpose  :
725 //============================================================================
726 SALOMEDS::SObject_ptr GEOM_Gen_i::AddInStudy (SALOMEDS::Study_ptr theStudy,
727                                               GEOM::GEOM_Object_ptr theObject,
728                                               const char* theName,
729                                               GEOM::GEOM_Object_ptr theFather)
730 {
731   SALOMEDS::SObject_var aResultSO;
732   if(theObject->_is_nil() || theStudy->_is_nil()) return aResultSO;
733
734   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
735   CORBA::String_var IOR;
736
737   if(!theFather->_is_nil()) {
738     IOR = _orb->object_to_string(theFather);
739     SALOMEDS::SObject_var aFatherSO = theStudy->FindObjectIOR(IOR.in());
740     if(aFatherSO->_is_nil()) return aResultSO._retn();
741     aResultSO = aStudyBuilder->NewObject(aFatherSO);
742     aFatherSO->Destroy();
743     //aStudyBuilder->Addreference(aResultSO, aResultSO);
744   }
745
746   aResultSO = PublishInStudy(theStudy, aResultSO, theObject, theName);
747   if(aResultSO->_is_nil()) return aResultSO._retn();
748
749   GEOM::ListOfGO_var aList = theObject->GetDependency();
750   Standard_Integer aLength = aList->length();
751   if(aLength < 1) return aResultSO._retn();
752
753   //Publish the arguments
754   for(Standard_Integer i = 0; i< aLength; i++) {
755     GEOM::GEOM_Object_var anObject = aList[i];
756     if(anObject->_is_nil()) continue;
757     IOR = _orb->object_to_string(anObject);
758     SALOMEDS::SObject_var aSO =  theStudy->FindObjectIOR(IOR.in());
759     if(aSO->_is_nil()) continue;
760     SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(aResultSO);
761     aStudyBuilder->Addreference(aSubSO, aSO);
762     aSO->Destroy();
763     aSubSO->Destroy();
764   }
765
766   return aResultSO._retn();
767 }
768
769 //============================================================================
770 // function : RestoreSubShapesO
771 // purpose  : Publish sub-shapes, standing for arguments and sub-shapes of arguments.
772 //            To be used from python scripts out of geompy.addToStudy (non-default usage)
773 //============================================================================
774 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapesO (SALOMEDS::Study_ptr     theStudy,
775                                                GEOM::GEOM_Object_ptr   theObject,
776                                                const GEOM::ListOfGO&   theArgs,
777                                                GEOM::find_shape_method theFindMethod,
778                                                CORBA::Boolean          theInheritFirstArg,
779                                                CORBA::Boolean          theAddPrefix)
780 {
781   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
782   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject))
783     return aParts._retn();
784
785   // find SObject in the study if it is already published
786   CORBA::String_var anIORo = _orb->object_to_string(theObject);
787   SALOMEDS::SObject_var aSO = theStudy->FindObjectIOR(anIORo.in());
788   //PTv, IMP 0020001, The salome object <aSO>
789   // is not obligatory in case of invokation from script
790   // if (CORBA::is_nil(aSO))
791   //  return aParts._retn();
792
793   aParts = RestoreSubShapes(theStudy, theObject, aSO, theArgs,
794                             theFindMethod, theInheritFirstArg, theAddPrefix);
795   aSO->Destroy();
796   return aParts._retn();
797 }
798
799 //============================================================================
800 // function : RestoreSubShapesSO
801 // purpose  : Publish sub-shapes, standing for arguments and sub-shapes of arguments.
802 //            To be used from GUI and from geompy.addToStudy
803 //============================================================================
804 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapesSO (SALOMEDS::Study_ptr     theStudy,
805                                                 SALOMEDS::SObject_ptr   theSObject,
806                                                 const GEOM::ListOfGO&   theArgs,
807                                                 GEOM::find_shape_method theFindMethod,
808                                                 CORBA::Boolean          theInheritFirstArg,
809                                                 CORBA::Boolean          theAddPrefix)
810 {
811   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
812   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theSObject))
813     return aParts._retn();
814
815   SALOMEDS::GenericAttribute_var anAttr;
816   if (!theSObject->FindAttribute(anAttr, "AttributeIOR"))
817     return aParts._retn();
818
819   SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
820   CORBA::String_var anIORso = anAttrIOR->Value();
821
822   // get Object from SObject
823   GEOM::GEOM_Object_var anO = GEOM::GEOM_Object::_narrow(_orb->string_to_object(anIORso));
824   if (CORBA::is_nil(anO))
825     return aParts._retn();
826
827   aParts = RestoreSubShapes(theStudy, anO, theSObject, theArgs,
828                             theFindMethod, theInheritFirstArg, theAddPrefix);
829   return aParts._retn();
830 }
831
832 //============================================================================
833 // function : addToListOfGO
834 // purpose  : static local function
835 //============================================================================
836 static void addToListOfGO( GEOM::GEOM_Object_ptr theObject,
837                            GEOM::ListOfGO& theList )
838 {
839   const int oldLen = theList.length();
840   theList.length(oldLen + 1);
841   theList[ oldLen ] = GEOM::GEOM_Object::_duplicate( theObject );
842 }
843
844 //============================================================================
845 // function : addToListOfGO
846 // purpose  : static local function
847 //============================================================================
848 static void addToListOfGO( const GEOM::ListOfGO& theSrcList,
849                            GEOM::ListOfGO& theTrgList )
850 {
851   const int oldLen = theTrgList.length();
852   const int srcLen = theSrcList.length();
853   theTrgList.length(oldLen + srcLen);
854   for( int i = 0; i < srcLen; i++ )
855     theTrgList[ oldLen + i ] = GEOM::GEOM_Object::_duplicate( theSrcList[ i ] );
856 }
857
858 //============================================================================
859 // function : RestoreSubShapes
860 // purpose  : Private method. Works only if both theObject and theSObject
861 //            are defined, and does not check, if they correspond to each other.
862 //============================================================================
863 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapes(SALOMEDS::Study_ptr     theStudy,
864                                              GEOM::GEOM_Object_ptr   theObject,
865                                              SALOMEDS::SObject_ptr   theSObject,
866                                              const GEOM::ListOfGO&   theArgs,
867                                              GEOM::find_shape_method theFindMethod,
868                                              CORBA::Boolean          theInheritFirstArg,
869                                              CORBA::Boolean          theAddPrefix)
870 {
871   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
872   //PTv, IMP 0020001, The salome object <theSObject>
873   //     is not obligatory in case of invokation from script
874   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theObject) /*|| CORBA::is_nil(theSObject)*/)
875     return aParts._retn();
876
877   // Arguments to be published
878   GEOM::ListOfGO_var aList;
879
880   // If theArgs list is empty, we try to publish all arguments,
881   // otherwise publish only passed args
882   Standard_Integer nbArgsActual = -1; // -1 means unknown
883   Standard_Integer aLength = theArgs.length();
884   if (aLength > 0) {
885     aList = new GEOM::ListOfGO;
886     aList->length(aLength);
887     for (int i = 0; i < aLength; i++) {
888       aList[i] = GEOM::GEOM_Object::_duplicate( theArgs[i] );
889     }
890   }
891   else {
892     // Get all arguments
893     aList = theObject->GetDependency();
894     aLength = aList->length();
895     nbArgsActual = aLength;
896   }
897
898   if (aLength < 1)
899     return aParts._retn();
900
901   if (theInheritFirstArg || (nbArgsActual == 1)) {
902     // Do not publish argument's reflection,
903     // but only reconstruct its published sub-shapes
904
905     CORBA::String_var anIOR = _orb->object_to_string(aList[0]);
906     SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
907
908     aParts = RestoreSubShapesOneLevel(theStudy, anArgSO, theSObject, theObject, theFindMethod, theAddPrefix);
909
910     // set the color of the transformed shape to the color of initial shape
911     theObject->SetColor(aList[0]->GetColor());
912     anArgSO->Destroy();
913   }
914   else {
915     // Get interface, containing method, which we will use to reconstruct sub-shapes
916     GEOM::GEOM_IShapesOperations_var aShapesOp = GetIShapesOperations(theStudy->StudyId());
917     GEOM::GEOM_IGroupOperations_var  aGroupOp  = GetIGroupOperations(theStudy->StudyId());
918
919     // Reconstruct arguments and tree of sub-shapes of the arguments
920     CORBA::String_var anIOR;
921     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
922     for (Standard_Integer i = 0; i < aLength; i++)
923     {
924       GEOM::GEOM_Object_var anArgO = aList[i];
925       if (!CORBA::is_nil(anArgO)) {
926         anIOR = _orb->object_to_string(anArgO);
927         SALOMEDS::SObject_var anArgSO = theStudy->FindObjectIOR(anIOR.in());
928         TCollection_AsciiString anArgName;
929         if (CORBA::is_nil(anArgSO)) {
930           anArgName = "arg_";
931           anArgName += TCollection_AsciiString(i);
932         }
933         else {
934           anArgName = anArgSO->GetName();
935         }
936
937         // Find a sub-shape of theObject in place of the argument
938         GEOM::GEOM_Object_var aSubO;
939         switch (theFindMethod) {
940         case GEOM::FSM_GetInPlace:
941           {
942             // Use GetInPlace
943             aSubO = aShapesOp->GetInPlace(theObject, anArgO);
944           }
945           break;
946         case GEOM::FSM_Transformed:
947           {
948             // transformation, cannot use GetInPlace, operate with indices
949             GEOM::ListOfLong_var anIDs = anArgO->GetSubShapeIndices();
950             if (anIDs->length() > 1) {
951               // group
952               aSubO = aGroupOp->CreateGroup(theObject, aGroupOp->GetType(anArgO));
953               if (!CORBA::is_nil(aSubO))
954                 aGroupOp->UnionIDs(aSubO, anIDs);
955             }
956             else if (anIDs->length() > 0) {
957               // single sub-shape
958               aSubO = aShapesOp->GetSubShape(theObject, anIDs[0]);
959             }
960           }
961           break;
962         case GEOM::FSM_GetSame:
963           {
964             // Use GetSame
965             aSubO = aShapesOp->GetSame(theObject, anArgO);
966           }
967           break;
968         case GEOM::FSM_GetShapesOnShape:
969           {
970             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
971             aSubO = aShapesOp->GetShapesOnShapeAsCompound(anArgO, theObject,
972               (short)GEOM::SOLID, GEOM::ST_ONIN);
973           }
974           break;
975         case GEOM::FSM_GetInPlaceByHistory:
976           {
977             // Use GetInPlaceByHistory
978             aSubO = aShapesOp->GetInPlaceByHistory(theObject, anArgO);
979           }
980           break;
981         default:
982           {}
983         }
984
985         if (!CORBA::is_nil(aSubO)) {
986           // add to parts list
987           addToListOfGO( aSubO, aParts );
988
989           // Publish the sub-shape
990           SALOMEDS::SObject_var aSubSO;
991           if (!CORBA::is_nil(theSObject)) {
992             TCollection_AsciiString aSubName;
993             if (theAddPrefix) {
994               aSubName = "from_";
995             }
996             aSubName += anArgName;
997             aSubSO = aStudyBuilder->NewObject(theSObject);
998             aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
999             // Restore color
1000             aSubO->SetColor(anArgO->GetColor());
1001           }
1002
1003           if (!CORBA::is_nil(anArgSO)) {
1004             // Restore published sub-shapes of the argument
1005             GEOM::ListOfGO_var aSubParts;
1006             if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
1007               // pass theObject, because only it has the history
1008               aSubParts = RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1009                                                    theObject, theFindMethod, theAddPrefix);
1010             else
1011               aSubParts = RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1012                                                    aSubO, theFindMethod, theAddPrefix);
1013             // add to parts list
1014             addToListOfGO( aSubParts, aParts );
1015           }
1016         }
1017         else { // GetInPlace failed, try to build from published parts
1018           if (!CORBA::is_nil(anArgSO)) {
1019             SALOMEDS::SObject_var aSubSO;
1020             if (!CORBA::is_nil(theSObject))
1021               aSubSO = aStudyBuilder->NewObject(theSObject);
1022
1023             // Restore published sub-shapes of the argument
1024             GEOM::ListOfGO_var aSubParts =
1025               RestoreSubShapesOneLevel(theStudy, anArgSO, aSubSO,
1026                                        theObject, theFindMethod, theAddPrefix);
1027
1028             // add to parts list
1029             addToListOfGO( aSubParts, aParts );
1030
1031             if (aSubParts->length() > 0) {
1032               // try to build an argument from a set of its sub-shapes,
1033               // that published and will be reconstructed
1034               if (aSubParts->length() > 1) {
1035                 aSubO = aShapesOp->MakeCompound(aSubParts);
1036                 // add to parts list
1037                 addToListOfGO( aSubO, aParts );
1038               }
1039               else {
1040                 aSubO = aSubParts[0];
1041               }
1042               if (!CORBA::is_nil(aSubO) && !CORBA::is_nil(aSubSO)) {
1043                 // Publish the sub-shape
1044                 TCollection_AsciiString aSubName;
1045                 if (theAddPrefix) {
1046                   aSubName = "from_parts_of_";
1047                 }
1048                 aSubName += anArgName;
1049                 aSubSO = PublishInStudy(theStudy, aSubSO, aSubO, aSubName.ToCString());
1050                 // Restore color
1051                 aSubO->SetColor(anArgO->GetColor());
1052               }
1053             }
1054             else if (!CORBA::is_nil(aSubSO)) {
1055               // remove created aSubSO, because no parts have been found
1056               aStudyBuilder->RemoveObject(aSubSO);
1057             }
1058           }
1059         } // try to build from published parts
1060         anArgSO->Destroy();
1061       }
1062     } // process arguments
1063   }
1064   set<string> anObjEntryMap;
1065   GEOM::ListOfGO_var aResParts = new GEOM::ListOfGO;
1066   int nbRes = 0;
1067   int nb = aParts->length();
1068   aResParts->length(nb);
1069   if (nb > 0)
1070   {
1071     Handle(GEOM_Object) aMainObj = _impl->GetObject(theObject->GetStudyID(), theObject->GetEntry());
1072     Handle(GEOM_Function) aFunction = aMainObj->GetLastFunction();
1073     GEOM::TPythonDump pd (aFunction, true);
1074     pd <<"[";
1075     int i = 0, j = 0;
1076     for ( ; i < nb; i++ )
1077     {
1078       GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_duplicate( aParts[ i ] );
1079       if (CORBA::is_nil(anObj))
1080         continue;
1081       char* anEntry = anObj->GetEntry();
1082       if (anObjEntryMap.count(anEntry))
1083         continue; // already treated
1084       anObjEntryMap.insert(anEntry);
1085       aResParts[nbRes++] = anObj;
1086       // clear python dump of object
1087       Handle(GEOM_Object) aGeomObj = _impl->GetObject(anObj->GetStudyID(), anEntry);
1088       Handle(GEOM_Function) anObjFun = aGeomObj->GetLastFunction();
1089       if ( !anObjFun.IsNull() )
1090         anObjFun->SetDescription( "" );
1091       if ( j > 0 )
1092         pd << ", ";
1093       pd << aGeomObj;
1094       j++;
1095     }
1096     pd <<"]" << " = geompy.RestoreSubShapes(" << aMainObj << ", " << "[";
1097     i = 0; nb = theArgs.length(); j = 0;
1098     for ( ; i < nb; i++ )
1099     {
1100       GEOM::GEOM_Object_var anObj = theArgs[ i ];
1101       if (CORBA::is_nil(anObj))
1102         continue;
1103       Handle(GEOM_Object) aGeomObj = _impl->GetObject(anObj->GetStudyID(), anObj->GetEntry());
1104       if ( j > 0 )
1105         pd << ", ";
1106       pd << aGeomObj;
1107       j++;
1108     }
1109     pd <<"]" << ", " <<"geompy.GEOM.";
1110     switch (theFindMethod) {
1111     case GEOM::FSM_GetInPlace:
1112       pd << "FSM_GetInPlace"; break;
1113     case GEOM::FSM_Transformed:
1114       pd << "FSM_Transformed"; break;
1115     case GEOM::FSM_GetSame:
1116       pd << "FSM_GetSame"; break;
1117     case GEOM::FSM_GetShapesOnShape:
1118       pd << "FSM_GetShapesOnShape"; break;
1119     case GEOM::FSM_GetInPlaceByHistory:
1120     default:
1121       pd << "FSM_GetInPlaceByHistory"; break;
1122     }
1123     pd << ", " << theInheritFirstArg << ", " << theAddPrefix << ")";
1124   }
1125   aResParts->length(nbRes);
1126   return aResParts._retn();
1127 }
1128
1129 //============================================================================
1130 // function : RestoreSubShapesOneLevel
1131 // purpose  : Private method
1132 //============================================================================
1133 GEOM::ListOfGO* GEOM_Gen_i::RestoreSubShapesOneLevel (SALOMEDS::Study_ptr     theStudy,
1134                                                       SALOMEDS::SObject_ptr   theOldSO,
1135                                                       SALOMEDS::SObject_ptr   theNewSO,
1136                                                       GEOM::GEOM_Object_ptr   theNewO,
1137                                                       GEOM::find_shape_method theFindMethod,
1138                                                       CORBA::Boolean          theAddPrefix)
1139 {
1140   int i = 0;
1141   GEOM::ListOfGO_var aParts = new GEOM::ListOfGO;
1142   GEOM::ListOfGO_var aNewParts = new GEOM::ListOfGO;
1143   if (CORBA::is_nil(theStudy) || CORBA::is_nil(theOldSO) ||
1144       CORBA::is_nil(theNewO) /*|| CORBA::is_nil(theNewSO)*/)
1145     return aParts._retn();
1146
1147   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
1148
1149   // Get interface, containing method, which we will use to reconstruct sub-shapes
1150   GEOM::GEOM_IShapesOperations_var aShapesOp = GetIShapesOperations(theStudy->StudyId());
1151   GEOM::GEOM_IGroupOperations_var  aGroupOp  = GetIGroupOperations(theStudy->StudyId());
1152
1153   // Reconstruct published sub-shapes
1154   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator(theOldSO);
1155
1156   int aLen = 0;
1157   for (it->Init(); it->More(); it->Next()) {
1158     aLen++;
1159   }
1160   aParts->length(aLen);
1161
1162   for (it->Init(); it->More(); it->Next()) {
1163     SALOMEDS::SObject_var anOldSubSO = it->Value();
1164
1165     TCollection_AsciiString anArgName = anOldSubSO->GetName();
1166
1167     SALOMEDS::GenericAttribute_var anAttr;
1168     if (anOldSubSO->FindAttribute(anAttr, "AttributeIOR")) {
1169       SALOMEDS::AttributeIOR_var anAttrIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1170       GEOM::GEOM_Object_var anOldSubO =
1171         GEOM::GEOM_Object::_narrow(_orb->string_to_object(anAttrIOR->Value()));
1172       if (!CORBA::is_nil(anOldSubO)) {
1173         // Find a sub-shape of theNewO in place of anOldSubO
1174         GEOM::GEOM_Object_var aNewSubO;
1175         switch (theFindMethod) {
1176         case GEOM::FSM_GetInPlace:
1177           {
1178             // Use GetInPlace
1179             aNewSubO = aShapesOp->GetInPlace(theNewO, anOldSubO);
1180           }
1181           break;
1182         case GEOM::FSM_Transformed:
1183           {
1184             // transformation, cannot use GetInPlace, operate with indices
1185             GEOM::ListOfLong_var anIDs = anOldSubO->GetSubShapeIndices();
1186             if (anIDs->length() > 1) {
1187               // group
1188               aNewSubO = aGroupOp->CreateGroup(theNewO, aGroupOp->GetType(anOldSubO));
1189               if (!CORBA::is_nil(aNewSubO))
1190                 aGroupOp->UnionIDs(aNewSubO, anIDs);
1191             }
1192             else {
1193               // single sub-shape
1194               aNewSubO = aShapesOp->GetSubShape(theNewO, anIDs[0]);
1195             }
1196           }
1197           break;
1198         case GEOM::FSM_GetSame:
1199           {
1200             // Use GetSame
1201             aNewSubO = aShapesOp->GetSame(theNewO, anOldSubO);
1202           }
1203           break;
1204         case GEOM::FSM_GetShapesOnShape:
1205           {
1206             // Use GetShapesOnShape. Can work only on solids, so it has sense to search only solids
1207             aNewSubO = aShapesOp->GetShapesOnShapeAsCompound(anOldSubO, theNewO,
1208                                                              (short)GEOM::SOLID, GEOM::ST_ONIN);
1209           }
1210           break;
1211         case GEOM::FSM_GetInPlaceByHistory:
1212           {
1213             // Use GetInPlaceByHistory
1214             aNewSubO = aShapesOp->GetInPlaceByHistory(theNewO, anOldSubO);
1215           }
1216           break;
1217         default:
1218           {}
1219         }
1220
1221         if (!CORBA::is_nil(aNewSubO)) {
1222           // add the part to the list
1223           aParts[i] = aNewSubO;
1224           i++;
1225           // add to parts list
1226           addToListOfGO( aNewSubO, aNewParts );
1227
1228           SALOMEDS::SObject_var aNewSubSO;
1229           if (!CORBA::is_nil(theNewSO)) {
1230             // Publish the sub-shape
1231             TCollection_AsciiString aSubName;
1232             if (theAddPrefix) {
1233               aSubName = "from_";
1234             }
1235             aSubName += anArgName;
1236             aNewSubSO = aStudyBuilder->NewObject(theNewSO);
1237             aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
1238             // Restore color
1239             aNewSubO->SetColor(anOldSubO->GetColor());
1240           }
1241           // Restore published sub-shapes of the argument
1242           GEOM::ListOfGO_var aSubParts;
1243           if (theFindMethod == GEOM::FSM_GetInPlaceByHistory)
1244             // pass the main shape as Object, because only it has the history
1245             aSubParts = RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1246                                                  theNewO, theFindMethod, theAddPrefix);
1247           else
1248             aSubParts = RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1249                                                  aNewSubO, theFindMethod, theAddPrefix);
1250           // add to parts list
1251           addToListOfGO( aSubParts, aNewParts );
1252         }
1253         else { // GetInPlace failed, try to build from published parts
1254           SALOMEDS::SObject_var aNewSubSO;
1255           if (!CORBA::is_nil(theNewSO))
1256             aNewSubSO = aStudyBuilder->NewObject(theNewSO);
1257
1258           // Restore published sub-shapes of the argument
1259           GEOM::ListOfGO_var aSubParts =
1260             RestoreSubShapesOneLevel(theStudy, anOldSubSO, aNewSubSO,
1261                                      theNewO, theFindMethod, theAddPrefix);
1262           // add to parts list
1263           addToListOfGO( aSubParts, aNewParts );
1264
1265           if (aSubParts->length() > 0) {
1266             // try to build an object from a set of its sub-shapes,
1267             // that published and will be reconstructed
1268             if (aSubParts->length() > 1) {
1269               aNewSubO = aShapesOp->MakeCompound(aSubParts);
1270               // add to parts list
1271               addToListOfGO( aNewSubO, aNewParts );
1272             }
1273             else {
1274               aNewSubO = aSubParts[0];
1275             }
1276
1277             if (!CORBA::is_nil(aNewSubO)) {
1278               // add the part to the list
1279               aSubParts[i] = aNewSubO;
1280               i++;
1281
1282               // Publish the sub-shape
1283               if (!CORBA::is_nil(aNewSubSO)) {
1284                 TCollection_AsciiString aSubName;
1285                 if (theAddPrefix) {
1286                   aSubName = "from_parts_of_";
1287                 }
1288                 aSubName += anArgName;
1289                 aNewSubSO = PublishInStudy(theStudy, aNewSubSO, aNewSubO, aSubName.ToCString());
1290                 // Restore color
1291                 aNewSubO->SetColor(anOldSubO->GetColor());
1292               }
1293             }
1294           }
1295           else if (!CORBA::is_nil(aNewSubSO)) {
1296             // remove created aSubSO, because no parts have been found
1297             aStudyBuilder->RemoveObject(aNewSubSO);
1298           }
1299         } // try to build from published parts
1300       }
1301     }
1302   } // iterate on published sub-shapes
1303
1304   aParts->length(i);
1305   // add to parts list
1306   addToListOfGO( aNewParts, aParts );
1307   return aParts._retn();
1308 }
1309
1310 //============================================================================
1311 // function : register()
1312 // purpose  : register 'name' in 'name_service'
1313 //============================================================================
1314 void GEOM_Gen_i::register_name(char * name)
1315 {
1316   GEOM::GEOM_Gen_var g = _this();
1317   name_service->Register(g, name);
1318 }
1319
1320 //============================================================================
1321 // function : Undo
1322 // purpose  :
1323 //============================================================================
1324 void GEOM_Gen_i::Undo(CORBA::Long theStudyID)
1325 {
1326   _impl->Undo(theStudyID);
1327 }
1328
1329 //============================================================================
1330 // function : Redo
1331 // purpose  :
1332 //============================================================================
1333 void GEOM_Gen_i::Redo(CORBA::Long theStudyID)
1334 {
1335   _impl->Redo(theStudyID);
1336 }
1337
1338 //============================================================================
1339 // function : GetIBasicOperations
1340 // purpose  :
1341 //============================================================================
1342 GEOM::GEOM_IBasicOperations_ptr GEOM_Gen_i::GetIBasicOperations(CORBA::Long theStudyID)
1343      throw ( SALOME::SALOME_Exception )
1344 {
1345   Unexpect aCatch(SALOME_SalomeException);
1346   MESSAGE( "GEOM_Gen_i::GetIBasicOperations" );
1347
1348   GEOM::GEOM_Gen_ptr engine = _this();
1349
1350   //transfer reference on engine
1351   GEOM_IBasicOperations_i* aServant =
1352     new GEOM_IBasicOperations_i(_poa, engine, _impl->GetIBasicOperations(theStudyID));
1353
1354   PortableServer::ObjectId_var id = _poa->activate_object(aServant);
1355   // activate the CORBA servant
1356   GEOM::GEOM_IBasicOperations_var operations = aServant->_this();
1357   return operations._retn();
1358 }
1359
1360 //============================================================================
1361 // function : GetITransformOperations
1362 // purpose  :
1363 //============================================================================
1364 GEOM::GEOM_ITransformOperations_ptr GEOM_Gen_i::GetITransformOperations(CORBA::Long theStudyID)
1365      throw ( SALOME::SALOME_Exception )
1366 {
1367   Unexpect aCatch(SALOME_SalomeException);
1368   MESSAGE( "GEOM_Gen_i::GetITransformOperations" );
1369
1370   GEOM::GEOM_Gen_ptr engine = _this();
1371
1372   GEOM_ITransformOperations_i* aServant =
1373     new GEOM_ITransformOperations_i(_poa, engine, _impl->GetITransformOperations(theStudyID));
1374
1375   // activate the CORBA servant
1376   GEOM::GEOM_ITransformOperations_var operations = aServant->_this();
1377   return operations._retn();
1378 }
1379
1380 //============================================================================
1381 // function : GetI3DPrimOperations
1382 // purpose  :
1383 //============================================================================
1384 GEOM::GEOM_I3DPrimOperations_ptr GEOM_Gen_i::GetI3DPrimOperations(CORBA::Long theStudyID)
1385      throw ( SALOME::SALOME_Exception )
1386 {
1387   Unexpect aCatch(SALOME_SalomeException);
1388   MESSAGE( "GEOM_Gen_i::GetI3DPrimOperations" );
1389
1390   GEOM::GEOM_Gen_ptr engine = _this();
1391
1392   GEOM_I3DPrimOperations_i* aServant =
1393     new GEOM_I3DPrimOperations_i(_poa, engine, _impl->GetI3DPrimOperations(theStudyID));
1394   PortableServer::ObjectId_var id = _poa->activate_object(aServant);
1395
1396   // activate the CORBA servant
1397   GEOM::GEOM_I3DPrimOperations_var operations = aServant->_this();
1398   return operations._retn();
1399 }
1400
1401 //============================================================================
1402 // function : GetIShapesOperations
1403 // purpose  :
1404 //============================================================================
1405 GEOM::GEOM_IShapesOperations_ptr GEOM_Gen_i::GetIShapesOperations(CORBA::Long theStudyID)
1406      throw ( SALOME::SALOME_Exception )
1407 {
1408   Unexpect aCatch(SALOME_SalomeException);
1409   MESSAGE( "GEOM_Gen_i::GetIShapesOperations" );
1410
1411   GEOM::GEOM_Gen_ptr engine = _this();
1412
1413   GEOM_IShapesOperations_i* aServant =
1414     new GEOM_IShapesOperations_i(_poa, engine, _impl->GetIShapesOperations(theStudyID));
1415
1416   // activate the CORBA servant
1417   GEOM::GEOM_IShapesOperations_var operations = aServant->_this();
1418   return operations._retn();
1419 }
1420
1421 //============================================================================
1422 // function : GetIBlocksOperations
1423 // purpose  :
1424 //============================================================================
1425 GEOM::GEOM_IBlocksOperations_ptr GEOM_Gen_i::GetIBlocksOperations(CORBA::Long theStudyID)
1426      throw ( SALOME::SALOME_Exception )
1427 {
1428   Unexpect aCatch(SALOME_SalomeException);
1429   MESSAGE( "GEOM_Gen_i::GetIBlocksOperations" );
1430
1431   GEOM::GEOM_Gen_ptr engine = _this();
1432
1433   GEOM_IBlocksOperations_i* aServant =
1434     new GEOM_IBlocksOperations_i(_poa, engine, _impl->GetIBlocksOperations(theStudyID));
1435
1436   // activate the CORBA servant
1437   GEOM::GEOM_IBlocksOperations_var operations = aServant->_this();
1438   return operations._retn();
1439 }
1440
1441 //============================================================================
1442 // function : GetIBooleanOperations
1443 // purpose  :
1444 //============================================================================
1445 GEOM::GEOM_IBooleanOperations_ptr GEOM_Gen_i::GetIBooleanOperations(CORBA::Long theStudyID)
1446      throw ( SALOME::SALOME_Exception )
1447 {
1448   Unexpect aCatch(SALOME_SalomeException);
1449   MESSAGE( "GEOM_Gen_i::GetIBooleanOperations" );
1450
1451   GEOM::GEOM_Gen_ptr engine = _this();
1452
1453   GEOM_IBooleanOperations_i* aServant =
1454     new GEOM_IBooleanOperations_i(_poa, engine, _impl->GetIBooleanOperations(theStudyID));
1455
1456   // activate the CORBA servant
1457   GEOM::GEOM_IBooleanOperations_var operations = aServant->_this();
1458   return operations._retn();
1459 }
1460
1461 //============================================================================
1462 // function : GetICurvesOperations
1463 // purpose  :
1464 //============================================================================
1465 GEOM::GEOM_ICurvesOperations_ptr GEOM_Gen_i::GetICurvesOperations(CORBA::Long theStudyID)
1466      throw ( SALOME::SALOME_Exception )
1467 {
1468   Unexpect aCatch(SALOME_SalomeException);
1469   MESSAGE( "GEOM_Gen_i::GetICurvesOperations" );
1470
1471   GEOM::GEOM_Gen_ptr engine = _this();
1472
1473   GEOM_ICurvesOperations_i* aServant =
1474     new GEOM_ICurvesOperations_i(_poa, engine, _impl->GetICurvesOperations(theStudyID));
1475
1476   // activate the CORBA servant
1477   GEOM::GEOM_ICurvesOperations_var operations = aServant->_this();
1478   return operations._retn();
1479 }
1480
1481 //============================================================================
1482 // function : GetILocalOperations
1483 // purpose  :
1484 //============================================================================
1485 GEOM::GEOM_ILocalOperations_ptr GEOM_Gen_i::GetILocalOperations(CORBA::Long theStudyID)
1486      throw ( SALOME::SALOME_Exception )
1487 {
1488   Unexpect aCatch(SALOME_SalomeException);
1489   MESSAGE( "GEOM_Gen_i::GetILocalOperations" );
1490
1491   GEOM::GEOM_Gen_ptr engine = _this();
1492
1493   GEOM_ILocalOperations_i* aServant =
1494     new GEOM_ILocalOperations_i(_poa, engine, _impl->GetILocalOperations(theStudyID));
1495
1496   // activate the CORBA servant
1497   GEOM::GEOM_ILocalOperations_var operations = aServant->_this();
1498   return operations._retn();
1499 }
1500
1501 //============================================================================
1502 // function : GetIHealingOperations
1503 // purpose  :
1504 //============================================================================
1505 GEOM::GEOM_IHealingOperations_ptr GEOM_Gen_i::GetIHealingOperations(CORBA::Long theStudyID)
1506      throw ( SALOME::SALOME_Exception )
1507 {
1508   Unexpect aCatch(SALOME_SalomeException);
1509   MESSAGE( "GEOM_Gen_i::IHealingOperations" );
1510
1511   GEOM::GEOM_Gen_ptr engine = _this();
1512
1513   GEOM_IHealingOperations_i* aServant =
1514     new GEOM_IHealingOperations_i(_poa, engine, _impl->GetIHealingOperations(theStudyID));
1515
1516   // activate the CORBA servant
1517   GEOM::GEOM_IHealingOperations_var operations = aServant->_this();
1518   return operations._retn();
1519 }
1520
1521 //============================================================================
1522 // function : GetIInsertOperations
1523 // purpose  :
1524 //============================================================================
1525 GEOM::GEOM_IInsertOperations_ptr GEOM_Gen_i::GetIInsertOperations(CORBA::Long theStudyID)
1526      throw ( SALOME::SALOME_Exception )
1527 {
1528   Unexpect aCatch(SALOME_SalomeException);
1529   MESSAGE( "GEOM_Gen_i::GetIInsertOperations" );
1530
1531   GEOM::GEOM_Gen_ptr engine = _this();
1532
1533   GEOM_IInsertOperations_i* aServant =
1534     new GEOM_IInsertOperations_i(_poa, engine, _impl->GetIInsertOperations(theStudyID));
1535
1536   // activate the CORBA servant
1537   GEOM::GEOM_IInsertOperations_var operations = aServant->_this();
1538   return operations._retn();
1539 }
1540
1541 //============================================================================
1542 // function : GetIMeasureOperations
1543 // purpose  :
1544 //============================================================================
1545 GEOM::GEOM_IMeasureOperations_ptr GEOM_Gen_i::GetIMeasureOperations(CORBA::Long theStudyID)
1546      throw ( SALOME::SALOME_Exception )
1547 {
1548   Unexpect aCatch(SALOME_SalomeException);
1549   MESSAGE( "GEOM_Gen_i::GetIMeasureOperations" );
1550
1551   GEOM::GEOM_Gen_ptr engine = _this();
1552
1553   GEOM_IMeasureOperations_i* aServant =
1554     new GEOM_IMeasureOperations_i(_poa, engine, _impl->GetIMeasureOperations(theStudyID));
1555
1556   // activate the CORBA servant
1557   GEOM::GEOM_IMeasureOperations_var operations = aServant->_this();
1558   return operations._retn();
1559 }
1560
1561 //============================================================================
1562 // function : GetIGroupOperations
1563 // purpose  :
1564 //============================================================================
1565 GEOM::GEOM_IGroupOperations_ptr GEOM_Gen_i::GetIGroupOperations(CORBA::Long theStudyID)
1566      throw ( SALOME::SALOME_Exception )
1567 {
1568   Unexpect aCatch(SALOME_SalomeException);
1569   MESSAGE( "GEOM_Gen_i::GetIGroupOperations" );
1570
1571   GEOM::GEOM_Gen_ptr engine = _this();
1572
1573   GEOM_IGroupOperations_i* aServant =
1574     new GEOM_IGroupOperations_i(_poa, engine, _impl->GetIGroupOperations(theStudyID));
1575
1576   // activate the CORBA servant
1577   GEOM::GEOM_IGroupOperations_var operations = aServant->_this();
1578   return operations._retn();
1579 }
1580
1581 //============================================================================
1582 // function : GetIAdvancedOperations
1583 // purpose  :
1584 //============================================================================
1585 GEOM::GEOM_IAdvancedOperations_ptr GEOM_Gen_i::GetIAdvancedOperations(CORBA::Long theStudyID)
1586      throw ( SALOME::SALOME_Exception )
1587 {
1588   Unexpect aCatch(SALOME_SalomeException);
1589   MESSAGE( "GEOM_Gen_i::GetIAdvancedOperations" );
1590
1591   GEOM::GEOM_Gen_ptr engine = _this();
1592
1593   GEOM_IAdvancedOperations_i* aServant =
1594     new GEOM_IAdvancedOperations_i(_poa, engine, _impl->GetIAdvancedOperations(theStudyID));
1595
1596   // activate the CORBA servant
1597   GEOM::GEOM_IAdvancedOperations_var operations = aServant->_this();
1598   return operations._retn();
1599 }
1600
1601 //=============================================================================
1602 /*!
1603  *  AddSubShape
1604  */
1605 //=============================================================================
1606 GEOM::GEOM_Object_ptr GEOM_Gen_i::AddSubShape (GEOM::GEOM_Object_ptr theMainShape,
1607                                                const GEOM::ListOfLong& theIndices)
1608 {
1609   if (CORBA::is_nil(theMainShape) || theIndices.length() < 1)
1610     return GEOM::GEOM_Object::_nil();
1611   CORBA::String_var entry = theMainShape->GetEntry();
1612   Handle(GEOM_Object) aMainsShape = _impl->GetObject(theMainShape->GetStudyID(), entry);
1613   if (aMainsShape.IsNull()) return GEOM::GEOM_Object::_nil();
1614
1615   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1, theIndices.length());
1616   for(Standard_Integer i = 0; i<theIndices.length(); i++) anArray->SetValue(i+1, theIndices[i]);
1617
1618   Handle(GEOM_Object) anObject = _impl->AddSubShape(aMainsShape, anArray, true);
1619   if(anObject.IsNull()) return GEOM::GEOM_Object::_nil();
1620
1621   TCollection_AsciiString anEntry;
1622   TDF_Tool::Entry(anObject->GetEntry(), anEntry);
1623   return GetObject(anObject->GetDocID(), anEntry.ToCString());
1624 }
1625
1626 //=============================================================================
1627 /*!
1628  *  RemoveObject
1629  */
1630 //=============================================================================
1631 void GEOM_Gen_i::RemoveObject(GEOM::GEOM_Object_ptr theObject)
1632 {
1633   CORBA::String_var anEntry = theObject->GetEntry();
1634   Handle(GEOM_Object) anObject = _impl->GetObject(theObject->GetStudyID(), anEntry, false);
1635   if (anObject.IsNull()) return;
1636   _impl->RemoveObject(anObject);
1637   return;
1638 }
1639
1640
1641 //=================================================================================
1642 // function : GetStringFromIOR()
1643 // purpose  : returns a string that represents  a 'GEOM::GEOM_Object_var'
1644 //=================================================================================
1645 char* GEOM_Gen_i::GetStringFromIOR(GEOM::GEOM_Object_ptr theObject) {
1646   return _orb->object_to_string(theObject);
1647 }
1648
1649
1650 //=================================================================================
1651 // function : GetIORFromString()
1652 // purpose  : returns a 'GEOM::GEOM_Object_var' from a string representing it
1653 //=================================================================================
1654 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetIORFromString(const char* stringIOR) {
1655   GEOM::GEOM_Object_var aGeomObject;
1656   if(strcmp(stringIOR,"") != 0){
1657     CORBA::Object_var anObject = _orb->string_to_object(stringIOR);
1658     if(!CORBA::is_nil(anObject))
1659       aGeomObject =  GEOM::GEOM_Object::_narrow(anObject.in());
1660   }
1661   return aGeomObject._retn();
1662 }
1663
1664 //=================================================================================
1665 // function : GetObject()
1666 // purpose  :
1667 //=================================================================================
1668 GEOM::GEOM_Object_ptr GEOM_Gen_i::GetObject (CORBA::Long theStudyID, const char* theEntry)
1669 {
1670   GEOM::GEOM_Object_var obj;
1671   Handle(GEOM_Object) handle_object = _impl->GetObject(theStudyID, (char*)theEntry);
1672   if (handle_object.IsNull()) return obj._retn();
1673
1674   TCollection_AsciiString stringIOR = handle_object->GetIOR();
1675   if (stringIOR.Length() > 1) {
1676     CORBA::Object_var corba_object = _orb->string_to_object(stringIOR.ToCString());
1677     if (!CORBA::is_nil(corba_object)) obj = GEOM::GEOM_Object::_narrow(corba_object);
1678     return obj._retn();
1679   }
1680
1681   GEOM::GEOM_Gen_ptr engine = _this();
1682   //transfer the reference to GEOM_Object_i
1683   GEOM_Object_i* servant = new GEOM_Object_i (_poa, engine, handle_object);
1684   PortableServer::ObjectId_var id = _poa->activate_object(servant);
1685
1686   obj = servant->_this();
1687   CORBA::String_var objStr = _orb->object_to_string(obj);
1688   TCollection_AsciiString anAscii( (char *)objStr.in() );
1689   handle_object->SetIOR( anAscii );
1690   return obj._retn();
1691 }
1692
1693 //=================================================================================
1694 // function : hasObjectInfo()
1695 // purpose  : shows if module provides information for its objects
1696 //=================================================================================
1697 bool GEOM_Gen_i::hasObjectInfo()
1698 {
1699   return true;
1700 }
1701
1702 //=================================================================================
1703 // function : getObjectInfo()
1704 // purpose  : returns an information for a given object by its entry
1705 //=================================================================================
1706 char* GEOM_Gen_i::getObjectInfo(CORBA::Long studyId, const char* entry)
1707 {
1708   GEOM::GEOM_Object_var aGeomObject;
1709  
1710   CORBA::Object_var aSMObject = name_service->Resolve( "/myStudyManager" );
1711   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow( aSMObject );
1712   SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID( studyId );
1713   SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( entry );
1714   SALOMEDS::SObject_var aResultSObj;
1715   if (aSObj->ReferencedObject(aResultSObj))
1716     aSObj = aResultSObj;
1717
1718   SALOMEDS::GenericAttribute_var anAttr;
1719   if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
1720     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1721     CORBA::String_var aVal = anIOR->Value();
1722     anIOR->Destroy();
1723     CORBA::Object_var anObject = aStudy->ConvertIORToObject(aVal);
1724     aGeomObject = GEOM::GEOM_Object::_narrow(anObject);
1725   }
1726   if (!aSObj->_is_nil() )
1727     aSObj->Destroy();
1728   
1729   const char* aTypeInfo = "Object";
1730   if ( !aGeomObject->_is_nil() ) {
1731     GEOM::GEOM_IKindOfShape::shape_kind aKind;
1732     GEOM::ListOfLong_var anInts;
1733     GEOM::ListOfDouble_var aDbls;
1734
1735     GEOM::GEOM_IMeasureOperations_var anOp = GetIMeasureOperations( studyId );
1736     aKind = anOp->KindOfShape( aGeomObject, anInts, aDbls );
1737
1738     if ( anOp->IsDone() ) {
1739       switch ( aKind ) {
1740       case GEOM::GEOM_IKindOfShape::COMPOUND:
1741         aTypeInfo = "Compound";
1742         break;
1743       case GEOM::GEOM_IKindOfShape::COMPSOLID:
1744         aTypeInfo = "CompSolid";
1745         break;
1746       case GEOM::GEOM_IKindOfShape::SHELL:
1747         aTypeInfo = "Shell";
1748         break;
1749       case GEOM::GEOM_IKindOfShape::WIRE:
1750         if ( anInts[0] == 1 )
1751           aTypeInfo = "Closed Wire";
1752         else if ( anInts[0] == 2 )
1753           aTypeInfo = "Opened Wire";
1754         else
1755           aTypeInfo = "Wire";
1756         break;
1757         // SOLIDs
1758       case GEOM::GEOM_IKindOfShape::SPHERE:
1759         aTypeInfo = "Sphere";
1760         break;
1761       case GEOM::GEOM_IKindOfShape::CYLINDER:
1762         aTypeInfo = "Cylinder";
1763         break;
1764       case GEOM::GEOM_IKindOfShape::BOX:
1765       case GEOM::GEOM_IKindOfShape::ROTATED_BOX:
1766         aTypeInfo = "Box";
1767         break;
1768       case GEOM::GEOM_IKindOfShape::TORUS:
1769         aTypeInfo = "Torus";
1770         break;
1771       case GEOM::GEOM_IKindOfShape::CONE:
1772         aTypeInfo = "Cone";
1773         break;
1774       case GEOM::GEOM_IKindOfShape::POLYHEDRON:
1775         aTypeInfo = "Polyhedron";
1776         break;
1777       case GEOM::GEOM_IKindOfShape::SOLID:
1778         aTypeInfo = "Solid";
1779         break;
1780         // FACEs
1781       case GEOM::GEOM_IKindOfShape::SPHERE2D:
1782         aTypeInfo = "Spherical Face";
1783         break;
1784       case GEOM::GEOM_IKindOfShape::CYLINDER2D:
1785         aTypeInfo = "Cylindrical Face";
1786         break;
1787       case GEOM::GEOM_IKindOfShape::TORUS2D:
1788         aTypeInfo = "Toroidal Face";
1789         break;
1790       case GEOM::GEOM_IKindOfShape::CONE2D:
1791         aTypeInfo = "Conical Face";
1792         break;
1793       case GEOM::GEOM_IKindOfShape::DISK_CIRCLE:
1794         aTypeInfo = "Disk";
1795         break;
1796       case GEOM::GEOM_IKindOfShape::DISK_ELLIPSE:
1797         aTypeInfo = "Elliptical Face";
1798         break;
1799       case GEOM::GEOM_IKindOfShape::POLYGON:
1800         aTypeInfo = "Polygon";
1801         break;
1802       case GEOM::GEOM_IKindOfShape::PLANE:
1803         aTypeInfo = "Plane";
1804         break;
1805       case GEOM::GEOM_IKindOfShape::PLANAR:
1806         aTypeInfo = "Planar Face";
1807         break;
1808       case GEOM::GEOM_IKindOfShape::FACE:
1809         aTypeInfo = "Face";
1810         break;
1811         // EDGEs
1812       case GEOM::GEOM_IKindOfShape::CIRCLE:
1813         aTypeInfo = "Circle";
1814         break;
1815       case GEOM::GEOM_IKindOfShape::ARC_CIRCLE:
1816         aTypeInfo = "Ark";
1817         break;
1818       case GEOM::GEOM_IKindOfShape::ELLIPSE:
1819         aTypeInfo = "Ellipse";
1820         break;
1821       case GEOM::GEOM_IKindOfShape::ARC_ELLIPSE:
1822         aTypeInfo = "Arc Ellipse";
1823         break;
1824       case GEOM::GEOM_IKindOfShape::LINE:
1825         aTypeInfo = "Line";
1826         break;
1827       case GEOM::GEOM_IKindOfShape::SEGMENT:
1828         aTypeInfo = "Segment";
1829         break;
1830       case GEOM::GEOM_IKindOfShape::EDGE:
1831         aTypeInfo = "Edge";
1832         break;
1833       case GEOM::GEOM_IKindOfShape::VERTEX:
1834         aTypeInfo = "Vertex";
1835         break;
1836       default:
1837         break;
1838       }
1839     }
1840   }
1841     
1842   char* anInfo = new char[strlen("Module ") + strlen(ComponentDataType()) + strlen(", ") + strlen(aTypeInfo) + 3];
1843   sprintf(anInfo, "Module %s, %s", ComponentDataType(), aTypeInfo);
1844   
1845   char* ret = CORBA::string_dup(anInfo);
1846   delete [] anInfo;
1847   return ret;
1848 }
1849
1850 //=====================================================================================
1851 // EXPORTED METHODS
1852 //=====================================================================================
1853 extern "C"
1854 {
1855   /*
1856   GEOM_I_EXPORT
1857   PortableServer::ObjectId* GEOMEngine_factory(CORBA::ORB*, PortableServer::POA*, PortableServer::ObjectId*, const char*, const char*);
1858   */
1859   
1860   GEOM_I_EXPORT
1861   PortableServer::ObjectId* GEOMEngine_factory(CORBA::ORB_ptr            orb,
1862                                                PortableServer::POA_ptr   poa,
1863                                                PortableServer::ObjectId* contId,
1864                                                const char*               instanceName,
1865                                                const char*               interfaceName)
1866   {
1867     GEOM_Gen_i* myGEOM_Gen_i = new GEOM_Gen_i(orb, poa, contId, instanceName, interfaceName);
1868     return myGEOM_Gen_i->getId();
1869   }
1870 }