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