1 // Copyright (C) 2008 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SMESH_NoteBook.cxx
21 // Author : Roman NIKOLAEV
23 #include "SMESH_2smeshpy.hxx"
24 #include "SMESH_NoteBook.hxx"
25 #include "SMESH_Gen_i.hxx"
26 #include "SMESH_PythonDump.hxx"
28 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
29 #include <TColStd_SequenceOfAsciiString.hxx>
30 #include <TColStd_HSequenceOfInteger.hxx>
36 static int MYDEBUG = 1;
38 static int MYDEBUG = 1;
45 //================================================================================
49 //================================================================================
50 ObjectStates::ObjectStates(TCollection_AsciiString theType)
56 //================================================================================
60 //================================================================================
61 ObjectStates::~ObjectStates()
65 //================================================================================
67 * \brief Add new object state
68 * \param theState - Object state (vector of notebook variable)
70 //================================================================================
71 void ObjectStates::AddState(const TState &theState)
73 _states.push_back(theState);
76 //================================================================================
78 * \brief Return current object state
79 * \\retval state - Object state (vector of notebook variable)
81 //================================================================================
82 TState ObjectStates::GetCurrectState() const
84 return _states[_dumpstate];
88 //================================================================================
92 //================================================================================
93 TAllStates ObjectStates::GetAllStates() const
98 //================================================================================
102 //================================================================================
103 void ObjectStates::IncrementState()
108 //================================================================================
112 //================================================================================
113 TCollection_AsciiString ObjectStates::GetObjectType() const{
118 //================================================================================
122 //================================================================================
123 LayerDistributionStates::LayerDistributionStates():
124 ObjectStates("LayerDistribution")
127 //================================================================================
131 //================================================================================
132 LayerDistributionStates::~LayerDistributionStates()
137 //================================================================================
139 * \brief AddDistribution
141 //================================================================================
142 void LayerDistributionStates::AddDistribution(const TCollection_AsciiString& theDistribution)
144 _distributions.insert(pair<TCollection_AsciiString,TCollection_AsciiString>(theDistribution,""));
147 //================================================================================
149 * \brief HasDistribution
151 //================================================================================
152 bool LayerDistributionStates::HasDistribution(const TCollection_AsciiString& theDistribution) const
154 return _distributions.find(theDistribution) != _distributions.end();
157 //================================================================================
159 * \brief SetDistributionType
161 //================================================================================
162 bool LayerDistributionStates::SetDistributionType(const TCollection_AsciiString& theDistribution,
163 const TCollection_AsciiString& theType)
165 TDistributionMap::iterator it = _distributions.find(theDistribution);
166 if(it == _distributions.end())
168 (*it).second = theType;
172 //================================================================================
174 * \brief GetDistributionType
176 //================================================================================
177 TCollection_AsciiString LayerDistributionStates::
178 GetDistributionType(const TCollection_AsciiString& theDistribution) const
180 TDistributionMap::const_iterator it = _distributions.find(theDistribution);
181 return (it == _distributions.end()) ? TCollection_AsciiString() : (*it).second;
184 //================================================================================
188 //================================================================================
189 SMESH_NoteBook::SMESH_NoteBook()
194 //================================================================================
198 //================================================================================
199 SMESH_NoteBook::~SMESH_NoteBook()
201 TVariablesMap::const_iterator it = _objectMap.begin();
202 for(;it!=_objectMap.end();it++) {
208 //================================================================================
210 * \brief Replace parameters of the functions on the Salome NoteBook Variables
211 * \param theString - Input string
212 * \retval TCollection_AsciiString - Convertion result
214 //================================================================================
215 void SMESH_NoteBook::ReplaceVariables()
218 for(int i=0;i<_commands.size();i++) {
219 Handle(_pyCommand) aCmd = _commands[i];
220 TCollection_AsciiString aMethod = aCmd->GetMethod();
221 TCollection_AsciiString aObject = aCmd->GetObject();
222 TCollection_AsciiString aResultValue = aCmd->GetResultValue();
224 cout<<"Command before : "<< aCmd->GetString()<<endl;
225 cout<<"Method : "<< aMethod<<endl;
226 cout<<"Object : "<< aObject<<endl;
227 cout<<"Result : "<< aResultValue<<endl;
230 // check if method modifies the object itself
231 TVariablesMap::const_iterator it = _objectMap.find(aObject);
232 if(it == _objectMap.end()) // check if method returns a new object
233 it = _objectMap.find(aResultValue);
235 if(it == _objectMap.end()) { // check if method modifies a mesh using mesh editor
236 TMeshEditorMap::const_iterator meIt = myMeshEditors.find(aObject);
237 if(meIt != myMeshEditors.end()) {
238 TCollection_AsciiString aMesh = (*meIt).second;
239 it = _objectMap.find(aMesh);
243 if(it != _objectMap.end() && !aMethod.IsEmpty()) {
244 ObjectStates *aStates = (*it).second;
245 // Case for LocalLength hypothesis
246 if(aStates->GetObjectType().IsEqual("LocalLength")) {
247 if(aMethod.IsEqual("SetLength")) {
248 if(!aStates->GetCurrectState().at(0).IsEmpty() )
249 aCmd->SetArg(1,aStates->GetCurrectState().at(0));
250 aStates->IncrementState();
252 else if(aMethod.IsEqual("SetPrecision")) {
253 if(!aStates->GetCurrectState().at(1).IsEmpty() )
254 aCmd->SetArg(1,aStates->GetCurrectState().at(1));
255 aStates->IncrementState();
259 // Case for SegmentLengthAroundVertex hypothesis
260 else if(aStates->GetObjectType().IsEqual("SegmentLengthAroundVertex")) {
261 if(aMethod == "SetLength") {
262 if(!aStates->GetCurrectState().at(0).IsEmpty() )
263 aCmd->SetArg(1,aStates->GetCurrectState().at(0));
264 aStates->IncrementState();
267 // Case for LayerDistribution hypothesis (not finished yet)
268 else if(aStates->GetObjectType() == "LayerDistribution") {
269 if(aMethod == "SetLayerDistribution"){
270 LayerDistributionStates* aLDStates = (LayerDistributionStates*)(aStates);
271 aLDStates->AddDistribution(aCmd->GetArg(1));
275 else if(aStates->GetObjectType().IsEqual("Mesh")) {
276 if(aMethod.IsEqual("Translate") ||
277 aMethod.IsEqual("TranslateMakeGroups") ||
278 aMethod.IsEqual("TranslateMakeMesh")) {
279 bool isVariableFound = false;
281 for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
282 if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
288 for(int j = 0; j <= 2; j++) {
289 if(!aStates->GetCurrectState().at(j).IsEmpty()) {
290 isVariableFound = true;
291 aCmd->SetArg(anArgIndex+j, aStates->GetCurrectState().at(j));
295 if(isVariableFound) {
296 aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
297 aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
299 aStates->IncrementState();
304 cout<<"Command after: "<< aCmd->GetString()<<endl;
307 // ProcessLayerDistribution();
309 //================================================================================
311 * \brief Private method
313 //================================================================================
314 void SMESH_NoteBook::InitObjectMap()
316 SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
320 SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
321 if(aStudy->_is_nil())
324 SALOMEDS::SObject_var aSO = aStudy->FindComponent(aGen->ComponentDataType());
325 if(CORBA::is_nil(aSO))
328 SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
330 for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
331 SALOMEDS::SObject_var aSObject = Itr->Value();
332 SALOMEDS::GenericAttribute_var anAttr;
333 if ( aSObject->FindAttribute(anAttr, "AttributeString")) {
334 aParameters = SALOMEDS::AttributeString::_narrow(anAttr)->Value();
335 SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters);
337 cout<<"Entry : "<< aSObject->GetID()<<endl;
338 cout<<"aParameters : "<<aParameters<<endl;
340 TCollection_AsciiString anObjType;
341 CORBA::Object_var anObject = SMESH_Gen_i::SObjectToObject(aSObject);
342 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
343 if(!aHyp->_is_nil()) {
344 anObjType = TCollection_AsciiString(aHyp->GetName());
346 else if(SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObject)) {
347 anObjType = TCollection_AsciiString("Mesh");
350 cout<<"The object Type : "<<anObjType<<endl;
351 ObjectStates *aState = NULL;
352 if(anObjType == "LayerDistribution")
353 aState = new LayerDistributionStates();
355 aState = new ObjectStates(anObjType);
357 for(int i = 0; i < aSections->length(); i++) {
359 SALOMEDS::ListOfStrings aListOfVars = aSections[i];
360 for(int j = 0;j<aListOfVars.length();j++) {
361 TCollection_AsciiString aVar(aListOfVars[j].in());
362 if(!aVar.IsEmpty() && aStudy->IsVariable(aVar.ToCString())) {
363 aVar.InsertBefore(1,"\"");
364 aVar.InsertAfter(aVar.Length(),"\"");
366 aVars.push_back(aVar);
368 cout<<"Variable: '"<<aVar<<"'"<<endl;
371 aState->AddState(aVars);
373 _objectMap.insert(pair<TCollection_AsciiString,ObjectStates*>(TCollection_AsciiString(aSObject->GetID()),aState));
378 //================================================================================
382 //================================================================================
383 void SMESH_NoteBook::AddCommand(const TCollection_AsciiString& theString)
385 Handle(_pyCommand) aCommand = new _pyCommand( theString, -1);
386 _commands.push_back(aCommand);
388 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
389 myMeshEditors.insert( make_pair( aCommand->GetResultValue(),
390 aCommand->GetObject() ) );
394 //================================================================================
398 //================================================================================
399 void SMESH_NoteBook::ProcessLayerDistribution()
401 // 1) Find all LayerDistribution states
402 vector<LayerDistributionStates*> aLDS;
403 TVariablesMap::const_iterator it = _objectMap.begin();
404 for(;it != _objectMap.end();it++)
405 if(LayerDistributionStates* aLDStates = (LayerDistributionStates*)((*it).second)) {
406 aLDS.push_back(aLDStates);
409 // 2) Initialize all type of 1D Distribution hypothesis
410 for(int i=0;i<_commands.size();i++){
411 for(int j =0;j < aLDS.size();j++){
412 TCollection_AsciiString aResultValue = _commands[i]->GetResultValue();
413 if(_commands[i]->GetMethod() == "CreateHypothesis" &&
414 aLDS[j]->HasDistribution(aResultValue)){
415 TCollection_AsciiString aType = _commands[i]->GetArg(1);
416 aType.RemoveAll('\'');
417 aLDS[j]->SetDistributionType(aResultValue,aType);
421 // 3) ... and replase variables ...
423 for(int i=0;i<_commands.size();i++){
424 for(int j =0;j < aLDS.size();j++){
425 TCollection_AsciiString anObject = _commands[i]->GetObject();
427 if(aLDS[j]->HasDistribution(anObject)) {
428 TCollection_AsciiString aType = aLDS[j]->GetDistributionType(anObject);
429 TCollection_AsciiString aMethod = _commands[i]->GetMethod();
430 if(aType == "LocalLength") {
431 if(aMethod == "SetLength") {
432 if(!aLDS[j]->GetCurrectState().at(0).IsEmpty() )
433 _commands[i]->SetArg(1,aLDS[j]->GetCurrectState().at(0));
434 aLDS[j]->IncrementState();
436 else if(aMethod == "SetPrecision") {
437 if(!aLDS[j]->GetCurrectState().at(1).IsEmpty() )
438 _commands[i]->SetArg(1,aLDS[j]->GetCurrectState().at(1));
439 aLDS[j]->IncrementState();
446 //================================================================================
448 * \brief Return result script
450 //================================================================================
451 TCollection_AsciiString SMESH_NoteBook::GetResultScript() const
453 TCollection_AsciiString aResult;
454 for(int i=0;i<_commands.size();i++)
455 aResult+=_commands[i]->GetString()+"\n";