Salome HOME
f8a7c6908652a254649e027acdc9878063d15330
[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   myWorkingDirectory(DefaultWorkingDirectory()),
50   myKeepFiles(DefaultKeepFiles()),
51   myVerboseLevel(DefaultVerboseLevel()),
52   myToCreateNewNodes(DefaultToCreateNewNodes()),
53   myToUseBoundaryRecoveryVersion(DefaultToUseBoundaryRecoveryVersion()),
54   myToUseFemCorrection(DefaultToUseFEMCorrection()),
55   myToRemoveCentralPoint(DefaultToRemoveCentralPoint()),
56   myGradation(DefaultGradation()),
57   myLogInStandardOutput(DefaultStandardOutputLog()),
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("-c 0"))
95       return true;
96     if ( myTextOption.find("-c 1"))
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(double 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 double GHS3DPlugin_Hypothesis::GetMaximumMemory() const
153 {
154   return myMaximumMemory;
155 }
156
157 //=======================================================================
158 //function : SetInitialMemory
159 //=======================================================================
160
161 void GHS3DPlugin_Hypothesis::SetInitialMemory(double MB)
162 {
163   if ( myInitialMemory != MB ) {
164     myInitialMemory = MB;
165     NotifySubMeshesHypothesisModification();
166   }
167 }
168
169 //=======================================================================
170 //function : GetInitialMemory
171 //=======================================================================
172
173 double 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 (int 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 double  GHS3DPlugin_Hypothesis::DefaultMaximumMemory()
856 {
857 #ifndef WIN32
858   struct sysinfo si;
859   int err = sysinfo( &si );
860   if ( err == 0 ) {
861     int 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   int err = GlobalMemoryStatusEx (&statex);
869   if (err != 0) {
870     int 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 double  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 (int 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 = (load >> i);
1119   if (isOK)
1120     myToMeshHoles = i;
1121   else
1122     load.clear(ios::badbit | load.rdstate());
1123
1124   isOK = (load >> d);
1125   if (isOK)
1126     myMaximumMemory = d;
1127   else
1128     load.clear(ios::badbit | load.rdstate());
1129
1130   isOK = (load >> d);
1131   if (isOK)
1132     myInitialMemory = d;
1133   else
1134     load.clear(ios::badbit | load.rdstate());
1135
1136   isOK = (load >> i);
1137   if (isOK)
1138     myOptimizationLevel = i;
1139   else
1140     load.clear(ios::badbit | load.rdstate());
1141
1142   isOK = (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 = (load >> i);
1158     if (isOK)
1159       myKeepFiles = i;
1160     else
1161       load.clear(ios::badbit | load.rdstate());
1162   }
1163
1164   isOK = (load >> i);
1165   if (isOK)
1166     myVerboseLevel = (short) i;
1167   else
1168     load.clear(ios::badbit | load.rdstate());
1169
1170   isOK = (load >> i);
1171   if (isOK)
1172     myToCreateNewNodes = (bool) i;
1173   else
1174     load.clear(ios::badbit | load.rdstate());
1175
1176   isOK = (load >> i);
1177   if (isOK)
1178     myToUseBoundaryRecoveryVersion = (bool) i;
1179   else
1180     load.clear(ios::badbit | load.rdstate());
1181
1182   isOK = (load >> i);
1183   if (isOK)
1184     myToUseFemCorrection = (bool) i;
1185   else
1186     load.clear(ios::badbit | load.rdstate());
1187
1188   isOK = (load >> i);
1189   if (isOK)
1190     myToRemoveCentralPoint = (bool) i;
1191   else
1192     load.clear(ios::badbit | load.rdstate());
1193
1194   isOK = (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 = (load >> separator);
1205
1206   if ( isOK && ( separator == "0" || separator == "1" ))
1207   {
1208     myToMakeGroupsOfDomains = ( separator == "1" );
1209     isOK = (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 = (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 = (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 = (load >> txt);  // __BEGIN_VERTEX__
1254     while (isOK) {
1255       if (txt == "__ENFORCED_VERTICES_END__")
1256         isOK = false;
1257
1258       TGHS3DEnforcedVertex *enfVertex = new TGHS3DEnforcedVertex();
1259       while (isOK) {
1260         isOK = (load >> txt);
1261         if (txt == "__END_VERTEX__") {
1262           enfVertex->name = name;
1263           enfVertex->geomEntry = entry;
1264           enfVertex->isCompound = isCompound;
1265           enfVertex->groupName = groupName;
1266           enfVertex->coords.clear();
1267           if (hasCoords)
1268             enfVertex->coords.assign(coords,coords+3);
1269
1270           _enfVertexList.insert(enfVertex);
1271
1272           if (enfVertex->coords.size())
1273             _coordsEnfVertexMap[enfVertex->coords] = enfVertex;
1274           if (!enfVertex->geomEntry.empty())
1275             _geomEntryEnfVertexMap[enfVertex->geomEntry] = enfVertex;
1276
1277           name.clear();
1278           entry.clear();
1279           groupName.clear();
1280           hasCoords = false;
1281           isOK = false;
1282         }
1283
1284         if (txt == "__BEGIN_NAME__") {  // __BEGIN_NAME__
1285           while (isOK && (txt != "__END_NAME__")) {
1286             isOK = (load >> txt);
1287             if (txt != "__END_NAME__") {
1288               if (!name.empty())
1289                 name += " ";
1290               name += txt;
1291             }
1292           }
1293           MESSAGE("name: " <<name);
1294         }
1295
1296         if (txt == "__BEGIN_ENTRY__") {  // __BEGIN_ENTRY__
1297           isOK = (load >> entry);
1298           isOK = (load >> isCompound);
1299           isOK = (load >> txt); // __END_ENTRY__
1300           if (txt != "__END_ENTRY__")
1301             throw std::exception();
1302           MESSAGE("entry: " << entry);
1303         }
1304
1305         if (txt == "__BEGIN_GROUP__") {  // __BEGIN_GROUP__
1306           while (isOK && (txt != "__END_GROUP__")) {
1307             isOK = (load >> txt);
1308             if (txt != "__END_GROUP__") {
1309               if (!groupName.empty())
1310                 groupName += " ";
1311               groupName += txt;
1312             }
1313           }
1314           MESSAGE("groupName: " << groupName);
1315         }
1316
1317         if (txt == "__BEGIN_COORDS__") {  // __BEGIN_COORDS__
1318           hasCoords = true;
1319           isOK = (load >> coords[0] >> coords[1] >> coords[2]);
1320           isOK = (load >> txt); // __END_COORDS__
1321           if (txt != "__END_COORDS__")
1322             throw std::exception();
1323           MESSAGE("coords: " << coords[0] <<","<< coords[1] <<","<< coords[2]);
1324         }
1325
1326         if (txt == "__BEGIN_SIZE__") {  // __BEGIN_ENTRY__
1327           isOK = (load >> size);
1328           isOK = (load >> txt); // __END_ENTRY__
1329           if (txt != "__END_SIZE__") {
1330             throw std::exception();
1331           }
1332           MESSAGE("size: " << size);
1333         }
1334       }
1335       isOK = (load >> txt);  // __BEGIN_VERTEX__
1336     }
1337   }
1338
1339   if (hasEnforcedVertices) {
1340     isOK = (load >> separator);
1341     if (isOK && separator == "__ENFORCED_MESHES_BEGIN__")
1342       hasEnforcedMeshes = true;
1343   }
1344
1345   if (hasEnforcedMeshes) {
1346     std::string txt, name, entry, groupName;
1347     int elementType = -1, persistID = -1;
1348     isOK = (load >> txt);  // __BEGIN_ENF_MESH__
1349     while (isOK) {
1350       //                if (isOK) {
1351       if (txt == "__ENFORCED_MESHES_END__")
1352         isOK = false;
1353
1354       TGHS3DEnforcedMesh *enfMesh = new TGHS3DEnforcedMesh();
1355       while (isOK) {
1356         isOK = (load >> txt);
1357         if (txt == "__END_ENF_MESH__") {
1358           enfMesh->name = name;
1359           enfMesh->entry = entry;
1360           enfMesh->elementType = (SMESH::ElementType)elementType;
1361           enfMesh->groupName = groupName;
1362           enfMesh->persistID = persistID;
1363
1364           _enfMeshList.insert(enfMesh);
1365           std::cout << "Restoring of enforced mesh " <<name  << " done" << std::endl;
1366
1367           name.clear();
1368           entry.clear();
1369           elementType = -1;
1370           groupName.clear();
1371           persistID = -1;
1372           isOK = false;
1373         }
1374
1375         if (txt == "__BEGIN_NAME__") {  // __BEGIN_NAME__
1376           while (isOK && (txt != "__END_NAME__")) {
1377             isOK = (load >> txt);
1378             if (txt != "__END_NAME__") {
1379               if (!name.empty())
1380                 name += " ";
1381               name += txt;
1382             }
1383           }
1384           MESSAGE("name: " <<name);
1385         }
1386
1387         if (txt == "__BEGIN_ENTRY__") {  // __BEGIN_ENTRY__
1388           isOK = (load >> entry);
1389           isOK = (load >> txt); // __END_ENTRY__
1390           if (txt != "__END_ENTRY__")
1391             throw std::exception();
1392           MESSAGE("entry: " << entry);
1393         }
1394
1395         if (txt == "__BEGIN_ELEM_TYPE__") {  // __BEGIN_ELEM_TYPE__
1396           isOK = (load >> elementType);
1397           isOK = (load >> txt); // __END_ELEM_TYPE__
1398           if (txt != "__END_ELEM_TYPE__")
1399             throw std::exception();
1400           MESSAGE("elementType: " << elementType);
1401         }
1402
1403         if (txt == "__BEGIN_GROUP__") {  // __BEGIN_GROUP__
1404           while (isOK && (txt != "__END_GROUP__")) {
1405             isOK = (load >> txt);
1406             if (txt != "__END_GROUP__") {
1407               if (!groupName.empty())
1408                 groupName += " ";
1409               groupName += txt;
1410             }
1411           } // while
1412           MESSAGE("groupName: " << groupName);
1413         } // if
1414
1415         if (txt == "__PERSIST_ID__") {
1416           isOK = (load >> persistID);
1417           MESSAGE("persistID: " << persistID);
1418         }
1419         std::cout << "isOK: " << isOK << std::endl;
1420       } // while
1421       //                } // if
1422       isOK = (load >> txt);  // __BEGIN_ENF_MESH__
1423     } // while
1424   } // if
1425
1426   return load;
1427 }
1428
1429 //=======================================================================
1430 //function : SetParametersByMesh
1431 //=======================================================================
1432
1433 bool GHS3DPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* ,const TopoDS_Shape&)
1434 {
1435   return false;
1436 }
1437
1438
1439 //================================================================================
1440 /*!
1441  * \brief Sets myToMakeGroupsOfDomains depending on whether theMesh is on shape or not
1442  */
1443 //================================================================================
1444
1445 bool GHS3DPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  dflts,
1446                                                      const SMESH_Mesh* /*theMesh*/)
1447 {
1448   myToMakeGroupsOfDomains = ( !dflts._shape || dflts._shape->IsNull() );
1449   return true;
1450 }
1451
1452 //================================================================================
1453 /*!
1454  * \brief Return command to run MG-Tetra mesher excluding file prefix (-f)
1455  */
1456 //================================================================================
1457
1458 std::string GHS3DPlugin_Hypothesis::CommandToRun(const GHS3DPlugin_Hypothesis* hyp,
1459                                                  const bool         hasShapeToMesh)
1460 {
1461   TCollection_AsciiString cmd = GetExeName().c_str();
1462   // check if any option is overridden by hyp->myTextOption
1463   bool m   = hyp ? ( hyp->myTextOption.find("-m")  == std::string::npos ) : true;
1464   bool M   = hyp ? ( hyp->myTextOption.find("-M")  == std::string::npos ) : true;
1465   bool c   = hyp ? ( hyp->myTextOption.find("-c")  == std::string::npos ) : true;
1466   bool o   = hyp ? ( hyp->myTextOption.find("-o")  == std::string::npos ) : true;
1467   bool p0  = hyp ? ( hyp->myTextOption.find("-p0") == std::string::npos ) : true;
1468   bool C   = hyp ? ( hyp->myTextOption.find("-C")  == std::string::npos ) : true;
1469   bool v   = hyp ? ( hyp->myTextOption.find("-v")  == std::string::npos ) : true;
1470   bool fem = hyp ? ( hyp->myTextOption.find("-FEM")== std::string::npos ) : true;
1471   bool rem = hyp ? ( hyp->myTextOption.find("-no_initial_central_point")== std::string::npos ) : true;
1472   bool gra = hyp ? ( hyp->myTextOption.find("-Dcpropa")== std::string::npos ) : true;
1473
1474   // if use boundary recovery version, few options are allowed
1475   bool useBndRecovery = !C;
1476   if ( !useBndRecovery && hyp )
1477     useBndRecovery = hyp->myToUseBoundaryRecoveryVersion;
1478
1479   // MG-Tetra needs to know amount of memory it may use (MB).
1480   // Default memory is defined at MG-Tetra installation but it may be not enough,
1481   // so allow to use about all available memory
1482   if ( m ) {
1483     double aMaximumMemory = hyp ? hyp->myMaximumMemory : -1;
1484     ostringstream tmpMaximumMemory;
1485     cmd += " -m ";
1486     if ( aMaximumMemory < 0 )
1487       tmpMaximumMemory << std::fixed << std::setprecision(0) << DefaultMaximumMemory();
1488     else
1489       tmpMaximumMemory << std::fixed << std::setprecision(0) << aMaximumMemory;
1490     cmd += tmpMaximumMemory.str().c_str();
1491   }
1492   if ( M && !useBndRecovery ) {
1493     double aInitialMemory = hyp ? hyp->myInitialMemory : -1;
1494     cmd += " -M ";
1495     if ( aInitialMemory > 0 )
1496       cmd += aInitialMemory;
1497     else
1498       cmd += "100";
1499   }
1500   // component to mesh
1501   // 0 , all components to be meshed
1502   // 1 , only the main ( outermost ) component to be meshed
1503   if ( c && !useBndRecovery ) {
1504     // We always run MG-Tetra with "to mesh holes'==TRUE (see PAL19680)
1505     if ( hasShapeToMesh )
1506       cmd += " -c 0";
1507     else {
1508       bool aToMeshHoles = hyp ? hyp->myToMeshHoles : DefaultMeshHoles();
1509       if ( aToMeshHoles )
1510         cmd += " -c 0";
1511       else
1512         cmd += " -c 1";
1513     }
1514   }
1515   const bool toCreateNewNodes = ( p0 && ( !hyp || hyp->myToCreateNewNodes ));
1516
1517   // optimization level
1518   if ( o && hyp && !useBndRecovery && toCreateNewNodes ) {
1519     if ( hyp->myOptimizationLevel >= 0 && hyp->myOptimizationLevel < 5 ) {
1520       const char* level[] = { "none" , "light" , "standard" , "standard+" , "strong" };
1521       cmd += " -o ";
1522       cmd += level[ hyp->myOptimizationLevel ];
1523     }
1524   }
1525   if ( !toCreateNewNodes ) {
1526     cmd += " -o none"; // issue 22608
1527   }
1528
1529   // to create internal nodes
1530   if ( p0 && !toCreateNewNodes ) {
1531     cmd += " -p0";
1532   }
1533
1534   // verbose mode
1535   if ( v && hyp ) {
1536     cmd += " -v ";
1537     cmd += hyp->myVerboseLevel;
1538   }
1539
1540   // boundary recovery version
1541   if ( useBndRecovery ) {
1542     cmd += " -C";
1543   }
1544
1545   // to use FEM correction
1546   if ( fem && hyp && hyp->myToUseFemCorrection) {
1547     cmd += " -FEM";
1548   }
1549
1550   // to remove initial central point.
1551   if ( rem && hyp && hyp->myToRemoveCentralPoint) {
1552     cmd += " -no_initial_central_point";
1553   }
1554
1555   // options as text
1556   if ( hyp && !hyp->myTextOption.empty() ) {
1557     cmd += " ";
1558     cmd += (char*) hyp->myTextOption.c_str();
1559   }
1560
1561   // to define volumic gradation.
1562   if ( gra && hyp) {
1563     cmd += " -Dcpropa=";
1564     cmd += hyp->myGradation;
1565   }
1566
1567 #ifdef WIN32
1568   cmd += " < NUL";
1569 #endif
1570
1571   return cmd.ToCString();
1572 }
1573
1574 //================================================================================
1575 /*!
1576  * \brief Return a unique file name
1577  */
1578 //================================================================================
1579
1580 std::string GHS3DPlugin_Hypothesis::GetFileName(const GHS3DPlugin_Hypothesis* hyp)
1581 {
1582   std::string aTmpDir = hyp ? hyp->GetWorkingDirectory() : DefaultWorkingDirectory();
1583   const char lastChar = *aTmpDir.rbegin();
1584 #ifdef WIN32
1585     if(lastChar != '\\') aTmpDir+='\\';
1586 #else
1587     if(lastChar != '/') aTmpDir+='/';
1588 #endif      
1589
1590   TCollection_AsciiString aGenericName = (char*)aTmpDir.c_str();
1591   aGenericName += "GHS3D_";
1592   aGenericName += getpid();
1593   aGenericName += "_";
1594   aGenericName += Abs((Standard_Integer)(long) aGenericName.ToCString());
1595
1596   return aGenericName.ToCString();
1597 }
1598
1599 //================================================================================
1600 /*
1601  * Return the name of executable
1602  */
1603 //================================================================================
1604
1605 std::string GHS3DPlugin_Hypothesis::GetExeName()
1606 {
1607   return "mg-tetra.exe";
1608 }
1609
1610 //================================================================================
1611 /*!
1612 * \brief Return the enforced vertices
1613 */
1614 //================================================================================
1615
1616 GHS3DPlugin_Hypothesis::TGHS3DEnforcedVertexList GHS3DPlugin_Hypothesis::GetEnforcedVertices(const GHS3DPlugin_Hypothesis* hyp)
1617 {
1618   return hyp ? hyp->_GetEnforcedVertices():DefaultGHS3DEnforcedVertexList();
1619 }
1620
1621 GHS3DPlugin_Hypothesis::TGHS3DEnforcedVertexCoordsValues GHS3DPlugin_Hypothesis::GetEnforcedVerticesCoordsSize (const GHS3DPlugin_Hypothesis* hyp)
1622 {  
1623   return hyp ? hyp->_GetEnforcedVerticesCoordsSize(): DefaultGHS3DEnforcedVertexCoordsValues();
1624 }
1625
1626 GHS3DPlugin_Hypothesis::TGHS3DEnforcedVertexEntryValues GHS3DPlugin_Hypothesis::GetEnforcedVerticesEntrySize (const GHS3DPlugin_Hypothesis* hyp)
1627 {  
1628   return hyp ? hyp->_GetEnforcedVerticesEntrySize(): DefaultGHS3DEnforcedVertexEntryValues();
1629 }
1630
1631 GHS3DPlugin_Hypothesis::TCoordsGHS3DEnforcedVertexMap GHS3DPlugin_Hypothesis::GetEnforcedVerticesByCoords (const GHS3DPlugin_Hypothesis* hyp)
1632 {  
1633   return hyp ? hyp->_GetEnforcedVerticesByCoords(): DefaultCoordsGHS3DEnforcedVertexMap();
1634 }
1635
1636 GHS3DPlugin_Hypothesis::TGeomEntryGHS3DEnforcedVertexMap GHS3DPlugin_Hypothesis::GetEnforcedVerticesByEntry (const GHS3DPlugin_Hypothesis* hyp)
1637 {  
1638   return hyp ? hyp->_GetEnforcedVerticesByEntry(): DefaultGeomEntryGHS3DEnforcedVertexMap();
1639 }
1640
1641 GHS3DPlugin_Hypothesis::TIDSortedNodeGroupMap GHS3DPlugin_Hypothesis::GetEnforcedNodes(const GHS3DPlugin_Hypothesis* hyp)
1642 {
1643   return hyp ? hyp->_GetEnforcedNodes():DefaultIDSortedNodeGroupMap();
1644 }
1645
1646 GHS3DPlugin_Hypothesis::TIDSortedElemGroupMap GHS3DPlugin_Hypothesis::GetEnforcedEdges(const GHS3DPlugin_Hypothesis* hyp)
1647 {
1648   return hyp ? hyp->_GetEnforcedEdges():DefaultIDSortedElemGroupMap();
1649 }
1650
1651 GHS3DPlugin_Hypothesis::TIDSortedElemGroupMap GHS3DPlugin_Hypothesis::GetEnforcedTriangles(const GHS3DPlugin_Hypothesis* hyp)
1652 {
1653   return hyp ? hyp->_GetEnforcedTriangles():DefaultIDSortedElemGroupMap();
1654 }
1655
1656 GHS3DPlugin_Hypothesis::TID2SizeMap GHS3DPlugin_Hypothesis::GetNodeIDToSizeMap(const GHS3DPlugin_Hypothesis* hyp)
1657 {
1658   return hyp ? hyp->_GetNodeIDToSizeMap(): DefaultID2SizeMap();
1659 }
1660
1661 GHS3DPlugin_Hypothesis::TSetStrings GHS3DPlugin_Hypothesis::GetGroupsToRemove(const GHS3DPlugin_Hypothesis* hyp)
1662 {
1663   return hyp ? hyp->_GetGroupsToRemove(): DefaultGroupsToRemove();
1664 }