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>
29 //=======================================================================
30 //function : GHS3DPlugin_Hypothesis
31 //=======================================================================
33 GHS3DPlugin_Hypothesis::GHS3DPlugin_Hypothesis(int hypId, int studyId, SMESH_Gen * gen)
34 : SMESH_Hypothesis(hypId, studyId, gen)
36 _name = "GHS3D_Parameters";
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();
50 //=======================================================================
51 //function : SetToMeshHoles
52 //=======================================================================
54 void GHS3DPlugin_Hypothesis::SetToMeshHoles(bool toMesh)
56 if ( myToMeshHoles != toMesh ) {
57 myToMeshHoles = toMesh;
58 NotifySubMeshesHypothesisModification();
62 //=======================================================================
63 //function : GetToMeshHoles
64 //=======================================================================
66 bool GHS3DPlugin_Hypothesis::GetToMeshHoles(bool checkFreeOption) const
68 if (checkFreeOption && !myTextOption.empty()) {
69 if ( myTextOption.find("-c 0"))
71 if ( myTextOption.find("-c 1"))
77 //=======================================================================
78 //function : SetMaximumMemory
79 //=======================================================================
81 void GHS3DPlugin_Hypothesis::SetMaximumMemory(short MB)
83 if ( myMaximumMemory != MB ) {
85 NotifySubMeshesHypothesisModification();
89 //=======================================================================
90 //function : GetMaximumMemory
91 // * automatic memory adjustment mode. Default is zero
92 //=======================================================================
94 short GHS3DPlugin_Hypothesis::GetMaximumMemory() const
96 return myMaximumMemory;
99 //=======================================================================
100 //function : SetInitialMemory
101 //=======================================================================
103 void GHS3DPlugin_Hypothesis::SetInitialMemory(short MB)
105 if ( myInitialMemory != MB ) {
106 myInitialMemory = MB;
107 NotifySubMeshesHypothesisModification();
111 //=======================================================================
112 //function : GetInitialMemory
113 //=======================================================================
115 short GHS3DPlugin_Hypothesis::GetInitialMemory() const
117 return myInitialMemory;
120 //=======================================================================
121 //function : SetOptimizationLevel
122 //=======================================================================
124 void GHS3DPlugin_Hypothesis::SetOptimizationLevel(OptimizationLevel level)
126 if ( myOptimizationLevel != level ) {
127 myOptimizationLevel = level;
128 NotifySubMeshesHypothesisModification();
132 //=======================================================================
133 //function : GetOptimizationLevel
134 //=======================================================================
136 GHS3DPlugin_Hypothesis::OptimizationLevel GHS3DPlugin_Hypothesis::GetOptimizationLevel() const
138 return (OptimizationLevel) myOptimizationLevel;
141 //=======================================================================
142 //function : SetWorkingDirectory
143 //=======================================================================
145 void GHS3DPlugin_Hypothesis::SetWorkingDirectory(const string& path)
147 if ( myWorkingDirectory != path ) {
148 myWorkingDirectory = path;
149 NotifySubMeshesHypothesisModification();
153 //=======================================================================
154 //function : GetWorkingDirectory
155 //=======================================================================
157 string GHS3DPlugin_Hypothesis::GetWorkingDirectory() const
159 return myWorkingDirectory;
162 //=======================================================================
163 //function : SetKeepFiles
164 //=======================================================================
166 void GHS3DPlugin_Hypothesis::SetKeepFiles(bool toKeep)
168 if ( myKeepFiles != toKeep ) {
169 myKeepFiles = toKeep;
170 NotifySubMeshesHypothesisModification();
174 //=======================================================================
175 //function : GetKeepFiles
176 //=======================================================================
178 bool GHS3DPlugin_Hypothesis::GetKeepFiles() const
183 //=======================================================================
184 //function : SetVerboseLevel
185 //=======================================================================
187 void GHS3DPlugin_Hypothesis::SetVerboseLevel(short level)
189 if ( myVerboseLevel != level ) {
190 myVerboseLevel = level;
191 NotifySubMeshesHypothesisModification();
195 //=======================================================================
196 //function : GetVerboseLevel
197 //=======================================================================
199 short GHS3DPlugin_Hypothesis::GetVerboseLevel() const
201 return myVerboseLevel;
204 //=======================================================================
205 //function : SetToCreateNewNodes
206 //=======================================================================
208 void GHS3DPlugin_Hypothesis::SetToCreateNewNodes(bool toCreate)
210 if ( myToCreateNewNodes != toCreate ) {
211 myToCreateNewNodes = toCreate;
212 NotifySubMeshesHypothesisModification();
216 //=======================================================================
217 //function : GetToCreateNewNodes
218 //=======================================================================
220 bool GHS3DPlugin_Hypothesis::GetToCreateNewNodes() const
222 return myToCreateNewNodes;
225 //=======================================================================
226 //function : SetToUseBoundaryRecoveryVersion
227 //=======================================================================
229 void GHS3DPlugin_Hypothesis::SetToUseBoundaryRecoveryVersion(bool toUse)
231 if ( myToUseBoundaryRecoveryVersion != toUse ) {
232 myToUseBoundaryRecoveryVersion = toUse;
233 NotifySubMeshesHypothesisModification();
237 //=======================================================================
238 //function : GetToUseBoundaryRecoveryVersion
239 //=======================================================================
241 bool GHS3DPlugin_Hypothesis::GetToUseBoundaryRecoveryVersion() const
243 return myToUseBoundaryRecoveryVersion;
246 //=======================================================================
247 //function : SetTextOption
248 //=======================================================================
250 void GHS3DPlugin_Hypothesis::SetTextOption(const string& option)
252 if ( myTextOption != option ) {
253 myTextOption = option;
254 NotifySubMeshesHypothesisModification();
258 //=======================================================================
259 //function : GetTextOption
260 //=======================================================================
262 string GHS3DPlugin_Hypothesis::GetTextOption() const
268 //=======================================================================
269 //function : DefaultMeshHoles
270 //=======================================================================
272 bool GHS3DPlugin_Hypothesis::DefaultMeshHoles()
274 return false; // PAL19680
277 //=======================================================================
278 //function : DefaultMaximumMemory
279 //=======================================================================
282 #include <sys/sysinfo.h>
285 short GHS3DPlugin_Hypothesis::DefaultMaximumMemory()
289 int err = sysinfo( &si );
291 int ramMB = si.totalram * si.mem_unit / 1024 / 1024;
292 return (short) ( 0.7 * ramMB );
298 //=======================================================================
299 //function : DefaultInitialMemory
300 //=======================================================================
302 short GHS3DPlugin_Hypothesis::DefaultInitialMemory()
304 return DefaultMaximumMemory();
307 //=======================================================================
308 //function : DefaultOptimizationLevel
309 //=======================================================================
311 short GHS3DPlugin_Hypothesis::DefaultOptimizationLevel()
316 //=======================================================================
317 //function : DefaultWorkingDirectory
318 //=======================================================================
320 string GHS3DPlugin_Hypothesis::DefaultWorkingDirectory()
322 TCollection_AsciiString aTmpDir;
324 char *Tmp_dir = getenv("SALOME_TMP_DIR");
325 if(Tmp_dir != NULL) {
330 aTmpDir = TCollection_AsciiString("C:\\");
332 aTmpDir = TCollection_AsciiString("/tmp/");
335 return aTmpDir.ToCString();
338 //=======================================================================
339 //function : DefaultKeepFiles
340 //=======================================================================
342 bool GHS3DPlugin_Hypothesis::DefaultKeepFiles()
347 //=======================================================================
348 //function : DefaultVerboseLevel
349 //=======================================================================
351 short GHS3DPlugin_Hypothesis::DefaultVerboseLevel()
356 //=======================================================================
357 //function : DefaultToCreateNewNodes
358 //=======================================================================
360 bool GHS3DPlugin_Hypothesis::DefaultToCreateNewNodes()
365 //=======================================================================
366 //function : DefaultToUseBoundaryRecoveryVersion
367 //=======================================================================
369 bool GHS3DPlugin_Hypothesis::DefaultToUseBoundaryRecoveryVersion()
374 //=======================================================================
376 //=======================================================================
378 ostream & GHS3DPlugin_Hypothesis::SaveTo(ostream & save)
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 << " ";
393 //=======================================================================
394 //function : LoadFrom
395 //=======================================================================
397 istream & GHS3DPlugin_Hypothesis::LoadFrom(istream & load)
406 load.clear(ios::badbit | load.rdstate());
412 load.clear(ios::badbit | load.rdstate());
418 load.clear(ios::badbit | load.rdstate());
422 myOptimizationLevel = i;
424 load.clear(ios::badbit | load.rdstate());
426 isOK = (load >> myWorkingDirectory);
428 if ( myWorkingDirectory == "0") { // myWorkingDirectory was empty
430 myWorkingDirectory.clear();
432 else if ( myWorkingDirectory == "1" ) {
434 myWorkingDirectory.clear();
438 load.clear(ios::badbit | load.rdstate());
440 if ( !myWorkingDirectory.empty() ) {
445 load.clear(ios::badbit | load.rdstate());
450 myVerboseLevel = (short) i;
452 load.clear(ios::badbit | load.rdstate());
456 myToCreateNewNodes = (bool) i;
458 load.clear(ios::badbit | load.rdstate());
462 myToUseBoundaryRecoveryVersion = (bool) i;
464 load.clear(ios::badbit | load.rdstate());
466 isOK = (load >> myTextOption);
477 // load.clear(ios::badbit | load.rdstate());
482 //=======================================================================
483 //function : SetParametersByMesh
484 //=======================================================================
486 bool GHS3DPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* ,const TopoDS_Shape&)
492 //================================================================================
494 * \brief Return false
496 //================================================================================
498 bool GHS3DPlugin_Hypothesis::SetParametersByDefaults(const TDefaults& /*dflts*/,
499 const SMESH_Mesh* /*theMesh*/)
504 //================================================================================
506 * \brief Return command to run ghs3d mesher excluding file prefix (-f)
508 //================================================================================
510 string GHS3DPlugin_Hypothesis::CommandToRun(const GHS3DPlugin_Hypothesis* hyp,
511 const bool hasShapeToMesh)
514 TCollection_AsciiString cmd( "ghs3d" );
516 TCollection_AsciiString cmd( "ghs3d.exe" );
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;
527 // if use boundary recovery version, few options are allowed
528 bool useBndRecovery = !C;
529 if ( !useBndRecovery && hyp )
530 useBndRecovery = hyp->myToUseBoundaryRecoveryVersion;
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
536 short aMaximumMemory = hyp ? hyp->myMaximumMemory : -1;
538 if ( aMaximumMemory < 0 )
539 cmd += DefaultMaximumMemory();
541 cmd += aMaximumMemory;
543 if ( M && !useBndRecovery ) {
544 short aInitialMemory = hyp ? hyp->myInitialMemory : -1;
546 if ( aInitialMemory > 0 )
547 cmd += aInitialMemory;
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 )
559 bool aToMeshHoles = hyp ? hyp->myToMeshHoles : DefaultMeshHoles();
567 // optimization level
568 if ( o && hyp && !useBndRecovery ) {
569 if ( hyp->myOptimizationLevel >= 0 && hyp->myOptimizationLevel < 4 ) {
570 char* level[] = { "none" , "light" , "standard" , "strong" };
572 cmd += level[ hyp->myOptimizationLevel ];
576 // to create internal nodes
577 if ( p0 && hyp && !hyp->myToCreateNewNodes ) {
584 cmd += hyp->myVerboseLevel;
587 // boundary recovery version
588 if ( useBndRecovery ) {
593 if ( hyp && !hyp->myTextOption.empty() ) {
595 cmd += (char*) hyp->myTextOption.c_str();
598 return cmd.ToCString();
601 //================================================================================
603 * \brief Return a unique file name
605 //================================================================================
607 string GHS3DPlugin_Hypothesis::GetFileName(const GHS3DPlugin_Hypothesis* hyp)
609 string aTmpDir = hyp ? hyp->GetWorkingDirectory() : DefaultWorkingDirectory();
610 const char lastChar = *aTmpDir.rbegin();
612 if(lastChar != '\\') aTmpDir+='\\';
614 if(lastChar != '/') aTmpDir+='/';
617 TCollection_AsciiString aGenericName = (char*)aTmpDir.c_str();
618 aGenericName += "GHS3D_";
620 aGenericName += GetCurrentProcessId();
622 aGenericName += getpid();
625 aGenericName += Abs((Standard_Integer)(long) aGenericName.ToCString());
627 return aGenericName.ToCString();