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