]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESH_I/SMESH_NoteBook.cxx
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
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  * \brief Constructor
45  */
46 //================================================================================
47 SMESH_NoteBook::SMESH_NoteBook()
48 {
49   InitObjectMap();
50 }
51
52 //================================================================================
53 /*!
54  * \brief Constructor
55  */
56 //================================================================================
57 SMESH_NoteBook::~SMESH_NoteBook()
58 {
59 }
60
61 //================================================================================
62 /*!
63  * \brief Replace parameters of the functions on the Salome NoteBook Variables
64  * \param theString - Input string
65  * \retval TCollection_AsciiString - Convertion result
66  */
67 //================================================================================
68 TCollection_AsciiString SMESH_NoteBook::ReplaceVariables(const TCollection_AsciiString& theString) const
69 {
70   _pyCommand aCmd( theString, -1);
71   TCollection_AsciiString aMethod = aCmd.GetMethod();
72   TCollection_AsciiString aObject = aCmd.GetObject();
73   TVariablesMap::const_iterator it = _objectMap.find(aObject);
74   if(!aMethod.IsEmpty() && it != _objectMap.end() ) {
75     
76     if(aMethod == "SetLength" && !(*it).second.at(0).IsEmpty() ) {
77       aCmd.SetArg(1,(*it).second.at(0));
78     }
79     else if(aMethod == "SetPrecision" && !(*it).second.at(1).IsEmpty() ){
80       aCmd.SetArg(1,(*it).second.at(1));
81     }
82     return aCmd.GetString();
83   }
84   
85   return theString;
86 }
87 //================================================================================
88 /*!
89  * \brief Private method
90  */
91 //================================================================================
92 void SMESH_NoteBook::InitObjectMap()
93 {
94   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
95   if(!aGen)
96     return;
97   
98   SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
99   if(aStudy->_is_nil())
100     return;
101   
102   SALOMEDS::SObject_var aSO = aStudy->FindComponent(aGen->ComponentDataType());
103   if(CORBA::is_nil(aSO))
104     return;
105
106   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
107   TCollection_AsciiString aParameters;
108   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
109     SALOMEDS::SObject_var aSObject = Itr->Value();
110     SALOMEDS::GenericAttribute_var anAttr;
111     if ( aSObject->FindAttribute(anAttr, "AttributeString")) {
112       aParameters = TCollection_AsciiString(SALOMEDS::AttributeString::_narrow(anAttr)->Value());
113       vector<string> vect = ParseVariables(aParameters.ToCString(),':');
114       if(MYDEBUG) {
115         cout<<"Entry : "<< aSObject->GetID()<<endl;
116         cout<<"aParameters : "<<aParameters<<endl;
117       }
118       vector<TCollection_AsciiString> aVars;
119       for(int i = 0;i<vect.size();i++) {
120         TCollection_AsciiString aVar(vect[i].c_str());
121         if(!aVar.IsEmpty() && aStudy->IsVariable(vect[i].c_str())) {
122           aVar.InsertBefore(1,"\"");
123           aVar.InsertAfter(aVar.Length(),"\"");
124         }
125         aVars.push_back(aVar);
126         if(MYDEBUG) {
127           cout<<"Variable: "<<aVar<<endl;
128         }
129       }
130       _objectMap.insert(pair<TCollection_AsciiString,vector<TCollection_AsciiString> >(TCollection_AsciiString(aSObject->GetID()),aVars));
131     }
132   }
133 }
134
135 //================================================================================
136 /*!
137  * \brief Private method
138  */
139 //================================================================================
140 vector<string> SMESH_NoteBook::ParseVariables(const string& theVariables, const char sep) const
141 {
142   vector<string> aResult;
143   if(theVariables[0] == sep ) aResult.push_back(string());
144   int pos = theVariables.find(sep);
145   if(pos < 0) {
146     aResult.push_back(theVariables);
147     return aResult;
148   }
149
150   string s = theVariables;
151   if(s[0] == sep) s = s.substr(1, s.size());
152   while((pos = s.find(sep)) >= 0) {
153     aResult.push_back(s.substr(0, pos));
154     s = s.substr(pos+1, s.size());
155   }
156
157   if(!s.empty() && s[0] != sep) aResult.push_back(s);
158   if(theVariables[theVariables.size()-1] == sep) aResult.push_back(string());
159
160   return aResult;
161 }