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