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