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