Salome HOME
Dump python extension.
[modules/smesh.git] / src / SMESH_I / SMESH_NoteBook.cxx
1 // Copyright (C) 2008  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 ObjectStates* theStates, int position, int theArgNb);
45
46 //================================================================================
47 /*!
48  * \brief Constructor
49  */
50 //================================================================================
51 ObjectStates::ObjectStates(TCollection_AsciiString theType)
52 {
53   _type = theType;
54   _dumpstate = 0;
55 }
56
57 //================================================================================
58 /*!
59  * \brief Destructor
60  */
61 //================================================================================
62 ObjectStates::~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 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 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 ObjectStates::GetAllStates() const
98 {
99   return _states;
100 }
101
102 //================================================================================
103 /*!
104  *
105  */
106 //================================================================================
107 void ObjectStates::IncrementState()
108 {
109   _dumpstate++;
110 }
111
112 //================================================================================
113 /*!
114  *
115  */
116 //================================================================================
117 TCollection_AsciiString ObjectStates::GetObjectType() const{
118   return _type;
119 }
120
121
122 //================================================================================
123 /*!
124  * \brief Constructor
125  */
126 //================================================================================
127 LayerDistributionStates::LayerDistributionStates():
128   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()) {
247       ObjectStates *aStates = (*it).second;
248       // Case for LocalLength hypothesis
249       if(aStates->GetObjectType().IsEqual("LocalLength") && aStates->GetCurrectState().size() >= 2) {
250         if(aMethod.IsEqual("SetLength")) {
251           if(!aStates->GetCurrectState().at(0).IsEmpty() )
252             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
253           aStates->IncrementState();
254         }
255         else if(aMethod.IsEqual("SetPrecision")) {
256           if(!aStates->GetCurrectState().at(1).IsEmpty() )
257             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
258           aStates->IncrementState();
259         }
260       }
261       
262       // Case for SegmentLengthAroundVertex hypothesis
263       else if(aStates->GetObjectType().IsEqual("SegmentLengthAroundVertex")
264               && aStates->GetCurrectState().size() >= 1) {
265         if(aMethod == "SetLength") {
266           if(!aStates->GetCurrectState().at(0).IsEmpty() )
267             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
268           aStates->IncrementState();
269         }
270       }
271
272       // Case for Arithmetic1D and StartEndLength hypothesis
273       else if(aStates->GetObjectType().IsEqual("Arithmetic1D") || 
274               aStates->GetObjectType().IsEqual("StartEndLength")) {
275         if(aMethod == "SetLength" &&
276            aStates->GetCurrectState().size() >= 2) {
277           if(aCmd->GetArg(2) == "1" && !aStates->GetCurrectState().at(0).IsEmpty())
278             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
279           else if(!aStates->GetCurrectState().at(1).IsEmpty())
280             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
281           aStates->IncrementState();
282         }
283       }
284       
285       //Case for Deflection1D hypothesis
286       else if(aStates->GetObjectType().IsEqual("Deflection1D")){
287         if(aMethod == "SetDeflection" && aStates->GetCurrectState().size() >= 1) {
288           if(!aStates->GetCurrectState().at(0).IsEmpty() )
289             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
290           aStates->IncrementState();
291         }
292       }
293       
294       // Case for LayerDistribution hypothesis (not finished yet)
295       else if(aStates->GetObjectType() == "LayerDistribution") {
296         if(aMethod == "SetLayerDistribution"){
297           LayerDistributionStates* aLDStates = (LayerDistributionStates*)(aStates);
298           aLDStates->AddDistribution(aCmd->GetArg(1));
299           if(MYDEBUG)
300             cout<<"Add Distribution :"<<aCmd->GetArg(1)<<endl;
301         }
302       }
303       
304       // Case for MaxElementArea hypothesis
305       else if(aStates->GetObjectType().IsEqual("MaxElementArea")){
306         if(aMethod == "SetMaxElementArea" && aStates->GetCurrectState().size() >= 1) {
307           if(!aStates->GetCurrectState().at(0).IsEmpty() )
308             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
309           aStates->IncrementState();
310         }
311       }
312
313       // Case for MaxElementVolume hypothesis
314       else if(aStates->GetObjectType().IsEqual("MaxElementVolume")){
315         if(aMethod == "SetMaxElementVolume" && aStates->GetCurrectState().size() >= 1) {
316           if(!aStates->GetCurrectState().at(0).IsEmpty() )
317             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
318           aStates->IncrementState();
319         }
320       }
321
322       // Case for NETGEN_Parameters_2D or NETGEN_Parameters_2D hypothesis
323       else if(aStates->GetObjectType().IsEqual("NETGEN_Parameters_2D") ||
324               aStates->GetObjectType().IsEqual("NETGEN_Parameters")){
325         if(aMethod == "SetMaxSize" && aStates->GetCurrectState().size() >= 1) {
326           if(!aStates->GetCurrectState().at(0).IsEmpty() )
327             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
328           aStates->IncrementState();
329         }
330         else if(aMethod == "SetGrowthRate" && aStates->GetCurrectState().size() >= 2) {
331           if(!aStates->GetCurrectState().at(1).IsEmpty() )
332             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
333           aStates->IncrementState();
334         }
335         else if(aMethod == "SetNbSegPerEdge" && aStates->GetCurrectState().size() >= 3) {
336           if(!aStates->GetCurrectState().at(2).IsEmpty() )
337             aCmd->SetArg(1,aStates->GetCurrectState().at(2));
338           aStates->IncrementState();
339         } 
340         else if(aMethod == "SetNbSegPerRadius" && aStates->GetCurrectState().size() >= 4) {
341           if(!aStates->GetCurrectState().at(3).IsEmpty() )
342             aCmd->SetArg(1,aStates->GetCurrectState().at(3));
343           aStates->IncrementState();
344         } 
345       }
346
347       // Case for NETGEN_SimpleParameters_3D or NETGEN_SimpleParameters_2D hypothesis
348       else if(aStates->GetObjectType().IsEqual("NETGEN_SimpleParameters_3D") ||
349               aStates->GetObjectType().IsEqual("NETGEN_SimpleParameters_2D")){
350
351         if((aMethod == "SetNumberOfSegments" || aMethod == "SetLocalLength") && 
352            aStates->GetCurrectState().size() >= 1) {
353           if(!aStates->GetCurrectState().at(0).IsEmpty() )
354             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
355           aStates->IncrementState();
356         }
357         else if(aMethod == "SetMaxElementArea" && aStates->GetCurrectState().size() >= 2) {
358           if(!aStates->GetCurrectState().at(1).IsEmpty() )
359             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
360           aStates->IncrementState();
361         }
362         else if(aMethod == "SetMaxElementVolume" && aStates->GetCurrectState().size() >= 3) {
363           if(!aStates->GetCurrectState().at(2).IsEmpty() )
364             aCmd->SetArg(1,aStates->GetCurrectState().at(2));
365           aStates->IncrementState();
366         }
367         else if(aMethod == "LengthFromEdges" || aMethod == "LengthFromFaces"){
368           aStates->IncrementState();
369         }
370       }
371       
372       // Case for NumberOfLayers hypothesis
373       else if(aStates->GetObjectType().IsEqual("NumberOfLayers")){
374         if(aMethod == "SetNumberOfLayers" && aStates->GetCurrectState().size() >= 1) {
375           if(!aStates->GetCurrectState().at(0).IsEmpty() )
376             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
377           aStates->IncrementState();
378         }
379       }
380
381       // Case for NumberOfSegments hypothesis
382       else if(aStates->GetObjectType().IsEqual("NumberOfSegments")){
383         if(aMethod == "SetNumberOfSegments" && aStates->GetCurrectState().size() >= 1) {
384           if(!aStates->GetCurrectState().at(0).IsEmpty() )
385             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
386           if(aStates->GetCurrectState().size()==1)
387             aStates->IncrementState();
388         }
389         else if (aMethod == "SetScaleFactor" && aStates->GetCurrectState().size() >= 2) {
390           if(!aStates->GetCurrectState().at(1).IsEmpty() )
391             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
392           aStates->IncrementState();
393         }
394       }
395       
396       else if(aStates->GetObjectType().IsEqual("Mesh")) {
397         TState aCurrentState = aStates->GetCurrectState();
398         int aCurrentStateSize = aCurrentState.size();
399         if(aMethod.IsEqual("Translate") ||
400            aMethod.IsEqual("TranslateMakeGroups") ||
401            aMethod.IsEqual("TranslateMakeMesh")) {
402           bool isVariableFound = false;
403           int anArgIndex = 0;
404           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
405             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
406               anArgIndex = i+1;
407               break;
408             }
409           }
410           if(anArgIndex > 0) {
411             if(aCurrentStateSize == 3) { // translation by dx, dy, dz
412               for(int j = 0; j < aCurrentStateSize; j++) {
413                 if(!aCurrentState.at(j).IsEmpty()) {
414                   isVariableFound = true;
415                   aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
416                 }
417               }
418             }
419             else if(aCurrentStateSize == 6) { // translation by x1, x2, y1, y2, z1, z2
420               // TODO: limitation until operations on the variables will be introduced
421               /*
422               isVariableFound = true;
423               for(int j = 0; j < 3; j++) {
424                 TCollection_AsciiString anArg = aCmd->GetArg(anArgIndex+j);
425                 TCollection_AsciiString aValue1 = aCurrentState.at(2*j), aValue2 = aCurrentState.at(2*j+1);
426                 bool aV1 = !aValue1.IsEmpty();
427                 bool aV2 = !aValue2.IsEmpty();
428                 double aValue, aCurrentValue = anArg.IsRealValue() ? anArg.RealValue() : 0;
429                 if(aV1 && !aV2) {
430                   if(!GetReal(aValue1, aValue))
431                     aValue = 0;
432                   aValue2 = TCollection_AsciiString( aValue + aCurrentValue );
433                 }
434                 else if(!aV1 && aV2) {
435                   if(!GetReal(aValue2, aValue))
436                     aValue = 0;
437                   aValue1 = TCollection_AsciiString( aValue - aCurrentValue );
438                 }
439                 else if(!aV1 && !aV2) {
440                   aValue1 = TCollection_AsciiString( 0 );
441                   aValue2 = TCollection_AsciiString( aCurrentValue );
442                 }
443                 aCmd->SetArg(anArgIndex+j, aValue1 + ", " + aValue2 );
444               }
445               */
446             }
447           }
448           if(isVariableFound) {
449             TCollection_AsciiString aDim;
450             if(aCurrentStateSize == 6)
451               aDim = "6";
452             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr"+aDim);
453             aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
454           }
455           aStates->IncrementState();
456         }
457         else if(aMethod.IsEqual("Rotate") ||
458                 aMethod.IsEqual("RotateMakeGroups") ||
459                 aMethod.IsEqual("RotateMakeMesh") ||
460                 aMethod.IsEqual("RotationSweep") ||
461                 aMethod.IsEqual("RotationSweepMakeGroups") ||
462                 aMethod.IsEqual("Mirror") ||
463                 aMethod.IsEqual("MirrorMakeGroups")) {
464           bool isSubstitute = false;
465           int anArgIndex = 0;
466           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
467             if(aCmd->GetArg(i).IsEqual("SMESH.AxisStruct")) {
468               anArgIndex = i+1;
469               break;
470             }
471           }
472           if(anArgIndex > 0) {
473             for(int j = 0; j < aCurrentStateSize; j++) {
474               if(!aCurrentState.at(j).IsEmpty()) {
475                 if(j < 6) // 0-5 - axis struct, 6 - angle (rotation & sweep), 7-8 - nbSteps and tolerance (sweep)
476                   isSubstitute = true;
477                 aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
478               }
479             }
480           }
481           if(isSubstitute)
482             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".AxisStructStr");
483           aStates->IncrementState();
484         }
485         else if(aMethod.IsEqual("AddNode") ||
486                 aMethod.IsEqual("MoveClosestNodeToPoint")) {
487           for(int j = 0; j < aCurrentStateSize; j++) {
488             if(!aCurrentState.at(j).IsEmpty())
489               aCmd->SetArg(j+1, aCurrentState.at(j));
490           }
491           aStates->IncrementState();
492         }
493         else if(aMethod.IsEqual("MoveNode")) {
494           for(int j = 0; j < aCurrentStateSize; j++) {
495             if(!aCurrentState.at(j).IsEmpty())
496               aCmd->SetArg(j+2, aCurrentState.at(j));
497           }
498           aStates->IncrementState();
499         }
500         else if(aMethod.IsEqual("ExtrusionSweep") ||
501                 aMethod.IsEqual("ExtrusionSweepMakeGroups")) {
502           bool isSubstitute = false;
503           int anArgIndex = 0;
504           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
505             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
506               anArgIndex = i+1;
507               break;
508             }
509           }
510           if(anArgIndex > 0) {
511             for(int j = 0; j < aCurrentStateSize; j++) {
512               if(!aCurrentState.at(j).IsEmpty()) {
513                 if(j < 3) // 0-2 - dir struct, 3 - number of steps
514                   isSubstitute = true;
515                 aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
516               }
517             }
518           }
519           if(isSubstitute) {
520             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
521             aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
522           }
523           aStates->IncrementState();
524         }
525         else if(aMethod.IsEqual("ExtrusionAlongPath") ||
526                 aMethod.IsEqual("ExtrusionAlongPathMakeGroups") ||
527                 /* workaround for a bug in the command parsing algorithm */
528                 aCmd->GetString().Search("ExtrusionAlongPathMakeGroups") != -1) {
529           int aNbAngles = aCurrentStateSize-3; // State looks like "Angle1:...:AngleN:X:Y:Z"
530           bool isSubstitute = false;
531           int anArgIndex = 0;
532           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
533             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
534               anArgIndex = i-1-aNbAngles;
535               break;
536             }
537           }
538           if(anArgIndex > 0) {
539             int j = 0;
540             for(; j < aNbAngles; j++) {
541               if(!aCurrentState.at(j).IsEmpty()) {
542                 aCmd->SetArg(anArgIndex+j-1, aCurrentState.at(j));
543               }
544             }
545             for(; j < aNbAngles+3; j++) {
546               if(!aCurrentState.at(j).IsEmpty()) {
547                 isSubstitute = true;
548                 aCmd->SetArg(anArgIndex+j+2, aCurrentState.at(j));
549               }
550             }
551           }
552           if(isSubstitute)
553             aCmd->SetArg(anArgIndex + aNbAngles + 1,
554                          TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
555           aStates->IncrementState();
556         }
557         else if(aMethod.IsEqual("TriToQuad") ||
558                 aMethod.IsEqual("Concatenate") ||
559                 aMethod.IsEqual("ConcatenateWithGroups")) {
560           if(aCurrentStateSize && !aCurrentState.at(0).IsEmpty())
561             aCmd->SetArg(aCmd->GetNbArgs(), aCurrentState.at(0));
562           aStates->IncrementState();
563         }
564         else if(aMethod.IsEqual("Smooth") ||
565                 aMethod.IsEqual("SmoothParametric")) {
566           int anArgIndex = aCmd->GetNbArgs() - 2;
567           for(int j = 0; j < aCurrentStateSize; j++) {
568             if(!aCurrentState.at(j).IsEmpty())
569               aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
570           }
571           aStates->IncrementState();
572         }
573       }
574     }
575     if(MYDEBUG) {
576       cout<<"Command after: "<< aCmd->GetString()<<endl;
577     }
578   }
579   
580   ProcessLayerDistribution();
581 }
582 //================================================================================
583 /*!
584  * \brief Private method
585  */
586 //================================================================================
587 void SMESH_NoteBook::InitObjectMap()
588 {
589   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
590   if(!aGen)
591     return;
592   
593   SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
594   if(aStudy->_is_nil())
595     return;
596   
597   SALOMEDS::SObject_var aSO = aStudy->FindComponent(aGen->ComponentDataType());
598   if(CORBA::is_nil(aSO))
599     return;
600   
601   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
602   char* aParameters;
603   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
604     SALOMEDS::SObject_var aSObject = Itr->Value();
605     SALOMEDS::GenericAttribute_var anAttr;
606     if ( aSObject->FindAttribute(anAttr, "AttributeString")) {
607       aParameters = SALOMEDS::AttributeString::_narrow(anAttr)->Value();
608       SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters);
609       if(MYDEBUG) {
610         cout<<"Entry : "<< aSObject->GetID()<<endl;
611         cout<<"aParameters : "<<aParameters<<endl;
612       }      
613       TCollection_AsciiString anObjType;
614       CORBA::Object_var anObject = SMESH_Gen_i::SObjectToObject(aSObject);
615       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
616       if(!aHyp->_is_nil()) {
617         anObjType = TCollection_AsciiString(aHyp->GetName());
618       }
619       else if(SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObject)) {
620         anObjType = TCollection_AsciiString("Mesh");
621       }
622       if(MYDEBUG)
623         cout<<"The object Type : "<<anObjType<<endl;
624       ObjectStates *aState = NULL;
625       if(anObjType == "LayerDistribution") {
626         aState = new LayerDistributionStates();
627       }
628       else
629         aState = new  ObjectStates(anObjType);
630       
631       for(int i = 0; i < aSections->length(); i++) {
632         TState aVars;
633         SALOMEDS::ListOfStrings aListOfVars = aSections[i];
634         for(int j = 0;j<aListOfVars.length();j++) {
635           TCollection_AsciiString aVar(aListOfVars[j].in());
636           if(!aVar.IsEmpty() && aStudy->IsVariable(aVar.ToCString())) {
637             aVar.InsertBefore(1,"\"");
638             aVar.InsertAfter(aVar.Length(),"\"");
639           }
640           aVars.push_back(aVar);
641           if(MYDEBUG) {
642             cout<<"Variable: '"<<aVar<<"'"<<endl;
643           }
644         }
645         aState->AddState(aVars);
646       }
647       _objectMap.insert(pair<TCollection_AsciiString,ObjectStates*>(TCollection_AsciiString(aSObject->GetID()),aState));
648     }
649   }
650 }
651
652 //================================================================================
653 /*!
654  * 
655  */
656 //================================================================================
657 void SMESH_NoteBook::AddCommand(const TCollection_AsciiString& theString)
658 {
659   if(MYDEBUG)
660     cout<<theString<<endl;
661   Handle(_pyCommand) aCommand = new _pyCommand( theString, -1);
662   _commands.push_back(aCommand);
663
664   if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
665     myMeshEditors.insert( make_pair( aCommand->GetResultValue(),
666                                      aCommand->GetObject() ) );
667   }
668 }
669
670 //================================================================================
671 /*!
672  * 
673  */
674 //================================================================================
675 void SMESH_NoteBook::ProcessLayerDistribution()
676 {
677   // 1) Find all LayerDistribution states
678   vector<LayerDistributionStates*> aLDS;
679   TVariablesMap::const_iterator it = _objectMap.begin();
680   for(;it != _objectMap.end();it++) {
681     LayerDistributionStates* aLDStates = dynamic_cast<LayerDistributionStates*>(((*it).second));
682     if(aLDStates!=NULL) {
683       aLDS.push_back(aLDStates);
684     }
685   }
686   
687   if(!aLDS.size())
688     return;
689   
690   // 2) Initialize all type of 1D Distribution hypothesis
691   for(int i=0;i<_commands.size();i++){
692     for(int j =0;j < aLDS.size();j++){
693       TCollection_AsciiString aResultValue = _commands[i]->GetResultValue();
694       if(_commands[i]->GetMethod() == "CreateHypothesis" &&
695          aLDS[j]->HasDistribution(aResultValue)){
696         TCollection_AsciiString aType = _commands[i]->GetArg(1);
697         aType.RemoveAll('\'');
698         aLDS[j]->SetDistributionType(aResultValue,aType);
699       }
700     }
701   }
702   // 3) ... and replase variables ...
703
704   for(int i=0;i<_commands.size();i++){
705     for(int j =0;j < aLDS.size();j++){
706       TCollection_AsciiString anObject = _commands[i]->GetObject();
707
708       if(aLDS[j]->HasDistribution(anObject)) {
709         TCollection_AsciiString aType = aLDS[j]->GetDistributionType(anObject);
710         TCollection_AsciiString aMethod = _commands[i]->GetMethod();
711         if(aType == "LocalLength") {
712           if(aMethod == "SetLength") {
713             SetVariable(_commands[i], aLDS[j],0,1);
714             aLDS[j]->IncrementState();
715           }
716           else if(aMethod == "SetPrecision") {
717             SetVariable(_commands[i], aLDS[j],1,1);
718             aLDS[j]->IncrementState();
719           }
720         }
721
722         // Case for NumberOfSegments hypothesis
723         else if(aType == "NumberOfSegments"){
724           if(aMethod == "SetNumberOfSegments") {
725             SetVariable(_commands[i], aLDS[j],0,1);
726             if(aLDS[j]->GetCurrectState().size()==1)
727               aLDS[j]->IncrementState();
728           }
729           else if (aMethod == "SetScaleFactor") {
730             SetVariable(_commands[i], aLDS[j],1,1);
731             aLDS[j]->IncrementState();
732           }
733         }
734         
735         else if( aType == "Deflection1D" ){
736           if(aMethod == "SetDeflection"){
737             SetVariable(_commands[i], aLDS[j],0,1);
738             aLDS[j]->IncrementState();
739           }
740         }
741         // Case for Arithmetic1D and StartEndLength hypothesis
742         else if(aType == "Arithmetic1D" || aType == "StartEndLength") {
743           if(aMethod == "SetLength") {
744             int anArgNb = (_commands[i]->GetArg(2) == "1") ? 0 : 1;
745             SetVariable(_commands[i], aLDS[j],anArgNb,1);
746             aLDS[j]->IncrementState();
747           }
748         }
749       }
750     }
751   }
752 }
753 //================================================================================
754 /*!
755  *  \brief Return result script
756  */
757 //================================================================================
758 TCollection_AsciiString SMESH_NoteBook::GetResultScript() const
759 {
760   TCollection_AsciiString aResult;
761   for(int i=0;i<_commands.size();i++)
762     aResult+=_commands[i]->GetString()+"\n";
763   return aResult;
764 }
765
766 //================================================================================
767 /*!
768  *  \brief Return value of the variable
769  */
770 //================================================================================
771 bool SMESH_NoteBook::GetReal(const TCollection_AsciiString& theVarName, double& theValue)
772 {
773   bool ok = false;
774
775   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
776   if(!aGen)
777     return ok;
778
779   SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
780   if(aStudy->_is_nil())
781     return ok;
782
783   TCollection_AsciiString aVarName = theVarName;
784   aVarName.RemoveAll('\"');
785
786   if(aVarName.IsEmpty())
787     return ok;
788
789   const char* aName = aVarName.ToCString();
790   if(aStudy->IsVariable(aName) && (aStudy->IsReal(aName) || aStudy->IsInteger(aName))) {
791     theValue = aStudy->GetReal(aVarName.ToCString());
792     ok = true;
793   }
794
795   return ok;
796 }
797
798
799 /*!
800  *  Set variable of the ObjectStates from position to the _pyCommand
801  *  method as nbArg argument
802  */
803 void SetVariable(Handle(_pyCommand) theCommand, const ObjectStates* theStates, int position, int theArgNb)
804 {
805   if(theStates->GetCurrectState().size() > position)
806     if(!theStates->GetCurrectState().at(position).IsEmpty())
807       theCommand->SetArg(theArgNb,theStates->GetCurrectState().at(position));
808 }