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