Salome HOME
Module MULTIPR
[modules/multipr.git] / src / MULTIPR / MULTIPR_i.cxx
1 // Project MULTIPR, IOLS WP1.2.1 - EDF/CS
2 // Partitioning/decimation module for the SALOME v3.2 platform
3
4 /**
5  * \file    MULTIPR_i.cxx
6  *
7  * \brief   see MULTIPR_i.hxx
8  *
9  * \author  Olivier LE ROUX - CS, Virtual Reality Dpt
10  * 
11  * \date    01/2007
12  */
13  
14
15 //*****************************************************************************
16 // Includes section
17 //*****************************************************************************
18  
19 using namespace std;
20
21 #include "MULTIPR_i.hxx"
22 #include "utilities.h"
23
24 #include <string>
25
26 #include "MULTIPR_API.hxx"
27 #include "MULTIPR_Exceptions.hxx"
28
29
30 //*****************************************************************************
31 // Class MULTIPR_Gen_i implementation
32 //*****************************************************************************
33
34 MULTIPR_Gen_i::MULTIPR_Gen_i(
35         CORBA::ORB_ptr orb,
36         PortableServer::POA_ptr poa,
37         PortableServer::ObjectId* contId, 
38         const char* instanceName, 
39         const char* interfaceName) :
40         Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
41 {
42         MESSAGE("activate object");
43         _thisObj = this ;
44         _id = _poa->activate_object(_thisObj);
45 }
46
47
48 MULTIPR_Gen_i::~MULTIPR_Gen_i()
49 {
50 }
51
52
53 //-----------------------------------------------------------------------------
54 // High level API
55 //-----------------------------------------------------------------------------
56
57 char* MULTIPR_Gen_i::getVersion()
58         throw (SALOME::SALOME_Exception)
59 {
60         return CORBA::string_dup(multipr::getVersion());
61 }
62
63
64 void MULTIPR_Gen_i::partitionneDomaine(
65         const char* medFilename, 
66         const char* meshName)
67         throw (SALOME::SALOME_Exception)
68 {
69         try
70         {
71                 multipr::partitionneDomaine(medFilename, meshName);
72         }
73         catch (multipr::RuntimeException& e)
74         {
75                 e.dump(cout);
76                 THROW_SALOME_CORBA_EXCEPTION("partitionneDomaine() failed", SALOME::INTERNAL_ERROR);
77         }
78 }
79
80
81 void MULTIPR_Gen_i::partitionneGrain(
82         const char* medFilename, 
83         const char* partName, 
84         CORBA::Long nbParts, 
85         CORBA::Long partitionner)
86         throw (SALOME::SALOME_Exception)
87 {
88         try
89         {
90                 multipr::partitionneGrain(
91                         medFilename,
92                         partName,
93                         nbParts,
94                         partitionner);
95         }
96         catch (multipr::RuntimeException& e)
97         {
98                 e.dump(cout);
99                 THROW_SALOME_CORBA_EXCEPTION("partitionneGrain() failed", SALOME::INTERNAL_ERROR);
100         }
101 }
102
103
104 void MULTIPR_Gen_i::decimePartition(
105         const char*   medFilename, 
106         const char*   partName, 
107         const char*   fieldName,
108         CORBA::Long   fieldIt,
109         const char*   filterName,
110         CORBA::Double tmed,
111         CORBA::Double tlow,
112         CORBA::Double radius,
113         CORBA::Long   boxing)
114         throw (SALOME::SALOME_Exception)
115 {
116         /*
117         // debug
118         cout << "File  : " << medFilename << endl;
119         cout << "Part  : " << partName    << endl;
120         cout << "Field : " << fieldName   << endl;
121         cout << "It    : " << fieldIt     << endl;
122         cout << "Filter: " << filterName  << endl;
123         cout << "Med   : " << tmed        << endl;
124         cout << "Low   : " << tlow        << endl;
125         cout << "Rad   : " << radius      << endl;
126         cout << "Box   : " << boxing      << endl;
127         cout << endl;
128         */
129         
130         try
131         {
132                 multipr::decimePartition(
133                         medFilename, 
134                         partName, 
135                         fieldName,
136                         fieldIt,
137                         filterName,
138                         tmed,
139                         tlow,
140                         radius,
141                         boxing);
142         }
143         catch (multipr::RuntimeException& e)
144         {
145                 e.dump(cout);
146                 THROW_SALOME_CORBA_EXCEPTION("decimePartition() failed", SALOME::INTERNAL_ERROR);
147         }
148 }
149
150
151 //-----------------------------------------------------------------------------
152 // Low level API
153 //-----------------------------------------------------------------------------
154
155 MULTIPR_ORB::MULTIPR_Obj_ptr MULTIPR_Gen_i::getObject(const char* medFilename)
156         throw (SALOME::SALOME_Exception)
157 {
158     MULTIPR_Obj_i* obj = new MULTIPR_Obj_i(medFilename);
159         return obj->POA_MULTIPR_ORB::MULTIPR_Obj::_this();
160 }
161
162
163 MULTIPR_Obj_i::MULTIPR_Obj_i(const char* medFilename)
164         throw (SALOME::SALOME_Exception)
165 {
166         mObj    = new multipr::Obj();
167         mBoxing = 100;
168         
169         try
170         {
171                 cout << "Load " << medFilename << endl;
172                 mObj->create(medFilename);
173                 cout << endl;
174         }
175         catch (multipr::RuntimeException& e)
176         {
177                 delete mObj;
178                 mObj = NULL;
179                 e.dump(cout);
180                 THROW_SALOME_CORBA_EXCEPTION("Unable to create object", SALOME::INTERNAL_ERROR);
181         }
182 }
183
184
185 MULTIPR_Obj_i::~MULTIPR_Obj_i()
186 {
187         if (mObj != NULL)
188         {
189                 delete mObj;
190                 mObj = NULL;
191         }
192 }
193
194
195 CORBA::Boolean MULTIPR_Obj_i::isValidSequentialMEDFile()
196                 throw (SALOME::SALOME_Exception)
197 {
198         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
199         
200         return mObj->isValidSequentialMEDFile();
201 }
202
203
204 CORBA::Boolean MULTIPR_Obj_i::isValidDistributedMEDFile()
205         throw (SALOME::SALOME_Exception)
206 {
207         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
208         
209         return mObj->isValidDistributedMEDFile();
210 }
211
212
213 void MULTIPR_Obj_i::setMesh(const char* meshName)
214                 throw (SALOME::SALOME_Exception)
215 {
216         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
217         
218         try
219         {
220                 mObj->setMesh(meshName);
221                 
222                 cout << "Set mesh OK" << endl << endl;
223         }
224         catch (multipr::RuntimeException& e)
225         {
226                 e.dump(cout);
227                 THROW_SALOME_CORBA_EXCEPTION("Unable to set mesh", SALOME::INTERNAL_ERROR);
228         }
229 }
230
231
232 void MULTIPR_Obj_i::setBoxing(CORBA::Long pBoxing)
233         throw (SALOME::SALOME_Exception)
234 {
235         if (mBoxing < 0) THROW_SALOME_CORBA_EXCEPTION("Invalid boxing parameter; should be >= 1", SALOME::INTERNAL_ERROR);
236         if (mBoxing > 200) THROW_SALOME_CORBA_EXCEPTION("Invalid boxing parameter; should be <= 200", SALOME::INTERNAL_ERROR);
237         
238         mBoxing = pBoxing;
239 }
240
241
242 MULTIPR_ORB::string_array* MULTIPR_Obj_i::getMeshes() 
243         throw (SALOME::SALOME_Exception)
244 {
245         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
246         
247         MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
248         
249         try
250         {
251                 std::vector<std::string> listMeshes = mObj->getMeshes();
252                 mySeq->length(listMeshes.size());
253                 
254                 for (int i = 0 ; i < listMeshes.size() ; i++)
255         {
256                 mySeq[i] = CORBA::string_dup(listMeshes[i].c_str());
257         }
258         }
259         catch (multipr::RuntimeException& e)
260         {
261                 e.dump(cout);
262                 THROW_SALOME_CORBA_EXCEPTION("Unable to get meshes", SALOME::INTERNAL_ERROR);
263         }
264         
265     return mySeq._retn();
266 }
267
268
269 MULTIPR_ORB::string_array* MULTIPR_Obj_i::getFields() 
270         throw (SALOME::SALOME_Exception)
271 {
272         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
273         
274         MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
275         
276         try
277         {
278                 std::vector<std::string> listFields = mObj->getFields();
279                 mySeq->length(listFields.size());
280                 
281                 for (int i = 0 ; i < listFields.size() ; i++)
282         {
283                 mySeq[i] = CORBA::string_dup(listFields[i].c_str());
284         }
285         }
286         catch (multipr::RuntimeException& e)
287         {
288                 e.dump(cout);
289                 THROW_SALOME_CORBA_EXCEPTION("Unable to get fields", SALOME::INTERNAL_ERROR);
290         }
291         
292     return mySeq._retn();
293 }
294
295
296 CORBA::Long MULTIPR_Obj_i::getTimeStamps(const char* fieldName)
297         throw (SALOME::SALOME_Exception)
298 {
299         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
300         
301         try
302         {
303                 return mObj->getTimeStamps(fieldName);
304         }
305         catch (multipr::RuntimeException& e)
306         {
307                 e.dump(cout);
308                 THROW_SALOME_CORBA_EXCEPTION("Unable to get time stamps", SALOME::INTERNAL_ERROR);
309         }
310 }
311
312
313 MULTIPR_ORB::string_array* MULTIPR_Obj_i::getParts()
314         throw (SALOME::SALOME_Exception)
315 {
316         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
317         
318         MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
319         
320         try
321         {
322                 std::vector<std::string> listParts = mObj->getParts();
323                 mySeq->length(listParts.size());
324                 
325                 for (int i = 0 ; i < listParts.size() ; i++)
326         {
327                 mySeq[i] = CORBA::string_dup(listParts[i].c_str());
328         }
329         }
330         catch (multipr::RuntimeException& e)
331         {
332                 e.dump(cout);
333                 THROW_SALOME_CORBA_EXCEPTION("Unable to get parts", SALOME::INTERNAL_ERROR);
334         }
335         
336     return mySeq._retn();
337 }
338
339
340 char* MULTIPR_Obj_i::getPartInfo(const char* pPartName)
341                 throw (SALOME::SALOME_Exception)
342 {
343         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
344         
345         return CORBA::string_dup(mObj->getPartInfo(pPartName).c_str());
346 }
347
348
349 MULTIPR_ORB::string_array* MULTIPR_Obj_i::partitionneDomaine()
350         throw (SALOME::SALOME_Exception)
351 {
352         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
353         
354         MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
355         
356         try
357         {
358                 std::vector<std::string> listParts = mObj->partitionneDomaine();
359                 mySeq->length(listParts.size());
360                 
361                 for (int i = 0 ; i < listParts.size() ; i++)
362         {
363                 mySeq[i] = CORBA::string_dup(listParts[i].c_str());
364         }
365         }
366         catch (multipr::RuntimeException& e)
367         {
368                 e.dump(cout);
369                 THROW_SALOME_CORBA_EXCEPTION("Unable to partition mesh", SALOME::INTERNAL_ERROR);
370         }
371         
372     return mySeq._retn();
373 }
374
375
376 MULTIPR_ORB::string_array* MULTIPR_Obj_i::partitionneGrain(
377                 const char* pPartName, 
378                 CORBA::Long pNbParts, 
379                 CORBA::Long pPartitionner)
380                 throw (SALOME::SALOME_Exception)
381 {
382         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
383         
384         MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
385         
386         try
387         {
388                 std::vector<std::string> listParts = mObj->partitionneGrain(
389                         pPartName,
390                         pNbParts,
391                         pPartitionner);
392                         
393                 mySeq->length(listParts.size());
394                 
395                 for (int i = 0 ; i < listParts.size() ; i++)
396         {
397                 mySeq[i] = CORBA::string_dup(listParts[i].c_str());
398         }
399         }
400         catch (multipr::RuntimeException& e)
401         {
402                 e.dump(cout);
403                 THROW_SALOME_CORBA_EXCEPTION("Unable to partition group", SALOME::INTERNAL_ERROR);
404         }
405         
406     return mySeq._retn();
407 }
408
409
410 MULTIPR_ORB::string_array* MULTIPR_Obj_i::decimePartition(
411                 const char*   pPartName,
412                 const char*   pFieldName,
413                 CORBA::Long   pFieldIt,
414                 const char*   pFilterName,
415                 CORBA::Double pTmed,
416                 CORBA::Double pTlow,
417                 CORBA::Double pRadius)
418                 throw (SALOME::SALOME_Exception)
419 {
420         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
421         
422         MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
423         
424         try
425         {
426                 std::vector<std::string> listParts = mObj->decimePartition(
427                         pPartName,
428                         pFieldName,
429                         pFieldIt,
430                         pFilterName,
431                         pTmed,
432                         pTlow,
433                         pRadius,
434                         mBoxing);
435                 
436                 mySeq->length(listParts.size());
437                 
438                 for (int i = 0 ; i < listParts.size() ; i++)
439         {
440                 mySeq[i] = CORBA::string_dup(listParts[i].c_str());
441         }
442         }
443         catch (multipr::RuntimeException& e)
444         {
445                 e.dump(cout);
446                 THROW_SALOME_CORBA_EXCEPTION("Unable to decimate", SALOME::INTERNAL_ERROR);
447         }
448         
449         return mySeq._retn();
450 }
451
452
453 void MULTIPR_Obj_i::save()
454         throw (SALOME::SALOME_Exception)
455 {
456         if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
457         
458         try
459         {
460                 mObj->save();
461                 
462         }
463         catch (multipr::RuntimeException& e)
464         {
465                 e.dump(cout);
466                 THROW_SALOME_CORBA_EXCEPTION("Unable to save MED file", SALOME::INTERNAL_ERROR);
467         }
468 }
469
470
471 extern "C"
472 {
473         PortableServer::ObjectId* MULTIPREngine_factory(
474                 CORBA::ORB_ptr orb,
475                 PortableServer::POA_ptr poa, 
476                 PortableServer::ObjectId * contId,
477                 const char* instanceName, 
478                 const char* interfaceName)
479         {
480                 MESSAGE("PortableServer::ObjectId* MULTIPREngine_factory()");
481                 SCRUTE(interfaceName);
482                 MULTIPR_Gen_i* myMULTIPR = new MULTIPR_Gen_i(orb, poa, contId, instanceName, interfaceName);
483                 return myMULTIPR->getId();
484         }
485 }