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