Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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 /*!
77  * \brief Class operating on a command string looking like
78  *        ResultValue = Object.Method( Arg1, Arg2,...)
79  */
80 class _pyCommand: public Standard_Transient
81 {
82   int myOrderNb; // position within the script
83   TCollection_AsciiString myString;
84   TCollection_AsciiString myRes, myObj, myMeth;
85   TColStd_SequenceOfAsciiString myArgs;
86   TColStd_SequenceOfInteger myBegPos; //!< where myRes, myObj, ... begin
87   std::list< Handle(_pyCommand) > myDependentCmds;
88   enum { UNKNOWN=-1, EMPTY=0, RESULT_IND, OBJECT_IND, METHOD_IND, ARG1_IND };
89   int GetBegPos( int thePartIndex );
90   void SetBegPos( int thePartIndex, int thePosition );
91   void SetPart( int thePartIndex, const TCollection_AsciiString& theNewPart,
92                 TCollection_AsciiString& theOldPart);
93   void FindAllArgs() { GetArg(1); }
94 public:
95   _pyCommand() {};
96   _pyCommand( const TCollection_AsciiString& theString, int theNb )
97     : myString( theString ), myOrderNb( theNb ) {};
98   TCollection_AsciiString & GetString() { return myString; }
99   int GetOrderNb() const { return myOrderNb; }
100   void SetOrderNb( int theNb ) { myOrderNb = theNb; }
101   int Length() { return myString.Length(); }
102   void Clear() { myString.Clear(); myBegPos.Clear(); }
103   bool IsEmpty() const { return myString.IsEmpty(); }
104   const TCollection_AsciiString & GetResultValue();
105   const TCollection_AsciiString & GetObject();
106   const TCollection_AsciiString & GetMethod();
107   const TCollection_AsciiString & GetArg( int index );
108   int GetNbArgs() { FindAllArgs(); return myArgs.Length(); }
109   //Handle(TColStd_HSequenceOfAsciiString) GetArgs();
110   void SetResultValue( const TCollection_AsciiString& theResult )
111   { GetResultValue(); SetPart( RESULT_IND, theResult, myRes ); }
112   void SetObject(const TCollection_AsciiString& theObject)
113   { GetObject(); SetPart( OBJECT_IND, theObject, myObj ); }
114   void SetMethod(const TCollection_AsciiString& theMethod)
115   { GetMethod(); SetPart( METHOD_IND, theMethod, myMeth ); }
116   void SetArg( int index, const TCollection_AsciiString& theArg);
117   void RemoveArgs();
118   static bool SkipSpaces( const TCollection_AsciiString & theSring, int & thePos );
119   static TCollection_AsciiString GetWord( const TCollection_AsciiString & theSring,
120                                           int & theStartPos, const bool theForward,
121                                           const bool dotIsWord = false);
122   void AddDependantCmd( Handle(_pyCommand) cmd)
123   { return myDependentCmds.push_back( cmd ); }
124   bool SetDependentCmdsAfter() const;
125
126   DEFINE_STANDARD_RTTI (_pyCommand)
127 };
128
129 /*!
130  * \brief Root of all objects
131  */
132 typedef TCollection_AsciiString _pyID;
133
134 class _pyObject: public Standard_Transient
135 {
136   Handle(_pyCommand)              myCreationCmd;
137 public:
138   _pyObject(const Handle(_pyCommand)& theCreationCmd): myCreationCmd(theCreationCmd) {}
139   const _pyID& GetID() { return myCreationCmd->GetResultValue(); }
140   const Handle(_pyCommand)& GetCreationCmd() { return myCreationCmd; }
141   void  SetCreationCmd( Handle(_pyCommand) cmd ) { myCreationCmd = cmd; }
142   int GetCommandNb() { return myCreationCmd->GetOrderNb(); }
143   virtual void Process(const Handle(_pyCommand) & theCommand) = 0;
144   virtual void Flush() = 0;
145
146   DEFINE_STANDARD_RTTI (_pyObject)
147 };
148
149 /*!
150  * \brief Class corresponding to SMESH_Gen. It holds info on existing
151  *        meshes and hypotheses
152  */
153 class _pyGen: public _pyObject
154 {
155 public:
156   _pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod);
157   //~_pyGen();
158   void AddCommand( const TCollection_AsciiString& theCommand );
159   void Process( const Handle(_pyCommand)& theCommand );
160   void Flush();
161   Handle(_pyHypothesis) FindHyp( const _pyID& theHypID );
162   Handle(_pyHypothesis) FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
163                                  const TCollection_AsciiString& theAlgoType);
164   void ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 );
165   void SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd );
166   std::list< Handle(_pyCommand) >& GetCommands() { return myCommands; }
167   void SetAccessorMethod(const _pyID& theID, const char* theMethod );
168 private:
169   std::map< _pyID, Handle(_pyMesh) > myMeshes;
170   std::list< Handle(_pyHypothesis) > myHypos;
171   std::list< Handle(_pyCommand) >    myCommands;
172   int                                myNbCommands;
173   Resource_DataMapOfAsciiStringAsciiString& myID2AccessorMethod;
174
175   DEFINE_STANDARD_RTTI (_pyGen)
176 };
177
178 /*!
179  * \brief Contains commands concerning mesh substructures
180  */
181 class _pyMesh: public _pyObject
182 {
183   std::list< Handle(_pyCommand) > myAddHypCmds;
184   std::list< Handle(_pyCommand) > mySubmeshes; 
185 public:
186   _pyMesh(const Handle(_pyCommand) theCreationCmd);
187   const _pyID& GetGeom() { return GetCreationCmd()->GetArg(1); }
188   void Process( const Handle(_pyCommand)& theCommand);
189   void Flush();
190 private:
191   static void AddMeshAccess( const Handle(_pyCommand)& theCommand )
192   { theCommand->SetObject( theCommand->GetObject() + ".GetMesh()" ); }
193
194   DEFINE_STANDARD_RTTI (_pyMesh)
195 };
196
197 /*!
198  * \brief Root class for hypothesis
199  *
200  * HOWTO assure convertion of a new hypothesis
201  * In NewHypothesis():
202  * 1. add a case for the name of the new hypothesis and
203  * 2. initialize _pyHypothesis fields:
204  *    . myDim - hypothesis dimention;
205  *    . myType - type name of the algorithm creating the hypothesis;
206  *    . myCreationMethod - method name of the algorithm creating the hypothesis;
207  *    . append to myArgMethods interface methods setting param values in the
208  *    order they are used when myCreationMethod is called. It is supposed that
209  *    each interface method sets only one parameter, if it is not so, you are
210  *    to derive a specific class from _pyHypothesis that would redefine Process(),
211  *    see _pyComplexParamHypo for example
212  */
213 class _pyHypothesis: public _pyObject
214 {
215 protected:
216   bool    myIsAlgo, /*myIsLocal, */myIsWrapped, myIsConverted;
217   int     myDim, myAdditionCmdNb;
218   _pyID    myGeom, myMesh;
219   TCollection_AsciiString       myCreationMethod, myType;
220   TColStd_SequenceOfAsciiString myArgs;
221   TColStd_SequenceOfAsciiString myArgMethods;
222   std::list<Handle(_pyCommand)>  myArgCommands;
223   std::list<Handle(_pyCommand)>  myUnknownCommands;
224 public:
225   _pyHypothesis(const Handle(_pyCommand)& theCreationCmd);
226   virtual bool IsAlgo() const { return myIsAlgo; }
227   bool IsWrapped() const { return myIsWrapped; }
228   bool & IsConverted() { return myIsConverted; }
229   int GetDim() const { return myDim; }
230   const _pyID & GetGeom() const { return myGeom; }
231   void SetMesh( const _pyID& theMeshId) { if ( myMesh.IsEmpty() ) myMesh = theMeshId; }
232   const _pyID & GetMesh() const { return myMesh; }
233   const TCollection_AsciiString GetType() { return myType; }
234   bool IsWrappable(const _pyID& theMesh) { return !myIsWrapped && myMesh == theMesh; }
235   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
236                                   const _pyID&              theMesh);
237   static Handle(_pyHypothesis) NewHypothesis( const Handle(_pyCommand)& theCreationCmd);
238   //     bool HasMesh() const { return !myMesh.IsEmpty(); }
239   //     void SetGeom( const _pyID& theGeomID ) { myGeom = theGeomID; }
240   void Process( const Handle(_pyCommand)& theCommand);
241   void Flush();
242
243   DEFINE_STANDARD_RTTI (_pyHypothesis)
244 };
245
246 /*!
247  * \brief Class for hypotheses having several parameters modified by one method
248  */
249 class _pyComplexParamHypo: public _pyHypothesis
250 {
251 public:
252   _pyComplexParamHypo(const Handle(_pyCommand)& theCreationCmd): _pyHypothesis(theCreationCmd) {}
253   void Process( const Handle(_pyCommand)& theCommand);
254
255   DEFINE_STANDARD_RTTI (_pyComplexParamHypo)
256 };
257 DEFINE_STANDARD_HANDLE (_pyComplexParamHypo, _pyHypothesis);
258
259
260 /*!
261  * \brief Class representing NumberOfSegments hypothesis
262  */
263 class _pyNumberOfSegmentsHyp: public _pyHypothesis
264 {
265 public:
266   _pyNumberOfSegmentsHyp(const Handle(_pyCommand)& theCrCmd): _pyHypothesis(theCrCmd) {}
267   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
268                                   const _pyID&              theMesh);
269
270   DEFINE_STANDARD_RTTI (_pyNumberOfSegmentsHyp)
271 };
272 DEFINE_STANDARD_HANDLE (_pyNumberOfSegmentsHyp, _pyHypothesis);
273
274 /*!
275  * \brief Class representing smesh.Mesh_Algorithm
276  */
277 class _pyAlgorithm: public _pyHypothesis
278 {
279 public:
280   _pyAlgorithm(const Handle(_pyCommand)& theCreationCmd);
281   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
282                                   const _pyID&              theMesh);
283
284   DEFINE_STANDARD_RTTI (_pyAlgorithm)
285 };
286
287 #endif