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