1 // Copyright (C) 2004-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //=============================================================================
20 // File : GHS3DPlugin_Hypothesis.cxx
21 // Created : Wed Apr 2 12:36:29 2008
22 // Author : Edward AGAPOV (eap)
23 //=============================================================================
25 #include "GHS3DPlugin_Hypothesis.hxx"
27 #include <TCollection_AsciiString.hxx>
31 #define getpid _getpid
34 //=======================================================================
35 //function : GHS3DPlugin_Hypothesis
36 //=======================================================================
38 GHS3DPlugin_Hypothesis::GHS3DPlugin_Hypothesis(int hypId, int studyId, SMESH_Gen * gen)
39 : SMESH_Hypothesis(hypId, studyId, gen)
41 _name = "GHS3D_Parameters";
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();
55 //=======================================================================
56 //function : SetToMeshHoles
57 //=======================================================================
59 void GHS3DPlugin_Hypothesis::SetToMeshHoles(bool toMesh)
61 if ( myToMeshHoles != toMesh ) {
62 myToMeshHoles = toMesh;
63 NotifySubMeshesHypothesisModification();
67 //=======================================================================
68 //function : GetToMeshHoles
69 //=======================================================================
71 bool GHS3DPlugin_Hypothesis::GetToMeshHoles(bool checkFreeOption) const
73 if (checkFreeOption && !myTextOption.empty()) {
74 if ( myTextOption.find("-c 0"))
76 if ( myTextOption.find("-c 1"))
82 //=======================================================================
83 //function : SetMaximumMemory
84 //=======================================================================
86 void GHS3DPlugin_Hypothesis::SetMaximumMemory(short MB)
88 if ( myMaximumMemory != MB ) {
90 NotifySubMeshesHypothesisModification();
94 //=======================================================================
95 //function : GetMaximumMemory
96 // * automatic memory adjustment mode. Default is zero
97 //=======================================================================
99 short GHS3DPlugin_Hypothesis::GetMaximumMemory() const
101 return myMaximumMemory;
104 //=======================================================================
105 //function : SetInitialMemory
106 //=======================================================================
108 void GHS3DPlugin_Hypothesis::SetInitialMemory(short MB)
110 if ( myInitialMemory != MB ) {
111 myInitialMemory = MB;
112 NotifySubMeshesHypothesisModification();
116 //=======================================================================
117 //function : GetInitialMemory
118 //=======================================================================
120 short GHS3DPlugin_Hypothesis::GetInitialMemory() const
122 return myInitialMemory;
125 //=======================================================================
126 //function : SetOptimizationLevel
127 //=======================================================================
129 void GHS3DPlugin_Hypothesis::SetOptimizationLevel(OptimizationLevel level)
131 if ( myOptimizationLevel != level ) {
132 myOptimizationLevel = level;
133 NotifySubMeshesHypothesisModification();
137 //=======================================================================
138 //function : GetOptimizationLevel
139 //=======================================================================
141 GHS3DPlugin_Hypothesis::OptimizationLevel GHS3DPlugin_Hypothesis::GetOptimizationLevel() const
143 return (OptimizationLevel) myOptimizationLevel;
146 //=======================================================================
147 //function : SetWorkingDirectory
148 //=======================================================================
150 void GHS3DPlugin_Hypothesis::SetWorkingDirectory(const string& path)
152 if ( myWorkingDirectory != path ) {
153 myWorkingDirectory = path;
154 NotifySubMeshesHypothesisModification();
158 //=======================================================================
159 //function : GetWorkingDirectory
160 //=======================================================================
162 string GHS3DPlugin_Hypothesis::GetWorkingDirectory() const
164 return myWorkingDirectory;
167 //=======================================================================
168 //function : SetKeepFiles
169 //=======================================================================
171 void GHS3DPlugin_Hypothesis::SetKeepFiles(bool toKeep)
173 if ( myKeepFiles != toKeep ) {
174 myKeepFiles = toKeep;
175 NotifySubMeshesHypothesisModification();
179 //=======================================================================
180 //function : GetKeepFiles
181 //=======================================================================
183 bool GHS3DPlugin_Hypothesis::GetKeepFiles() const
188 //=======================================================================
189 //function : SetVerboseLevel
190 //=======================================================================
192 void GHS3DPlugin_Hypothesis::SetVerboseLevel(short level)
194 if ( myVerboseLevel != level ) {
195 myVerboseLevel = level;
196 NotifySubMeshesHypothesisModification();
200 //=======================================================================
201 //function : GetVerboseLevel
202 //=======================================================================
204 short GHS3DPlugin_Hypothesis::GetVerboseLevel() const
206 return myVerboseLevel;
209 //=======================================================================
210 //function : SetToCreateNewNodes
211 //=======================================================================
213 void GHS3DPlugin_Hypothesis::SetToCreateNewNodes(bool toCreate)
215 if ( myToCreateNewNodes != toCreate ) {
216 myToCreateNewNodes = toCreate;
217 NotifySubMeshesHypothesisModification();
221 //=======================================================================
222 //function : GetToCreateNewNodes
223 //=======================================================================
225 bool GHS3DPlugin_Hypothesis::GetToCreateNewNodes() const
227 return myToCreateNewNodes;
230 //=======================================================================
231 //function : SetToUseBoundaryRecoveryVersion
232 //=======================================================================
234 void GHS3DPlugin_Hypothesis::SetToUseBoundaryRecoveryVersion(bool toUse)
236 if ( myToUseBoundaryRecoveryVersion != toUse ) {
237 myToUseBoundaryRecoveryVersion = toUse;
238 NotifySubMeshesHypothesisModification();
242 //=======================================================================
243 //function : GetToUseBoundaryRecoveryVersion
244 //=======================================================================
246 bool GHS3DPlugin_Hypothesis::GetToUseBoundaryRecoveryVersion() const
248 return myToUseBoundaryRecoveryVersion;
251 //=======================================================================
252 //function : SetTextOption
253 //=======================================================================
255 void GHS3DPlugin_Hypothesis::SetTextOption(const string& option)
257 if ( myTextOption != option ) {
258 myTextOption = option;
259 NotifySubMeshesHypothesisModification();
263 //=======================================================================
264 //function : GetTextOption
265 //=======================================================================
267 string GHS3DPlugin_Hypothesis::GetTextOption() const
273 //=======================================================================
274 //function : DefaultMeshHoles
275 //=======================================================================
277 bool GHS3DPlugin_Hypothesis::DefaultMeshHoles()
279 return false; // PAL19680
282 //=======================================================================
283 //function : DefaultMaximumMemory
284 //=======================================================================
287 #include <sys/sysinfo.h>
292 short GHS3DPlugin_Hypothesis::DefaultMaximumMemory()
296 int err = sysinfo( &si );
298 int ramMB = si.totalram * si.mem_unit / 1024 / 1024;
299 return (short) ( 0.7 * ramMB );
302 // See http://msdn.microsoft.com/en-us/library/aa366589.aspx
303 MEMORYSTATUSEX statex;
304 statex.dwLength = sizeof (statex);
305 int err = GlobalMemoryStatusEx (&statex);
308 statex.ullTotalPhys / 1024 / 1024 +
309 statex.ullTotalPageFile / 1024 / 1024 +
310 statex.ullTotalVirtual / 1024 / 1024;
311 return (int) ( 0.7 * totMB );
316 //=======================================================================
317 //function : DefaultInitialMemory
318 //=======================================================================
320 short GHS3DPlugin_Hypothesis::DefaultInitialMemory()
322 return DefaultMaximumMemory();
325 //=======================================================================
326 //function : DefaultOptimizationLevel
327 //=======================================================================
329 short GHS3DPlugin_Hypothesis::DefaultOptimizationLevel()
334 //=======================================================================
335 //function : DefaultWorkingDirectory
336 //=======================================================================
338 string GHS3DPlugin_Hypothesis::DefaultWorkingDirectory()
340 TCollection_AsciiString aTmpDir;
342 char *Tmp_dir = getenv("SALOME_TMP_DIR");
343 if(Tmp_dir != NULL) {
348 aTmpDir = TCollection_AsciiString("C:\\");
350 aTmpDir = TCollection_AsciiString("/tmp/");
353 return aTmpDir.ToCString();
356 //=======================================================================
357 //function : DefaultKeepFiles
358 //=======================================================================
360 bool GHS3DPlugin_Hypothesis::DefaultKeepFiles()
365 //=======================================================================
366 //function : DefaultVerboseLevel
367 //=======================================================================
369 short GHS3DPlugin_Hypothesis::DefaultVerboseLevel()
374 //=======================================================================
375 //function : DefaultToCreateNewNodes
376 //=======================================================================
378 bool GHS3DPlugin_Hypothesis::DefaultToCreateNewNodes()
383 //=======================================================================
384 //function : DefaultToUseBoundaryRecoveryVersion
385 //=======================================================================
387 bool GHS3DPlugin_Hypothesis::DefaultToUseBoundaryRecoveryVersion()
392 //=======================================================================
394 //=======================================================================
396 ostream & GHS3DPlugin_Hypothesis::SaveTo(ostream & save)
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 << " ";
411 //=======================================================================
412 //function : LoadFrom
413 //=======================================================================
415 istream & GHS3DPlugin_Hypothesis::LoadFrom(istream & load)
424 load.clear(ios::badbit | load.rdstate());
430 load.clear(ios::badbit | load.rdstate());
436 load.clear(ios::badbit | load.rdstate());
440 myOptimizationLevel = i;
442 load.clear(ios::badbit | load.rdstate());
444 isOK = (load >> myWorkingDirectory);
446 if ( myWorkingDirectory == "0") { // myWorkingDirectory was empty
448 myWorkingDirectory.clear();
450 else if ( myWorkingDirectory == "1" ) {
452 myWorkingDirectory.clear();
456 load.clear(ios::badbit | load.rdstate());
458 if ( !myWorkingDirectory.empty() ) {
463 load.clear(ios::badbit | load.rdstate());
468 myVerboseLevel = (short) i;
470 load.clear(ios::badbit | load.rdstate());
474 myToCreateNewNodes = (bool) i;
476 load.clear(ios::badbit | load.rdstate());
480 myToUseBoundaryRecoveryVersion = (bool) i;
482 load.clear(ios::badbit | load.rdstate());
484 isOK = (load >> myTextOption);
495 // load.clear(ios::badbit | load.rdstate());
500 //=======================================================================
501 //function : SetParametersByMesh
502 //=======================================================================
504 bool GHS3DPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* ,const TopoDS_Shape&)
510 //================================================================================
512 * \brief Return false
514 //================================================================================
516 bool GHS3DPlugin_Hypothesis::SetParametersByDefaults(const TDefaults& /*dflts*/,
517 const SMESH_Mesh* /*theMesh*/)
522 //================================================================================
524 * \brief Return command to run ghs3d mesher excluding file prefix (-f)
526 //================================================================================
528 string GHS3DPlugin_Hypothesis::CommandToRun(const GHS3DPlugin_Hypothesis* hyp,
529 const bool hasShapeToMesh)
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;
541 // if use boundary recovery version, few options are allowed
542 bool useBndRecovery = !C;
543 if ( !useBndRecovery && hyp )
544 useBndRecovery = hyp->myToUseBoundaryRecoveryVersion;
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
550 short aMaximumMemory = hyp ? hyp->myMaximumMemory : -1;
552 if ( aMaximumMemory < 0 )
553 cmd += DefaultMaximumMemory();
555 cmd += aMaximumMemory;
557 if ( M && !useBndRecovery ) {
558 short aInitialMemory = hyp ? hyp->myInitialMemory : -1;
560 if ( aInitialMemory > 0 )
561 cmd += aInitialMemory;
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 )
573 bool aToMeshHoles = hyp ? hyp->myToMeshHoles : DefaultMeshHoles();
581 // optimization level
582 if ( o && hyp && !useBndRecovery ) {
583 if ( hyp->myOptimizationLevel >= 0 && hyp->myOptimizationLevel < 4 ) {
584 char* level[] = { "none" , "light" , "standard" , "strong" };
586 cmd += level[ hyp->myOptimizationLevel ];
590 // to create internal nodes
591 if ( p0 && hyp && !hyp->myToCreateNewNodes ) {
598 cmd += hyp->myVerboseLevel;
601 // boundary recovery version
602 if ( useBndRecovery ) {
607 if ( hyp && !hyp->myTextOption.empty() ) {
609 cmd += (char*) hyp->myTextOption.c_str();
616 return cmd.ToCString();
619 //================================================================================
621 * \brief Return a unique file name
623 //================================================================================
625 string GHS3DPlugin_Hypothesis::GetFileName(const GHS3DPlugin_Hypothesis* hyp)
627 string aTmpDir = hyp ? hyp->GetWorkingDirectory() : DefaultWorkingDirectory();
628 const char lastChar = *aTmpDir.rbegin();
630 if(lastChar != '\\') aTmpDir+='\\';
632 if(lastChar != '/') aTmpDir+='/';
635 TCollection_AsciiString aGenericName = (char*)aTmpDir.c_str();
636 aGenericName += "GHS3D_";
637 aGenericName += getpid();
639 aGenericName += Abs((Standard_Integer)(long) aGenericName.ToCString());
641 return aGenericName.ToCString();