Salome HOME
Merging from V3_2_6pre4
[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    * \brief Return errors of mesh computation
229    */
230   SMESH::compute_error_array* GetComputeErrors(SMESH::SMESH_Mesh_ptr theMesh,
231                                                GEOM::GEOM_Object_ptr  theShapeObject )
232     throw ( SALOME::SALOME_Exception );
233
234   // Returns true if mesh contains enough data to be computed
235   CORBA::Boolean IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
236                                    GEOM::GEOM_Object_ptr theShapeObject )
237     throw ( SALOME::SALOME_Exception );
238
239   // Returns errors of hypotheses definintion
240   SMESH::algo_error_array* GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh, 
241                                          GEOM::GEOM_Object_ptr theSubObject )
242       throw ( SALOME::SALOME_Exception );
243
244   // Get sub-shapes unique ID's list
245   SMESH::long_array* GetSubShapesId( GEOM::GEOM_Object_ptr      theMainShapeObject,
246                                      const SMESH::object_array& theListOfSubShape )
247     throw ( SALOME::SALOME_Exception );
248
249   // Return geometrical object the given element is built on. Publish it in study.
250   GEOM::GEOM_Object_ptr GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
251                                                   CORBA::Long            theElementID,
252                                                   const char*            theGeomName)
253     throw ( SALOME::SALOME_Exception );
254
255   // Return geometrical object the given element is built on. Don't publish it in study.
256   GEOM::GEOM_Object_ptr FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
257                                                    CORBA::Long            theElementID)
258     throw ( SALOME::SALOME_Exception );
259
260   // Concatenate the given meshes into one mesh
261   SMESH::SMESH_Mesh_ptr Concatenate(const SMESH::mesh_array& theMeshesArray, 
262                                     CORBA::Boolean           theUniteIdenticalGroups, 
263                                     CORBA::Boolean           theMergeNodesAndElements, 
264                                     CORBA::Double            theMergeTolerance)
265     throw ( SALOME::SALOME_Exception );
266
267
268   // ****************************************************
269   // Interface inherited methods (from SALOMEDS::Driver)
270   // ****************************************************
271
272   // Save SMESH data
273   SALOMEDS::TMPFile* Save( SALOMEDS::SComponent_ptr theComponent,
274                          const char*              theURL,
275                          bool                     isMultiFile );
276   // Load SMESH data
277   bool Load( SALOMEDS::SComponent_ptr theComponent,
278              const SALOMEDS::TMPFile& theStream,
279              const char*              theURL,
280              bool                     isMultiFile );
281   // Save SMESH data in ASCII format
282   SALOMEDS::TMPFile* SaveASCII( SALOMEDS::SComponent_ptr theComponent,
283                                 const char*              theURL,
284                                 bool                     isMultiFile );
285   // Load SMESH data in ASCII format
286   bool LoadASCII( SALOMEDS::SComponent_ptr theComponent,
287                   const SALOMEDS::TMPFile& theStream,
288                   const char*              theURL,
289                   bool                     isMultiFile );
290
291   // Create filter manager
292   SMESH::FilterManager_ptr CreateFilterManager();
293
294   // Return a pattern mesher
295   SMESH::SMESH_Pattern_ptr GetPattern();
296
297   // Clears study-connected data when it is closed
298   void Close( SALOMEDS::SComponent_ptr theComponent );
299   
300   // Get component data type
301   char* ComponentDataType();
302     
303   // Transform data from transient form to persistent
304   char* IORToLocalPersistentID( SALOMEDS::SObject_ptr theSObject,
305                                 const char*           IORString,
306                                 CORBA::Boolean        isMultiFile,
307                                 CORBA::Boolean        isASCII );
308   // Transform data from persistent form to transient
309   char* LocalPersistentIDToIOR( SALOMEDS::SObject_ptr theSObject,
310                                 const char*           aLocalPersistentID,
311                                 CORBA::Boolean        isMultiFile,
312                                 CORBA::Boolean        isASCII );
313
314   // Returns true if object can be published in the study
315   bool CanPublishInStudy( CORBA::Object_ptr theIOR );
316   // Publish object in the study
317   SALOMEDS::SObject_ptr PublishInStudy( SALOMEDS::Study_ptr   theStudy,
318                                         SALOMEDS::SObject_ptr theSObject,
319                                         CORBA::Object_ptr     theObject,
320                                         const char*           theName ) 
321     throw ( SALOME::SALOME_Exception );
322
323   // Copy-paste methods - returns true if object can be copied to the clipboard
324   CORBA::Boolean CanCopy( SALOMEDS::SObject_ptr theObject ) { return false; }
325   // Copy-paste methods - copy object to the clipboard
326   SALOMEDS::TMPFile* CopyFrom( SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID ) { return false; }
327   // Copy-paste methods - returns true if object can be pasted from the clipboard
328   CORBA::Boolean CanPaste( const char* theComponentName, CORBA::Long theObjectID ) { return false; }
329   // Copy-paste methods - paste object from the clipboard
330   SALOMEDS::SObject_ptr PasteInto( const SALOMEDS::TMPFile& theStream,
331                                    CORBA::Long              theObjectID,
332                                    SALOMEDS::SObject_ptr    theObject ) {
333     SALOMEDS::SObject_var aResultSO;
334     return aResultSO._retn();
335   }
336
337   // ============
338   // Dump python
339   // ============
340
341   virtual Engines::TMPFile* DumpPython(CORBA::Object_ptr theStudy, 
342                                        CORBA::Boolean isPublished, 
343                                        CORBA::Boolean& isValidScript);
344
345   void AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString);
346
347   void RemoveLastFromPythonScript (int theStudyID);
348
349   void SavePython (SALOMEDS::Study_ptr theStudy);
350
351   TCollection_AsciiString DumpPython_impl (int theStudyID, 
352                                            Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
353                                            Resource_DataMapOfAsciiStringAsciiString& theNames,
354                                            bool isPublished, 
355                                            bool& aValidScript,
356                                            const TCollection_AsciiString& theSavedTrace);
357
358   TCollection_AsciiString GetNewPythonLines (int theStudyID);
359
360   void CleanPythonTrace (int theStudyID);
361
362
363   // *****************************************
364   // Internal methods
365   // *****************************************
366 public:
367   // Get shape reader
368   GEOM_Client* GetShapeReader();
369
370   // Tags definition 
371   static long GetHypothesisRootTag();
372   static long GetAlgorithmsRootTag();
373   static long GetRefOnShapeTag();
374   static long GetRefOnAppliedHypothesisTag();
375   static long GetRefOnAppliedAlgorithmsTag();
376   static long GetSubMeshOnVertexTag();
377   static long GetSubMeshOnEdgeTag();
378   static long GetSubMeshOnFaceTag();
379   static long GetSubMeshOnSolidTag();
380   static long GetSubMeshOnCompoundTag();
381   static long GetSubMeshOnWireTag();
382   static long GetSubMeshOnShellTag();
383   static long GetNodeGroupsTag();
384   static long GetEdgeGroupsTag();
385   static long GetFaceGroupsTag();
386   static long GetVolumeGroupsTag();
387
388   // publishing methods
389   SALOMEDS::SComponent_ptr PublishComponent(SALOMEDS::Study_ptr theStudy);
390   SALOMEDS::SObject_ptr PublishMesh (SALOMEDS::Study_ptr   theStudy,
391                                      SMESH::SMESH_Mesh_ptr theMesh,
392                                      const char*           theName = 0);
393   SALOMEDS::SObject_ptr PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
394                                            SMESH::SMESH_Hypothesis_ptr theHyp,
395                                            const char*                 theName = 0);
396   SALOMEDS::SObject_ptr PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
397                                         SMESH::SMESH_Mesh_ptr    theMesh,
398                                         SMESH::SMESH_subMesh_ptr theSubMesh,
399                                         GEOM::GEOM_Object_ptr    theShapeObject,
400                                         const char*              theName = 0);
401   SALOMEDS::SObject_ptr PublishGroup (SALOMEDS::Study_ptr    theStudy,
402                                       SMESH::SMESH_Mesh_ptr  theMesh,
403                                       SMESH::SMESH_GroupBase_ptr theGroup,
404                                       GEOM::GEOM_Object_ptr  theShapeObject,
405                                       const char*            theName = 0);
406   bool AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
407                             SMESH::SMESH_Mesh_ptr       theMesh,
408                             GEOM::GEOM_Object_ptr       theShapeObject,
409                             SMESH::SMESH_Hypothesis_ptr theHyp);
410   bool RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
411                                  SMESH::SMESH_Mesh_ptr       theMesh,
412                                  GEOM::GEOM_Object_ptr       theShapeObject,
413                                  SMESH::SMESH_Hypothesis_ptr theHyp);
414   SALOMEDS::SObject_ptr GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
415                                                  SMESH::SMESH_Mesh_ptr theMesh,
416                                                  GEOM::GEOM_Object_ptr theShape);
417   static void SetName(SALOMEDS::SObject_ptr theSObject,
418                       const char*           theName,
419                       const char*           theDefaultName = 0);
420
421   //  Get study context
422   StudyContext* GetCurrentStudyContext();
423
424   // Register an object in a StudyContext; return object id
425   int RegisterObject(CORBA::Object_ptr theObject);
426
427   // Return id of registered object
428   int GetObjectId(CORBA::Object_ptr theObject);
429
430   // Return an object that previously had an oldID
431   template<class TInterface> 
432   typename TInterface::_var_type GetObjectByOldId( const int oldID )
433   {
434     if ( StudyContext* myStudyContext = GetCurrentStudyContext() ) {
435       string ior = myStudyContext->getIORbyOldId( oldID );
436       if ( !ior.empty() )
437         return TInterface::_narrow(GetORB()->string_to_object( ior.c_str() ));
438     }
439     return TInterface::_nil();
440   }
441
442   // Get current study ID
443   int GetCurrentStudyID()
444   { return myCurrentStudy->_is_nil() ? -1 : myCurrentStudy->StudyId(); }
445
446   /*!
447    * \brief Find SObject for an algo
448    */
449   SALOMEDS::SObject_ptr GetAlgoSO(const ::SMESH_Algo* algo);
450  
451 private:
452   // Create hypothesis of given type
453   SMESH::SMESH_Hypothesis_ptr createHypothesis( const char* theHypName,
454                                                 const char* theLibName)
455     throw ( SALOME::SALOME_Exception );
456   
457   // Create empty mesh on shape
458   SMESH::SMESH_Mesh_ptr createMesh()
459     throw ( SALOME::SALOME_Exception );
460
461   static void loadGeomData( SALOMEDS::SComponent_ptr theCompRoot );
462   
463 private:
464
465   static CORBA::ORB_var          myOrb;         // ORB reference
466   static PortableServer::POA_var myPoa;         // POA reference
467   static SALOME_NamingService*   myNS;          // Naming Service
468   static SALOME_LifeCycleCORBA*  myLCC;         // Life Cycle CORBA
469   static SMESH_Gen_i*            mySMESHGen;    // Point to last created instance of the class
470   ::SMESH_Gen               myGen;              // SMESH_Gen local implementation
471
472   // hypotheses managing
473   map<string, GenericHypothesisCreator_i*> myHypCreatorMap;
474
475   map<int, StudyContext*>   myStudyContextMap;  // Map of study context objects
476
477   GEOM_Client*              myShapeReader;      // Shape reader
478   SALOMEDS::Study_var       myCurrentStudy;     // Current study
479   CORBA::Boolean            myIsEmbeddedMode;   // Current mode
480
481   // Dump Python: trace of API methods calls
482   std::map < int, Handle(TColStd_HSequenceOfAsciiString) > myPythonScripts;
483 };
484
485
486 namespace SMESH
487 {
488   template<class T>
489   T
490   DownCast(CORBA::Object_ptr theArg)
491   {
492     return dynamic_cast<T>(SMESH_Gen_i::GetServant(theArg).in());
493   }
494 }
495
496
497 #endif