Salome HOME
Implementation notebook in the SMESH module.
[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
27 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
28 #include <TColStd_SequenceOfAsciiString.hxx>
29 #include <TColStd_HSequenceOfInteger.hxx>
30
31 #include <vector>
32 #include <string>
33
34 #ifdef _DEBUG_
35 static int MYDEBUG = 0;
36 #else
37 static int MYDEBUG = 0;
38 #endif
39
40 using namespace std;
41
42
43
44 //================================================================================
45 /*!
46  * \brief Constructor
47  */
48 //================================================================================
49 ObjectStates::ObjectStates(TCollection_AsciiString theType)
50 {
51   _type = theType;
52   _dumpstate = 0;
53 }
54
55 //================================================================================
56 /*!
57  * \brief Destructor
58  */
59 //================================================================================
60 ObjectStates::~ObjectStates()
61 {
62 }
63
64 //================================================================================
65 /*!
66  * \brief Add new object state 
67  * \param theState - Object state (vector of notebook variable)
68  */
69 //================================================================================
70 void ObjectStates::AddState(const TState &theState)
71 {
72   _states.push_back(theState);
73 }
74
75 //================================================================================
76 /*!
77  * \brief Return current object state
78  * \\retval state - Object state (vector of notebook variable)
79  */
80 //================================================================================
81 TState ObjectStates::GetCurrectState() const
82 {
83   return _states[_dumpstate];
84 }
85
86
87 //================================================================================
88 /*!
89  *
90  */
91 //================================================================================
92 TAllStates ObjectStates::GetAllStates() const
93 {
94   return _states;
95 }
96
97 //================================================================================
98 /*!
99  *
100  */
101 //================================================================================
102 void ObjectStates::IncrementState()
103 {
104   _dumpstate++;
105 }
106
107 //================================================================================
108 /*!
109  *
110  */
111 //================================================================================
112 TCollection_AsciiString ObjectStates::GetObjectType() const{
113   return _type;
114 }
115
116 //================================================================================
117 /*!
118  * \brief Constructor
119  */
120 //================================================================================
121 SMESH_NoteBook::SMESH_NoteBook()
122 {
123   InitObjectMap();
124 }
125
126 //================================================================================
127 /*!
128  * \brief Destructor
129  */
130 //================================================================================
131 SMESH_NoteBook::~SMESH_NoteBook()
132 {
133   TVariablesMap::const_iterator it = _objectMap.begin();
134   for(;it!=_objectMap.end();it++) {
135     if((*it).second)
136       delete (*it).second;
137   }
138 }
139
140 //================================================================================
141 /*!
142  * \brief Replace parameters of the functions on the Salome NoteBook Variables
143  * \param theString - Input string
144  * \retval TCollection_AsciiString - Convertion result
145  */
146 //================================================================================
147 TCollection_AsciiString SMESH_NoteBook::ReplaceVariables(const TCollection_AsciiString& theString) const
148 {
149   _pyCommand aCmd( theString, -1);
150   TCollection_AsciiString aMethod = aCmd.GetMethod();
151   TCollection_AsciiString aObject = aCmd.GetObject();
152   TVariablesMap::const_iterator it = _objectMap.find(aObject);
153   if(!aMethod.IsEmpty() && it != _objectMap.end() ) {
154     ObjectStates *aStates = (*it).second;
155     bool modified = false;
156     if(MYDEBUG)
157       cout<<"SMESH_NoteBook::ReplaceVariables :Object Type : "<<aStates->GetObjectType()<<endl;
158     if(aStates->GetObjectType().IsEqual("LocalLength")) {
159       if(aMethod == "SetLength") {
160         if(!aStates->GetCurrectState().at(0).IsEmpty() )
161           aCmd.SetArg(1,aStates->GetCurrectState().at(0));
162         aStates->IncrementState();
163       }
164       else if(aMethod == "SetPrecision") {
165         if(!aStates->GetCurrectState().at(1).IsEmpty() )
166           aCmd.SetArg(1,aStates->GetCurrectState().at(1));
167         aStates->IncrementState();
168       }
169     }
170     return aCmd.GetString();
171   }
172   
173   return theString;
174 }
175 //================================================================================
176 /*!
177  * \brief Private method
178  */
179 //================================================================================
180 void SMESH_NoteBook::InitObjectMap()
181 {
182   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
183   if(!aGen)
184     return;
185   
186   SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
187   if(aStudy->_is_nil())
188     return;
189   
190   SALOMEDS::SObject_var aSO = aStudy->FindComponent(aGen->ComponentDataType());
191   if(CORBA::is_nil(aSO))
192     return;
193   
194   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
195   char* aParameters;
196   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
197     SALOMEDS::SObject_var aSObject = Itr->Value();
198     SALOMEDS::GenericAttribute_var anAttr;
199     if ( aSObject->FindAttribute(anAttr, "AttributeString")) {
200       aParameters = SALOMEDS::AttributeString::_narrow(anAttr)->Value();
201       SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters);
202       if(MYDEBUG) {
203         cout<<"Entry : "<< aSObject->GetID()<<endl;
204         cout<<"aParameters : "<<aParameters<<endl;
205       }      
206       TCollection_AsciiString anObjType;
207       CORBA::Object_var anObject = SMESH_Gen_i::SObjectToObject(aSObject);
208       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
209       if(!aHyp->_is_nil()) {
210         anObjType = TCollection_AsciiString(aHyp->GetName());
211       }
212       else if(SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObject)) {
213         anObjType = TCollection_AsciiString("Mesh");
214       }
215       if(MYDEBUG)
216         cout<<"The object Type : "<<anObjType<<endl;
217       
218       ObjectStates *aState = new  ObjectStates(anObjType);
219       for(int i = 0; i < aSections->length(); i++) {
220         TState aVars;
221         SALOMEDS::ListOfStrings aListOfVars = aSections[i];
222         for(int j = 0;j<aListOfVars.length();j++) {
223           TCollection_AsciiString aVar(aListOfVars[j].in());
224           if(!aVar.IsEmpty() && aStudy->IsVariable(aVar.ToCString())) {
225             aVar.InsertBefore(1,"\"");
226             aVar.InsertAfter(aVar.Length(),"\"");
227           }
228           aVars.push_back(aVar);
229           if(MYDEBUG) {
230             cout<<"Variable: '"<<aVar<<"'"<<endl;
231           }
232         }
233         aState->AddState(aVars);
234       }
235       _objectMap.insert(pair<TCollection_AsciiString,ObjectStates*>(TCollection_AsciiString(aSObject->GetID()),aState));
236     }
237   }
238 }