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