Salome HOME
452065cd677d06b1787e717ccaadbcba953833fb
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
23 //  File   : SMESH_Gen_i.hxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : SMESH
26 //  $Header$
27 //
28 #ifndef _SMESH_GEN_I_HXX_
29 #define _SMESH_GEN_I_HXX_
30
31 #include "SMESH.hxx"
32
33 #include <SALOMEconfig.h>
34 #include CORBA_SERVER_HEADER(SMESH_Gen)
35 #include CORBA_SERVER_HEADER(SMESH_Mesh)
36 #include CORBA_SERVER_HEADER(SMESH_Hypothesis)
37 #include CORBA_CLIENT_HEADER(GEOM_Gen)
38 #include CORBA_CLIENT_HEADER(SALOMEDS)
39 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
40
41 #include "SMESH_Mesh_i.hxx"
42 #include "SMESH_Hypothesis_i.hxx"
43 #include "SALOME_Component_i.hxx"
44 #include "SALOME_NamingService.hxx"
45
46 #include "SMESH_Gen.hxx"
47 #include "GEOM_Client.hxx"
48
49 #include <TCollection_AsciiString.hxx>
50 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
51 #include <TColStd_HSequenceOfAsciiString.hxx>
52
53 #include <map>
54 #include <sstream>
55
56 class SMESH_Mesh_i;
57 class SALOME_LifeCycleCORBA;
58
59 // ===========================================================
60 // Study context - stores study-connected objects references
61 // ==========================================================
62 class SMESH_I_EXPORT StudyContext
63 {
64 public:
65   // constructor
66   StudyContext() {}
67   // destructor
68   ~StudyContext() 
69   { 
70     mapIdToIOR.clear();
71     mapIdToId.clear();
72   }
73   // register object in the internal map and return its id
74   int addObject( string theIOR )
75   {
76     int nextId = getNextId();
77     mapIdToIOR[ nextId ]  = theIOR;
78     return nextId;
79   }
80   // find the object id in the internal map by the IOR
81   int findId( string theIOR )
82   {
83     map<int, string>::iterator imap;
84     for ( imap = mapIdToIOR.begin(); imap != mapIdToIOR.end(); ++imap ) {
85       if ( imap->second == theIOR )
86         return imap->first;
87     }
88     return 0;
89   }
90   // get object's IOR by id
91   string getIORbyId( const int theId )
92   {
93     if ( mapIdToIOR.find( theId ) != mapIdToIOR.end() )
94       return mapIdToIOR[ theId ];
95     return string( "" );
96   }
97   // get object's IOR by old id
98   string getIORbyOldId( const int theOldId )
99   {
100     if ( mapIdToId.find( theOldId ) != mapIdToId.end() )
101       return getIORbyId( mapIdToId[ theOldId ] );
102     return string( "" );
103   }
104   // maps old object id to the new one (used when restoring data)
105   void mapOldToNew( const int oldId, const int newId ) {
106     mapIdToId[ oldId ] = newId;
107   }
108   // get old id by a new one
109   int getOldId( const int newId ) {
110     map<int, int>::iterator imap;
111     for ( imap = mapIdToId.begin(); imap != mapIdToId.end(); ++imap ) {
112       if ( imap->second == newId )
113         return imap->first;
114     }
115     return 0;
116   }
117     
118 private:
119   // get next free object identifier
120   int getNextId()
121   {
122     int id = 1;
123     while( mapIdToIOR.find( id ) != mapIdToIOR.end() )
124       id++;
125     return id;
126   }
127
128   map<int, string> mapIdToIOR;      // persistent-to-transient map
129   map<int, int>    mapIdToId;       // used to translate object from persistent to transient form
130 };
131
132 // ===========================================================
133 // SMESH module's engine
134 // ==========================================================
135 class SMESH_I_EXPORT SMESH_Gen_i:
136   public virtual POA_SMESH::SMESH_Gen,
137   public virtual Engines_Component_i 
138 {
139 public:
140   // Get last created instance of the class
141   static SMESH_Gen_i* GetSMESHGen() { return mySMESHGen;}
142   // Get ORB object
143   static CORBA::ORB_var GetORB() { return myOrb;}
144   // Get SMESH module's POA object
145   static PortableServer::POA_var GetPOA() { return myPoa;}
146   // Get Naming Service object
147   static SALOME_NamingService* GetNS();
148   // Get SALOME_LifeCycleCORBA object
149   static SALOME_LifeCycleCORBA* GetLCC();
150   // Retrieve and get GEOM engine reference
151   static GEOM::GEOM_Gen_ptr GetGeomEngine();
152   // Get object of the CORBA reference
153   static PortableServer::ServantBase_var GetServant( CORBA::Object_ptr theObject );
154   // Get CORBA object corresponding to the SALOMEDS::SObject
155   static CORBA::Object_var SObjectToObject( SALOMEDS::SObject_ptr theSObject );
156   // Get the SALOMEDS::SObject corresponding to a CORBA object
157   static SALOMEDS::SObject_ptr ObjectToSObject(SALOMEDS::Study_ptr theStudy,
158                                                CORBA::Object_ptr   theObject);
159   // Get GEOM Object correspoding to TopoDS_Shape
160   GEOM::GEOM_Object_ptr ShapeToGeomObject (const TopoDS_Shape& theShape );
161   // Get TopoDS_Shape correspoding to GEOM_Object
162   TopoDS_Shape GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject);
163   
164   // Default constructor
165   SMESH_Gen_i();
166   // Standard constructor
167   SMESH_Gen_i( CORBA::ORB_ptr            orb,
168                PortableServer::POA_ptr   poa,
169                PortableServer::ObjectId* contId, 
170                const char*               instanceName, 
171                const char*               interfaceName );
172   // Destructor
173   virtual ~SMESH_Gen_i();
174
175   // *****************************************
176   // Interface methods
177   // *****************************************
178   //GEOM::GEOM_Gen_ptr SetGeomEngine( const char* containerLoc );
179   void SetGeomEngine( GEOM::GEOM_Gen_ptr geomcompo );
180
181   // Set current study
182   void SetEmbeddedMode( CORBA::Boolean theMode );
183   // Get current study
184   CORBA::Boolean IsEmbeddedMode();
185
186   // Set current study
187   void SetCurrentStudy( SALOMEDS::Study_ptr theStudy );
188   // Get current study
189   SALOMEDS::Study_ptr GetCurrentStudy();
190
191   // Create hypothesis/algorothm of given type
192   SMESH::SMESH_Hypothesis_ptr CreateHypothesis (const char* theHypType,
193                                                 const char* theLibName)
194     throw ( SALOME::SALOME_Exception );
195   
196   // Return hypothesis of given type holding parameter values of the existing mesh
197   SMESH::SMESH_Hypothesis_ptr GetHypothesisParameterValues (const char*           theHypType,
198                                                             const char*           theLibName,
199                                                             SMESH::SMESH_Mesh_ptr theMesh,
200                                                             GEOM::GEOM_Object_ptr theGeom)
201     throw ( SALOME::SALOME_Exception );
202   
203   // Create empty mesh on a shape
204   SMESH::SMESH_Mesh_ptr CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
205     throw ( SALOME::SALOME_Exception );
206
207   // Create empty mesh
208   SMESH::SMESH_Mesh_ptr CreateEmptyMesh()
209     throw ( SALOME::SALOME_Exception );
210
211   //  Create mesh(es) and import data from UNV file
212   SMESH::SMESH_Mesh_ptr CreateMeshesFromUNV( const char* theFileName )
213     throw ( SALOME::SALOME_Exception );
214
215   //  Create mesh(es) and import data from MED file
216   SMESH::mesh_array* CreateMeshesFromMED( const char* theFileName,
217                                           SMESH::DriverMED_ReadStatus& theStatus )
218     throw ( SALOME::SALOME_Exception );
219
220   //  Create mesh(es) and import data from STL file
221   SMESH::SMESH_Mesh_ptr CreateMeshesFromSTL( const char* theFileName )
222     throw ( SALOME::SALOME_Exception );
223
224   // Compute mesh on a shape
225   CORBA::Boolean Compute( SMESH::SMESH_Mesh_ptr theMesh,
226                           GEOM::GEOM_Object_ptr theShapeObject )
227     throw ( SALOME::SALOME_Exception );
228   /*!
229    * \brief Return errors of mesh computation
230    */
231   SMESH::compute_error_array* GetComputeErrors(SMESH::SMESH_Mesh_ptr theMesh,
232                                                GEOM::GEOM_Object_ptr  theShapeObject )
233     throw ( SALOME::SALOME_Exception );
234
235   // Returns true if mesh contains enough data to be computed
236   CORBA::Boolean IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
237                                    GEOM::GEOM_Object_ptr theShapeObject )
238     throw ( SALOME::SALOME_Exception );
239
240   // Returns errors of hypotheses definintion
241   SMESH::algo_error_array* GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh, 
242                                          GEOM::GEOM_Object_ptr theSubObject )
243       throw ( SALOME::SALOME_Exception );
244
245   // Return mesh elements preventing computation of a subshape
246   SMESH::MeshPreviewStruct* GetBadInputElements( SMESH::SMESH_Mesh_ptr theMesh,
247                                                  CORBA::Short          theSubShapeID )
248     throw ( SALOME::SALOME_Exception );
249
250   // Get sub-shapes unique ID's list
251   SMESH::long_array* GetSubShapesId( GEOM::GEOM_Object_ptr      theMainShapeObject,
252                                      const SMESH::object_array& theListOfSubShape )
253     throw ( SALOME::SALOME_Exception );
254
255   // Return geometrical object the given element is built on. Publish it in study.
256   GEOM::GEOM_Object_ptr GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
257                                                   CORBA::Long            theElementID,
258                                                   const char*            theGeomName)
259     throw ( SALOME::SALOME_Exception );
260
261   // Return geometrical object the given element is built on. Don't publish it in study.
262   GEOM::GEOM_Object_ptr FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
263                                                    CORBA::Long            theElementID)
264     throw ( SALOME::SALOME_Exception );
265
266   // Concatenate the given meshes into one mesh
267   SMESH::SMESH_Mesh_ptr ConcatenateCommon(const SMESH::mesh_array& theMeshesArray, 
268                                           CORBA::Boolean           theUniteIdenticalGroups, 
269                                           CORBA::Boolean           theMergeNodesAndElements, 
270                                           CORBA::Double            theMergeTolerance,
271                                           CORBA::Boolean           theCommonGroups)
272     throw ( SALOME::SALOME_Exception );
273
274   // Concatenate the given meshes into one mesh
275   SMESH::SMESH_Mesh_ptr Concatenate(const SMESH::mesh_array& theMeshesArray, 
276                                     CORBA::Boolean           theUniteIdenticalGroups, 
277                                     CORBA::Boolean           theMergeNodesAndElements, 
278                                     CORBA::Double            theMergeTolerance)
279     throw ( SALOME::SALOME_Exception );
280
281   // Concatenate the given meshes into one mesh
282   // Create the groups of all elements from initial meshes
283   SMESH::SMESH_Mesh_ptr ConcatenateWithGroups(const SMESH::mesh_array& theMeshesArray, 
284                                               CORBA::Boolean           theUniteIdenticalGroups, 
285                                               CORBA::Boolean           theMergeNodesAndElements, 
286                                               CORBA::Double            theMergeTolerance)
287     throw ( SALOME::SALOME_Exception );
288
289   // ****************************************************
290   // Interface inherited methods (from SALOMEDS::Driver)
291   // ****************************************************
292
293   // Save SMESH data
294   SALOMEDS::TMPFile* Save( SALOMEDS::SComponent_ptr theComponent,
295                          const char*              theURL,
296                          bool                     isMultiFile );
297   // Load SMESH data
298   bool Load( SALOMEDS::SComponent_ptr theComponent,
299              const SALOMEDS::TMPFile& theStream,
300              const char*              theURL,
301              bool                     isMultiFile );
302   // Save SMESH data in ASCII format
303   SALOMEDS::TMPFile* SaveASCII( SALOMEDS::SComponent_ptr theComponent,
304                                 const char*              theURL,
305                                 bool                     isMultiFile );
306   // Load SMESH data in ASCII format
307   bool LoadASCII( SALOMEDS::SComponent_ptr theComponent,
308                   const SALOMEDS::TMPFile& theStream,
309                   const char*              theURL,
310                   bool                     isMultiFile );
311
312   // Create filter manager
313   SMESH::FilterManager_ptr CreateFilterManager();
314
315   // Return a pattern mesher
316   SMESH::SMESH_Pattern_ptr GetPattern();
317
318   // Clears study-connected data when it is closed
319   void Close( SALOMEDS::SComponent_ptr theComponent );
320   
321   // Get component data type
322   char* ComponentDataType();
323     
324   // Transform data from transient form to persistent
325   char* IORToLocalPersistentID( SALOMEDS::SObject_ptr theSObject,
326                                 const char*           IORString,
327                                 CORBA::Boolean        isMultiFile,
328                                 CORBA::Boolean        isASCII );
329   // Transform data from persistent form to transient
330   char* LocalPersistentIDToIOR( SALOMEDS::SObject_ptr theSObject,
331                                 const char*           aLocalPersistentID,
332                                 CORBA::Boolean        isMultiFile,
333                                 CORBA::Boolean        isASCII );
334
335   // Returns true if object can be published in the study
336   bool CanPublishInStudy( CORBA::Object_ptr theIOR );
337   // Publish object in the study
338   SALOMEDS::SObject_ptr PublishInStudy( SALOMEDS::Study_ptr   theStudy,
339                                         SALOMEDS::SObject_ptr theSObject,
340                                         CORBA::Object_ptr     theObject,
341                                         const char*           theName ) 
342     throw ( SALOME::SALOME_Exception );
343
344   // Copy-paste methods - returns true if object can be copied to the clipboard
345   CORBA::Boolean CanCopy( SALOMEDS::SObject_ptr theObject ) { return false; }
346   // Copy-paste methods - copy object to the clipboard
347   SALOMEDS::TMPFile* CopyFrom( SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID ) { return false; }
348   // Copy-paste methods - returns true if object can be pasted from the clipboard
349   CORBA::Boolean CanPaste( const char* theComponentName, CORBA::Long theObjectID ) { return false; }
350   // Copy-paste methods - paste object from the clipboard
351   SALOMEDS::SObject_ptr PasteInto( const SALOMEDS::TMPFile& theStream,
352                                    CORBA::Long              theObjectID,
353                                    SALOMEDS::SObject_ptr    theObject ) {
354     SALOMEDS::SObject_var aResultSO;
355     return aResultSO._retn();
356   }
357
358   // ============
359   // Dump python
360   // ============
361
362   virtual Engines::TMPFile* DumpPython(CORBA::Object_ptr theStudy, 
363                                        CORBA::Boolean isPublished, 
364                                        CORBA::Boolean& isValidScript);
365
366   void AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString);
367
368   void RemoveLastFromPythonScript (int theStudyID);
369
370   void SavePython (SALOMEDS::Study_ptr theStudy);
371
372   TCollection_AsciiString DumpPython_impl (SALOMEDS::Study_ptr theStudy, 
373                                            Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
374                                            Resource_DataMapOfAsciiStringAsciiString& theNames,
375                                            bool isPublished, 
376                                            bool& aValidScript,
377                                            const TCollection_AsciiString& theSavedTrace);
378
379   TCollection_AsciiString GetNewPythonLines (int theStudyID);
380
381   void CleanPythonTrace (int theStudyID);
382
383
384   // *****************************************
385   // Internal methods
386   // *****************************************
387 public:
388   // Get shape reader
389   GEOM_Client* GetShapeReader();
390
391   // Tags definition 
392   static long GetHypothesisRootTag();
393   static long GetAlgorithmsRootTag();
394   static long GetRefOnShapeTag();
395   static long GetRefOnAppliedHypothesisTag();
396   static long GetRefOnAppliedAlgorithmsTag();
397   static long GetSubMeshOnVertexTag();
398   static long GetSubMeshOnEdgeTag();
399   static long GetSubMeshOnFaceTag();
400   static long GetSubMeshOnSolidTag();
401   static long GetSubMeshOnCompoundTag();
402   static long GetSubMeshOnWireTag();
403   static long GetSubMeshOnShellTag();
404   static long GetNodeGroupsTag();
405   static long GetEdgeGroupsTag();
406   static long GetFaceGroupsTag();
407   static long GetVolumeGroupsTag();
408
409   // publishing methods
410   SALOMEDS::SComponent_ptr PublishComponent(SALOMEDS::Study_ptr theStudy);
411   SALOMEDS::SObject_ptr PublishMesh (SALOMEDS::Study_ptr   theStudy,
412                                      SMESH::SMESH_Mesh_ptr theMesh,
413                                      const char*           theName = 0);
414   SALOMEDS::SObject_ptr PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
415                                            SMESH::SMESH_Hypothesis_ptr theHyp,
416                                            const char*                 theName = 0);
417   SALOMEDS::SObject_ptr PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
418                                         SMESH::SMESH_Mesh_ptr    theMesh,
419                                         SMESH::SMESH_subMesh_ptr theSubMesh,
420                                         GEOM::GEOM_Object_ptr    theShapeObject,
421                                         const char*              theName = 0);
422   SALOMEDS::SObject_ptr PublishGroup (SALOMEDS::Study_ptr    theStudy,
423                                       SMESH::SMESH_Mesh_ptr  theMesh,
424                                       SMESH::SMESH_GroupBase_ptr theGroup,
425                                       GEOM::GEOM_Object_ptr  theShapeObject,
426                                       const char*            theName = 0);
427   bool AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
428                             SMESH::SMESH_Mesh_ptr       theMesh,
429                             GEOM::GEOM_Object_ptr       theShapeObject,
430                             SMESH::SMESH_Hypothesis_ptr theHyp);
431   bool RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
432                                  SMESH::SMESH_Mesh_ptr       theMesh,
433                                  GEOM::GEOM_Object_ptr       theShapeObject,
434                                  SMESH::SMESH_Hypothesis_ptr theHyp);
435   SALOMEDS::SObject_ptr GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
436                                                  SMESH::SMESH_Mesh_ptr theMesh,
437                                                  GEOM::GEOM_Object_ptr theShape);
438   static void SetName(SALOMEDS::SObject_ptr theSObject,
439                       const char*           theName,
440                       const char*           theDefaultName = 0);
441
442   //  Get study context
443   StudyContext* GetCurrentStudyContext();
444
445   // Register an object in a StudyContext; return object id
446   int RegisterObject(CORBA::Object_ptr theObject);
447
448   // Return id of registered object
449   CORBA::Long GetObjectId(CORBA::Object_ptr theObject);
450
451   // Return an object that previously had an oldID
452   template<class TInterface> 
453   typename TInterface::_var_type GetObjectByOldId( const int oldID )
454   {
455     if ( StudyContext* myStudyContext = GetCurrentStudyContext() ) {
456       string ior = myStudyContext->getIORbyOldId( oldID );
457       if ( !ior.empty() )
458         return TInterface::_narrow(GetORB()->string_to_object( ior.c_str() ));
459     }
460     return TInterface::_nil();
461   }
462
463   // Get current study ID
464   int GetCurrentStudyID()
465   { return myCurrentStudy->_is_nil() ? -1 : myCurrentStudy->StudyId(); }
466
467   /*!
468    * \brief Find SObject for an algo
469    */
470   SALOMEDS::SObject_ptr GetAlgoSO(const ::SMESH_Algo* algo);
471  
472 private:
473   // Create hypothesis of given type
474   SMESH::SMESH_Hypothesis_ptr createHypothesis( const char* theHypName,
475                                                 const char* theLibName)
476     throw ( SALOME::SALOME_Exception );
477   
478   // Create empty mesh on shape
479   SMESH::SMESH_Mesh_ptr createMesh()
480     throw ( SALOME::SALOME_Exception );
481
482   static void loadGeomData( SALOMEDS::SComponent_ptr theCompRoot );
483   
484 private:
485   static GEOM::GEOM_Gen_var      myGeomGen;
486   static CORBA::ORB_var          myOrb;         // ORB reference
487   static PortableServer::POA_var myPoa;         // POA reference
488   static SALOME_NamingService*   myNS;          // Naming Service
489   static SALOME_LifeCycleCORBA*  myLCC;         // Life Cycle CORBA
490   static SMESH_Gen_i*            mySMESHGen;    // Point to last created instance of the class
491   ::SMESH_Gen               myGen;              // SMESH_Gen local implementation
492
493   // hypotheses managing
494   map<string, GenericHypothesisCreator_i*> myHypCreatorMap;
495
496   map<int, StudyContext*>   myStudyContextMap;  // Map of study context objects
497
498   GEOM_Client*              myShapeReader;      // Shape reader
499   SALOMEDS::Study_var       myCurrentStudy;     // Current study
500   CORBA::Boolean            myIsEmbeddedMode;   // Current mode
501
502   // Dump Python: trace of API methods calls
503   std::map < int, Handle(TColStd_HSequenceOfAsciiString) > myPythonScripts;
504 };
505
506
507 namespace SMESH
508 {
509   template<class T>
510   T
511   DownCast(CORBA::Object_ptr theArg)
512   {
513     return dynamic_cast<T>(SMESH_Gen_i::GetServant(theArg).in());
514   }
515 }
516
517
518 #endif