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