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