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 if(_states.size() > _dumpstate)
85 return _states[_dumpstate];
91 //================================================================================
95 //================================================================================
96 TAllStates ObjectStates::GetAllStates() const
101 //================================================================================
105 //================================================================================
106 void ObjectStates::IncrementState()
111 //================================================================================
115 //================================================================================
116 TCollection_AsciiString ObjectStates::GetObjectType() const{
121 //================================================================================
125 //================================================================================
126 LayerDistributionStates::LayerDistributionStates():
127 ObjectStates("LayerDistribution")
130 //================================================================================
134 //================================================================================
135 LayerDistributionStates::~LayerDistributionStates()
140 //================================================================================
142 * \brief AddDistribution
144 //================================================================================
145 void LayerDistributionStates::AddDistribution(const TCollection_AsciiString& theDistribution)
147 _distributions.insert(pair<TCollection_AsciiString,TCollection_AsciiString>(theDistribution,""));
150 //================================================================================
152 * \brief HasDistribution
154 //================================================================================
155 bool LayerDistributionStates::HasDistribution(const TCollection_AsciiString& theDistribution) const
157 return _distributions.find(theDistribution) != _distributions.end();
160 //================================================================================
162 * \brief SetDistributionType
164 //================================================================================
165 bool LayerDistributionStates::SetDistributionType(const TCollection_AsciiString& theDistribution,
166 const TCollection_AsciiString& theType)
168 TDistributionMap::iterator it = _distributions.find(theDistribution);
169 if(it == _distributions.end())
171 (*it).second = theType;
175 //================================================================================
177 * \brief GetDistributionType
179 //================================================================================
180 TCollection_AsciiString LayerDistributionStates::
181 GetDistributionType(const TCollection_AsciiString& theDistribution) const
183 TDistributionMap::const_iterator it = _distributions.find(theDistribution);
184 return (it == _distributions.end()) ? TCollection_AsciiString() : (*it).second;
187 //================================================================================
191 //================================================================================
192 SMESH_NoteBook::SMESH_NoteBook()
197 //================================================================================
201 //================================================================================
202 SMESH_NoteBook::~SMESH_NoteBook()
204 TVariablesMap::const_iterator it = _objectMap.begin();
205 for(;it!=_objectMap.end();it++) {
211 //================================================================================
213 * \brief Replace parameters of the functions on the Salome NoteBook Variables
214 * \param theString - Input string
215 * \retval TCollection_AsciiString - Convertion result
217 //================================================================================
218 void SMESH_NoteBook::ReplaceVariables()
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();
227 cout<<"Command before : "<< aCmd->GetString()<<endl;
228 cout<<"Method : "<< aMethod<<endl;
229 cout<<"Object : "<< aObject<<endl;
230 cout<<"Result : "<< aResultValue<<endl;
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);
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);
246 if(it != _objectMap.end() && !aMethod.IsEmpty()) {
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();
255 else if(aMethod.IsEqual("SetPrecision")) {
256 if(!aStates->GetCurrectState().at(1).IsEmpty() )
257 aCmd->SetArg(1,aStates->GetCurrectState().at(1));
258 aStates->IncrementState();
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();
272 else if(aStates->GetObjectType().IsEqual("Arithmetic1D")) {
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();
282 // Case for LayerDistribution hypothesis (not finished yet)
283 else if(aStates->GetObjectType() == "LayerDistribution") {
284 if(aMethod == "SetLayerDistribution"){
285 LayerDistributionStates* aLDStates = (LayerDistributionStates*)(aStates);
286 aLDStates->AddDistribution(aCmd->GetArg(1));
290 else if(aStates->GetObjectType().IsEqual("Mesh")) {
291 if(aMethod.IsEqual("Translate") ||
292 aMethod.IsEqual("TranslateMakeGroups") ||
293 aMethod.IsEqual("TranslateMakeMesh")) {
294 bool isVariableFound = false;
296 for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
297 if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
303 for(int j = 0; j <= 2; j++) {
304 if(!aStates->GetCurrectState().at(j).IsEmpty()) {
305 isVariableFound = true;
306 aCmd->SetArg(anArgIndex+j, aStates->GetCurrectState().at(j));
310 if(isVariableFound) {
311 aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
312 aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
314 aStates->IncrementState();
319 cout<<"Command after: "<< aCmd->GetString()<<endl;
322 // ProcessLayerDistribution();
324 //================================================================================
326 * \brief Private method
328 //================================================================================
329 void SMESH_NoteBook::InitObjectMap()
331 SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
335 SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
336 if(aStudy->_is_nil())
339 SALOMEDS::SObject_var aSO = aStudy->FindComponent(aGen->ComponentDataType());
340 if(CORBA::is_nil(aSO))
343 SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
345 for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
346 SALOMEDS::SObject_var aSObject = Itr->Value();
347 SALOMEDS::GenericAttribute_var anAttr;
348 if ( aSObject->FindAttribute(anAttr, "AttributeString")) {
349 aParameters = SALOMEDS::AttributeString::_narrow(anAttr)->Value();
350 SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters);
352 cout<<"Entry : "<< aSObject->GetID()<<endl;
353 cout<<"aParameters : "<<aParameters<<endl;
355 TCollection_AsciiString anObjType;
356 CORBA::Object_var anObject = SMESH_Gen_i::SObjectToObject(aSObject);
357 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
358 if(!aHyp->_is_nil()) {
359 anObjType = TCollection_AsciiString(aHyp->GetName());
361 else if(SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObject)) {
362 anObjType = TCollection_AsciiString("Mesh");
365 cout<<"The object Type : "<<anObjType<<endl;
366 ObjectStates *aState = NULL;
367 if(anObjType == "LayerDistribution")
368 aState = new LayerDistributionStates();
370 aState = new ObjectStates(anObjType);
372 for(int i = 0; i < aSections->length(); i++) {
374 SALOMEDS::ListOfStrings aListOfVars = aSections[i];
375 for(int j = 0;j<aListOfVars.length();j++) {
376 TCollection_AsciiString aVar(aListOfVars[j].in());
377 if(!aVar.IsEmpty() && aStudy->IsVariable(aVar.ToCString())) {
378 aVar.InsertBefore(1,"\"");
379 aVar.InsertAfter(aVar.Length(),"\"");
381 aVars.push_back(aVar);
383 cout<<"Variable: '"<<aVar<<"'"<<endl;
386 aState->AddState(aVars);
388 _objectMap.insert(pair<TCollection_AsciiString,ObjectStates*>(TCollection_AsciiString(aSObject->GetID()),aState));
393 //================================================================================
397 //================================================================================
398 void SMESH_NoteBook::AddCommand(const TCollection_AsciiString& theString)
400 Handle(_pyCommand) aCommand = new _pyCommand( theString, -1);
401 _commands.push_back(aCommand);
403 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
404 myMeshEditors.insert( make_pair( aCommand->GetResultValue(),
405 aCommand->GetObject() ) );
409 //================================================================================
413 //================================================================================
414 void SMESH_NoteBook::ProcessLayerDistribution()
416 // 1) Find all LayerDistribution states
417 vector<LayerDistributionStates*> aLDS;
418 TVariablesMap::const_iterator it = _objectMap.begin();
419 for(;it != _objectMap.end();it++)
420 if(LayerDistributionStates* aLDStates = (LayerDistributionStates*)((*it).second)) {
421 aLDS.push_back(aLDStates);
424 // 2) Initialize all type of 1D Distribution hypothesis
425 for(int i=0;i<_commands.size();i++){
426 for(int j =0;j < aLDS.size();j++){
427 TCollection_AsciiString aResultValue = _commands[i]->GetResultValue();
428 if(_commands[i]->GetMethod() == "CreateHypothesis" &&
429 aLDS[j]->HasDistribution(aResultValue)){
430 TCollection_AsciiString aType = _commands[i]->GetArg(1);
431 aType.RemoveAll('\'');
432 aLDS[j]->SetDistributionType(aResultValue,aType);
436 // 3) ... and replase variables ...
438 for(int i=0;i<_commands.size();i++){
439 for(int j =0;j < aLDS.size();j++){
440 TCollection_AsciiString anObject = _commands[i]->GetObject();
442 if(aLDS[j]->HasDistribution(anObject)) {
443 TCollection_AsciiString aType = aLDS[j]->GetDistributionType(anObject);
444 TCollection_AsciiString aMethod = _commands[i]->GetMethod();
445 if(aType == "LocalLength") {
446 if(aMethod == "SetLength") {
447 if(!aLDS[j]->GetCurrectState().at(0).IsEmpty() )
448 _commands[i]->SetArg(1,aLDS[j]->GetCurrectState().at(0));
449 aLDS[j]->IncrementState();
451 else if(aMethod == "SetPrecision") {
452 if(!aLDS[j]->GetCurrectState().at(1).IsEmpty() )
453 _commands[i]->SetArg(1,aLDS[j]->GetCurrectState().at(1));
454 aLDS[j]->IncrementState();
461 //================================================================================
463 * \brief Return result script
465 //================================================================================
466 TCollection_AsciiString SMESH_NoteBook::GetResultScript() const
468 TCollection_AsciiString aResult;
469 for(int i=0;i<_commands.size();i++)
470 aResult+=_commands[i]->GetString()+"\n";