Salome HOME
Updated copyright comment
[plugins/ghs3dprlplugin.git] / src / GHS3DPRLPlugin / GHS3DPRLPlugin_Hypothesis.cxx
1 // Copyright (C) 2007-2024  CEA, EDF
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 // ---
21 // File   : GHS3DPRLPlugin_Hypothesis.cxx
22 // Author : Christian VAN WAMBEKE (CEA) (from Hexotic plugin Lioka RAZAFINDRAZAKA)
23 // ---
24 //
25 #include "GHS3DPRLPlugin_Hypothesis.hxx"
26 #include <Basics_DirUtils.hxx>
27 #include <SMESH_File.hxx>
28 #include <utilities.h>
29 #include <unistd.h>
30
31 #ifdef WIN32
32 #include <process.h>
33 #define getpid _getpid
34 #endif
35
36 //=============================================================================
37 /*!
38  *
39  */
40 //=============================================================================
41 GHS3DPRLPlugin_Hypothesis::GHS3DPRLPlugin_Hypothesis (int hypId, SMESH_Gen * gen)
42   : SMESH_Hypothesis(hypId, gen),
43     _MEDName( GetDefaultMEDName() ),
44     _NbPart( GetDefaultNbPart() ),
45     _KeepFiles( GetDefaultKeepFiles() ),
46     _Background( GetDefaultBackground() ),
47     _Multithread( GetDefaultMultithread() ),
48     //_ToMergeSubdomains( GetDefaultToMergeSubdomains() ),
49     _Gradation( GetDefaultGradation() ),
50     _MinSize( GetDefaultMinSize() ),
51     _MaxSize( GetDefaultMaxSize() )
52 {
53   MESSAGE("GHS3DPRLPlugin_Hypothesis::GHS3DPRLPlugin_Hypothesis");
54   _name = GetHypType();
55   _param_algo_dim = 3;
56 }
57
58 //=======================================================================
59 //function : DefaultWorkingDirectory
60 //=======================================================================
61
62 std::string GHS3DPRLPlugin_Hypothesis::DefaultWorkingDirectory()
63 {
64   TCollection_AsciiString aTmpDir;
65
66   char *Tmp_dir = getenv("SALOME_TMP_DIR");
67   if(Tmp_dir != NULL) {
68     aTmpDir = Tmp_dir;
69   }
70   else {
71 #ifdef WIN32
72     aTmpDir = TCollection_AsciiString("C:\\");
73 #else
74     aTmpDir = TCollection_AsciiString("/tmp/");
75 #endif
76   }
77   return aTmpDir.ToCString();
78 }
79
80 //================================================================================
81 /*!
82  * \brief Return a unique file name
83  */
84 //================================================================================
85
86 std::string GHS3DPRLPlugin_Hypothesis::GetFileName(const GHS3DPRLPlugin_Hypothesis* hyp)
87 {
88   std::string aTmpDir = DefaultWorkingDirectory();
89   if ( !SMESH_File( aTmpDir ).exists() )
90     aTmpDir = Kernel_Utils::GetTmpDirByPath( aTmpDir );
91
92   const char lastChar = *aTmpDir.rbegin();
93 #ifdef WIN32
94     if(lastChar != '\\') aTmpDir+='\\';
95 #else
96     if(lastChar != '/') aTmpDir+='/';
97 #endif
98
99   TCollection_AsciiString aGenericName = (char*)aTmpDir.c_str();
100   aGenericName += "MGTETRAHPC_";
101   aGenericName += getpid();
102   aGenericName += "_";
103   aGenericName += Abs((Standard_Integer)(long) aGenericName.ToCString());
104
105   return aGenericName.ToCString();
106 }
107
108 //=============================================================================
109 /*!
110  *
111  */
112 //=============================================================================
113 static std::string cutOrReplaceBlancs(std::string theIn)
114 {
115   // cut all blancs at the beginning and at the end of the string,
116   // replace each blancs sequence of maximum length inside the string by one '_' symbol,
117   // as blancs consider: 9 (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR) and 32 (Space)
118   int len = theIn.length();
119   int lastgood = 0;
120   const char* str1 = theIn.c_str();
121   char* str2 = new char [len + 1];
122   bool del = true;
123
124   for (int i = 0, j = 0; i < len; i++)
125   {
126     unsigned short ucs = (unsigned short)(str1[i]);
127     if ((9 <= ucs && ucs <= 13) || ucs == 32)
128     {
129       if (!del)
130       {
131         str2[j++] = '_';
132         del = true;
133       }
134     }
135     else
136     {
137       str2[j++] = str1[i];
138       lastgood = j;
139       del = false;
140     }
141   }
142   str2[lastgood] = '\0';
143   std::string anOut = str2;
144   return anOut;
145 }
146
147 void GHS3DPRLPlugin_Hypothesis::SetMEDName(std::string theVal) {
148   //without whitespaces! ..from python?
149   std::string tmp1 = cutOrReplaceBlancs(theVal);
150   std::string tmp2 = _MEDName;
151   if (tmp1 != tmp2) {
152     _MEDName = tmp1.c_str();
153     NotifySubMeshesHypothesisModification();
154   }
155 }
156
157 void GHS3DPRLPlugin_Hypothesis::SetNbPart(int theVal) {
158   if (theVal != _NbPart) {
159     _NbPart = theVal;
160     NotifySubMeshesHypothesisModification();
161   }
162 }
163
164 void GHS3DPRLPlugin_Hypothesis::SetKeepFiles(bool theVal) {
165   if (theVal != _KeepFiles) {
166     _KeepFiles = theVal;
167     NotifySubMeshesHypothesisModification();
168   }
169 }
170
171 void GHS3DPRLPlugin_Hypothesis::SetBackground(bool theVal) {
172   if (theVal != _Background) {
173     _Background = theVal;
174     NotifySubMeshesHypothesisModification();
175   }
176 }
177
178 void GHS3DPRLPlugin_Hypothesis::SetMultithread(bool theVal) {
179   if (theVal != _Multithread) {
180     _Multithread = theVal;
181     NotifySubMeshesHypothesisModification();
182   }
183 }
184
185 //void GHS3DPRLPlugin_Hypothesis::SetToMergeSubdomains(bool theVal) {
186 //  if (theVal != _ToMergeSubdomains) {
187 //    _ToMergeSubdomains = theVal;
188 //    NotifySubMeshesHypothesisModification();
189 //  }
190 //}
191
192 void GHS3DPRLPlugin_Hypothesis::SetGradation(float theVal) {
193   if (theVal != _Gradation) {
194     _Gradation = theVal;
195     NotifySubMeshesHypothesisModification();
196   }
197 }
198
199 void GHS3DPRLPlugin_Hypothesis::SetMinSize(float theVal) {
200   if (theVal != _MinSize) {
201     _MinSize = theVal;
202     NotifySubMeshesHypothesisModification();
203   }
204 }
205
206 void GHS3DPRLPlugin_Hypothesis::SetMaxSize(float theVal) {
207   if (theVal != _MaxSize) {
208     _MaxSize = theVal;
209     NotifySubMeshesHypothesisModification();
210   }
211 }
212
213 void GHS3DPRLPlugin_Hypothesis::SetAdvancedOption(const char* theOptAndVals )
214 {
215   if ( _AdvOptions != theOptAndVals )
216   {
217     _AdvOptions = theOptAndVals;
218     NotifySubMeshesHypothesisModification();
219   }
220 }
221
222
223
224 //=============================================================================
225 /*!
226  *
227  */
228 //=============================================================================
229 std::ostream& GHS3DPRLPlugin_Hypothesis::SaveTo(std::ostream& save)
230 {
231   //explicit outputs for future code compatibility of saved .hdf
232   //save without any whitespaces!
233   save<<"MEDName="<<_MEDName<<";";
234   save<<"NbPart="<<_NbPart<<";";
235   //save<<"ToMeshHoles="<<(int) _ToMeshHoles<<";";
236   //save<<"ToMergeSubdomains="<<(int) _ToMergeSubdomains<<";";
237   save<<"Gradation="<<(float) _Gradation<<";";
238   save<<"MinSize="<<(float) _MinSize<<";";
239   save<<"MaxSize="<<(float) _MaxSize<<";";
240   save<<"KeepFiles="<<(int) _KeepFiles<<";";
241   save<<"Background="<<(int) _Background<<";";
242   save<<"Multithread="<<(int) _Multithread<<";";
243   save<<" " << _AdvOptions.size() << " " << _AdvOptions;
244   return save;
245 }
246
247 //=============================================================================
248 /*!
249  *
250  */
251 //=============================================================================
252 std::istream& GHS3DPRLPlugin_Hypothesis::LoadFrom(std::istream& load)
253 {
254    //explicit inputs for future code compatibility of saved .hdf
255    bool isOK = true;
256    std::string str1,str2,str3,str4;
257
258    //save without any whitespaces!
259    isOK = static_cast<bool>(load >> str1);
260    if (!(isOK)) {
261      //defaults values assumed
262      load.clear(std::ios::badbit | load.rdstate());
263      return load;
264    }
265    int pos = 0;
266    int len = str1.length();
267    while (pos < len) {
268      int found = str1.find(';',pos);
269      str2 = str1.substr(pos,found-pos);
270      int eqpos = str2.find('=',0);
271      str3 = str2.substr(0,eqpos);
272      str4 = str2.substr(eqpos+1);
273      pos = found + 1;
274
275      if (str3=="MEDName")     _MEDName     = str4.c_str();
276      if (str3=="NbPart")      _NbPart      = atoi(str4.c_str());
277      if (str3=="KeepFiles")   _KeepFiles   = (bool) atoi(str4.c_str());
278      if (str3=="Gradation")   _Gradation   = atof(str4.c_str());
279      if (str3=="MinSize")     _MinSize     = atof(str4.c_str());
280      if (str3=="MaxSize")     _MaxSize     = atof(str4.c_str());
281      if (str3=="Background")  _Background  = (bool) atoi(str4.c_str());
282      if (str3=="Multithread") _Multithread = (bool) atoi(str4.c_str());
283    }
284
285    len = 0;
286    isOK = static_cast<bool>(load >> len >> std::ws);
287    if ( isOK && len > 0 )
288    {
289      _AdvOptions.resize( len );
290      load.get( &_AdvOptions[0], len + 1);
291    }
292    return load;
293 }
294
295 //=============================================================================
296 /*!
297  *
298  */
299 //=============================================================================
300 std::ostream& operator <<(std::ostream& save, GHS3DPRLPlugin_Hypothesis& hyp)
301 {
302   return hyp.SaveTo( save );
303 }
304
305 //=============================================================================
306 /*!
307  *
308  */
309 //=============================================================================
310 std::istream& operator >>(std::istream& load, GHS3DPRLPlugin_Hypothesis& hyp)
311 {
312   return hyp.LoadFrom( load );
313 }
314
315
316 //================================================================================
317 /*!
318  * \brief Does nothing
319  * \param theMesh - the built mesh
320  * \param theShape - the geometry of interest
321  * \retval bool - always false
322  */
323 //================================================================================
324 bool GHS3DPRLPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   /*theMesh*/,
325                                                     const TopoDS_Shape& /*theShape*/)
326 {
327   return false;
328 }
329 //================================================================================
330 /*!
331  * \brief Initialize my parameter values by default parameters.
332  *  \retval bool - true if parameter values have been successfully defined
333  */
334 //================================================================================
335
336 bool GHS3DPRLPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  /*dflts*/,
337                                                         const SMESH_Mesh* /*theMesh*/)
338 {
339   return false;
340 }
341
342 //=============================================================================
343 std::string GHS3DPRLPlugin_Hypothesis::GetDefaultMEDName()
344 {
345   return "DOMAIN\0";
346 }
347
348 //=============================================================================
349 int GHS3DPRLPlugin_Hypothesis::GetDefaultNbPart()
350 {
351   return 8;
352 }
353
354 //=============================================================================
355 bool GHS3DPRLPlugin_Hypothesis::GetDefaultKeepFiles()
356 {
357   return false;
358 }
359
360 //=============================================================================
361 bool GHS3DPRLPlugin_Hypothesis::GetDefaultBackground()
362 {
363   return false;
364 }
365
366 //=============================================================================
367 bool GHS3DPRLPlugin_Hypothesis::GetDefaultMultithread()
368 {
369   return false;
370 }
371
372 //=============================================================================
373 //bool GHS3DPRLPlugin_Hypothesis::GetDefaultToMeshHoles()
374 //{
375 //  return false;
376 //}
377
378 //=============================================================================
379 //bool GHS3DPRLPlugin_Hypothesis::GetDefaultToMergeSubdomains()
380 //{
381 //  return false;
382 //}
383
384 //=============================================================================
385 float GHS3DPRLPlugin_Hypothesis::GetDefaultGradation()
386 {
387   return 1.05;
388 }
389 //=============================================================================
390 float GHS3DPRLPlugin_Hypothesis::GetDefaultMinSize()
391 {
392   return 0.;
393 }
394 //=============================================================================
395 float GHS3DPRLPlugin_Hypothesis::GetDefaultMaxSize()
396 {
397   return 0.;
398 }
399