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