Salome HOME
SMH: Preparation version 3.0.0 - merge (HEAD+POLYWORK)
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i.hxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESH_Gen_i.hxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 #ifndef _SMESH_GEN_I_HXX_
30 #define _SMESH_GEN_I_HXX_
31
32 #include <SALOMEconfig.h>
33 #include CORBA_SERVER_HEADER(SMESH_Gen)
34 #include CORBA_SERVER_HEADER(SMESH_Mesh)
35 #include CORBA_SERVER_HEADER(SMESH_Hypothesis)
36 #include CORBA_CLIENT_HEADER(GEOM_Gen)
37 #include CORBA_CLIENT_HEADER(SALOMEDS)
38 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
39
40 #include "SMESH_Mesh_i.hxx"
41 #include "SMESH_Hypothesis_i.hxx"
42 #include "SALOME_Component_i.hxx"
43 #include "SALOME_NamingService.hxx"
44
45 #include "SMESH_Gen.hxx"
46 #include "GEOM_Client.hxx"
47
48 #include <TCollection_AsciiString.hxx>
49 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
50 #include <TColStd_HSequenceOfAsciiString.hxx>
51
52 #include <map>
53 #include <sstream>
54
55 class SMESH_Mesh_i;
56 class SALOME_LifeCycleCORBA;
57
58 // ===========================================================
59 // Study context - stores study-connected objects references
60 // ==========================================================
61 class StudyContext
62 {
63 public:
64   // constructor
65   StudyContext() {}
66   // destructor
67   ~StudyContext() 
68   { 
69     mapIdToIOR.clear();
70     mapIdToId.clear();
71   }
72   // register object in the internal map and return its id
73   int addObject( string theIOR )
74   {
75     int nextId = getNextId();
76     mapIdToIOR[ nextId ]  = theIOR;
77     return nextId;
78   }
79   // find the object id in the internal map by the IOR
80   int findId( string theIOR )
81   {
82     map<int, string>::iterator imap;
83     for ( imap = mapIdToIOR.begin(); imap != mapIdToIOR.end(); ++imap ) {
84       if ( imap->second == theIOR )
85         return imap->first;
86     }
87     return 0;
88   }
89   // get object's IOR by id
90   string getIORbyId( const int theId )
91   {
92     if ( mapIdToIOR.find( theId ) != mapIdToIOR.end() )
93       return mapIdToIOR[ theId ];
94     return string( "" );
95   }
96   // get object's IOR by old id
97   string getIORbyOldId( const int theOldId )
98   {
99     if ( mapIdToId.find( theOldId ) != mapIdToId.end() )
100       return getIORbyId( mapIdToId[ theOldId ] );
101     return string( "" );
102   }
103   // maps old object id to the new one (used when restoring data)
104   void mapOldToNew( const int oldId, const int newId ) {
105     mapIdToId[ oldId ] = newId;
106   }
107     
108 private:
109   // get next free object identifier
110   int getNextId()
111   {
112     int id = 1;
113     while( mapIdToIOR.find( id ) != mapIdToIOR.end() )
114       id++;
115     return id;
116   }
117
118   map<int, string> mapIdToIOR;      // persistent-to-transient map
119   map<int, int>    mapIdToId;       // used to translate object from persistent to transient form
120 };
121
122 // ===========================================================
123 // SMESH module's engine
124 // ==========================================================
125 class SMESH_Gen_i:
126   public virtual POA_SMESH::SMESH_Gen,
127   public virtual Engines_Component_i 
128 {
129 public:
130   // Get last created instance of the class
131   static SMESH_Gen_i* GetSMESHGen() { return mySMESHGen;}
132   // Get ORB object
133   static CORBA::ORB_var GetORB() { return myOrb;}
134   // Get SMESH module's POA object
135   static PortableServer::POA_var GetPOA() { return myPoa;}
136   // Get Naming Service object
137   static SALOME_NamingService* GetNS();
138   // Get SALOME_LifeCycleCORBA object
139   static SALOME_LifeCycleCORBA* GetLCC();
140   // Retrieve and get GEOM engine reference
141   static GEOM::GEOM_Gen_ptr GetGeomEngine();
142   // Get object of the CORBA reference
143   static PortableServer::ServantBase_var GetServant( CORBA::Object_ptr theObject );
144   // Get CORBA object corresponding to the SALOMEDS::SObject
145   static CORBA::Object_var SObjectToObject( SALOMEDS::SObject_ptr theSObject );
146   // Get the SALOMEDS::SObject corresponding to a CORBA object
147   static SALOMEDS::SObject_ptr ObjectToSObject(SALOMEDS::Study_ptr theStudy,
148                                                CORBA::Object_ptr   theObject);
149   // Get GEOM Object correspoding to TopoDS_Shape
150   GEOM::GEOM_Object_ptr ShapeToGeomObject (const TopoDS_Shape& theShape );
151   // Get TopoDS_Shape correspoding to GEOM_Object
152   TopoDS_Shape GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject);
153   
154   // Default constructor
155   SMESH_Gen_i();
156   // Standard constructor
157   SMESH_Gen_i( CORBA::ORB_ptr            orb,
158                PortableServer::POA_ptr   poa,
159                PortableServer::ObjectId* contId, 
160                const char*               instanceName, 
161                const char*               interfaceName );
162   // Destructor
163   virtual ~SMESH_Gen_i();
164   
165   // *****************************************
166   // Interface methods
167   // *****************************************
168
169   // Set current study
170   void SetCurrentStudy( SALOMEDS::Study_ptr theStudy );
171   // Get current study
172   SALOMEDS::Study_ptr GetCurrentStudy();
173
174   // Create hypothesis/algorothm of given type
175   SMESH::SMESH_Hypothesis_ptr CreateHypothesis (const char* theHypType,
176                                                 const char* theLibName)
177     throw ( SALOME::SALOME_Exception );
178   
179   // Create empty mesh on a shape
180   SMESH::SMESH_Mesh_ptr CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
181     throw ( SALOME::SALOME_Exception );
182
183   //  Create mesh(es) and import data from UNV file
184   SMESH::SMESH_Mesh_ptr CreateMeshesFromUNV( const char* theFileName )
185     throw ( SALOME::SALOME_Exception );
186
187   //  Create mesh(es) and import data from MED file
188   SMESH::mesh_array* CreateMeshesFromMED( const char* theFileName,
189                                           SMESH::DriverMED_ReadStatus& theStatus )
190     throw ( SALOME::SALOME_Exception );
191
192   //  Create mesh(es) and import data from STL file
193   SMESH::SMESH_Mesh_ptr CreateMeshesFromSTL( const char* theFileName )
194     throw ( SALOME::SALOME_Exception );
195
196   // Compute mesh on a shape
197   CORBA::Boolean Compute( SMESH::SMESH_Mesh_ptr theMesh,
198                           GEOM::GEOM_Object_ptr  theShapeObject )
199     throw ( SALOME::SALOME_Exception );
200
201   // Returns true if mesh contains enough data to be computed
202   CORBA::Boolean IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
203                                    GEOM::GEOM_Object_ptr theShapeObject )
204     throw ( SALOME::SALOME_Exception );
205
206   // Get sub-shapes unique ID's list
207   SMESH::long_array* GetSubShapesId( GEOM::GEOM_Object_ptr      theMainShapeObject,
208                                      const SMESH::object_array& theListOfSubShape )
209     throw ( SALOME::SALOME_Exception );
210
211
212   // ****************************************************
213   // Interface inherited methods (from SALOMEDS::Driver)
214   // ****************************************************
215
216   // Save SMESH data
217   SALOMEDS::TMPFile* Save( SALOMEDS::SComponent_ptr theComponent,
218                          const char*              theURL,
219                          bool                     isMultiFile );
220   // Load SMESH data
221   bool Load( SALOMEDS::SComponent_ptr theComponent,
222              const SALOMEDS::TMPFile& theStream,
223              const char*              theURL,
224              bool                     isMultiFile );
225   // Save SMESH data in ASCII format
226   SALOMEDS::TMPFile* SaveASCII( SALOMEDS::SComponent_ptr theComponent,
227                                 const char*              theURL,
228                                 bool                     isMultiFile );
229   // Load SMESH data in ASCII format
230   bool LoadASCII( SALOMEDS::SComponent_ptr theComponent,
231                   const SALOMEDS::TMPFile& theStream,
232                   const char*              theURL,
233                   bool                     isMultiFile );
234
235   // Create filter manager
236   SMESH::FilterManager_ptr CreateFilterManager();
237
238   // Return a pattern mesher
239   SMESH::SMESH_Pattern_ptr GetPattern();
240
241   // Clears study-connected data when it is closed
242   void Close( SALOMEDS::SComponent_ptr theComponent );
243   
244   // Get component data type
245   char* ComponentDataType();
246     
247   // Transform data from transient form to persistent
248   char* IORToLocalPersistentID( SALOMEDS::SObject_ptr theSObject,
249                                 const char*           IORString,
250                                 CORBA::Boolean        isMultiFile,
251                                 CORBA::Boolean        isASCII );
252   // Transform data from persistent form to transient
253   char* LocalPersistentIDToIOR( SALOMEDS::SObject_ptr theSObject,
254                                 const char*           aLocalPersistentID,
255                                 CORBA::Boolean        isMultiFile,
256                                 CORBA::Boolean        isASCII );
257
258   // Returns true if object can be published in the study
259   bool CanPublishInStudy( CORBA::Object_ptr theIOR );
260   // Publish object in the study
261   SALOMEDS::SObject_ptr PublishInStudy( SALOMEDS::Study_ptr   theStudy,
262                                         SALOMEDS::SObject_ptr theSObject,
263                                         CORBA::Object_ptr     theObject,
264                                         const char*           theName ) 
265     throw ( SALOME::SALOME_Exception );
266
267   // Copy-paste methods - returns true if object can be copied to the clipboard
268   CORBA::Boolean CanCopy( SALOMEDS::SObject_ptr theObject ) { return false; }
269   // Copy-paste methods - copy object to the clipboard
270   SALOMEDS::TMPFile* CopyFrom( SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID ) { return false; }
271   // Copy-paste methods - returns true if object can be pasted from the clipboard
272   CORBA::Boolean CanPaste( const char* theComponentName, CORBA::Long theObjectID ) { return false; }
273   // Copy-paste methods - paste object from the clipboard
274   SALOMEDS::SObject_ptr PasteInto( const SALOMEDS::TMPFile& theStream,
275                                    CORBA::Long              theObjectID,
276                                    SALOMEDS::SObject_ptr    theObject ) {
277     SALOMEDS::SObject_var aResultSO;
278     return aResultSO._retn();
279   }
280
281   // ============
282   // Dump python
283   // ============
284
285   virtual Engines::TMPFile* DumpPython(CORBA::Object_ptr theStudy, 
286                                        CORBA::Boolean isPublished, 
287                                        CORBA::Boolean& isValidScript);
288
289   void AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString);
290
291   void RemoveLastFromPythonScript (int theStudyID);
292
293   static void AddToCurrentPyScript (const TCollection_AsciiString& theString);
294
295   void SavePython (SALOMEDS::Study_ptr theStudy);
296
297   TCollection_AsciiString DumpPython_impl (int theStudyID, 
298                                            Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
299                                            Resource_DataMapOfAsciiStringAsciiString& theNames,
300                                            bool isPublished, 
301                                            bool& aValidScript,
302                                            const TCollection_AsciiString& theSavedTrace);
303
304   TCollection_AsciiString GetNewPythonLines (int theStudyID);
305
306   void CleanPythonTrace (int theStudyID);
307
308   // Dump python comfort methods
309
310   static TCollection_AsciiString& AddObject(TCollection_AsciiString& theStr,
311                                             CORBA::Object_ptr        theObject);
312   // add object to script string
313
314   template <class _array>
315     static TCollection_AsciiString& AddArray(TCollection_AsciiString& theStr,
316                                              const _array &           array)
317       // put array contents into theStr like this: "[ 1, 2, 5 ]"
318     {
319       ostringstream sout; // can convert long int, and TCollection_AsciiString cant
320       sout << "[ ";
321       for (int i = 1; i <= array.length(); i++) {
322         sout << array[i-1];
323         if ( i < array.length() )
324           sout << ", ";
325       }
326       sout << " ]";
327       theStr += (char*) sout.str().c_str();
328       return theStr;
329     }
330
331   // *****************************************
332   // Internal methods
333   // *****************************************
334 public:
335   // Get shape reader
336   GEOM_Client* GetShapeReader();
337
338   // Tags definition 
339   static long GetHypothesisRootTag();
340   static long GetAlgorithmsRootTag();
341   static long GetRefOnShapeTag();
342   static long GetRefOnAppliedHypothesisTag();
343   static long GetRefOnAppliedAlgorithmsTag();
344   static long GetSubMeshOnVertexTag();
345   static long GetSubMeshOnEdgeTag();
346   static long GetSubMeshOnFaceTag();
347   static long GetSubMeshOnSolidTag();
348   static long GetSubMeshOnCompoundTag();
349   static long GetSubMeshOnWireTag();
350   static long GetSubMeshOnShellTag();
351   static long GetNodeGroupsTag();
352   static long GetEdgeGroupsTag();
353   static long GetFaceGroupsTag();
354   static long GetVolumeGroupsTag();
355
356   // publishing methods
357   SALOMEDS::SComponent_ptr PublishComponent(SALOMEDS::Study_ptr theStudy);
358   SALOMEDS::SObject_ptr PublishMesh (SALOMEDS::Study_ptr   theStudy,
359                                      SMESH::SMESH_Mesh_ptr theMesh,
360                                      const char*           theName = 0);
361   SALOMEDS::SObject_ptr PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
362                                            SMESH::SMESH_Hypothesis_ptr theHyp,
363                                            const char*                 theName = 0);
364   SALOMEDS::SObject_ptr PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
365                                         SMESH::SMESH_Mesh_ptr    theMesh,
366                                         SMESH::SMESH_subMesh_ptr theSubMesh,
367                                         GEOM::GEOM_Object_ptr    theShapeObject,
368                                         const char*              theName = 0);
369   SALOMEDS::SObject_ptr PublishGroup (SALOMEDS::Study_ptr    theStudy,
370                                       SMESH::SMESH_Mesh_ptr  theMesh,
371                                       SMESH::SMESH_GroupBase_ptr theGroup,
372                                       GEOM::GEOM_Object_ptr  theShapeObject,
373                                       const char*            theName = 0);
374   bool AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
375                             SMESH::SMESH_Mesh_ptr       theMesh,
376                             GEOM::GEOM_Object_ptr       theShapeObject,
377                             SMESH::SMESH_Hypothesis_ptr theHyp);
378   bool RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
379                                  SMESH::SMESH_Mesh_ptr       theMesh,
380                                  GEOM::GEOM_Object_ptr       theShapeObject,
381                                  SMESH::SMESH_Hypothesis_ptr theHyp);
382   SALOMEDS::SObject_ptr GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
383                                                  SMESH::SMESH_Mesh_ptr theMesh,
384                                                  GEOM::GEOM_Object_ptr theShape);
385   static void SetName(SALOMEDS::SObject_ptr theSObject,
386                       const char*           theName,
387                       const char*           theDefaultName = 0);
388
389   //  Get study context
390   StudyContext* GetCurrentStudyContext();
391
392   // Register an object in a StudyContext; return object id
393   int RegisterObject(CORBA::Object_ptr theObject);
394
395   // Get current study ID
396   int GetCurrentStudyID()
397   { return myCurrentStudy->_is_nil() ? -1 : myCurrentStudy->StudyId(); }
398  
399 private:
400   // Create hypothesis of given type
401   SMESH::SMESH_Hypothesis_ptr createHypothesis( const char* theHypName,
402                                                 const char* theLibName)
403     throw ( SALOME::SALOME_Exception );
404   
405   // Create empty mesh on shape
406   SMESH::SMESH_Mesh_ptr createMesh()
407     throw ( SALOME::SALOME_Exception );
408
409   static void loadGeomData( SALOMEDS::SComponent_ptr theCompRoot );
410   
411 private:
412
413   static CORBA::ORB_var          myOrb;         // ORB reference
414   static PortableServer::POA_var myPoa;         // POA reference
415   static SALOME_NamingService*   myNS;          // Naming Service
416   static SALOME_LifeCycleCORBA*  myLCC;         // Life Cycle CORBA
417   static SMESH_Gen_i*            mySMESHGen;    // Point to last created instance of the class
418   ::SMESH_Gen               myGen;              // SMESH_Gen local implementation
419
420   // hypotheses managing
421   map<string, GenericHypothesisCreator_i*> myHypCreatorMap;
422
423   map<int, StudyContext*>   myStudyContextMap;  // Map of study context objects
424
425   GEOM_Client*              myShapeReader;      // Shape reader
426   SALOMEDS::Study_var       myCurrentStudy;     // Current study
427
428   // Dump Python: trace of API methods calls
429   std::map < int, Handle(TColStd_HSequenceOfAsciiString) > myPythonScripts;
430 };
431
432
433 namespace SMESH
434 {
435   template<class T>
436   T
437   DownCast(CORBA::Object_ptr theArg)
438   {
439     return dynamic_cast<T>(SMESH_Gen_i::GetServant(theArg).in());
440   }
441 }
442
443
444 #endif