Salome HOME
Fix compilation errors using gcc-5.X relating to explicit stream::operator bool()
[plugins/ghs3dplugin.git] / src / GHS3DPlugin / GHS3DPlugin_Hypothesis.cxx
1 // Copyright (C) 2004-2015  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, or (at your option) any later version.
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 //=============================================================================
21 // File      : GHS3DPlugin_Hypothesis.cxx
22 // Created   : Wed Apr  2 12:36:29 2008
23 // Author    : Edward AGAPOV (eap)
24 //=============================================================================
25 //
26 #include "GHS3DPlugin_Hypothesis.hxx"
27 #include <SMESH_ProxyMesh.hxx>
28 #include <SMESH_Group.hxx>
29 #include <StdMeshers_QuadToTriaAdaptor.hxx>
30
31 #include <TCollection_AsciiString.hxx>
32
33 #ifdef WIN32
34 #include <process.h>
35 #define getpid _getpid
36 #endif
37
38 //=======================================================================
39 //function : GHS3DPlugin_Hypothesis
40 //=======================================================================
41
42 GHS3DPlugin_Hypothesis::GHS3DPlugin_Hypothesis(int hypId, int studyId, SMESH_Gen * gen)
43   : SMESH_Hypothesis(hypId, studyId, gen),
44   myToMeshHoles(DefaultMeshHoles()),
45   myToMakeGroupsOfDomains(DefaultToMakeGroupsOfDomains()),
46   myMaximumMemory(-1),
47   myInitialMemory(-1),
48   myOptimizationLevel(DefaultOptimizationLevel()),
49   myKeepFiles(DefaultKeepFiles()),
50   myWorkingDirectory(DefaultWorkingDirectory()),
51   myVerboseLevel(DefaultVerboseLevel()),
52   myToCreateNewNodes(DefaultToCreateNewNodes()),
53   myToUseBoundaryRecoveryVersion(DefaultToUseBoundaryRecoveryVersion()),
54   myToUseFemCorrection(DefaultToUseFEMCorrection()),
55   myToRemoveCentralPoint(DefaultToRemoveCentralPoint()),
56   myLogInStandardOutput(DefaultStandardOutputLog()),
57   myGradation(DefaultGradation()),
58   _enfVertexList(DefaultGHS3DEnforcedVertexList()),
59   _enfVertexCoordsSizeList(DefaultGHS3DEnforcedVertexCoordsValues()),
60   _enfVertexEntrySizeList(DefaultGHS3DEnforcedVertexEntryValues()),
61   _coordsEnfVertexMap(DefaultCoordsGHS3DEnforcedVertexMap()),
62   _geomEntryEnfVertexMap(DefaultGeomEntryGHS3DEnforcedVertexMap()),
63   _enfMeshList(DefaultGHS3DEnforcedMeshList()),
64   _entryEnfMeshMap(DefaultEntryGHS3DEnforcedMeshListMap()),
65   _enfNodes(TIDSortedNodeGroupMap()),
66   _enfEdges(TIDSortedElemGroupMap()),
67   _enfTriangles(TIDSortedElemGroupMap()),
68   _nodeIDToSizeMap(DefaultID2SizeMap()),
69   _groupsToRemove(DefaultGroupsToRemove())
70 {
71   _name = GetHypType();
72   _param_algo_dim = 3;
73 }
74
75 //=======================================================================
76 //function : SetToMeshHoles
77 //=======================================================================
78
79 void GHS3DPlugin_Hypothesis::SetToMeshHoles(bool toMesh)
80 {
81   if ( myToMeshHoles != toMesh ) {
82     myToMeshHoles = toMesh;
83     NotifySubMeshesHypothesisModification();
84   }
85 }
86
87 //=======================================================================
88 //function : GetToMeshHoles
89 //=======================================================================
90
91 bool GHS3DPlugin_Hypothesis::GetToMeshHoles(bool checkFreeOption) const
92 {
93   if (checkFreeOption && !myTextOption.empty()) {
94     if ( myTextOption.find("--components all"))
95       return true;
96     if ( myTextOption.find("--components outside_components"))
97       return false;
98   }
99   return myToMeshHoles;
100 }
101
102 //=======================================================================
103 //function : SetToMakeGroupsOfDomains
104 //=======================================================================
105
106 void GHS3DPlugin_Hypothesis::SetToMakeGroupsOfDomains(bool toMakeGroups)
107 {
108   if ( myToMakeGroupsOfDomains != toMakeGroups ) {
109     myToMakeGroupsOfDomains = toMakeGroups;
110     NotifySubMeshesHypothesisModification();
111   }
112 }
113
114 //=======================================================================
115 //function : GetToMakeGroupsOfDomains
116 //=======================================================================
117
118 bool GHS3DPlugin_Hypothesis::GetToMakeGroupsOfDomains() const
119 {
120   return myToMakeGroupsOfDomains;
121 }
122
123 //=======================================================================
124 //function : GetToMakeGroupsOfDomains
125 //=======================================================================
126
127 bool GHS3DPlugin_Hypothesis::GetToMakeGroupsOfDomains(const GHS3DPlugin_Hypothesis* hyp)
128 {
129   bool res;
130   if ( hyp ) res = /*hyp->GetToMeshHoles(true) &&*/ hyp->GetToMakeGroupsOfDomains();
131   else       res = /*DefaultMeshHoles()        &&*/ DefaultToMakeGroupsOfDomains();
132   return res;
133 }
134
135 //=======================================================================
136 //function : SetMaximumMemory
137 //=======================================================================
138
139 void GHS3DPlugin_Hypothesis::SetMaximumMemory(long MB)
140 {
141   if ( myMaximumMemory != MB ) {
142     myMaximumMemory = MB;
143     NotifySubMeshesHypothesisModification();
144   }
145 }
146
147 //=======================================================================
148 //function : GetMaximumMemory
149 //           * automatic memory adjustment mode. Default is zero
150 //=======================================================================
151
152 long GHS3DPlugin_Hypothesis::GetMaximumMemory() const
153 {
154   return myMaximumMemory;
155 }
156
157 //=======================================================================
158 //function : SetInitialMemory
159 //=======================================================================
160
161 void GHS3DPlugin_Hypothesis::SetInitialMemory(long MB)
162 {
163   if ( myInitialMemory != MB ) {
164     myInitialMemory = MB;
165     NotifySubMeshesHypothesisModification();
166   }
167 }
168
169 //=======================================================================
170 //function : GetInitialMemory
171 //=======================================================================
172
173 long GHS3DPlugin_Hypothesis::GetInitialMemory() const
174 {
175   return myInitialMemory;
176 }
177
178 //=======================================================================
179 //function : SetOptimizationLevel
180 //=======================================================================
181
182 void GHS3DPlugin_Hypothesis::SetOptimizationLevel(OptimizationLevel level)
183 {
184   if ( myOptimizationLevel != level ) {
185     myOptimizationLevel = level;
186     NotifySubMeshesHypothesisModification();
187   }
188 }
189
190 //=======================================================================
191 //function : GetOptimizationLevel
192 //=======================================================================
193
194 GHS3DPlugin_Hypothesis::OptimizationLevel GHS3DPlugin_Hypothesis::GetOptimizationLevel() const
195 {
196   return (OptimizationLevel) myOptimizationLevel;
197 }
198
199 //=======================================================================
200 //function : SetWorkingDirectory
201 //=======================================================================
202
203 void GHS3DPlugin_Hypothesis::SetWorkingDirectory(const std::string& path)
204 {
205   if ( myWorkingDirectory != path ) {
206     myWorkingDirectory = path;
207     NotifySubMeshesHypothesisModification();
208   }
209 }
210
211 //=======================================================================
212 //function : GetWorkingDirectory
213 //=======================================================================
214
215 std::string GHS3DPlugin_Hypothesis::GetWorkingDirectory() const
216 {
217   return myWorkingDirectory;
218 }
219
220 //=======================================================================
221 //function : SetKeepFiles
222 //=======================================================================
223
224 void GHS3DPlugin_Hypothesis::SetKeepFiles(bool toKeep)
225 {
226   if ( myKeepFiles != toKeep ) {
227     myKeepFiles = toKeep;
228     NotifySubMeshesHypothesisModification();
229   }
230 }
231
232 //=======================================================================
233 //function : GetKeepFiles
234 //=======================================================================
235
236 bool GHS3DPlugin_Hypothesis::GetKeepFiles() const
237 {
238   return myKeepFiles;
239 }
240
241 //=======================================================================
242 //function : SetVerboseLevel
243 //=======================================================================
244
245 void GHS3DPlugin_Hypothesis::SetVerboseLevel(short level)
246 {
247   if ( myVerboseLevel != level ) {
248     myVerboseLevel = level;
249     NotifySubMeshesHypothesisModification();
250   }
251 }
252
253 //=======================================================================
254 //function : GetVerboseLevel
255 //=======================================================================
256
257 short GHS3DPlugin_Hypothesis::GetVerboseLevel() const
258 {
259   return myVerboseLevel;
260 }
261
262 //=======================================================================
263 //function : SetToCreateNewNodes
264 //=======================================================================
265
266 void GHS3DPlugin_Hypothesis::SetToCreateNewNodes(bool toCreate)
267 {
268   if ( myToCreateNewNodes != toCreate ) {
269     myToCreateNewNodes = toCreate;
270     NotifySubMeshesHypothesisModification();
271   }
272 }
273
274 //=======================================================================
275 //function : GetToCreateNewNodes
276 //=======================================================================
277
278 bool GHS3DPlugin_Hypothesis::GetToCreateNewNodes() const
279 {
280   return myToCreateNewNodes;
281 }
282
283 //=======================================================================
284 //function : SetToUseBoundaryRecoveryVersion
285 //=======================================================================
286
287 void GHS3DPlugin_Hypothesis::SetToUseBoundaryRecoveryVersion(bool toUse)
288 {
289   if ( myToUseBoundaryRecoveryVersion != toUse ) {
290     myToUseBoundaryRecoveryVersion = toUse;
291     NotifySubMeshesHypothesisModification();
292   }
293 }
294
295 //=======================================================================
296 //function : GetToUseBoundaryRecoveryVersion
297 //=======================================================================
298
299 bool GHS3DPlugin_Hypothesis::GetToUseBoundaryRecoveryVersion() const
300 {
301   return myToUseBoundaryRecoveryVersion;
302 }
303
304 //=======================================================================
305 //function : SetFEMCorrection
306 //=======================================================================
307
308 void GHS3DPlugin_Hypothesis::SetFEMCorrection(bool toUseFem)
309 {
310   if ( myToUseFemCorrection != toUseFem ) {
311     myToUseFemCorrection = toUseFem;
312     NotifySubMeshesHypothesisModification();
313   }
314 }
315
316 //=======================================================================
317 //function : GetFEMCorrection
318 //=======================================================================
319
320 bool GHS3DPlugin_Hypothesis::GetFEMCorrection() const
321 {
322   return myToUseFemCorrection;
323 }
324
325 //=======================================================================
326 //function : SetToRemoveCentralPoint
327 //=======================================================================
328
329 void GHS3DPlugin_Hypothesis::SetToRemoveCentralPoint(bool toRemove)
330 {
331   if ( myToRemoveCentralPoint != toRemove ) {
332     myToRemoveCentralPoint = toRemove;
333     NotifySubMeshesHypothesisModification();
334   }
335 }
336
337 //=======================================================================
338 //function : GetToRemoveCentralPoint
339 //=======================================================================
340
341 bool GHS3DPlugin_Hypothesis::GetToRemoveCentralPoint() const
342 {
343   return myToRemoveCentralPoint;
344 }
345
346 //=======================================================================
347 //function : SetTextOption
348 //=======================================================================
349
350 void GHS3DPlugin_Hypothesis::SetTextOption(const std::string& option)
351 {
352   if ( myTextOption != option ) {
353     myTextOption = option;
354     NotifySubMeshesHypothesisModification();
355   }
356 }
357
358 //=======================================================================
359 //function : GetTextOption
360 //=======================================================================
361
362 std::string GHS3DPlugin_Hypothesis::GetTextOption() const
363 {
364   return myTextOption;
365 }
366
367 //=======================================================================
368 //function : SetGradation
369 //=======================================================================
370
371 void GHS3DPlugin_Hypothesis::SetGradation(double gradation)
372 {
373   if ( myGradation != gradation ) {
374     myGradation = gradation;
375     NotifySubMeshesHypothesisModification();
376   }
377 }
378
379 //=======================================================================
380 //function : GetGradation
381 //=======================================================================
382
383 double GHS3DPlugin_Hypothesis::GetGradation() const
384 {
385   return myGradation;
386 }
387
388 //=======================================================================
389 //function : SetStandardOutputLog
390 //=======================================================================
391
392 void GHS3DPlugin_Hypothesis::SetStandardOutputLog(bool logInStandardOutput)
393 {
394   if ( myLogInStandardOutput != logInStandardOutput ) {
395     myLogInStandardOutput = logInStandardOutput;
396     NotifySubMeshesHypothesisModification();
397   }
398 }
399
400 //=======================================================================
401 //function : GetStandardOutputLog
402 //=======================================================================
403
404 bool GHS3DPlugin_Hypothesis::GetStandardOutputLog() const
405 {
406   return myLogInStandardOutput;
407 }
408
409 //=======================================================================
410 //function : SetRemoveLogOnSuccess
411 //=======================================================================
412
413 void GHS3DPlugin_Hypothesis::SetRemoveLogOnSuccess(bool removeLogOnSuccess)
414 {
415   if ( myRemoveLogOnSuccess != removeLogOnSuccess ) {
416     myRemoveLogOnSuccess = removeLogOnSuccess;
417     NotifySubMeshesHypothesisModification();
418   }
419 }
420
421 //=======================================================================
422 //function : GetRemoveLogOnSuccess
423 //=======================================================================
424
425 bool GHS3DPlugin_Hypothesis::GetRemoveLogOnSuccess() const
426 {
427   return myRemoveLogOnSuccess;
428 }
429
430 //=======================================================================
431 //function : SetEnforcedVertex
432 //=======================================================================
433
434 bool GHS3DPlugin_Hypothesis::SetEnforcedVertex(std::string theName, std::string theEntry, std::string theGroupName,
435                                                double size, double x, double y, double z, bool isCompound)
436 {
437   MESSAGE("GHS3DPlugin_Hypothesis::SetEnforcedVertex(\""<< theName << "\", \""<< theEntry << "\", \"" << theGroupName << "\", "
438                                                       << size << ", " << x << ", " << y << ", " << z  << ", "<< isCompound << ")");
439
440   bool toNotify = false;
441   bool toCreate = true;
442
443   TGHS3DEnforcedVertex *oldEnVertex;
444   TGHS3DEnforcedVertex *newEnfVertex = new TGHS3DEnforcedVertex();
445   newEnfVertex->name = theName;
446   newEnfVertex->geomEntry = theEntry;
447   newEnfVertex->coords.clear();
448   if (!isCompound) {
449     newEnfVertex->coords.push_back(x);
450     newEnfVertex->coords.push_back(y);
451     newEnfVertex->coords.push_back(z);
452   }
453   newEnfVertex->groupName = theGroupName;
454   newEnfVertex->size = size;
455   newEnfVertex->isCompound = isCompound;
456   
457   
458   // update _enfVertexList
459   TGHS3DEnforcedVertexList::iterator it = _enfVertexList.find(newEnfVertex);
460   if (it != _enfVertexList.end()) {
461     toCreate = false;
462     oldEnVertex = (*it);
463     MESSAGE("Enforced Vertex was found => Update");
464     if (oldEnVertex->name != theName) {
465       MESSAGE("Update name from \"" << oldEnVertex->name << "\" to \"" << theName << "\"");
466       oldEnVertex->name = theName;
467       toNotify = true;
468     }
469     if (oldEnVertex->groupName != theGroupName) {
470       MESSAGE("Update group name from \"" << oldEnVertex->groupName << "\" to \"" << theGroupName << "\"");
471       oldEnVertex->groupName = theGroupName;
472       toNotify = true;
473     }
474     if (oldEnVertex->size != size) {
475       MESSAGE("Update size from \"" << oldEnVertex->size << "\" to \"" << size << "\"");
476       oldEnVertex->size = size;
477       toNotify = true;
478     }
479     if (toNotify) {
480       // update map coords / enf vertex if needed
481       if (oldEnVertex->coords.size()) {
482         _coordsEnfVertexMap[oldEnVertex->coords] = oldEnVertex;
483         _enfVertexCoordsSizeList[oldEnVertex->coords] = size;
484       }
485
486       // update map geom entry / enf vertex if needed
487       if (oldEnVertex->geomEntry != "") {
488         _geomEntryEnfVertexMap[oldEnVertex->geomEntry] = oldEnVertex;
489         _enfVertexEntrySizeList[oldEnVertex->geomEntry] = size;
490       }
491     }
492   }
493
494 //   //////// CREATE ////////////
495   if (toCreate) {
496     toNotify = true;
497     MESSAGE("Creating new enforced vertex");
498     _enfVertexList.insert(newEnfVertex);
499     if (theEntry == "") {
500       _coordsEnfVertexMap[newEnfVertex->coords] = newEnfVertex;
501       _enfVertexCoordsSizeList[newEnfVertex->coords] = size;
502     }
503     else {
504       _geomEntryEnfVertexMap[newEnfVertex->geomEntry] = newEnfVertex;
505       _enfVertexEntrySizeList[newEnfVertex->geomEntry] = size;
506     }
507   }
508
509   if (toNotify)
510     NotifySubMeshesHypothesisModification();
511
512   MESSAGE("GHS3DPlugin_Hypothesis::SetEnforcedVertex END");
513   return toNotify;
514 }
515
516
517 //=======================================================================
518 //function : SetEnforcedMesh
519 //=======================================================================
520 bool GHS3DPlugin_Hypothesis::SetEnforcedMesh(SMESH_Mesh& theMesh, SMESH::ElementType elementType, std::string name, std::string entry, std::string groupName)
521 {
522   TIDSortedElemSet theElemSet;
523   SMDS_ElemIteratorPtr eIt = theMesh.GetMeshDS()->elementsIterator(SMDSAbs_ElementType(elementType));
524   while ( eIt->more() )
525     theElemSet.insert( eIt->next() );
526   MESSAGE("Add "<<theElemSet.size()<<" types["<<elementType<<"] from source mesh");
527   bool added = SetEnforcedElements( theElemSet, elementType, groupName);
528   if (added) {
529     TGHS3DEnforcedMesh* newEnfMesh = new TGHS3DEnforcedMesh();
530     newEnfMesh->persistID   = theMesh.GetMeshDS()->GetPersistentId();
531     newEnfMesh->name        = name;
532     newEnfMesh->entry       = entry;
533     newEnfMesh->elementType = elementType;
534     newEnfMesh->groupName   = groupName;
535     
536     TGHS3DEnforcedMeshList::iterator it = _enfMeshList.find(newEnfMesh);
537     if (it == _enfMeshList.end()) {
538       _entryEnfMeshMap[entry].insert(newEnfMesh);
539       _enfMeshList.insert(newEnfMesh);
540     }
541     else {
542       delete newEnfMesh;
543     }
544   }
545   return added;
546 }
547
548 //=======================================================================
549 //function : SetEnforcedGroup
550 //=======================================================================
551 bool GHS3DPlugin_Hypothesis::SetEnforcedGroup(const SMESHDS_Mesh* theMeshDS, SMESH::long_array_var theIDs, SMESH::ElementType elementType, std::string name, std::string entry, std::string groupName)
552 {
553   MESSAGE("GHS3DPlugin_Hypothesis::SetEnforcedGroup");
554   TIDSortedElemSet theElemSet;
555     if ( theIDs->length() == 0 ){MESSAGE("The source group is empty");}
556     for ( CORBA::ULong i=0; i < theIDs->length(); i++) {
557       CORBA::Long ind = theIDs[i];
558       if (elementType == SMESH::NODE)
559       {
560         const SMDS_MeshNode * node = theMeshDS->FindNode(ind);
561         if (node)
562           theElemSet.insert( node );
563       }
564       else
565       {
566         const SMDS_MeshElement * elem = theMeshDS->FindElement(ind);
567         if (elem)
568           theElemSet.insert( elem );
569       }
570     }
571
572 //   SMDS_ElemIteratorPtr it = theGroup->GetGroupDS()->GetElements();
573 //   while ( it->more() ) 
574 //     theElemSet.insert( it->next() );
575
576   MESSAGE("Add "<<theElemSet.size()<<" types["<<elementType<<"] from source group ");
577   bool added = SetEnforcedElements( theElemSet, elementType, groupName);
578   if (added) {
579     TGHS3DEnforcedMesh* newEnfMesh = new TGHS3DEnforcedMesh();
580     newEnfMesh->name = name;
581     newEnfMesh->entry = entry;
582     newEnfMesh->elementType = elementType;
583     newEnfMesh->groupName = groupName;
584     
585     TGHS3DEnforcedMeshList::iterator it = _enfMeshList.find(newEnfMesh);
586     if (it == _enfMeshList.end()) {
587       _entryEnfMeshMap[entry].insert(newEnfMesh);
588       _enfMeshList.insert(newEnfMesh);
589     }
590   }
591   return added;
592 }
593
594 //=======================================================================
595 //function : SetEnforcedElements
596 //=======================================================================
597 bool GHS3DPlugin_Hypothesis::SetEnforcedElements(TIDSortedElemSet theElemSet, SMESH::ElementType elementType, std::string groupName)
598 {
599   MESSAGE("GHS3DPlugin_Hypothesis::SetEnforcedElements");
600   TIDSortedElemSet::const_iterator it = theElemSet.begin();
601   const SMDS_MeshElement* elem;
602   const SMDS_MeshNode* node;
603   bool added = true;
604   pair<TIDSortedNodeGroupMap::iterator,bool> nodeRet;
605   pair<TIDSortedElemGroupMap::iterator,bool> elemRet;
606
607   for (;it != theElemSet.end();++it)
608   {
609     elem = (*it);
610     switch (elementType) {
611       case SMESH::NODE:
612         node = dynamic_cast<const SMDS_MeshNode*>(elem);
613         if (node) {
614           nodeRet = _enfNodes.insert(make_pair(node,groupName));
615           added = added && nodeRet.second;
616           string msg = added ? "yes":"no";
617           MESSAGE( "Node (" <<node->X()<<","<<node->Y()<<","<<node->Z()<< ") with ID " << node->GetID() <<" added ? " << msg);
618         }
619         else {
620           SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
621           for (;nodeIt->more();) {
622             node = dynamic_cast<const SMDS_MeshNode*>(nodeIt->next());
623             nodeRet = _enfNodes.insert(make_pair(node,groupName));
624             added = added && nodeRet.second;
625           }
626 //          added = true;s
627         }
628         break;
629       case SMESH::EDGE:
630         if (elem->GetType() == SMDSAbs_Edge) {
631           elemRet = _enfEdges.insert(make_pair(elem,groupName));
632           added = added && elemRet.second;
633         }
634         else if (elem->GetType() > SMDSAbs_Edge) {
635           SMDS_ElemIteratorPtr it = elem->edgesIterator();
636           for (;it->more();) {
637             const SMDS_MeshElement* anEdge = it->next();
638             elemRet = _enfEdges.insert(make_pair(anEdge,groupName));
639             added = added && elemRet.second;
640           }
641         }
642         break;
643       case SMESH::FACE:
644         if (elem->GetType() == SMDSAbs_Face)
645         {
646           if (elem->NbCornerNodes() == 3) {
647             elemRet = _enfTriangles.insert(make_pair(elem,groupName));
648             added = added && elemRet.second;
649           }
650         }
651         else if (elem->GetType() > SMDSAbs_Face) { // Group of faces
652           SMDS_ElemIteratorPtr it = elem->facesIterator();
653           for (;it->more();) {
654             const SMDS_MeshElement* aFace = it->next();
655             if (aFace->NbCornerNodes() == 3) {
656               elemRet = _enfTriangles.insert(make_pair(aFace,groupName));
657               added = added && elemRet.second;
658             }
659           }
660         }
661         break;
662       default:
663         break;
664     };
665   }
666   if (added)
667     NotifySubMeshesHypothesisModification();
668   return added;
669 }
670
671
672 //=======================================================================
673 //function : GetEnforcedVertex
674 //=======================================================================
675
676 GHS3DPlugin_Hypothesis::TGHS3DEnforcedVertex* GHS3DPlugin_Hypothesis::GetEnforcedVertex(double x, double y, double z)
677   throw (std::invalid_argument)
678 {
679   std::vector<double> coord(3);
680   coord[0] = x;
681   coord[1] = y;
682   coord[2] = z;
683   if (_coordsEnfVertexMap.count(coord)>0)
684     return _coordsEnfVertexMap[coord];
685   std::ostringstream msg ;
686   msg << "No enforced vertex at " << x << ", " << y << ", " << z;
687   throw std::invalid_argument(msg.str());
688 }
689
690 GHS3DPlugin_Hypothesis::TGHS3DEnforcedVertex* GHS3DPlugin_Hypothesis::GetEnforcedVertex(const std::string theEntry)
691   throw (std::invalid_argument)
692 {
693   if (_geomEntryEnfVertexMap.count(theEntry)>0)
694     return _geomEntryEnfVertexMap[theEntry];
695   
696   std::ostringstream msg ;
697   msg << "No enforced vertex with entry " << theEntry;
698   throw std::invalid_argument(msg.str());
699 }
700
701 //=======================================================================
702 //function : RemoveEnforcedVertex
703 //=======================================================================
704
705 bool GHS3DPlugin_Hypothesis::RemoveEnforcedVertex(double x, double y, double z, const std::string theEntry)
706   throw (std::invalid_argument)
707 {
708   bool toNotify = false;
709   std::ostringstream msg;
710   TGHS3DEnforcedVertex *oldEnfVertex;
711   std::vector<double> coords(3);
712   coords[0] = x;
713   coords[1] = y;
714   coords[2] = z;
715   
716   // check that enf vertex with given enf vertex entry exists
717   TGeomEntryGHS3DEnforcedVertexMap::iterator it_enfVertexEntry = _geomEntryEnfVertexMap.find(theEntry);
718   if (it_enfVertexEntry != _geomEntryEnfVertexMap.end()) {
719     // Success
720     MESSAGE("Found enforced vertex with geom entry " << theEntry);
721     oldEnfVertex = it_enfVertexEntry->second;
722     _geomEntryEnfVertexMap.erase(it_enfVertexEntry);
723   } else {
724     // Fail
725     MESSAGE("Enforced vertex with geom entry " << theEntry << " not found");
726     // check that enf vertex with given coords exists
727     TCoordsGHS3DEnforcedVertexMap::iterator it_coords_enf = _coordsEnfVertexMap.find(coords);
728     if (it_coords_enf != _coordsEnfVertexMap.end()) {
729       // Success
730       MESSAGE("Found enforced vertex with coords " << x << ", " << y << ", " << z);
731       oldEnfVertex = it_coords_enf->second;
732       _coordsEnfVertexMap.erase(it_coords_enf);
733       _enfVertexCoordsSizeList.erase(_enfVertexCoordsSizeList.find(coords));
734     } else {
735       // Fail
736       MESSAGE("Enforced vertex with coords " << x << ", " << y << ", " << z << " not found");
737       throw std::invalid_argument(msg.str());
738     }
739   }
740
741   MESSAGE("Remove enf vertex from _enfVertexList");
742
743   // update _enfVertexList
744   TGHS3DEnforcedVertexList::iterator it = _enfVertexList.find(oldEnfVertex);
745   if (it != _enfVertexList.end()) {
746     if ((*it)->groupName != "")
747       _groupsToRemove.insert((*it)->groupName);
748     _enfVertexList.erase(it);
749     toNotify = true;
750     MESSAGE("Done");
751   }
752
753   if (toNotify)
754     NotifySubMeshesHypothesisModification();
755
756   return toNotify;
757 }
758
759 //=======================================================================
760 //function : ClearEnforcedVertices
761 //=======================================================================
762 void GHS3DPlugin_Hypothesis::ClearEnforcedVertices()
763 {
764   TGHS3DEnforcedVertexList::const_iterator it = _enfVertexList.begin();
765   for(;it != _enfVertexList.end();++it) {
766     if ((*it)->groupName != "")
767       _groupsToRemove.insert((*it)->groupName);
768   }
769   _enfVertexList.clear();
770   _coordsEnfVertexMap.clear();
771   _geomEntryEnfVertexMap.clear();
772   _enfVertexCoordsSizeList.clear();
773   _enfVertexEntrySizeList.clear();
774   NotifySubMeshesHypothesisModification();
775 }
776
777 //=======================================================================
778 //function : ClearEnforcedMeshes
779 //=======================================================================
780 void GHS3DPlugin_Hypothesis::ClearEnforcedMeshes()
781 {
782   TGHS3DEnforcedMeshList::const_iterator it = _enfMeshList.begin();
783   for(;it != _enfMeshList.end();++it) {
784     if ((*it)->groupName != "")
785       _groupsToRemove.insert((*it)->groupName);
786   }
787   _enfNodes.clear();
788   _enfEdges.clear();
789   _enfTriangles.clear();
790   _nodeIDToSizeMap.clear();
791   _enfMeshList.clear();
792   _entryEnfMeshMap.clear();
793   NotifySubMeshesHypothesisModification();
794 }
795
796 //================================================================================
797 /*!
798  * \brief At mesh loading, restore enforced elements by just loaded enforced meshes
799  */
800 //================================================================================
801
802 void GHS3DPlugin_Hypothesis::RestoreEnfElemsByMeshes()
803 {
804   TGHS3DEnforcedMeshList::const_iterator it = _enfMeshList.begin();
805   for(;it != _enfMeshList.end();++it) {
806     TGHS3DEnforcedMesh* enfMesh = *it;
807     if ( SMESH_Mesh* mesh = GetMeshByPersistentID( enfMesh->persistID ))
808       SetEnforcedMesh( *mesh,
809                        enfMesh->elementType,
810                        enfMesh->name,
811                        enfMesh->entry,
812                        enfMesh->groupName );
813     enfMesh->persistID = -1; // not to restore again
814   }
815 }
816
817 //=======================================================================
818 //function : SetGroupsToRemove
819 //=======================================================================
820
821 void GHS3DPlugin_Hypothesis::ClearGroupsToRemove()
822 {
823   _groupsToRemove.clear();
824 }
825
826
827 //=======================================================================
828 //function : DefaultMeshHoles
829 //=======================================================================
830
831 bool GHS3DPlugin_Hypothesis::DefaultMeshHoles()
832 {
833   return false; // PAL19680
834 }
835
836 //=======================================================================
837 //function : DefaultToMakeGroupsOfDomains
838 //=======================================================================
839
840 bool GHS3DPlugin_Hypothesis::DefaultToMakeGroupsOfDomains()
841 {
842   return false; // issue 0022172
843 }
844
845 //=======================================================================
846 //function : DefaultMaximumMemory
847 //=======================================================================
848
849 #ifndef WIN32
850 #include <sys/sysinfo.h>
851 #else
852 #include <windows.h>
853 #endif
854
855 long  GHS3DPlugin_Hypothesis::DefaultMaximumMemory()
856 {
857 #ifndef WIN32
858   struct sysinfo si;
859   long err = sysinfo( &si );
860   if ( err == 0 ) {
861     long ramMB = si.totalram * si.mem_unit / 1024 / 1024;
862     return ( 0.7 * ramMB );
863   }
864 #else
865   // See http://msdn.microsoft.com/en-us/library/aa366589.aspx
866   MEMORYSTATUSEX statex;
867   statex.dwLength = sizeof (statex);
868   long err = GlobalMemoryStatusEx (&statex);
869   if (err != 0) {
870     long totMB = 
871       statex.ullTotalPhys / 1024 / 1024 +
872       statex.ullTotalPageFile / 1024 / 1024 +
873       statex.ullTotalVirtual / 1024 / 1024;
874     return ( 0.7 * totMB );
875   }
876 #endif
877   return 1024;
878 }
879
880 //=======================================================================
881 //function : DefaultInitialMemory
882 //=======================================================================
883
884 long  GHS3DPlugin_Hypothesis::DefaultInitialMemory()
885 {
886   return DefaultMaximumMemory();
887 }
888
889 //=======================================================================
890 //function : DefaultOptimizationLevel
891 //=======================================================================
892
893 short  GHS3DPlugin_Hypothesis::DefaultOptimizationLevel()
894 {
895   return Medium;
896 }
897
898 //=======================================================================
899 //function : DefaultWorkingDirectory
900 //=======================================================================
901
902 std::string GHS3DPlugin_Hypothesis::DefaultWorkingDirectory()
903 {
904   TCollection_AsciiString aTmpDir;
905
906   char *Tmp_dir = getenv("SALOME_TMP_DIR");
907   if(Tmp_dir != NULL) {
908     aTmpDir = Tmp_dir;
909   }
910   else {
911 #ifdef WIN32
912     aTmpDir = TCollection_AsciiString("C:\\");
913 #else
914     aTmpDir = TCollection_AsciiString("/tmp/");
915 #endif
916   }
917   return aTmpDir.ToCString();
918 }
919
920 //=======================================================================
921 //function : DefaultKeepFiles
922 //=======================================================================
923
924 bool   GHS3DPlugin_Hypothesis::DefaultKeepFiles()
925 {
926   return false;
927 }
928
929 //=======================================================================
930 //function : DefaultRemoveLogOnSuccess
931 //=======================================================================
932
933 bool   GHS3DPlugin_Hypothesis::DefaultRemoveLogOnSuccess()
934 {
935   return false;
936 }
937
938
939 //=======================================================================
940 //function : DefaultVerboseLevel
941 //=======================================================================
942
943 short  GHS3DPlugin_Hypothesis::DefaultVerboseLevel()
944 {
945   return 10;
946 }
947
948 //=======================================================================
949 //function : DefaultToCreateNewNodes
950 //=======================================================================
951
952 bool GHS3DPlugin_Hypothesis::DefaultToCreateNewNodes()
953 {
954   return true;
955 }
956
957 //=======================================================================
958 //function : DefaultToUseBoundaryRecoveryVersion
959 //=======================================================================
960
961 bool GHS3DPlugin_Hypothesis::DefaultToUseBoundaryRecoveryVersion()
962 {
963   return false;
964 }
965
966 //=======================================================================
967 //function : DefaultToUseFEMCorrection
968 //=======================================================================
969
970 bool GHS3DPlugin_Hypothesis::DefaultToUseFEMCorrection()
971 {
972   return false;
973 }
974
975 //=======================================================================
976 //function : DefaultToRemoveCentralPoint
977 //=======================================================================
978
979 bool GHS3DPlugin_Hypothesis::DefaultToRemoveCentralPoint()
980 {
981   return false;
982 }
983
984 //=======================================================================
985 //function : DefaultGradation
986 //=======================================================================
987
988 double GHS3DPlugin_Hypothesis::DefaultGradation()
989 {
990   return 1.05;
991 }
992
993 //=======================================================================
994 //function : DefaultStandardOutputLog
995 //=======================================================================
996
997 bool GHS3DPlugin_Hypothesis::DefaultStandardOutputLog()
998 {
999   return false;
1000 }
1001
1002 // //=======================================================================
1003 // //function : DefaultID2SizeMap
1004 // //=======================================================================
1005 // 
1006 // GHS3DPlugin_Hypothesis::TID2SizeMap GHS3DPlugin_Hypothesis::DefaultID2SizeMap()
1007 // {
1008 //   return GHS3DPlugin_Hypothesis::TID2SizeMap();
1009 // }
1010
1011
1012 //=======================================================================
1013 //function : SaveTo
1014 //=======================================================================
1015
1016 std::ostream & GHS3DPlugin_Hypothesis::SaveTo(std::ostream & save)
1017 {
1018   save << (int) myToMeshHoles                 << " ";
1019   save << myMaximumMemory                     << " ";
1020   save << myInitialMemory                     << " ";
1021   save << myOptimizationLevel                 << " ";
1022   save << myWorkingDirectory                  << " ";
1023   save << (int)myKeepFiles                    << " ";
1024   save << myVerboseLevel                      << " ";
1025   save << (int)myToCreateNewNodes             << " ";
1026   save << (int)myToUseBoundaryRecoveryVersion << " ";
1027   save << (int)myToUseFemCorrection           << " ";
1028   save << (int)myToRemoveCentralPoint         << " ";
1029   save << myGradation                         << " ";
1030   save << myToMakeGroupsOfDomains             << " ";
1031   if (!myTextOption.empty()) {
1032     save << "__OPTIONS_BEGIN__ ";
1033     save << myTextOption                      << " ";
1034     save << "__OPTIONS_END__ ";
1035   }
1036   
1037
1038   TGHS3DEnforcedVertexList::iterator it  = _enfVertexList.begin();
1039   if (it != _enfVertexList.end()) {
1040     save << " " << "__ENFORCED_VERTICES_BEGIN__ ";
1041     for ( ; it != _enfVertexList.end(); ++it ) {
1042       TGHS3DEnforcedVertex *enfVertex = (*it);
1043       save << " " << "__BEGIN_VERTEX__";
1044       if (!enfVertex->name.empty()) {
1045         save << " " << "__BEGIN_NAME__";
1046         save << " " << enfVertex->name;
1047         save << " " << "__END_NAME__";
1048       }
1049       if (!enfVertex->geomEntry.empty()) {
1050         save << " " << "__BEGIN_ENTRY__";
1051         save << " " << enfVertex->geomEntry;
1052         save << " " << enfVertex->isCompound;
1053         save << " " << "__END_ENTRY__";
1054       }
1055       if (!enfVertex->groupName.empty()) {
1056         save << " " << "__BEGIN_GROUP__";
1057         save << " " << enfVertex->groupName;
1058         save << " " << "__END_GROUP__";
1059       }
1060       if (enfVertex->coords.size()) {
1061         save << " " << "__BEGIN_COORDS__";
1062         for ( size_t i = 0; i < enfVertex->coords.size(); i++ )
1063           save << " " << enfVertex->coords[i];
1064         save << " " << "__END_COORDS__";
1065       }
1066       save << " " << "__BEGIN_SIZE__";
1067       save << " " << enfVertex->size;
1068       save << " " << "__END_SIZE__";
1069       save << " " << "__END_VERTEX__";
1070     }
1071     save << " " << "__ENFORCED_VERTICES_END__ ";
1072   }
1073
1074   TGHS3DEnforcedMeshList::iterator it_mesh  = _enfMeshList.begin();
1075   if (it_mesh != _enfMeshList.end()) {
1076     save << " " << "__ENFORCED_MESHES_BEGIN__ ";
1077     for ( ; it_mesh != _enfMeshList.end(); ++it_mesh ) {
1078       TGHS3DEnforcedMesh *enfMesh = (*it_mesh);
1079       save << " " << "__BEGIN_ENF_MESH__";
1080
1081       save << " " << "__BEGIN_NAME__";
1082       save << " " << enfMesh->name;
1083       save << " " << "__END_NAME__";
1084
1085       save << " " << "__BEGIN_ENTRY__";
1086       save << " " << enfMesh->entry;
1087       save << " " << "__END_ENTRY__";
1088
1089       save << " " << "__BEGIN_ELEM_TYPE__";
1090       save << " " << (int)enfMesh->elementType;
1091       save << " " << "__END_ELEM_TYPE__";
1092
1093       if (!enfMesh->groupName.empty()) {
1094         save << " " << "__BEGIN_GROUP__";
1095         save << " " << enfMesh->groupName;
1096         save << " " << "__END_GROUP__";
1097       }
1098       save << " " << "__PERSIST_ID__";
1099       save << " " << enfMesh->persistID;
1100       save << " " << "__END_ENF_MESH__";
1101       std::cout << "Saving of enforced mesh " << enfMesh->name.c_str() << " done" << std::endl;
1102     }
1103     save << " "  << "__ENFORCED_MESHES_END__ ";
1104   }
1105   return save;
1106 }
1107
1108 //=======================================================================
1109 //function : LoadFrom
1110 //=======================================================================
1111
1112 std::istream & GHS3DPlugin_Hypothesis::LoadFrom(std::istream & load)
1113 {
1114   bool isOK = true;
1115   int i;
1116   double d;
1117
1118   isOK = static_cast<bool>(load >> i);
1119   if (isOK)
1120     myToMeshHoles = i;
1121   else
1122     load.clear(ios::badbit | load.rdstate());
1123
1124   isOK = static_cast<bool>(load >> d);
1125   if (isOK)
1126     myMaximumMemory = d;
1127   else
1128     load.clear(ios::badbit | load.rdstate());
1129
1130   isOK = static_cast<bool>(load >> d);
1131   if (isOK)
1132     myInitialMemory = d;
1133   else
1134     load.clear(ios::badbit | load.rdstate());
1135
1136   isOK = static_cast<bool>(load >> i);
1137   if (isOK)
1138     myOptimizationLevel = i;
1139   else
1140     load.clear(ios::badbit | load.rdstate());
1141
1142   isOK = static_cast<bool>(load >> myWorkingDirectory);
1143   if (isOK) {
1144     if ( myWorkingDirectory == "0") { // myWorkingDirectory was empty
1145       myKeepFiles = false;
1146       myWorkingDirectory.clear();
1147     }
1148     else if ( myWorkingDirectory == "1" ) {
1149       myKeepFiles = true;
1150       myWorkingDirectory.clear();
1151     }
1152   }
1153   else
1154     load.clear(ios::badbit | load.rdstate());
1155
1156   if ( !myWorkingDirectory.empty() ) {
1157     isOK = static_cast<bool>(load >> i);
1158     if (isOK)
1159       myKeepFiles = i;
1160     else
1161       load.clear(ios::badbit | load.rdstate());
1162   }
1163
1164   isOK = static_cast<bool>(load >> i);
1165   if (isOK)
1166     myVerboseLevel = (short) i;
1167   else
1168     load.clear(ios::badbit | load.rdstate());
1169
1170   isOK = static_cast<bool>(load >> i);
1171   if (isOK)
1172     myToCreateNewNodes = (bool) i;
1173   else
1174     load.clear(ios::badbit | load.rdstate());
1175
1176   isOK = static_cast<bool>(load >> i);
1177   if (isOK)
1178     myToUseBoundaryRecoveryVersion = (bool) i;
1179   else
1180     load.clear(ios::badbit | load.rdstate());
1181
1182   isOK = static_cast<bool>(load >> i);
1183   if (isOK)
1184     myToUseFemCorrection = (bool) i;
1185   else
1186     load.clear(ios::badbit | load.rdstate());
1187
1188   isOK = static_cast<bool>(load >> i);
1189   if (isOK)
1190     myToRemoveCentralPoint = (bool) i;
1191   else
1192     load.clear(ios::badbit | load.rdstate());
1193
1194   isOK = static_cast<bool>(load >> d);
1195   if (isOK)
1196     myGradation = d;
1197   else
1198     load.clear(ios::badbit | load.rdstate());
1199
1200   std::string separator;
1201   bool hasOptions = false;
1202   bool hasEnforcedVertices = false;
1203   bool hasEnforcedMeshes = false;
1204   isOK = static_cast<bool>(load >> separator);
1205
1206   if ( isOK && ( separator == "0" || separator == "1" ))
1207   {
1208     myToMakeGroupsOfDomains = ( separator == "1" );
1209     isOK = static_cast<bool>(load >> separator);
1210   }
1211
1212   if (isOK) {
1213     if (separator == "__OPTIONS_BEGIN__")
1214       hasOptions = true;
1215     else if (separator == "__ENFORCED_VERTICES_BEGIN__")
1216       hasEnforcedVertices = true;
1217     else if (separator == "__ENFORCED_MESHES_BEGIN__")
1218       hasEnforcedMeshes = true;
1219   }
1220
1221   if (hasOptions) {
1222     std::string txt;
1223     while (isOK) {
1224       isOK = static_cast<bool>(load >> txt);
1225       if (isOK) {
1226         if (txt == "__OPTIONS_END__") {
1227           if (!myTextOption.empty()) {
1228             // Remove last space
1229             myTextOption.erase(myTextOption.end()-1);
1230           }
1231           isOK = false;
1232           break;
1233         }
1234         myTextOption += txt;
1235         myTextOption += " ";
1236       }
1237     }
1238   }
1239
1240   if (hasOptions) {
1241     isOK = static_cast<bool>(load >> separator);
1242     if (isOK && separator == "__ENFORCED_VERTICES_BEGIN__")
1243       hasEnforcedVertices = true;
1244     if (isOK && separator == "__ENFORCED_MESHES_BEGIN__")
1245       hasEnforcedMeshes = true;
1246   }
1247
1248   if (hasEnforcedVertices) {
1249     std::string txt, name, entry, groupName;
1250     double size, coords[3];
1251     bool isCompound;
1252     bool hasCoords = false;
1253     isOK = static_cast<bool>(load >> txt);  // __BEGIN_VERTEX__
1254     while (isOK) {
1255       if (txt == "__ENFORCED_VERTICES_END__") {
1256         //isOK = false;
1257         break;
1258       }
1259       
1260       TGHS3DEnforcedVertex *enfVertex = new TGHS3DEnforcedVertex();
1261       while (isOK) {
1262         isOK = static_cast<bool>(load >> txt);
1263         if (txt == "__END_VERTEX__") {
1264           enfVertex->name = name;
1265           enfVertex->geomEntry = entry;
1266           enfVertex->isCompound = isCompound;
1267           enfVertex->groupName = groupName;
1268           enfVertex->coords.clear();
1269           if (hasCoords)
1270             enfVertex->coords.assign(coords,coords+3);
1271
1272           _enfVertexList.insert(enfVertex);
1273
1274           if (enfVertex->coords.size())
1275             _coordsEnfVertexMap[enfVertex->coords] = enfVertex;
1276           if (!enfVertex->geomEntry.empty())
1277             _geomEntryEnfVertexMap[enfVertex->geomEntry] = enfVertex;
1278
1279           name.clear();
1280           entry.clear();
1281           groupName.clear();
1282           hasCoords = false;
1283           isOK = false;
1284         }
1285
1286         if (txt == "__BEGIN_NAME__") {  // __BEGIN_NAME__
1287           while (isOK && (txt != "__END_NAME__")) {
1288             isOK = static_cast<bool>(load >> txt);
1289             if (txt != "__END_NAME__") {
1290               if (!name.empty())
1291                 name += " ";
1292               name += txt;
1293             }
1294           }
1295           MESSAGE("name: " <<name);
1296         }
1297
1298         if (txt == "__BEGIN_ENTRY__") {  // __BEGIN_ENTRY__
1299           isOK = static_cast<bool>(load >> entry);
1300           isOK = static_cast<bool>(load >> isCompound);
1301           isOK = static_cast<bool>(load >> txt); // __END_ENTRY__
1302           if (txt != "__END_ENTRY__")
1303             throw std::exception();
1304           MESSAGE("entry: " << entry);
1305         }
1306
1307         if (txt == "__BEGIN_GROUP__") {  // __BEGIN_GROUP__
1308           while (isOK && (txt != "__END_GROUP__")) {
1309             isOK = static_cast<bool>(load >> txt);
1310             if (txt != "__END_GROUP__") {
1311               if (!groupName.empty())
1312                 groupName += " ";
1313               groupName += txt;
1314             }
1315           }
1316           MESSAGE("groupName: " << groupName);
1317         }
1318
1319         if (txt == "__BEGIN_COORDS__") {  // __BEGIN_COORDS__
1320           hasCoords = true;
1321           isOK = static_cast<bool>(load >> coords[0] >> coords[1] >> coords[2]);
1322           isOK = static_cast<bool>(load >> txt); // __END_COORDS__
1323           if (txt != "__END_COORDS__")
1324             throw std::exception();
1325           MESSAGE("coords: " << coords[0] <<","<< coords[1] <<","<< coords[2]);
1326         }
1327
1328         if (txt == "__BEGIN_SIZE__") {  // __BEGIN_ENTRY__
1329           isOK = static_cast<bool>(load >> size);
1330           isOK = static_cast<bool>(load >> txt); // __END_ENTRY__
1331           if (txt != "__END_SIZE__") {
1332             throw std::exception();
1333           }
1334           MESSAGE("size: " << size);
1335         }
1336       }
1337       isOK = static_cast<bool>(load >> txt);  // __BEGIN_VERTEX__
1338     }
1339   }
1340
1341   if (hasEnforcedVertices) {
1342     isOK = static_cast<bool>(load >> separator);
1343     if (isOK && separator == "__ENFORCED_MESHES_BEGIN__")
1344       hasEnforcedMeshes = true;
1345   }
1346
1347   if (hasEnforcedMeshes) {
1348     std::string txt, name, entry, groupName;
1349     int elementType = -1, persistID = -1;
1350     isOK = static_cast<bool>(load >> txt);  // __BEGIN_ENF_MESH__
1351     while (isOK) {
1352       //                if (isOK) {
1353       if (txt == "__ENFORCED_MESHES_END__")
1354         isOK = false;
1355
1356       TGHS3DEnforcedMesh *enfMesh = new TGHS3DEnforcedMesh();
1357       while (isOK) {
1358         isOK = static_cast<bool>(load >> txt);
1359         if (txt == "__END_ENF_MESH__") {
1360           enfMesh->name = name;
1361           enfMesh->entry = entry;
1362           enfMesh->elementType = (SMESH::ElementType)elementType;
1363           enfMesh->groupName = groupName;
1364           enfMesh->persistID = persistID;
1365
1366           _enfMeshList.insert(enfMesh);
1367           std::cout << "Restoring of enforced mesh " <<name  << " done" << std::endl;
1368
1369           name.clear();
1370           entry.clear();
1371           elementType = -1;
1372           groupName.clear();
1373           persistID = -1;
1374           isOK = false;
1375         }
1376
1377         if (txt == "__BEGIN_NAME__") {  // __BEGIN_NAME__
1378           while (isOK && (txt != "__END_NAME__")) {
1379             isOK = static_cast<bool>(load >> txt);
1380             if (txt != "__END_NAME__") {
1381               if (!name.empty())
1382                 name += " ";
1383               name += txt;
1384             }
1385           }
1386           MESSAGE("name: " <<name);
1387         }
1388
1389         if (txt == "__BEGIN_ENTRY__") {  // __BEGIN_ENTRY__
1390           isOK = static_cast<bool>(load >> entry);
1391           isOK = static_cast<bool>(load >> txt); // __END_ENTRY__
1392           if (txt != "__END_ENTRY__")
1393             throw std::exception();
1394           MESSAGE("entry: " << entry);
1395         }
1396
1397         if (txt == "__BEGIN_ELEM_TYPE__") {  // __BEGIN_ELEM_TYPE__
1398           isOK = static_cast<bool>(load >> elementType);
1399           isOK = static_cast<bool>(load >> txt); // __END_ELEM_TYPE__
1400           if (txt != "__END_ELEM_TYPE__")
1401             throw std::exception();
1402           MESSAGE("elementType: " << elementType);
1403         }
1404
1405         if (txt == "__BEGIN_GROUP__") {  // __BEGIN_GROUP__
1406           while (isOK && (txt != "__END_GROUP__")) {
1407             isOK = static_cast<bool>(load >> txt);
1408             if (txt != "__END_GROUP__") {
1409               if (!groupName.empty())
1410                 groupName += " ";
1411               groupName += txt;
1412             }
1413           } // while
1414           MESSAGE("groupName: " << groupName);
1415         } // if
1416
1417         if (txt == "__PERSIST_ID__") {
1418           isOK = static_cast<bool>(load >> persistID);
1419           MESSAGE("persistID: " << persistID);
1420         }
1421         std::cout << "isOK: " << isOK << std::endl;
1422       } // while
1423       //                } // if
1424       isOK = static_cast<bool>(load >> txt);  // __BEGIN_ENF_MESH__
1425     } // while
1426   } // if
1427
1428   return load;
1429 }
1430
1431 //=======================================================================
1432 //function : SetParametersByMesh
1433 //=======================================================================
1434
1435 bool GHS3DPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* ,const TopoDS_Shape&)
1436 {
1437   return false;
1438 }
1439
1440
1441 //================================================================================
1442 /*!
1443  * \brief Sets myToMakeGroupsOfDomains depending on whether theMesh is on shape or not
1444  */
1445 //================================================================================
1446
1447 bool GHS3DPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  dflts,
1448                                                      const SMESH_Mesh* /*theMesh*/)
1449 {
1450   myToMakeGroupsOfDomains = ( !dflts._shape || dflts._shape->IsNull() );
1451   return true;
1452 }
1453
1454 //================================================================================
1455 /*!
1456  * \brief Return command to run MG-Tetra mesher excluding file prefix (-f)
1457  */
1458 //================================================================================
1459
1460 std::string GHS3DPlugin_Hypothesis::CommandToRun(const GHS3DPlugin_Hypothesis* hyp,
1461                                                  const bool         hasShapeToMesh)
1462 {
1463   TCollection_AsciiString cmd = GetExeName().c_str();
1464   // check if any option is overridden by hyp->myTextOption
1465   bool max_memory   = hyp ? ( hyp->myTextOption.find("--max_memory")  == std::string::npos ) : true;
1466   bool auto_memory   = hyp ? ( hyp->myTextOption.find("--automatic_memory")  == std::string::npos ) : true;
1467   bool comp   = hyp ? ( hyp->myTextOption.find("--components")  == std::string::npos ) : true;
1468   bool optim_level   = hyp ? ( hyp->myTextOption.find("--optimisation_level")  == std::string::npos ) : true;
1469   bool no_int_points  = hyp ? ( hyp->myTextOption.find("--no_internal_points") == std::string::npos ) : true;
1470   bool C   = hyp ? ( hyp->myTextOption.find("-C")  == std::string::npos ) : true;
1471   bool verbose   = hyp ? ( hyp->myTextOption.find("--verbose")  == std::string::npos ) : true;
1472   bool fem = hyp ? ( hyp->myTextOption.find("-FEM")== std::string::npos ) : true;
1473   bool rem = hyp ? ( hyp->myTextOption.find("--no_initial_central_point")== std::string::npos ) : true;
1474   bool gra = hyp ? ( hyp->myTextOption.find("-Dcpropa")== std::string::npos ) : true;
1475
1476   // if use boundary recovery version, few options are allowed
1477   bool useBndRecovery = !C;
1478   if ( !useBndRecovery && hyp )
1479     useBndRecovery = hyp->myToUseBoundaryRecoveryVersion;
1480
1481   // MG-Tetra needs to know amount of memory it may use (MB).
1482   // Default memory is defined at MG-Tetra installation but it may be not enough,
1483   // so allow to use about all available memory
1484   if ( max_memory ) {
1485     long aMaximumMemory = hyp ? hyp->myMaximumMemory : -1;
1486     ostringstream tmpMaximumMemory;
1487     cmd += " --max_memory ";
1488     if ( aMaximumMemory < 0 )
1489       tmpMaximumMemory << DefaultMaximumMemory();
1490     else
1491       tmpMaximumMemory << aMaximumMemory;
1492     cmd += tmpMaximumMemory.str().c_str();
1493   }
1494   if ( auto_memory && !useBndRecovery ) {
1495     long aInitialMemory = hyp ? hyp->myInitialMemory : -1;
1496     cmd += " --automatic_memory ";
1497     if ( aInitialMemory > 0 )
1498       cmd += (int)aInitialMemory;
1499     else
1500       cmd += "100";
1501   }
1502   // component to mesh
1503   if ( comp && !useBndRecovery ) {
1504     // We always run MG-Tetra with "to mesh holes'==TRUE (see PAL19680)
1505     if ( hasShapeToMesh )
1506       cmd += " --components all";
1507     else {
1508       bool aToMeshHoles = hyp ? hyp->myToMeshHoles : DefaultMeshHoles();
1509       if ( aToMeshHoles )
1510         cmd += " --components all";
1511       else
1512         cmd += " --components outside_components";
1513     }
1514   }
1515   const bool toCreateNewNodes = ( no_int_points && ( !hyp || hyp->myToCreateNewNodes ));
1516
1517   // optimization level
1518   // This option need to be improved concerning new mg-tetra version
1519   if ( optim_level && hyp && !useBndRecovery && toCreateNewNodes ) {
1520     if ( hyp->myOptimizationLevel >= 0 && hyp->myOptimizationLevel < 5 ) {
1521       const char* level[] = { "none" , "light" , "standard" , "standard+" , "strong" };
1522       cmd += " --optimisation_level ";
1523       cmd += level[ hyp->myOptimizationLevel ];
1524     }
1525   }
1526   if ( !toCreateNewNodes ) {
1527     cmd += " --optimisation_level none"; // issue 22608
1528   }
1529
1530   // to create internal nodes
1531   if ( no_int_points && !toCreateNewNodes ) {
1532     cmd += " --no_internal_points";
1533   }
1534
1535   // verbose mode
1536   if ( verbose && hyp ) {
1537     cmd += " --verbose ";
1538     cmd += hyp->myVerboseLevel;
1539   }
1540
1541   // boundary recovery version
1542   if ( useBndRecovery ) {
1543     cmd += " -C";
1544   }
1545
1546   // to use FEM correction
1547   if ( fem && hyp && hyp->myToUseFemCorrection) {
1548     cmd += " -FEM";
1549   }
1550
1551   // to remove initial central point.
1552   if ( rem && hyp && hyp->myToRemoveCentralPoint) {
1553     cmd += " --no_initial_central_point";
1554   }
1555
1556   // options as text
1557   if ( hyp && !hyp->myTextOption.empty() ) {
1558     cmd += " ";
1559     cmd += (char*) hyp->myTextOption.c_str();
1560   }
1561
1562   // to define volumic gradation.
1563   if ( gra && hyp) {
1564     cmd += " -Dcpropa=";
1565     cmd += hyp->myGradation;
1566   }
1567
1568 #ifdef WIN32
1569   cmd += " < NUL";
1570 #endif
1571
1572   return cmd.ToCString();
1573 }
1574
1575 //================================================================================
1576 /*!
1577  * \brief Return a unique file name
1578  */
1579 //================================================================================
1580
1581 std::string GHS3DPlugin_Hypothesis::GetFileName(const GHS3DPlugin_Hypothesis* hyp)
1582 {
1583   std::string aTmpDir = hyp ? hyp->GetWorkingDirectory() : DefaultWorkingDirectory();
1584   const char lastChar = *aTmpDir.rbegin();
1585 #ifdef WIN32
1586     if(lastChar != '\\') aTmpDir+='\\';
1587 #else
1588     if(lastChar != '/') aTmpDir+='/';
1589 #endif      
1590
1591   TCollection_AsciiString aGenericName = (char*)aTmpDir.c_str();
1592   aGenericName += "GHS3D_";
1593   aGenericName += getpid();
1594   aGenericName += "_";
1595   aGenericName += Abs((Standard_Integer)(long) aGenericName.ToCString());
1596
1597   return aGenericName.ToCString();
1598 }
1599
1600 //================================================================================
1601 /*
1602  * Return the name of executable
1603  */
1604 //================================================================================
1605
1606 std::string GHS3DPlugin_Hypothesis::GetExeName()
1607 {
1608   return "mg-tetra.exe";
1609 }
1610
1611 //================================================================================
1612 /*!
1613 * \brief Return the enforced vertices
1614 */
1615 //================================================================================
1616
1617 GHS3DPlugin_Hypothesis::TGHS3DEnforcedVertexList GHS3DPlugin_Hypothesis::GetEnforcedVertices(const GHS3DPlugin_Hypothesis* hyp)
1618 {
1619   return hyp ? hyp->_GetEnforcedVertices():DefaultGHS3DEnforcedVertexList();
1620 }
1621
1622 GHS3DPlugin_Hypothesis::TGHS3DEnforcedVertexCoordsValues GHS3DPlugin_Hypothesis::GetEnforcedVerticesCoordsSize (const GHS3DPlugin_Hypothesis* hyp)
1623 {  
1624   return hyp ? hyp->_GetEnforcedVerticesCoordsSize(): DefaultGHS3DEnforcedVertexCoordsValues();
1625 }
1626
1627 GHS3DPlugin_Hypothesis::TGHS3DEnforcedVertexEntryValues GHS3DPlugin_Hypothesis::GetEnforcedVerticesEntrySize (const GHS3DPlugin_Hypothesis* hyp)
1628 {  
1629   return hyp ? hyp->_GetEnforcedVerticesEntrySize(): DefaultGHS3DEnforcedVertexEntryValues();
1630 }
1631
1632 GHS3DPlugin_Hypothesis::TCoordsGHS3DEnforcedVertexMap GHS3DPlugin_Hypothesis::GetEnforcedVerticesByCoords (const GHS3DPlugin_Hypothesis* hyp)
1633 {  
1634   return hyp ? hyp->_GetEnforcedVerticesByCoords(): DefaultCoordsGHS3DEnforcedVertexMap();
1635 }
1636
1637 GHS3DPlugin_Hypothesis::TGeomEntryGHS3DEnforcedVertexMap GHS3DPlugin_Hypothesis::GetEnforcedVerticesByEntry (const GHS3DPlugin_Hypothesis* hyp)
1638 {  
1639   return hyp ? hyp->_GetEnforcedVerticesByEntry(): DefaultGeomEntryGHS3DEnforcedVertexMap();
1640 }
1641
1642 GHS3DPlugin_Hypothesis::TIDSortedNodeGroupMap GHS3DPlugin_Hypothesis::GetEnforcedNodes(const GHS3DPlugin_Hypothesis* hyp)
1643 {
1644   return hyp ? hyp->_GetEnforcedNodes():DefaultIDSortedNodeGroupMap();
1645 }
1646
1647 GHS3DPlugin_Hypothesis::TIDSortedElemGroupMap GHS3DPlugin_Hypothesis::GetEnforcedEdges(const GHS3DPlugin_Hypothesis* hyp)
1648 {
1649   return hyp ? hyp->_GetEnforcedEdges():DefaultIDSortedElemGroupMap();
1650 }
1651
1652 GHS3DPlugin_Hypothesis::TIDSortedElemGroupMap GHS3DPlugin_Hypothesis::GetEnforcedTriangles(const GHS3DPlugin_Hypothesis* hyp)
1653 {
1654   return hyp ? hyp->_GetEnforcedTriangles():DefaultIDSortedElemGroupMap();
1655 }
1656
1657 GHS3DPlugin_Hypothesis::TID2SizeMap GHS3DPlugin_Hypothesis::GetNodeIDToSizeMap(const GHS3DPlugin_Hypothesis* hyp)
1658 {
1659   return hyp ? hyp->_GetNodeIDToSizeMap(): DefaultID2SizeMap();
1660 }
1661
1662 GHS3DPlugin_Hypothesis::TSetStrings GHS3DPlugin_Hypothesis::GetGroupsToRemove(const GHS3DPlugin_Hypothesis* hyp)
1663 {
1664   return hyp ? hyp->_GetGroupsToRemove(): DefaultGroupsToRemove();
1665 }