Salome HOME
First publish of HEXABLOCKcomponant
[modules/hexablock.git] / src / HEXABLOCK_I / HEXABLOCK.cxx
1 using namespace std;
2
3 #include <TCollection_AsciiString.hxx>
4 #include <TopAbs_ShapeEnum.hxx>
5 #include <TopoDS.hxx>
6 #include <TopoDS_Vertex.hxx>
7 #include <TopoDS_Edge.hxx>
8 #include <TopoDS_Face.hxx>
9
10
11 #include <Geom_Curve.hxx>
12
13 #include <gp_Pnt.hxx>
14 #include <BRep_Builder.hxx>
15 #include <BRep_Tool.hxx>
16 #include <BRepTools.hxx>
17
18 #include "SALOME_NamingService.hxx"
19 #include "SALOME_LifeCycleCORBA.hxx"
20 #include "Utils_SINGLETON.hxx"
21
22 #include "HexElements_grid.cxx"
23
24
25 #include "HEXABLOCK.hxx"
26 #include "utilities.h"
27
28 #include "HexDocument_impl.hxx"
29 #include <string>
30
31 using namespace HEXABLOCK_ORB;
32
33
34 // Static variables definition
35 PortableServer::POA_var HEXABLOCK::_poa;
36 HEXABLOCK*                   HEXABLOCK::_HEXABLOCKGen = NULL;
37 SALOME_LifeCycleCORBA*  HEXABLOCK::_lcc = NULL;
38 GEOM::GEOM_Gen_var      HEXABLOCK::_geomGen = GEOM::GEOM_Gen::_nil();
39 // GEOM_Client*            HEXABLOCK::_geomClient = NULL;
40 CORBA::ORB_var          HEXABLOCK::_orb;
41 SALOME_NamingService*   HEXABLOCK::_ns = NULL;
42
43 //=============================================================================
44 /*!
45  *  standard constructor
46  */
47 //=============================================================================
48
49 HEXABLOCK::HEXABLOCK(CORBA::ORB_ptr orb,
50         PortableServer::POA_ptr poa,
51         PortableServer::ObjectId * contId, 
52         const char *instanceName, 
53         const char *interfaceName) :
54   Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
55 {
56   MESSAGE("activate object");
57   _thisObj = this ;
58   _poa = PortableServer::POA::_duplicate(poa);
59   _orb = CORBA::ORB::_duplicate(orb);
60   _id = _poa->activate_object(_thisObj);
61   _engine_cpp = new HEXA_NS::Hex;
62   _geomClient = NULL;
63   _HEXABLOCKGen = this;
64   MESSAGE("ALRRRRRIGHTTTTT");
65 }
66
67 //=============================================================================
68 /*!
69  *  GetServant [ static ]
70  *
71  *  Get servant of the CORBA object
72  */
73 //=============================================================================
74
75 PortableServer::ServantBase_var HEXABLOCK::GetServant( CORBA::Object_ptr theObject )
76 {
77   if( CORBA::is_nil( theObject ) || CORBA::is_nil( GetPOA() ) )
78     return NULL;
79   try {
80     PortableServer::Servant aServant = GetPOA()->reference_to_servant( theObject );
81     return aServant;
82   } 
83   catch (...) {
84     INFOS( "GetServant - Unknown exception was caught!!!" ); 
85     return NULL;
86   }
87 }
88
89
90
91 //=============================================================================
92 //  destructor
93 //=============================================================================
94 HEXABLOCK::~HEXABLOCK()
95 {
96 }
97
98
99 void HEXABLOCK::test()
100 {
101   MESSAGE("HEEEEEEEEEEEEEEYYYYYYYYYYY");
102 }
103
104 CORBA::Long HEXABLOCK::countDocument()
105 {
106     CORBA::Long nbDocument = _engine_cpp->countDocument();
107 //     CORBA::Long nbDocument = 333;
108     return nbDocument;
109 }
110
111 Document_ptr HEXABLOCK::getDocument(CORBA::Long i)
112 {
113     HEXA_NS::Document *doc=_engine_cpp->getDocument(i);
114     Document_impl *servantCorba=new Document_impl(doc);
115     return servantCorba->_this();
116 }
117
118 void HEXABLOCK::removeDocument(HEXABLOCK_ORB::Document_ptr docIn)
119 {
120   Document_impl* docServant = ::DownCast<Document_impl*>( docIn );
121   ASSERT( docServant );
122   if ( docServant ) {
123     HEXA_NS::Document* doc = docServant->GetImpl();
124     _engine_cpp->removeDocument( doc );
125   }
126 }
127
128
129 Document_ptr HEXABLOCK::addDocument()
130 {
131     MESSAGE("ADD1");
132     HEXA_NS::Document *doc=_engine_cpp->addDocument();
133     MESSAGE("ADD2");
134     Document_impl *servantCorba=new Document_impl(doc);
135     MESSAGE("ADD3");
136     return servantCorba->_this();
137 }
138
139
140
141 Document_ptr HEXABLOCK::loadDocument(const char* xmlFilename)
142 {
143     HEXA_NS::Document *doc=_engine_cpp->loadDocument(xmlFilename);
144     Document_impl *servantCorba=new Document_impl(doc);
145     return servantCorba->_this();
146 }
147
148 GEOM_Client* HEXABLOCK::getGeomClient()
149 {
150   // create shape reader if necessary
151   if ( _geomClient == NULL ) 
152     _geomClient = new GEOM_Client(GetContainerRef());
153   ASSERT( _geomClient);
154   return _geomClient;
155 }
156
157
158 TopoDS_Shape HEXABLOCK::geomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
159 {
160   TopoDS_Shape S;
161   if ( !theGeomObject->_is_nil() ){
162     _geomClient = getGeomClient();
163
164     if(CORBA::is_nil(_geomGen)) {
165       Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
166       _geomGen=GEOM::GEOM_Gen::_narrow(temp);
167     }
168     if ( _geomClient != NULL  && !_geomGen->_is_nil () )
169       S = _geomClient->GetShape( _geomGen, theGeomObject );
170   }
171   return S;
172 }
173
174
175
176
177
178 // HEXABLOCK_ORB::GEOM_Object_ptr HEXABLOCK::shapeToGeomObject (const TopoDS_Shape& theShape )
179 // {
180 //   HEXABLOCK_ORB::GEOM_Object_var aShapeObj;
181 //   if ( !theShape.IsNull() ) {
182 //     _geomClient = getGeomClient();//new GEOM_Client(GetContainerRef());
183 //     TCollection_AsciiString IOR;
184 //     bool found = _geomClient->Find(theShape, IOR);
185 //     if (found){
186 //       CORBA::Object_var obj = _orb->string_to_object( IOR.ToCString() );
187 //       aShapeObj = HEXABLOCK_ORB::GEOM_Object::_narrow ( obj );
188 //     }
189 //   }
190 //   return aShapeObj._retn();
191 // }
192
193
194
195 // GEOM::GEOM_Object_ptr HEXABLOCK::_makeFace( const TopoDS_Shape& theShape )
196 // {
197 //   ASSERT ( theShape.ShapeType() == TopAbs_FACE );
198 // 
199 //   int theStudyID = 0;
200 //   GEOM::GEOM_Object_var result; 
201 //   
202 // // PrimOp   = self.GetI3DPrimOperations 
203 // // GEOM_IShapesOperations
204 //   return result._retn();
205 // }
206
207 // GEOM::GEOM_Object_ptr HEXABLOCK::_makeEdge( const TopoDS_Shape& theShape )
208 // {
209 //   ASSERT ( theShape.ShapeType() == TopAbs_EDGE );
210 //   int theStudyID = 0;
211 //   GEOM::GEOM_Object_var result;
212 //   GEOM::GEOM_Object_var p1;
213 //   GEOM::GEOM_Object_var p2;
214 // 
215 //   GEOM::GEOM_IBasicOperations_var     aBasicOp  = _geomGen->GetIBasicOperations(theStudyID);
216 //   GEOM::GEOM_IShapesOperations_var    aShapesOp = _geomGen->GetIShapesOperations(theStudyID);
217 // 
218 //   TopoDS_Edge anEdge = TopoDS::Edge( theShape );
219 //   double f, l;
220 //   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, f, l);
221 // 
222 //   gp_Pnt aCurveStart = aCurve->Value( f );
223 //   gp_Pnt aCurveEnd   = aCurve->Value( l );
224 // 
225 //   double x1, y1, z1;
226 //   double x2, y2, z2;
227 // 
228 //   x1 = aCurveStart.X(); y1 = aCurveStart.Y(); z1 = aCurveStart.Z();
229 //   x2 = aCurveEnd.X();   y2 = aCurveEnd.Y(); z2 = aCurveEnd.Z();
230 //   p1= aBasicOp->MakePointXYZ(x1, y1, z1);
231 //   p2= aBasicOp->MakePointXYZ(x2, y2, z2);
232 //   result = aShapesOp->MakeEdge(p1, p2);
233 // 
234 //   return result._retn();
235 // }
236
237 GEOM::GEOM_Object_ptr HEXABLOCK::_makeVertex( const TopoDS_Shape& theShape )
238 {
239   ASSERT ( theShape.ShapeType() == TopAbs_VERTEX );
240
241   int theStudyID = 0;
242   GEOM::GEOM_Object_var result;
243
244   TopoDS_Vertex aPoint;
245   aPoint      = TopoDS::Vertex( theShape );
246   gp_Pnt aPnt = BRep_Tool::Pnt( aPoint );
247   double x = aPnt.X();
248   double y = aPnt.Y();
249   double z = aPnt.Z();
250
251   GEOM::GEOM_IBasicOperations_var aBasicOp = _geomGen->GetIBasicOperations(theStudyID);
252   result = aBasicOp->MakePointXYZ(x, y, z);
253
254   return result._retn();
255 }
256
257
258 GEOM::GEOM_Object_ptr HEXABLOCK::shapeToGeomObject (const TopoDS_Shape& theShape )
259 {
260   GEOM::GEOM_Object_var aShapeObj;
261   if ( !theShape.IsNull() ) {
262     switch ( theShape.ShapeType() ){
263       case TopAbs_VERTEX: aShapeObj = _makeVertex( theShape ); break;
264 //       case TopAbs_FACE:   aShapeObj = _makeFace( theShape ); break;
265 //       case TopAbs_EDGE:   aShapeObj = _makeEdge( theShape ); break;
266       default: throw (SALOME_Exception(LOCALIZED("shapeToGeomObject : shape type not supported")));
267     }
268   }
269   return aShapeObj._retn();
270 }
271
272
273 // GEOM::GEOM_Object_ptr HEXABLOCK::shapeToGeomObject (const TopoDS_Shape& theShape )
274 // {
275 //   GEOM::GEOM_Object_var aShapeObj;
276 //   std::cout<<"shapeToGeomObject "<<std::endl;
277 //   if ( !theShape.IsNull() ) {
278 //     std::cout<<"shapeToGeomObject !theShape.IsNull()"<<std::endl;
279 //     _geomClient = getGeomClient();//new GEOM_Client(GetContainerRef());
280 //     TCollection_AsciiString IOR;
281 //     bool found = _geomClient->Find(theShape, IOR);
282 //     std::cout<<"shapeToGeomObject found"<<found<<std::endl;
283 //     if (found){
284 //       CORBA::Object_var obj = _orb->string_to_object( IOR.ToCString() );
285 //       aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
286 //     }
287 //   }
288 //   return aShapeObj._retn();
289 // }
290
291
292
293
294
295
296
297
298 SALOME_LifeCycleCORBA*  HEXABLOCK::GetLCC() {
299   if ( _lcc == NULL ) {
300     _lcc = new SALOME_LifeCycleCORBA( GetNS() );
301   }
302   return _lcc;
303 }
304
305 SALOME_NamingService* HEXABLOCK::GetNS()
306 {
307   if ( _ns == NULL ) {
308     _ns = SINGLETON_<SALOME_NamingService>::Instance();
309     ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
310     _ns->init_orb( _orb );
311   }
312   return _ns;
313 }
314
315
316
317 TopoDS_Shape string2shape( const string& brep )
318 {
319   TopoDS_Shape shape;
320
321 //   istringstream streamBrep(brep.c_str());
322   istringstream streamBrep(brep);
323 //   char* chaine = new char[brep.size()];
324 //   strcpy(chaine, brep.c_str()); 
325 //   istringstream streamBrep( chaine );
326
327   BRep_Builder aBuilder;
328   BRepTools::Read(shape, streamBrep, aBuilder);
329 //   BRepTools::Read(shape, brep, aBuilder);
330   return shape;
331 }
332
333 // TopoDS_Shape STRING_TO_SHAPE( char* buf, int sizebuf )
334 // {
335 //   TopoDS_Shape shape;
336 //   std::istrstream streamBrep(buf,sizebuf);
337 //   BRep_Builder aBuilder;
338 //   BRepTools::Read(shape, streamBrep, aBuilder);
339 //   return(shape);
340 // }
341
342
343
344
345 string shape2string( const TopoDS_Shape& aShape )
346 {
347   ostringstream streamShape;
348 //   string  strShape;
349   BRepTools::Write(aShape, streamShape);
350 //   BRepTools::Write(aShape, strShape);
351
352   return streamShape.str();
353 }
354
355
356 extern "C"
357 {
358   PortableServer::ObjectId * HEXABLOCKEngine_factory(
359                                CORBA::ORB_ptr orb,
360                                PortableServer::POA_ptr poa, 
361                                PortableServer::ObjectId * contId,
362                                const char *instanceName, 
363                                const char *interfaceName)
364   {
365     MESSAGE("PortableServer::ObjectId * HEXABLOCKEngine_factory()");
366     SCRUTE(interfaceName);
367     HEXABLOCK * myHEXABLOCK 
368       = new HEXABLOCK(orb, poa, contId, instanceName, interfaceName);
369     return myHEXABLOCK->getId() ;
370   }
371
372 }
373
374 // //================= Primitives Construction : BasicOperations =================
375 // //=============================================================================
376 // //  makeBanner: renvoie un message
377 // //=============================================================================
378 // 
379 // 
380 // #include "Vertex_impl.hxx"
381 // #include "Edge_impl.hh"
382 // char* HEXABLOCK::makeBanner(const char* name)
383 // {
384 //     string banner="Pourquoi?, ";
385 //     banner+=name;
386 //     return CORBA::string_dup(banner.c_str());
387 // }
388 // 
389 // //=============================================================================
390 // //  makeVertex: construit un sommet
391 // //=============================================================================
392 // 
393 // HEXABLOCK_ORB::Vertex_ptr HEXABLOCK::makeVertex(CORBA::Double x, CORBA::Double y, CORBA::Double z)
394 // {
395 //   Vertex *vert=_engine_cpp->makeVertex(x,y,z);
396 //   Vertex_impl *servantCorba=new Vertex_impl(vert);
397 //   return servantCorba->_this();
398 // }
399 // 
400 // //=============================================================================
401 // //  makeEdge: construit une arrĂȘte
402 // //=============================================================================
403 // 
404 // HEXABLOCK_ORB::Edge_ptr HEXABLOCK::makeEdge(HEXABLOCK_ORB::Vertex_ptr va, HEXABLOCK_ORB::Vertex_ptr vb)
405 // {
406 //   Edge *edg=_engine_cpp->makeEdge(va,vb);
407 //   Edge_impl *servantCorba=new Edge_impl(edg);
408 //   return servantCorba->_this();
409 // }
410 // //=============================================================================
411 // // 
412 // //=============================================================================
413