Salome HOME
Ménage avant intégration
[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             SMESH::MG_ADAPT_OBJECT_i* mg_adapt_object = new SMESH::MG_ADAPT_OBJECT_i();
116                 SMESH::MG_ADAPT_OBJECT_var anObj = mg_adapt_object->_this();
117                 return anObj._retn();
118         }
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
311 void MG_ADAPT_i::setConstantSize(double value)
312 {
313         myMgAdapt->setConstantValue(value);
314 }
315 double MG_ADAPT_i::getConstantSize()
316 {
317         return myMgAdapt->getConstantValue();
318 }
319 void MG_ADAPT_i::setSizeMapFile(const char* str)
320 {
321         myMgAdapt->setSizeMapFile(str);
322 }
323 char* MG_ADAPT_i::getSizeMapFile()
324 {
325         return CORBA::string_dup(myMgAdapt->getSizeMapFile().c_str());
326 }
327 void MG_ADAPT_i::setFromMedFile(bool mybool)
328 {
329         myMgAdapt->setFromMedFile(mybool);
330 }
331 bool MG_ADAPT_i::isFromMedFile()
332 {
333         return myMgAdapt->isFromMedFile();
334 }
335
336 void MG_ADAPT_i::setKeepWorkingFiles(bool mybool)
337 {
338         myMgAdapt->setKeepWorkingFiles(mybool);
339 }
340 bool MG_ADAPT_i::getKeepWorkingFiles()
341 {
342         return myMgAdapt->getKeepWorkingFiles();
343 }
344
345 //~void MG_ADAPT_i::setPrCORBA::LongLogInFile(bool);
346 //~bool MG_ADAPT_i::getPrCORBA::LongLogInFile();
347
348 void MG_ADAPT_i::setSizeMapType(const char* type)
349 {
350         setUseLocalMap(false);
351         setUseBackgroundMap(false);
352         setUseConstantValue(false);
353
354         if (!strcmp("Local", type))
355                 setUseLocalMap(true);
356         else if (!strcmp("Background", type))
357             setUseBackgroundMap(true);
358         else
359             setUseConstantValue(true);
360 }
361 void MG_ADAPT_i::setWorkingDir(const char* dir)
362 {
363         myMgAdapt->setWorkingDir(dir);
364 }
365 char* MG_ADAPT_i::getWorkingDir()
366 {
367         return CORBA::string_dup(myMgAdapt->getWorkingDir().c_str());
368 }
369 bool MG_ADAPT_i::setAll()
370 {
371         return myMgAdapt->setAll();
372 }
373 char* MG_ADAPT_i::getCommandToRun()
374 {
375         return CORBA::string_dup(myMgAdapt->getCommandToRun().c_str());
376 }
377
378 //~CORBA::Long MG_ADAPT_i::compute(::CORBA::String_out errStr)
379 //~{
380         //~std::string err("");
381         //~CORBA::Long ret = myMgAdapt->compute(err);
382         //~errStr =  err.c_str();
383         //~return ret;
384 //~}
385 CORBA::Long MG_ADAPT_i::compute()
386 {
387         errStr = "";
388         CORBA::Long ret;
389         try
390     {
391             ret = myMgAdapt->compute(errStr);
392     }
393     catch (const std::exception& e)
394     {
395         std::cerr<<e.what();
396         ret = -1;
397     }
398     if(ret!=-1 && myMgAdapt->getPublish())
399     {
400                 SMESH_Gen_i* smeshGen_i = SMESH_Gen_i::GetSMESHGen();
401                 SMESH::DriverMED_ReadStatus theStatus;
402                 smeshGen_i->CreateMeshesFromMED(myMgAdapt->getMedFileOut().c_str(), theStatus);
403         }
404         //~errStr =  err.c_str();
405         return ret;
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) throw (std::invalid_argument)
438 {
439         myMgAdapt->setOptionValue(optionName, optionValue);
440 }
441
442 char* MG_ADAPT_i::getOptionValue(const char* optionName,
443                                                    bool&              isDefault)  throw (std::invalid_argument)
444 {
445     return CORBA::string_dup(myMgAdapt->getOptionValue(optionName, &isDefault).c_str());
446 }
447 str_array* MG_ADAPT_i::getCustomOptionValuesStrVec()
448 {
449         SMESH::str_array_var result = new SMESH::str_array();
450         std::vector <std::string> vals = myMgAdapt->getCustomOptionValuesStrVec();
451         result->length(vals.size());
452         for (int i = 0; i<vals.size(); i++) result[i] = CORBA::string_dup(vals[i].c_str());
453         return result._retn();
454 }
455 str_array*  MG_ADAPT_i::getOptionValuesStrVec()
456 {
457
458         SMESH::str_array_var result = new SMESH::str_array();
459         std::vector <std::string> vals = myMgAdapt->getOptionValuesStrVec();
460         result->length(vals.size());
461         for (int i = 0; i<vals.size(); i++) result[i] = CORBA::string_dup(vals[i].c_str());
462         return result._retn();
463 }
464
465 void MG_ADAPT_i::setPrintLogInFile(bool mybool)
466 {
467         myMgAdapt->setPrintLogInFile(mybool);
468 }
469 bool MG_ADAPT_i::getPrintLogInFile()
470 {
471         return myMgAdapt->getPrintLogInFile();
472 }
473 //~TOptionValues        MG_ADAPT_i::getOptionValues()       const;
474 //~const TOptionValues& MG_ADAPT_i::getCustomOptionValues() const ;
475
476 MG_ADAPT_OBJECT_i::MG_ADAPT_OBJECT_i(): SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
477 {
478     medFileIn="";
479     medFileOut="";
480     medFileBackground="";
481     publish = false;
482     //~myMesh = CORBA::nil;
483 }
484
485 void MG_ADAPT_OBJECT_i::setMeshIn(SMESH::SMESH_Mesh_ptr theMesh )
486 {
487         myMesh = SMESH::SMESH_Mesh::_duplicate(theMesh);
488 }
489 void MG_ADAPT_OBJECT_i::setMEDFileIn(const char* f)
490 {
491         medFileIn =  f;
492 }
493 void MG_ADAPT_OBJECT_i::setMEDFileOut(const char* f)
494 {
495         medFileOut = f;
496 }
497 void MG_ADAPT_OBJECT_i::setMEDFileBackground(const char* f)
498 {
499         medFileBackground = f;
500 }
501 void MG_ADAPT_OBJECT_i::AddHypothesis(SMESH::MG_ADAPT_ptr mg)
502 {
503
504         mg->setMedFileIn(medFileIn.c_str());
505         mg->setMedFileOut(medFileOut.c_str());
506         mg->setSizeMapFile(medFileBackground.c_str());
507         hypothesis = SMESH::MG_ADAPT::_duplicate(mg);
508 }
509 CORBA::Long MG_ADAPT_OBJECT_i::Compute(bool publish)
510 {
511         if(!checkMeshFileIn()){
512                 std::cerr<< "\n Error : Please check the MED file input or mesh input. \n";
513                 return -1;
514         }
515         hypothesis->setPublish(publish);
516         return hypothesis->compute();
517 }
518
519 bool MG_ADAPT_OBJECT_i::checkMeshFileIn()
520 {
521         bool ret = false; // 1 ok , 0 nook
522     if(!::MG_ADAPT::MgAdapt::isFileExist(medFileIn))
523     {
524         if(!myMesh->_is_nil())
525         {
526                 bool toOverwrite  = true;
527                         bool toFindOutDim = true;
528                         medFileIn = hypothesis->getFileName();
529                         medFileIn+= ".med";
530                         myMesh->ExportMED(medFileIn.c_str(), false, -1, toOverwrite, toFindOutDim);
531                         hypothesis->setMedFileIn(medFileIn.c_str());
532                         ret = true;
533                 }
534         }
535         else
536             ret = true;
537
538     return ret;
539 }