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