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