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