Salome HOME
Merge from V6_main 06/03/2013
[modules/smesh.git] / src / SMESH_I / SMESH_DumpPython.cxx
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 //  File    : SMESH_DumpPython.cxx
23 //  Created : Thu Mar 24 17:17:59 2005
24 //  Author  : Julia DOROVSKIKH
25 //  Module  : SMESH
26
27 #include "SMESH_PythonDump.hxx"
28
29 #include "SMESH_2smeshpy.hxx"
30 #include "SMESH_Comment.hxx"
31 #include "SMESH_Filter_i.hxx"
32 #include "SMESH_Gen_i.hxx"
33 #include "SMESH_MeshEditor_i.hxx"
34
35 #include <SALOMEDS_wrap.hxx>
36
37 #include <TColStd_HSequenceOfInteger.hxx>
38 #include <TCollection_AsciiString.hxx>
39
40 #ifdef _DEBUG_
41 static int MYDEBUG = 0;
42 #else
43 static int MYDEBUG = 0;
44 #endif
45
46 static TCollection_AsciiString NotPublishedObjectName()
47 {
48   return "__NOT__Published__Object__";
49 }
50
51 namespace SMESH
52 {
53
54   size_t TPythonDump::myCounter = 0;
55
56   TVar::TVar(CORBA::Double value):myVals(1) { myVals[0] = SMESH_Comment(value); }
57   TVar::TVar(CORBA::Long   value):myVals(1) { myVals[0] = SMESH_Comment(value); }
58   TVar::TVar(CORBA::Short  value):myVals(1) { myVals[0] = SMESH_Comment(value); }
59   TVar::TVar(const SMESH::double_array& value):myVals(value.length())
60   {
61     for ( size_t i = 0; i < value.length(); i++)
62       myVals[i] = SMESH_Comment(value[i]);
63   }
64
65   TPythonDump::
66   TPythonDump():myVarsCounter(0)
67   {
68     ++myCounter;
69   }
70   TPythonDump::
71   ~TPythonDump()
72   {
73     if(--myCounter == 0){
74       SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
75       std::string aString = myStream.str();
76       TCollection_AsciiString aCollection(Standard_CString(aString.c_str()));
77       SALOMEDS::Study_var aStudy = aSMESHGen->GetCurrentStudy();
78       if(!aStudy->_is_nil() && !aCollection.IsEmpty()){
79         aSMESHGen->AddToPythonScript(aStudy->StudyId(),aCollection);
80         if(MYDEBUG) MESSAGE(aString);
81         aSMESHGen->UpdateParameters(""); // prevent misuse of already treated variables
82       }
83     }
84   }
85
86   TPythonDump& //!< to store a variable value
87   TPythonDump::
88   operator<<(const TVar& theVarValue)
89   {
90     const std::vector< std::string >& varNames = SMESH_Gen_i::GetSMESHGen()->GetLastParameters();
91     if ( theVarValue.myVals.size() != 1 )
92     {
93       myStream << "[ ";
94       for ( size_t i = 1; i <= theVarValue.myVals.size(); ++i )
95       {
96         if ( myVarsCounter < varNames.size() && !varNames[ myVarsCounter ].empty() )
97           myStream << TVar::Quote() << varNames[ myVarsCounter ] << TVar::Quote();
98         else
99           myStream << theVarValue.myVals[i-1];
100         if ( i < theVarValue.myVals.size() )
101           myStream << ", ";
102         ++myVarsCounter;
103       }
104       myStream << " ]";
105     }
106     else
107     {
108       if ( myVarsCounter < varNames.size() && !varNames[ myVarsCounter ].empty() )
109         myStream << TVar::Quote() << varNames[ myVarsCounter ] << TVar::Quote();
110       else
111         myStream << theVarValue.myVals[0];
112       ++myVarsCounter;
113     }
114     return *this;
115   }
116
117   TPythonDump&
118   TPythonDump::
119   operator<<(long int theArg){
120     myStream<<theArg;
121     return *this;
122   }
123
124   TPythonDump&
125   TPythonDump::
126   operator<<(int theArg){
127     myStream<<theArg;
128     return *this;
129   }
130
131   TPythonDump&
132   TPythonDump::
133   operator<<(double theArg){
134     myStream<<theArg;
135     return *this;
136   }
137
138   TPythonDump&
139   TPythonDump::
140   operator<<(float theArg){
141     myStream<<theArg;
142     return *this;
143   }
144
145   TPythonDump&
146   TPythonDump::
147   operator<<(const void* theArg){
148     myStream<<theArg;
149     return *this;
150   }
151
152   TPythonDump&
153   TPythonDump::
154   operator<<(const char* theArg){
155     if ( theArg )
156       myStream<<theArg;
157     return *this;
158   }
159
160   TPythonDump&
161   TPythonDump::
162   operator<<(const SMESH::ElementType& theArg)
163   {
164     myStream<<"SMESH.";
165     switch(theArg){
166     case ALL:    myStream<<"ALL";    break;
167     case NODE:   myStream<<"NODE";   break;
168     case EDGE:   myStream<<"EDGE";   break;
169     case FACE:   myStream<<"FACE";   break;
170     case VOLUME: myStream<<"VOLUME"; break;
171     case ELEM0D: myStream<<"ELEM0D"; break;
172     case BALL:   myStream<<"BALL";   break;
173     default:     myStream<<"__UNKNOWN__ElementType: " << theArg;
174     }
175     return *this;
176   }
177
178   TPythonDump&
179   TPythonDump::
180   operator<<(const SMESH::GeometryType& theArg)
181   {
182     myStream<<"SMESH.";
183     switch(theArg){
184     case Geom_POINT:      myStream<<"Geom_POINT";      break;
185     case Geom_EDGE:       myStream<<"Geom_EDGE";       break;
186     case Geom_TRIANGLE:   myStream<<"Geom_TRIANGLE";   break;
187     case Geom_QUADRANGLE: myStream<<"Geom_QUADRANGLE"; break;
188     case Geom_POLYGON:    myStream<<"Geom_POLYGON";    break;
189     case Geom_TETRA:      myStream<<"Geom_TETRA";      break;
190     case Geom_PYRAMID:    myStream<<"Geom_PYRAMID";    break;
191     case Geom_HEXA:       myStream<<"Geom_HEXA";       break;
192     case Geom_PENTA:      myStream<<"Geom_PENTA";      break;
193     case Geom_POLYHEDRA:  myStream<<"Geom_POLYHEDRA";  break;
194     case Geom_BALL:       myStream<<"Geom_BALL";       break;
195     default:    myStream<<"__UNKNOWN__GeometryType: " << theArg;
196     }
197     return *this;
198   }
199   TPythonDump&
200   TPythonDump::
201   operator<<(const SMESH::EntityType& theArg)
202   {
203     myStream<<"SMESH.";
204     switch(theArg){
205     case Entity_0D:                myStream<<"Entity_0D";                break;
206     case Entity_Edge:              myStream<<"Entity_Edge";              break;
207     case Entity_Quad_Edge:         myStream<<"Entity_Quad_Edge";         break;
208     case Entity_Triangle:          myStream<<"Entity_Triangle";          break;
209     case Entity_Quad_Triangle:     myStream<<"Entity_Quad_Triangle";     break;
210     case Entity_Quadrangle:        myStream<<"Entity_Quadrangle";        break;
211     case Entity_Quad_Quadrangle:   myStream<<"Entity_Quad_Quadrangle";   break;
212     case Entity_BiQuad_Quadrangle: myStream<<"Entity_BiQuad_Quadrangle"; break;
213     case Entity_Polygon:           myStream<<"Entity_Polygon";           break;
214     case Entity_Quad_Polygon:      myStream<<"Entity_Quad_Polygon";      break;
215     case Entity_Tetra:             myStream<<"Entity_Tetra";             break;
216     case Entity_Quad_Tetra:        myStream<<"Entity_Quad_Tetra";        break;
217     case Entity_Pyramid:           myStream<<"Entity_Pyramid";           break;
218     case Entity_Quad_Pyramid:      myStream<<"Entity_Quad_Pyramid";      break;
219     case Entity_Hexa:              myStream<<"Entity_Hexa";              break;
220     case Entity_Quad_Hexa:         myStream<<"Entity_Quad_Hexa";         break;
221     case Entity_TriQuad_Hexa:      myStream<<"Entity_TriQuad_Hexa";      break;
222     case Entity_Penta:             myStream<<"Entity_Penta";             break;
223     case Entity_Quad_Penta:        myStream<<"Entity_Quad_Penta";        break;
224     case Entity_Hexagonal_Prism:   myStream<<"Entity_Hexagonal_Prism";   break;
225     case Entity_Polyhedra:         myStream<<"Entity_Polyhedra";         break;
226     case Entity_Quad_Polyhedra:    myStream<<"Entity_Quad_Polyhedra";    break;
227     case Entity_Ball:              myStream<<"Entity_Ball";              break;
228     case Entity_Last:              myStream<<"Entity_Last";              break;
229     default:    myStream<<"__UNKNOWN__EntityType: " << theArg;
230     }
231     return *this;
232   }
233
234   template<class TArray>
235   void DumpArray(const TArray& theArray, TPythonDump & theStream)
236   {
237     theStream << "[ ";
238     for (int i = 1; i <= theArray.length(); i++) {
239       theStream << theArray[i-1];
240       if ( i < theArray.length() )
241         theStream << ", ";
242     }
243     theStream << " ]";
244   }
245
246   TPythonDump&
247   TPythonDump::operator<<(const SMESH::long_array& theArg)
248   {
249     DumpArray( theArg, *this );
250     return *this;
251   }
252
253   TPythonDump&
254   TPythonDump::operator<<(const SMESH::double_array& theArg)
255   {
256     DumpArray( theArg, *this );
257     return *this;
258   }
259
260   TPythonDump&
261   TPythonDump::operator<<(const SMESH::string_array& theArray)
262   {
263     myStream << "[ ";
264     for (int i = 1; i <= theArray.length(); i++) {
265       myStream << "'" << theArray[i-1] << "'";
266       if ( i < theArray.length() )
267         myStream << ", ";
268     }
269     myStream << " ]";
270     return *this;
271   }
272
273   TPythonDump&
274   TPythonDump::
275   operator<<(SALOMEDS::SObject_ptr aSObject)
276   {
277     if ( !aSObject->_is_nil() ) {
278       CORBA::String_var entry = aSObject->GetID();
279       myStream << entry.in();
280     }
281     else {
282       myStream << NotPublishedObjectName();
283     }
284     return *this;
285   }
286
287   TPythonDump&
288   TPythonDump::
289   operator<<(CORBA::Object_ptr theArg)
290   {
291     SMESH_Gen_i*          aSMESHGen = SMESH_Gen_i::GetSMESHGen();
292     SALOMEDS::Study_var      aStudy = aSMESHGen->GetCurrentStudy();
293     SALOMEDS::SObject_wrap aSObject = SMESH_Gen_i::ObjectToSObject(aStudy,theArg);
294     if(!aSObject->_is_nil()) {
295       CORBA::String_var id = aSObject->GetID();
296       myStream << id;
297     } else if ( !CORBA::is_nil(theArg)) {
298       if ( aSMESHGen->CanPublishInStudy( theArg )) // not published SMESH object
299         myStream << "smeshObj_" << size_t(theArg);
300       else
301         myStream << NotPublishedObjectName();
302     }
303     else
304       myStream << "None";
305     return *this;
306   }
307
308   TPythonDump&
309   TPythonDump::
310   operator<<(SMESH::SMESH_Hypothesis_ptr theArg)
311   {
312     SALOMEDS::Study_var     aStudy = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
313     SALOMEDS::SObject_wrap aSObject = SMESH_Gen_i::ObjectToSObject(aStudy,theArg);
314     if(aSObject->_is_nil() && !CORBA::is_nil(theArg))
315       myStream << "hyp_" << theArg->GetId();
316     else
317       *this << CORBA::Object_ptr( theArg );
318     return *this;
319   }
320
321   TPythonDump&
322   TPythonDump::
323   operator<<(SMESH::SMESH_IDSource_ptr theArg)
324   {
325     if ( CORBA::is_nil( theArg ) )
326       return *this << "None";
327     SMESH_Gen_i*          aSMESHGen = SMESH_Gen_i::GetSMESHGen();
328     SALOMEDS::Study_var      aStudy = aSMESHGen->GetCurrentStudy();
329     SALOMEDS::SObject_wrap aSObject = SMESH_Gen_i::ObjectToSObject(aStudy,theArg);
330     if(!aSObject->_is_nil())
331     {
332       return *this << aSObject;
333     }
334     if ( SMESH::Filter_i* filter = SMESH::DownCast<SMESH::Filter_i*>( theArg ))
335     {
336       return *this << filter;
337     }
338     if ( SMESH_MeshEditor_i::IsTemporaryIDSource( theArg ))
339     {
340       SMESH::SMESH_Mesh_var            mesh = theArg->GetMesh();
341       SMESH::long_array_var    anElementsId = theArg->GetIDs();
342       SMESH::array_of_ElementType_var types = theArg->GetTypes();
343       SMESH::ElementType type = types->length() ? types[0] : SMESH::ALL;
344       return *this << mesh << ".GetIDSource(" << anElementsId << ", " << type << ")";
345     }
346     return *this;
347   }
348
349   TPythonDump&
350   TPythonDump::
351   operator<<(SMESH::FilterLibrary_i* theArg)
352   {
353     myStream<<"aFilterLibrary"<<theArg;
354     return *this;
355   }
356
357   TPythonDump&
358   TPythonDump::
359   operator<<(SMESH::FilterManager_i* theArg)
360   {
361     myStream<<"aFilterManager";
362     return *this;
363   }
364
365   TPythonDump&
366   TPythonDump::
367   operator<<(SMESH::Filter_i* theArg)
368   {
369     myStream<<"aFilter"<<theArg;
370     return *this;
371   }
372
373   TPythonDump&
374   TPythonDump::
375   operator<<(SMESH::Functor_i* theArg)
376   {
377     if ( theArg ) {
378       FunctorType aFunctorType = theArg->GetFunctorType();
379       switch(aFunctorType){
380       case FT_AspectRatio:           myStream<< "anAspectRatio";          break;
381       case FT_AspectRatio3D:         myStream<< "anAspectRatio3D";        break;
382       case FT_Warping:               myStream<< "aWarping";               break;
383       case FT_MinimumAngle:          myStream<< "aMinimumAngle";          break;
384       case FT_Taper:                 myStream<< "aTaper";                 break;
385       case FT_Skew:                  myStream<< "aSkew";                  break;
386       case FT_Area:                  myStream<< "aArea";                  break;
387       case FT_Volume3D:              myStream<< "aVolume3D";              break;
388       case FT_MaxElementLength2D:    myStream<< "aMaxElementLength2D";    break;
389       case FT_MaxElementLength3D:    myStream<< "aMaxElementLength3D";    break;
390       case FT_FreeBorders:           myStream<< "aFreeBorders";           break;
391       case FT_FreeEdges:             myStream<< "aFreeEdges";             break;
392       case FT_FreeNodes:             myStream<< "aFreeNodes";             break;
393       case FT_FreeFaces:             myStream<< "aFreeFaces";             break;
394       case FT_MultiConnection:       myStream<< "aMultiConnection";       break;
395       case FT_MultiConnection2D:     myStream<< "aMultiConnection2D";     break;
396       case FT_Length:                myStream<< "aLength";                break;
397       case FT_Length2D:              myStream<< "aLength2D";              break;
398       case FT_BelongToGeom:          myStream<< "aBelongToGeom";          break;
399       case FT_BelongToPlane:         myStream<< "aBelongToPlane";         break;
400       case FT_BelongToCylinder:      myStream<< "aBelongToCylinder";      break;
401       case FT_BelongToGenSurface:    myStream<< "aBelongToGenSurface";    break;
402       case FT_LyingOnGeom:           myStream<< "aLyingOnGeom";           break;
403       case FT_CoplanarFaces:         myStream<< "aCoplanarFaces";         break;
404       case FT_RangeOfIds:            myStream<< "aRangeOfIds";            break;
405       case FT_BadOrientedVolume:     myStream<< "aBadOrientedVolume";     break;
406       case FT_BareBorderVolume:      myStream<< "aBareBorderVolume";      break;
407       case FT_BareBorderFace:        myStream<< "aBareBorderFace";        break;
408       case FT_OverConstrainedVolume: myStream<< "aOverConstrainedVolume"; break;
409       case FT_OverConstrainedFace:   myStream<< "aOverConstrainedFace";   break;
410       case FT_LinearOrQuadratic:     myStream<< "aLinearOrQuadratic";     break;
411       case FT_GroupColor:            myStream<< "aGroupColor";            break;
412       case FT_ElemGeomType:          myStream<< "anElemGeomType";         break;
413       case FT_LessThan:              myStream<< "aLessThan";              break;
414       case FT_MoreThan:              myStream<< "aMoreThan";              break;
415       case FT_EqualTo:               myStream<< "anEqualTo";              break;
416       case FT_LogicalNOT:            myStream<< "aLogicalNOT";            break;
417       case FT_LogicalAND:            myStream<< "aLogicalAND";            break;
418       case FT_LogicalOR:             myStream<< "aLogicalOR";             break;
419       case FT_Undefined:
420       default:                       myStream<< "anUndefined";            break;
421       }
422       myStream<<theArg;
423     }
424     return *this;
425   }
426
427   TPythonDump&
428   TPythonDump::
429   operator<<(SMESH::Measurements_i* theArg)
430   {
431     myStream<<"aMeasurements";
432     return *this;
433   }
434
435
436   TPythonDump& TPythonDump:: operator<<(SMESH_Gen_i* theArg)
437   {
438     myStream << SMESHGenName(); return *this;
439   }
440
441   TPythonDump& TPythonDump::operator<<(SMESH_MeshEditor_i* theArg)
442   {
443     myStream << MeshEditorName() << "_" << ( theArg ? theArg->GetMeshId() : -1 ); return *this;
444   }
445
446   TPythonDump& TPythonDump::operator<<(const TCollection_AsciiString & theStr)
447   {
448     myStream << theStr; return *this;
449   }
450
451
452   TPythonDump& TPythonDump::operator<<(SMESH::MED_VERSION theVersion)
453   {
454     switch (theVersion) {
455     case SMESH::MED_V2_1: myStream << "SMESH.MED_V2_1"; break;
456     case SMESH::MED_V2_2: myStream << "SMESH.MED_V2_2"; break;
457     default: myStream << theVersion;
458     }
459     return *this;
460   }
461
462   TPythonDump& TPythonDump::operator<<(const SMESH::AxisStruct & theAxis)
463   {
464     *this << "SMESH.AxisStruct( "
465           << TVar( theAxis.x  ) << ", "
466           << TVar( theAxis.y  ) << ", "
467           << TVar( theAxis.z  ) << ", "
468           << TVar( theAxis.vx ) << ", "
469           << TVar( theAxis.vy ) << ", "
470           << TVar( theAxis.vz ) << " )";
471     return *this;
472   }
473
474   TPythonDump& TPythonDump::operator<<(const SMESH::DirStruct & theDir)
475   {
476     const SMESH::PointStruct & P = theDir.PS;
477     *this << "SMESH.DirStruct( SMESH.PointStruct ( "
478           << TVar( P.x ) << ", "
479           << TVar( P.y ) << ", "
480           << TVar( P.z ) << " ))";
481     return *this;
482   }
483
484   TPythonDump& TPythonDump::operator<<(const SMESH::PointStruct & P)
485   {
486     *this << "SMESH.PointStruct ( "
487           << TVar( P.x ) << ", "
488           << TVar( P.y ) << ", "
489           << TVar( P.z ) << " )";
490     return *this;
491   }
492
493   TPythonDump& TPythonDump::operator<<(const SMESH::ListOfGroups& theList)
494   {
495     DumpArray( theList, *this );
496     return *this;
497   }
498   TPythonDump& TPythonDump::operator<<(const SMESH::ListOfGroups * theList)
499   {
500     DumpArray( *theList, *this );
501     return *this;
502   }
503   TPythonDump& TPythonDump::operator<<(const SMESH::ListOfIDSources& theList)
504   {
505     DumpArray( theList, *this );
506     return *this;
507   }
508
509   TCollection_AsciiString myLongStringStart( "TPythonDump::LongStringStart" );
510   TCollection_AsciiString myLongStringEnd  ( "TPythonDump::LongStringEnd" );
511
512   //================================================================================
513   /*!
514    * \brief Return marker of long string literal beginning
515    * \param type - a name of functionality producing the string literal
516    * \retval TCollection_AsciiString - the marker string to be written into
517    * a raw python script
518    */
519   //================================================================================
520
521   TCollection_AsciiString TPythonDump::LongStringStart(const char* type)
522   {
523     return
524       myLongStringStart +
525       (Standard_Integer) strlen(type) +
526       " " +
527       (char*) type;
528   }
529
530   //================================================================================
531   /*!
532      * \brief Return marker of long string literal end
533       * \retval TCollection_AsciiString - the marker string to be written into
534       * a raw python script
535    */
536   //================================================================================
537
538   TCollection_AsciiString TPythonDump::LongStringEnd()
539   {
540     return myLongStringEnd;
541   }
542
543   //================================================================================
544   /*!
545      * \brief Cut out a long string literal from a string
546       * \param theText - text possibly containing string literals
547       * \param theFrom - position in the text to search from
548       * \param theLongString - the retrieved literal
549       * \param theStringType - a name of functionality produced the literal
550       * \retval bool - true if a string literal found
551      *
552      * The literal is removed from theText; theFrom points position right after
553      * the removed literal
554    */
555   //================================================================================
556
557   bool  TPythonDump::CutoutLongString( TCollection_AsciiString & theText,
558                                        int                     & theFrom,
559                                        TCollection_AsciiString & theLongString,
560                                        TCollection_AsciiString & theStringType)
561   {
562     if ( theFrom < 1 || theFrom > theText.Length() )
563       return false;
564
565     // ...script \  beg marker    \ \ type \       literal              \  end marker  \ script...
566     //  "theText myLongStringStart7 Pattern!!! SALOME Mesh Pattern file myLongStringEndtextEnd"
567     //  012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
568     //  0         1         2         3         4         5         6         7         8
569
570     theFrom = theText.Location( myLongStringStart, theFrom, theText.Length() ); // = 09
571     if ( !theFrom )
572       return false;
573
574     // find where literal begins
575     int literalBeg = theFrom + myLongStringStart.Length(); // = 26
576     char* typeLenStr = (char*) theText.ToCString() + literalBeg - 1; // = "7 Pattern!!! SALO...."
577     int typeLen = atoi ( typeLenStr ); // = 7
578     while ( *typeLenStr != ' ' ) { // look for ' ' after typeLen
579       literalBeg++; // 26 -> 27
580       typeLenStr++;
581     }
582     literalBeg += typeLen + 1; // = 35
583     if ( literalBeg > theText.Length() )
584       return false;
585
586     // where literal ends (i.e. end marker begins)
587     int literalEnd = theText.Location( myLongStringEnd, literalBeg, theText.Length() ); // = 64
588     if ( !literalEnd )
589       literalEnd = theText.Length();
590
591     // literal
592     theLongString = theText.SubString( literalBeg, literalEnd - 1); // "!!! SALOME Mesh Pattern file "
593     // type
594     theStringType = theText.SubString( literalBeg - typeLen, literalBeg - 1 ); // "Pattern"
595     // cut off literal
596     literalEnd += myLongStringEnd.Length(); // = 79
597     TCollection_AsciiString textEnd = theText.SubString( literalEnd, theText.Length() ); // "textE..."
598     theText = theText.SubString( 1, theFrom - 1 ) + textEnd;
599
600     return true;
601   }
602 }
603
604 //=======================================================================
605 //function : RemoveTabulation
606 //purpose  : 
607 //=======================================================================
608 void RemoveTabulation( TCollection_AsciiString& theScript )
609 {
610   std::string aString( theScript.ToCString() );
611   std::string::size_type aPos = 0;
612   while( aPos < aString.length() )
613   {
614     aPos = aString.find( "\n\t", aPos );
615     if( aPos == std::string::npos )
616       break;
617     aString.replace( aPos, 2, "\n" );
618     aPos++;
619   }
620   theScript = aString.c_str();
621 }
622
623 //=======================================================================
624 //function : DumpPython
625 //purpose  :
626 //=======================================================================
627 Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
628                                            CORBA::Boolean isPublished,
629                                            CORBA::Boolean isMultiFile,
630                                            CORBA::Boolean& isValidScript)
631 {
632   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
633   if (CORBA::is_nil(aStudy))
634     return new Engines::TMPFile(0);
635
636   CORBA::String_var compDataType = ComponentDataType();
637   SALOMEDS::SObject_wrap aSO = aStudy->FindComponent( compDataType.in() );
638   if (CORBA::is_nil(aSO))
639     return new Engines::TMPFile(0);
640
641   // Map study entries to object names
642   Resource_DataMapOfAsciiStringAsciiString aMap;
643   Resource_DataMapOfAsciiStringAsciiString aMapNames;
644   //TCollection_AsciiString s ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
645
646   SALOMEDS::ChildIterator_wrap Itr = aStudy->NewChildIterator(aSO);
647   for (Itr->InitEx(true); Itr->More(); Itr->Next()) {
648     SALOMEDS::SObject_wrap aValue = Itr->Value();
649     CORBA::String_var anID = aValue->GetID();
650     CORBA::String_var aName = aValue->GetName();
651     TCollection_AsciiString aGUIName ( (char*) aName.in() );
652     TCollection_AsciiString anEnrty ( (char*) anID.in() );
653     if (aGUIName.Length() > 0) {
654       aMapNames.Bind( anEnrty, aGUIName );
655       aMap.Bind( anEnrty, aGUIName );
656     }
657   }
658
659   // Get trace of restored study
660   //SALOMEDS::SObject_wrap aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
661   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
662   SALOMEDS::GenericAttribute_wrap anAttr =
663     aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
664
665   SALOMEDS::AttributePythonObject_var pyAttr =
666     SALOMEDS::AttributePythonObject::_narrow(anAttr);
667   CORBA::String_var oldValue = pyAttr->GetObject();
668   TCollection_AsciiString aSavedTrace (oldValue.in());
669
670   // Add trace of API methods calls and replace study entries by names
671   TCollection_AsciiString aScript;
672   aScript += DumpPython_impl(aStudy, aMap, aMapNames, isPublished, isMultiFile,
673                              myIsHistoricalPythonDump, isValidScript, aSavedTrace);
674
675   int aLen = aScript.Length();
676   unsigned char* aBuffer = new unsigned char[aLen+1];
677   strcpy((char*)aBuffer, aScript.ToCString());
678
679   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
680   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
681
682   bool hasNotPublishedObjects = aScript.Location( NotPublishedObjectName(), 1, aLen);
683   isValidScript = isValidScript && !hasNotPublishedObjects;
684
685   return aStreamFile._retn();
686 }
687
688 //=============================================================================
689 /*!
690  *  AddToPythonScript
691  */
692 //=============================================================================
693 void SMESH_Gen_i::AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString)
694 {
695   if (myPythonScripts.find(theStudyID) == myPythonScripts.end()) {
696     myPythonScripts[theStudyID] = new TColStd_HSequenceOfAsciiString;
697   }
698   myPythonScripts[theStudyID]->Append(theString);
699 }
700
701 //=============================================================================
702 /*!
703  *  RemoveLastFromPythonScript
704  */
705 //=============================================================================
706 void SMESH_Gen_i::RemoveLastFromPythonScript (int theStudyID)
707 {
708   if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
709     int aLen = myPythonScripts[theStudyID]->Length();
710     myPythonScripts[theStudyID]->Remove(aLen);
711   }
712 }
713
714 //=======================================================================
715 //function : SavePython
716 //purpose  :
717 //=======================================================================
718 void SMESH_Gen_i::SavePython (SALOMEDS::Study_ptr theStudy)
719 {
720   // Dump trace of API methods calls
721   TCollection_AsciiString aScript = GetNewPythonLines(theStudy->StudyId());
722
723   // Check contents of PythonObject attribute
724   CORBA::String_var compDataType = ComponentDataType();
725   SALOMEDS::SObject_wrap aSO = theStudy->FindComponent( compDataType.in() );
726   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
727   SALOMEDS::GenericAttribute_wrap anAttr =
728     aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
729
730   SALOMEDS::AttributePythonObject_var pyAttr =
731     SALOMEDS::AttributePythonObject::_narrow(anAttr);
732   CORBA::String_var oldValue = pyAttr->GetObject();
733   TCollection_AsciiString oldScript (oldValue.in());
734
735   if (oldScript.Length() > 0) {
736     oldScript += "\n";
737     oldScript += aScript;
738   } else {
739     oldScript = aScript;
740   }
741
742   // Store in PythonObject attribute
743   pyAttr->SetObject(oldScript.ToCString(), 1);
744
745   // Clean trace of API methods calls
746   CleanPythonTrace(theStudy->StudyId());
747 }
748
749
750 // impl
751
752
753 //=============================================================================
754 /*!
755  *  FindEntries: Returns a sequence of start/end positions of entries in the string
756  */
757 //=============================================================================
758 Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theString)
759 {
760   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
761   Standard_Integer aLen = theString.Length();
762   Standard_Boolean isFound = Standard_False;
763
764   char* arr = (char*) theString.ToCString();
765   Standard_Integer i = 0, j;
766
767   while(i < aLen) {
768     int c = (int)arr[i];
769     j = i+1;
770     if ( isdigit( c )) { //Is digit?
771
772       isFound = Standard_False;
773       while((j < aLen) && ( isdigit(c) || c == ':' )) { //Check if it is an entry
774         c = (int)arr[j++];
775         if(c == ':') isFound = Standard_True;
776       }
777
778       if (isFound) {
779         int prev = (i < 1) ? 0 : (int)arr[i - 1];
780         // to distinguish from a sketcher command:
781         // last char should be a digit, not ":",
782         // previous char should not be '"'.
783         if (arr[j-2] != ':' && prev != '"') {
784           aSeq->Append(i+1); // +1 because AsciiString starts from 1
785           aSeq->Append(j-1);
786         }
787       }
788     }
789
790     i = j;
791   }
792
793   return aSeq;
794 }
795
796 namespace {
797
798   //================================================================================
799   /*!
800    * \brief Make a string be a valid python name
801     * \param aName - a string to fix
802     * \retval bool - true if aName was not modified
803    */
804   //================================================================================
805
806   bool fixPythonName(TCollection_AsciiString & aName )
807   {
808     const TCollection_AsciiString allowedChars =
809       "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_";
810     bool isValidName = true;
811     int nbUnderscore = 0;
812     int p=1; // replace not allowed chars by underscore
813     while (p <= aName.Length() &&
814            (p = aName.FirstLocationNotInSet(allowedChars, p, aName.Length())))
815     {
816       if ( p == 1 || p == aName.Length() || aName.Value(p-1) == '_')
817         aName.Remove( p, 1 ); // remove double _ from the start and the end
818       else
819         aName.SetValue(p, '_'), nbUnderscore++;
820       isValidName = false;
821     }
822     if ( aName.IsIntegerValue() ) { // aName must not start with a digit
823       aName.Insert( 1, 'a' );
824       isValidName = false;
825     }
826     // shorten names like CartesianParameters3D_400_400_400_1000000_1
827     const int nbAllowedUnderscore = 3; /* changed from 2 to 3 by an user request
828                                           posted to SALOME Forum */
829     if ( aName.Length() > 20 && nbUnderscore > nbAllowedUnderscore )
830     {
831       p = aName.Location( "_", 20, aName.Length());
832       if ( p > 1 )
833         aName.Trunc( p-1 );
834     }
835     return isValidName;
836   }
837 }
838
839 //=============================================================================
840 /*!
841  *  DumpPython
842  */
843 //=============================================================================
844 TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
845                         (SALOMEDS::Study_ptr                       theStudy,
846                          Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
847                          Resource_DataMapOfAsciiStringAsciiString& theNames,
848                          bool                                      isPublished,
849                          bool                                      isMultiFile,
850                          bool                                      isHistoricalDump,
851                          bool&                                     aValidScript,
852                          const TCollection_AsciiString&            theSavedTrace)
853 {
854   int aStudyID = theStudy->StudyId();
855
856   TCollection_AsciiString helper; // to comfortably concatenate C strings
857   TCollection_AsciiString aSmeshpy( SMESH_2smeshpy::SmeshpyName() );
858   TCollection_AsciiString aSMESHGen( SMESH_2smeshpy::GenName() );
859   TCollection_AsciiString anOldGen( SMESH::TPythonDump::SMESHGenName() );
860
861   TCollection_AsciiString aScript;
862   if( isMultiFile )
863     aScript += "def RebuildData(theStudy):";
864   aScript += "\n\t";
865   aScript += helper + "aFilterManager = " + aSMESHGen + ".CreateFilterManager()\n\t";
866   aScript += helper + "aMeasurements = " + aSMESHGen + ".CreateMeasurements()\n\t";
867   if ( isPublished )
868     aScript += aSMESHGen + ".SetCurrentStudy(theStudy)";
869   else
870     aScript += aSMESHGen + ".SetCurrentStudy(None)";
871
872   // import python files corresponding to plugins
873   set<string> moduleNameSet;
874   map<string, GenericHypothesisCreator_i*>::iterator hyp_creator = myHypCreatorMap.begin();
875   for ( ; hyp_creator != myHypCreatorMap.end(); ++hyp_creator ) {
876     string moduleName = hyp_creator->second->GetModuleName();
877     bool newModule = moduleNameSet.insert( moduleName ).second;
878     if ( newModule )
879       aScript += helper + "\n\t" + "import " + (char*) moduleName.c_str();
880   }
881
882   // Dump trace of restored study
883   if (theSavedTrace.Length() > 0) {
884     // For the convertion of IDL API calls -> smesh.py API, "smesh" standing for SMESH_Gen
885     // was replaces with "smeshgen" (==TPythonDump::SMESHGenName()).
886     // Change "smesh" -> "smeshgen" in the trace saved before passage to smesh.py API
887     bool isNewVersion =
888       theSavedTrace.Location( anOldGen + ".", 1, theSavedTrace.Length() );
889     if ( !isNewVersion ) {
890       TCollection_AsciiString aSavedTrace( theSavedTrace );
891       TCollection_AsciiString aSmeshCall ( "smesh." ), gen( "gen" );
892       int beg, end = aSavedTrace.Length(), from = 1;
893       while ( from < end && ( beg = aSavedTrace.Location( aSmeshCall, from, end ))) {
894         char charBefore = ( beg == 1 ) ? ' ' : aSavedTrace.Value( beg - 1 );
895         if ( isspace( charBefore ) || charBefore == '=' ) { // "smesh." is not a part of a long word
896           aSavedTrace.Insert( beg + aSmeshCall.Length() - 1, gen );// "smesh" -> "smeshgen"
897           end += gen.Length();
898         }
899         from = beg + aSmeshCall.Length();
900       }
901       aScript += helper + "\n" + aSavedTrace;
902     }
903     else
904       // append a saved trace to the script
905       aScript += helper + "\n" + theSavedTrace;
906   }
907
908   // Dump trace of API methods calls
909   TCollection_AsciiString aNewLines = GetNewPythonLines(aStudyID);
910   if (aNewLines.Length() > 0) {
911     aScript += helper + "\n" + aNewLines;
912   }
913
914   // Convert IDL API calls into smesh.py API.
915   // Some objects are wrapped with python classes and
916   // Resource_DataMapOfAsciiStringAsciiString holds methods returning wrapped objects
917   Resource_DataMapOfAsciiStringAsciiString anEntry2AccessorMethod;
918   if ( !getenv("NO_2smeshpy_conversion"))
919     aScript = SMESH_2smeshpy::ConvertScript( aScript, anEntry2AccessorMethod,
920                                              theObjectNames, theStudy, isHistoricalDump );
921
922   // Replace characters used instead of quote marks to quote notebook variables
923   {
924     int pos = 1;
925     while (( pos = aScript.Location( 1, SMESH::TVar::Quote(), pos, aScript.Length() )))
926       aScript.SetValue( pos, '"' );
927   }
928
929   // Find entries to be replaced by names
930   Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
931   Standard_Integer aLen = aSeq->Length();
932
933   if (aLen == 0 && isMultiFile)
934     return aScript;
935
936   // Replace entries by the names
937   GEOM::GEOM_Gen_ptr geom = GetGeomEngine();
938   TColStd_SequenceOfAsciiString seqRemoved;
939   Resource_DataMapOfAsciiStringAsciiString mapRemoved;
940   Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
941   TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_");
942
943   // Collect names of GEOM objects to exclude same names of SMESH objects
944   GEOM::string_array_var aGeomNames = geom->GetAllDumpNames();
945   int ign = 0, nbgn = aGeomNames->length();
946   for (; ign < nbgn; ign++) {
947     aName = aGeomNames[ign];
948     theObjectNames.Bind(aName, "1");
949   }
950
951   bool importGeom = false;
952   for (Standard_Integer i = 1; i <= aLen; i += 2) {
953     anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i) - 1);
954     anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
955     // is a GEOM object?
956     aName = geom->GetDumpName( anEntry.ToCString() );
957     if (aName.IsEmpty()) {
958       // is a SMESH object
959       if (theObjectNames.IsBound(anEntry)) {
960         // The Object is in Study
961         aName = theObjectNames.Find(anEntry);
962         // check validity of aName
963         bool isValidName = fixPythonName( aName );
964         if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) {
965           // diff objects have same name - make a new name by appending a digit
966           TCollection_AsciiString aName2;
967           Standard_Integer i = 0;
968           do {
969             aName2 = aName + "_" + ++i;
970           } while (theObjectNames.IsBound(aName2) && anEntry != theObjectNames(aName2));
971           aName = aName2;
972           isValidName = false;
973         }
974         if ( !isValidName )
975           theObjectNames(anEntry) = aName;
976
977       } else {
978         // Removed Object
979         do {
980           aName = aBaseName + (++objectCounter);
981         } while (theObjectNames.IsBound(aName));
982         seqRemoved.Append(aName);
983         mapRemoved.Bind(anEntry, "1");
984         theObjectNames.Bind(anEntry, aName);
985       }
986       theObjectNames.Bind(aName, anEntry); // to detect same name of diff objects
987     }
988     else
989     {
990       importGeom = true;
991     }
992     anUpdatedScript += aName;
993     aStart = aSeq->Value(i + 1) + 1;
994   }
995
996   // set initial part of aSript
997   TCollection_AsciiString initPart = "import ";
998   if ( isMultiFile )
999     initPart += helper + "salome, ";
1000   initPart += aSmeshpy + ", SMESH, SALOMEDS\n";
1001   if ( importGeom && isMultiFile )
1002   {
1003     initPart += ("\n## import GEOM dump file ## \n"
1004                  "import string, os, sys, re\n"
1005                  "sys.path.insert( 0, os.path.dirname(__file__) )\n"
1006                  "exec(\"from \"+re.sub(\"SMESH$\",\"GEOM\",__name__)+\" import *\")\n");
1007   }
1008   anUpdatedScript.Insert ( 1, initPart );
1009
1010   // add final part of aScript
1011   if (aLen && aSeq->Value(aLen) < aScriptLength)
1012     anUpdatedScript += aScript.SubString(aSeq->Value(aLen) + 1, aScriptLength);
1013
1014   // Remove removed objects
1015   if ( seqRemoved.Length() > 0 ) {
1016     anUpdatedScript += "\n\t## some objects were removed";
1017     anUpdatedScript += "\n\taStudyBuilder = theStudy.NewBuilder()";
1018   }
1019   for (int ir = 1; ir <= seqRemoved.Length(); ir++) {
1020     anUpdatedScript += "\n\tSO = theStudy.FindObjectIOR(theStudy.ConvertObjectToIOR(";
1021     anUpdatedScript += seqRemoved.Value(ir);
1022     // for object wrapped by class of smesh.py
1023     anEntry = theObjectNames( seqRemoved.Value(ir) );
1024     if ( anEntry2AccessorMethod.IsBound( anEntry ) )
1025       anUpdatedScript += helper + "." + anEntry2AccessorMethod( anEntry );
1026     anUpdatedScript += "))\n\tif SO is not None: aStudyBuilder.RemoveObjectWithChildren(SO)";
1027   }
1028
1029   // Set object names
1030   anUpdatedScript += "\n\t## set object names";
1031 //   anUpdatedScript += "\n\t\tsmeshgui = salome.ImportComponentGUI(\"SMESH\")";
1032 //   anUpdatedScript += "\n\t\tsmeshgui.Init(theStudy._get_StudyId())";
1033 //   anUpdatedScript += "\n";
1034
1035   TCollection_AsciiString aGUIName;
1036   Resource_DataMapOfAsciiStringAsciiString mapEntries;
1037   for (Standard_Integer i = 1; i <= aLen; i += 2)
1038   {
1039     anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
1040     aName = geom->GetDumpName( anEntry.ToCString() );
1041     if (aName.IsEmpty() && // Not a GEOM object
1042         theNames.IsBound(anEntry) &&
1043         !mapEntries.IsBound(anEntry) && // Not yet processed
1044         !mapRemoved.IsBound(anEntry)) // Was not removed
1045     {
1046       aName = theObjectNames.Find(anEntry);
1047       aGUIName = theNames.Find(anEntry);
1048       mapEntries.Bind(anEntry, aName);
1049       anUpdatedScript += helper + "\n\t" + aSMESHGen + ".SetName(" + aName;
1050       if ( anEntry2AccessorMethod.IsBound( anEntry ) )
1051         anUpdatedScript += helper + "." + anEntry2AccessorMethod( anEntry );
1052       anUpdatedScript += helper + ", '" + aGUIName + "')";
1053     }
1054   }
1055
1056   // Issue 0021249: removed (a similar block is dumped by SALOMEDSImpl_Study)
1057   //anUpdatedScript += "\n\tif salome.sg.hasDesktop():";
1058   //anUpdatedScript += "\n\t\tsalome.sg.updateObjBrowser(0)";
1059
1060   // -----------------------------------------------------------------
1061   // store visual properties of displayed objects
1062   // -----------------------------------------------------------------
1063
1064   if (isPublished)
1065   {
1066     //Output the script that sets up the visual parameters.
1067     CORBA::String_var compDataType = ComponentDataType();
1068     char* script = theStudy->GetDefaultScript( compDataType.in(), "\t");
1069     if (script && strlen(script) > 0) {
1070       anUpdatedScript += "\n\n\t### Store presentation parameters of displayed objects\n";
1071       anUpdatedScript += script;
1072       CORBA::string_free(script);
1073     }
1074   }
1075
1076   if( isMultiFile )
1077     anUpdatedScript += "\n\tpass";
1078   anUpdatedScript += "\n";
1079
1080   if( !isMultiFile ) // remove unnecessary tabulation
1081     RemoveTabulation( anUpdatedScript );
1082
1083   // -----------------------------------------------------------------
1084   // put string literals describing patterns into separate functions
1085   // -----------------------------------------------------------------
1086
1087   TCollection_AsciiString aLongString, aFunctionType;
1088   int where = 1;
1089   set< string > functionNameSet;
1090   while ( SMESH::TPythonDump::CutoutLongString( anUpdatedScript, where, aLongString, aFunctionType ))
1091   {
1092     // make a python string literal
1093     aLongString.Prepend(":\n\treturn '''\n");
1094     aLongString += "\n\t'''\n\tpass\n";
1095
1096     TCollection_AsciiString functionName;
1097
1098     // check if the function returning this literal is already defined
1099     int posAlready = anUpdatedScript.Location( aLongString, where, anUpdatedScript.Length() );
1100     if ( posAlready ) // already defined
1101     {
1102       // find the function name
1103       int functBeg = posAlready;
1104       char* script = (char*) anUpdatedScript.ToCString() + posAlready - 1; // look at ":" after "def fuction()"
1105       while ( *script != ' ' ) {
1106         script--;
1107         functBeg--;
1108       }
1109       functBeg++; // do not take ' '
1110       posAlready--; // do not take ':'
1111       functionName = anUpdatedScript.SubString( functBeg, posAlready );
1112     }
1113     else // not defined yet
1114     {
1115       // find a unique function name
1116       fixPythonName( aFunctionType );
1117       Standard_Integer nb = 0;
1118       do functionName = aFunctionType + "_" + ( nb++ ) + "()";
1119       while ( !functionNameSet.insert( functionName.ToCString() ).second );
1120
1121       // define function
1122       TCollection_AsciiString funDef = helper + "def " + functionName + aLongString;
1123       if ( isMultiFile )
1124       {
1125         anUpdatedScript += helper + "\n\n" + funDef;
1126       }
1127       else
1128       {
1129         funDef += "\n\n";
1130         anUpdatedScript.Insert( 1, funDef);
1131         where += funDef.Length();
1132       }
1133     }
1134     anUpdatedScript.InsertBefore( where, functionName ); // call function
1135   }
1136
1137   aValidScript = true;
1138
1139   return anUpdatedScript;
1140 }
1141
1142 //=============================================================================
1143 /*!
1144  *  GetNewPythonLines
1145  */
1146 //=============================================================================
1147 TCollection_AsciiString SMESH_Gen_i::GetNewPythonLines (int theStudyID)
1148 {
1149   TCollection_AsciiString aScript;
1150
1151   // Dump trace of API methods calls
1152   if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
1153     Handle(TColStd_HSequenceOfAsciiString) aPythonScript = myPythonScripts[theStudyID];
1154     Standard_Integer istr, aLen = aPythonScript->Length();
1155     for (istr = 1; istr <= aLen; istr++) {
1156       aScript += "\n\t";
1157       aScript += aPythonScript->Value(istr);
1158     }
1159     aScript += "\n";
1160   }
1161
1162   return aScript;
1163 }
1164
1165 //=============================================================================
1166 /*!
1167  *  CleanPythonTrace
1168  */
1169 //=============================================================================
1170 void SMESH_Gen_i::CleanPythonTrace (int theStudyID)
1171 {
1172   TCollection_AsciiString aScript;
1173
1174   // Clean trace of API methods calls
1175   if (myPythonScripts.find(theStudyID) != myPythonScripts.end()) {
1176     myPythonScripts[theStudyID]->Clear();
1177   }
1178 }