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 = 1;
37 #else
38 static int MYDEBUG = 1;
39 #endif
40
41 using namespace std;
42
43
44
45 //================================================================================
46 /*!
47  * \brief Constructor
48  */
49 //================================================================================
50 ObjectStates::ObjectStates(TCollection_AsciiString theType)
51 {
52   _type = theType;
53   _dumpstate = 0;
54 }
55
56 //================================================================================
57 /*!
58  * \brief Destructor
59  */
60 //================================================================================
61 ObjectStates::~ObjectStates()
62 {
63 }
64
65 //================================================================================
66 /*!
67  * \brief Add new object state 
68  * \param theState - Object state (vector of notebook variable)
69  */
70 //================================================================================
71 void ObjectStates::AddState(const TState &theState)
72 {
73   _states.push_back(theState);
74 }
75
76 //================================================================================
77 /*!
78  * \brief Return current object state
79  * \\retval state - Object state (vector of notebook variable)
80  */
81 //================================================================================
82 TState ObjectStates::GetCurrectState() const
83 {
84   if(_states.size() > _dumpstate)
85     return _states[_dumpstate];
86   TState empty;
87   return empty;
88 }
89
90
91 //================================================================================
92 /*!
93  *
94  */
95 //================================================================================
96 TAllStates ObjectStates::GetAllStates() const
97 {
98   return _states;
99 }
100
101 //================================================================================
102 /*!
103  *
104  */
105 //================================================================================
106 void ObjectStates::IncrementState()
107 {
108   _dumpstate++;
109 }
110
111 //================================================================================
112 /*!
113  *
114  */
115 //================================================================================
116 TCollection_AsciiString ObjectStates::GetObjectType() const{
117   return _type;
118 }
119
120
121 //================================================================================
122 /*!
123  * \brief Constructor
124  */
125 //================================================================================
126 LayerDistributionStates::LayerDistributionStates():
127   ObjectStates("LayerDistribution")
128 {
129 }
130 //================================================================================
131 /*!
132  * \brief Destructor
133  */
134 //================================================================================
135 LayerDistributionStates::~LayerDistributionStates()
136 {
137 }
138
139
140 //================================================================================
141 /*!
142  * \brief AddDistribution
143  */
144 //================================================================================
145 void LayerDistributionStates::AddDistribution(const TCollection_AsciiString& theDistribution)
146 {
147   _distributions.insert(pair<TCollection_AsciiString,TCollection_AsciiString>(theDistribution,""));
148 }
149
150 //================================================================================
151 /*!
152  * \brief HasDistribution
153  */
154 //================================================================================
155 bool LayerDistributionStates::HasDistribution(const TCollection_AsciiString& theDistribution) const
156 {
157   return _distributions.find(theDistribution) != _distributions.end();
158 }
159
160 //================================================================================
161 /*!
162  * \brief SetDistributionType
163  */
164 //================================================================================
165 bool LayerDistributionStates::SetDistributionType(const TCollection_AsciiString& theDistribution,
166                                                   const TCollection_AsciiString& theType)
167 {
168   TDistributionMap::iterator it = _distributions.find(theDistribution);
169   if(it == _distributions.end())
170     return false;
171   (*it).second = theType;
172   return true;
173 }
174
175 //================================================================================
176 /*!
177  * \brief GetDistributionType
178  */
179 //================================================================================
180 TCollection_AsciiString LayerDistributionStates::
181 GetDistributionType(const TCollection_AsciiString& theDistribution) const
182 {
183   TDistributionMap::const_iterator it = _distributions.find(theDistribution);
184   return (it == _distributions.end()) ? TCollection_AsciiString() : (*it).second;
185 }
186
187 //================================================================================
188 /*!
189  * \brief Constructor
190  */
191 //================================================================================
192 SMESH_NoteBook::SMESH_NoteBook()
193 {
194   InitObjectMap();
195 }
196
197 //================================================================================
198 /*!
199  * \brief Destructor
200  */
201 //================================================================================
202 SMESH_NoteBook::~SMESH_NoteBook()
203 {
204   TVariablesMap::const_iterator it = _objectMap.begin();
205   for(;it!=_objectMap.end();it++) {
206     if((*it).second)
207       delete (*it).second;
208   }
209 }
210
211 //================================================================================
212 /*!
213  * \brief Replace parameters of the functions on the Salome NoteBook Variables
214  * \param theString - Input string
215  * \retval TCollection_AsciiString - Convertion result
216  */
217 //================================================================================
218 void SMESH_NoteBook::ReplaceVariables()
219 {
220   for(int i=0;i<_commands.size();i++) {
221     Handle(_pyCommand) aCmd = _commands[i];
222     TCollection_AsciiString aMethod = aCmd->GetMethod();
223     TCollection_AsciiString aObject = aCmd->GetObject();
224     TCollection_AsciiString aResultValue = aCmd->GetResultValue();
225     if(MYDEBUG) {
226       cout<<"Command before : "<< aCmd->GetString()<<endl;
227       cout<<"Method : "<< aMethod<<endl;
228       cout<<"Object : "<< aObject<<endl;
229       cout<<"Result : "<< aResultValue<<endl;
230     }
231     
232     // check if method modifies the object itself
233     TVariablesMap::const_iterator it = _objectMap.find(aObject);
234     if(it == _objectMap.end()) // check if method returns a new object
235       it = _objectMap.find(aResultValue);
236     
237     if(it == _objectMap.end()) { // check if method modifies a mesh using mesh editor
238       TMeshEditorMap::const_iterator meIt = myMeshEditors.find(aObject);
239       if(meIt != myMeshEditors.end()) {
240         TCollection_AsciiString aMesh = (*meIt).second;
241         it = _objectMap.find(aMesh);
242       }
243     }
244     
245     if(it != _objectMap.end() && !aMethod.IsEmpty()) {
246       ObjectStates *aStates = (*it).second;
247       // Case for LocalLength hypothesis
248       if(aStates->GetObjectType().IsEqual("LocalLength") && aStates->GetCurrectState().size() >= 2) {
249         if(aMethod.IsEqual("SetLength")) {
250           if(!aStates->GetCurrectState().at(0).IsEmpty() )
251             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
252           aStates->IncrementState();
253         }
254         else if(aMethod.IsEqual("SetPrecision")) {
255           if(!aStates->GetCurrectState().at(1).IsEmpty() )
256             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
257           aStates->IncrementState();
258         }
259       }
260       
261       // Case for SegmentLengthAroundVertex hypothesis
262       else if(aStates->GetObjectType().IsEqual("SegmentLengthAroundVertex")
263               && aStates->GetCurrectState().size() >= 1) {
264         if(aMethod == "SetLength") {
265           if(!aStates->GetCurrectState().at(0).IsEmpty() )
266             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
267           aStates->IncrementState();
268         }
269       }
270       // Case for Arithmetic1D or StartEndLength hypothesis
271       else if(aStates->GetObjectType().IsEqual("Arithmetic1D") || 
272               aStates->GetObjectType().IsEqual("StartEndLength")) {
273         if(aMethod == "SetLength" &&
274            aStates->GetCurrectState().size() >= 2) {
275           if(aCmd->GetArg(2) == "1" && !aStates->GetCurrectState().at(0).IsEmpty())
276             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
277           else if(!aStates->GetCurrectState().at(1).IsEmpty())
278             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
279           aStates->IncrementState();
280         }
281       }
282       else if(aStates->GetObjectType().IsEqual("Deflection1D")){
283         if(aMethod == "SetDeflection" && aStates->GetCurrectState().size() >= 1) {
284           if(!aStates->GetCurrectState().at(0).IsEmpty() )
285             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
286           aStates->IncrementState();
287         }
288       }
289       // Case for LayerDistribution hypothesis (not finished yet)
290       else if(aStates->GetObjectType() == "LayerDistribution") {
291         if(aMethod == "SetLayerDistribution"){
292           LayerDistributionStates* aLDStates = (LayerDistributionStates*)(aStates);
293           aLDStates->AddDistribution(aCmd->GetArg(1));
294         }
295       }
296
297       else if(aStates->GetObjectType().IsEqual("Mesh")) {
298         TState aCurrentState = aStates->GetCurrectState();
299         int aCurrentStateSize = aCurrentState.size();
300         if(aMethod.IsEqual("Translate") ||
301            aMethod.IsEqual("TranslateMakeGroups") ||
302            aMethod.IsEqual("TranslateMakeMesh")) {
303           bool isVariableFound = false;
304           int anArgIndex = 0;
305           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
306             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
307               anArgIndex = i+1;
308               break;
309             }
310           }
311           if(anArgIndex > 0) {
312             if(aCurrentStateSize == 3) { // translation by dx, dy, dz
313               for(int j = 0; j < aCurrentStateSize; j++) {
314                 if(!aCurrentState.at(j).IsEmpty()) {
315                   isVariableFound = true;
316                   aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
317                 }
318               }
319             }
320             else if(aCurrentStateSize == 6) { // translation by x1, x2, y1, y2, z1, z2
321               // TODO
322             }
323           }
324           if(isVariableFound) {
325             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
326             aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
327           }
328           aStates->IncrementState();
329         }
330         else if(aMethod.IsEqual("Rotate") ||
331                 aMethod.IsEqual("RotateMakeGroups") ||
332                 aMethod.IsEqual("RotateMakeMesh") ||
333                 aMethod.IsEqual("Mirror") ||
334                 aMethod.IsEqual("MirrorMakeGroups") ||
335                 aMethod.IsEqual("MirrorMakeMesh")) {
336           bool isSubstitute = false;
337           int anArgIndex = 0;
338           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
339             if(aCmd->GetArg(i).IsEqual("SMESH.AxisStruct")) {
340               anArgIndex = i+1;
341               break;
342             }
343           }
344           if(anArgIndex > 0) {
345             for(int j = 0; j < aCurrentStateSize; j++) {
346               if(!aCurrentState.at(j).IsEmpty()) {
347                 if(j < 6) // from 0 to 5 - axis struct, 6 - angle
348                   isSubstitute = true;
349                 aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
350               }
351             }
352           }
353           if(isSubstitute)
354             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".AxisStructStr");
355           aStates->IncrementState();
356         }
357         else if(aMethod.IsEqual("AddNode") ||
358                 aMethod.IsEqual("MoveClosestNodeToPoint")) {
359           for(int j = 0; j < aCurrentStateSize; j++) {
360             if(!aCurrentState.at(j).IsEmpty())
361               aCmd->SetArg(j+1, aCurrentState.at(j));
362           }
363           aStates->IncrementState();
364         }
365         else if(aMethod.IsEqual("MoveNode")) {
366           for(int j = 0; j < aCurrentStateSize; j++) {
367             if(!aCurrentState.at(j).IsEmpty())
368               aCmd->SetArg(j+2, aCurrentState.at(j));
369           }
370           aStates->IncrementState();
371         }
372         else if(aMethod.IsEqual("ExtrusionSweep") ||
373                 aMethod.IsEqual("ExtrusionSweepMakeGroups")) {
374           bool isSubstitute = false;
375           int anArgIndex = 0;
376           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
377             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
378               anArgIndex = i+1;
379               break;
380             }
381           }
382           if(anArgIndex > 0) {
383             for(int j = 0; j < aCurrentStateSize; j++) {
384               if(!aCurrentState.at(j).IsEmpty()) {
385                 if(j < 3) // from 0 to 2 - dir struct, 3 - number of steps
386                   isSubstitute = true;
387                 aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
388               }
389             }
390           }
391           if(isSubstitute) {
392             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
393             aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
394           }
395           aStates->IncrementState();
396         }
397         else if(aMethod.IsEqual("TriToQuad") ||
398                 aMethod.IsEqual("Concatenate") ||
399                 aMethod.IsEqual("ConcatenateWithGroups")) {
400           if(aCurrentStateSize && !aCurrentState.at(0).IsEmpty())
401             aCmd->SetArg(aCmd->GetNbArgs(), aCurrentState.at(0));
402           aStates->IncrementState();
403         }
404         else if(aMethod.IsEqual("Smooth") ||
405                 aMethod.IsEqual("SmoothParametric")) {
406           int anArgIndex = aCmd->GetNbArgs() - 2;
407           for(int j = 0; j < aCurrentStateSize; j++) {
408             if(!aCurrentState.at(j).IsEmpty())
409               aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
410           }
411           aStates->IncrementState();
412         }
413       }
414     }
415     if(MYDEBUG) {
416       cout<<"Command after: "<< aCmd->GetString()<<endl;
417     }
418   }
419   //  ProcessLayerDistribution();
420 }
421 //================================================================================
422 /*!
423  * \brief Private method
424  */
425 //================================================================================
426 void SMESH_NoteBook::InitObjectMap()
427 {
428   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
429   if(!aGen)
430     return;
431   
432   SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
433   if(aStudy->_is_nil())
434     return;
435   
436   SALOMEDS::SObject_var aSO = aStudy->FindComponent(aGen->ComponentDataType());
437   if(CORBA::is_nil(aSO))
438     return;
439   
440   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
441   char* aParameters;
442   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
443     SALOMEDS::SObject_var aSObject = Itr->Value();
444     SALOMEDS::GenericAttribute_var anAttr;
445     if ( aSObject->FindAttribute(anAttr, "AttributeString")) {
446       aParameters = SALOMEDS::AttributeString::_narrow(anAttr)->Value();
447       SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters);
448       if(MYDEBUG) {
449         cout<<"Entry : "<< aSObject->GetID()<<endl;
450         cout<<"aParameters : "<<aParameters<<endl;
451       }      
452       TCollection_AsciiString anObjType;
453       CORBA::Object_var anObject = SMESH_Gen_i::SObjectToObject(aSObject);
454       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
455       if(!aHyp->_is_nil()) {
456         anObjType = TCollection_AsciiString(aHyp->GetName());
457       }
458       else if(SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObject)) {
459         anObjType = TCollection_AsciiString("Mesh");
460       }
461       if(MYDEBUG)
462         cout<<"The object Type : "<<anObjType<<endl;
463       ObjectStates *aState = NULL;
464       if(anObjType == "LayerDistribution")
465         aState = new LayerDistributionStates();
466       else
467         aState = new  ObjectStates(anObjType);
468       
469       for(int i = 0; i < aSections->length(); i++) {
470         TState aVars;
471         SALOMEDS::ListOfStrings aListOfVars = aSections[i];
472         for(int j = 0;j<aListOfVars.length();j++) {
473           TCollection_AsciiString aVar(aListOfVars[j].in());
474           if(!aVar.IsEmpty() && aStudy->IsVariable(aVar.ToCString())) {
475             aVar.InsertBefore(1,"\"");
476             aVar.InsertAfter(aVar.Length(),"\"");
477           }
478           aVars.push_back(aVar);
479           if(MYDEBUG) {
480             cout<<"Variable: '"<<aVar<<"'"<<endl;
481           }
482         }
483         aState->AddState(aVars);
484       }
485       _objectMap.insert(pair<TCollection_AsciiString,ObjectStates*>(TCollection_AsciiString(aSObject->GetID()),aState));
486     }
487   }
488 }
489
490 //================================================================================
491 /*!
492  * 
493  */
494 //================================================================================
495 void SMESH_NoteBook::AddCommand(const TCollection_AsciiString& theString)
496 {
497   Handle(_pyCommand) aCommand = new _pyCommand( theString, -1);
498   _commands.push_back(aCommand);
499
500   if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
501     myMeshEditors.insert( make_pair( aCommand->GetResultValue(),
502                                      aCommand->GetObject() ) );
503   }
504 }
505
506 //================================================================================
507 /*!
508  * 
509  */
510 //================================================================================
511 void SMESH_NoteBook::ProcessLayerDistribution()
512 {
513   // 1) Find all LayerDistribution states
514   vector<LayerDistributionStates*> aLDS;
515   TVariablesMap::const_iterator it = _objectMap.begin();
516   for(;it != _objectMap.end();it++)
517     if(LayerDistributionStates* aLDStates = (LayerDistributionStates*)((*it).second)) {
518       aLDS.push_back(aLDStates);
519     }
520   
521   // 2) Initialize all type of 1D Distribution hypothesis
522   for(int i=0;i<_commands.size();i++){
523     for(int j =0;j < aLDS.size();j++){
524       TCollection_AsciiString aResultValue = _commands[i]->GetResultValue();
525       if(_commands[i]->GetMethod() == "CreateHypothesis" &&
526          aLDS[j]->HasDistribution(aResultValue)){
527         TCollection_AsciiString aType = _commands[i]->GetArg(1);
528         aType.RemoveAll('\'');
529         aLDS[j]->SetDistributionType(aResultValue,aType);
530       }
531     }
532   }
533   // 3) ... and replase variables ...
534
535   for(int i=0;i<_commands.size();i++){
536     for(int j =0;j < aLDS.size();j++){
537       TCollection_AsciiString anObject = _commands[i]->GetObject();
538
539       if(aLDS[j]->HasDistribution(anObject)) {
540         TCollection_AsciiString aType = aLDS[j]->GetDistributionType(anObject);
541         TCollection_AsciiString aMethod = _commands[i]->GetMethod();
542         if(aType == "LocalLength") {
543           if(aMethod == "SetLength") {
544             if(!aLDS[j]->GetCurrectState().at(0).IsEmpty() )
545               _commands[i]->SetArg(1,aLDS[j]->GetCurrectState().at(0));
546             aLDS[j]->IncrementState();
547           }
548           else if(aMethod == "SetPrecision") {
549             if(!aLDS[j]->GetCurrectState().at(1).IsEmpty() )
550               _commands[i]->SetArg(1,aLDS[j]->GetCurrectState().at(1));
551             aLDS[j]->IncrementState();
552           }
553         }
554       }
555     }
556   }
557 }
558 //================================================================================
559 /*!
560  *  \brief Return result script
561  */
562 //================================================================================
563 TCollection_AsciiString SMESH_NoteBook::GetResultScript() const
564 {
565   TCollection_AsciiString aResult;
566   for(int i=0;i<_commands.size();i++)
567     aResult+=_commands[i]->GetString()+"\n";
568   return aResult;
569 }