Salome HOME
Merge from V6_5_BR 05/06/2012
[modules/smesh.git] / src / SMESH_I / SMESH_NoteBook.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File      : SMESH_NoteBook.cxx
21 // Author    : Roman NIKOLAEV
22 //
23 #include "SMESH_2smeshpy.hxx"
24 #include "SMESH_NoteBook.hxx"
25 #include "SMESH_Gen_i.hxx"
26 #include "SMESH_PythonDump.hxx"
27 #include "SMESH_Hypothesis_i.hxx"
28
29 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
30 #include <TColStd_SequenceOfAsciiString.hxx>
31 #include <TColStd_HSequenceOfInteger.hxx>
32
33 #include <vector>
34 #include <string>
35
36 #ifdef _DEBUG_
37 static int MYDEBUG = 0;
38 #else
39 static int MYDEBUG = 0;
40 #endif
41
42 using namespace std;
43
44
45 namespace
46 {
47   /*!
48    *  Set variable of the SMESH_ObjectStates from position to the _pyCommand
49    *  method as nbArg argument
50    */
51   void SetVariable(Handle(_pyCommand) theCommand,
52                    const SMESH_ObjectStates* theStates,
53                    int position, int theArgNb)
54   {
55     if(theStates->GetCurrectState().size() > position)
56       if(!theStates->GetCurrectState().at(position).IsEmpty())
57         theCommand->SetArg(theArgNb,theStates->GetCurrectState().at(position));
58   }
59 }
60
61 //================================================================================
62 /*!
63  * \brief Constructor
64  */
65 //================================================================================
66 SMESH_ObjectStates::SMESH_ObjectStates(TCollection_AsciiString theType)
67 {
68   _type = theType;
69   _dumpstate = 0;
70 }
71
72 //================================================================================
73 /*!
74  * \brief Destructor
75  */
76 //================================================================================
77 SMESH_ObjectStates::~SMESH_ObjectStates()
78 {
79 }
80
81 //================================================================================
82 /*!
83  * \brief Add new object state 
84  * \param theState - Object state (vector of notebook variable)
85  */
86 //================================================================================
87 void SMESH_ObjectStates::AddState(const TState &theState)
88 {
89   _states.push_back(theState);
90 }
91
92 //================================================================================
93 /*!
94  * \brief Return current object state
95  * \\retval state - Object state (vector of notebook variable)
96  */
97 //================================================================================
98 TState SMESH_ObjectStates::GetCurrectState() const
99 {
100   if(_states.size() > _dumpstate)
101     return _states[_dumpstate];
102   TState empty;
103   return empty;
104 }
105
106
107 //================================================================================
108 /*!
109  *
110  */
111 //================================================================================
112 TAllStates SMESH_ObjectStates::GetAllStates() const
113 {
114   return _states;
115 }
116
117 //================================================================================
118 /*!
119  *
120  */
121 //================================================================================
122 void SMESH_ObjectStates::IncrementState()
123 {
124   _dumpstate++;
125 }
126
127 //================================================================================
128 /*!
129  *
130  */
131 //================================================================================
132 TCollection_AsciiString SMESH_ObjectStates::GetObjectType() const{
133   return _type;
134 }
135
136
137 //================================================================================
138 /*!
139  * \brief Constructor
140  */
141 //================================================================================
142 LayerDistributionStates::LayerDistributionStates():
143   SMESH_ObjectStates("LayerDistribution")
144 {
145 }
146 //================================================================================
147 /*!
148  * \brief Destructor
149  */
150 //================================================================================
151 LayerDistributionStates::~LayerDistributionStates()
152 {
153 }
154
155
156 //================================================================================
157 /*!
158  * \brief AddDistribution
159  */
160 //================================================================================
161 void LayerDistributionStates::AddDistribution(const TCollection_AsciiString& theDistribution)
162 {
163   _distributions.insert(pair<TCollection_AsciiString,TCollection_AsciiString>(theDistribution,""));
164 }
165
166 //================================================================================
167 /*!
168  * \brief HasDistribution
169  */
170 //================================================================================
171 bool LayerDistributionStates::HasDistribution(const TCollection_AsciiString& theDistribution) const
172 {
173   return _distributions.find(theDistribution) != _distributions.end();
174 }
175
176 //================================================================================
177 /*!
178  * \brief SetDistributionType
179  */
180 //================================================================================
181 bool LayerDistributionStates::SetDistributionType(const TCollection_AsciiString& theDistribution,
182                                                   const TCollection_AsciiString& theType)
183 {
184   TDistributionMap::iterator it = _distributions.find(theDistribution);
185   if(it == _distributions.end())
186     return false;
187   (*it).second = theType;
188   return true;
189 }
190
191 //================================================================================
192 /*!
193  * \brief GetDistributionType
194  */
195 //================================================================================
196 TCollection_AsciiString LayerDistributionStates::
197 GetDistributionType(const TCollection_AsciiString& theDistribution) const
198 {
199   TDistributionMap::const_iterator it = _distributions.find(theDistribution);
200   return (it == _distributions.end()) ? TCollection_AsciiString() : (*it).second;
201 }
202
203 //================================================================================
204 /*!
205  * \brief Constructor
206  */
207 //================================================================================
208 SMESH_NoteBook::SMESH_NoteBook()
209 {
210   InitObjectMap();
211 }
212
213 //================================================================================
214 /*!
215  * \brief Destructor
216  */
217 //================================================================================
218 SMESH_NoteBook::~SMESH_NoteBook()
219 {
220   TVariablesMap::const_iterator it = _objectMap.begin();
221   for(;it!=_objectMap.end();it++) {
222     if((*it).second)
223       delete (*it).second;
224   }
225 }
226
227 //================================================================================
228 /*!
229  * \brief Replace parameters of the functions on the Salome NoteBook Variables
230  * \param theString - Input string
231  * \retval TCollection_AsciiString - Convertion result
232  */
233 //================================================================================
234 void SMESH_NoteBook::ReplaceVariables()
235 {
236   for(int i=0;i<_commands.size();i++) {
237     Handle(_pyCommand) aCmd = _commands[i];
238     TCollection_AsciiString aMethod = aCmd->GetMethod();
239     TCollection_AsciiString aObject = aCmd->GetObject();
240     TCollection_AsciiString aResultValue = aCmd->GetResultValue();
241     if(MYDEBUG) {
242       cout<<"Command before : "<< aCmd->GetString()<<endl;
243       cout<<"Method : "<< aMethod<<endl;
244       cout<<"Object : "<< aObject<<endl;
245       cout<<"Result : "<< aResultValue<<endl;
246     }
247
248     // check if method modifies the object itself
249     TVariablesMap::const_iterator it = _objectMap.find(aObject);
250     if(it == _objectMap.end()) // check if method returns a new object
251       it = _objectMap.find(aResultValue);
252     
253     if(it == _objectMap.end()) { // check if method modifies a mesh using mesh editor
254       TMeshEditorMap::const_iterator meIt = myMeshEditors.find(aObject);
255       if(meIt != myMeshEditors.end()) {
256         TCollection_AsciiString aMesh = (*meIt).second;
257         it = _objectMap.find(aMesh);
258       }
259     }
260     
261     if(it == _objectMap.end()) { // additional check for pattern mapping
262       if(aMethod.IsEqual("ApplyToMeshFaces") ||
263          aMethod.IsEqual("ApplyToHexahedrons"))
264         it = _objectMap.find(aCmd->GetArg(1));
265     }
266
267     if(it != _objectMap.end()) {
268       if(MYDEBUG)
269         cout << "Found object : " << (*it).first << endl;
270       SMESH_ObjectStates *aStates = (*it).second;
271       // Case for LocalLength hypothesis
272       if(aStates->GetObjectType().IsEqual("LocalLength") && aStates->GetCurrectState().size() >= 2)
273       {
274         if(aMethod.IsEqual("SetLength")) {
275           if(!aStates->GetCurrectState().at(0).IsEmpty() )
276             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
277           aStates->IncrementState();
278         }
279         else if(aMethod.IsEqual("SetPrecision")) {
280           if(!aStates->GetCurrectState().at(1).IsEmpty() )
281             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
282           aStates->IncrementState();
283         }
284       }
285       
286       // Case for SegmentLengthAroundVertex hypothesis
287       else if(aStates->GetObjectType().IsEqual("SegmentLengthAroundVertex")
288               && aStates->GetCurrectState().size() >= 1) {
289         if(aMethod == "SetLength") {
290           if(!aStates->GetCurrectState().at(0).IsEmpty() )
291             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
292           aStates->IncrementState();
293         }
294       }
295
296       // Case for Arithmetic1D and StartEndLength hypothesis
297       else if(aStates->GetObjectType().IsEqual("Arithmetic1D") || 
298               aStates->GetObjectType().IsEqual("StartEndLength")) {
299         if(aMethod == "SetLength" &&
300            aStates->GetCurrectState().size() >= 2) {
301           if(aCmd->GetArg(2) == "1" && !aStates->GetCurrectState().at(0).IsEmpty())
302             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
303           else if(!aStates->GetCurrectState().at(1).IsEmpty())
304             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
305           aStates->IncrementState();
306         }
307       }
308       
309       //Case for Deflection1D hypothesis
310       else if(aStates->GetObjectType().IsEqual("Deflection1D")){
311         if(aMethod == "SetDeflection" && aStates->GetCurrectState().size() >= 1) {
312           if(!aStates->GetCurrectState().at(0).IsEmpty() )
313             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
314           aStates->IncrementState();
315         }
316       }
317       
318       // Case for LayerDistribution hypothesis (not finished yet)
319       else if(aStates->GetObjectType() == "LayerDistribution") {
320         if(aMethod == "SetLayerDistribution"){
321           LayerDistributionStates* aLDStates = (LayerDistributionStates*)(aStates);
322           aLDStates->AddDistribution(aCmd->GetArg(1));
323           if(MYDEBUG)
324             cout<<"Add Distribution :"<<aCmd->GetArg(1)<<endl;
325         }
326       }
327       
328       // Case for MaxElementArea hypothesis
329       else if(aStates->GetObjectType().IsEqual("MaxElementArea")){
330         if(aMethod == "SetMaxElementArea" && aStates->GetCurrectState().size() >= 1) {
331           if(!aStates->GetCurrectState().at(0).IsEmpty() )
332             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
333           aStates->IncrementState();
334         }
335       }
336
337       // Case for MaxElementVolume hypothesis
338       else if(aStates->GetObjectType().IsEqual("MaxElementVolume")){
339         if(aMethod == "SetMaxElementVolume" && aStates->GetCurrectState().size() >= 1) {
340           if(!aStates->GetCurrectState().at(0).IsEmpty() )
341             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
342           aStates->IncrementState();
343         }
344       }
345       // Case for NumberOfLayers hypothesis
346       else if(aStates->GetObjectType().IsEqual("NumberOfLayers")){
347         if(aMethod == "SetNumberOfLayers" && aStates->GetCurrectState().size() >= 1) {
348           if(!aStates->GetCurrectState().at(0).IsEmpty() )
349             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
350           aStates->IncrementState();
351         }
352       }
353
354       // Case for NumberOfSegments hypothesis
355       else if(aStates->GetObjectType().IsEqual("NumberOfSegments")){
356         if(aMethod == "SetNumberOfSegments" && aStates->GetCurrectState().size() >= 1) {
357           if(!aStates->GetCurrectState().at(0).IsEmpty() )
358             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
359           if(aStates->GetCurrectState().size()==1)
360             aStates->IncrementState();
361         }
362         else if (aMethod == "SetScaleFactor" && aStates->GetCurrectState().size() >= 2) {
363           if(!aStates->GetCurrectState().at(1).IsEmpty() )
364             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
365           aStates->IncrementState();
366         }
367       }
368       
369       else if(aStates->GetObjectType().IsEqual("Mesh")) {
370         TState aCurrentState = aStates->GetCurrectState();
371         int aCurrentStateSize = aCurrentState.size();
372         if(aMethod.IsEqual("Translate")                  ||
373            aMethod.IsEqual("TranslateMakeGroups")        ||
374            aMethod.IsEqual("TranslateMakeMesh")          ||
375            aMethod.IsEqual("TranslateObject")            ||
376            aMethod.IsEqual("TranslateObjectMakeGroups")  ||
377            aMethod.IsEqual("TranslateObjectMakeMesh")) {
378           bool isVariableFound = false;
379           int anArgIndex = 0;
380           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
381             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
382               anArgIndex = i+1;
383               break;
384             }
385           }
386           if(anArgIndex > 0) {
387             if(aCurrentStateSize == 3) { // translation by dx, dy, dz
388               for(int j = 0; j < aCurrentStateSize; j++) {
389                 if(!aCurrentState.at(j).IsEmpty()) {
390                   isVariableFound = true;
391                   aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
392                 }
393               }
394             }
395             else if(aCurrentStateSize == 6) { // translation by x1, x2, y1, y2, z1, z2
396               // TODO: limitation until operations on the variables will be introduced
397               /*
398               isVariableFound = true;
399               for(int j = 0; j < 3; j++) {
400                 TCollection_AsciiString anArg = aCmd->GetArg(anArgIndex+j);
401                 TCollection_AsciiString aValue1 = aCurrentState.at(2*j), aValue2 = aCurrentState.at(2*j+1);
402                 bool aV1 = !aValue1.IsEmpty();
403                 bool aV2 = !aValue2.IsEmpty();
404                 double aValue, aCurrentValue = anArg.IsRealValue() ? anArg.RealValue() : 0;
405                 if(aV1 && !aV2) {
406                   if(!GetReal(aValue1, aValue))
407                     aValue = 0;
408                   aValue2 = TCollection_AsciiString( aValue + aCurrentValue );
409                 }
410                 else if(!aV1 && aV2) {
411                   if(!GetReal(aValue2, aValue))
412                     aValue = 0;
413                   aValue1 = TCollection_AsciiString( aValue - aCurrentValue );
414                 }
415                 else if(!aV1 && !aV2) {
416                   aValue1 = TCollection_AsciiString( 0 );
417                   aValue2 = TCollection_AsciiString( aCurrentValue );
418                 }
419                 aCmd->SetArg(anArgIndex+j, aValue1 + ", " + aValue2 );
420               }
421               */
422             }
423           }
424           if(isVariableFound) {
425             TCollection_AsciiString aDim;
426             if(aCurrentStateSize == 6)
427               aDim = "6";
428             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr"+aDim);
429             aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
430           }
431           aStates->IncrementState();
432         }
433         else if(aMethod.IsEqual("Rotate")                  ||
434                 aMethod.IsEqual("RotateMakeGroups")        ||
435                 aMethod.IsEqual("RotateMakeMesh")          ||
436                 aMethod.IsEqual("RotateObject")            ||
437                 aMethod.IsEqual("RotateObjectMakeGroups")  ||
438                 aMethod.IsEqual("RotateObjectMakeMesh")    ||
439                 aMethod.IsEqual("RotationSweep")           ||
440                 aMethod.IsEqual("RotationSweepObject")     ||
441                 aMethod.IsEqual("RotationSweepObject1D")   ||
442                 aMethod.IsEqual("RotationSweepObject2D")   ||
443                 aMethod.IsEqual("RotationSweepMakeGroups") ||
444                 aMethod.IsEqual("RotationSweepObjectMakeGroups") ||
445                 aMethod.IsEqual("RotationSweepObject1DMakeGroups") ||
446                 aMethod.IsEqual("RotationSweepObject2DMakeGroups") ||
447                 aMethod.IsEqual("Mirror")                  ||
448                 aMethod.IsEqual("MirrorMakeMesh")          ||
449                 aMethod.IsEqual("MirrorMakeGroups")        ||
450                 aMethod.IsEqual("MirrorObject")            || 
451                 aMethod.IsEqual("MirrorObjectMakeMesh")    ||
452                 aMethod.IsEqual("MirrorObjectMakeGroups")) {
453           bool isSubstitute = false;
454           int anArgIndex = 0;
455           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
456             if(aCmd->GetArg(i).IsEqual("SMESH.AxisStruct")) {
457               anArgIndex = i+1;
458               break;
459             }
460           }
461           if(anArgIndex > 0) {
462             for(int j = 0; j < aCurrentStateSize; j++) {
463               if(!aCurrentState.at(j).IsEmpty()) {
464                 if(j < 6) // 0-5 - axis struct, 6 - angle (rotation & sweep), 7-8 - nbSteps and tolerance (sweep)
465                   isSubstitute = true;
466                 aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
467               }
468             }
469           }
470           if(isSubstitute)
471             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".AxisStructStr");
472           aStates->IncrementState();
473         }
474         else if(aMethod.IsEqual("AddNode") ||
475                 aMethod.IsEqual("MoveClosestNodeToPoint")) {
476           for(int j = 0; j < aCurrentStateSize; j++) {
477             if(!aCurrentState.at(j).IsEmpty())
478               aCmd->SetArg(j+1, aCurrentState.at(j));
479           }
480           aStates->IncrementState();
481         }
482         else if(aMethod.IsEqual("MoveNode")) {
483           for(int j = 0; j < aCurrentStateSize; j++) {
484             if(!aCurrentState.at(j).IsEmpty())
485               aCmd->SetArg(j+2, aCurrentState.at(j));
486           }
487           aStates->IncrementState();
488         }
489         else if(aMethod.IsEqual("ExtrusionSweep") ||
490                 aMethod.IsEqual("ExtrusionSweepObject") ||
491                 aMethod.IsEqual("ExtrusionSweepObject1D") ||
492                 aMethod.IsEqual("ExtrusionSweepObject2D") ||
493                 aMethod.IsEqual("ExtrusionSweepMakeGroups") ||
494                 aMethod.IsEqual("ExtrusionSweepObjectMakeGroups") ||
495                 aMethod.IsEqual("ExtrusionSweepObject1DMakeGroups") ||
496                 aMethod.IsEqual("ExtrusionSweepObject2DMakeGroups")) {
497           bool isSubstitute = false;
498           int anArgIndex = 0;
499           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
500             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
501               anArgIndex = i+1;
502               break;
503             }
504           }
505           if(anArgIndex > 0) {
506             for(int j = 0; j < aCurrentStateSize; j++) {
507               if(!aCurrentState.at(j).IsEmpty()) {
508                 if(j < 3) // 0-2 - dir struct, 3 - number of steps
509                   isSubstitute = true;
510                 aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
511               }
512             }
513           }
514           if(isSubstitute) {
515             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
516             aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
517           }
518           aStates->IncrementState();
519         }
520         else if(aMethod.IsEqual("ExtrusionAlongPath") ||
521                 aMethod.IsEqual("ExtrusionAlongPathObject") ||
522                 aMethod.IsEqual("ExtrusionAlongPathObject1D") ||
523                 aMethod.IsEqual("ExtrusionAlongPathObject2D") ||
524                 aMethod.IsEqual("ExtrusionAlongPathMakeGroups") ||
525                 aMethod.IsEqual("ExtrusionAlongPathObjectMakeGroups") ||
526                 aMethod.IsEqual("ExtrusionAlongPathObject1DMakeGroups") ||
527                 aMethod.IsEqual("ExtrusionAlongPathObject2DMakeGroups") ||
528                 /* workaround for a bug in the command parsing algorithm */
529                 aCmd->GetString().Search("ExtrusionAlongPathMakeGroups") != -1 ||
530                 aCmd->GetString().Search("ExtrusionAlongPathObjectMakeGroups") != -1 ||
531                 aCmd->GetString().Search("ExtrusionAlongPathObject1DMakeGroups") != -1 ||
532                 aCmd->GetString().Search("ExtrusionAlongPathObject2DMakeGroups") != -1 ) {
533           int aNbAngles = aCurrentStateSize-3; // State looks like "Angle1:...:AngleN:X:Y:Z"
534           bool isSubstitute = false;
535           int anArgIndex = 0;
536           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
537             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
538               anArgIndex = i-1-aNbAngles;
539               break;
540             }
541           }
542           if(anArgIndex > 0) {
543             int j = 0;
544             for(; j < aNbAngles; j++) {
545               if(!aCurrentState.at(j).IsEmpty()) {
546                 aCmd->SetArg(anArgIndex+j-1, aCurrentState.at(j));
547               }
548             }
549             for(; j < aNbAngles+3; j++) {
550               if(!aCurrentState.at(j).IsEmpty()) {
551                 isSubstitute = true;
552                 aCmd->SetArg(anArgIndex+j+2, aCurrentState.at(j));
553               }
554             }
555           }
556           if(isSubstitute)
557             aCmd->SetArg(anArgIndex + aNbAngles + 1,
558                          TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
559           aStates->IncrementState();
560         }
561         else if(aMethod.IsEqual("TriToQuad") ||
562                 aMethod.IsEqual("Concatenate") ||
563                 aMethod.IsEqual("ConcatenateWithGroups")) {
564           if(aCurrentStateSize && !aCurrentState.at(0).IsEmpty())
565             aCmd->SetArg(aCmd->GetNbArgs(), aCurrentState.at(0));
566           aStates->IncrementState();
567         }
568         else if(aMethod.IsEqual("Smooth") ||
569                 aMethod.IsEqual("SmoothObject") ||
570                 aMethod.IsEqual("SmoothParametric") ||
571                 aMethod.IsEqual("SmoothParametricObject")) {
572           int anArgIndex = aCmd->GetNbArgs() - 2;
573           for(int j = 0; j < aCurrentStateSize; j++) {
574             if(!aCurrentState.at(j).IsEmpty())
575               aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
576           }
577           aStates->IncrementState();
578         }
579         else if(aMethod.IsEqual("ApplyToMeshFaces") ||
580                 aMethod.IsEqual("ApplyToHexahedrons")) {
581           int anArgIndex = aCmd->GetNbArgs()-1;
582           for(int j = 0; j < aCurrentStateSize; j++)
583             if(!aCurrentState.at(j).IsEmpty())
584               aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
585           aStates->IncrementState();
586         }
587       } // if ( aStates->GetObjectType().IsEqual("Mesh"))
588
589       // Case for NETGEN_Parameters_2D or NETGEN_Parameters_2D hypothesis
590       // else if(aStates->GetObjectType().IsEqual("NETGEN_Parameters_2D") ||
591       //         aStates->GetObjectType().IsEqual("NETGEN_Parameters")){
592       //   if(aMethod == "SetMaxSize" && aStates->GetCurrectState().size() >= 1) {
593       //     if(!aStates->GetCurrectState().at(0).IsEmpty() )
594       //       aCmd->SetArg(1,aStates->GetCurrectState().at(0));
595       //     aStates->IncrementState();
596       //   }
597       //   else if(aMethod == "SetGrowthRate" && aStates->GetCurrectState().size() >= 2) {
598       //     if(!aStates->GetCurrectState().at(1).IsEmpty() )
599       //       aCmd->SetArg(1,aStates->GetCurrectState().at(1));
600       //     aStates->IncrementState();
601       //   }
602       //   else if(aMethod == "SetNbSegPerEdge" && aStates->GetCurrectState().size() >= 3) {
603       //     if(!aStates->GetCurrectState().at(2).IsEmpty() )
604       //       aCmd->SetArg(1,aStates->GetCurrectState().at(2));
605       //     aStates->IncrementState();
606       //   }
607       //   else if(aMethod == "SetNbSegPerRadius" && aStates->GetCurrectState().size() >= 4) {
608       //     if(!aStates->GetCurrectState().at(3).IsEmpty() )
609       //       aCmd->SetArg(1,aStates->GetCurrectState().at(3));
610       //     aStates->IncrementState();
611       //   }
612       // }
613
614       // // Case for NETGEN_SimpleParameters_3D or NETGEN_SimpleParameters_2D hypothesis
615       // else if(aStates->GetObjectType().IsEqual("NETGEN_SimpleParameters_3D") ||
616       //         aStates->GetObjectType().IsEqual("NETGEN_SimpleParameters_2D")) {
617
618       //   if((aMethod == "SetNumberOfSegments" || aMethod == "SetLocalLength") && 
619       //      aStates->GetCurrectState().size() >= 1) {
620       //     if(!aStates->GetCurrectState().at(0).IsEmpty() )
621       //       aCmd->SetArg(1,aStates->GetCurrectState().at(0));
622       //     aStates->IncrementState();
623       //   }
624       //   else if(aMethod == "SetMaxElementArea" && aStates->GetCurrectState().size() >= 2) {
625       //     if(!aStates->GetCurrectState().at(1).IsEmpty() )
626       //       aCmd->SetArg(1,aStates->GetCurrectState().at(1));
627       //     aStates->IncrementState();
628       //   }
629       //   else if(aMethod == "SetMaxElementVolume" && aStates->GetCurrectState().size() >= 3) {
630       //     if(!aStates->GetCurrectState().at(2).IsEmpty() )
631       //       aCmd->SetArg(1,aStates->GetCurrectState().at(2));
632       //     aStates->IncrementState();
633       //   }
634       //   else if(aMethod == "LengthFromEdges" || aMethod == "LengthFromFaces"){
635       //     aStates->IncrementState();
636       //   }
637       // }
638
639       else
640       {
641         // treat Netgen hypotheses;
642         // this (and above) code can work wrong since nb of states can differ from nb of
643         // dumped calls due to the fix of
644         // issue 0021364:: Dump of netgen parameters has duplicate lines
645         SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
646         SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
647         SALOMEDS::SObject_var sobj = aStudy->FindObjectID( (*it).first.ToCString() );
648         CORBA::Object_var      obj = aGen->SObjectToObject( sobj );
649         if ( SMESH_Hypothesis_i* h = SMESH::DownCast< SMESH_Hypothesis_i*>( obj ))
650         {
651           TState aCurrentState = aStates->GetCurrectState();
652           int argIndex = h->getParamIndex( aMethod, aCurrentState.size() );
653           if ( 0 <= argIndex && argIndex < aCurrentState.size() &&
654                !aCurrentState[argIndex].IsEmpty() )
655             aCmd->SetArg( 1, aCurrentState[argIndex] );
656
657           if ( argIndex >= 0 )
658             aStates->IncrementState();
659         }
660       }
661     }
662     else {
663       if(MYDEBUG)
664         cout << "Object not found" << endl;
665     }
666     if(MYDEBUG) {
667       cout<<"Command after: "<< aCmd->GetString()<<endl;
668     }
669   }
670   
671   ProcessLayerDistribution();
672 }
673 //================================================================================
674 /*!
675  * \brief Private method
676  */
677 //================================================================================
678 void SMESH_NoteBook::InitObjectMap()
679 {
680   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
681   if(!aGen)
682     return;
683   
684   SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
685   if(aStudy->_is_nil())
686     return;
687   
688   SALOMEDS::SObject_var aSO = aStudy->FindComponent(aGen->ComponentDataType());
689   if(CORBA::is_nil(aSO))
690     return;
691   
692   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
693   char* aParameters;
694   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
695     SALOMEDS::SObject_var aSObject = Itr->Value();
696     SALOMEDS::GenericAttribute_var anAttr;
697     if ( aSObject->FindAttribute(anAttr, "AttributeString")) {
698       aParameters = SALOMEDS::AttributeString::_narrow(anAttr)->Value();
699       SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters);
700       if(MYDEBUG) {
701         cout<<"Entry : "<< aSObject->GetID()<<endl;
702         cout<<"aParameters : "<<aParameters<<endl;
703       }      
704       TCollection_AsciiString anObjType;
705       CORBA::Object_var       anObject = SMESH_Gen_i::SObjectToObject(aSObject);
706       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
707       SMESH::SMESH_Mesh_var      aMesh = SMESH::SMESH_Mesh::_narrow(anObject);
708       if(!aHyp->_is_nil()) {
709         CORBA::String_var hypName = aHyp->GetName();
710         anObjType = hypName.in();
711       }
712       else if (!aMesh->_is_nil() ) {
713         anObjType = "Mesh";
714       }
715       if(MYDEBUG)
716         cout<<"The object Type : "<<anObjType<<endl;
717       SMESH_ObjectStates *aState = NULL;
718       if(anObjType == "LayerDistribution")
719         aState = new LayerDistributionStates();
720       else
721         aState = new  SMESH_ObjectStates(anObjType);
722
723       for(int i = 0; i < aSections->length(); i++) {
724         TState aVars;
725         SALOMEDS::ListOfStrings aListOfVars = aSections[i];
726         for(int j = 0;j<aListOfVars.length();j++) {
727           TCollection_AsciiString aVar(aListOfVars[j].in());
728           if(!aVar.IsEmpty() && aStudy->IsVariable(aVar.ToCString())) {
729             aVar.InsertBefore(1,            SMESH::TVar::Quote() );
730             aVar.InsertAfter(aVar.Length(), SMESH::TVar::Quote() );
731           }
732           aVars.push_back(aVar);
733           if(MYDEBUG) {
734             cout<<"Variable: '"<<aVar<<"'"<<endl;
735           }
736         }
737         aState->AddState(aVars);
738       }
739       if ( aState->GetAllStates().empty() )
740       {
741         delete aState;
742       }
743       else
744       {
745         CORBA::String_var objID = aSObject->GetID();
746         _objectMap.insert( make_pair(TCollection_AsciiString( objID.in() ), aState ));
747       }
748     }
749   }
750 }
751
752 //================================================================================
753 /*!
754  * 
755  */
756 //================================================================================
757 void SMESH_NoteBook::AddCommand(const TCollection_AsciiString& theString)
758 {
759   if(MYDEBUG)
760     cout<<theString<<endl;
761   Handle(_pyCommand) aCommand = new _pyCommand( theString, -1);
762   _commands.push_back(aCommand);
763
764   if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
765     myMeshEditors.insert( make_pair( aCommand->GetResultValue(),
766                                      aCommand->GetObject() ) );
767   }
768 }
769
770 //================================================================================
771 /*!
772  * 
773  */
774 //================================================================================
775 void SMESH_NoteBook::ProcessLayerDistribution()
776 {
777   // 1) Find all LayerDistribution states
778   vector<LayerDistributionStates*> aLDS;
779   TVariablesMap::const_iterator it = _objectMap.begin();
780   for(;it != _objectMap.end();it++) {
781     LayerDistributionStates* aLDStates = dynamic_cast<LayerDistributionStates*>(((*it).second));
782     if(aLDStates!=NULL) {
783       aLDS.push_back(aLDStates);
784     }
785   }
786   
787   if(!aLDS.size())
788     return;
789   
790   // 2) Initialize all type of 1D Distribution hypothesis
791   for(int i=0;i<_commands.size();i++){
792     for(int j =0;j < aLDS.size();j++){
793       TCollection_AsciiString aResultValue = _commands[i]->GetResultValue();
794       if(_commands[i]->GetMethod() == "CreateHypothesis" &&
795          aLDS[j]->HasDistribution(aResultValue)){
796         TCollection_AsciiString aType = _commands[i]->GetArg(1);
797         aType.RemoveAll('\'');
798         aLDS[j]->SetDistributionType(aResultValue,aType);
799       }
800     }
801   }
802   // 3) ... and replase variables ...
803
804   for(int i=0;i<_commands.size();i++){
805     for(int j =0;j < aLDS.size();j++){
806       TCollection_AsciiString anObject = _commands[i]->GetObject();
807
808       if(aLDS[j]->HasDistribution(anObject)) {
809         TCollection_AsciiString aType = aLDS[j]->GetDistributionType(anObject);
810         TCollection_AsciiString aMethod = _commands[i]->GetMethod();
811         if(aType == "LocalLength") {
812           if(aMethod == "SetLength") {
813             SetVariable(_commands[i], aLDS[j],0,1);
814             aLDS[j]->IncrementState();
815           }
816           else if(aMethod == "SetPrecision") {
817             SetVariable(_commands[i], aLDS[j],1,1);
818             aLDS[j]->IncrementState();
819           }
820         }
821
822         // Case for NumberOfSegments hypothesis
823         else if(aType == "NumberOfSegments"){
824           if(aMethod == "SetNumberOfSegments") {
825             SetVariable(_commands[i], aLDS[j],0,1);
826             if(aLDS[j]->GetCurrectState().size()==1)
827               aLDS[j]->IncrementState();
828           }
829           else if (aMethod == "SetScaleFactor") {
830             SetVariable(_commands[i], aLDS[j],1,1);
831             aLDS[j]->IncrementState();
832           }
833         }
834         
835         else if( aType == "Deflection1D" ){
836           if(aMethod == "SetDeflection"){
837             SetVariable(_commands[i], aLDS[j],0,1);
838             aLDS[j]->IncrementState();
839           }
840         }
841         // Case for Arithmetic1D and StartEndLength hypothesis
842         else if(aType == "Arithmetic1D" || aType == "StartEndLength") {
843           if(aMethod == "SetLength") {
844             int anArgNb = (_commands[i]->GetArg(2) == "1") ? 0 : 1;
845             SetVariable(_commands[i], aLDS[j],anArgNb,1);
846             aLDS[j]->IncrementState();
847           }
848         }
849       }
850     }
851   }
852 }
853 //================================================================================
854 /*!
855  *  \brief Return result script
856  */
857 //================================================================================
858 TCollection_AsciiString SMESH_NoteBook::GetResultScript() const
859 {
860   TCollection_AsciiString aResult;
861   for(int i=0;i<_commands.size();i++)
862     aResult+=_commands[i]->GetString()+"\n";
863   return aResult;
864 }
865
866 //================================================================================
867 /*!
868  *  \brief Return value of the variable
869  */
870 //================================================================================
871 bool SMESH_NoteBook::GetReal(const TCollection_AsciiString& theVarName, double& theValue)
872 {
873   bool ok = false;
874
875   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
876   if(!aGen)
877     return ok;
878
879   SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
880   if(aStudy->_is_nil())
881     return ok;
882
883   TCollection_AsciiString aVarName = theVarName;
884   aVarName.RemoveAll('\"');
885
886   if(aVarName.IsEmpty())
887     return ok;
888
889   const char* aName = aVarName.ToCString();
890   if(aStudy->IsVariable(aName) && (aStudy->IsReal(aName) || aStudy->IsInteger(aName))) {
891     theValue = aStudy->GetReal(aVarName.ToCString());
892     ok = true;
893   }
894
895   return ok;
896 }
897