]> SALOME platform Git repositories - plugins/ghs3dplugin.git/blob - src/GHS3DPlugin_Hypothesis.cxx
Salome HOME
windows port
[plugins/ghs3dplugin.git] / src / GHS3DPlugin_Hypothesis.cxx
1 //  Copyright (C) 2004-2008  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.
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 // File      : GHS3DPlugin_Hypothesis.cxx
21 // Created   : Wed Apr  2 12:36:29 2008
22 // Author    : Edward AGAPOV (eap)
23 //=============================================================================
24 //
25 #include "GHS3DPlugin_Hypothesis.hxx"
26
27 #include <TCollection_AsciiString.hxx>
28
29 #ifdef WNT
30 #include <process.h>
31 #define getpid _getpid
32 #endif
33
34 //=======================================================================
35 //function : GHS3DPlugin_Hypothesis
36 //=======================================================================
37
38 GHS3DPlugin_Hypothesis::GHS3DPlugin_Hypothesis(int hypId, int studyId, SMESH_Gen * gen)
39   : SMESH_Hypothesis(hypId, studyId, gen)
40 {
41   _name = "GHS3D_Parameters";
42   _param_algo_dim = 3;
43
44   myToMeshHoles                  = DefaultMeshHoles();        
45   myMaximumMemory                = -1;//DefaultMaximumMemory();    
46   myInitialMemory                = -1;//DefaultInitialMemory();    
47   myOptimizationLevel            = DefaultOptimizationLevel();
48   myWorkingDirectory             = DefaultWorkingDirectory(); 
49   myKeepFiles                    = DefaultKeepFiles();
50   myVerboseLevel                 = DefaultVerboseLevel();
51   myToCreateNewNodes             = DefaultToCreateNewNodes();
52   myToUseBoundaryRecoveryVersion = DefaultToUseBoundaryRecoveryVersion();
53 }
54
55 //=======================================================================
56 //function : SetToMeshHoles
57 //=======================================================================
58
59 void GHS3DPlugin_Hypothesis::SetToMeshHoles(bool toMesh)
60 {
61   if ( myToMeshHoles != toMesh ) {
62     myToMeshHoles = toMesh;
63     NotifySubMeshesHypothesisModification();
64   }
65 }
66
67 //=======================================================================
68 //function : GetToMeshHoles
69 //=======================================================================
70
71 bool GHS3DPlugin_Hypothesis::GetToMeshHoles(bool checkFreeOption) const
72 {
73   if (checkFreeOption && !myTextOption.empty()) {
74     if ( myTextOption.find("-c 0"))
75       return true;
76     if ( myTextOption.find("-c 1"))
77       return false;
78   }
79   return myToMeshHoles;
80 }
81
82 //=======================================================================
83 //function : SetMaximumMemory
84 //=======================================================================
85
86 void GHS3DPlugin_Hypothesis::SetMaximumMemory(short MB)
87 {
88   if ( myMaximumMemory != MB ) {
89     myMaximumMemory = MB;
90     NotifySubMeshesHypothesisModification();
91   }
92 }
93
94 //=======================================================================
95 //function : GetMaximumMemory
96 //           * automatic memory adjustment mode. Default is zero
97 //=======================================================================
98
99 short GHS3DPlugin_Hypothesis::GetMaximumMemory() const
100 {
101   return myMaximumMemory;
102 }
103
104 //=======================================================================
105 //function : SetInitialMemory
106 //=======================================================================
107
108 void GHS3DPlugin_Hypothesis::SetInitialMemory(short MB)
109 {
110   if ( myInitialMemory != MB ) {
111     myInitialMemory = MB;
112     NotifySubMeshesHypothesisModification();
113   }
114 }
115
116 //=======================================================================
117 //function : GetInitialMemory
118 //=======================================================================
119
120 short GHS3DPlugin_Hypothesis::GetInitialMemory() const
121 {
122   return myInitialMemory;
123 }
124
125 //=======================================================================
126 //function : SetOptimizationLevel
127 //=======================================================================
128
129 void GHS3DPlugin_Hypothesis::SetOptimizationLevel(OptimizationLevel level)
130 {
131   if ( myOptimizationLevel != level ) {
132     myOptimizationLevel = level;
133     NotifySubMeshesHypothesisModification();
134   }
135 }
136
137 //=======================================================================
138 //function : GetOptimizationLevel
139 //=======================================================================
140
141 GHS3DPlugin_Hypothesis::OptimizationLevel GHS3DPlugin_Hypothesis::GetOptimizationLevel() const
142 {
143   return (OptimizationLevel) myOptimizationLevel;
144 }
145
146 //=======================================================================
147 //function : SetWorkingDirectory
148 //=======================================================================
149
150 void GHS3DPlugin_Hypothesis::SetWorkingDirectory(const string& path)
151 {
152   if ( myWorkingDirectory != path ) {
153     myWorkingDirectory = path;
154     NotifySubMeshesHypothesisModification();
155   }
156 }
157
158 //=======================================================================
159 //function : GetWorkingDirectory
160 //=======================================================================
161
162 string GHS3DPlugin_Hypothesis::GetWorkingDirectory() const
163 {
164   return myWorkingDirectory;
165 }
166
167 //=======================================================================
168 //function : SetKeepFiles
169 //=======================================================================
170
171 void GHS3DPlugin_Hypothesis::SetKeepFiles(bool toKeep)
172 {
173   if ( myKeepFiles != toKeep ) {
174     myKeepFiles = toKeep;
175     NotifySubMeshesHypothesisModification();
176   }
177 }
178
179 //=======================================================================
180 //function : GetKeepFiles
181 //=======================================================================
182
183 bool GHS3DPlugin_Hypothesis::GetKeepFiles() const
184 {
185   return myKeepFiles;
186 }
187
188 //=======================================================================
189 //function : SetVerboseLevel
190 //=======================================================================
191
192 void GHS3DPlugin_Hypothesis::SetVerboseLevel(short level)
193 {
194   if ( myVerboseLevel != level ) {
195     myVerboseLevel = level;
196     NotifySubMeshesHypothesisModification();
197   }
198 }
199
200 //=======================================================================
201 //function : GetVerboseLevel
202 //=======================================================================
203
204 short GHS3DPlugin_Hypothesis::GetVerboseLevel() const
205 {
206   return myVerboseLevel;
207 }
208
209 //=======================================================================
210 //function : SetToCreateNewNodes
211 //=======================================================================
212
213 void GHS3DPlugin_Hypothesis::SetToCreateNewNodes(bool toCreate)
214 {
215   if ( myToCreateNewNodes != toCreate ) {
216     myToCreateNewNodes = toCreate;
217     NotifySubMeshesHypothesisModification();
218   }
219 }
220
221 //=======================================================================
222 //function : GetToCreateNewNodes
223 //=======================================================================
224
225 bool GHS3DPlugin_Hypothesis::GetToCreateNewNodes() const
226 {
227   return myToCreateNewNodes;
228 }
229
230 //=======================================================================
231 //function : SetToUseBoundaryRecoveryVersion
232 //=======================================================================
233
234 void GHS3DPlugin_Hypothesis::SetToUseBoundaryRecoveryVersion(bool toUse)
235 {
236   if ( myToUseBoundaryRecoveryVersion != toUse ) {
237     myToUseBoundaryRecoveryVersion = toUse;
238     NotifySubMeshesHypothesisModification();
239   }
240 }
241
242 //=======================================================================
243 //function : GetToUseBoundaryRecoveryVersion
244 //=======================================================================
245
246 bool GHS3DPlugin_Hypothesis::GetToUseBoundaryRecoveryVersion() const
247 {
248   return myToUseBoundaryRecoveryVersion;
249 }
250
251 //=======================================================================
252 //function : SetTextOption
253 //=======================================================================
254
255 void GHS3DPlugin_Hypothesis::SetTextOption(const string& option)
256 {
257   if ( myTextOption != option ) {
258     myTextOption = option;
259     NotifySubMeshesHypothesisModification();
260   }
261 }
262
263 //=======================================================================
264 //function : GetTextOption
265 //=======================================================================
266
267 string GHS3DPlugin_Hypothesis::GetTextOption() const
268 {
269   return myTextOption;
270 }
271
272
273 //=======================================================================
274 //function : DefaultMeshHoles
275 //=======================================================================
276
277 bool GHS3DPlugin_Hypothesis::DefaultMeshHoles()
278 {
279   return false; // PAL19680
280 }
281
282 //=======================================================================
283 //function : DefaultMaximumMemory
284 //=======================================================================
285
286 #ifndef WIN32
287 #include <sys/sysinfo.h>
288 #else
289 #include <windows.h>
290 #endif
291
292 short  GHS3DPlugin_Hypothesis::DefaultMaximumMemory()
293 {
294 #ifndef WIN32
295   struct sysinfo si;
296   int err = sysinfo( &si );
297   if ( err == 0 ) {
298     int ramMB = si.totalram * si.mem_unit / 1024 / 1024;
299     return (short) ( 0.7 * ramMB );
300   }
301 #else
302   // See http://msdn.microsoft.com/en-us/library/aa366589.aspx
303   MEMORYSTATUSEX statex;
304   statex.dwLength = sizeof (statex);
305   int err = GlobalMemoryStatusEx (&statex);
306   if (err != 0) {
307     int totMB = 
308       statex.ullTotalPhys / 1024 / 1024 +
309       statex.ullTotalPageFile / 1024 / 1024 +
310       statex.ullTotalVirtual / 1024 / 1024;
311     return (int) ( 0.7 * totMB );
312   }
313 #endif
314 }
315
316 //=======================================================================
317 //function : DefaultInitialMemory
318 //=======================================================================
319
320 short  GHS3DPlugin_Hypothesis::DefaultInitialMemory()
321 {
322   return DefaultMaximumMemory();
323 }
324
325 //=======================================================================
326 //function : DefaultOptimizationLevel
327 //=======================================================================
328
329 short  GHS3DPlugin_Hypothesis::DefaultOptimizationLevel()
330 {
331   return Medium;
332 }
333
334 //=======================================================================
335 //function : DefaultWorkingDirectory
336 //=======================================================================
337
338 string GHS3DPlugin_Hypothesis::DefaultWorkingDirectory()
339 {
340   TCollection_AsciiString aTmpDir;
341
342   char *Tmp_dir = getenv("SALOME_TMP_DIR");
343   if(Tmp_dir != NULL) {
344     aTmpDir = Tmp_dir;
345   }
346   else {
347 #ifdef WIN32
348     aTmpDir = TCollection_AsciiString("C:\\");
349 #else
350     aTmpDir = TCollection_AsciiString("/tmp/");
351 #endif
352   }
353   return aTmpDir.ToCString();
354 }
355
356 //=======================================================================
357 //function : DefaultKeepFiles
358 //=======================================================================
359
360 bool   GHS3DPlugin_Hypothesis::DefaultKeepFiles()
361 {
362   return false;
363 }
364
365 //=======================================================================
366 //function : DefaultVerboseLevel
367 //=======================================================================
368
369 short  GHS3DPlugin_Hypothesis::DefaultVerboseLevel()
370 {
371   return 10;
372 }
373
374 //=======================================================================
375 //function : DefaultToCreateNewNodes
376 //=======================================================================
377
378 bool GHS3DPlugin_Hypothesis::DefaultToCreateNewNodes()
379 {
380   return true;
381 }
382
383 //=======================================================================
384 //function : DefaultToUseBoundaryRecoveryVersion
385 //=======================================================================
386
387 bool GHS3DPlugin_Hypothesis::DefaultToUseBoundaryRecoveryVersion()
388 {
389   return false;
390 }
391
392 //=======================================================================
393 //function : SaveTo
394 //=======================================================================
395
396 ostream & GHS3DPlugin_Hypothesis::SaveTo(ostream & save)
397 {
398   save << (int) myToMeshHoles                 << " ";
399   save << myMaximumMemory                     << " ";
400   save << myInitialMemory                     << " ";
401   save << myOptimizationLevel                 << " ";
402   save << myWorkingDirectory                  << " ";
403   save << (int)myKeepFiles                    << " ";
404   save << myVerboseLevel                      << " ";
405   save << (int)myToCreateNewNodes             << " ";
406   save << (int)myToUseBoundaryRecoveryVersion << " ";
407   save << myTextOption                        << " ";
408   return save;
409 }
410
411 //=======================================================================
412 //function : LoadFrom
413 //=======================================================================
414
415 istream & GHS3DPlugin_Hypothesis::LoadFrom(istream & load)
416 {
417   bool isOK = true;
418   int i;
419
420   isOK = (load >> i);
421   if (isOK)
422     myToMeshHoles = i;
423   else
424     load.clear(ios::badbit | load.rdstate());
425
426   isOK = (load >> i);
427   if (isOK)
428     myMaximumMemory = i;
429   else
430     load.clear(ios::badbit | load.rdstate());
431
432   isOK = (load >> i);
433   if (isOK)
434     myInitialMemory = i;
435   else
436     load.clear(ios::badbit | load.rdstate());
437
438   isOK = (load >> i);
439   if (isOK)
440     myOptimizationLevel = i;
441   else
442     load.clear(ios::badbit | load.rdstate());
443
444   isOK = (load >> myWorkingDirectory);
445   if (isOK) {
446     if ( myWorkingDirectory == "0") { // myWorkingDirectory was empty
447       myKeepFiles = false;
448       myWorkingDirectory.clear();
449     }
450     else if ( myWorkingDirectory == "1" ) {
451       myKeepFiles = true;
452       myWorkingDirectory.clear();
453     }
454   }
455   else
456     load.clear(ios::badbit | load.rdstate());
457
458   if ( !myWorkingDirectory.empty() ) {
459     isOK = (load >> i);
460     if (isOK)
461       myKeepFiles = i;
462     else
463       load.clear(ios::badbit | load.rdstate());
464   }
465
466   isOK = (load >> i);
467   if (isOK)
468     myVerboseLevel = (short) i;
469   else
470     load.clear(ios::badbit | load.rdstate());
471
472   isOK = (load >> i);
473   if (isOK)
474     myToCreateNewNodes = (bool) i;
475   else
476     load.clear(ios::badbit | load.rdstate());
477
478   isOK = (load >> i);
479   if (isOK)
480     myToUseBoundaryRecoveryVersion = (bool) i;
481   else
482     load.clear(ios::badbit | load.rdstate());
483
484   isOK = (load >> myTextOption);
485   while (isOK) {
486     string txt;
487     if (load >> txt) {
488       myTextOption += " ";
489       myTextOption += txt;
490     }
491     else
492       isOK = false;
493   }
494 //   else
495 //     load.clear(ios::badbit | load.rdstate());
496
497   return load;
498 }
499
500 //=======================================================================
501 //function : SetParametersByMesh
502 //=======================================================================
503
504 bool GHS3DPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* ,const TopoDS_Shape&)
505 {
506   return false;
507 }
508
509
510 //================================================================================
511 /*!
512  * \brief Return false
513  */
514 //================================================================================
515
516 bool GHS3DPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  /*dflts*/,
517                                                      const SMESH_Mesh* /*theMesh*/)
518 {
519   return false;
520 }
521
522 //================================================================================
523 /*!
524  * \brief Return command to run ghs3d mesher excluding file prefix (-f)
525  */
526 //================================================================================
527
528 string GHS3DPlugin_Hypothesis::CommandToRun(const GHS3DPlugin_Hypothesis* hyp,
529                                             const bool                    hasShapeToMesh)
530 {
531   TCollection_AsciiString cmd( "ghs3d" );
532   // check if any option is overridden by hyp->myTextOption
533   bool m = hyp ? ( hyp->myTextOption.find("-m") == string::npos ) : true;
534   bool M = hyp ? ( hyp->myTextOption.find("-M") == string::npos ) : true;
535   bool c = hyp ? ( hyp->myTextOption.find("-c") == string::npos ) : true;
536   bool o = hyp ? ( hyp->myTextOption.find("-o") == string::npos ) : true;
537   bool p0= hyp ? ( hyp->myTextOption.find("-p0")== string::npos ) : true;
538   bool C = hyp ? ( hyp->myTextOption.find("-C") == string::npos ) : true;
539   bool v = hyp ? ( hyp->myTextOption.find("-v") == string::npos ) : true;
540
541   // if use boundary recovery version, few options are allowed
542   bool useBndRecovery = !C;
543   if ( !useBndRecovery && hyp )
544     useBndRecovery = hyp->myToUseBoundaryRecoveryVersion;
545
546   // ghs3d needs to know amount of memory it may use (MB).
547   // Default memory is defined at ghs3d installation but it may be not enough,
548   // so allow to use about all available memory
549   if ( m ) {
550     short aMaximumMemory = hyp ? hyp->myMaximumMemory : -1;
551     cmd += " -m ";
552     if ( aMaximumMemory < 0 )
553       cmd += DefaultMaximumMemory();
554     else
555       cmd += aMaximumMemory;
556   }
557   if ( M && !useBndRecovery ) {
558     short aInitialMemory = hyp ? hyp->myInitialMemory : -1;
559     cmd += " -M ";
560     if ( aInitialMemory > 0 )
561       cmd += aInitialMemory;
562     else
563       cmd += "100";
564   }
565   // component to mesh
566   // 0 , all components to be meshed
567   // 1 , only the main ( outermost ) component to be meshed
568   if ( c && !useBndRecovery ) {
569     // We always run GHS3D with "to mesh holes'==TRUE (see PAL19680)
570     if ( hasShapeToMesh )
571       cmd += " -c 0";
572     else {
573       bool aToMeshHoles = hyp ? hyp->myToMeshHoles : DefaultMeshHoles();
574       if ( aToMeshHoles )
575         cmd += " -c 0";
576       else
577         cmd += " -c 1";
578     }
579   }
580
581   // optimization level
582   if ( o && hyp && !useBndRecovery ) {
583     if ( hyp->myOptimizationLevel >= 0 && hyp->myOptimizationLevel < 4 ) {
584       char* level[] = { "none" , "light" , "standard" , "strong" };
585       cmd += " -o ";
586       cmd += level[ hyp->myOptimizationLevel ];
587     }
588   }
589
590   // to create internal nodes
591   if ( p0 && hyp && !hyp->myToCreateNewNodes ) {
592     cmd += " -p0";
593   }
594
595   // verbose mode
596   if ( v && hyp ) {
597     cmd += " -v ";
598     cmd += hyp->myVerboseLevel;
599   }
600
601   // boundary recovery version
602   if ( useBndRecovery ) {
603     cmd += " -C";
604   }
605
606   // options as text
607   if ( hyp && !hyp->myTextOption.empty() ) {
608     cmd += " ";
609     cmd += (char*) hyp->myTextOption.c_str();
610   }
611
612 #ifdef WNT
613   cmd += " < NUL";
614 #endif
615
616   return cmd.ToCString();
617 }
618
619 //================================================================================
620 /*!
621  * \brief Return a unique file name
622  */
623 //================================================================================
624
625 string GHS3DPlugin_Hypothesis::GetFileName(const GHS3DPlugin_Hypothesis* hyp)
626 {
627   string aTmpDir = hyp ? hyp->GetWorkingDirectory() : DefaultWorkingDirectory();
628   const char lastChar = *aTmpDir.rbegin();
629 #ifdef WIN32
630     if(lastChar != '\\') aTmpDir+='\\';
631 #else
632     if(lastChar != '/') aTmpDir+='/';
633 #endif      
634
635   TCollection_AsciiString aGenericName = (char*)aTmpDir.c_str();
636   aGenericName += "GHS3D_";
637   aGenericName += getpid();
638   aGenericName += "_";
639   aGenericName += Abs((Standard_Integer)(long) aGenericName.ToCString());
640
641   return aGenericName.ToCString();
642 }