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