Salome HOME
71500c7b5d6d590720c8a84c0bb4bcb974f993e1
[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
32 class Resource_DataMapOfAsciiStringAsciiString;
33
34 class SMESH_2smeshpy
35 {
36 public:
37   /*!
38    * \brief Convert a python script using commands of smesh.py
39    * \param theScript - Input script
40    * \param theEntry2AccessorMethod - The returning method names to access to
41    *        objects wrapped with python class
42    * \retval TCollection_AsciiString - Convertion result
43    */
44   static TCollection_AsciiString
45   ConvertScript(const TCollection_AsciiString& theScript,
46                 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod);
47
48   /*!
49    * \brief Return the name of the python file wrapping IDL API
50     * \retval TCollection_AsciiString - The file name
51    */
52   static char* SmeshpyName() { return "smesh"; }
53   static char* GenName() { return "smesh.smesh"; }
54 };
55
56 // =====================
57 //    INTERNAL STUFF
58 // =====================
59
60 class _pyCommand;
61 class _pyObject;
62 class _pyGen;
63 class _pyMesh;
64 class _pyHypothesis;
65 class _pyAlgorithm;
66
67 DEFINE_STANDARD_HANDLE (_pyCommand   ,Standard_Transient);
68 DEFINE_STANDARD_HANDLE (_pyObject    ,Standard_Transient);
69 DEFINE_STANDARD_HANDLE (_pyGen       ,_pyObject);
70 DEFINE_STANDARD_HANDLE (_pyMesh      ,_pyObject);
71 DEFINE_STANDARD_HANDLE (_pyHypothesis,_pyObject);
72 DEFINE_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
73
74 /*!
75  * \brief Class operating on a command string looking like
76  *        ResultValue = Object.Method( Arg1, Arg2,...)
77  */
78 class _pyCommand: public Standard_Transient
79 {
80   int myOrderNb; // position within the script
81   TCollection_AsciiString myString;
82   TCollection_AsciiString myRes, myObj, myMeth;
83   TColStd_SequenceOfAsciiString myArgs;
84   TColStd_SequenceOfInteger myBegPos; //!< where myRes, myObj, ... begin
85   enum { UNKNOWN=-1, EMPTY=0, RESULT_IND, OBJECT_IND, METHOD_IND, ARG1_IND };
86   int GetBegPos( int thePartIndex );
87   void SetBegPos( int thePartIndex, int thePosition );
88   void SetPart( int thePartIndex, const TCollection_AsciiString& theNewPart,
89                 TCollection_AsciiString& theOldPart);
90   void FindAllArgs() { GetArg(1); }
91 public:
92   _pyCommand() {};
93   _pyCommand( const TCollection_AsciiString& theString, int theNb )
94     : myString( theString ), myOrderNb( theNb ) {};
95   TCollection_AsciiString & GetString() { return myString; }
96   int GetOrderNb() const { return myOrderNb; }
97   void SetOrderNb( int theNb ) { myOrderNb = theNb; }
98   int Length() { return myString.Length(); }
99   void Clear() { myString.Clear(); myBegPos.Clear(); }
100   bool IsEmpty() const { return myString.IsEmpty(); }
101   const TCollection_AsciiString & GetResultValue();
102   const TCollection_AsciiString & GetObject();
103   const TCollection_AsciiString & GetMethod();
104   const TCollection_AsciiString & GetArg( int index );
105   int GetNbArgs() { FindAllArgs(); return myArgs.Length(); }
106   //Handle(TColStd_HSequenceOfAsciiString) GetArgs();
107   void SetResultValue( const TCollection_AsciiString& theResult )
108   { GetResultValue(); SetPart( RESULT_IND, theResult, myRes ); }
109   void SetObject(const TCollection_AsciiString& theObject)
110   { GetObject(); SetPart( OBJECT_IND, theObject, myObj ); }
111   void SetMethod(const TCollection_AsciiString& theMethod)
112   { GetMethod(); SetPart( METHOD_IND, theMethod, myMeth ); }
113   void SetArg( int index, const TCollection_AsciiString& theArg);
114   void RemoveArgs();
115   static bool SkipSpaces( const TCollection_AsciiString & theSring, int & thePos );
116   static TCollection_AsciiString GetWord( const TCollection_AsciiString & theSring,
117                                           int & theStartPos, const bool theForward,
118                                           const bool dotIsWord = false);
119   DEFINE_STANDARD_RTTI (_pyCommand)
120 };
121
122 /*!
123  * \brief Root of all objects
124  */
125 typedef TCollection_AsciiString _pyID;
126
127 class _pyObject: public Standard_Transient
128 {
129   Handle(_pyCommand) myCreationCmd;
130 public:
131   _pyObject(const Handle(_pyCommand)& theCreationCmd): myCreationCmd(theCreationCmd) {}
132   const _pyID& GetID() { return myCreationCmd->GetResultValue(); }
133   const Handle(_pyCommand)& GetCreationCmd() { return myCreationCmd; }
134   int GetCommandNb() { return myCreationCmd->GetOrderNb(); }
135   virtual void Process(const Handle(_pyCommand) & theCommand) = 0;
136   virtual void Flush() = 0;
137
138   DEFINE_STANDARD_RTTI (_pyObject)
139 };
140
141 /*!
142  * \brief Class corresponding to SMESH_Gen. It holds info on existing
143  *        meshes and hypotheses
144  */
145 class _pyGen: public _pyObject
146 {
147 public:
148   _pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod);
149   //~_pyGen();
150   void AddCommand( const TCollection_AsciiString& theCommand );
151   void Process( const Handle(_pyCommand)& theCommand );
152   void Flush();
153   Handle(_pyHypothesis) FindHyp( const _pyID& theHypID );
154   Handle(_pyHypothesis) FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
155                                  const TCollection_AsciiString& theAlgoType);
156   void ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 );
157   void SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd );
158   std::list< Handle(_pyCommand) >& GetCommands() { return myCommands; }
159   void SetAccessorMethod(const _pyID& theID, const char* theMethod );
160 private:
161   std::map< _pyID, Handle(_pyMesh) > myMeshes;
162   std::list< Handle(_pyHypothesis) > myHypos;
163   std::list< Handle(_pyCommand) >    myCommands;
164   int                                myNbCommands;
165   Resource_DataMapOfAsciiStringAsciiString& myID2AccessorMethod;
166
167   DEFINE_STANDARD_RTTI (_pyGen)
168 };
169
170 /*!
171  * \brief Contains commands concerning mesh substructures
172  */
173 class _pyMesh: public _pyObject
174 {
175   std::list< Handle(_pyCommand) > myAddHypCmds;
176   std::list< Handle(_pyCommand) > mySubmeshes; 
177 public:
178   _pyMesh(const Handle(_pyCommand) theCreationCmd);
179   const _pyID& GetGeom() { return GetCreationCmd()->GetArg(1); }
180   void Process( const Handle(_pyCommand)& theCommand);
181   void Flush();
182 private:
183   static void AddMeshAccess( const Handle(_pyCommand)& theCommand )
184   { theCommand->SetObject( theCommand->GetObject() + ".GetMesh()" ); }
185
186   DEFINE_STANDARD_RTTI (_pyMesh)
187 };
188
189 /*!
190  * \brief Root class for smesh.Mesh_Algorithm
191  */
192 class _pyHypothesis: public _pyObject
193 {
194 protected:
195   bool    myIsAlgo, /*myIsLocal, */myIsWrapped, myIsConverted;
196   int     myDim, myAdditionCmdNb;
197   _pyID    myGeom, myMesh;
198   TCollection_AsciiString       myCreationMethod, myType;
199   TColStd_SequenceOfAsciiString myArgs;
200   TColStd_SequenceOfAsciiString myArgMethods;
201   std::list<Handle(_pyCommand)>  myArgCommands;
202   std::list<Handle(_pyCommand)>  myUnknownCommands;
203 public:
204   _pyHypothesis(const Handle(_pyCommand)& theCreationCmd);
205   virtual bool IsAlgo() const { return myIsAlgo; }
206   bool IsWrapped() const { return myIsWrapped; }
207   bool & IsConverted() { return myIsConverted; }
208   int GetDim() const { return myDim; }
209   const _pyID & GetGeom() const { return myGeom; }
210   void SetMesh( const _pyID& theMeshId) { if ( myMesh.IsEmpty() ) myMesh = theMeshId; }
211   const _pyID & GetMesh() const { return myMesh; }
212   const TCollection_AsciiString GetType() { return myType; }
213   bool IsWrappable(const _pyID& theMesh) { return !myIsWrapped && myMesh == theMesh; }
214   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
215                                   const _pyID&              theMesh);
216   static Handle(_pyHypothesis) NewHypothesis( const Handle(_pyCommand)& theCreationCmd);
217   //     bool HasMesh() const { return !myMesh.IsEmpty(); }
218   //     void SetGeom( const _pyID& theGeomID ) { myGeom = theGeomID; }
219   void Process( const Handle(_pyCommand)& theCommand);
220   void Flush();
221
222   DEFINE_STANDARD_RTTI (_pyHypothesis)
223 };
224
225 /*!
226  * \brief Class for hypotheses having several parameters modified by one method
227  */
228 class _pyComplexParamHypo: public _pyHypothesis
229 {
230 public:
231   _pyComplexParamHypo(const Handle(_pyCommand)& theCreationCmd): _pyHypothesis(theCreationCmd) {}
232   void Process( const Handle(_pyCommand)& theCommand);
233
234   DEFINE_STANDARD_RTTI (_pyComplexParamHypo)
235 };
236 DEFINE_STANDARD_HANDLE (_pyComplexParamHypo, _pyHypothesis);
237
238
239 /*!
240  * \brief Class representing NumberOfSegments hypothesis
241  */
242 class _pyNumberOfSegmentsHyp: public _pyHypothesis
243 {
244 public:
245   _pyNumberOfSegmentsHyp(const Handle(_pyCommand)& theCrCmd): _pyHypothesis(theCrCmd) {}
246   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
247                                   const _pyID&              theMesh);
248
249   DEFINE_STANDARD_RTTI (_pyNumberOfSegmentsHyp)
250 };
251 DEFINE_STANDARD_HANDLE (_pyNumberOfSegmentsHyp, _pyHypothesis);
252
253 /*!
254  * \brief Class representing smesh.Mesh_Algorithm
255  */
256 class _pyAlgorithm: public _pyHypothesis
257 {
258 public:
259   _pyAlgorithm(const Handle(_pyCommand)& theCreationCmd);
260   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
261                                   const _pyID&              theMesh);
262
263   DEFINE_STANDARD_RTTI (_pyAlgorithm)
264 };
265
266 #endif