Salome HOME
a1ac46b7889267b965de3c6ee3dd20abf413580a
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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     
110 private:
111   // get next free object identifier
112   int getNextId()
113   {
114     int id = 1;
115     while( mapIdToIOR.find( id ) != mapIdToIOR.end() )
116       id++;
117     return id;
118   }
119
120   map<int, string> mapIdToIOR;      // persistent-to-transient map
121   map<int, int>    mapIdToId;       // used to translate object from persistent to transient form
122 };
123
124 // ===========================================================
125 // SMESH module's engine
126 // ==========================================================
127 class SMESH_I_EXPORT SMESH_Gen_i:
128   public virtual POA_SMESH::SMESH_Gen,
129   public virtual Engines_Component_i 
130 {
131 public:
132   // Get last created instance of the class
133   static SMESH_Gen_i* GetSMESHGen() { return mySMESHGen;}
134   // Get ORB object
135   static CORBA::ORB_var GetORB() { return myOrb;}
136   // Get SMESH module's POA object
137   static PortableServer::POA_var GetPOA() { return myPoa;}
138   // Get Naming Service object
139   static SALOME_NamingService* GetNS();
140   // Get SALOME_LifeCycleCORBA object
141   static SALOME_LifeCycleCORBA* GetLCC();
142   // Retrieve and get GEOM engine reference
143   static GEOM::GEOM_Gen_ptr GetGeomEngine();
144   // Get object of the CORBA reference
145   static PortableServer::ServantBase_var GetServant( CORBA::Object_ptr theObject );
146   // Get CORBA object corresponding to the SALOMEDS::SObject
147   static CORBA::Object_var SObjectToObject( SALOMEDS::SObject_ptr theSObject );
148   // Get the SALOMEDS::SObject corresponding to a CORBA object
149   static SALOMEDS::SObject_ptr ObjectToSObject(SALOMEDS::Study_ptr theStudy,
150                                                CORBA::Object_ptr   theObject);
151   // Get GEOM Object correspoding to TopoDS_Shape
152   GEOM::GEOM_Object_ptr ShapeToGeomObject (const TopoDS_Shape& theShape );
153   // Get TopoDS_Shape correspoding to GEOM_Object
154   TopoDS_Shape GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject);
155   
156   // Default constructor
157   SMESH_Gen_i();
158   // Standard constructor
159   SMESH_Gen_i( CORBA::ORB_ptr            orb,
160                PortableServer::POA_ptr   poa,
161                PortableServer::ObjectId* contId, 
162                const char*               instanceName, 
163                const char*               interfaceName );
164   // Destructor
165   virtual ~SMESH_Gen_i();
166   
167   // *****************************************
168   // Interface methods
169   // *****************************************
170
171   // Set current study
172   void SetCurrentStudy( SALOMEDS::Study_ptr theStudy );
173   // Get current study
174   SALOMEDS::Study_ptr GetCurrentStudy();
175
176   // Create hypothesis/algorothm of given type
177   SMESH::SMESH_Hypothesis_ptr CreateHypothesis (const char* theHypType,
178                                                 const char* theLibName)
179     throw ( SALOME::SALOME_Exception );
180   
181   // Return hypothesis of given type holding parameter values of the existing mesh
182   SMESH::SMESH_Hypothesis_ptr GetHypothesisParameterValues (const char*           theHypType,
183                                                             const char*           theLibName,
184                                                             SMESH::SMESH_Mesh_ptr theMesh,
185                                                             GEOM::GEOM_Object_ptr theGeom)
186     throw ( SALOME::SALOME_Exception );
187   
188   // Create empty mesh on a shape
189   SMESH::SMESH_Mesh_ptr CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
190     throw ( SALOME::SALOME_Exception );
191
192   //  Create mesh(es) and import data from UNV file
193   SMESH::SMESH_Mesh_ptr CreateMeshesFromUNV( const char* theFileName )
194     throw ( SALOME::SALOME_Exception );
195
196   //  Create mesh(es) and import data from MED file
197   SMESH::mesh_array* CreateMeshesFromMED( const char* theFileName,
198                                           SMESH::DriverMED_ReadStatus& theStatus )
199     throw ( SALOME::SALOME_Exception );
200
201   //  Create mesh(es) and import data from STL file
202   SMESH::SMESH_Mesh_ptr CreateMeshesFromSTL( const char* theFileName )
203     throw ( SALOME::SALOME_Exception );
204
205   // Compute mesh on a shape
206   CORBA::Boolean Compute( SMESH::SMESH_Mesh_ptr theMesh,
207                           GEOM::GEOM_Object_ptr  theShapeObject )
208     throw ( SALOME::SALOME_Exception );
209
210   // Returns true if mesh contains enough data to be computed
211   CORBA::Boolean IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
212                                    GEOM::GEOM_Object_ptr theShapeObject )
213     throw ( SALOME::SALOME_Exception );
214
215   // Returns errors of hypotheses definintion
216   SMESH::algo_error_array* GetAlgoState( SMESH::SMESH_Mesh_ptr theMesh, 
217                                          GEOM::GEOM_Object_ptr theSubObject )
218       throw ( SALOME::SALOME_Exception );
219
220   // Get sub-shapes unique ID's list
221   SMESH::long_array* GetSubShapesId( GEOM::GEOM_Object_ptr      theMainShapeObject,
222                                      const SMESH::object_array& theListOfSubShape )
223     throw ( SALOME::SALOME_Exception );
224
225   // Return geometrical object the given element is built on
226   GEOM::GEOM_Object_ptr GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
227                                                   CORBA::Long            theElementID,
228                                                   const char*            theGeomName)
229     throw ( SALOME::SALOME_Exception );
230
231   // ****************************************************
232   // Interface inherited methods (from SALOMEDS::Driver)
233   // ****************************************************
234
235   // Save SMESH data
236   SALOMEDS::TMPFile* Save( SALOMEDS::SComponent_ptr theComponent,
237                          const char*              theURL,
238                          bool                     isMultiFile );
239   // Load SMESH data
240   bool Load( SALOMEDS::SComponent_ptr theComponent,
241              const SALOMEDS::TMPFile& theStream,
242              const char*              theURL,
243              bool                     isMultiFile );
244   // Save SMESH data in ASCII format
245   SALOMEDS::TMPFile* SaveASCII( SALOMEDS::SComponent_ptr theComponent,
246                                 const char*              theURL,
247                                 bool                     isMultiFile );
248   // Load SMESH data in ASCII format
249   bool LoadASCII( SALOMEDS::SComponent_ptr theComponent,
250                   const SALOMEDS::TMPFile& theStream,
251                   const char*              theURL,
252                   bool                     isMultiFile );
253
254   // Create filter manager
255   SMESH::FilterManager_ptr CreateFilterManager();
256
257   // Return a pattern mesher
258   SMESH::SMESH_Pattern_ptr GetPattern();
259
260   // Clears study-connected data when it is closed
261   void Close( SALOMEDS::SComponent_ptr theComponent );
262   
263   // Get component data type
264   char* ComponentDataType();
265     
266   // Transform data from transient form to persistent
267   char* IORToLocalPersistentID( SALOMEDS::SObject_ptr theSObject,
268                                 const char*           IORString,
269                                 CORBA::Boolean        isMultiFile,
270                                 CORBA::Boolean        isASCII );
271   // Transform data from persistent form to transient
272   char* LocalPersistentIDToIOR( SALOMEDS::SObject_ptr theSObject,
273                                 const char*           aLocalPersistentID,
274                                 CORBA::Boolean        isMultiFile,
275                                 CORBA::Boolean        isASCII );
276
277   // Returns true if object can be published in the study
278   bool CanPublishInStudy( CORBA::Object_ptr theIOR );
279   // Publish object in the study
280   SALOMEDS::SObject_ptr PublishInStudy( SALOMEDS::Study_ptr   theStudy,
281                                         SALOMEDS::SObject_ptr theSObject,
282                                         CORBA::Object_ptr     theObject,
283                                         const char*           theName ) 
284     throw ( SALOME::SALOME_Exception );
285
286   // Copy-paste methods - returns true if object can be copied to the clipboard
287   CORBA::Boolean CanCopy( SALOMEDS::SObject_ptr theObject ) { return false; }
288   // Copy-paste methods - copy object to the clipboard
289   SALOMEDS::TMPFile* CopyFrom( SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID ) { return false; }
290   // Copy-paste methods - returns true if object can be pasted from the clipboard
291   CORBA::Boolean CanPaste( const char* theComponentName, CORBA::Long theObjectID ) { return false; }
292   // Copy-paste methods - paste object from the clipboard
293   SALOMEDS::SObject_ptr PasteInto( const SALOMEDS::TMPFile& theStream,
294                                    CORBA::Long              theObjectID,
295                                    SALOMEDS::SObject_ptr    theObject ) {
296     SALOMEDS::SObject_var aResultSO;
297     return aResultSO._retn();
298   }
299
300   // ============
301   // Dump python
302   // ============
303
304   virtual Engines::TMPFile* DumpPython(CORBA::Object_ptr theStudy, 
305                                        CORBA::Boolean isPublished, 
306                                        CORBA::Boolean& isValidScript);
307
308   void AddToPythonScript (int theStudyID, const TCollection_AsciiString& theString);
309
310   void RemoveLastFromPythonScript (int theStudyID);
311
312   void SavePython (SALOMEDS::Study_ptr theStudy);
313
314   TCollection_AsciiString DumpPython_impl (int theStudyID, 
315                                            Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
316                                            Resource_DataMapOfAsciiStringAsciiString& theNames,
317                                            bool isPublished, 
318                                            bool& aValidScript,
319                                            const TCollection_AsciiString& theSavedTrace);
320
321   TCollection_AsciiString GetNewPythonLines (int theStudyID);
322
323   void CleanPythonTrace (int theStudyID);
324
325
326   // *****************************************
327   // Internal methods
328   // *****************************************
329 public:
330   // Get shape reader
331   GEOM_Client* GetShapeReader();
332
333   // Tags definition 
334   static long GetHypothesisRootTag();
335   static long GetAlgorithmsRootTag();
336   static long GetRefOnShapeTag();
337   static long GetRefOnAppliedHypothesisTag();
338   static long GetRefOnAppliedAlgorithmsTag();
339   static long GetSubMeshOnVertexTag();
340   static long GetSubMeshOnEdgeTag();
341   static long GetSubMeshOnFaceTag();
342   static long GetSubMeshOnSolidTag();
343   static long GetSubMeshOnCompoundTag();
344   static long GetSubMeshOnWireTag();
345   static long GetSubMeshOnShellTag();
346   static long GetNodeGroupsTag();
347   static long GetEdgeGroupsTag();
348   static long GetFaceGroupsTag();
349   static long GetVolumeGroupsTag();
350
351   // publishing methods
352   SALOMEDS::SComponent_ptr PublishComponent(SALOMEDS::Study_ptr theStudy);
353   SALOMEDS::SObject_ptr PublishMesh (SALOMEDS::Study_ptr   theStudy,
354                                      SMESH::SMESH_Mesh_ptr theMesh,
355                                      const char*           theName = 0);
356   SALOMEDS::SObject_ptr PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
357                                            SMESH::SMESH_Hypothesis_ptr theHyp,
358                                            const char*                 theName = 0);
359   SALOMEDS::SObject_ptr PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
360                                         SMESH::SMESH_Mesh_ptr    theMesh,
361                                         SMESH::SMESH_subMesh_ptr theSubMesh,
362                                         GEOM::GEOM_Object_ptr    theShapeObject,
363                                         const char*              theName = 0);
364   SALOMEDS::SObject_ptr PublishGroup (SALOMEDS::Study_ptr    theStudy,
365                                       SMESH::SMESH_Mesh_ptr  theMesh,
366                                       SMESH::SMESH_GroupBase_ptr theGroup,
367                                       GEOM::GEOM_Object_ptr  theShapeObject,
368                                       const char*            theName = 0);
369   bool AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
370                             SMESH::SMESH_Mesh_ptr       theMesh,
371                             GEOM::GEOM_Object_ptr       theShapeObject,
372                             SMESH::SMESH_Hypothesis_ptr theHyp);
373   bool RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
374                                  SMESH::SMESH_Mesh_ptr       theMesh,
375                                  GEOM::GEOM_Object_ptr       theShapeObject,
376                                  SMESH::SMESH_Hypothesis_ptr theHyp);
377   SALOMEDS::SObject_ptr GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
378                                                  SMESH::SMESH_Mesh_ptr theMesh,
379                                                  GEOM::GEOM_Object_ptr theShape);
380   static void SetName(SALOMEDS::SObject_ptr theSObject,
381                       const char*           theName,
382                       const char*           theDefaultName = 0);
383
384   //  Get study context
385   StudyContext* GetCurrentStudyContext();
386
387   // Register an object in a StudyContext; return object id
388   int RegisterObject(CORBA::Object_ptr theObject);
389
390   // Get current study ID
391   int GetCurrentStudyID()
392   { return myCurrentStudy->_is_nil() ? -1 : myCurrentStudy->StudyId(); }
393  
394 private:
395   // Create hypothesis of given type
396   SMESH::SMESH_Hypothesis_ptr createHypothesis( const char* theHypName,
397                                                 const char* theLibName)
398     throw ( SALOME::SALOME_Exception );
399   
400   // Create empty mesh on shape
401   SMESH::SMESH_Mesh_ptr createMesh()
402     throw ( SALOME::SALOME_Exception );
403
404   static void loadGeomData( SALOMEDS::SComponent_ptr theCompRoot );
405   
406 private:
407
408   static CORBA::ORB_var          myOrb;         // ORB reference
409   static PortableServer::POA_var myPoa;         // POA reference
410   static SALOME_NamingService*   myNS;          // Naming Service
411   static SALOME_LifeCycleCORBA*  myLCC;         // Life Cycle CORBA
412   static SMESH_Gen_i*            mySMESHGen;    // Point to last created instance of the class
413   ::SMESH_Gen               myGen;              // SMESH_Gen local implementation
414
415   // hypotheses managing
416   map<string, GenericHypothesisCreator_i*> myHypCreatorMap;
417
418   map<int, StudyContext*>   myStudyContextMap;  // Map of study context objects
419
420   GEOM_Client*              myShapeReader;      // Shape reader
421   SALOMEDS::Study_var       myCurrentStudy;     // Current study
422
423   // Dump Python: trace of API methods calls
424   std::map < int, Handle(TColStd_HSequenceOfAsciiString) > myPythonScripts;
425 };
426
427
428 namespace SMESH
429 {
430   template<class T>
431   T
432   DownCast(CORBA::Object_ptr theArg)
433   {
434     return dynamic_cast<T>(SMESH_Gen_i::GetServant(theArg).in());
435   }
436 }
437
438
439 #endif