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