Salome HOME
3ee18068486695ffe2597a0848ddada234dcd72e
[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 };
120
121 /*!
122  * \brief Root of all objects
123  */
124 typedef TCollection_AsciiString _pyID;
125
126 class _pyObject: public Standard_Transient
127 {
128   Handle(_pyCommand) myCreationCmd;
129 public:
130   _pyObject(const Handle(_pyCommand)& theCreationCmd): myCreationCmd(theCreationCmd) {}
131   const _pyID& GetID() { return myCreationCmd->GetResultValue(); }
132   const Handle(_pyCommand)& GetCreationCmd() { return myCreationCmd; }
133   int GetCommandNb() { return myCreationCmd->GetOrderNb(); }
134   virtual void Process(const Handle(_pyCommand) & theCommand) = 0;
135   virtual void Flush() = 0;
136 };
137
138 /*!
139  * \brief Class corresponding to SMESH_Gen. It holds info on existing
140  *        meshes and hypotheses
141  */
142 class _pyGen: public _pyObject
143 {
144 public:
145   _pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod);
146   //~_pyGen();
147   void AddCommand( const TCollection_AsciiString& theCommand );
148   void Process( const Handle(_pyCommand)& theCommand );
149   void Flush();
150   Handle(_pyHypothesis) FindHyp( const _pyID& theHypID );
151   Handle(_pyHypothesis) FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
152                                  const TCollection_AsciiString& theAlgoType);
153   void ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 );
154   void SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd );
155   std::list< Handle(_pyCommand) >& GetCommands() { return myCommands; }
156   void SetAccessorMethod(const _pyID& theID, const char* theMethod );
157 private:
158   std::map< _pyID, Handle(_pyMesh) > myMeshes;
159   std::list< Handle(_pyHypothesis) > myHypos;
160   std::list< Handle(_pyCommand) >    myCommands;
161   int                                myNbCommands;
162   Resource_DataMapOfAsciiStringAsciiString& myID2AccessorMethod;
163 };
164
165 /*!
166  * \brief Contains commands concerning mesh substructures
167  */
168 class _pyMesh: public _pyObject
169 {
170   std::list< Handle(_pyCommand) > myAddHypCmds;
171   std::list< Handle(_pyCommand) > mySubmeshes; 
172 public:
173   _pyMesh(const Handle(_pyCommand) theCreationCmd);
174   const _pyID& GetGeom() { return GetCreationCmd()->GetArg(1); }
175   void Process( const Handle(_pyCommand)& theCommand);
176   void Flush();
177 private:
178   static void AddMeshAccess( const Handle(_pyCommand)& theCommand )
179   { theCommand->SetObject( theCommand->GetObject() + ".GetMesh()" ); }
180 };
181
182 /*!
183  * \brief Root class for smesh.Mesh_Algorithm
184  */
185 class _pyHypothesis: public _pyObject
186 {
187 protected:
188   bool    myIsAlgo, /*myIsLocal, */myIsWrapped, myIsConverted;
189   int     myDim, myAdditionCmdNb;
190   _pyID    myGeom, myMesh;
191   TCollection_AsciiString       myCreationMethod, myType;
192   TColStd_SequenceOfAsciiString myArgs;
193   TColStd_SequenceOfAsciiString myArgMethods;
194   std::list<Handle(_pyCommand)>  myArgCommands;
195   std::list<Handle(_pyCommand)>  myUnknownCommands;
196 public:
197   _pyHypothesis(const Handle(_pyCommand)& theCreationCmd);
198   virtual bool IsAlgo() const { return myIsAlgo; }
199   bool IsWrapped() const { return myIsWrapped; }
200   bool & IsConverted() { return myIsConverted; }
201   int GetDim() const { return myDim; }
202   const _pyID & GetGeom() const { return myGeom; }
203   void SetMesh( const _pyID& theMeshId) { if ( myMesh.IsEmpty() ) myMesh = theMeshId; }
204   const _pyID & GetMesh() const { return myMesh; }
205   const TCollection_AsciiString GetType() { return myType; }
206   bool IsWrappable(const _pyID& theMesh) { return !myIsWrapped && myMesh == theMesh; }
207   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
208                                   const _pyID&              theMesh);
209   static Handle(_pyHypothesis) NewHypothesis( const Handle(_pyCommand)& theCreationCmd);
210   //     bool HasMesh() const { return !myMesh.IsEmpty(); }
211   //     void SetGeom( const _pyID& theGeomID ) { myGeom = theGeomID; }
212   void Process( const Handle(_pyCommand)& theCommand);
213   void Flush();
214 };
215
216 /*!
217  * \brief Class for hypotheses having several parameters modified by one method
218  */
219 class _pyComplexParamHypo: public _pyHypothesis
220 {
221 public:
222   _pyComplexParamHypo(const Handle(_pyCommand)& theCreationCmd): _pyHypothesis(theCreationCmd) {}
223   void Process( const Handle(_pyCommand)& theCommand);
224 };
225 DEFINE_STANDARD_HANDLE (_pyComplexParamHypo, _pyHypothesis);
226
227
228 /*!
229  * \brief Class representing NumberOfSegments hypothesis
230  */
231 class _pyNumberOfSegmentsHyp: public _pyHypothesis
232 {
233 public:
234   _pyNumberOfSegmentsHyp(const Handle(_pyCommand)& theCrCmd): _pyHypothesis(theCrCmd) {}
235   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
236                                   const _pyID&              theMesh);
237 };
238 DEFINE_STANDARD_HANDLE (_pyNumberOfSegmentsHyp, _pyHypothesis);
239
240 /*!
241  * \brief Class representing smesh.Mesh_Algorithm
242  */
243 class _pyAlgorithm: public _pyHypothesis
244 {
245 public:
246   _pyAlgorithm(const Handle(_pyCommand)& theCreationCmd);
247   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
248                                   const _pyID&              theMesh);
249 };
250
251 #endif