Salome HOME
Merge from V6_main 11/02/2013
[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     Handle(_pyCommand) aCmd = _commands[i];
241     TCollection_AsciiString aMethod = aCmd->GetMethod();
242     TCollection_AsciiString aObject = aCmd->GetObject();
243     TCollection_AsciiString aResultValue = aCmd->GetResultValue();
244     if(MYDEBUG) {
245       cout<<"Command before : "<< aCmd->GetString()<<endl;
246       cout<<"Method : "<< aMethod<<endl;
247       cout<<"Object : "<< aObject<<endl;
248       cout<<"Result : "<< aResultValue<<endl;
249     }
250
251     // check if method modifies the object itself
252     TVariablesMap::const_iterator it = _objectMap.find(aObject);
253     if(it == _objectMap.end()) // check if method returns a new object
254       it = _objectMap.find(aResultValue);
255     
256     if(it == _objectMap.end()) { // check if method modifies a mesh using mesh editor
257       TMeshEditorMap::const_iterator meIt = myMeshEditors.find(aObject);
258       if(meIt != myMeshEditors.end()) {
259         TCollection_AsciiString aMesh = (*meIt).second;
260         it = _objectMap.find(aMesh);
261       }
262     }
263     
264     if(it == _objectMap.end()) { // additional check for pattern mapping
265       if(aMethod.IsEqual("ApplyToMeshFaces") ||
266          aMethod.IsEqual("ApplyToHexahedrons"))
267         it = _objectMap.find(aCmd->GetArg(1));
268     }
269
270     if(it != _objectMap.end()) {
271       if(MYDEBUG)
272         cout << "Found object : " << (*it).first << endl;
273       SMESH_ObjectStates *aStates = (*it).second;
274       // Case for LocalLength hypothesis
275       if(aStates->GetObjectType().IsEqual("LocalLength") && aStates->GetCurrectState().size() >= 2)
276       {
277         if(aMethod.IsEqual("SetLength")) {
278           if(!aStates->GetCurrectState().at(0).IsEmpty() )
279             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
280           aStates->IncrementState();
281         }
282         else if(aMethod.IsEqual("SetPrecision")) {
283           if(!aStates->GetCurrectState().at(1).IsEmpty() )
284             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
285           aStates->IncrementState();
286         }
287       }
288       
289       // Case for SegmentLengthAroundVertex hypothesis
290       else if(aStates->GetObjectType().IsEqual("SegmentLengthAroundVertex")
291               && aStates->GetCurrectState().size() >= 1) {
292         if(aMethod == "SetLength") {
293           if(!aStates->GetCurrectState().at(0).IsEmpty() )
294             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
295           aStates->IncrementState();
296         }
297       }
298
299       // Case for Arithmetic1D and StartEndLength hypothesis
300       else if(aStates->GetObjectType().IsEqual("Arithmetic1D") || 
301               aStates->GetObjectType().IsEqual("StartEndLength")) {
302         if(aMethod == "SetLength" &&
303            aStates->GetCurrectState().size() >= 2) {
304           if(aCmd->GetArg(2) == "1" && !aStates->GetCurrectState().at(0).IsEmpty())
305             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
306           else if(!aStates->GetCurrectState().at(1).IsEmpty())
307             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
308           aStates->IncrementState();
309         }
310       }
311       
312       //Case for Deflection1D hypothesis
313       else if(aStates->GetObjectType().IsEqual("Deflection1D")){
314         if(aMethod == "SetDeflection" && 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 LayerDistribution hypothesis (not finished yet)
322       else if(aStates->GetObjectType() == "LayerDistribution") {
323         if(aMethod == "SetLayerDistribution"){
324           LayerDistributionStates* aLDStates = (LayerDistributionStates*)(aStates);
325           aLDStates->AddDistribution(aCmd->GetArg(1));
326           if(MYDEBUG)
327             cout<<"Add Distribution :"<<aCmd->GetArg(1)<<endl;
328         }
329       }
330       
331       // Case for MaxElementArea hypothesis
332       else if(aStates->GetObjectType().IsEqual("MaxElementArea")){
333         if(aMethod == "SetMaxElementArea" && aStates->GetCurrectState().size() >= 1) {
334           if(!aStates->GetCurrectState().at(0).IsEmpty() )
335             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
336           aStates->IncrementState();
337         }
338       }
339
340       // Case for MaxElementVolume hypothesis
341       else if(aStates->GetObjectType().IsEqual("MaxElementVolume")){
342         if(aMethod == "SetMaxElementVolume" && aStates->GetCurrectState().size() >= 1) {
343           if(!aStates->GetCurrectState().at(0).IsEmpty() )
344             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
345           aStates->IncrementState();
346         }
347       }
348       // Case for NumberOfLayers hypothesis
349       else if(aStates->GetObjectType().IsEqual("NumberOfLayers")){
350         if(aMethod == "SetNumberOfLayers" && aStates->GetCurrectState().size() >= 1) {
351           if(!aStates->GetCurrectState().at(0).IsEmpty() )
352             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
353           aStates->IncrementState();
354         }
355       }
356
357       // Case for NumberOfSegments hypothesis
358       else if(aStates->GetObjectType().IsEqual("NumberOfSegments")){
359         if(aMethod == "SetNumberOfSegments" && aStates->GetCurrectState().size() >= 1) {
360           if(!aStates->GetCurrectState().at(0).IsEmpty() )
361             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
362           if(aStates->GetCurrectState().size()==1)
363             aStates->IncrementState();
364         }
365         else if (aMethod == "SetScaleFactor" && aStates->GetCurrectState().size() >= 2) {
366           if(!aStates->GetCurrectState().at(1).IsEmpty() )
367             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
368           aStates->IncrementState();
369         }
370       }
371       
372       else if(aStates->GetObjectType().IsEqual("Mesh")) {
373         TState aCurrentState = aStates->GetCurrectState();
374         int aCurrentStateSize = aCurrentState.size();
375         if(aMethod.IsEqual("Translate")                  ||
376            aMethod.IsEqual("TranslateMakeGroups")        ||
377            aMethod.IsEqual("TranslateMakeMesh")          ||
378            aMethod.IsEqual("TranslateObject")            ||
379            aMethod.IsEqual("TranslateObjectMakeGroups")  ||
380            aMethod.IsEqual("TranslateObjectMakeMesh")) {
381           bool isVariableFound = false;
382           int anArgIndex = 0;
383           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
384             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
385               anArgIndex = i+1;
386               break;
387             }
388           }
389           if(anArgIndex > 0) {
390             if(aCurrentStateSize == 3) { // translation by dx, dy, dz
391               for(int j = 0; j < aCurrentStateSize; j++) {
392                 if(!aCurrentState.at(j).IsEmpty()) {
393                   isVariableFound = true;
394                   aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
395                 }
396               }
397             }
398             else if(aCurrentStateSize == 6) { // translation by x1, x2, y1, y2, z1, z2
399               // TODO: limitation until operations on the variables will be introduced
400               /*
401               isVariableFound = true;
402               for(int j = 0; j < 3; j++) {
403                 TCollection_AsciiString anArg = aCmd->GetArg(anArgIndex+j);
404                 TCollection_AsciiString aValue1 = aCurrentState.at(2*j), aValue2 = aCurrentState.at(2*j+1);
405                 bool aV1 = !aValue1.IsEmpty();
406                 bool aV2 = !aValue2.IsEmpty();
407                 double aValue, aCurrentValue = anArg.IsRealValue() ? anArg.RealValue() : 0;
408                 if(aV1 && !aV2) {
409                   if(!GetReal(aValue1, aValue))
410                     aValue = 0;
411                   aValue2 = TCollection_AsciiString( aValue + aCurrentValue );
412                 }
413                 else if(!aV1 && aV2) {
414                   if(!GetReal(aValue2, aValue))
415                     aValue = 0;
416                   aValue1 = TCollection_AsciiString( aValue - aCurrentValue );
417                 }
418                 else if(!aV1 && !aV2) {
419                   aValue1 = TCollection_AsciiString( 0 );
420                   aValue2 = TCollection_AsciiString( aCurrentValue );
421                 }
422                 aCmd->SetArg(anArgIndex+j, aValue1 + ", " + aValue2 );
423               }
424               */
425             }
426           }
427           if(isVariableFound) {
428             TCollection_AsciiString aDim;
429             if(aCurrentStateSize == 6)
430               aDim = "6";
431             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr"+aDim);
432             aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
433           }
434           aStates->IncrementState();
435         }
436         else if(aMethod.IsEqual("Rotate")                  ||
437                 aMethod.IsEqual("RotateMakeGroups")        ||
438                 aMethod.IsEqual("RotateMakeMesh")          ||
439                 aMethod.IsEqual("RotateObject")            ||
440                 aMethod.IsEqual("RotateObjectMakeGroups")  ||
441                 aMethod.IsEqual("RotateObjectMakeMesh")    ||
442                 aMethod.IsEqual("RotationSweep")           ||
443                 aMethod.IsEqual("RotationSweepObject")     ||
444                 aMethod.IsEqual("RotationSweepObject1D")   ||
445                 aMethod.IsEqual("RotationSweepObject2D")   ||
446                 aMethod.IsEqual("RotationSweepMakeGroups") ||
447                 aMethod.IsEqual("RotationSweepObjectMakeGroups") ||
448                 aMethod.IsEqual("RotationSweepObject1DMakeGroups") ||
449                 aMethod.IsEqual("RotationSweepObject2DMakeGroups") ||
450                 aMethod.IsEqual("Mirror")                  ||
451                 aMethod.IsEqual("MirrorMakeMesh")          ||
452                 aMethod.IsEqual("MirrorMakeGroups")        ||
453                 aMethod.IsEqual("MirrorObject")            || 
454                 aMethod.IsEqual("MirrorObjectMakeMesh")    ||
455                 aMethod.IsEqual("MirrorObjectMakeGroups")) {
456           bool isSubstitute = false;
457           int anArgIndex = 0;
458           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
459             if(aCmd->GetArg(i).IsEqual("SMESH.AxisStruct")) {
460               anArgIndex = i+1;
461               break;
462             }
463           }
464           if(anArgIndex > 0) {
465             for(int j = 0; j < aCurrentStateSize; j++) {
466               if(!aCurrentState.at(j).IsEmpty()) {
467                 if(j < 6) // 0-5 - axis struct, 6 - angle (rotation & sweep), 7-8 - nbSteps and tolerance (sweep)
468                   isSubstitute = true;
469                 aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
470               }
471             }
472           }
473           if(isSubstitute)
474             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".AxisStructStr");
475           aStates->IncrementState();
476         }
477         else if(aMethod.IsEqual("AddNode") ||
478                 aMethod.IsEqual("MoveClosestNodeToPoint")) {
479           for(int j = 0; j < aCurrentStateSize; j++) {
480             if(!aCurrentState.at(j).IsEmpty())
481               aCmd->SetArg(j+1, aCurrentState.at(j));
482           }
483           aStates->IncrementState();
484         }
485         else if(aMethod.IsEqual("MoveNode")) {
486           for(int j = 0; j < aCurrentStateSize; j++) {
487             if(!aCurrentState.at(j).IsEmpty())
488               aCmd->SetArg(j+2, aCurrentState.at(j));
489           }
490           aStates->IncrementState();
491         }
492         else if(aMethod.IsEqual("ExtrusionSweep") ||
493                 aMethod.IsEqual("ExtrusionSweepObject") ||
494                 aMethod.IsEqual("ExtrusionSweepObject1D") ||
495                 aMethod.IsEqual("ExtrusionSweepObject2D") ||
496                 aMethod.IsEqual("ExtrusionSweepMakeGroups") ||
497                 aMethod.IsEqual("ExtrusionSweepObjectMakeGroups") ||
498                 aMethod.IsEqual("ExtrusionSweepObject1DMakeGroups") ||
499                 aMethod.IsEqual("ExtrusionSweepObject2DMakeGroups")) {
500           bool isSubstitute = false;
501           int anArgIndex = 0;
502           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
503             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
504               anArgIndex = i+1;
505               break;
506             }
507           }
508           if(anArgIndex > 0) {
509             for(int j = 0; j < aCurrentStateSize; j++) {
510               if(!aCurrentState.at(j).IsEmpty()) {
511                 if(j < 3) // 0-2 - dir struct, 3 - number of steps
512                   isSubstitute = true;
513                 aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
514               }
515             }
516           }
517           if(isSubstitute) {
518             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
519             aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
520           }
521           aStates->IncrementState();
522         }
523         else if(aMethod.IsEqual("ExtrusionAlongPath") ||
524                 aMethod.IsEqual("ExtrusionAlongPathObject") ||
525                 aMethod.IsEqual("ExtrusionAlongPathObject1D") ||
526                 aMethod.IsEqual("ExtrusionAlongPathObject2D") ||
527                 aMethod.IsEqual("ExtrusionAlongPathMakeGroups") ||
528                 aMethod.IsEqual("ExtrusionAlongPathObjectMakeGroups") ||
529                 aMethod.IsEqual("ExtrusionAlongPathObject1DMakeGroups") ||
530                 aMethod.IsEqual("ExtrusionAlongPathObject2DMakeGroups") ||
531                 /* workaround for a bug in the command parsing algorithm */
532                 aCmd->GetString().Search("ExtrusionAlongPathMakeGroups") != -1 ||
533                 aCmd->GetString().Search("ExtrusionAlongPathObjectMakeGroups") != -1 ||
534                 aCmd->GetString().Search("ExtrusionAlongPathObject1DMakeGroups") != -1 ||
535                 aCmd->GetString().Search("ExtrusionAlongPathObject2DMakeGroups") != -1 ) {
536           int aNbAngles = aCurrentStateSize-3; // State looks like "Angle1:...:AngleN:X:Y:Z"
537           bool isSubstitute = false;
538           int anArgIndex = 0;
539           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
540             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
541               anArgIndex = i-1-aNbAngles;
542               break;
543             }
544           }
545           if(anArgIndex > 0) {
546             int j = 0;
547             for(; j < aNbAngles; j++) {
548               if(!aCurrentState.at(j).IsEmpty()) {
549                 aCmd->SetArg(anArgIndex+j-1, aCurrentState.at(j));
550               }
551             }
552             for(; j < aNbAngles+3; j++) {
553               if(!aCurrentState.at(j).IsEmpty()) {
554                 isSubstitute = true;
555                 aCmd->SetArg(anArgIndex+j+2, aCurrentState.at(j));
556               }
557             }
558           }
559           if(isSubstitute)
560             aCmd->SetArg(anArgIndex + aNbAngles + 1,
561                          TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
562           aStates->IncrementState();
563         }
564         else if(aMethod.IsEqual("TriToQuad") ||
565                 aMethod.IsEqual("Concatenate") ||
566                 aMethod.IsEqual("ConcatenateWithGroups")) {
567           if(aCurrentStateSize && !aCurrentState.at(0).IsEmpty())
568             aCmd->SetArg(aCmd->GetNbArgs(), aCurrentState.at(0));
569           aStates->IncrementState();
570         }
571         else if(aMethod.IsEqual("Smooth") ||
572                 aMethod.IsEqual("SmoothObject") ||
573                 aMethod.IsEqual("SmoothParametric") ||
574                 aMethod.IsEqual("SmoothParametricObject")) {
575           int anArgIndex = aCmd->GetNbArgs() - 2;
576           for(int j = 0; j < aCurrentStateSize; j++) {
577             if(!aCurrentState.at(j).IsEmpty())
578               aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
579           }
580           aStates->IncrementState();
581         }
582         else if(aMethod.IsEqual("ApplyToMeshFaces") ||
583                 aMethod.IsEqual("ApplyToHexahedrons")) {
584           int anArgIndex = aCmd->GetNbArgs()-1;
585           for(int j = 0; j < aCurrentStateSize; j++)
586             if(!aCurrentState.at(j).IsEmpty())
587               aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
588           aStates->IncrementState();
589         }
590       } // if ( aStates->GetObjectType().IsEqual("Mesh"))
591
592       // Case for NETGEN_Parameters_2D or NETGEN_Parameters_2D hypothesis
593       // else if(aStates->GetObjectType().IsEqual("NETGEN_Parameters_2D") ||
594       //         aStates->GetObjectType().IsEqual("NETGEN_Parameters")){
595       //   if(aMethod == "SetMaxSize" && aStates->GetCurrectState().size() >= 1) {
596       //     if(!aStates->GetCurrectState().at(0).IsEmpty() )
597       //       aCmd->SetArg(1,aStates->GetCurrectState().at(0));
598       //     aStates->IncrementState();
599       //   }
600       //   else if(aMethod == "SetGrowthRate" && aStates->GetCurrectState().size() >= 2) {
601       //     if(!aStates->GetCurrectState().at(1).IsEmpty() )
602       //       aCmd->SetArg(1,aStates->GetCurrectState().at(1));
603       //     aStates->IncrementState();
604       //   }
605       //   else if(aMethod == "SetNbSegPerEdge" && aStates->GetCurrectState().size() >= 3) {
606       //     if(!aStates->GetCurrectState().at(2).IsEmpty() )
607       //       aCmd->SetArg(1,aStates->GetCurrectState().at(2));
608       //     aStates->IncrementState();
609       //   }
610       //   else if(aMethod == "SetNbSegPerRadius" && aStates->GetCurrectState().size() >= 4) {
611       //     if(!aStates->GetCurrectState().at(3).IsEmpty() )
612       //       aCmd->SetArg(1,aStates->GetCurrectState().at(3));
613       //     aStates->IncrementState();
614       //   }
615       // }
616
617       // // Case for NETGEN_SimpleParameters_3D or NETGEN_SimpleParameters_2D hypothesis
618       // else if(aStates->GetObjectType().IsEqual("NETGEN_SimpleParameters_3D") ||
619       //         aStates->GetObjectType().IsEqual("NETGEN_SimpleParameters_2D")) {
620
621       //   if((aMethod == "SetNumberOfSegments" || aMethod == "SetLocalLength") && 
622       //      aStates->GetCurrectState().size() >= 1) {
623       //     if(!aStates->GetCurrectState().at(0).IsEmpty() )
624       //       aCmd->SetArg(1,aStates->GetCurrectState().at(0));
625       //     aStates->IncrementState();
626       //   }
627       //   else if(aMethod == "SetMaxElementArea" && aStates->GetCurrectState().size() >= 2) {
628       //     if(!aStates->GetCurrectState().at(1).IsEmpty() )
629       //       aCmd->SetArg(1,aStates->GetCurrectState().at(1));
630       //     aStates->IncrementState();
631       //   }
632       //   else if(aMethod == "SetMaxElementVolume" && aStates->GetCurrectState().size() >= 3) {
633       //     if(!aStates->GetCurrectState().at(2).IsEmpty() )
634       //       aCmd->SetArg(1,aStates->GetCurrectState().at(2));
635       //     aStates->IncrementState();
636       //   }
637       //   else if(aMethod == "LengthFromEdges" || aMethod == "LengthFromFaces"){
638       //     aStates->IncrementState();
639       //   }
640       // }
641
642       else
643       {
644         // treat Netgen hypotheses;
645         // this (and above) code can work wrong since nb of states can differ from nb of
646         // dumped calls due to the fix of
647         // issue 0021364:: Dump of netgen parameters has duplicate lines
648         SMESH_Gen_i *          aGen = SMESH_Gen_i::GetSMESHGen();
649         SALOMEDS::Study_var  aStudy = aGen->GetCurrentStudy();
650         SALOMEDS::SObject_wrap sobj = aStudy->FindObjectID( (*it).first.ToCString() );
651         CORBA::Object_var       obj = aGen->SObjectToObject( sobj );
652         if ( SMESH_Hypothesis_i* h = SMESH::DownCast< SMESH_Hypothesis_i*>( obj ))
653         {
654           TState aCurrentState = aStates->GetCurrectState();
655           int         argIndex = h->getParamIndex( aMethod, aCurrentState.size() );
656           if ( 0 <= argIndex && argIndex < aCurrentState.size() &&
657                !aCurrentState[argIndex].IsEmpty() )
658             aCmd->SetArg( 1, aCurrentState[argIndex] );
659
660           if ( argIndex >= 0 )
661             aStates->IncrementState();
662         }
663       }
664     }
665     else {
666       if(MYDEBUG)
667         cout << "Object not found" << endl;
668     }
669     if(MYDEBUG) {
670       cout<<"Command after: "<< aCmd->GetString()<<endl;
671     }
672   }
673   
674   ProcessLayerDistribution();
675 }
676 //================================================================================
677 /*!
678  * \brief Private method
679  */
680 //================================================================================
681 void SMESH_NoteBook::InitObjectMap()
682 {
683   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
684   if(!aGen)
685     return;
686   
687   SALOMEDS::Study_var aStudy = aGen->GetCurrentStudy();
688   if(aStudy->_is_nil())
689     return;
690   
691   CORBA::String_var compDataType = aGen->ComponentDataType();
692   SALOMEDS::SObject_wrap     aSO = aStudy->FindComponent( compDataType.in() );
693   if(CORBA::is_nil(aSO))
694     return;
695   
696   SALOMEDS::ChildIterator_wrap Itr = aStudy->NewChildIterator(aSO);
697   CORBA::String_var aParameters;
698   for( Itr->InitEx(true); Itr->More(); Itr->Next())
699   {
700     SALOMEDS::SObject_wrap aSObject = Itr->Value();
701     SALOMEDS::GenericAttribute_wrap anAttr;
702     if ( aSObject->FindAttribute( anAttr.inout(), "AttributeString"))
703     {
704       SALOMEDS::AttributeString_wrap strAttr = anAttr;
705       aParameters = strAttr->Value();
706       SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters.in());
707       if(MYDEBUG) {
708         cout<<"Entry : "<< aSObject->GetID()<<endl;
709         cout<<"aParameters : "<<aParameters<<endl;
710       }      
711       TCollection_AsciiString anObjType;
712       CORBA::Object_var       anObject = SMESH_Gen_i::SObjectToObject(aSObject);
713       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
714       SMESH::SMESH_Mesh_var      aMesh = SMESH::SMESH_Mesh::_narrow(anObject);
715       if(!aHyp->_is_nil()) {
716         CORBA::String_var hypName = aHyp->GetName();
717         anObjType = hypName.in();
718       }
719       else if (!aMesh->_is_nil() ) {
720         anObjType = "Mesh";
721       }
722       if(MYDEBUG)
723         cout<<"The object Type : "<<anObjType<<endl;
724       SMESH_ObjectStates *aState = NULL;
725       if(anObjType == "LayerDistribution")
726         aState = new LayerDistributionStates();
727       else
728         aState = new SMESH_ObjectStates(anObjType);
729
730       for(int i = 0; i < aSections->length(); i++) {
731         TState aVars;
732         SALOMEDS::ListOfStrings aListOfVars = aSections[i];
733         for ( int j = 0; j<aListOfVars.length(); j++)
734         {
735           TCollection_AsciiString aVar(aListOfVars[j].in());
736           if(!aVar.IsEmpty() && aStudy->IsVariable(aVar.ToCString())) {
737             aVar.InsertBefore(1,            SMESH::TVar::Quote() );
738             aVar.InsertAfter(aVar.Length(), SMESH::TVar::Quote() );
739           }
740           aVars.push_back(aVar);
741           if(MYDEBUG) {
742             cout<<"Variable: '"<<aVar<<"'"<<endl;
743           }
744         }
745         aState->AddState(aVars);
746       }
747       if ( aState->GetAllStates().empty() )
748       {
749         delete aState;
750       }
751       else
752       {
753         CORBA::String_var objID = aSObject->GetID();
754         _objectMap.insert( make_pair(TCollection_AsciiString( objID.in() ), aState ));
755       }
756     }
757   }
758 }
759
760 //================================================================================
761 /*!
762  * 
763  */
764 //================================================================================
765 void SMESH_NoteBook::AddCommand(const TCollection_AsciiString& theString)
766 {
767   if(MYDEBUG)
768     cout<<theString<<endl;
769   Handle(_pyCommand) aCommand = new _pyCommand( theString, -1);
770   _commands.push_back(aCommand);
771
772   if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
773     myMeshEditors.insert( make_pair( aCommand->GetResultValue(),
774                                      aCommand->GetObject() ) );
775   }
776 }
777
778 //================================================================================
779 /*!
780  * 
781  */
782 //================================================================================
783 void SMESH_NoteBook::ProcessLayerDistribution()
784 {
785   // 1) Find all LayerDistribution states
786   vector<LayerDistributionStates*> aLDS;
787   TVariablesMap::const_iterator it = _objectMap.begin();
788   for(;it != _objectMap.end();it++) {
789     LayerDistributionStates* aLDStates = dynamic_cast<LayerDistributionStates*>(((*it).second));
790     if(aLDStates!=NULL) {
791       aLDS.push_back(aLDStates);
792     }
793   }
794   
795   if(!aLDS.size())
796     return;
797   
798   // 2) Initialize all type of 1D Distribution hypothesis
799   for(int i=0;i<_commands.size();i++){
800     for(int j =0;j < aLDS.size();j++){
801       TCollection_AsciiString aResultValue = _commands[i]->GetResultValue();
802       if(_commands[i]->GetMethod() == "CreateHypothesis" &&
803          aLDS[j]->HasDistribution(aResultValue)){
804         TCollection_AsciiString aType = _commands[i]->GetArg(1);
805         aType.RemoveAll('\'');
806         aLDS[j]->SetDistributionType(aResultValue,aType);
807       }
808     }
809   }
810   // 3) ... and replase variables ...
811
812   for(int i=0;i<_commands.size();i++){
813     for(int j =0;j < aLDS.size();j++){
814       TCollection_AsciiString anObject = _commands[i]->GetObject();
815
816       if(aLDS[j]->HasDistribution(anObject)) {
817         TCollection_AsciiString aType = aLDS[j]->GetDistributionType(anObject);
818         TCollection_AsciiString aMethod = _commands[i]->GetMethod();
819         if(aType == "LocalLength") {
820           if(aMethod == "SetLength") {
821             SetVariable(_commands[i], aLDS[j],0,1);
822             aLDS[j]->IncrementState();
823           }
824           else if(aMethod == "SetPrecision") {
825             SetVariable(_commands[i], aLDS[j],1,1);
826             aLDS[j]->IncrementState();
827           }
828         }
829
830         // Case for NumberOfSegments hypothesis
831         else if(aType == "NumberOfSegments"){
832           if(aMethod == "SetNumberOfSegments") {
833             SetVariable(_commands[i], aLDS[j],0,1);
834             if(aLDS[j]->GetCurrectState().size()==1)
835               aLDS[j]->IncrementState();
836           }
837           else if (aMethod == "SetScaleFactor") {
838             SetVariable(_commands[i], aLDS[j],1,1);
839             aLDS[j]->IncrementState();
840           }
841         }
842         
843         else if( aType == "Deflection1D" ){
844           if(aMethod == "SetDeflection"){
845             SetVariable(_commands[i], aLDS[j],0,1);
846             aLDS[j]->IncrementState();
847           }
848         }
849         // Case for Arithmetic1D and StartEndLength hypothesis
850         else if(aType == "Arithmetic1D" || aType == "StartEndLength") {
851           if(aMethod == "SetLength") {
852             int anArgNb = (_commands[i]->GetArg(2) == "1") ? 0 : 1;
853             SetVariable(_commands[i], aLDS[j],anArgNb,1);
854             aLDS[j]->IncrementState();
855           }
856         }
857       }
858     }
859   }
860 }
861 //================================================================================
862 /*!
863  *  \brief Return result script
864  */
865 //================================================================================
866 TCollection_AsciiString SMESH_NoteBook::GetResultScript() const
867 {
868   TCollection_AsciiString aResult;
869   for(int i=0;i<_commands.size();i++)
870     aResult+=_commands[i]->GetString()+"\n";
871   return aResult;
872 }
873
874 //================================================================================
875 /*!
876  *  \brief Return value of the variable
877  */
878 //================================================================================
879 bool SMESH_NoteBook::GetReal(const TCollection_AsciiString& theVarName, double& theValue)
880 {
881   bool ok = false;
882
883   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
884   if(!aGen)
885     return ok;
886
887   SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
888   if(aStudy->_is_nil())
889     return ok;
890
891   TCollection_AsciiString aVarName = theVarName;
892   aVarName.RemoveAll('\"');
893
894   if(aVarName.IsEmpty())
895     return ok;
896
897   const char* aName = aVarName.ToCString();
898   if(aStudy->IsVariable(aName) && (aStudy->IsReal(aName) || aStudy->IsInteger(aName))) {
899     theValue = aStudy->GetReal(aName);
900     ok = true;
901   }
902
903   return ok;
904 }
905