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