Salome HOME
Forward medcoupling exception thrown in case of problem of conversion
[modules/smesh.git] / src / SMESH_I / MG_ADAPT_i.cxx
1 // Copyright (C) 2011-2020  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, or (at your option) any later version.
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
20 #include "MG_ADAPT_i.hxx"
21
22 #include "string.h"
23 #include "SMESH_Gen_i.hxx"
24 #include <SMESH_Gen.hxx>
25 #include <SALOMEconfig.h>
26 #include CORBA_CLIENT_HEADER(SALOMEDS)
27
28
29 //=============================================================================
30 /*!
31  *  SMESH_Gen_i::CreateMG_ADAPT
32  *
33  *  Create measurement instance
34  */
35 //=============================================================================
36
37
38 using namespace SMESH;
39 void MG_ADAPT_i::copyHypothesisDataToImpl(const SMESH::MgAdaptHypothesisData& from, ::MG_ADAPT::MgAdaptHypothesisData* to) const
40 {
41   to->myFileInDir = from.myFileInDir;
42   to->myMeshFileIn = from.myMeshFileIn;
43   to->myMeshFileBackground = from.myMeshFileBackground;
44   to->myOutMeshName = from.myOutMeshName;
45   to->myMeshFileOut = from.myMeshFileOut;
46   to->myFileOutDir = from.myFileOutDir;
47   to->myFileSizeMapDir = from.myFileSizeMapDir;
48   to->myFieldName = from.myFieldName;
49   to->fromMedFile = from.fromMedFile;
50   to->myPublish = from.myPublish;
51   to->myMeshOutMed = from.myMeshOutMed;
52   to->myUseLocalMap = from.myUseLocalMap;
53   to->myUseBackgroundMap = from.myUseBackgroundMap;
54   to->myUseConstantValue = from.myUseConstantValue;
55   to->myConstantValue = from.myConstantValue;
56   to->myTimeStep = from.myTimeStep;
57   to->myRank = from.myRank;
58   to->myUseNoTimeStep = from.myUseNoTimeStep;
59   to->myUseLastTimeStep = from.myUseLastTimeStep;
60   to->myUseChosenTimeStep = from.myUseChosenTimeStep;
61   to->myWorkingDir = from.myWorkingDir;
62   to->myLogFile = from.myLogFile;
63   to->myPrintLogInFile = from.myPrintLogInFile;
64   to->myKeepFiles = from.myKeepFiles;
65   to->myRemoveLogOnSuccess = from.myRemoveLogOnSuccess;
66   to->myVerboseLevel = from.myVerboseLevel;
67 }
68 void MG_ADAPT_i::copyHypothesisDataFromImpl(const ::MG_ADAPT::MgAdaptHypothesisData* from, SMESH::MgAdaptHypothesisData* to) const
69 {
70   to->myFileInDir = CORBA::string_dup(from->myFileInDir.c_str());
71   to->myMeshFileIn = CORBA::string_dup(from->myMeshFileIn.c_str());
72   to->myMeshFileBackground = CORBA::string_dup(from->myMeshFileBackground.c_str());
73   to->myOutMeshName = CORBA::string_dup(from->myOutMeshName.c_str());
74   to->myMeshFileOut = CORBA::string_dup(from->myMeshFileOut.c_str());
75   to->myFileOutDir = CORBA::string_dup(from->myFileOutDir.c_str());
76   to->myFileSizeMapDir = CORBA::string_dup(from->myFileSizeMapDir.c_str());
77   to->myFieldName = CORBA::string_dup(from->myFieldName.c_str());
78   to->fromMedFile = from->fromMedFile;
79   to->myPublish = from->myPublish;
80   to->myMeshOutMed = from->myMeshOutMed;
81   to->myUseLocalMap = from->myUseLocalMap;
82   to->myUseBackgroundMap = from->myUseBackgroundMap;
83   to->myUseConstantValue = from->myUseConstantValue;
84   to->myConstantValue = from->myConstantValue;
85   to->myTimeStep = from->myTimeStep;
86   to->myRank = from->myRank;
87   to->myUseNoTimeStep = from->myUseNoTimeStep;
88   to->myUseLastTimeStep = from->myUseLastTimeStep;
89   to->myUseChosenTimeStep = from->myUseChosenTimeStep;
90   to->myWorkingDir = CORBA::string_dup(from->myWorkingDir.c_str());
91   to->myLogFile = CORBA::string_dup(from->myLogFile.c_str());
92   to->myPrintLogInFile = from->myPrintLogInFile;
93   to->myKeepFiles = from->myKeepFiles;
94   to->myRemoveLogOnSuccess = from->myRemoveLogOnSuccess;
95   to->myVerboseLevel = from->myVerboseLevel;
96 }
97 SMESH::MG_ADAPT_ptr SMESH_Gen_i::CreateMG_ADAPT()
98 {
99   SMESH::MG_ADAPT_i* aMGadapt = new SMESH::MG_ADAPT_i();
100   SMESH::MG_ADAPT_var anObj = aMGadapt->_this();
101   return anObj._retn();
102 }
103 SMESH::MG_ADAPT_ptr SMESH_Gen_i::CreateAdaptationHypothesis()
104 {
105   SMESH::MG_ADAPT_i* aMGadapt = new SMESH::MG_ADAPT_i();
106   SMESH::MG_ADAPT_var anObj = aMGadapt->_this();
107   return anObj._retn();
108 }
109 SMESH::MG_ADAPT_OBJECT_ptr SMESH_Gen_i::Adaptation( const char* adaptationType)
110 {
111
112   if (!strcmp(adaptationType, "MG_Adapt"))
113   {
114     SMESH::MG_ADAPT_OBJECT_i* mg_adapt_object = new SMESH::MG_ADAPT_OBJECT_i();
115     SMESH::MG_ADAPT_OBJECT_var anObj = mg_adapt_object->_this();
116     return anObj._retn();
117   }
118
119 }
120 //~SMESH::MG_ADAPT_ptr MG_ADAPT_i::CreateMG_ADAPT()
121 //~{
122
123   //~SMESH_Gen_i* smeshGen_i = SMESH_Gen_i::GetSMESHGen();
124   //~SMESH::MG_ADAPT_i* aMGadapt = new SMESH::MG_ADAPT_i(smeshGen_i->GetPOA());
125   //~SMESH::MG_ADAPT_var anObj = aMGadapt->_this();
126   //~return anObj._retn();
127 //~}
128 //=============================================================================
129 /*!
130  *  standard constructor
131  */
132 //=============================================================================
133 MG_ADAPT_i::MG_ADAPT_i(): SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
134 {
135   myMgAdapt = new ::MG_ADAPT::MgAdapt();
136 }
137 //~MG_ADAPT_i::MG_ADAPT_i(PortableServer::POA_var myPoa): SALOME::GenericObj_i( myPoa )
138 //~{
139   //~myMgAdapt = new ::MG_ADAPT::MgAdapt();
140 //~}
141
142 //=============================================================================
143 /*!
144  *  standard constructor
145  */
146 //=============================================================================
147 //~MG_ADAPT_i::MG_ADAPT_i( CORBA::ORB_ptr orb,
148                             //~ADAPT::ADAPT_Gen_var engine )
149 //~{
150
151   //~_gen_i = engine;
152   //~_orb = orb;
153   //~myMgAdapt = new MgAdapt();
154 //~}
155
156 //=============================================================================
157 /*!
158  *  standard destructor
159  */
160 //=============================================================================
161 MG_ADAPT_i::~MG_ADAPT_i()
162 {
163 }
164 void MG_ADAPT_i::setData( SMESH::MgAdaptHypothesisData& data)
165 {
166   ::MG_ADAPT::MgAdaptHypothesisData* baseData = new ::MG_ADAPT::MgAdaptHypothesisData();
167   copyHypothesisDataToImpl(data, baseData);
168   myMgAdapt->setData(baseData);
169   delete baseData;
170 }
171 void MG_ADAPT_i::setMedFileIn(const char* str)
172 {
173   myMgAdapt->setMedFileIn(str);
174 }
175 char* MG_ADAPT_i::getMedFileIn()
176 {
177   return CORBA::string_dup(myMgAdapt->getMedFileIn().c_str());
178 }
179 void MG_ADAPT_i::setMedFileOut(const char* str)
180 {
181   myMgAdapt->setMedFileOut(str);
182 }
183 char* MG_ADAPT_i::getMedFileOut()
184 {
185   return CORBA::string_dup(myMgAdapt->getMedFileOut().c_str());
186 }
187 void MG_ADAPT_i::setMeshName(const char* str)
188 {
189   myMgAdapt->setMeshName(str);
190 }
191 char* MG_ADAPT_i::getMeshName()
192 {
193   return CORBA::string_dup(myMgAdapt->getMeshName().c_str());
194 }
195 void MG_ADAPT_i::setMeshNameOut(const char* str)
196 {
197   myMgAdapt->setMeshNameOut(str);
198 }
199 char* MG_ADAPT_i::getMeshNameOut()
200 {
201   return CORBA::string_dup(myMgAdapt->getMeshNameOut().c_str());
202 }
203 void MG_ADAPT_i::setMeshOutMed(bool mybool)
204 {
205   myMgAdapt->setMeshOutMed(mybool);
206 }
207 bool MG_ADAPT_i::getMeshOutMed()
208 {
209   return myMgAdapt->getMeshOutMed();
210 }
211 void MG_ADAPT_i::setPublish(bool mybool)
212 {
213   myMgAdapt->setPublish(mybool);
214 }
215 bool MG_ADAPT_i::getPublish()
216 {
217   return myMgAdapt->getPublish();
218 }
219 void MG_ADAPT_i::setSizeMapFieldName(const char* str)
220 {
221   myMgAdapt->setFieldName(str);
222 }
223 char* MG_ADAPT_i::getSizeMapFieldName()
224 {
225   return CORBA::string_dup(myMgAdapt->getFieldName().c_str());
226 }
227 void MG_ADAPT_i::setTimeStep(CORBA::Long t)
228 {
229   myMgAdapt->setTimeStep(t);
230 }
231 CORBA::Long MG_ADAPT_i::getTimeStep()
232 {
233   return myMgAdapt->getTimeStep();
234 }
235 void MG_ADAPT_i::setTimeStepRank(CORBA::Long t, CORBA::Long r)
236 {
237   myMgAdapt->setChosenTimeStepRank();
238   myMgAdapt->setRankTimeStep(t, r);
239 }
240 CORBA::Long MG_ADAPT_i::getRank()
241 {
242   return myMgAdapt->getRank();
243 }
244 void MG_ADAPT_i::setTimeStepRankLast()
245 {
246   myMgAdapt->setTimeStepRankLast();
247 }
248 void MG_ADAPT_i::setNoTimeStep()
249 {
250   myMgAdapt->setNoTimeStep();
251 }
252 void MG_ADAPT_i::setLogFile(const char* str)
253 {
254   myMgAdapt->setLogFile(str);
255 }
256 char* MG_ADAPT_i::getLogFile()
257 {
258   return CORBA::string_dup(myMgAdapt->getLogFile().c_str());
259 }
260
261 void MG_ADAPT_i::setVerbosityLevel(CORBA::Long v)
262 {
263   myMgAdapt->setVerbosityLevel(v);
264 }
265 CORBA::Long MG_ADAPT_i::getVerbosityLevel()
266 {
267   return myMgAdapt->getVerbosityLevel();
268 }
269 void MG_ADAPT_i::setRemoveOnSuccess(bool mybool)
270 {
271   myMgAdapt->setRemoveOnSuccess(mybool);
272 }
273 bool MG_ADAPT_i::getRemoveOnSuccess()
274 {
275   myMgAdapt->getRemoveOnSuccess();
276 }
277 SMESH::MgAdaptHypothesisData* MG_ADAPT_i::getData()
278 {
279   SMESH::MgAdaptHypothesisData* result = new SMESH::MgAdaptHypothesisData();
280   ::MG_ADAPT::MgAdaptHypothesisData* from =  myMgAdapt->getData();
281   copyHypothesisDataFromImpl(from, result);
282   return result;
283 }
284 void MG_ADAPT_i::setUseLocalMap(bool mybool)
285 {
286   myMgAdapt->setUseLocalMap(mybool);
287 }
288 bool MG_ADAPT_i::getUseLocalMap()
289 {
290   return myMgAdapt->getUseLocalMap();
291 }
292 void MG_ADAPT_i::setUseBackgroundMap(bool mybool)
293 {
294   myMgAdapt->setUseBackgroundMap(mybool);
295 }
296 bool MG_ADAPT_i::getUseBackgroundMap()
297 {
298   return myMgAdapt->getUseBackgroundMap();
299 }
300 void MG_ADAPT_i::setUseConstantValue(bool mybool)
301 {
302   myMgAdapt->setUseConstantValue(mybool);
303 }
304 bool MG_ADAPT_i::getUseConstantValue()
305 {
306   return myMgAdapt->getUseConstantValue();
307 }
308 void MG_ADAPT_i::setConstantSize(double value)
309 {
310   myMgAdapt->setConstantValue(value);
311 }
312 double MG_ADAPT_i::getConstantSize()
313 {
314   return myMgAdapt->getConstantValue();
315 }
316 void MG_ADAPT_i::setSizeMapFile(const char* str)
317 {
318   myMgAdapt->setSizeMapFile(str);
319 }
320 char* MG_ADAPT_i::getSizeMapFile()
321 {
322   return CORBA::string_dup(myMgAdapt->getSizeMapFile().c_str());
323 }
324 void MG_ADAPT_i::setFromMedFile(bool mybool)
325 {
326   myMgAdapt->setFromMedFile(mybool);
327 }
328 bool MG_ADAPT_i::isFromMedFile()
329 {
330   return myMgAdapt->isFromMedFile();
331 }
332
333 void MG_ADAPT_i::setKeepWorkingFiles(bool mybool)
334 {
335   myMgAdapt->setKeepWorkingFiles(mybool);
336 }
337 bool MG_ADAPT_i::getKeepWorkingFiles()
338 {
339   return myMgAdapt->getKeepWorkingFiles();
340 }
341
342 //~void MG_ADAPT_i::setPrCORBA::LongLogInFile(bool);
343 //~bool MG_ADAPT_i::getPrCORBA::LongLogInFile();
344
345 void MG_ADAPT_i::setSizeMapType(const char* type)
346 {
347   setUseLocalMap(false);
348   setUseBackgroundMap(false);
349   setUseConstantValue(false);
350
351   if (!strcmp("Local", type))
352     setUseLocalMap(true);
353   else if (!strcmp("Background", type))
354     setUseBackgroundMap(true);
355   else
356     setUseConstantValue(true);
357 }
358 void MG_ADAPT_i::setWorkingDir(const char* dir)
359 {
360   myMgAdapt->setWorkingDir(dir);
361 }
362 char* MG_ADAPT_i::getWorkingDir()
363 {
364   return CORBA::string_dup(myMgAdapt->getWorkingDir().c_str());
365 }
366 bool MG_ADAPT_i::setAll()
367 {
368   return myMgAdapt->setAll();
369 }
370 char* MG_ADAPT_i::getCommandToRun()
371 {
372   return CORBA::string_dup(myMgAdapt->getCommandToRun().c_str());
373 }
374
375 void MG_ADAPT_i::compute()
376 {
377   errStr = "";
378   try
379   {
380     myMgAdapt->compute(errStr);
381   }
382   catch (const std::exception& e)
383   {
384     std::ostringstream oss; oss << "Exception thrown on MG_ADAPT_i::compute invocation with error message \"" << errStr
385                  << "\" with exception message \"" << e.what() << "\"";
386                 THROW_SALOME_CORBA_EXCEPTION(oss.str().c_str(),SALOME::INTERNAL_ERROR);
387   }
388   if(myMgAdapt->getPublish())
389   {
390     SMESH_Gen_i* smeshGen_i = SMESH_Gen_i::GetSMESHGen();
391     SMESH::DriverMED_ReadStatus theStatus;
392     smeshGen_i->CreateMeshesFromMED(myMgAdapt->getMedFileOut().c_str(), theStatus);
393   }
394 }
395 char* MG_ADAPT_i::getErrMsg()
396 {
397   return CORBA::string_dup(errStr.c_str());
398 }
399 char* MG_ADAPT_i::getFileName()
400 {
401   return CORBA::string_dup(myMgAdapt->getFileName().c_str());
402 }
403 char* MG_ADAPT_i::getExeName()
404 {
405   return CORBA::string_dup(myMgAdapt->getExeName().c_str());
406 }
407 void MG_ADAPT_i::copyMgAdaptHypothesisData( const SMESH::MgAdaptHypothesisData& data)
408 {
409   ::MG_ADAPT::MgAdaptHypothesisData* baseData = new ::MG_ADAPT::MgAdaptHypothesisData();
410   copyHypothesisDataToImpl(data, baseData);
411   myMgAdapt->copyMgAdaptHypothesisData(baseData);
412   delete baseData;
413 }
414
415 //~void MG_ADAPT_i::checkDirPath(char*& str)
416 //~{
417   //~myMgAdapt->checkDirPath(str);
418 //~}
419
420 bool MG_ADAPT_i::hasOptionDefined( const char* optionName )
421 {
422   return myMgAdapt->hasOptionDefined(optionName);
423 }
424 void MG_ADAPT_i::setOptionValue(const char* optionName,
425           const char* optionValue) throw (std::invalid_argument)
426 {
427   myMgAdapt->setOptionValue(optionName, optionValue);
428 }
429
430 char* MG_ADAPT_i::getOptionValue(const char* optionName,
431                bool&              isDefault)  throw (std::invalid_argument)
432 {
433   return CORBA::string_dup(myMgAdapt->getOptionValue(optionName, &isDefault).c_str());
434 }
435 str_array* MG_ADAPT_i::getCustomOptionValuesStrVec()
436 {
437   SMESH::str_array_var result = new SMESH::str_array();
438   std::vector <std::string> vals = myMgAdapt->getCustomOptionValuesStrVec();
439   result->length(vals.size());
440   for (int i = 0; i<vals.size(); i++) result[i] = CORBA::string_dup(vals[i].c_str());
441   return result._retn();
442 }
443 str_array*  MG_ADAPT_i::getOptionValuesStrVec()
444 {
445
446   SMESH::str_array_var result = new SMESH::str_array();
447   std::vector <std::string> vals = myMgAdapt->getOptionValuesStrVec();
448   result->length(vals.size());
449   for (int i = 0; i<vals.size(); i++) result[i] = CORBA::string_dup(vals[i].c_str());
450   return result._retn();
451 }
452
453 void MG_ADAPT_i::setPrintLogInFile(bool mybool)
454 {
455   myMgAdapt->setPrintLogInFile(mybool);
456 }
457 bool MG_ADAPT_i::getPrintLogInFile()
458 {
459   return myMgAdapt->getPrintLogInFile();
460 }
461 //~TOptionValues        MG_ADAPT_i::getOptionValues()       const;
462 //~const TOptionValues& MG_ADAPT_i::getCustomOptionValues() const ;
463
464 MG_ADAPT_OBJECT_i::MG_ADAPT_OBJECT_i(): SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
465 {
466   medFileIn="";
467   medFileOut="";
468   medFileBackground="";
469   publish = false;
470   //~myMesh = CORBA::nil;
471 }
472
473 void MG_ADAPT_OBJECT_i::setMeshIn(SMESH::SMESH_Mesh_ptr theMesh )
474 {
475   myMesh = SMESH::SMESH_Mesh::_duplicate(theMesh);
476 }
477 void MG_ADAPT_OBJECT_i::setMEDFileIn(const char* f)
478 {
479   medFileIn =  f;
480 }
481 void MG_ADAPT_OBJECT_i::setMEDFileOut(const char* f)
482 {
483   medFileOut = f;
484 }
485 void MG_ADAPT_OBJECT_i::setMEDFileBackground(const char* f)
486 {
487   medFileBackground = f;
488 }
489 void MG_ADAPT_OBJECT_i::AddHypothesis(SMESH::MG_ADAPT_ptr mg)
490 {
491
492   mg->setMedFileIn(medFileIn.c_str());
493   mg->setMedFileOut(medFileOut.c_str());
494   mg->setSizeMapFile(medFileBackground.c_str());
495   hypothesis = SMESH::MG_ADAPT::_duplicate(mg);
496 }
497 CORBA::Long MG_ADAPT_OBJECT_i::Compute(bool publish)
498 {
499   if(!checkMeshFileIn()){
500     std::cerr<< "\n Error : Please check the MED file input or mesh input. \n";
501     return -1;
502   }
503   hypothesis->setPublish(publish);
504   hypothesis->compute();
505   return 0;
506 }
507
508 bool MG_ADAPT_OBJECT_i::checkMeshFileIn()
509 {
510   bool ret = false; // 1 ok , 0 nook
511   if(!::MG_ADAPT::MgAdapt::isFileExist(medFileIn))
512   {
513     if(!myMesh->_is_nil())
514     {
515       bool toOverwrite  = true;
516       bool toFindOutDim = true;
517       medFileIn = hypothesis->getFileName();
518       medFileIn+= ".med";
519       myMesh->ExportMED(medFileIn.c_str(), false, -1, toOverwrite, toFindOutDim);
520       hypothesis->setMedFileIn(medFileIn.c_str());
521       ret = true;
522     }
523   }
524   else
525     ret = true;
526
527   return ret;
528 }