]> SALOME platform Git repositories - modules/hexablock.git/blob - src/HEXABLOCK_I/HEXABLOCK.hxx
Salome HOME
Merge from V6_main 01/04/2013
[modules/hexablock.git] / src / HEXABLOCK_I / HEXABLOCK.hxx
1 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifndef _HEXABLOCK_HXX_
20 #define _HEXABLOCK_HXX_
21
22 #include <SALOMEconfig.h>
23 #include CORBA_SERVER_HEADER(HEXABLOCK_Gen)
24 #include "SALOME_Component_i.hxx"
25 //  #include "SALOMEDSImpl_TMPFile.hxx"
26 //  #include "SALOMEDSImpl_SComponent.hxx"
27
28
29
30 #include <sstream>
31
32
33 #include <TopoDS_Shape.hxx>
34 #include "GEOM_Client.hxx"
35
36 #include "hexa_base.hxx"
37 #include "Hex.hxx"
38
39 using namespace HEXABLOCK_ORB;
40
41 class SALOME_LifeCycleCORBA;
42 class SALOME_NamingService;
43
44
45
46
47
48 // ===========================================================
49 // Study context - stores study-connected objects references
50 // ==========================================================
51 class StudyContext
52 {
53 public:
54   // constructor
55   StudyContext() {}
56   // destructor
57   ~StudyContext()
58   {
59     mapIdToIOR.clear();
60     mapIdToId.clear();
61   }
62   // register object in the internal map and return its id
63   int addObject( string theIOR )
64   {
65     int nextId = getNextId();
66     mapIdToIOR[ nextId ]  = theIOR;
67     return nextId;
68   }
69   // find the object id in the internal map by the IOR
70   int findId( string theIOR )
71   {
72     map<int, string>::iterator imap;
73     for ( imap = mapIdToIOR.begin(); imap != mapIdToIOR.end(); ++imap ) {
74       if ( imap->second == theIOR )
75         return imap->first;
76     }
77     return 0;
78   }
79   // get object's IOR by id
80   string getIORbyId( const int theId )
81   {
82     if ( mapIdToIOR.find( theId ) != mapIdToIOR.end() )
83       return mapIdToIOR[ theId ];
84     return string( "" );
85   }
86   // get object's IOR by old id
87   string getIORbyOldId( const int theOldId )
88   {
89     if ( mapIdToId.find( theOldId ) != mapIdToId.end() )
90       return getIORbyId( mapIdToId[ theOldId ] );
91     return string( "" );
92   }
93   // maps old object id to the new one (used when restoring data)
94   void mapOldToNew( const int oldId, const int newId ) {
95     mapIdToId[ oldId ] = newId;
96   }
97   // get old id by a new one
98   int getOldId( const int newId ) {
99     map<int, int>::iterator imap;
100     for ( imap = mapIdToId.begin(); imap != mapIdToId.end(); ++imap ) {
101       if ( imap->second == newId )
102         return imap->first;
103     }
104     return 0;
105   }
106
107 private:
108   // get next free object identifier
109   int getNextId()
110   {
111     int id = 1;
112     while( mapIdToIOR.find( id ) != mapIdToIOR.end() )
113       id++;
114     return id;
115   }
116
117   map<int, string> mapIdToIOR;      // persistent-to-transient map
118   map<int, int>    mapIdToId;       // used to translate object from persistent to transient form
119 };
120
121
122
123 class HEXABLOCK_Gen_i: public POA_HEXABLOCK_ORB::HEXABLOCK_Gen,
124                        public Engines_Component_i 
125 {
126 public:
127     HEXABLOCK_Gen_i( CORBA::ORB_ptr orb,
128             PortableServer::POA_ptr poa,
129             PortableServer::ObjectId * contId, 
130             const char *instanceName, 
131             const char *interfaceName);
132   // Get object of the CORBA reference
133     static CORBA::ORB_var GetORB() { return _orb;}
134     static PortableServer::POA_var GetPOA() { return _poa;}
135     static PortableServer::ServantBase_var GetServant( CORBA::Object_ptr theObject );
136
137     virtual ~HEXABLOCK_Gen_i();
138
139     void test();
140     ::CORBA::Long countDocument();
141     Document_ptr getDocument(::CORBA::Long i);
142     void removeDocument(Document_ptr d); //CS_TODO
143     Document_ptr addDocument  (const char* name);
144     Document_ptr findDocument (const char* name);
145     Document_ptr loadDocument (const char* xmlFilename);
146
147     GEOM_Client* getGeomClient();
148     TopoDS_Shape geomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject);
149 //     GEOM::GEOM_Object_ptr shapeToGeomObject(const TopoDS_Shape& theShape );
150     GEOM::GEOM_Object_ptr brepToGeomObject(const std::string& theBrep);
151     static SALOME_LifeCycleCORBA*  GetLCC();
152     static SALOME_NamingService* GetNS();
153     static HEXABLOCK_Gen_i* GetHEXABLOCKGen() { return _HEXABLOCKGen;}
154
155
156     virtual Engines::TMPFile* DumpPython(CORBA::Object_ptr theStudy,
157                                          CORBA::Boolean isPublished,
158                                          CORBA::Boolean isMultiFile,
159                                          CORBA::Boolean& isValidScript);
160
161   void SetCurrentStudy( SALOMEDS::Study_ptr theStudy );
162   int GetCurrentStudyID();
163   StudyContext* GetCurrentStudyContext();
164   std::string /*void */SetName( SALOMEDS::SObject_ptr theSObject,
165                 const char*           theName,
166                 const char*           theDefaultName );
167   int   RegisterObject(CORBA::Object_ptr theObject);
168 //   SMESH::SMESH_Mesh_ptr SMESH_Gen_i::createMesh() throw ( SALOME::SALOME_Exception )
169   char* ComponentDataType();
170   
171
172   bool CanPublishInStudy(CORBA::Object_ptr theIOR);
173   SALOMEDS::SComponent_ptr PublishComponent(SALOMEDS::Study_ptr theStudy);
174   static SALOMEDS::SObject_ptr ObjectToSObject(SALOMEDS::Study_ptr theStudy, CORBA::Object_ptr   theObject);
175   
176   SALOMEDS::SObject_ptr PublishDoc( SALOMEDS::Study_ptr   theStudy,
177                                     Document_ptr          theDoc,
178                                     const char*           theName = 0);
179
180
181
182   Document_ptr createDoc        (const char* name) 
183                throw ( SALOME::SALOME_Exception );
184   Document_ptr createDocInStudy (const char* name)
185                throw ( SALOME::SALOME_Exception );
186   CORBA::Long  addDocInStudy    (Document_ptr doc)
187                throw ( SALOME::SALOME_Exception );
188
189   //-----------------------------------------------------------------------//
190   // Inherited methods from SALOMEDS::Driver                               //
191   //-----------------------------------------------------------------------//
192
193 public :
194   SALOMEDS::TMPFile* Save(SALOMEDS::SComponent_ptr theComponent,
195                           const char* theURL,
196                           bool isMultiFile);
197
198   SALOMEDS::TMPFile* SaveASCII(SALOMEDS::SComponent_ptr theComponent,
199                                const char* theURL,
200                                bool isMultiFile);
201
202   CORBA::Boolean Load(SALOMEDS::SComponent_ptr theComponent,
203                       const SALOMEDS::TMPFile& theStream,
204                       const char* theURL,
205                       bool isMultiFile);
206
207   CORBA::Boolean LoadASCII(SALOMEDS::SComponent_ptr theComponent,
208                            const SALOMEDS::TMPFile& theStream,
209                            const char* theURL,
210                            bool isMultiFile);
211
212   void Close(SALOMEDS::SComponent_ptr theComponent);
213   // char* ComponentDataType();  Deja la 
214
215   CORBA::Boolean CanCopy  (SALOMEDS::SObject_ptr theObject);
216   CORBA::Boolean CanPaste (const char* theComponentName, 
217                            CORBA::Long theObjectID);
218
219   SALOMEDS::TMPFile* CopyFrom(SALOMEDS::SObject_ptr theObject, 
220                               CORBA::Long& theObjectID);
221
222   SALOMEDS::SObject_ptr PasteInto(const SALOMEDS::TMPFile& theStream,
223                                   CORBA::Long theObjectID,
224                                   SALOMEDS::SObject_ptr theObject);
225   SALOMEDS::SObject_ptr PublishInStudy(SALOMEDS::Study_ptr theStudy,
226                                        SALOMEDS::SObject_ptr theSObject,
227                                        CORBA::Object_ptr theObject,
228                                        const char* theName) throw (SALOME::SALOME_Exception) ;
229
230
231   char* IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
232                                const char* IORString,
233                                CORBA::Boolean isMultiFile,
234                                CORBA::Boolean isASCII);
235   char* LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
236                                const char* aLocalPersistentID,
237                                CORBA::Boolean isMultiFile,
238                                CORBA::Boolean isASCII);
239
240   char* getVersion();
241
242 #if 0
243   virtual std::string GetIOR();
244
245   /*******************
246   virtual SALOMEDSImpl_TMPFile* Save(const SALOMEDSImpl_SComponent& compo,
247                                      const std::string& theURL,
248                                      long& theStreamLength,
249                                      bool isMultiFile);
250
251   virtual SALOMEDSImpl_TMPFile* SaveASCII(const SALOMEDSImpl_SComponent& compo,
252                                           const std::string& theURL,
253                                           long& theStreamLength,
254                                           bool isMultiFile);
255   *********************/
256   
257   virtual SALOMEDS::TMPFile* Save(SALOMEDS::SComponent_ptr theComponent,
258                                   const char* theURL,
259                                   bool isMultiFile);
260
261
262   virtual SALOMEDS::TMPFile* SaveASCII(SALOMEDS::SComponent_ptr theComponent,
263                                const char* theURL,
264                                bool isMultiFile);
265
266
267   
268   virtual bool Load(const SALOMEDSImpl_SComponent& compo,
269                     const unsigned char* theStream,
270                     const long theStreamLength,
271                     const std::string& theURL,
272                     bool isMultiFile);
273
274   virtual bool LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
275                          const unsigned char* theStream,
276                          const long theStreamLength,
277                          const std::string& theURL,
278                          bool isMultiFile);
279
280   virtual void Close(const SALOMEDSImpl_SComponent& theComponent);
281  
282   // virtual std::string ComponentDataType();
283
284   virtual std::string IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
285                                              const std::string& IORString,
286                                              bool isMultiFile,
287                                              bool isASCII);
288
289   virtual std::string LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theSObject,
290                                              const std::string& aLocalPersistentID,
291                                              bool isMultiFile,
292                                              bool isASCII);
293
294   virtual bool CanCopy(const SALOMEDSImpl_SObject& theObject);
295
296   virtual SALOMEDSImpl_TMPFile* CopyFrom(const SALOMEDSImpl_SObject& theObject, 
297                                          int& theObjectID,
298                                          long& theStreamLength);
299   
300   virtual bool CanPaste(const std::string& theComponentName, int theObjectID);
301
302   virtual std::string PasteInto(const unsigned char* theStream,
303                                 const long theStreamLength,
304                                 int theObjectID,
305                                 const SALOMEDSImpl_SObject& theObject);
306
307   virtual SALOMEDSImpl_TMPFile* DumpPython(SALOMEDSImpl_Study* theStudy, 
308                                            bool isPublished, 
309                                            bool isMultiFile,
310                                            bool& isValidScript,
311                                            long& theStreamLength);
312 #endif
313 private :
314    HEXA_NS::Hex* _engine_cpp;
315    GEOM_Client*  _geomClient;
316
317
318   static PortableServer::POA_var _poa;// POA reference
319   static GEOM::GEOM_Gen_var      _geomGen;
320   static SALOME_LifeCycleCORBA*  _lcc;
321   static CORBA::ORB_var          _orb;
322   static SALOME_NamingService*   _ns;
323   static HEXABLOCK_Gen_i*        _HEXABLOCKGen;
324
325
326   GEOM::GEOM_Object_ptr _makeVertex( const TopoDS_Shape& theShape );
327   GEOM::GEOM_Object_ptr _makeEdge( const TopoDS_Shape& theShape );
328
329   map<int, StudyContext*>   myStudyContextMap;  // Map of study context objects
330   SALOMEDS::Study_var       myCurrentStudy;     // Current study
331
332   bool   first_document;
333
334 };
335
336 extern "C"
337     PortableServer::ObjectId * HEXABLOCKEngine_factory(
338       CORBA::ORB_ptr orb,
339       PortableServer::POA_ptr poa,
340       PortableServer::ObjectId * contId,
341       const char *instanceName,
342       const char *interfaceName
343     );
344
345
346 template<class T>
347   T
348   DownCast(CORBA::Object_ptr theArg)
349   {
350     return dynamic_cast<T>(HEXABLOCK_Gen_i::GetServant(theArg).in());
351   }
352
353
354
355
356
357 TopoDS_Shape string2shape( const string& brep );
358
359
360 string shape2string( const TopoDS_Shape& aShape );
361
362
363
364
365 #endif
366
367
368 // 1) Séquence de code convertissant une chaîne au format BREP vers un TopoDS_Shape
369 // 
370 // TopoDS_Shape STRING_TO_SHAPE( char* buf, int sizebuf )
371 // {
372 //   TopoDS_Shape shape;
373 //   std::istrstream streamBrep(buf,sizebuf);
374 //   BRep_Builder aBuilder;
375 //   BRepTools::Read(shape, streamBrep, aBuilder);
376 //   return(shape);
377 // }
378 // 
379 // 2) Séquence de code convertissant un TopoDS_Shape en une chaîne au format BREP
380 // 
381 // char* SHAPE_TO_STRING( TopoDS_Shape aShape, int& sizebuf )
382 // {
383 //  ostrstream streamShape;
384 //  BRepTools::Write(aShape, streamShape);
385 //  sizebuf = streamShape.pcount();
386 //  char* buf = new char [sizebuf];
387 //  char* valueOfStream = streamShape.str();
388 //  memcpy(buf, valueOfStream, size);
389 //  return buf;
390 // }
391 // 
392 // 3) Séquence de code accédant aux coordonnées d'un TopoDS_Shape qui est un Vertex
393 // 
394 // bool SHAPE_TO_COORD(TopoDS_Shape aShape, double& x, double& y, double& z)
395 // {
396 //   if ( aShape.ShapeType() == TopAbs_VERTEX ) {
397 //       TopoDS_Vertex aPoint;
398 //        aPoint = TopoDS::Vertex( aShape );
399 //       gp_Pnt aPnt = BRep_Tool::Pnt( aPoint );
400 //       x = aPnt.X();
401 //       y = aPnt.Y();
402 //       z = aPnt.Z();
403 //        return(1);
404 //    } else {
405 //        return(0);
406 //   };
407 // }
408 // 
409 // 4) Séquence de code créant un TopoDS_Shape à partir de coordonnées
410 // 
411 // TopoDS_Shape COORD_TO_SHAPE(double x, double y, double z)
412 // {
413 //       gp_Pnt aPnt(x, y, z);
414 //       BRepBuilderAPI_MakeVertex mkVertex (aPnt);
415 //      TopoDS_Vertex aVertex = TopoDS::Vertex(mkVertex.Shape());
416 //       return aVertex;
417 // }