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