Salome HOME
Fix bug 12796: Warning missed for the bad file 'test18.med'
[modules/smesh.git] / src / SMESH_I / SMESH_2smeshpy.hxx
1 // Copyright (C) 2005  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_smesh.hxx
21 // Created   : Fri Nov 18 12:05:18 2005
22 // Author    : Edward AGAPOV (eap)
23
24 #ifndef SMESH_smesh_HeaderFile
25 #define SMESH_smesh_HeaderFile
26
27 #include <Standard_DefineHandle.hxx>
28 #include <Standard_Type.hxx>
29 #include <Standard_Transient.hxx>
30 #include <TCollection_AsciiString.hxx>
31 #include <TColStd_SequenceOfAsciiString.hxx>
32 #include <TColStd_SequenceOfInteger.hxx>
33
34 #include <list>
35 #include <map>
36
37 /*!
38  * \brief Tool converting SMESH engine calls into commands defined in smesh.py
39  *
40  * This file was created in order to respond to requirement of bug PAL10494:
41  * SMESH python dump uses idl interface.
42  *
43  * The creation reason is that smesh.py commands defining hypotheses encapsulate
44  * several SMESH engine method calls. As well, the dependencies between smesh.py
45  * classes differ from ones between SMESH IDL interfaces.
46  * 
47  * The only API method here is SMESH_2smeshpy::ConvertScript(), the rest ones are
48  * for internal usage
49  *
50  * See comments to _pyHypothesis class to know how to assure convertion of a new hypothesis
51  */
52
53 class Resource_DataMapOfAsciiStringAsciiString;
54
55 class SMESH_2smeshpy
56 {
57 public:
58   /*!
59    * \brief Convert a python script using commands of smesh.py
60    * \param theScript - Input script
61    * \param theEntry2AccessorMethod - The returning method names to access to
62    *        objects wrapped with python class
63    * \retval TCollection_AsciiString - Convertion result
64    */
65   static TCollection_AsciiString
66   ConvertScript(const TCollection_AsciiString& theScript,
67                 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod);
68
69   /*!
70    * \brief Return the name of the python file wrapping IDL API
71     * \retval TCollection_AsciiString - The file name
72    */
73   static char* SmeshpyName() { return "smesh"; }
74   static char* GenName() { return "smesh.smesh"; }
75 };
76
77 // =====================
78 //    INTERNAL STUFF
79 // =====================
80
81 class _pyCommand;
82 class _pyObject;
83 class _pyGen;
84 class _pyMesh;
85 class _pyHypothesis;
86 class _pyAlgorithm;
87
88 DEFINE_STANDARD_HANDLE (_pyCommand   ,Standard_Transient);
89 DEFINE_STANDARD_HANDLE (_pyObject    ,Standard_Transient);
90 DEFINE_STANDARD_HANDLE (_pyGen       ,_pyObject);
91 DEFINE_STANDARD_HANDLE (_pyMesh      ,_pyObject);
92 DEFINE_STANDARD_HANDLE (_pyHypothesis,_pyObject);
93 DEFINE_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
94
95 typedef TCollection_AsciiString _pyID;
96
97 // ===========================================================
98 /*!
99  * \brief Class operating on a command string looking like
100  *        ResultValue = Object.Method( Arg1, Arg2,...)
101  */
102 // ===========================================================
103
104 class _pyCommand: public Standard_Transient
105 {
106   int                             myOrderNb;            //!< position within the script
107   TCollection_AsciiString         myString;             //!< command text
108   TCollection_AsciiString         myRes, myObj, myMeth; //!< found parts of command
109   TColStd_SequenceOfAsciiString   myArgs;               //!< found arguments
110   TColStd_SequenceOfInteger       myBegPos;             //!< where myRes, myObj, ... begin
111   std::list< Handle(_pyCommand) > myDependentCmds; //!< commands that sould follow me in the script
112
113   enum { UNKNOWN=-1, EMPTY=0, RESULT_IND, OBJECT_IND, METHOD_IND, ARG1_IND };
114   int GetBegPos( int thePartIndex );
115   void SetBegPos( int thePartIndex, int thePosition );
116   void SetPart( int thePartIndex, const TCollection_AsciiString& theNewPart,
117                 TCollection_AsciiString& theOldPart);
118   void FindAllArgs() { GetArg(1); }
119
120 public:
121   _pyCommand() {};
122   _pyCommand( const TCollection_AsciiString& theString, int theNb )
123     : myString( theString ), myOrderNb( theNb ) {};
124   TCollection_AsciiString & GetString() { return myString; }
125   int GetOrderNb() const { return myOrderNb; }
126   void SetOrderNb( int theNb ) { myOrderNb = theNb; }
127   int Length() { return myString.Length(); }
128   void Clear() { myString.Clear(); myBegPos.Clear(); }
129   bool IsEmpty() const { return myString.IsEmpty(); }
130   const TCollection_AsciiString & GetResultValue();
131   const TCollection_AsciiString & GetObject();
132   const TCollection_AsciiString & GetMethod();
133   const TCollection_AsciiString & GetArg( int index );
134   int GetNbArgs() { FindAllArgs(); return myArgs.Length(); }
135   //Handle(TColStd_HSequenceOfAsciiString) GetArgs();
136   void SetResultValue( const TCollection_AsciiString& theResult )
137   { GetResultValue(); SetPart( RESULT_IND, theResult, myRes ); }
138   void SetObject(const TCollection_AsciiString& theObject)
139   { GetObject(); SetPart( OBJECT_IND, theObject, myObj ); }
140   void SetMethod(const TCollection_AsciiString& theMethod)
141   { GetMethod(); SetPart( METHOD_IND, theMethod, myMeth ); }
142   void SetArg( int index, const TCollection_AsciiString& theArg);
143   void RemoveArgs();
144   static bool SkipSpaces( const TCollection_AsciiString & theSring, int & thePos );
145   static TCollection_AsciiString GetWord( const TCollection_AsciiString & theSring,
146                                           int & theStartPos, const bool theForward,
147                                           const bool dotIsWord = false);
148   void AddDependantCmd( Handle(_pyCommand) cmd)
149   { return myDependentCmds.push_back( cmd ); }
150   bool SetDependentCmdsAfter() const;
151
152   bool AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod );
153
154   DEFINE_STANDARD_RTTI (_pyCommand)
155 };
156
157 /*!
158  * \brief Root of all objects
159  */
160
161 class _pyObject: public Standard_Transient
162 {
163   Handle(_pyCommand)              myCreationCmd;
164 public:
165   _pyObject(const Handle(_pyCommand)& theCreationCmd): myCreationCmd(theCreationCmd) {}
166   const _pyID& GetID() { return myCreationCmd->GetResultValue(); }
167   const Handle(_pyCommand)& GetCreationCmd() { return myCreationCmd; }
168   void  SetCreationCmd( Handle(_pyCommand) cmd ) { myCreationCmd = cmd; }
169   int GetCommandNb() { return myCreationCmd->GetOrderNb(); }
170   virtual void Process(const Handle(_pyCommand) & theCommand) = 0;
171   virtual void Flush() = 0;
172   virtual const char* AccessorMethod() const;
173
174   DEFINE_STANDARD_RTTI (_pyObject)
175 };
176
177 /*!
178  * \brief Class corresponding to SMESH_Gen. It holds info on existing
179  *        meshes and hypotheses
180  */
181 class _pyGen: public _pyObject
182 {
183 public:
184   _pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod);
185   //~_pyGen();
186   void AddCommand( const TCollection_AsciiString& theCommand );
187   void Process( const Handle(_pyCommand)& theCommand );
188   void Flush();
189   Handle(_pyHypothesis) FindHyp( const _pyID& theHypID );
190   Handle(_pyHypothesis) FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
191                                  const TCollection_AsciiString& theAlgoType);
192   void ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 );
193   void SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd );
194   std::list< Handle(_pyCommand) >& GetCommands() { return myCommands; }
195   void SetAccessorMethod(const _pyID& theID, const char* theMethod );
196   const char* AccessorMethod() const { return SMESH_2smeshpy::GenName(); }
197 private:
198   std::map< _pyID, Handle(_pyMesh) > myMeshes;
199   std::list< Handle(_pyHypothesis) > myHypos;
200   std::list< Handle(_pyCommand) >    myCommands;
201   int                                myNbCommands;
202   bool                               myHasPattern;
203   Resource_DataMapOfAsciiStringAsciiString& myID2AccessorMethod;
204
205   DEFINE_STANDARD_RTTI (_pyGen)
206 };
207
208 /*!
209  * \brief Contains commands concerning mesh substructures
210  */
211 #define _pyMesh_ACCESS_METHOD "GetMesh()"
212 class _pyMesh: public _pyObject
213 {
214   std::list< Handle(_pyHypothesis) > myHypos;
215   std::list< Handle(_pyCommand) > myAddHypCmds;
216   std::list< Handle(_pyCommand) > mySubmeshes;
217   bool                            myHasEditor;
218 public:
219   _pyMesh(const Handle(_pyCommand) theCreationCmd);
220   const _pyID& GetGeom() { return GetCreationCmd()->GetArg(1); }
221   void Process( const Handle(_pyCommand)& theCommand);
222   void Flush();
223   const char* AccessorMethod() const { return _pyMesh_ACCESS_METHOD; }
224 private:
225   static void AddMeshAccess( const Handle(_pyCommand)& theCommand )
226   { theCommand->SetObject( theCommand->GetObject() + "." _pyMesh_ACCESS_METHOD ); }
227
228   DEFINE_STANDARD_RTTI (_pyMesh)
229 };
230 #undef _pyMesh_ACCESS_METHOD 
231
232 /*!
233  * \brief Root class for hypothesis
234  *
235  * HOWTO assure convertion of a new hypothesis
236  * In NewHypothesis():
237  * 1. add a case for the name of the new hypothesis and
238  * 2. initialize _pyHypothesis fields:
239  *    . myDim - hypothesis dimention;
240  *    . myType - type name of the algorithm creating the hypothesis;
241  *    . myCreationMethod - method name of the algorithm creating the hypothesis;
242  *    . append to myArgMethods interface methods setting param values in the
243  *    order they are used when myCreationMethod is called. It is supposed that
244  *    each interface method sets only one parameter, if it is not so, you are
245  *    to derive a specific class from _pyHypothesis that would redefine Process(),
246  *    see _pyComplexParamHypo for example
247  */
248 class _pyHypothesis: public _pyObject
249 {
250 protected:
251   bool    myIsAlgo, /*myIsLocal, */myIsWrapped, myIsConverted;
252   int     myDim, myAdditionCmdNb;
253   _pyID    myGeom, myMesh;
254   TCollection_AsciiString       myCreationMethod, myType;
255   TColStd_SequenceOfAsciiString myArgs;
256   TColStd_SequenceOfAsciiString myArgMethods;
257   std::list<Handle(_pyCommand)>  myArgCommands;
258   std::list<Handle(_pyCommand)>  myUnknownCommands;
259 public:
260   _pyHypothesis(const Handle(_pyCommand)& theCreationCmd);
261   virtual bool IsAlgo() const { return myIsAlgo; }
262   bool IsWrapped() const { return myIsWrapped; }
263   bool & IsConverted() { return myIsConverted; }
264   int GetDim() const { return myDim; }
265   const _pyID & GetGeom() const { return myGeom; }
266   void SetMesh( const _pyID& theMeshId) { if ( myMesh.IsEmpty() ) myMesh = theMeshId; }
267   const _pyID & GetMesh() const { return myMesh; }
268   const TCollection_AsciiString GetType() { return myType; }
269   bool IsWrappable(const _pyID& theMesh) { return !myIsWrapped && myMesh == theMesh; }
270   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
271                                   const _pyID&              theMesh);
272   static Handle(_pyHypothesis) NewHypothesis( const Handle(_pyCommand)& theCreationCmd);
273   //     bool HasMesh() const { return !myMesh.IsEmpty(); }
274   //     void SetGeom( const _pyID& theGeomID ) { myGeom = theGeomID; }
275   void Process( const Handle(_pyCommand)& theCommand);
276   void Flush();
277
278   DEFINE_STANDARD_RTTI (_pyHypothesis)
279 };
280
281 /*!
282  * \brief Class for hypotheses having several parameters modified by one method
283  */
284 class _pyComplexParamHypo: public _pyHypothesis
285 {
286 public:
287   _pyComplexParamHypo(const Handle(_pyCommand)& theCreationCmd): _pyHypothesis(theCreationCmd) {}
288   void Process( const Handle(_pyCommand)& theCommand);
289
290   DEFINE_STANDARD_RTTI (_pyComplexParamHypo)
291 };
292 DEFINE_STANDARD_HANDLE (_pyComplexParamHypo, _pyHypothesis);
293
294
295 /*!
296  * \brief Class representing NumberOfSegments hypothesis
297  */
298 class _pyNumberOfSegmentsHyp: public _pyHypothesis
299 {
300 public:
301   _pyNumberOfSegmentsHyp(const Handle(_pyCommand)& theCrCmd): _pyHypothesis(theCrCmd) {}
302   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
303                                   const _pyID&              theMesh);
304   void Flush();
305
306   DEFINE_STANDARD_RTTI (_pyNumberOfSegmentsHyp)
307 };
308 DEFINE_STANDARD_HANDLE (_pyNumberOfSegmentsHyp, _pyHypothesis);
309
310 /*!
311  * \brief Class representing smesh.Mesh_Algorithm
312  */
313 class _pyAlgorithm: public _pyHypothesis
314 {
315 public:
316   _pyAlgorithm(const Handle(_pyCommand)& theCreationCmd);
317   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
318                                   const _pyID&              theMesh);
319   const char* AccessorMethod() const { return "GetAlgorithm()"; }
320
321   DEFINE_STANDARD_RTTI (_pyAlgorithm)
322 };
323
324 #endif