Salome HOME
*** empty log message ***
[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         cout << "MULTIPR: Destructor: remove mObj" << endl;
190         delete mObj;
191         mObj = NULL;
192     }
193 }
194
195
196 CORBA::Boolean MULTIPR_Obj_i::isValidSequentialMEDFile()
197         throw (SALOME::SALOME_Exception)
198 {
199     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
200     
201     return mObj->isValidSequentialMEDFile();
202 }
203
204
205 CORBA::Boolean MULTIPR_Obj_i::isValidDistributedMEDFile()
206     throw (SALOME::SALOME_Exception)
207 {
208     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
209     
210     return mObj->isValidDistributedMEDFile();
211 }
212
213
214 char* MULTIPR_Obj_i::getFilename()
215         throw (SALOME::SALOME_Exception)
216 {
217     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
218     
219     return CORBA::string_dup(mObj->getMEDFilename().c_str());
220 }
221
222
223 char* MULTIPR_Obj_i::getSeqFilename()
224         throw (SALOME::SALOME_Exception)
225 {
226     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
227     
228     return CORBA::string_dup(mObj->getSequentialMEDFilename().c_str());
229 }
230
231
232 void MULTIPR_Obj_i::setMesh(const char* meshName)
233         throw (SALOME::SALOME_Exception)
234 {
235     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
236     
237     try
238     {
239         mObj->setMesh(meshName);
240         
241         cout << "Set mesh OK" << endl << endl;
242     }
243     catch (multipr::RuntimeException& e)
244     {
245         e.dump(cout);
246         THROW_SALOME_CORBA_EXCEPTION("Unable to set mesh", SALOME::INTERNAL_ERROR);
247     }
248 }
249
250
251 void MULTIPR_Obj_i::setBoxing(CORBA::Long pBoxing)
252     throw (SALOME::SALOME_Exception)
253 {
254     if (mBoxing < 0) THROW_SALOME_CORBA_EXCEPTION("Invalid boxing parameter; should be >= 1", SALOME::INTERNAL_ERROR);
255     if (mBoxing > 200) THROW_SALOME_CORBA_EXCEPTION("Invalid boxing parameter; should be <= 200", SALOME::INTERNAL_ERROR);
256     
257     mBoxing = pBoxing;
258 }
259
260
261 MULTIPR_ORB::string_array* MULTIPR_Obj_i::getMeshes() 
262     throw (SALOME::SALOME_Exception)
263 {
264     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
265     
266     MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
267     
268     try
269     {
270         std::vector<std::string> listMeshes = mObj->getMeshes();
271         mySeq->length(listMeshes.size());
272         
273         for (int i = 0 ; i < listMeshes.size() ; i++)
274         {
275             mySeq[i] = CORBA::string_dup(listMeshes[i].c_str());
276         }
277     }
278     catch (multipr::RuntimeException& e)
279     {
280         e.dump(cout);
281         THROW_SALOME_CORBA_EXCEPTION("Unable to get meshes", SALOME::INTERNAL_ERROR);
282     }
283     
284     return mySeq._retn();
285 }
286
287
288 MULTIPR_ORB::string_array* MULTIPR_Obj_i::getFields() 
289     throw (SALOME::SALOME_Exception)
290 {
291     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
292     
293     MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
294     
295     try
296     {
297         std::vector<std::string> listFields = mObj->getFields();
298         mySeq->length(listFields.size());
299         
300         for (int i = 0 ; i < listFields.size() ; i++)
301         {
302             mySeq[i] = CORBA::string_dup(listFields[i].c_str());
303         }
304     }
305     catch (multipr::RuntimeException& e)
306     {
307         e.dump(cout);
308         THROW_SALOME_CORBA_EXCEPTION("Unable to get fields", SALOME::INTERNAL_ERROR);
309     }
310     
311     return mySeq._retn();
312 }
313
314
315 CORBA::Long MULTIPR_Obj_i::getTimeStamps(const char* fieldName)
316     throw (SALOME::SALOME_Exception)
317 {
318     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
319     
320     try
321     {
322         return mObj->getTimeStamps(fieldName);
323     }
324     catch (multipr::RuntimeException& e)
325     {
326         e.dump(cout);
327         THROW_SALOME_CORBA_EXCEPTION("Unable to get time stamps", SALOME::INTERNAL_ERROR);
328     }
329 }
330
331
332 MULTIPR_ORB::string_array* MULTIPR_Obj_i::getParts()
333     throw (SALOME::SALOME_Exception)
334 {
335     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
336     
337     MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
338     
339     try
340     {
341         std::vector<std::string> listParts = mObj->getParts();
342         mySeq->length(listParts.size());
343         
344         for (int i = 0 ; i < listParts.size() ; i++)
345         {
346             mySeq[i] = CORBA::string_dup(listParts[i].c_str());
347         }
348     }
349     catch (multipr::RuntimeException& e)
350     {
351         e.dump(cout);
352         THROW_SALOME_CORBA_EXCEPTION("Unable to get parts", SALOME::INTERNAL_ERROR);
353     }
354     
355     return mySeq._retn();
356 }
357
358
359 char* MULTIPR_Obj_i::getPartInfo(const char* pPartName)
360     throw (SALOME::SALOME_Exception)
361 {
362     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
363     
364     return CORBA::string_dup(mObj->getPartInfo(pPartName).c_str());
365 }
366
367
368 MULTIPR_ORB::string_array* MULTIPR_Obj_i::partitionneDomaine()
369     throw (SALOME::SALOME_Exception)
370 {
371     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
372     
373     MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
374     
375     try
376     {
377         std::vector<std::string> listParts = mObj->partitionneDomaine();
378         mySeq->length(listParts.size());
379         
380         for (int i = 0 ; i < listParts.size() ; i++)
381         {
382             mySeq[i] = CORBA::string_dup(listParts[i].c_str());
383         }
384     }
385     catch (multipr::RuntimeException& e)
386     {
387         e.dump(cout);
388         THROW_SALOME_CORBA_EXCEPTION("Unable to partition mesh", SALOME::INTERNAL_ERROR);
389     }
390     
391     return mySeq._retn();
392 }
393
394
395 MULTIPR_ORB::string_array* MULTIPR_Obj_i::partitionneGrain(
396         const char* pPartName, 
397         CORBA::Long pNbParts, 
398         CORBA::Long pPartitionner)
399         throw (SALOME::SALOME_Exception)
400 {
401     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
402     
403     MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
404     
405     try
406     {
407         std::vector<std::string> listParts = mObj->partitionneGrain(
408             pPartName,
409             pNbParts,
410             pPartitionner);
411             
412         mySeq->length(listParts.size());
413         
414         for (int i = 0 ; i < listParts.size() ; i++)
415         {
416             mySeq[i] = CORBA::string_dup(listParts[i].c_str());
417         }
418     }
419     catch (multipr::RuntimeException& e)
420     {
421         e.dump(cout);
422         THROW_SALOME_CORBA_EXCEPTION("Unable to partition group", SALOME::INTERNAL_ERROR);
423     }
424     
425     return mySeq._retn();
426 }
427
428
429 MULTIPR_ORB::string_array* MULTIPR_Obj_i::decimePartition(
430         const char*   pPartName,
431         const char*   pFieldName,
432         CORBA::Long   pFieldIt,
433         const char*   pFilterName,
434         CORBA::Double pTmed,
435         CORBA::Double pTlow,
436         CORBA::Double pRadius)
437         throw (SALOME::SALOME_Exception)
438 {
439     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
440     
441     MULTIPR_ORB::string_array_var mySeq = new MULTIPR_ORB::string_array();
442     
443     try
444     {
445         std::vector<std::string> listParts = mObj->decimePartition(
446             pPartName,
447             pFieldName,
448             pFieldIt,
449             pFilterName,
450             pTmed,
451             pTlow,
452             pRadius,
453             mBoxing);
454         
455         mySeq->length(listParts.size());
456         
457         for (int i = 0 ; i < listParts.size() ; i++)
458         {
459             mySeq[i] = CORBA::string_dup(listParts[i].c_str());
460         }
461     }
462     catch (multipr::RuntimeException& e)
463     {
464         e.dump(cout);
465         THROW_SALOME_CORBA_EXCEPTION("Unable to decimate", SALOME::INTERNAL_ERROR);
466     }
467     
468     return mySeq._retn();
469 }
470
471
472 char* MULTIPR_Obj_i::evalDecimationParams(
473     const char* pPartName, 
474     const char* pFieldName, 
475     CORBA::Long pFieldIt, 
476     const char* pFilterName,
477     const char* pFilterParams)
478     throw (SALOME::SALOME_Exception)
479 {
480     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
481     
482     try
483     {
484         string res = mObj->evalDecimationParams(
485             pPartName,
486             pFieldName,
487             pFieldIt,
488             pFilterName,
489             pFilterParams);
490             
491         return CORBA::string_dup(res.c_str());
492             
493     }
494     catch (multipr::RuntimeException& e)
495     {
496         e.dump(cout);
497         THROW_SALOME_CORBA_EXCEPTION("Unable to evaluate decimation parameters", SALOME::INTERNAL_ERROR);
498     }
499 }
500
501
502 void MULTIPR_Obj_i::removeParts(const char* pPrefixPartName)
503     throw (SALOME::SALOME_Exception)
504 {
505     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
506     
507     mObj->removeParts(pPrefixPartName);
508 }
509
510
511 void MULTIPR_Obj_i::save(const char* pPath)
512     throw (SALOME::SALOME_Exception)
513 {
514     if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
515     
516     try
517     {
518         mObj->save(pPath);
519         
520     }
521     catch (multipr::RuntimeException& e)
522     {
523         e.dump(cout);
524         THROW_SALOME_CORBA_EXCEPTION("Unable to save MED file", SALOME::INTERNAL_ERROR);
525     }
526 }
527
528
529 extern "C"
530 {
531     PortableServer::ObjectId* MULTIPREngine_factory(
532         CORBA::ORB_ptr orb,
533         PortableServer::POA_ptr poa, 
534         PortableServer::ObjectId * contId,
535         const char* instanceName, 
536         const char* interfaceName)
537     {
538         MESSAGE("PortableServer::ObjectId* MULTIPREngine_factory()");
539         SCRUTE(interfaceName);
540         MULTIPR_Gen_i* myMULTIPR = new MULTIPR_Gen_i(orb, poa, contId, instanceName, interfaceName);
541         return myMULTIPR->getId();
542     }
543 }