Salome HOME
Merging with WPdev
[modules/smesh.git] / src / SMESH_I / SMESH_2smeshpy.hxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File      : SMESH_smesh.hxx
21 // Created   : Fri Nov 18 12:05:18 2005
22 // Author    : Edward AGAPOV (eap)
23
24 #ifndef SMESH_smesh_HeaderFile
25 #define SMESH_smesh_HeaderFile
26
27 #include <Standard_DefineHandle.hxx>
28 #include <Standard_Type.hxx>
29 #include <Standard_Transient.hxx>
30 #include <TCollection_AsciiString.hxx>
31 #include <TColStd_SequenceOfAsciiString.hxx>
32 #include <TColStd_SequenceOfInteger.hxx>
33
34 #include <list>
35 #include <map>
36
37 // ===========================================================================================
38 /*!
39  * \brief Tool converting SMESH engine calls into commands defined in smesh.py
40  *
41  * This file was created in order to respond to requirement of bug PAL10494:
42  * SMESH python dump uses idl interface.
43  *
44  * The creation reason is that smesh.py commands defining hypotheses encapsulate
45  * several SMESH engine method calls. As well, the dependencies between smesh.py
46  * classes differ from ones between SMESH IDL interfaces.
47  * 
48  * The only API method here is SMESH_2smeshpy::ConvertScript(), the rest ones are
49  * for internal usage
50  *
51  * See comments to _pyHypothesis class to know how to assure convertion of a new hypothesis
52  */
53 // ===========================================================================================
54
55 class Resource_DataMapOfAsciiStringAsciiString;
56
57 class SMESH_2smeshpy
58 {
59 public:
60   /*!
61    * \brief Convert a python script using commands of smesh.py
62    * \param theScript - Input script
63    * \param theEntry2AccessorMethod - The returning method names to access to
64    *        objects wrapped with python class
65    * \retval TCollection_AsciiString - Convertion result
66    */
67   static TCollection_AsciiString
68   ConvertScript(const TCollection_AsciiString& theScript,
69                 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod);
70
71   /*!
72    * \brief Return the name of the python file wrapping IDL API
73     * \retval TCollection_AsciiString - The file name
74    */
75   static char* SmeshpyName() { return "smesh"; }
76   static char* GenName() { return "smesh.smesh"; }
77 };
78
79 // ===========================================================================================
80 // =====================
81 //    INTERNAL STUFF
82 // =====================
83 // ===========================================================================================
84
85 class _pyCommand;
86 class _pyObject;
87 class _pyGen;
88 class _pyMesh;
89 class _pyHypothesis;
90 class _pyAlgorithm;
91
92 DEFINE_STANDARD_HANDLE (_pyCommand   ,Standard_Transient);
93 DEFINE_STANDARD_HANDLE (_pyObject    ,Standard_Transient);
94 DEFINE_STANDARD_HANDLE (_pyGen       ,_pyObject);
95 DEFINE_STANDARD_HANDLE (_pyMesh      ,_pyObject);
96 DEFINE_STANDARD_HANDLE (_pyHypothesis,_pyObject);
97 DEFINE_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
98
99 typedef TCollection_AsciiString _pyID;
100
101 // ===========================================================
102 /*!
103  * \brief Class operating on a command string looking like
104  *        ResultValue = Object.Method( Arg1, Arg2,...)
105  */
106 // ===========================================================
107
108 class _pyCommand: public Standard_Transient
109 {
110   int                             myOrderNb;            //!< position within the script
111   TCollection_AsciiString         myString;             //!< command text
112   TCollection_AsciiString         myRes, myObj, myMeth; //!< found parts of command
113   TColStd_SequenceOfAsciiString   myArgs;               //!< found arguments
114   TColStd_SequenceOfInteger       myBegPos;             //!< where myRes, myObj, ... begin
115   std::list< Handle(_pyCommand) > myDependentCmds; //!< commands that sould follow me in the script
116
117   enum { UNKNOWN=-1, EMPTY=0, RESULT_IND, OBJECT_IND, METHOD_IND, ARG1_IND };
118   int GetBegPos( int thePartIndex );
119   void SetBegPos( int thePartIndex, int thePosition );
120   void SetPart( int thePartIndex, const TCollection_AsciiString& theNewPart,
121                 TCollection_AsciiString& theOldPart);
122   void FindAllArgs() { GetArg(1); }
123
124 public:
125   _pyCommand() {};
126   _pyCommand( const TCollection_AsciiString& theString, int theNb )
127     : myString( theString ), myOrderNb( theNb ) {};
128   TCollection_AsciiString & GetString() { return myString; }
129   int GetOrderNb() const { return myOrderNb; }
130   void SetOrderNb( int theNb ) { myOrderNb = theNb; }
131   int Length() { return myString.Length(); }
132   void Clear() { myString.Clear(); myBegPos.Clear(); }
133   bool IsEmpty() const { return myString.IsEmpty(); }
134   const TCollection_AsciiString & GetResultValue();
135   const TCollection_AsciiString & GetObject();
136   const TCollection_AsciiString & GetMethod();
137   const TCollection_AsciiString & GetArg( int index );
138   int GetNbArgs() { FindAllArgs(); return myArgs.Length(); }
139   //Handle(TColStd_HSequenceOfAsciiString) GetArgs();
140   void SetResultValue( const TCollection_AsciiString& theResult )
141   { GetResultValue(); SetPart( RESULT_IND, theResult, myRes ); }
142   void SetObject(const TCollection_AsciiString& theObject)
143   { GetObject(); SetPart( OBJECT_IND, theObject, myObj ); }
144   void SetMethod(const TCollection_AsciiString& theMethod)
145   { GetMethod(); SetPart( METHOD_IND, theMethod, myMeth ); }
146   void SetArg( int index, const TCollection_AsciiString& theArg);
147   void RemoveArgs();
148   static bool SkipSpaces( const TCollection_AsciiString & theSring, int & thePos );
149   static TCollection_AsciiString GetWord( const TCollection_AsciiString & theSring,
150                                           int & theStartPos, const bool theForward,
151                                           const bool dotIsWord = false);
152   void AddDependantCmd( Handle(_pyCommand) cmd)
153   { return myDependentCmds.push_back( cmd ); }
154   bool SetDependentCmdsAfter() const;
155
156   bool AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod );
157
158   DEFINE_STANDARD_RTTI (_pyCommand)
159 };
160
161 // -------------------------------------------------------------------------------------
162 /*!
163  * \brief Root of all objects
164  */
165 // -------------------------------------------------------------------------------------
166
167 class _pyObject: public Standard_Transient
168 {
169   Handle(_pyCommand)              myCreationCmd;
170 public:
171   _pyObject(const Handle(_pyCommand)& theCreationCmd): myCreationCmd(theCreationCmd) {}
172   const _pyID& GetID() { return myCreationCmd->GetResultValue(); }
173   const Handle(_pyCommand)& GetCreationCmd() { return myCreationCmd; }
174   void  SetCreationCmd( Handle(_pyCommand) cmd ) { myCreationCmd = cmd; }
175   int GetCommandNb() { return myCreationCmd->GetOrderNb(); }
176   virtual void Process(const Handle(_pyCommand) & theCommand) = 0;
177   virtual void Flush() = 0;
178   virtual const char* AccessorMethod() const;
179
180   DEFINE_STANDARD_RTTI (_pyObject)
181 };
182
183 // -------------------------------------------------------------------------------------
184 /*!
185  * \brief Class corresponding to SMESH_Gen. It holds info on existing
186  *        meshes and hypotheses
187  */
188 // -------------------------------------------------------------------------------------
189 class _pyGen: public _pyObject
190 {
191 public:
192   _pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod);
193   //~_pyGen();
194   void AddCommand( const TCollection_AsciiString& theCommand );
195   void Process( const Handle(_pyCommand)& theCommand );
196   void Flush();
197   Handle(_pyHypothesis) FindHyp( const _pyID& theHypID );
198   Handle(_pyHypothesis) FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
199                                  const TCollection_AsciiString& theAlgoType);
200   void ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 );
201   void SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd );
202   std::list< Handle(_pyCommand) >& GetCommands() { return myCommands; }
203   void SetAccessorMethod(const _pyID& theID, const char* theMethod );
204   bool AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const;
205   bool AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const;
206   const char* AccessorMethod() const { return SMESH_2smeshpy::GenName(); }
207 private:
208   std::map< _pyID, Handle(_pyMesh) > myMeshes;
209   std::list< Handle(_pyHypothesis) > myHypos;
210   std::list< Handle(_pyCommand) >    myCommands;
211   int                                myNbCommands;
212   bool                               myHasPattern;
213   Resource_DataMapOfAsciiStringAsciiString& myID2AccessorMethod;
214
215   DEFINE_STANDARD_RTTI (_pyGen)
216 };
217
218 // -------------------------------------------------------------------------------------
219 /*!
220  * \brief Contains commands concerning mesh substructures
221  */
222 // -------------------------------------------------------------------------------------
223 #define _pyMesh_ACCESS_METHOD "GetMesh()"
224 class _pyMesh: public _pyObject
225 {
226   std::list< Handle(_pyHypothesis) > myHypos;
227   std::list< Handle(_pyCommand) > myAddHypCmds;
228   std::list< Handle(_pyCommand) > mySubmeshes;
229   bool                            myHasEditor;
230 public:
231   _pyMesh(const Handle(_pyCommand) theCreationCmd);
232   const _pyID& GetGeom() { return GetCreationCmd()->GetArg(1); }
233   void Process( const Handle(_pyCommand)& theCommand);
234   void Flush();
235   const char* AccessorMethod() const { return _pyMesh_ACCESS_METHOD; }
236 private:
237   static void AddMeshAccess( const Handle(_pyCommand)& theCommand )
238   { theCommand->SetObject( theCommand->GetObject() + "." _pyMesh_ACCESS_METHOD ); }
239
240   DEFINE_STANDARD_RTTI (_pyMesh)
241 };
242 #undef _pyMesh_ACCESS_METHOD 
243
244 // -------------------------------------------------------------------------------------
245 /*!
246  * \brief Root class for hypothesis
247  *
248  * HOWTO assure convertion of a new hypothesis
249  * In NewHypothesis():
250  * 1. add a case for the name of the new hypothesis and
251  * 2. initialize _pyHypothesis fields:
252  *    . myDim - hypothesis dimention;
253  *    . myType - type name of the algorithm creating the hypothesis;
254  *    . myCreationMethod - method name of the algorithm creating the hypothesis;
255  *    . append to myArgMethods interface methods setting param values in the
256  *    order they are used when myCreationMethod is called. It is supposed that
257  *    each interface method sets only one parameter, if it is not so, you are
258  *    to derive a specific class from _pyHypothesis that would redefine Process(),
259  *    see _pyComplexParamHypo for example
260  */
261 // -------------------------------------------------------------------------------------
262 class _pyHypothesis: public _pyObject
263 {
264 protected:
265   bool    myIsAlgo, /*myIsLocal, */myIsWrapped, myIsConverted;
266   int     myDim/*, myAdditionCmdNb*/;
267   _pyID    myGeom, myMesh;
268   TCollection_AsciiString       myCreationMethod, myType;
269   TColStd_SequenceOfAsciiString myArgs;
270   TColStd_SequenceOfAsciiString myArgMethods;
271   TColStd_SequenceOfInteger     myNbArgsByMethod;
272   std::list<Handle(_pyCommand)>  myArgCommands;
273   std::list<Handle(_pyCommand)>  myUnknownCommands;
274 public:
275   _pyHypothesis(const Handle(_pyCommand)& theCreationCmd);
276   void SetDimMethodType(const int dim, const char* creationMethod, const char* type=0)
277   { myDim = dim; myCreationMethod = (char*)creationMethod; if ( type ) myType = (char*)type; }
278   void AddArgMethod(const char* method, const int nbArgs = 1)
279   { myArgMethods.Append( (char*)method ); myNbArgsByMethod.Append( nbArgs ); }
280   const TColStd_SequenceOfAsciiString& GetArgs() const { return myArgs; }
281   const TCollection_AsciiString& GetCreationMethod() const { return myCreationMethod; }
282   const std::list<Handle(_pyCommand)>& GetArgCommands() const { return myArgCommands; }
283   void ClearAllCommands();
284   virtual bool IsAlgo() const { return myIsAlgo; }
285   bool IsWrapped() const { return myIsWrapped; }
286   bool & IsConverted() { return myIsConverted; }
287   int GetDim() const { return myDim; }
288   const _pyID & GetGeom() const { return myGeom; }
289   void SetMesh( const _pyID& theMeshId) { if ( myMesh.IsEmpty() ) myMesh = theMeshId; }
290   const _pyID & GetMesh() const { return myMesh; }
291   const TCollection_AsciiString GetType() { return myType; }
292   bool IsWrappable(const _pyID& theMesh) { return !myIsWrapped && myMesh == theMesh; }
293   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
294                                   const _pyID&              theMesh);
295   static Handle(_pyHypothesis) NewHypothesis( const Handle(_pyCommand)& theCreationCmd);
296   //     bool HasMesh() const { return !myMesh.IsEmpty(); }
297   //     void SetGeom( const _pyID& theGeomID ) { myGeom = theGeomID; }
298   void Process( const Handle(_pyCommand)& theCommand);
299   void Flush();
300
301   DEFINE_STANDARD_RTTI (_pyHypothesis)
302 };
303
304 // -------------------------------------------------------------------------------------
305 /*!
306  * \brief Class for hypotheses having several parameters modified by one method
307  */
308 // -------------------------------------------------------------------------------------
309 class _pyComplexParamHypo: public _pyHypothesis
310 {
311 public:
312   _pyComplexParamHypo(const Handle(_pyCommand)& theCreationCmd): _pyHypothesis(theCreationCmd) {}
313   void Process( const Handle(_pyCommand)& theCommand);
314
315   DEFINE_STANDARD_RTTI (_pyComplexParamHypo)
316 };
317 DEFINE_STANDARD_HANDLE (_pyComplexParamHypo, _pyHypothesis);
318
319 // -------------------------------------------------------------------------------------
320 /*!
321  * \brief Class for LayerDistribution hypothesis conversion
322  */
323 // -------------------------------------------------------------------------------------
324 class _pyLayerDistributionHypo: public _pyHypothesis
325 {
326   Handle(_pyHypothesis) my1dHyp;
327 public:
328   _pyLayerDistributionHypo(const Handle(_pyCommand)& theCreationCmd):
329     _pyHypothesis(theCreationCmd) {}
330   void Process( const Handle(_pyCommand)& theCommand);
331   void Flush();
332   bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
333                           const _pyID&              theMesh);
334
335   DEFINE_STANDARD_RTTI (_pyLayerDistributionHypo)
336 };
337 DEFINE_STANDARD_HANDLE (_pyLayerDistributionHypo, _pyHypothesis);
338
339
340 // -------------------------------------------------------------------------------------
341 /*!
342  * \brief Class representing NumberOfSegments hypothesis
343  */
344 // -------------------------------------------------------------------------------------
345 class _pyNumberOfSegmentsHyp: public _pyHypothesis
346 {
347 public:
348   _pyNumberOfSegmentsHyp(const Handle(_pyCommand)& theCrCmd): _pyHypothesis(theCrCmd) {}
349   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
350                                   const _pyID&              theMesh);
351   void Flush();
352
353   DEFINE_STANDARD_RTTI (_pyNumberOfSegmentsHyp)
354 };
355 DEFINE_STANDARD_HANDLE (_pyNumberOfSegmentsHyp, _pyHypothesis);
356
357 // -------------------------------------------------------------------------------------
358 /*!
359  * \brief Class representing smesh.Mesh_Algorithm
360  */
361 // -------------------------------------------------------------------------------------
362 class _pyAlgorithm: public _pyHypothesis
363 {
364 public:
365   _pyAlgorithm(const Handle(_pyCommand)& theCreationCmd);
366   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
367                                   const _pyID&              theMesh);
368   const char* AccessorMethod() const { return "GetAlgorithm()"; }
369
370   DEFINE_STANDARD_RTTI (_pyAlgorithm)
371 };
372
373 #endif