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