Salome HOME
Merge from V5_1_main 14/05/2010
[modules/smesh.git] / src / SMESH_I / SMESH_2smeshpy.hxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File      : SMESH_smesh.hxx
24 // Created   : Fri Nov 18 12:05:18 2005
25 // Author    : Edward AGAPOV (eap)
26 //
27 #ifndef SMESH_smesh_HeaderFile
28 #define SMESH_smesh_HeaderFile
29
30 #include <Standard_DefineHandle.hxx>
31 #include <Standard_Type.hxx>
32 #include <Standard_Transient.hxx>
33 #include <TCollection_AsciiString.hxx>
34 #include <TColStd_SequenceOfAsciiString.hxx>
35 #include <TColStd_SequenceOfInteger.hxx>
36
37 #include <list>
38 #include <map>
39
40 // ===========================================================================================
41 /*!
42  * This file was created in order to respond to requirement of bug PAL10494:
43  * SMESH python dump uses idl interface.
44  *
45  * The creation reason is that smesh.py commands defining hypotheses encapsulate
46  * several SMESH engine method calls. As well, the dependencies between smesh.py
47  * classes differ from ones between corresponding SMESH IDL interfaces.
48  * 
49  * Everything here is for internal usage by SMESH_2smeshpy::ConvertScript()
50  * declared in SMESH_PythonDump.hxx
51  *
52  * See comments to _pyHypothesis class to know how to assure convertion of a new
53  * type of hypothesis
54  */
55 // ===========================================================================================
56
57 class Resource_DataMapOfAsciiStringAsciiString;
58
59 // ===========================================================================================
60 // =====================
61 //    INTERNAL STUFF
62 // =====================
63 // ===========================================================================================
64
65 class _pyCommand;
66 class _pyObject;
67 class _pyGen;
68 class _pyMesh;
69 class _pySubMesh;
70 class _pyHypothesis;
71 class _pyAlgorithm;
72 class _pyFilterManager;
73
74 DEFINE_STANDARD_HANDLE (_pyCommand   ,Standard_Transient);
75 DEFINE_STANDARD_HANDLE (_pyObject    ,Standard_Transient);
76 DEFINE_STANDARD_HANDLE (_pyGen       ,_pyObject);
77 DEFINE_STANDARD_HANDLE (_pyMesh      ,_pyObject);
78 DEFINE_STANDARD_HANDLE (_pySubMesh   ,_pyObject);
79 DEFINE_STANDARD_HANDLE (_pyMeshEditor,_pyObject);
80 DEFINE_STANDARD_HANDLE (_pyHypothesis,_pyObject);
81 DEFINE_STANDARD_HANDLE (_pyFilterManager,_pyObject);
82 DEFINE_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
83
84 typedef TCollection_AsciiString _pyID;
85
86 // ===========================================================
87 /*!
88  * \brief Class operating on a command string looking like
89  *        ResultValue = Object.Method( Arg1, Arg2,...)
90  */
91 // ===========================================================
92
93 class _pyCommand: public Standard_Transient
94 {
95   int                             myOrderNb;            //!< position within the script
96   TCollection_AsciiString         myString;             //!< command text
97   TCollection_AsciiString         myRes, myObj, myMeth; //!< found parts of command
98   TColStd_SequenceOfAsciiString   myArgs;               //!< found arguments
99   TColStd_SequenceOfInteger       myBegPos;             //!< where myRes, myObj, ... begin
100   std::list< Handle(_pyCommand) > myDependentCmds; //!< commands that sould follow me in the script
101
102   enum { UNKNOWN=-1, EMPTY=0, RESULT_IND, OBJECT_IND, METHOD_IND, ARG1_IND };
103   int GetBegPos( int thePartIndex );
104   void SetBegPos( int thePartIndex, int thePosition );
105   void SetPart( int thePartIndex, const TCollection_AsciiString& theNewPart,
106                 TCollection_AsciiString& theOldPart);
107   void FindAllArgs() { GetArg(1); }
108
109 public:
110   _pyCommand() {};
111   _pyCommand( const TCollection_AsciiString& theString, int theNb )
112     : myString( theString ), myOrderNb( theNb ) {};
113   TCollection_AsciiString & GetString() { return myString; }
114   int GetOrderNb() const { return myOrderNb; }
115   void SetOrderNb( int theNb ) { myOrderNb = theNb; }
116   int Length() { return myString.Length(); }
117   void Clear() { myString.Clear(); myBegPos.Clear(); myArgs.Clear(); }
118   bool IsEmpty() const { return myString.IsEmpty(); }
119   TCollection_AsciiString GetIndentation();
120   const TCollection_AsciiString & GetResultValue();
121   const int GetNbResultValues();
122   const TCollection_AsciiString & GetResultValue(int res);
123   const TCollection_AsciiString & GetObject();
124   const TCollection_AsciiString & GetMethod();
125   const TCollection_AsciiString & GetArg( int index );
126   int GetNbArgs() { FindAllArgs(); return myArgs.Length(); }
127   //Handle(TColStd_HSequenceOfAsciiString) GetArgs();
128   void SetResultValue( const TCollection_AsciiString& theResult )
129   { GetResultValue(); SetPart( RESULT_IND, theResult, myRes ); }
130   void SetObject(const TCollection_AsciiString& theObject)
131   { GetObject(); SetPart( OBJECT_IND, theObject, myObj ); }
132   void SetMethod(const TCollection_AsciiString& theMethod)
133   { GetMethod(); SetPart( METHOD_IND, theMethod, myMeth ); }
134   void SetArg( int index, const TCollection_AsciiString& theArg);
135   void RemoveArgs();
136   static bool SkipSpaces( const TCollection_AsciiString & theSring, int & thePos );
137   static TCollection_AsciiString GetWord( const TCollection_AsciiString & theSring,
138                                           int & theStartPos, const bool theForward,
139                                           const bool dotIsWord = false);
140   void AddDependantCmd( Handle(_pyCommand) cmd, bool prepend = false)
141   { if (prepend) myDependentCmds.push_front( cmd ); else myDependentCmds.push_back( cmd ); }
142   bool SetDependentCmdsAfter() const;
143
144   bool AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod );
145
146   DEFINE_STANDARD_RTTI (_pyCommand)
147 };
148
149 // -------------------------------------------------------------------------------------
150 /*!
151  * \brief Root of all objects. It counts calls of Process()
152  */
153 // -------------------------------------------------------------------------------------
154
155 class _pyObject: public Standard_Transient
156 {
157   Handle(_pyCommand) myCreationCmd;
158   int                myNbCalls;
159 public:
160   _pyObject(const Handle(_pyCommand)& theCreationCmd)
161     : myCreationCmd(theCreationCmd), myNbCalls(0) {}
162   const _pyID& GetID() { return myCreationCmd->GetResultValue(); }
163   static _pyID FatherID(const _pyID & childID);
164   const Handle(_pyCommand)& GetCreationCmd() { return myCreationCmd; }
165   int GetNbCalls() const { return myNbCalls; }
166   void  SetCreationCmd( Handle(_pyCommand) cmd ) { myCreationCmd = cmd; }
167   int GetCommandNb() { return myCreationCmd->GetOrderNb(); }
168   virtual void Process(const Handle(_pyCommand) & theCommand) { myNbCalls++; }
169   virtual void Flush() = 0;
170   virtual const char* AccessorMethod() const;
171
172   DEFINE_STANDARD_RTTI (_pyObject)
173 };
174
175 // -------------------------------------------------------------------------------------
176 /*!
177  * \brief Class corresponding to SMESH_Gen. It holds info on existing
178  *        meshes and hypotheses
179  */
180 // -------------------------------------------------------------------------------------
181 class _pyGen: public _pyObject
182 {
183 public:
184   _pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
185          Resource_DataMapOfAsciiStringAsciiString& theObjectNames);
186   //~_pyGen();
187   Handle(_pyCommand) AddCommand( const TCollection_AsciiString& theCommand );
188   void Process( const Handle(_pyCommand)& theCommand );
189   void Flush();
190   Handle(_pyHypothesis) FindHyp( const _pyID& theHypID );
191   Handle(_pyHypothesis) FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
192                                   const Handle(_pyHypothesis)& theHypothesis);
193   Handle(_pySubMesh) FindSubMesh( const _pyID& theSubMeshID );
194   void ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 );
195   void SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd );
196   void SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd );
197   Handle(_pyCommand)& GetLastCommand();
198   std::list< Handle(_pyCommand) >& GetCommands() { return myCommands; }
199   void SetAccessorMethod(const _pyID& theID, const char* theMethod );
200   bool AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const;
201   bool AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const;
202   const char* AccessorMethod() const;
203   _pyID GenerateNewID( const _pyID& theID );
204
205 private:
206   void setNeighbourCommand( Handle(_pyCommand)& theCmd,
207                             Handle(_pyCommand)& theOtherCmd,
208                             const bool theIsAfter );
209   
210 private:
211   std::map< _pyID, Handle(_pyMesh) >       myMeshes;
212   //std::map< _pyID, Handle(_pySubMesh) >    mySubMeshes;
213   std::map< _pyID, Handle(_pyMeshEditor) > myMeshEditors;
214   std::map< _pyID, Handle(_pyObject) >     myObjects;
215   std::list< Handle(_pyHypothesis) >       myHypos;
216   std::list< Handle(_pyCommand) >          myCommands;
217   int                                      myNbCommands;
218   Resource_DataMapOfAsciiStringAsciiString& myID2AccessorMethod;
219   Resource_DataMapOfAsciiStringAsciiString& myObjectNames;
220   Handle(_pyCommand)                       myLastCommand;
221
222   DEFINE_STANDARD_RTTI (_pyGen)
223 };
224
225 // -------------------------------------------------------------------------------------
226 /*!
227  * \brief Contains commands concerning mesh substructures
228  */
229 // -------------------------------------------------------------------------------------
230 #define _pyMesh_ACCESS_METHOD "GetMesh()"
231 class _pyMesh: public _pyObject
232 {
233   std::list< Handle(_pyHypothesis) > myHypos;
234   std::list< Handle(_pyCommand) > myAddHypCmds;
235   std::list< Handle(_pySubMesh) > mySubmeshes;
236   bool                            myHasEditor;
237 public:
238   _pyMesh(const Handle(_pyCommand) creationCmd);
239   _pyMesh(const Handle(_pyCommand) theCreationCmd, const TCollection_AsciiString & id);
240   const _pyID& GetGeom() { return GetCreationCmd()->GetArg(1); }
241   void Process( const Handle(_pyCommand)& theCommand);
242   void Flush();
243   const char* AccessorMethod() const { return _pyMesh_ACCESS_METHOD; }
244 private:
245   static bool NeedMeshAccess( const Handle(_pyCommand)& theCommand );
246   static void AddMeshAccess( const Handle(_pyCommand)& theCommand )
247   { theCommand->SetObject( theCommand->GetObject() + "." _pyMesh_ACCESS_METHOD ); }
248
249   //friend class _pyMeshEditor;
250   DEFINE_STANDARD_RTTI (_pyMesh)
251 };
252 #undef _pyMesh_ACCESS_METHOD 
253
254 // -------------------------------------------------------------------------------------
255 /*!
256  * \brief MeshEditor convert its commands to ones of mesh
257  */
258 // -------------------------------------------------------------------------------------
259 class _pyMeshEditor: public _pyObject
260 {
261   _pyID myMesh;
262   TCollection_AsciiString myCreationCmdStr;
263 public:
264   _pyMeshEditor(const Handle(_pyCommand)& theCreationCmd);
265   void Process( const Handle(_pyCommand)& theCommand);
266   virtual void Flush() {}
267
268   DEFINE_STANDARD_RTTI (_pyMesh)
269 };
270
271 // -------------------------------------------------------------------------------------
272 /*!
273  * \brief Root class for hypothesis
274  *
275  * HOWTO assure convertion of a new type of hypothesis
276  * In _pyHypothesis::NewHypothesis():
277  * 1. add a case for the name of the new hypothesis
278  * 2. use SetConvMethodAndType() to set
279  *    . for algo: algorithm name and method of Mesh creating the algo
280  *    . for hypo: name of the algorithm and method creating the hypothesis
281  * 3. append to myArgMethods interface methods setting param values in the
282  *    order they are used when creation method is called. If arguments of
283  *    the creation method can't be easily got from calls of hypothesis methods, you are
284  *    to derive a specific class from _pyHypothesis that would redefine Process(),
285  *    see _pyComplexParamHypo for example
286  */
287 // -------------------------------------------------------------------------------------
288 class _pyHypothesis: public _pyObject
289 {
290 protected:
291   bool    myIsAlgo, myIsWrapped;
292   _pyID   myGeom,   myMesh;
293   // a hypothesis can be used and created by different algos by different methods
294   std::map<TCollection_AsciiString, TCollection_AsciiString > myType2CreationMethod;
295   //TCollection_AsciiString       myCreationMethod, myType;
296   TColStd_SequenceOfAsciiString myArgs;           // creation arguments
297   TColStd_SequenceOfAsciiString myArgMethods;     // hypo methods setting myArgs
298   TColStd_SequenceOfInteger     myNbArgsByMethod; // nb args set by each method
299   std::list<Handle(_pyCommand)>  myArgCommands;
300   std::list<Handle(_pyCommand)>  myUnknownCommands;
301 public:
302   _pyHypothesis(const Handle(_pyCommand)& theCreationCmd);
303   void SetConvMethodAndType(const char* creationMethod, const char* type)
304   { myType2CreationMethod[ (char*)type ] = (char*)creationMethod; }
305   void AddArgMethod(const char* method, const int nbArgs = 1)
306   { myArgMethods.Append( (char*)method ); myNbArgsByMethod.Append( nbArgs ); }
307   const TColStd_SequenceOfAsciiString& GetArgs() const { return myArgs; }
308   const std::list<Handle(_pyCommand)>& GetArgCommands() const { return myArgCommands; }
309   void ClearAllCommands();
310   virtual bool IsAlgo() const { return myIsAlgo; }
311   bool IsValid() const { return !myType2CreationMethod.empty(); }
312   bool IsWrapped() const { return myIsWrapped; }
313   const _pyID & GetGeom() const { return myGeom; }
314   void SetMesh( const _pyID& theMeshId) { if ( myMesh.IsEmpty() ) myMesh = theMeshId; }
315   const _pyID & GetMesh() const { return myMesh; }
316   const TCollection_AsciiString& GetAlgoType() const
317   { return myType2CreationMethod.begin()->first; }
318   const TCollection_AsciiString& GetAlgoCreationMethod() const
319   { return myType2CreationMethod.begin()->second; }
320   bool CanBeCreatedBy(const TCollection_AsciiString& algoType ) const
321   { return myType2CreationMethod.find( algoType ) != myType2CreationMethod.end(); }
322   const TCollection_AsciiString& GetCreationMethod(const TCollection_AsciiString& algoType) const
323   { return myType2CreationMethod.find( algoType )->second; }
324   virtual bool IsWrappable(const _pyID& theMesh) { return !myIsWrapped && myMesh == theMesh; }
325   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
326                                   const _pyID&              theMesh);
327   static Handle(_pyHypothesis) NewHypothesis( const Handle(_pyCommand)& theCreationCmd);
328   void Process( const Handle(_pyCommand)& theCommand);
329   void Flush();
330   virtual void Assign( const Handle(_pyHypothesis)& theOther,
331                        const _pyID&                 theMesh );
332
333   DEFINE_STANDARD_RTTI (_pyHypothesis)
334 };
335
336 // -------------------------------------------------------------------------------------
337 /*!
338  * \brief Class representing smesh.Mesh_Algorithm
339  */
340 // -------------------------------------------------------------------------------------
341 class _pyAlgorithm: public _pyHypothesis
342 {
343 public:
344   _pyAlgorithm(const Handle(_pyCommand)& theCreationCmd);
345   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
346                                   const _pyID&              theMesh);
347   const char* AccessorMethod() const { return "GetAlgorithm()"; }
348   virtual bool IsWrappable(const _pyID& theMesh) { return !myIsWrapped; }
349
350   DEFINE_STANDARD_RTTI (_pyAlgorithm)
351 };
352
353 // -------------------------------------------------------------------------------------
354 /*!
355  * \brief Class for hypotheses having several parameters modified by one method
356  */
357 // -------------------------------------------------------------------------------------
358 class _pyComplexParamHypo: public _pyHypothesis
359 {
360 public:
361   _pyComplexParamHypo(const Handle(_pyCommand)& theCreationCmd): _pyHypothesis(theCreationCmd) {}
362   void Process( const Handle(_pyCommand)& theCommand);
363   void Flush();
364
365   DEFINE_STANDARD_RTTI (_pyComplexParamHypo)
366 };
367 DEFINE_STANDARD_HANDLE (_pyComplexParamHypo, _pyHypothesis);
368
369 // -------------------------------------------------------------------------------------
370 /*!
371  * \brief Class for LayerDistribution hypothesis conversion
372  */
373 // -------------------------------------------------------------------------------------
374 class _pyLayerDistributionHypo: public _pyHypothesis
375 {
376   Handle(_pyHypothesis) my1dHyp;
377   TCollection_AsciiString myAlgoMethod;
378 public:
379   _pyLayerDistributionHypo(const Handle(_pyCommand)& theCreationCmd, const char* algoMethod):
380     _pyHypothesis(theCreationCmd), myAlgoMethod((char*)algoMethod) {}
381   void Process( const Handle(_pyCommand)& theCommand);
382   void Flush();
383   bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
384                           const _pyID&              theMesh);
385
386   DEFINE_STANDARD_RTTI (_pyLayerDistributionHypo)
387 };
388 DEFINE_STANDARD_HANDLE (_pyLayerDistributionHypo, _pyHypothesis);
389
390 // -------------------------------------------------------------------------------------
391 /*!
392  * \brief Class representing NumberOfSegments hypothesis
393  */
394 // -------------------------------------------------------------------------------------
395 class _pyNumberOfSegmentsHyp: public _pyHypothesis
396 {
397 public:
398   _pyNumberOfSegmentsHyp(const Handle(_pyCommand)& theCrCmd): _pyHypothesis(theCrCmd) {}
399   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
400                                   const _pyID&              theMesh);
401   void Flush();
402
403   DEFINE_STANDARD_RTTI (_pyNumberOfSegmentsHyp)
404 };
405 DEFINE_STANDARD_HANDLE (_pyNumberOfSegmentsHyp, _pyHypothesis);
406
407 // -------------------------------------------------------------------------------------
408 /*!
409  * \brief Class representing SegmentLengthAroundVertex hypothesis
410  */
411 // -------------------------------------------------------------------------------------
412 class _pySegmentLengthAroundVertexHyp: public _pyHypothesis
413 {
414 public:
415   _pySegmentLengthAroundVertexHyp(const Handle(_pyCommand)& theCrCmd): _pyHypothesis(theCrCmd) {}
416   virtual bool Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
417                                   const _pyID&              theMesh);
418   DEFINE_STANDARD_RTTI (_pySegmentLengthAroundVertexHyp)
419 };
420 DEFINE_STANDARD_HANDLE (_pySegmentLengthAroundVertexHyp, _pyHypothesis);
421
422 // -------------------------------------------------------------------------------------
423 /*!
424  * \brief SelfEraser erases creation command if no more it's commands invoked
425  */
426 // -------------------------------------------------------------------------------------
427 class _pySelfEraser: public _pyObject
428 {
429 public:
430   _pySelfEraser(const Handle(_pyCommand)& theCreationCmd):_pyObject(theCreationCmd) {}
431   virtual void Flush();
432
433   DEFINE_STANDARD_RTTI (_pySelfEraser)
434 };
435 DEFINE_STANDARD_HANDLE (_pySelfEraser, _pyObject);
436
437 // -------------------------------------------------------------------------------------
438 /*!
439  * \brief SubMesh creation can be moved to the end of engine commands
440  */
441 // -------------------------------------------------------------------------------------
442 class _pySubMesh:  public _pyObject
443 {
444 public:
445   _pySubMesh(const Handle(_pyCommand)& theCreationCmd):_pyObject(theCreationCmd) {}
446   void Process( const Handle(_pyCommand)& theCommand);
447   virtual void Flush();
448   void SetCreator( const Handle(_pyObject)& theCreator ) { myCreator = theCreator; }
449
450   DEFINE_STANDARD_RTTI (_pySubMesh)
451 private:
452   Handle(_pyObject) myCreator;
453 };
454
455 #endif