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