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