1 // Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA 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 //=============================================================================
26 #include "GHS3DPlugin_Hypothesis.hxx"
28 #include <TCollection_AsciiString.hxx>
30 //=======================================================================
31 //function : GHS3DPlugin_Hypothesis
32 //=======================================================================
34 GHS3DPlugin_Hypothesis::GHS3DPlugin_Hypothesis(int hypId, int studyId, SMESH_Gen * gen)
35 : SMESH_Hypothesis(hypId, studyId, gen)
37 _name = "GHS3D_Parameters";
40 myToMeshHoles = DefaultMeshHoles();
41 myMaximumMemory = -1;//DefaultMaximumMemory();
42 myInitialMemory = -1;//DefaultInitialMemory();
43 myOptimizationLevel = DefaultOptimizationLevel();
44 myWorkingDirectory = DefaultWorkingDirectory();
45 myKeepFiles = DefaultKeepFiles();
46 myVerboseLevel = DefaultVerboseLevel();
47 myToCreateNewNodes = DefaultToCreateNewNodes();
48 myToUseBoundaryRecoveryVersion = DefaultToUseBoundaryRecoveryVersion();
51 //=======================================================================
52 //function : SetToMeshHoles
53 //=======================================================================
55 void GHS3DPlugin_Hypothesis::SetToMeshHoles(bool toMesh)
57 if ( myToMeshHoles != toMesh ) {
58 myToMeshHoles = toMesh;
59 NotifySubMeshesHypothesisModification();
63 //=======================================================================
64 //function : GetToMeshHoles
65 //=======================================================================
67 bool GHS3DPlugin_Hypothesis::GetToMeshHoles(bool checkFreeOption) const
69 if (checkFreeOption && !myTextOption.empty()) {
70 if ( myTextOption.find("-c 0"))
72 if ( myTextOption.find("-c 1"))
78 //=======================================================================
79 //function : SetMaximumMemory
80 //=======================================================================
82 void GHS3DPlugin_Hypothesis::SetMaximumMemory(short MB)
84 if ( myMaximumMemory != MB ) {
86 NotifySubMeshesHypothesisModification();
90 //=======================================================================
91 //function : GetMaximumMemory
92 // * automatic memory adjustment mode. Default is zero
93 //=======================================================================
95 short GHS3DPlugin_Hypothesis::GetMaximumMemory() const
97 return myMaximumMemory;
100 //=======================================================================
101 //function : SetInitialMemory
102 //=======================================================================
104 void GHS3DPlugin_Hypothesis::SetInitialMemory(short MB)
106 if ( myInitialMemory != MB ) {
107 myInitialMemory = MB;
108 NotifySubMeshesHypothesisModification();
112 //=======================================================================
113 //function : GetInitialMemory
114 //=======================================================================
116 short GHS3DPlugin_Hypothesis::GetInitialMemory() const
118 return myInitialMemory;
121 //=======================================================================
122 //function : SetOptimizationLevel
123 //=======================================================================
125 void GHS3DPlugin_Hypothesis::SetOptimizationLevel(OptimizationLevel level)
127 if ( myOptimizationLevel != level ) {
128 myOptimizationLevel = level;
129 NotifySubMeshesHypothesisModification();
133 //=======================================================================
134 //function : GetOptimizationLevel
135 //=======================================================================
137 GHS3DPlugin_Hypothesis::OptimizationLevel GHS3DPlugin_Hypothesis::GetOptimizationLevel() const
139 return (OptimizationLevel) myOptimizationLevel;
142 //=======================================================================
143 //function : SetWorkingDirectory
144 //=======================================================================
146 void GHS3DPlugin_Hypothesis::SetWorkingDirectory(const string& path)
148 if ( myWorkingDirectory != path ) {
149 myWorkingDirectory = path;
150 NotifySubMeshesHypothesisModification();
154 //=======================================================================
155 //function : GetWorkingDirectory
156 //=======================================================================
158 string GHS3DPlugin_Hypothesis::GetWorkingDirectory() const
160 return myWorkingDirectory;
163 //=======================================================================
164 //function : SetKeepFiles
165 //=======================================================================
167 void GHS3DPlugin_Hypothesis::SetKeepFiles(bool toKeep)
169 if ( myKeepFiles != toKeep ) {
170 myKeepFiles = toKeep;
171 NotifySubMeshesHypothesisModification();
175 //=======================================================================
176 //function : GetKeepFiles
177 //=======================================================================
179 bool GHS3DPlugin_Hypothesis::GetKeepFiles() const
184 //=======================================================================
185 //function : SetVerboseLevel
186 //=======================================================================
188 void GHS3DPlugin_Hypothesis::SetVerboseLevel(short level)
190 if ( myVerboseLevel != level ) {
191 myVerboseLevel = level;
192 NotifySubMeshesHypothesisModification();
196 //=======================================================================
197 //function : GetVerboseLevel
198 //=======================================================================
200 short GHS3DPlugin_Hypothesis::GetVerboseLevel() const
202 return myVerboseLevel;
205 //=======================================================================
206 //function : SetToCreateNewNodes
207 //=======================================================================
209 void GHS3DPlugin_Hypothesis::SetToCreateNewNodes(bool toCreate)
211 if ( myToCreateNewNodes != toCreate ) {
212 myToCreateNewNodes = toCreate;
213 NotifySubMeshesHypothesisModification();
217 //=======================================================================
218 //function : GetToCreateNewNodes
219 //=======================================================================
221 bool GHS3DPlugin_Hypothesis::GetToCreateNewNodes() const
223 return myToCreateNewNodes;
226 //=======================================================================
227 //function : SetToUseBoundaryRecoveryVersion
228 //=======================================================================
230 void GHS3DPlugin_Hypothesis::SetToUseBoundaryRecoveryVersion(bool toUse)
232 if ( myToUseBoundaryRecoveryVersion != toUse ) {
233 myToUseBoundaryRecoveryVersion = toUse;
234 NotifySubMeshesHypothesisModification();
238 //=======================================================================
239 //function : GetToUseBoundaryRecoveryVersion
240 //=======================================================================
242 bool GHS3DPlugin_Hypothesis::GetToUseBoundaryRecoveryVersion() const
244 return myToUseBoundaryRecoveryVersion;
247 //=======================================================================
248 //function : SetTextOption
249 //=======================================================================
251 void GHS3DPlugin_Hypothesis::SetTextOption(const string& option)
253 if ( myTextOption != option ) {
254 myTextOption = option;
255 NotifySubMeshesHypothesisModification();
259 //=======================================================================
260 //function : GetTextOption
261 //=======================================================================
263 string GHS3DPlugin_Hypothesis::GetTextOption() const
269 //=======================================================================
270 //function : DefaultMeshHoles
271 //=======================================================================
273 bool GHS3DPlugin_Hypothesis::DefaultMeshHoles()
275 return false; // PAL19680
278 //=======================================================================
279 //function : DefaultMaximumMemory
280 //=======================================================================
283 #include <sys/sysinfo.h>
286 short GHS3DPlugin_Hypothesis::DefaultMaximumMemory()
290 int err = sysinfo( &si );
292 int ramMB = si.totalram * si.mem_unit / 1024 / 1024;
293 return (short) ( 0.7 * ramMB );
299 //=======================================================================
300 //function : DefaultInitialMemory
301 //=======================================================================
303 short GHS3DPlugin_Hypothesis::DefaultInitialMemory()
305 return DefaultMaximumMemory();
308 //=======================================================================
309 //function : DefaultOptimizationLevel
310 //=======================================================================
312 short GHS3DPlugin_Hypothesis::DefaultOptimizationLevel()
317 //=======================================================================
318 //function : DefaultWorkingDirectory
319 //=======================================================================
321 string GHS3DPlugin_Hypothesis::DefaultWorkingDirectory()
323 TCollection_AsciiString aTmpDir;
325 char *Tmp_dir = getenv("SALOME_TMP_DIR");
326 if(Tmp_dir != NULL) {
331 aTmpDir = TCollection_AsciiString("C:\\");
333 aTmpDir = TCollection_AsciiString("/tmp/");
336 return aTmpDir.ToCString();
339 //=======================================================================
340 //function : DefaultKeepFiles
341 //=======================================================================
343 bool GHS3DPlugin_Hypothesis::DefaultKeepFiles()
348 //=======================================================================
349 //function : DefaultVerboseLevel
350 //=======================================================================
352 short GHS3DPlugin_Hypothesis::DefaultVerboseLevel()
357 //=======================================================================
358 //function : DefaultToCreateNewNodes
359 //=======================================================================
361 bool GHS3DPlugin_Hypothesis::DefaultToCreateNewNodes()
366 //=======================================================================
367 //function : DefaultToUseBoundaryRecoveryVersion
368 //=======================================================================
370 bool GHS3DPlugin_Hypothesis::DefaultToUseBoundaryRecoveryVersion()
375 //=======================================================================
377 //=======================================================================
379 ostream & GHS3DPlugin_Hypothesis::SaveTo(ostream & save)
381 save << (int) myToMeshHoles << " ";
382 save << myMaximumMemory << " ";
383 save << myInitialMemory << " ";
384 save << myOptimizationLevel << " ";
385 save << myWorkingDirectory << " ";
386 save << (int)myKeepFiles << " ";
387 save << myVerboseLevel << " ";
388 save << (int)myToCreateNewNodes << " ";
389 save << (int)myToUseBoundaryRecoveryVersion << " ";
390 save << myTextOption << " ";
394 //=======================================================================
395 //function : LoadFrom
396 //=======================================================================
398 istream & GHS3DPlugin_Hypothesis::LoadFrom(istream & load)
407 load.clear(ios::badbit | load.rdstate());
413 load.clear(ios::badbit | load.rdstate());
419 load.clear(ios::badbit | load.rdstate());
423 myOptimizationLevel = i;
425 load.clear(ios::badbit | load.rdstate());
427 isOK = (load >> myWorkingDirectory);
429 if ( myWorkingDirectory == "0") { // myWorkingDirectory was empty
431 myWorkingDirectory.clear();
433 else if ( myWorkingDirectory == "1" ) {
435 myWorkingDirectory.clear();
439 load.clear(ios::badbit | load.rdstate());
441 if ( !myWorkingDirectory.empty() ) {
446 load.clear(ios::badbit | load.rdstate());
451 myVerboseLevel = (short) i;
453 load.clear(ios::badbit | load.rdstate());
457 myToCreateNewNodes = (bool) i;
459 load.clear(ios::badbit | load.rdstate());
463 myToUseBoundaryRecoveryVersion = (bool) i;
465 load.clear(ios::badbit | load.rdstate());
467 isOK = (load >> myTextOption);
478 // load.clear(ios::badbit | load.rdstate());
483 //=======================================================================
484 //function : SetParametersByMesh
485 //=======================================================================
487 bool GHS3DPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* ,const TopoDS_Shape&)
492 //================================================================================
494 * \brief Return command to run ghs3d mesher excluding file prefix (-f)
496 //================================================================================
498 string GHS3DPlugin_Hypothesis::CommandToRun(const GHS3DPlugin_Hypothesis* hyp,
499 const bool hasShapeToMesh)
502 TCollection_AsciiString cmd( "ghs3d" );
504 TCollection_AsciiString cmd( "ghs3d.exe" );
506 // check if any option is overridden by hyp->myTextOption
507 bool m = hyp ? ( hyp->myTextOption.find("-m") == string::npos ) : true;
508 bool M = hyp ? ( hyp->myTextOption.find("-M") == string::npos ) : true;
509 bool c = hyp ? ( hyp->myTextOption.find("-c") == string::npos ) : true;
510 bool o = hyp ? ( hyp->myTextOption.find("-o") == string::npos ) : true;
511 bool p0= hyp ? ( hyp->myTextOption.find("-p0")== string::npos ) : true;
512 bool C = hyp ? ( hyp->myTextOption.find("-C") == string::npos ) : true;
513 bool v = hyp ? ( hyp->myTextOption.find("-v") == string::npos ) : true;
515 // if use boundary recovery version, few options are allowed
516 bool useBndRecovery = !C;
517 if ( !useBndRecovery && hyp )
518 useBndRecovery = hyp->myToUseBoundaryRecoveryVersion;
520 // ghs3d needs to know amount of memory it may use (MB).
521 // Default memory is defined at ghs3d installation but it may be not enough,
522 // so allow to use about all available memory
524 short aMaximumMemory = hyp ? hyp->myMaximumMemory : -1;
526 if ( aMaximumMemory < 0 )
527 cmd += DefaultMaximumMemory();
529 cmd += aMaximumMemory;
531 if ( M && !useBndRecovery ) {
532 short aInitialMemory = hyp ? hyp->myInitialMemory : -1;
534 if ( aInitialMemory > 0 )
535 cmd += aInitialMemory;
540 // 0 , all components to be meshed
541 // 1 , only the main ( outermost ) component to be meshed
542 if ( c && !useBndRecovery ) {
543 // We always run GHS3D with "to mesh holes'==TRUE (see PAL19680)
544 if ( hasShapeToMesh )
547 bool aToMeshHoles = hyp ? hyp->myToMeshHoles : DefaultMeshHoles();
555 // optimization level
556 if ( o && hyp && !useBndRecovery ) {
557 if ( hyp->myOptimizationLevel >= 0 && hyp->myOptimizationLevel < 4 ) {
558 char* level[] = { "none" , "light" , "standard" , "strong" };
560 cmd += level[ hyp->myOptimizationLevel ];
564 // to create internal nodes
565 if ( p0 && hyp && !hyp->myToCreateNewNodes ) {
572 cmd += hyp->myVerboseLevel;
575 // boundary recovery version
576 if ( useBndRecovery ) {
581 if ( hyp && !hyp->myTextOption.empty() ) {
583 cmd += (char*) hyp->myTextOption.c_str();
586 return cmd.ToCString();
589 //================================================================================
591 * \brief Return a unique file name
593 //================================================================================
595 string GHS3DPlugin_Hypothesis::GetFileName(const GHS3DPlugin_Hypothesis* hyp)
597 string aTmpDir = hyp ? hyp->GetWorkingDirectory() : DefaultWorkingDirectory();
598 const char lastChar = *aTmpDir.rbegin();
600 if(lastChar != '\\') aTmpDir+='\\';
602 if(lastChar != '/') aTmpDir+='/';
605 TCollection_AsciiString aGenericName = (char*)aTmpDir.c_str();
606 aGenericName += "GHS3D_";
608 aGenericName += GetCurrentProcessId();
610 aGenericName += getpid();
613 aGenericName += Abs((Standard_Integer)(long) aGenericName.ToCString());
615 return aGenericName.ToCString();