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