Salome HOME
TUI for mg-adapt
[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->setRankTimeStep(t, r);
250 }
251 CORBA::Long MG_ADAPT_i::getRank()
252 {
253         return myMgAdapt->getRank();
254 }
255 void MG_ADAPT_i::setTimeStepRankLast()
256 {
257         myMgAdapt->setTimeStepRankLast();
258 }
259 void MG_ADAPT_i::setNoTimeStep()
260 {
261         myMgAdapt->setNoTimeStep();
262 }
263 void MG_ADAPT_i::setLogFile(const char* str)
264 {
265         myMgAdapt->setLogFile(str);
266 }
267 char* MG_ADAPT_i::getLogFile()
268 {
269         return CORBA::string_dup(myMgAdapt->getLogFile().c_str());
270 }
271
272 void MG_ADAPT_i::setVerbosityLevel(CORBA::Long v)
273 {
274         myMgAdapt->setVerbosityLevel(v);
275 }
276 CORBA::Long MG_ADAPT_i::getVerbosityLevel()
277 {
278         return myMgAdapt->getVerbosityLevel();
279 }
280 void MG_ADAPT_i::setRemoveOnSuccess(bool mybool)
281 {
282         myMgAdapt->setRemoveOnSuccess(mybool);
283 }
284 bool MG_ADAPT_i::getRemoveOnSuccess()
285 {
286         myMgAdapt->getRemoveOnSuccess();
287 }
288 SMESH::MgAdaptHypothesisData* MG_ADAPT_i::getData() 
289 {
290         SMESH::MgAdaptHypothesisData* result = new      SMESH::MgAdaptHypothesisData();
291         ::MG_ADAPT::MgAdaptHypothesisData* from =  myMgAdapt->getData();
292         copyHypothesisDataFromImpl(from, result);       
293         return result;
294 }
295 void MG_ADAPT_i::setUseLocalMap(bool mybool)
296 {
297         myMgAdapt->setUseLocalMap(mybool);
298 }
299 bool MG_ADAPT_i::getUseLocalMap()
300 {
301         return myMgAdapt->getUseLocalMap();
302 }
303 void MG_ADAPT_i::setUseBackgroundMap(bool mybool)
304 {
305         myMgAdapt->setUseBackgroundMap(mybool);
306 }
307 bool MG_ADAPT_i::getUseBackgroundMap()
308 {
309         return myMgAdapt->getUseBackgroundMap();
310 }
311 void MG_ADAPT_i::setUseConstantValue(bool mybool)
312 {
313         myMgAdapt->setUseConstantValue(mybool);
314 }
315 bool MG_ADAPT_i::getUseConstantValue()
316 {
317         return myMgAdapt->getUseConstantValue();
318 }
319
320 void MG_ADAPT_i::setConstantSize(double value)
321 {
322         myMgAdapt->setConstantValue(value);
323 }
324 double MG_ADAPT_i::getConstantSize() 
325 {
326         return myMgAdapt->getConstantValue();
327 }
328 void MG_ADAPT_i::setSizeMapFile(const char* str)
329 {
330         myMgAdapt->setSizeMapFile(str);
331 }
332 char* MG_ADAPT_i::getSizeMapFile()
333 {
334         return CORBA::string_dup(myMgAdapt->getSizeMapFile().c_str());
335 }
336 void MG_ADAPT_i::setFromMedFile(bool mybool)
337 {
338         myMgAdapt->setFromMedFile(mybool);
339 }
340 bool MG_ADAPT_i::isFromMedFile()
341 {
342         return myMgAdapt->isFromMedFile();
343 }
344
345 void MG_ADAPT_i::setKeepWorkingFiles(bool mybool)
346 {
347         myMgAdapt->setKeepWorkingFiles(mybool);
348 }
349 bool MG_ADAPT_i::getKeepWorkingFiles()
350 {
351         return myMgAdapt->getKeepWorkingFiles();
352 }
353
354 //~void MG_ADAPT_i::setPrCORBA::LongLogInFile(bool);
355 //~bool MG_ADAPT_i::getPrCORBA::LongLogInFile();
356
357 void MG_ADAPT_i::setSizeMapType(const char* type)
358 {
359         setUseLocalMap(false);
360         setUseBackgroundMap(false);
361         setUseConstantValue(false);
362         
363         if (!strcmp("Local", type))
364                 setUseLocalMap(true);
365         else if (!strcmp("Background", type))
366             setUseBackgroundMap(true);
367         else 
368             setUseConstantValue(true);
369 }
370 void MG_ADAPT_i::setWorkingDir(const char* dir)
371 {
372         myMgAdapt->setWorkingDir(dir);
373 }
374 char* MG_ADAPT_i::getWorkingDir() 
375 {
376         return CORBA::string_dup(myMgAdapt->getWorkingDir().c_str());
377 }
378 bool MG_ADAPT_i::setAll()
379 {
380         return myMgAdapt->setAll();
381 }
382 char* MG_ADAPT_i::getCommandToRun()
383 {
384         return CORBA::string_dup(myMgAdapt->getCommandToRun().c_str());
385 }
386
387 //~CORBA::Long MG_ADAPT_i::compute(::CORBA::String_out errStr)
388 //~{
389         //~std::string err("");
390         //~CORBA::Long ret = myMgAdapt->compute(err);
391         //~errStr =  err.c_str();
392         //~return ret;
393 //~}
394 CORBA::Long MG_ADAPT_i::compute()
395 {
396         errStr = "";
397         CORBA::Long ret;
398         try
399     {
400             ret = myMgAdapt->compute(errStr);
401     }
402     catch (const std::exception& e)
403     {
404         std::cerr<<e.what();
405         ret = -1;
406     }
407         //~errStr =  err.c_str();
408         return ret;
409 }
410 char* MG_ADAPT_i::getErrMsg()
411 {
412         return CORBA::string_dup(errStr.c_str());
413 }
414 char* MG_ADAPT_i::getFileName() 
415 {
416         return CORBA::string_dup(myMgAdapt->getFileName().c_str());
417 }
418 char* MG_ADAPT_i::getExeName()
419 {
420         return CORBA::string_dup(myMgAdapt->getExeName().c_str());
421 }
422 void MG_ADAPT_i::copyMgAdaptHypothesisData( const SMESH::MgAdaptHypothesisData& data)
423 {
424         ::MG_ADAPT::MgAdaptHypothesisData* baseData = new ::MG_ADAPT::MgAdaptHypothesisData(); 
425         copyHypothesisDataToImpl(data, baseData);
426         myMgAdapt->copyMgAdaptHypothesisData(baseData);
427         delete baseData;
428 }
429
430 //~void MG_ADAPT_i::checkDirPath(char*& str)
431 //~{
432         //~myMgAdapt->checkDirPath(str);
433 //~}
434
435 bool MG_ADAPT_i::hasOptionDefined( const char* optionName ) 
436 {
437         return myMgAdapt->hasOptionDefined(optionName);
438 }
439 void MG_ADAPT_i::setOptionValue(const char* optionName,
440                                         const char* optionValue) throw (std::invalid_argument)
441 {
442         myMgAdapt->setOptionValue(optionName, optionValue);
443 }                                       
444
445 char* MG_ADAPT_i::getOptionValue(const char* optionName,
446                                                    bool&              isDefault)  throw (std::invalid_argument)
447 {
448     return CORBA::string_dup(myMgAdapt->getOptionValue(optionName, &isDefault).c_str());
449 }
450 str_array* MG_ADAPT_i::getCustomOptionValuesStrVec() 
451 {
452         SMESH::str_array_var result = new SMESH::str_array();
453         std::vector <std::string> vals = myMgAdapt->getCustomOptionValuesStrVec();
454         result->length(vals.size());
455         for (int i = 0; i<vals.size(); i++) result[i] = CORBA::string_dup(vals[i].c_str());
456         return result._retn();
457 }
458 str_array*  MG_ADAPT_i::getOptionValuesStrVec() 
459 {
460
461         SMESH::str_array_var result = new SMESH::str_array();
462         std::vector <std::string> vals = myMgAdapt->getOptionValuesStrVec();
463         result->length(vals.size());
464         for (int i = 0; i<vals.size(); i++) result[i] = CORBA::string_dup(vals[i].c_str());
465         return result._retn();
466 }
467
468 void MG_ADAPT_i::setPrintLogInFile(bool mybool)
469 {
470         myMgAdapt->setPrintLogInFile(mybool);
471 }
472 bool MG_ADAPT_i::getPrintLogInFile()
473 {
474         return myMgAdapt->getPrintLogInFile();
475 }
476 //~TOptionValues        MG_ADAPT_i::getOptionValues()       const;
477 //~const TOptionValues& MG_ADAPT_i::getCustomOptionValues() const ;
478
479 MG_ADAPT_OBJECT_i::MG_ADAPT_OBJECT_i(): SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
480 {
481     medFileIn="";
482     medFileOut="";
483     medFileBackground="";
484     publish = false;
485     //~myMesh = CORBA::nil;
486 }
487
488 void MG_ADAPT_OBJECT_i::setMeshIn(SMESH::SMESH_Mesh_ptr theMesh )
489 {
490         myMesh = SMESH::SMESH_Mesh::_duplicate(theMesh);
491 }
492 void MG_ADAPT_OBJECT_i::setMEDFileIn(const char* f)
493 {
494         medFileIn =  f;
495 }
496 void MG_ADAPT_OBJECT_i::setMEDFileOut(const char* f)
497 {
498         medFileOut = f;
499 }
500 void MG_ADAPT_OBJECT_i::setMEDFileBackground(const char* f)
501 {
502         medFileBackground = f;
503 }
504 void MG_ADAPT_OBJECT_i::AddHypothesis(SMESH::MG_ADAPT_ptr mg)
505 {
506         
507         mg->setMedFileIn(medFileIn.c_str());
508         mg->setMedFileOut(medFileOut.c_str());
509         mg->setSizeMapFile(medFileBackground.c_str());
510         hypothesis = SMESH::MG_ADAPT::_duplicate(mg);
511 }
512 CORBA::Long MG_ADAPT_OBJECT_i::Compute(bool publish)
513 {
514         if(!checkMeshFileIn()){
515                 std::cerr<< "\n Error : Please check the MED file input or mesh input. \n";
516                 return -1;
517         }
518         hypothesis->setPublish(publish);
519         return hypothesis->compute();   
520 }       
521
522 bool MG_ADAPT_OBJECT_i::checkMeshFileIn()
523 {
524         bool ret = false; // 1 ok , 0 nook
525     if(!::MG_ADAPT::MgAdapt::isFileExist(medFileIn))
526     {
527         if(!myMesh->_is_nil())
528         {
529                 bool toOverwrite  = true;
530                         bool toFindOutDim = true;
531                         medFileIn = hypothesis->getFileName();
532                         medFileIn+= ".med";
533                         myMesh->ExportMED(medFileIn.c_str(), false, -1, toOverwrite, toFindOutDim);
534                         hypothesis->setMedFileIn(medFileIn.c_str());
535                         ret = true; 
536                 }
537         }
538         else
539             ret = true;
540             
541     return ret;
542 }