Salome HOME
Windows compatibility
[plugins/blsurfplugin.git] / src / BLSURFPlugin / BLSURFPlugin_Hypothesis.cxx
1 // Copyright (C) 2007-2016  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    : BLSURFPlugin_Hypothesis.cxx
22 // Authors : Francis KLOSS (OCC) & Patrick LAUG (INRIA) & Lioka RAZAFINDRAZAKA (CEA)
23 //           & Aurelien ALLEAUME (DISTENE)
24 //           Size maps development: Nicolas GEIMER (OCC) & Gilles DAVID (EURIWARE)
25 // ---
26 //
27 #include "BLSURFPlugin_Hypothesis.hxx"
28 #include "BLSURFPlugin_Attractor.hxx"
29 #include "SMESH_Gen_i.hxx"
30 #include <utilities.h>
31 #include <cstring>
32 #include <iostream>
33 #include <sstream>
34
35 // cascade include
36 #include "ShapeAnalysis.hxx"
37
38 // CORBA includes
39 #include CORBA_CLIENT_HEADER(SALOMEDS)
40 #include CORBA_CLIENT_HEADER(GEOM_Gen)
41
42 namespace
43 {
44   struct GET_DEFAULT // struct used to get default value from GetOptionValue()
45   {
46     bool isDefault;
47     operator bool* () { return &isDefault; }
48   };
49 }
50
51 //=============================================================================
52 BLSURFPlugin_Hypothesis::BLSURFPlugin_Hypothesis(int hypId, int studyId, SMESH_Gen * gen) :
53   SMESH_Hypothesis(hypId, studyId, gen), 
54   _physicalMesh(GetDefaultPhysicalMesh()),
55   _geometricMesh(GetDefaultGeometricMesh()),
56   _phySize(GetDefaultPhySize()),
57   _phySizeRel(GetDefaultPhySizeRel()),
58   _minSize(GetDefaultMinSize()),
59   _maxSize(GetDefaultMaxSize()),
60   _minSizeRel(GetDefaultMinSizeRel()),
61   _maxSizeRel(GetDefaultMaxSizeRel()),
62   _useGradation(GetDefaultUseGradation()),
63   _gradation(GetDefaultGradation()),
64   _useVolumeGradation(GetDefaultUseVolumeGradation()),
65   _volumeGradation(GetDefaultVolumeGradation()),
66   _quadAllowed(GetDefaultQuadAllowed()),
67   _angleMesh(GetDefaultAngleMesh()),
68   _chordalError(GetDefaultChordalError()), 
69   _anisotropic(GetDefaultAnisotropic()),
70   _anisotropicRatio(GetDefaultAnisotropicRatio()),
71   _removeTinyEdges(GetDefaultRemoveTinyEdges()),
72   _tinyEdgeLength(GetDefaultTinyEdgeLength()),
73   _optimiseTinyEdges(GetDefaultOptimiseTinyEdges()),
74   _tinyEdgeOptimisationLength(GetDefaultTinyEdgeOptimisationLength()),
75   _correctSurfaceIntersec(GetDefaultCorrectSurfaceIntersection()),
76   _corrSurfaceIntersCost(GetDefaultCorrectSurfaceIntersectionMaxCost()),
77   _badElementRemoval(GetDefaultBadElementRemoval()),
78   _badElementAspectRatio(GetDefaultBadElementAspectRatio()),
79   _optimizeMesh(GetDefaultOptimizeMesh()),
80   _quadraticMesh(GetDefaultQuadraticMesh()),
81   _verb(GetDefaultVerbosity()),
82   _topology(GetDefaultTopology()),
83   _preCADMergeEdges(GetDefaultPreCADMergeEdges()),
84   _preCADRemoveTinyUVEdges(GetDefaultPreCADRemoveTinyUVEdges()),
85   _preCADRemoveDuplicateCADFaces(GetDefaultPreCADRemoveDuplicateCADFaces()),
86   _preCADProcess3DTopology(GetDefaultPreCADProcess3DTopology()),
87   _preCADDiscardInput(GetDefaultPreCADDiscardInput()),
88   _sizeMap(GetDefaultSizeMap()),
89   _attractors(GetDefaultSizeMap()),
90   _classAttractors(GetDefaultAttractorMap()),
91   _faceEntryEnfVertexListMap(GetDefaultFaceEntryEnfVertexListMap()),
92   _enfVertexList(GetDefaultEnfVertexList()),
93   _faceEntryCoordsListMap(GetDefaultFaceEntryCoordsListMap()),
94   _coordsEnfVertexMap(GetDefaultCoordsEnfVertexMap()),
95   _faceEntryEnfVertexEntryListMap(GetDefaultFaceEntryEnfVertexEntryListMap()),
96   _enfVertexEntryEnfVertexMap(GetDefaultEnfVertexEntryEnfVertexMap()),
97   _groupNameNodeIDMap(GetDefaultGroupNameNodeIDMap()),
98   _enforcedInternalVerticesAllFaces(GetDefaultInternalEnforcedVertex()),
99   _preCadFacesPeriodicityVector(GetDefaultPreCadFacesPeriodicityVector()),
100   _preCadEdgesPeriodicityVector(GetDefaultPreCadEdgesPeriodicityVector()),
101   _GMFFileName(GetDefaultGMFFile())
102 {
103   _name = GetHypType();
104   _param_algo_dim = 2;
105   
106 //   _GMFFileMode = false; // GMF ascii mode
107
108   // Advanced options with their defaults according to MG User Manual
109
110   const char* boolOptionNames[] = {         "enforce_cad_edge_sizes",                   // default = 0
111                                             // "correct_surface_intersections",            // default = 1
112                                             // "create_tag_on_collision",                  // default = 1
113                                             "jacobian_rectification_respect_geometry",  // default = 1
114                                             "rectify_jacobian",                         // default = 1
115                                             "respect_geometry",                         // default = 1
116                                             // "optimise_tiny_edges",                      // default = 0
117                                             // "remove_duplicate_cad_faces",               // default = 1
118                                             "tiny_edge_avoid_surface_intersections",    // default = 1
119                                             // "tiny_edge_respect_geometry",               // default = 0
120                                             "" // mark of end
121       };
122
123   const char* intOptionNames[] = {          "max_number_of_points_per_patch",           // default = 100000
124                                             "" // mark of end
125       };
126   const char* doubleOptionNames[] = {       // "surface_intersections_processing_max_cost",// default = 15
127                                             // "periodic_tolerance",                       // default = diag/100
128                                             // "volume_gradation",
129                                             // "tiny_edge_optimisation_length",            // default = diag * 1e-6
130                                             "" // mark of end
131       };
132   const char* charOptionNames[] = {         // "required_entities",                        // default = "respect"
133                                             // "tags",                                     // default = "respect"
134                                             "" // mark of end
135       };
136
137   // PreCAD advanced options
138   const char* preCADboolOptionNames[] = {   "closed_geometry",                          // default = 0
139                                             "discard_input_topology",                   // default = 0
140                                             "merge_edges",                              // default =  = 1
141                                             "remove_duplicate_cad_faces",               // default = 1
142                                             // "create_tag_on_collision",                  // default = 1
143                                             "debug",                                    // default = 0 
144                                             // "remove_tiny_edges",                        // default = 0
145                                             "" // mark of end
146       };
147   const char* preCADintOptionNames[] = {    // "manifold_geometry",                        // default = 0
148                                             "" // mark of end
149       };
150   const char* preCADdoubleOptionNames[] = { "periodic_tolerance",                       // default = diag * 1e-5
151                                             "sewing_tolerance",                         // default = diag * 5e-4
152                                             // "tiny_edge_length",                         // default = diag * 1e-5
153                                             "" // mark of end
154       };
155   const char* preCADcharOptionNames[] = {   "required_entities",                        // default = "respect"
156                                             "tags",                                     // default = "respect"
157                                             "" // mark of end
158       };
159   
160   int i = 0;
161   while (boolOptionNames[i][0])
162   {
163     _boolOptions.insert( boolOptionNames[i] );
164     _option2value[boolOptionNames[i++]].clear();
165   }
166   i = 0;
167   while (preCADboolOptionNames[i][0])
168   {
169     _boolOptions.insert( preCADboolOptionNames[i] );
170     _preCADoption2value[preCADboolOptionNames[i++]].clear();
171   }
172   i = 0;
173   while (intOptionNames[i][0])
174     _option2value[intOptionNames[i++]].clear();
175   
176   i = 0;
177   while (preCADintOptionNames[i][0])
178     _preCADoption2value[preCADintOptionNames[i++]].clear();
179
180   i = 0;
181   while (doubleOptionNames[i][0]) {
182     _doubleOptions.insert(doubleOptionNames[i]);
183     _option2value[doubleOptionNames[i++]].clear();
184   }
185   i = 0;
186   while (preCADdoubleOptionNames[i][0]) {
187     _preCADdoubleOptions.insert(preCADdoubleOptionNames[i]);
188     _preCADoption2value[preCADdoubleOptionNames[i++]].clear();
189   }
190   i = 0;
191   while (charOptionNames[i][0]) {
192     _charOptions.insert(charOptionNames[i]);
193     _option2value[charOptionNames[i++]].clear();
194   }
195   i = 0;
196   while (preCADcharOptionNames[i][0]) {
197     _preCADcharOptions.insert(preCADcharOptionNames[i]);
198     _preCADoption2value[preCADcharOptionNames[i++]].clear();
199   }
200
201   // default values to be used while MG meshing
202
203   _defaultOptionValues["enforce_cad_edge_sizes"                 ] = "no";
204   _defaultOptionValues["jacobian_rectification_respect_geometry"] = "yes";
205   _defaultOptionValues["max_number_of_points_per_patch"         ] = "0";
206   _defaultOptionValues["rectify_jacobian"                       ] = "yes";
207   _defaultOptionValues["respect_geometry"                       ] = "yes";
208   _defaultOptionValues["tiny_edge_avoid_surface_intersections"  ] = "yes";
209   _defaultOptionValues["closed_geometry"                        ] = "no";
210   _defaultOptionValues["debug"                                  ] = "no";
211   _defaultOptionValues["discard_input_topology"                 ] = "no";
212   _defaultOptionValues["merge_edges"                            ] = "no";
213   _defaultOptionValues["periodic_tolerance"                     ] = "1e-5*D";
214   _defaultOptionValues["remove_duplicate_cad_faces"             ] = "no";
215   _defaultOptionValues["required_entities"                      ] = "respect";
216   _defaultOptionValues["sewing_tolerance"                       ] = "5e-4*D";
217   _defaultOptionValues["tags"                                   ] = "respect";
218
219 #ifdef _DEBUG_
220   // check validity of option names of _defaultOptionValues
221   TOptionValues::iterator n2v = _defaultOptionValues.begin();
222   for ( ; n2v != _defaultOptionValues.end(); ++n2v )
223     ASSERT( _option2value.count( n2v->first ) || _preCADoption2value.count( n2v->first ));
224   ASSERT( _option2value.size() + _preCADoption2value.size() == _defaultOptionValues.size() );
225 #endif
226       
227   _sizeMap.clear();
228   _attractors.clear();
229   _faceEntryEnfVertexListMap.clear();
230   _enfVertexList.clear();
231   _faceEntryCoordsListMap.clear();
232   _coordsEnfVertexMap.clear();
233   _faceEntryEnfVertexEntryListMap.clear();
234   _enfVertexEntryEnfVertexMap.clear();
235   _groupNameNodeIDMap.clear();
236
237   /* TODO GROUPS
238    _groupNameEnfVertexListMap.clear();
239    _enfVertexGroupNameMap.clear();
240    */
241 }
242
243 TopoDS_Shape BLSURFPlugin_Hypothesis::entryToShape(std::string entry)
244 {
245   MESSAGE("BLSURFPlugin_Hypothesis::entryToShape "<<entry );
246   GEOM::GEOM_Object_var aGeomObj;
247   SMESH_Gen_i* smeshGen_i = SMESH_Gen_i::GetSMESHGen();
248   SALOMEDS::Study_ptr myStudy = smeshGen_i->GetCurrentStudy();
249   
250   TopoDS_Shape S = TopoDS_Shape();
251   SALOMEDS::SObject_var aSObj = myStudy->FindObjectID( entry.c_str() );
252   if (!aSObj->_is_nil() ) {
253     CORBA::Object_var obj = aSObj->GetObject();
254     aGeomObj = GEOM::GEOM_Object::_narrow(obj);
255     aSObj->UnRegister();
256   }
257   if ( !aGeomObj->_is_nil() )
258     S = smeshGen_i->GeomObjectToShape( aGeomObj.in() );
259   return S;
260 }
261
262 //=============================================================================
263 void BLSURFPlugin_Hypothesis::SetPhysicalMesh(PhysicalMesh thePhysicalMesh) {
264   if (thePhysicalMesh != _physicalMesh) {
265     _physicalMesh = thePhysicalMesh;
266     NotifySubMeshesHypothesisModification();
267   }
268 }
269
270 //=============================================================================
271 void BLSURFPlugin_Hypothesis::SetGeometricMesh(GeometricMesh theGeometricMesh) {
272   if (theGeometricMesh != _geometricMesh) {
273     _geometricMesh = theGeometricMesh;
274 //     switch (_geometricMesh) {
275 //       case DefaultGeom:
276 //       default:
277 //         _angleMesh = GetDefaultAngleMesh();
278 //         _gradation = GetDefaultGradation();
279 //         break;
280 //     }
281     NotifySubMeshesHypothesisModification();
282   }
283 }
284
285 //=============================================================================
286 void BLSURFPlugin_Hypothesis::SetPhySize(double theVal, bool isRelative) {
287   if ((theVal != _phySize) || (isRelative != _phySizeRel)) {
288     _phySizeRel = isRelative;
289     if (theVal == 0) {
290       _phySize = GetMaxSize();
291       MESSAGE("Warning: nul physical size is not allowed");
292     }
293     else
294       _phySize = theVal;
295     NotifySubMeshesHypothesisModification();
296   }
297 }
298
299 //=============================================================================
300 void BLSURFPlugin_Hypothesis::SetMinSize(double theMinSize, bool isRelative) {
301   if ((theMinSize != _minSize) || (isRelative != _minSizeRel)) {
302     _minSizeRel = isRelative;
303     _minSize = theMinSize;
304     NotifySubMeshesHypothesisModification();
305   }
306 }
307
308 //=============================================================================
309 void BLSURFPlugin_Hypothesis::SetMaxSize(double theMaxSize, bool isRelative) {
310   if ((theMaxSize != _maxSize) || (isRelative != _maxSizeRel)) {
311     _maxSizeRel = isRelative;
312     _maxSize = theMaxSize;
313     NotifySubMeshesHypothesisModification();
314   }
315 }
316
317 //=============================================================================
318 void BLSURFPlugin_Hypothesis::SetUseGradation(bool theVal) {
319   if (theVal != _useGradation) {
320     _useGradation = theVal;
321     NotifySubMeshesHypothesisModification();
322   }
323 }
324
325 //=============================================================================
326 void BLSURFPlugin_Hypothesis::SetGradation(double theVal) {
327   if (theVal != _gradation) {
328     _gradation = theVal;
329     NotifySubMeshesHypothesisModification();
330   }
331 }
332
333 //=============================================================================
334 void BLSURFPlugin_Hypothesis::SetUseVolumeGradation(bool theVal) {
335   if (theVal != _useVolumeGradation) {
336     _useVolumeGradation = theVal;
337     NotifySubMeshesHypothesisModification();
338   }
339 }
340
341 //=============================================================================
342 void BLSURFPlugin_Hypothesis::SetVolumeGradation(double theVal) {
343   if (theVal != _volumeGradation) {
344     _volumeGradation = theVal;
345     NotifySubMeshesHypothesisModification();
346   }
347 }
348
349 //=============================================================================
350 void BLSURFPlugin_Hypothesis::SetQuadAllowed(bool theVal) {
351   if (theVal != _quadAllowed) {
352     _quadAllowed = theVal;
353     NotifySubMeshesHypothesisModification();
354   }
355 }
356
357 //=============================================================================
358 void BLSURFPlugin_Hypothesis::SetAngleMesh(double theVal) {
359   if (theVal != _angleMesh) {
360     _angleMesh = theVal;
361     NotifySubMeshesHypothesisModification();
362   }
363 }
364
365 //=============================================================================
366 void BLSURFPlugin_Hypothesis::SetChordalError(double theDistance) {
367   if (theDistance != _chordalError) {
368     _chordalError = theDistance;
369     NotifySubMeshesHypothesisModification();
370   }
371 }
372
373 //=============================================================================
374 void BLSURFPlugin_Hypothesis::SetAnisotropic(bool theVal) {
375   if (theVal != _anisotropic) {
376     _anisotropic = theVal;
377     NotifySubMeshesHypothesisModification();
378   }
379 }
380
381 //=============================================================================
382 void BLSURFPlugin_Hypothesis::SetAnisotropicRatio(double theVal) {
383   if (theVal != _anisotropicRatio) {
384     _anisotropicRatio = theVal;
385     NotifySubMeshesHypothesisModification();
386   }
387 }
388
389 //=============================================================================
390 void BLSURFPlugin_Hypothesis::SetRemoveTinyEdges(bool theVal) {
391   if (theVal != _removeTinyEdges) {
392     _removeTinyEdges = theVal;
393     NotifySubMeshesHypothesisModification();
394   }
395 }
396
397 //=============================================================================
398 void BLSURFPlugin_Hypothesis::SetTinyEdgeLength(double theVal) {
399   if (theVal != _tinyEdgeLength) {
400     _tinyEdgeLength = theVal;
401     NotifySubMeshesHypothesisModification();
402   }
403 }
404
405 //=============================================================================
406 void BLSURFPlugin_Hypothesis::SetOptimiseTinyEdges(bool theVal) {
407   if (theVal != _optimiseTinyEdges) {
408     _optimiseTinyEdges = theVal;
409     NotifySubMeshesHypothesisModification();
410   }
411 }
412
413 //=============================================================================
414 void BLSURFPlugin_Hypothesis::SetTinyEdgeOptimisationLength(double theVal) {
415   if (theVal != _tinyEdgeOptimisationLength) {
416     _tinyEdgeOptimisationLength = theVal;
417     NotifySubMeshesHypothesisModification();
418   }
419 }
420
421 //=============================================================================
422 void BLSURFPlugin_Hypothesis::SetCorrectSurfaceIntersection(bool theVal) {
423   if (theVal != _correctSurfaceIntersec) {
424     _correctSurfaceIntersec = theVal;
425     NotifySubMeshesHypothesisModification();
426   }
427 }
428
429 //=============================================================================
430 void BLSURFPlugin_Hypothesis::SetCorrectSurfaceIntersectionMaxCost(double theVal) {
431   if (theVal != _corrSurfaceIntersCost) {
432     _corrSurfaceIntersCost = theVal;
433     NotifySubMeshesHypothesisModification();
434   }
435 }
436
437 //=============================================================================
438 void BLSURFPlugin_Hypothesis::SetBadElementRemoval(bool theVal) {
439   if (theVal != _badElementRemoval) {
440     _badElementRemoval = theVal;
441     NotifySubMeshesHypothesisModification();
442   }
443 }
444
445 //=============================================================================
446 void BLSURFPlugin_Hypothesis::SetBadElementAspectRatio(double theVal) {
447   if (theVal != _badElementAspectRatio) {
448     _badElementAspectRatio = theVal;
449     NotifySubMeshesHypothesisModification();
450   }
451 }
452
453 //=============================================================================
454 void BLSURFPlugin_Hypothesis::SetOptimizeMesh(bool theVal) {
455   if (theVal != _optimizeMesh) {
456     _optimizeMesh = theVal;
457     NotifySubMeshesHypothesisModification();
458   }
459 }
460
461 //=============================================================================
462 void BLSURFPlugin_Hypothesis::SetQuadraticMesh(bool theVal) {
463   if (theVal != _quadraticMesh) {
464     _quadraticMesh = theVal;
465     NotifySubMeshesHypothesisModification();
466   }
467 }
468
469 //=============================================================================
470 void BLSURFPlugin_Hypothesis::SetTopology(Topology theTopology) {
471   if (theTopology != _topology) {
472     _topology = theTopology;
473     NotifySubMeshesHypothesisModification();
474   }
475 }
476
477 //=============================================================================
478 void BLSURFPlugin_Hypothesis::SetVerbosity(int theVal) {
479   if (theVal != _verb) {
480     _verb = theVal;
481     NotifySubMeshesHypothesisModification();
482   }
483 }
484
485 //=============================================================================
486 void BLSURFPlugin_Hypothesis::SetEnforceCadEdgesSize( bool toEnforce )
487 {
488   if ( GetEnforceCadEdgesSize() != toEnforce )
489   {
490     SetOptionValue( "enforce_cad_edge_sizes", toEnforce ? "yes" : "no" );
491     NotifySubMeshesHypothesisModification();
492   }
493 }
494 //=============================================================================
495 bool BLSURFPlugin_Hypothesis::GetEnforceCadEdgesSize()
496 {
497   return ToBool( GetOptionValue( "enforce_cad_edge_sizes" ), GET_DEFAULT() );
498 }
499 //=============================================================================
500
501 void BLSURFPlugin_Hypothesis::SetJacobianRectificationRespectGeometry( bool allowRectification )
502 {
503   if ( GetJacobianRectificationRespectGeometry() != allowRectification )
504   {
505     SetOptionValue("jacobian_rectification_respect_geometry", allowRectification ? "yes" : "no" );
506     NotifySubMeshesHypothesisModification();
507   }
508 }
509 //=============================================================================
510 bool BLSURFPlugin_Hypothesis::GetJacobianRectificationRespectGeometry()
511 {
512   return ToBool( GetOptionValue("jacobian_rectification_respect_geometry", GET_DEFAULT()));
513 }
514 //=============================================================================
515
516 void BLSURFPlugin_Hypothesis::SetJacobianRectification( bool allowRectification )
517 {
518   if ( GetJacobianRectification() != allowRectification )
519   {
520     SetOptionValue( "rectify_jacobian", allowRectification ? "yes" : "no" );
521     NotifySubMeshesHypothesisModification();
522   }
523 }
524 //=============================================================================
525 bool BLSURFPlugin_Hypothesis::GetJacobianRectification()
526 {
527   return ToBool( GetOptionValue("rectify_jacobian", GET_DEFAULT()));
528 }
529 //=============================================================================
530
531 void BLSURFPlugin_Hypothesis::SetMaxNumberOfPointsPerPatch( int nb )
532   throw (std::invalid_argument)
533 {
534   if ( nb < 0 )
535     throw std::invalid_argument( SMESH_Comment("Invalid number of points: ") << nb );
536
537   if ( GetMaxNumberOfPointsPerPatch() != nb )
538   {
539     SetOptionValue("max_number_of_points_per_patch", SMESH_Comment( nb ));
540     NotifySubMeshesHypothesisModification();
541   }
542 }
543 //=============================================================================
544 int BLSURFPlugin_Hypothesis::GetMaxNumberOfPointsPerPatch()
545 {
546   return ToInt( GetOptionValue("max_number_of_points_per_patch", GET_DEFAULT()));
547 }
548 //=============================================================================
549
550 void BLSURFPlugin_Hypothesis::SetRespectGeometry( bool toRespect )
551 {
552   if ( GetRespectGeometry() != toRespect )
553   {
554     SetOptionValue("respect_geometry", toRespect ? "yes" : "no" );
555     NotifySubMeshesHypothesisModification();
556   }
557 }
558 //=============================================================================
559 bool BLSURFPlugin_Hypothesis::GetRespectGeometry()
560 {
561   return ToBool( GetOptionValue( "respect_geometry", GET_DEFAULT()));
562 }
563 //=============================================================================
564
565 void BLSURFPlugin_Hypothesis::SetTinyEdgesAvoidSurfaceIntersections( bool toAvoidIntersection )
566 {
567   if ( GetTinyEdgesAvoidSurfaceIntersections() != toAvoidIntersection )
568   {
569     SetOptionValue("tiny_edge_avoid_surface_intersections", toAvoidIntersection ? "yes" : "no" );
570     NotifySubMeshesHypothesisModification();
571   }
572 }
573 //=============================================================================
574 bool BLSURFPlugin_Hypothesis::GetTinyEdgesAvoidSurfaceIntersections()
575 {
576   return ToBool( GetOptionValue("tiny_edge_avoid_surface_intersections", GET_DEFAULT()));
577 }
578 //=============================================================================
579
580 void BLSURFPlugin_Hypothesis::SetClosedGeometry( bool isClosed )
581 {
582   if ( GetClosedGeometry() != isClosed )
583   {
584     SetPreCADOptionValue("closed_geometry", isClosed ? "yes" : "no" );
585     NotifySubMeshesHypothesisModification();
586   }
587 }
588 //=============================================================================
589 bool BLSURFPlugin_Hypothesis::GetClosedGeometry()
590 {
591   return ToBool( GetPreCADOptionValue( "closed_geometry", GET_DEFAULT()));
592 }
593 //=============================================================================
594
595 void BLSURFPlugin_Hypothesis::SetDebug( bool isDebug )
596 {
597   if ( GetDebug() != isDebug )
598   {
599     SetPreCADOptionValue("debug", isDebug ? "yes" : "no" );
600     NotifySubMeshesHypothesisModification();
601   }
602 }
603 //=============================================================================
604 bool BLSURFPlugin_Hypothesis::GetDebug()
605 {
606   return ToBool( GetPreCADOptionValue("debug", GET_DEFAULT()));
607 }
608 //=============================================================================
609
610 void BLSURFPlugin_Hypothesis::SetPeriodicTolerance( CORBA::Double tol )
611   throw (std::invalid_argument)
612 {
613   if ( tol <= 0 )
614     throw std::invalid_argument( SMESH_Comment("Invalid tolerance: ") << tol );
615   if ( GetPeriodicTolerance() != tol )
616   {
617     SetPreCADOptionValue("periodic_tolerance", SMESH_Comment( tol ) );
618     NotifySubMeshesHypothesisModification();
619   }
620 }
621 //=============================================================================
622 double BLSURFPlugin_Hypothesis::GetPeriodicTolerance()
623 {
624   return ToDbl( GetPreCADOptionValue( "periodic_tolerance", GET_DEFAULT()));
625 }
626 //=============================================================================
627
628 void BLSURFPlugin_Hypothesis::SetRequiredEntities( const std::string& howToTreat )
629   throw (std::invalid_argument)
630 {
631   if ( howToTreat != "respect" && howToTreat != "ignore" && howToTreat != "clear"  )
632     throw std::invalid_argument
633       ( SMESH_Comment("required_entities must be in ['respect','ignore','clear'] "));
634
635   if ( GetRequiredEntities() != howToTreat )
636   {
637     SetPreCADOptionValue("required_entities", howToTreat );
638     NotifySubMeshesHypothesisModification();
639   }
640 }
641 //=============================================================================
642 std::string BLSURFPlugin_Hypothesis::GetRequiredEntities()
643 {
644   return GetPreCADOptionValue("required_entities", GET_DEFAULT());
645 }
646 //=============================================================================
647
648 void BLSURFPlugin_Hypothesis::SetSewingTolerance( CORBA::Double tol )
649   throw (std::invalid_argument)
650 {
651   if ( tol <= 0 )
652     throw std::invalid_argument( SMESH_Comment("Invalid tolerance: ") << tol );
653   if ( GetSewingTolerance() != tol )
654   {
655     SetPreCADOptionValue("sewing_tolerance", SMESH_Comment( tol ) );
656     NotifySubMeshesHypothesisModification();
657   }
658 }
659 //=============================================================================
660 CORBA::Double BLSURFPlugin_Hypothesis::GetSewingTolerance()
661 {
662   return ToDbl( GetPreCADOptionValue("sewing_tolerance", GET_DEFAULT()));
663 }
664 //=============================================================================
665
666 void BLSURFPlugin_Hypothesis::SetTags( const std::string& howToTreat )
667   throw (std::invalid_argument)
668 {
669   if ( howToTreat != "respect" && howToTreat != "ignore" && howToTreat != "clear"  )
670     throw std::invalid_argument
671       ( SMESH_Comment("'tags' must be in ['respect','ignore','clear'] "));
672
673   if ( GetTags() != howToTreat )
674   {
675     SetPreCADOptionValue("tags", howToTreat );
676     NotifySubMeshesHypothesisModification();
677   }
678 }
679 //=============================================================================
680 std::string BLSURFPlugin_Hypothesis::GetTags()
681 {
682   return GetPreCADOptionValue("tags", GET_DEFAULT());
683 }
684 //=============================================================================
685 void BLSURFPlugin_Hypothesis::SetPreCADMergeEdges(bool theVal)
686 {
687   if (theVal != _preCADMergeEdges) {
688 //     SetTopology(PreCAD);
689     _preCADMergeEdges = theVal;
690     SetPreCADOptionValue("merge_edges", theVal ? "yes" : "no" );
691     NotifySubMeshesHypothesisModification();
692   }
693 }
694
695 //=============================================================================
696 void BLSURFPlugin_Hypothesis::SetPreCADRemoveTinyUVEdges(bool theVal)
697 {
698   if (theVal != _preCADRemoveTinyUVEdges) {
699 //     SetTopology(PreCAD);
700     _preCADRemoveTinyUVEdges = theVal;
701     NotifySubMeshesHypothesisModification();
702   }
703 }
704
705 //=============================================================================
706 void BLSURFPlugin_Hypothesis::SetPreCADRemoveDuplicateCADFaces(bool theVal)
707 {
708   if (theVal != _preCADRemoveDuplicateCADFaces) {
709     //     SetTopology(PreCAD);
710     _preCADRemoveDuplicateCADFaces = theVal;
711     SetPreCADOptionValue("remove_duplicate_cad_faces", theVal ? "yes" : "no" );
712     NotifySubMeshesHypothesisModification();
713   }
714 }
715
716 //=============================================================================
717 void BLSURFPlugin_Hypothesis::SetPreCADProcess3DTopology(bool theVal)
718 {
719   if (theVal != _preCADProcess3DTopology) {
720     //     SetTopology(PreCAD);
721     _preCADProcess3DTopology = theVal;
722     NotifySubMeshesHypothesisModification();
723   }
724 }
725
726 //=============================================================================
727 void BLSURFPlugin_Hypothesis::SetPreCADDiscardInput(bool theVal)
728 {
729   if (theVal != _preCADDiscardInput) {
730     //     SetTopology(PreCAD);
731     _preCADDiscardInput = theVal;
732     SetPreCADOptionValue("discard_input_topology", theVal ? "yes" : "no" );
733     NotifySubMeshesHypothesisModification();
734   }
735 }
736
737 //=============================================================================
738 // void BLSURFPlugin_Hypothesis::SetGMFFile(const std::string& theFileName, bool isBinary)
739 void BLSURFPlugin_Hypothesis::SetGMFFile(const std::string& theFileName)
740 {
741   _GMFFileName = theFileName;
742   //   _GMFFileMode = isBinary;
743   NotifySubMeshesHypothesisModification();
744 }
745
746 //=============================================================================
747 void BLSURFPlugin_Hypothesis::SetOptionValue(const std::string& optionName, const std::string& optionValue)
748   throw (std::invalid_argument) {
749
750   TOptionValues::iterator op_val = _option2value.find(optionName);
751   if (op_val == _option2value.end())
752   {
753     op_val = _preCADoption2value.find(optionName);
754     if (op_val == _preCADoption2value.end())
755     {
756       std::string msg = "Unknown MG-CADSurf option: '" + optionName + "'. Try SetAdvancedOption()";
757       throw std::invalid_argument(msg);
758     }
759   }
760   if (op_val->second != optionValue)
761   {
762     const char* ptr = optionValue.c_str();
763     // strip white spaces
764     while (ptr[0] == ' ')
765       ptr++;
766     int i = strlen(ptr);
767     while (i != 0 && ptr[i - 1] == ' ')
768       i--;
769     // check value type
770     bool typeOk = true;
771     std::string typeName;
772     if (i == 0) {
773       // empty string
774     } else if (_charOptions.count(optionName)) {
775       // do not check strings
776     } else if (_doubleOptions.count(optionName)) {
777       // check if value is double
778       ToDbl(ptr, &typeOk);
779       typeName = "real";
780     } else if (_boolOptions.count(optionName)) {
781       // check if value is bool
782       ToBool(ptr, &typeOk);
783       typeName = "bool";
784     } else {
785       // check if value is int
786       ToInt(ptr, &typeOk);
787       typeName = "integer";
788     }
789     if (!typeOk) {
790       std::string msg = "Advanced option '" + optionName + "' = '" + optionValue + "' but must be " + typeName;
791       throw std::invalid_argument(msg);
792     }
793     std::string value( ptr, i );
794     if ( _defaultOptionValues[ optionName ] == value )
795       value.clear();
796
797     op_val->second = value;
798
799     NotifySubMeshesHypothesisModification();
800   }
801 }
802
803 //=============================================================================
804 void BLSURFPlugin_Hypothesis::SetPreCADOptionValue(const std::string& optionName, const std::string& optionValue)
805   throw (std::invalid_argument) {
806
807   TOptionValues::iterator op_val = _preCADoption2value.find(optionName);
808   if (op_val == _preCADoption2value.end()) {
809     op_val = _option2value.find(optionName);
810     if (op_val == _option2value.end()) {
811       std::string msg = "Unknown MG-PreCAD option: '" + optionName + "'. Try SetAdvancedOption()";
812       throw std::invalid_argument(msg);
813     }
814   }
815   if (op_val->second != optionValue)
816   {
817     const char* ptr = optionValue.c_str();
818     // strip white spaces
819     while (ptr[0] == ' ')
820       ptr++;
821     int i = strlen(ptr);
822     while (i != 0 && ptr[i - 1] == ' ')
823       i--;
824     // check value type
825     bool typeOk = true;
826     std::string typeName;
827     if (i == 0) {
828       // empty string
829     } else if (_preCADcharOptions.find(optionName) != _preCADcharOptions.end()) {
830       // do not check strings
831     } else if (_preCADdoubleOptions.find(optionName) != _preCADdoubleOptions.end()) {
832       // check if value is double
833       char * endPtr;
834       strtod(ptr, &endPtr);
835       typeOk = (ptr != endPtr);
836       typeName = "real";
837     } else if (_boolOptions.count(optionName)) {
838       // check if value is bool
839       ToBool(ptr, &typeOk);
840       typeName = "bool";
841     } else {
842       // check if value is int
843       char * endPtr;
844       strtol(ptr, &endPtr, 10);
845       typeOk = (ptr != endPtr);
846       typeName = "integer";
847     }
848     if (!typeOk) {
849       std::string msg = "PreCAD advanced option '" + optionName + "' = '" + optionValue + "' but must be " + typeName;
850       throw std::invalid_argument(msg);
851     }
852     std::string value( ptr, i );
853     if ( _defaultOptionValues[ optionName ] == value )
854       value.clear();
855
856     op_val->second = value;
857
858     NotifySubMeshesHypothesisModification();
859   }
860 }
861
862 //=============================================================================
863 std::string BLSURFPlugin_Hypothesis::GetOptionValue(const std::string& optionName,
864                                                     bool*              isDefault) const
865   throw (std::invalid_argument)
866 {
867   TOptionValues::const_iterator op_val = _option2value.find(optionName);
868   if (op_val == _option2value.end())
869   {
870     op_val = _preCADoption2value.find(optionName);
871     if (op_val == _preCADoption2value.end())
872     {
873       op_val = _customOption2value.find(optionName);
874       if (op_val == _customOption2value.end())
875       {
876         std::string msg = "Unknown MG-CADSurf option: <" + optionName + ">";
877         throw std::invalid_argument(msg);
878       }
879     }
880   }
881   std::string val = op_val->second;
882   if ( isDefault ) *isDefault = ( val.empty() );
883
884   if ( val.empty() && isDefault )
885   {
886     op_val = _defaultOptionValues.find( optionName );
887     if (op_val != _defaultOptionValues.end())
888       val = op_val->second;
889   }
890   return val;
891 }
892
893 //=============================================================================
894 std::string BLSURFPlugin_Hypothesis::GetPreCADOptionValue(const std::string& optionName,
895                                                           bool*              isDefault) const
896   throw (std::invalid_argument)
897 {
898   TOptionValues::const_iterator op_val = _preCADoption2value.find(optionName);
899   if (op_val == _preCADoption2value.end())
900   {
901     op_val = _option2value.find(optionName);
902     if (op_val == _option2value.end())
903     {
904       op_val = _customOption2value.find(optionName);
905       if (op_val == _customOption2value.end())
906       {
907         std::string msg = "Unknown MG-CADSurf option: <" + optionName + ">";
908         throw std::invalid_argument(msg);
909       }
910     }
911   }
912   std::string val = op_val->second;
913   if ( isDefault ) *isDefault = ( val.empty() );
914
915   if ( val.empty() && isDefault )
916   {
917     op_val = _defaultOptionValues.find( optionName );
918     if (op_val != _option2value.end())
919       val = op_val->second;
920   }
921   return val;
922 }
923
924 //=============================================================================
925 void BLSURFPlugin_Hypothesis::ClearOption(const std::string& optionName)
926 {
927   TOptionValues::iterator op_val = _customOption2value.find(optionName);
928   if (op_val != _customOption2value.end())
929    _customOption2value.erase(op_val);
930   else {
931     op_val = _option2value.find(optionName);
932     if (op_val != _option2value.end())
933       op_val->second.clear();
934     else {
935       op_val = _preCADoption2value.find(optionName);
936       if (op_val != _preCADoption2value.end())
937         op_val->second.clear();
938     }
939   }
940 }
941
942 //=============================================================================
943 void BLSURFPlugin_Hypothesis::ClearPreCADOption(const std::string& optionName)
944 {
945   TOptionValues::iterator op_val = _preCADoption2value.find(optionName);
946   if (op_val != _preCADoption2value.end())
947     op_val->second.clear();
948 }
949
950 //=============================================================================
951 void BLSURFPlugin_Hypothesis::AddOption(const std::string& optionName, const std::string& optionValue)
952 {
953   bool modif = true;
954   TOptionValues::iterator op_val = _option2value.find(optionName);
955   if (op_val != _option2value.end())
956   {
957     if (op_val->second != optionValue)
958       op_val->second = optionValue;
959     else
960       modif = false;
961   }
962   else
963   {
964     op_val = _preCADoption2value.find(optionName);
965     if (op_val != _preCADoption2value.end())
966     {
967       if (op_val->second != optionValue)
968         op_val->second = optionValue;
969       else
970         modif = false;
971     }
972     else if ( optionValue.empty() )
973     {
974       _customOption2value.erase( optionName );
975     }
976     else
977     {
978       op_val = _customOption2value.find(optionName);
979       if (op_val == _customOption2value.end())
980         _customOption2value[optionName] = optionValue;
981       else if (op_val->second != optionValue)
982         op_val->second = optionValue;
983       else
984         modif = false;
985     }
986   }
987   if ( modif )
988     NotifySubMeshesHypothesisModification();
989 }
990
991 //=============================================================================
992 void BLSURFPlugin_Hypothesis::AddPreCADOption(const std::string& optionName, const std::string& optionValue)
993 {
994   AddOption( optionName, optionValue );
995 }
996
997 //=============================================================================
998 std::string BLSURFPlugin_Hypothesis::GetOption(const std::string& optionName)
999 {
1000   TOptionValues::iterator op_val = _customOption2value.find(optionName);
1001   if (op_val != _customOption2value.end())
1002     return op_val->second;
1003   else
1004     return "";
1005 }
1006
1007 //=============================================================================
1008 std::string BLSURFPlugin_Hypothesis::GetPreCADOption(const std::string& optionName)
1009 {
1010   TOptionValues::iterator op_val = _customOption2value.find(optionName);
1011   if (op_val != _customOption2value.end())
1012     return op_val->second;
1013   else
1014     return "";
1015 }
1016
1017 //=============================================================================
1018 BLSURFPlugin_Hypothesis::TOptionValues BLSURFPlugin_Hypothesis::GetOptionValues() const
1019 {
1020   TOptionValues vals;
1021   TOptionValues::const_iterator op_val = _option2value.begin();
1022   for ( ; op_val != _option2value.end(); ++op_val )
1023     vals.insert( make_pair( op_val->first, GetOptionValue( op_val->first, GET_DEFAULT() )));
1024
1025   return vals;
1026 }
1027
1028 //=============================================================================
1029 BLSURFPlugin_Hypothesis::TOptionValues BLSURFPlugin_Hypothesis::GetPreCADOptionValues() const
1030 {
1031   TOptionValues vals;
1032   TOptionValues::const_iterator op_val = _preCADoption2value.begin();
1033   for ( ; op_val != _preCADoption2value.end(); ++op_val )
1034     vals.insert( make_pair( op_val->first, GetPreCADOptionValue( op_val->first, GET_DEFAULT() )));
1035
1036   return vals;
1037 }
1038
1039 //=======================================================================
1040 //function : SetSizeMapEntry
1041 //=======================================================================
1042 void BLSURFPlugin_Hypothesis::SetSizeMapEntry(const std::string& entry, const std::string& sizeMap) {
1043   if (_sizeMap[entry].compare(sizeMap) != 0) {
1044     SetPhysicalMesh(PhysicalLocalSize);
1045     _sizeMap[entry] = sizeMap;
1046     NotifySubMeshesHypothesisModification();
1047   }
1048 }
1049
1050 //=======================================================================
1051 //function : GetSizeMapEntry
1052 //=======================================================================
1053 std::string BLSURFPlugin_Hypothesis::GetSizeMapEntry(const std::string& entry) {
1054   TSizeMap::iterator it = _sizeMap.find(entry);
1055   if (it != _sizeMap.end())
1056     return it->second;
1057   else
1058     return "No_Such_Entry";
1059 }
1060
1061 /*!
1062  * \brief Return the size maps
1063  */
1064 BLSURFPlugin_Hypothesis::TSizeMap BLSURFPlugin_Hypothesis::GetSizeMapEntries(const BLSURFPlugin_Hypothesis* hyp) {
1065   return hyp ? hyp->_GetSizeMapEntries() : GetDefaultSizeMap();
1066 }
1067
1068 //=======================================================================
1069 //function : SetAttractorEntry
1070 //=======================================================================
1071 void BLSURFPlugin_Hypothesis::SetAttractorEntry(const std::string& entry, const std::string& attractor) {
1072   if (_attractors[entry].compare(attractor) != 0) {
1073     SetPhysicalMesh(PhysicalLocalSize);
1074     _attractors[entry] = attractor;
1075     NotifySubMeshesHypothesisModification();
1076   }
1077 }
1078
1079 //=======================================================================
1080 //function : GetAttractorEntry
1081 //=======================================================================
1082 std::string BLSURFPlugin_Hypothesis::GetAttractorEntry(const std::string& entry) {
1083   TSizeMap::iterator it = _attractors.find(entry);
1084   if (it != _attractors.end())
1085     return it->second;
1086   else
1087     return "No_Such_Entry";
1088 }
1089
1090 /*!
1091  * \brief Return the attractors
1092  */
1093 BLSURFPlugin_Hypothesis::TSizeMap BLSURFPlugin_Hypothesis::GetAttractorEntries(const BLSURFPlugin_Hypothesis* hyp) {
1094   return hyp ? hyp->_GetAttractorEntries() : GetDefaultSizeMap();
1095 }
1096
1097 //=======================================================================
1098 //function : SetClassAttractorEntry
1099 //=======================================================================
1100 void BLSURFPlugin_Hypothesis::SetClassAttractorEntry(const std::string& entry, const std::string& attEntry, double StartSize, double EndSize, double ActionRadius, double ConstantRadius)
1101 {
1102   SetPhysicalMesh(PhysicalLocalSize);
1103
1104   // The new attractor can't be defined on the same face as another sizemap
1105   TSizeMap::iterator it  = _sizeMap.find( entry );
1106   if ( it != _sizeMap.end() ) {
1107     _sizeMap.erase(it);
1108     NotifySubMeshesHypothesisModification();
1109   }
1110   else {
1111     TSizeMap::iterator itAt  = _attractors.find( entry );
1112     if ( itAt != _attractors.end() ) {
1113       _attractors.erase(itAt);
1114       NotifySubMeshesHypothesisModification();
1115     }
1116   }
1117   
1118   const TopoDS_Shape AttractorShape = BLSURFPlugin_Hypothesis::entryToShape(attEntry);
1119   const TopoDS_Face FaceShape = TopoDS::Face(BLSURFPlugin_Hypothesis::entryToShape(entry));
1120   TAttractorMap::iterator attIt = _classAttractors.find(entry);
1121   for ( ; attIt != _classAttractors.end(); ++attIt )
1122     if ( attIt->first == entry && 
1123          attIt->second->GetAttractorEntry() == attEntry )
1124       break;
1125   bool attExists = (attIt != _classAttractors.end());
1126
1127   BLSURFPlugin_Attractor* myAttractor;
1128   if ( !attExists ) {
1129     myAttractor = new BLSURFPlugin_Attractor(FaceShape, AttractorShape, attEntry);//, 0.1 );
1130     _classAttractors.insert( make_pair( entry, myAttractor ));
1131   }
1132   else {
1133     myAttractor = attIt->second;
1134   }
1135   // if (!myAttractor->IsMapBuilt())
1136   //   myAttractor->BuildMap();
1137   myAttractor->SetParameters(StartSize, EndSize, ActionRadius, ConstantRadius);
1138
1139   NotifySubMeshesHypothesisModification();
1140 }
1141
1142 //=======================================================================
1143 //function : SetConstantSizeOnAdjacentFaces
1144 //=======================================================================
1145 // TODO uncomment and test (include the needed .hxx)
1146 // SetConstantSizeOnAdjacentFaces(myShape, att_entry, startSize, endSize = user_size, const_dist  ) {
1147 //   TopTools_IndexedMapOfShapListOdShape anEdge2FaceMap;
1148 //   TopExp::MapShapesAnAncestors(myShape,TopAbs_EDGE, TopAbs_FACE, anEdge2FaceMap);
1149 //   TopTools_IndexedMapOfShapListOdShape::iterator it;
1150 //   for (it = anEdge2FaceMap.begin();it != anEdge2FaceMap.end();it++){
1151 //       SetClassAttractorEntry((*it).first, att_entry, startSize, endSize, 0, const_dist)
1152 //   }
1153
1154
1155
1156
1157
1158
1159 //=======================================================================
1160 //function : GetClassAttractorEntry
1161 //=======================================================================
1162 // BLSURFPlugin_Attractor&  BLSURFPlugin_Hypothesis::GetClassAttractorEntry(const std::string& entry)
1163 // {
1164 //  TAttractorMap::iterator it  = _classAttractors.find( entry );
1165 //  if ( it != _classAttractors.end() )
1166 //    return it->second;
1167 //  else
1168 //    return "No_Such_Entry";
1169 // }
1170 // 
1171   /*!
1172    * \brief Return the map of attractor instances
1173    */
1174 BLSURFPlugin_Hypothesis::TAttractorMap BLSURFPlugin_Hypothesis::GetClassAttractorEntries(const BLSURFPlugin_Hypothesis* hyp)
1175 {
1176     return hyp ? hyp->_GetClassAttractorEntries():GetDefaultAttractorMap();
1177 }
1178
1179 //=======================================================================
1180 //function : ClearEntry
1181 //=======================================================================
1182 void BLSURFPlugin_Hypothesis::ClearEntry(const std::string& entry,
1183                                          const char * attEntry/*=0*/)
1184 {
1185  TSizeMap::iterator it  = _sizeMap.find( entry );
1186  
1187  if ( it != _sizeMap.end() ) {
1188    _sizeMap.erase(it);
1189    NotifySubMeshesHypothesisModification();
1190  }
1191  else {
1192    TSizeMap::iterator itAt  = _attractors.find( entry );
1193    if ( itAt != _attractors.end() ) {
1194      _attractors.erase(itAt);
1195      NotifySubMeshesHypothesisModification();
1196    }
1197    else {
1198      TAttractorMap::iterator it_clAt = _classAttractors.find( entry );
1199      if ( it_clAt != _classAttractors.end() ) {
1200        do {
1201          if ( !attEntry || it_clAt->second->GetAttractorEntry() == attEntry )
1202            _classAttractors.erase( it_clAt++ );
1203          else
1204            ++it_clAt;
1205        }
1206        while ( it_clAt != _classAttractors.end() );
1207        MESSAGE("_classAttractors.size() = "<<_classAttractors.size())
1208        NotifySubMeshesHypothesisModification();
1209      }
1210      else
1211        std::cout<<"No_Such_Entry"<<std::endl;
1212    }
1213  }
1214 }
1215
1216 //=======================================================================
1217 //function : ClearSizeMaps
1218 //=======================================================================
1219 void BLSURFPlugin_Hypothesis::ClearSizeMaps() {
1220   _sizeMap.clear();
1221   _attractors.clear();
1222   _classAttractors.clear();
1223 }
1224
1225 // Enable internal enforced vertices on specific face if requested by user
1226
1227 ////=======================================================================
1228 ////function : SetInternalEnforcedVertex
1229 ////=======================================================================
1230 //void BLSURFPlugin_Hypothesis::SetInternalEnforcedVertex(TEntry theFaceEntry,
1231 //                                                        bool toEnforceInternalVertices,
1232 //                                                        TEnfGroupName theGroupName) {
1233
1234 //  MESSAGE("BLSURFPlugin_Hypothesis::SetInternalEnforcedVertex("<< theFaceEntry << ", "
1235 //      << toEnforceInternalVertices << ", " << theGroupName << ")");
1236   
1237 //  TFaceEntryInternalVerticesList::iterator it = _faceEntryInternalVerticesList.find(theFaceEntry);
1238 //  if (it != _faceEntryInternalVerticesList.end()) {
1239 //    if (!toEnforceInternalVertices) {
1240 //      _faceEntryInternalVerticesList.erase(it);
1241 //    }
1242 //  }
1243 //  else {
1244 //    if (toEnforceInternalVertices) {
1245 //      _faceEntryInternalVerticesList.insert(theFaceEntry);
1246 //    }
1247 //  }
1248   
1249 //  // TODO
1250 //  // Take care of groups
1251 //}
1252
1253
1254 //=======================================================================
1255 //function : SetEnforcedVertex
1256 //=======================================================================
1257 bool BLSURFPlugin_Hypothesis::SetEnforcedVertex(TEntry theFaceEntry, TEnfName theVertexName, TEntry theVertexEntry,
1258                                                 TEnfGroupName theGroupName, double x, double y, double z) {
1259
1260   MESSAGE("BLSURFPlugin_Hypothesis::SetEnforcedVertex("<< theFaceEntry << ", "
1261       << x << ", " << y << ", " << z << ", " << theVertexName << ", " << theVertexEntry << ", " << theGroupName << ")");
1262
1263   SetPhysicalMesh(PhysicalLocalSize);
1264
1265   //  TEnfVertexList::iterator it;
1266   bool toNotify = false;
1267   bool toCreate = true;
1268
1269   TEnfVertex *oldEnVertex;
1270   TEnfVertex *newEnfVertex = new TEnfVertex();
1271   newEnfVertex->name = theVertexName;
1272   newEnfVertex->geomEntry = theVertexEntry;
1273   newEnfVertex->coords.clear();
1274   if (theVertexEntry == "") {
1275     newEnfVertex->coords.push_back(x);
1276     newEnfVertex->coords.push_back(y);
1277     newEnfVertex->coords.push_back(z);
1278   }
1279   newEnfVertex->grpName = theGroupName;
1280   newEnfVertex->faceEntries.clear();
1281   newEnfVertex->faceEntries.insert(theFaceEntry);
1282   
1283   
1284   // update _enfVertexList
1285   TEnfVertexList::iterator it = _enfVertexList.find(newEnfVertex);
1286   if (it != _enfVertexList.end()) {
1287     toCreate = false;
1288     oldEnVertex = (*it);
1289     MESSAGE("Enforced Vertex was found => Update");
1290     if (oldEnVertex->name != theVertexName) {
1291       MESSAGE("Update name from \"" << oldEnVertex->name << "\" to \"" << theVertexName << "\"");
1292       oldEnVertex->name = theVertexName;
1293       toNotify = true;
1294     }
1295     if (oldEnVertex->grpName != theGroupName) {
1296       MESSAGE("Update group name from \"" << oldEnVertex->grpName << "\" to \"" << theGroupName << "\"");
1297       oldEnVertex->grpName = theGroupName;
1298       toNotify = true;
1299     }
1300     TEntryList::iterator it_faceEntries = oldEnVertex->faceEntries.find(theFaceEntry);
1301     if (it_faceEntries == oldEnVertex->faceEntries.end()) {
1302       MESSAGE("Update face list by adding \"" << theFaceEntry << "\"");
1303       oldEnVertex->faceEntries.insert(theFaceEntry);
1304       _faceEntryEnfVertexListMap[theFaceEntry].insert(oldEnVertex);
1305       toNotify = true;
1306     }
1307     if (toNotify) {
1308       // update map coords / enf vertex if needed
1309       if (oldEnVertex->coords.size()) {
1310         _coordsEnfVertexMap[oldEnVertex->coords] = oldEnVertex;
1311         _faceEntryCoordsListMap[theFaceEntry].insert(oldEnVertex->coords);
1312       }
1313
1314       // update map geom entry / enf vertex if needed
1315       if (oldEnVertex->geomEntry != "") {
1316         _enfVertexEntryEnfVertexMap[oldEnVertex->geomEntry] = oldEnVertex;
1317         _faceEntryEnfVertexEntryListMap[theFaceEntry].insert(oldEnVertex->geomEntry);
1318       }
1319     }
1320   }
1321
1322 //   //////// CREATE ////////////
1323   if (toCreate) {
1324     toNotify = true;
1325     MESSAGE("Creating new enforced vertex");
1326     _faceEntryEnfVertexListMap[theFaceEntry].insert(newEnfVertex);
1327     _enfVertexList.insert(newEnfVertex);
1328     if (theVertexEntry == "") {
1329       _faceEntryCoordsListMap[theFaceEntry].insert(newEnfVertex->coords);
1330       _coordsEnfVertexMap[newEnfVertex->coords] = newEnfVertex;
1331     }
1332     else {
1333       _faceEntryEnfVertexEntryListMap[theFaceEntry].insert(newEnfVertex->geomEntry);
1334       _enfVertexEntryEnfVertexMap[newEnfVertex->geomEntry] = newEnfVertex;
1335     }
1336   }
1337
1338   if (toNotify)
1339     NotifySubMeshesHypothesisModification();
1340
1341   MESSAGE("BLSURFPlugin_Hypothesis::SetEnforcedVertex END");
1342   return toNotify;
1343 }
1344
1345
1346 //=======================================================================
1347 //function : GetEnforcedVertices
1348 //=======================================================================
1349
1350 BLSURFPlugin_Hypothesis::TEnfVertexList BLSURFPlugin_Hypothesis::GetEnfVertexList(const TEntry& theFaceEntry)
1351     throw (std::invalid_argument) {
1352
1353   if (_faceEntryEnfVertexListMap.count(theFaceEntry) > 0)
1354     return _faceEntryEnfVertexListMap[theFaceEntry];
1355   else
1356     return GetDefaultEnfVertexList();
1357
1358   std::ostringstream msg;
1359   msg << "No enforced vertex for face entry " << theFaceEntry;
1360   throw std::invalid_argument(msg.str());
1361 }
1362
1363 //=======================================================================
1364 //function : GetEnfVertexCoordsList
1365 //=======================================================================
1366
1367 BLSURFPlugin_Hypothesis::TEnfVertexCoordsList BLSURFPlugin_Hypothesis::GetEnfVertexCoordsList(
1368     const TEntry& theFaceEntry) throw (std::invalid_argument) {
1369
1370   if (_faceEntryCoordsListMap.count(theFaceEntry) > 0)
1371     return _faceEntryCoordsListMap[theFaceEntry];
1372
1373   std::ostringstream msg;
1374   msg << "No enforced vertex coords for face entry " << theFaceEntry;
1375   throw std::invalid_argument(msg.str());
1376 }
1377
1378 //=======================================================================
1379 //function : GetEnfVertexEntryList
1380 //=======================================================================
1381
1382 BLSURFPlugin_Hypothesis::TEntryList BLSURFPlugin_Hypothesis::GetEnfVertexEntryList(const TEntry& theFaceEntry)
1383     throw (std::invalid_argument) {
1384
1385   if (_faceEntryEnfVertexEntryListMap.count(theFaceEntry) > 0)
1386     return _faceEntryEnfVertexEntryListMap[theFaceEntry];
1387
1388   std::ostringstream msg;
1389   msg << "No enforced vertex entry for face entry " << theFaceEntry;
1390   throw std::invalid_argument(msg.str());
1391 }
1392
1393 //=======================================================================
1394 //function : GetEnfVertex(TEnfVertexCoords coords)
1395 //=======================================================================
1396
1397 BLSURFPlugin_Hypothesis::TEnfVertex* BLSURFPlugin_Hypothesis::GetEnfVertex(TEnfVertexCoords coords)
1398     throw (std::invalid_argument) {
1399
1400   if (_coordsEnfVertexMap.count(coords) > 0)
1401     return _coordsEnfVertexMap[coords];
1402
1403   std::ostringstream msg;
1404   msg << "No enforced vertex with coords (" << coords[0] << ", " << coords[1] << ", " << coords[2] << ")";
1405   throw std::invalid_argument(msg.str());
1406 }
1407
1408 //=======================================================================
1409 //function : GetEnfVertex(const TEntry& theEnfVertexEntry)
1410 //=======================================================================
1411
1412 BLSURFPlugin_Hypothesis::TEnfVertex* BLSURFPlugin_Hypothesis::GetEnfVertex(const TEntry& theEnfVertexEntry)
1413     throw (std::invalid_argument) {
1414
1415   if (_enfVertexEntryEnfVertexMap.count(theEnfVertexEntry) > 0)
1416     return _enfVertexEntryEnfVertexMap[theEnfVertexEntry];
1417
1418   std::ostringstream msg;
1419   msg << "No enforced vertex with entry " << theEnfVertexEntry;
1420   throw std::invalid_argument(msg.str());
1421 }
1422
1423 //Enable internal enforced vertices on specific face if requested by user
1424 ////=======================================================================
1425 ////function : GetInternalEnforcedVertex
1426 ////=======================================================================
1427
1428 //bool BLSURFPlugin_Hypothesis::GetInternalEnforcedVertex(const TEntry& theFaceEntry)
1429 //{
1430 //  if (_faceEntryInternalVerticesList.count(theFaceEntry) > 0)
1431 //    return true;
1432 //  return false;
1433 //}
1434
1435 //=======================================================================
1436 //function : ClearEnforcedVertex
1437 //=======================================================================
1438
1439 bool BLSURFPlugin_Hypothesis::ClearEnforcedVertex(const TEntry& theFaceEntry, double x, double y, double z,
1440     const TEntry& theVertexEntry) throw (std::invalid_argument) {
1441
1442   bool toNotify = false;
1443   std::ostringstream msg;
1444   TEnfVertex *oldEnfVertex;
1445   TEnfVertexCoords coords;
1446   coords.clear();
1447   coords.push_back(x);
1448   coords.push_back(y);
1449   coords.push_back(z);
1450
1451   // check that enf vertex with given enf vertex entry exists
1452   TEnfVertexEntryEnfVertexMap::iterator it_enfVertexEntry = _enfVertexEntryEnfVertexMap.find(theVertexEntry);
1453   if (it_enfVertexEntry != _enfVertexEntryEnfVertexMap.end()) {
1454     // Success
1455     MESSAGE("Found enforced vertex with geom entry " << theVertexEntry);
1456     oldEnfVertex = it_enfVertexEntry->second;
1457
1458     _enfVertexEntryEnfVertexMap.erase(it_enfVertexEntry);
1459
1460     TEntryList& enfVertexEntryList = _faceEntryEnfVertexEntryListMap[theFaceEntry];
1461     enfVertexEntryList.erase(theVertexEntry);
1462     if (enfVertexEntryList.size() == 0)
1463       _faceEntryEnfVertexEntryListMap.erase(theFaceEntry);
1464     //    TFaceEntryEnfVertexEntryListMap::iterator it_entry_entry = _faceEntryEnfVertexEntryListMap.find(theFaceEntry);
1465     //    TEntryList::iterator it_entryList = it_entry_entry->second.find(theVertexEntry);
1466     //    it_entry_entry->second.erase(it_entryList);
1467     //    if (it_entry_entry->second.size() == 0)
1468     //      _faceEntryEnfVertexEntryListMap.erase(it_entry_entry);
1469   } else {
1470     // Fail
1471     MESSAGE("Enforced vertex with geom entry " << theVertexEntry << " not found");
1472     msg << "No enforced vertex with geom entry " << theVertexEntry;
1473     // check that enf vertex with given coords exists
1474     TCoordsEnfVertexMap::iterator it_coords_enf = _coordsEnfVertexMap.find(coords);
1475     if (it_coords_enf != _coordsEnfVertexMap.end()) {
1476       // Success
1477       MESSAGE("Found enforced vertex with coords " << x << ", " << y << ", " << z);
1478       oldEnfVertex = it_coords_enf->second;
1479
1480       _coordsEnfVertexMap.erase(it_coords_enf);
1481
1482       TEnfVertexCoordsList& enfVertexCoordsList = _faceEntryCoordsListMap[theFaceEntry];
1483       enfVertexCoordsList.erase(coords);
1484       if (enfVertexCoordsList.size() == 0)
1485         _faceEntryCoordsListMap.erase(theFaceEntry);
1486       //      TFaceEntryCoordsListMap::iterator it_entry_coords = _faceEntryCoordsListMap.find(theFaceEntry);
1487       //      TEnfVertexCoordsList::iterator it_coordsList = it_entry_coords->second.find(coords);
1488       //      it_entry_coords->second.erase(it_coordsList);
1489       //      if (it_entry_coords->second.size() == 0)
1490       //        _faceEntryCoordsListMap.erase(it_entry_coords);
1491     } else {
1492       // Fail
1493       MESSAGE("Enforced vertex with coords " << x << ", " << y << ", " << z << " not found");
1494       msg << std::endl;
1495       msg << "No enforced vertex at " << x << ", " << y << ", " << z;
1496       throw std::invalid_argument(msg.str());
1497     }
1498   }
1499
1500   MESSAGE("Remove enf vertex from _enfVertexList");
1501
1502   // update _enfVertexList
1503   TEnfVertexList::iterator it = _enfVertexList.find(oldEnfVertex);
1504   if (it != _enfVertexList.end()) {
1505     (*it)->faceEntries.erase(theFaceEntry);
1506     if ((*it)->faceEntries.size() == 0){
1507       _enfVertexList.erase(it);
1508       toNotify = true;
1509     }
1510     MESSAGE("Done");
1511   }
1512
1513   // update _faceEntryEnfVertexListMap
1514   TEnfVertexList& currentEnfVertexList = _faceEntryEnfVertexListMap[theFaceEntry];
1515   currentEnfVertexList.erase(oldEnfVertex);
1516
1517   if (currentEnfVertexList.size() == 0) {
1518     MESSAGE("Remove _faceEntryEnfVertexListMap[" << theFaceEntry <<"]");
1519     _faceEntryEnfVertexListMap.erase(theFaceEntry);
1520     MESSAGE("Done");
1521   }
1522
1523   if (toNotify)
1524     NotifySubMeshesHypothesisModification();
1525
1526   return toNotify;
1527 }
1528
1529 //=======================================================================
1530 //function : ClearEnforcedVertices
1531 //=======================================================================
1532
1533 bool BLSURFPlugin_Hypothesis::ClearEnforcedVertices(const TEntry& theFaceEntry) throw (std::invalid_argument) {
1534
1535   bool toNotify = false;
1536   TEnfVertex *oldEnfVertex;
1537
1538   TFaceEntryCoordsListMap::iterator it_entry_coords = _faceEntryCoordsListMap.find(theFaceEntry);
1539   if (it_entry_coords != _faceEntryCoordsListMap.end()) {
1540     toNotify = true;
1541     TEnfVertexCoordsList coordsList = it_entry_coords->second;
1542     TEnfVertexCoordsList::iterator it_coordsList = coordsList.begin();
1543     for (; it_coordsList != coordsList.end(); ++it_coordsList) {
1544       TEnfVertexCoords coords = (*it_coordsList);
1545       oldEnfVertex = _coordsEnfVertexMap[coords];
1546       _coordsEnfVertexMap.erase(coords);
1547       // update _enfVertexList
1548       TEnfVertexList::iterator it = _enfVertexList.find(oldEnfVertex);
1549       if (it != _enfVertexList.end()) {
1550         (*it)->faceEntries.erase(theFaceEntry);
1551         if ((*it)->faceEntries.size() == 0){
1552           _enfVertexList.erase(it);
1553           toNotify = true;
1554         }
1555         MESSAGE("Done");
1556       }
1557     }
1558     _faceEntryCoordsListMap.erase(it_entry_coords);
1559     _faceEntryEnfVertexListMap.erase(theFaceEntry);
1560   }
1561
1562   TFaceEntryEnfVertexEntryListMap::iterator it_entry_entry = _faceEntryEnfVertexEntryListMap.find(theFaceEntry);
1563   if (it_entry_entry != _faceEntryEnfVertexEntryListMap.end()) {
1564     toNotify = true;
1565     TEntryList enfVertexEntryList = it_entry_entry->second;
1566     TEntryList::iterator it_enfVertexEntryList = enfVertexEntryList.begin();
1567     for (; it_enfVertexEntryList != enfVertexEntryList.end(); ++it_enfVertexEntryList) {
1568       TEntry enfVertexEntry = (*it_enfVertexEntryList);
1569       oldEnfVertex = _enfVertexEntryEnfVertexMap[enfVertexEntry];
1570       _enfVertexEntryEnfVertexMap.erase(enfVertexEntry);
1571       // update _enfVertexList
1572       TEnfVertexList::iterator it = _enfVertexList.find(oldEnfVertex);
1573       if (it != _enfVertexList.end()) {
1574         (*it)->faceEntries.erase(theFaceEntry);
1575         if ((*it)->faceEntries.size() == 0){
1576           _enfVertexList.erase(it);
1577           toNotify = true;
1578         }
1579         MESSAGE("Done");
1580       }
1581     }
1582     _faceEntryEnfVertexEntryListMap.erase(it_entry_entry);
1583     _faceEntryEnfVertexListMap.erase(theFaceEntry);
1584   }
1585
1586   if (toNotify)
1587     NotifySubMeshesHypothesisModification();
1588
1589   return toNotify;
1590   //  std::ostringstream msg;
1591   //  msg << "No enforced vertex for " << theFaceEntry;
1592   //  throw std::invalid_argument(msg.str());
1593 }
1594
1595 //=======================================================================
1596 //function : ClearAllEnforcedVertices
1597 //=======================================================================
1598 void BLSURFPlugin_Hypothesis::ClearAllEnforcedVertices() {
1599   _faceEntryEnfVertexListMap.clear();
1600   _enfVertexList.clear();
1601   _faceEntryCoordsListMap.clear();
1602   _coordsEnfVertexMap.clear();
1603   _faceEntryEnfVertexEntryListMap.clear();
1604   _enfVertexEntryEnfVertexMap.clear();
1605 //  Enable internal enforced vertices on specific face if requested by user
1606 //  _faceEntryInternalVerticesList.clear();
1607   NotifySubMeshesHypothesisModification();
1608 }
1609
1610 //================================================================================
1611 /*!
1612  * \brief Return the enforced vertices
1613  */
1614 //================================================================================
1615
1616
1617 BLSURFPlugin_Hypothesis::TFaceEntryEnfVertexListMap BLSURFPlugin_Hypothesis::GetAllEnforcedVerticesByFace(
1618     const BLSURFPlugin_Hypothesis* hyp) {
1619   return hyp ? hyp->_GetAllEnforcedVerticesByFace() : GetDefaultFaceEntryEnfVertexListMap();
1620 }
1621
1622 //Enable internal enforced vertices on specific face if requested by user
1623 //BLSURFPlugin_Hypothesis::TFaceEntryInternalVerticesList BLSURFPlugin_Hypothesis::GetAllInternalEnforcedVerticesByFace(
1624 //    const BLSURFPlugin_Hypothesis* hyp) {
1625 //  return hyp ? hyp->_GetAllInternalEnforcedVerticesByFace() : GetDefaultFaceEntryInternalVerticesMap();
1626 //}
1627
1628 bool BLSURFPlugin_Hypothesis::GetInternalEnforcedVertexAllFaces(const BLSURFPlugin_Hypothesis* hyp)
1629 {
1630   return hyp ? hyp->_GetInternalEnforcedVertexAllFaces() : GetDefaultInternalEnforcedVertex();
1631 }
1632
1633 BLSURFPlugin_Hypothesis::TEnfGroupName BLSURFPlugin_Hypothesis::GetInternalEnforcedVertexAllFacesGroup(const BLSURFPlugin_Hypothesis* hyp)
1634 {
1635   return hyp ? hyp->_GetInternalEnforcedVertexAllFacesGroup() : BLSURFPlugin_Hypothesis::TEnfGroupName();
1636 }
1637
1638 BLSURFPlugin_Hypothesis::TEnfVertexList BLSURFPlugin_Hypothesis::GetAllEnforcedVertices(
1639     const BLSURFPlugin_Hypothesis* hyp) {
1640   return hyp ? hyp->_GetAllEnforcedVertices() : GetDefaultEnfVertexList();
1641 }
1642
1643 BLSURFPlugin_Hypothesis::TFaceEntryCoordsListMap BLSURFPlugin_Hypothesis::GetAllCoordsByFace(
1644     const BLSURFPlugin_Hypothesis* hyp) {
1645   return hyp ? hyp->_GetAllCoordsByFace() : GetDefaultFaceEntryCoordsListMap();
1646 }
1647
1648 BLSURFPlugin_Hypothesis::TCoordsEnfVertexMap BLSURFPlugin_Hypothesis::GetAllEnforcedVerticesByCoords(
1649     const BLSURFPlugin_Hypothesis* hyp) {
1650   return hyp ? hyp->_GetAllEnforcedVerticesByCoords() : GetDefaultCoordsEnfVertexMap();
1651 }
1652
1653 BLSURFPlugin_Hypothesis::TFaceEntryEnfVertexEntryListMap BLSURFPlugin_Hypothesis::GetAllEnfVertexEntriesByFace(
1654     const BLSURFPlugin_Hypothesis* hyp) {
1655   return hyp ? hyp->_GetAllEnfVertexEntriesByFace() : GetDefaultFaceEntryEnfVertexEntryListMap();
1656 }
1657
1658 BLSURFPlugin_Hypothesis::TEnfVertexEntryEnfVertexMap BLSURFPlugin_Hypothesis::GetAllEnforcedVerticesByEnfVertexEntry(
1659     const BLSURFPlugin_Hypothesis* hyp) {
1660   return hyp ? hyp->_GetAllEnforcedVerticesByEnfVertexEntry() : GetDefaultEnfVertexEntryEnfVertexMap();
1661 }
1662
1663 std::set<int> BLSURFPlugin_Hypothesis::GetEnfVertexNodeIDs(TEnfGroupName theGroupName) throw (std::invalid_argument)
1664 {
1665   TGroupNameNodeIDMap::const_iterator it = _groupNameNodeIDMap.find(theGroupName);
1666   if (it != _groupNameNodeIDMap.end()) {
1667     return it->second;
1668   }
1669   std::ostringstream msg;
1670   msg << "No group " << theGroupName;
1671   throw std::invalid_argument(msg.str());
1672 }
1673
1674 void BLSURFPlugin_Hypothesis::AddEnfVertexNodeID(TEnfGroupName theGroupName,int theNodeID)
1675 {
1676   _groupNameNodeIDMap[theGroupName].insert(theNodeID);
1677 }
1678
1679 void BLSURFPlugin_Hypothesis::RemoveEnfVertexNodeID(TEnfGroupName theGroupName,int theNodeID) throw (std::invalid_argument)
1680 {
1681   TGroupNameNodeIDMap::iterator it = _groupNameNodeIDMap.find(theGroupName);
1682   if (it != _groupNameNodeIDMap.end()) {
1683     std::set<int>::iterator IDit = it->second.find(theNodeID);
1684     if (IDit != it->second.end())
1685       it->second.erase(IDit);
1686     std::ostringstream msg;
1687     msg << "No node IDs " << theNodeID << " for group " << theGroupName;
1688     throw std::invalid_argument(msg.str());
1689   }
1690   std::ostringstream msg;
1691   msg << "No group " << theGroupName;
1692   throw std::invalid_argument(msg.str());
1693 }
1694
1695
1696 //=============================================================================
1697 void BLSURFPlugin_Hypothesis::SetInternalEnforcedVertexAllFaces(bool toEnforceInternalVertices) {
1698   if (toEnforceInternalVertices != _enforcedInternalVerticesAllFaces) {
1699     _enforcedInternalVerticesAllFaces = toEnforceInternalVertices;
1700     if (toEnforceInternalVertices)
1701       SetPhysicalMesh(PhysicalLocalSize);
1702     NotifySubMeshesHypothesisModification();
1703   }
1704 }
1705
1706
1707 //=============================================================================
1708 void BLSURFPlugin_Hypothesis::SetInternalEnforcedVertexAllFacesGroup(BLSURFPlugin_Hypothesis::TEnfGroupName theGroupName) {
1709   if (std::string(theGroupName) != std::string(_enforcedInternalVerticesAllFacesGroup)) {
1710     _enforcedInternalVerticesAllFacesGroup = theGroupName;
1711     NotifySubMeshesHypothesisModification();
1712   }
1713 }
1714
1715 //=============================================================================
1716 BLSURFPlugin_Hypothesis::TPreCadPeriodicityVector BLSURFPlugin_Hypothesis::GetPreCadFacesPeriodicityVector(
1717     const BLSURFPlugin_Hypothesis* hyp) {
1718   return hyp ? hyp->_GetPreCadFacesPeriodicityVector() : GetDefaultPreCadFacesPeriodicityVector();
1719 }
1720
1721 //=============================================================================
1722 BLSURFPlugin_Hypothesis::TPreCadPeriodicityVector BLSURFPlugin_Hypothesis::GetPreCadEdgesPeriodicityVector(
1723     const BLSURFPlugin_Hypothesis* hyp) {
1724   return hyp ? hyp->_GetPreCadEdgesPeriodicityVector() : GetDefaultPreCadEdgesPeriodicityVector();
1725 }
1726
1727 //=============================================================================
1728 BLSURFPlugin_Hypothesis::TFacesPeriodicityVector BLSURFPlugin_Hypothesis::GetFacesPeriodicityVector(
1729     const BLSURFPlugin_Hypothesis* hyp) {
1730   return hyp ? hyp->_GetFacesPeriodicityVector() : GetDefaultFacesPeriodicityVector();
1731 }
1732
1733 //=============================================================================
1734 BLSURFPlugin_Hypothesis::TEdgesPeriodicityVector BLSURFPlugin_Hypothesis::GetEdgesPeriodicityVector(
1735     const BLSURFPlugin_Hypothesis* hyp){
1736   return hyp ? hyp->_GetEdgesPeriodicityVector() : GetDefaultEdgesPeriodicityVector();
1737 }
1738
1739 //=============================================================================
1740 BLSURFPlugin_Hypothesis::TVerticesPeriodicityVector BLSURFPlugin_Hypothesis::GetVerticesPeriodicityVector(
1741     const BLSURFPlugin_Hypothesis* hyp){
1742   return hyp ? hyp->_GetVerticesPeriodicityVector() : GetDefaultVerticesPeriodicityVector();
1743 }
1744
1745 //=======================================================================
1746 //function : ClearAllEnforcedVertices
1747 //=======================================================================
1748 void BLSURFPlugin_Hypothesis::ClearPreCadPeriodicityVectors() {
1749   _preCadFacesPeriodicityVector.clear();
1750   _preCadEdgesPeriodicityVector.clear();
1751   NotifySubMeshesHypothesisModification();
1752 }
1753
1754 //=======================================================================
1755 //function : AddPreCadFacesPeriodicity
1756 //=======================================================================
1757 void BLSURFPlugin_Hypothesis::AddPreCadFacesPeriodicity(TEntry theFace1Entry, TEntry theFace2Entry,
1758     std::vector<std::string> &theSourceVerticesEntries, std::vector<std::string> &theTargetVerticesEntries) {
1759
1760   TPreCadPeriodicity preCadFacesPeriodicity;
1761   preCadFacesPeriodicity.shape1Entry = theFace1Entry;
1762   preCadFacesPeriodicity.shape2Entry = theFace2Entry;
1763   preCadFacesPeriodicity.theSourceVerticesEntries = theSourceVerticesEntries;
1764   preCadFacesPeriodicity.theTargetVerticesEntries = theTargetVerticesEntries;
1765
1766   _preCadFacesPeriodicityVector.push_back(preCadFacesPeriodicity);
1767
1768   NotifySubMeshesHypothesisModification();
1769 }
1770
1771 //=======================================================================
1772 //function : AddPreCadEdgesPeriodicity
1773 //=======================================================================
1774 void BLSURFPlugin_Hypothesis::AddPreCadEdgesPeriodicity(TEntry theEdge1Entry, TEntry theEdge2Entry,
1775     std::vector<std::string> &theSourceVerticesEntries, std::vector<std::string> &theTargetVerticesEntries) {
1776
1777   TPreCadPeriodicity preCadEdgesPeriodicity;
1778   preCadEdgesPeriodicity.shape1Entry = theEdge1Entry;
1779   preCadEdgesPeriodicity.shape2Entry = theEdge2Entry;
1780   preCadEdgesPeriodicity.theSourceVerticesEntries = theSourceVerticesEntries;
1781   preCadEdgesPeriodicity.theTargetVerticesEntries = theTargetVerticesEntries;
1782
1783   _preCadEdgesPeriodicityVector.push_back(preCadEdgesPeriodicity);
1784
1785   NotifySubMeshesHypothesisModification();
1786 }
1787
1788 //=============================================================================
1789 std::ostream & BLSURFPlugin_Hypothesis::SaveTo(std::ostream & save) {
1790    // We must keep at least the same number of arguments when increasing the SALOME version
1791    // When MG-CADSurf becomes CADMESH, some parameters were fused into a single one. Thus the same
1792    // parameter can be written several times to keep the old global number of parameters.
1793
1794    // Treat old options which are now in the advanced options
1795    TOptionValues::iterator op_val;
1796    int _decimesh = -1;
1797    int _preCADRemoveNanoEdges = -1;
1798    double _preCADEpsNano = -1.0;
1799    op_val = _option2value.find("respect_geometry");
1800    if (op_val != _option2value.end()) {
1801      std::string value = op_val->second;
1802      if (!value.empty())
1803        _decimesh = value.compare("1") == 0 ? 1 : 0;
1804    }
1805    op_val = _preCADoption2value.find("remove_tiny_edges");
1806    if (op_val != _preCADoption2value.end()) {
1807      std::string value = op_val->second;
1808      if (!value.empty())
1809        _preCADRemoveNanoEdges = value.compare("1") == 0 ? 1 : 0;
1810    }
1811    op_val = _preCADoption2value.find("tiny_edge_length");
1812    if (op_val != _preCADoption2value.end()) {
1813      std::string value = op_val->second;
1814      if (!value.empty())
1815        _preCADEpsNano = strtod(value.c_str(), NULL);
1816    }
1817    
1818   save << " " << (int) _topology << " " << (int) _physicalMesh << " " << (int) _geometricMesh << " " << _phySize << " "
1819       << _angleMesh << " " << _gradation << " " << (int) _quadAllowed << " " << _decimesh;
1820   save << " " << _minSize << " " << _maxSize << " " << _angleMesh << " " << _minSize << " " << _maxSize << " " << _verb;
1821   save << " " << (int) _preCADMergeEdges << " " << _preCADRemoveNanoEdges << " " << (int) _preCADDiscardInput << " " << _preCADEpsNano ;
1822   save << " " << (int) _enforcedInternalVerticesAllFaces;
1823   save << " " << (int) _phySizeRel << " " << (int) _minSizeRel << " " << (int) _maxSizeRel << " " << _chordalError ;
1824   save << " " << (int) _anisotropic << " " << _anisotropicRatio << " " << (int) _removeTinyEdges << " " << _tinyEdgeLength ;
1825   save << " " << (int) _badElementRemoval << " " << _badElementAspectRatio << " " << (int) _optimizeMesh << " " << (int) _quadraticMesh ;
1826   save << " " << (int) _preCADProcess3DTopology << " " << (int) _preCADRemoveDuplicateCADFaces << " " << (int) _preCADRemoveTinyUVEdges;
1827   save << " " << (int)_optimiseTinyEdges << " " << _tinyEdgeOptimisationLength;
1828   save << " " << (int)_correctSurfaceIntersec << " " << _corrSurfaceIntersCost;
1829   save << " " << (int)_useGradation << " " << (int)_useVolumeGradation << " " << _volumeGradation;
1830
1831   op_val = _option2value.begin();
1832   if (op_val != _option2value.end()) {
1833     save << " " << "__OPTIONS_BEGIN__";
1834     for (; op_val != _option2value.end(); ++op_val) {
1835       if (!op_val->second.empty())
1836         save << " " << op_val->first << " " << op_val->second << "%#"; // "%#" is a mark of value end
1837     }
1838     save << " " << "__OPTIONS_END__";
1839   }
1840   
1841   op_val = _customOption2value.begin();
1842   if (op_val != _customOption2value.end()) {
1843     save << " " << "__CUSTOM_OPTIONS_BEGIN__";
1844     for (; op_val != _customOption2value.end(); ++op_val) {
1845       if (!op_val->second.empty())
1846         save << " " << op_val->first << " " << op_val->second << "%#"; // "%#" is a mark of value end
1847     }
1848     save << " " << "__CUSTOM_OPTIONS_END__";
1849   }
1850
1851   op_val = _preCADoption2value.begin();
1852   if (op_val != _preCADoption2value.end()) {
1853     save << " " << "__PRECAD_OPTIONS_BEGIN__";
1854     for (; op_val != _preCADoption2value.end(); ++op_val) {
1855       if (!op_val->second.empty())
1856         save << " " << op_val->first << " " << op_val->second << "%#"; // "%#" is a mark of value end
1857     }
1858     save << " " << "__PRECAD_OPTIONS_END__";
1859   }
1860
1861   TSizeMap::iterator it_sm = _sizeMap.begin();
1862   if (it_sm != _sizeMap.end()) {
1863     save << " " << "__SIZEMAP_BEGIN__";
1864     for (; it_sm != _sizeMap.end(); ++it_sm) {
1865       save << " " << it_sm->first << " " << it_sm->second << "%#"; // "%#" is a mark of value end
1866     }
1867     save << " " << "__SIZEMAP_END__";
1868   }
1869
1870   TSizeMap::iterator it_at = _attractors.begin();
1871   if (it_at != _attractors.end()) {
1872     save << " " << "__ATTRACTORS_BEGIN__";
1873     for (; it_at != _attractors.end(); ++it_at) {
1874       save << " " << it_at->first << " " << it_at->second << "%#"; // "%#" is a mark of value end
1875     }
1876     save << " " << "__ATTRACTORS_END__";
1877   }
1878   
1879   TAttractorMap::iterator it_At = _classAttractors.begin();
1880   if (it_At != _classAttractors.end()) {
1881     std::ostringstream test;
1882     save << " " << "__NEW_ATTRACTORS_BEGIN__";
1883     test << " " << "__NEW_ATTRACTORS_BEGIN__";
1884     for (; it_At != _classAttractors.end(); ++it_At) {
1885       std::vector<double> attParams;
1886       attParams   = it_At->second->GetParameters();
1887 //       double step = it_At->second->GetStep();
1888       save << " " << it_At->first;
1889       save << " " << it_At->second->GetAttractorEntry();
1890       save << " " << attParams[0]  << " " <<  attParams[1] << " " <<  attParams[2] << " " <<  attParams[3];
1891 //       save << " " << step;
1892       test << " " << it_At->first;
1893       test << " " << it_At->second->GetAttractorEntry();
1894       test << " " << attParams[0]  << " " <<  attParams[1] << " " <<  attParams[2] << " " <<  attParams[3];
1895 //       test << " " << step;
1896     }
1897     save << " " << "__NEW_ATTRACTORS_END__";
1898     test << " " << "__NEW_ATTRACTORS_END__";
1899     MESSAGE(" Attractor hypothesis saved as "<<test.str())
1900   }
1901
1902   TEnfVertexList::const_iterator it_enf = _enfVertexList.begin();
1903   if (it_enf != _enfVertexList.end()) {
1904     save << " " << "__ENFORCED_VERTICES_BEGIN__";
1905     for (; it_enf != _enfVertexList.end(); ++it_enf) {
1906       TEnfVertex *enfVertex = (*it_enf);
1907       save << " " << "__BEGIN_VERTEX__";
1908       if (!enfVertex->name.empty()) {
1909         save << " " << "__BEGIN_NAME__";
1910         save << " " << enfVertex->name;
1911         save << " " << "__END_NAME__";
1912       }
1913       if (!enfVertex->geomEntry.empty()) {
1914         save << " " << "__BEGIN_ENTRY__";
1915         save << " " << enfVertex->geomEntry;
1916         save << " " << "__END_ENTRY__";
1917       }
1918       if (!enfVertex->grpName.empty()) {
1919         save << " " << "__BEGIN_GROUP__";
1920         save << " " << enfVertex->grpName;
1921         save << " " << "__END_GROUP__";
1922       }
1923       if (enfVertex->coords.size()) {
1924         save << " " << "__BEGIN_COORDS__";
1925         for ( size_t i = 0; i < enfVertex->coords.size(); i++ )
1926           save << " " << enfVertex->coords[i];
1927         save << " " << "__END_COORDS__";
1928       }
1929       TEntryList::const_iterator faceEntriesIt = enfVertex->faceEntries.begin();
1930       bool hasFaces = false;
1931       if (faceEntriesIt != enfVertex->faceEntries.end()) {
1932         hasFaces = true;
1933         save << " " << "__BEGIN_FACELIST__";
1934       }
1935       for (; faceEntriesIt != enfVertex->faceEntries.end(); ++faceEntriesIt)
1936         save << " " << (*faceEntriesIt);
1937       if (hasFaces)
1938         save << " " << "__END_FACELIST__";
1939       save << " " << "__END_VERTEX__";
1940     }
1941     save << " " << "__ENFORCED_VERTICES_END__";
1942   }
1943
1944   //PERIODICITY
1945
1946   SavePreCADPeriodicity(save, "FACES");
1947   SavePreCADPeriodicity(save, "EDGES");
1948
1949   SaveFacesPeriodicity(save);
1950   SaveEdgesPeriodicity(save);
1951   SaveVerticesPeriodicity(save);
1952
1953   return save;
1954 }
1955
1956 void BLSURFPlugin_Hypothesis::SaveFacesPeriodicity(std::ostream & save){
1957
1958   TFacesPeriodicityVector::const_iterator it_faces_periodicity = _facesPeriodicityVector.begin();
1959   if (it_faces_periodicity != _facesPeriodicityVector.end()) {
1960     MESSAGE("__FACES_PERIODICITY_BEGIN__");
1961     save << " " << "__FACES_PERIODICITY_BEGIN__";
1962     for (; it_faces_periodicity != _facesPeriodicityVector.end(); ++it_faces_periodicity) {
1963       TFacesPeriodicity periodicity_i = (*it_faces_periodicity);
1964       save << " " << "__BEGIN_PERIODICITY_DESCRIPTION__";
1965       save << " " << "__BEGIN_ENTRY1__";
1966       save << " " << periodicity_i.first;
1967       save << " " << "__END_ENTRY1__";
1968       save << " " << "__BEGIN_ENTRY2__";
1969       save << " " << periodicity_i.second;
1970       save << " " << "__END_ENTRY2__";
1971       save << " " << "__END_PERIODICITY_DESCRIPTION__";
1972     }
1973     save << " " << "__FACES_PERIODICITY_END__";
1974     MESSAGE("__FACES_PERIODICITY_END__");
1975   }
1976 }
1977
1978 void BLSURFPlugin_Hypothesis::SaveEdgesPeriodicity(std::ostream & save){
1979
1980   TEdgesPeriodicityVector::const_iterator it_edges_periodicity = _edgesPeriodicityVector.begin();
1981   if (it_edges_periodicity != _edgesPeriodicityVector.end()) {
1982     save << " " << "__EDGES_PERIODICITY_BEGIN__";
1983     MESSAGE("__EDGES_PERIODICITY_BEGIN__");
1984     for (; it_edges_periodicity != _edgesPeriodicityVector.end(); ++it_edges_periodicity) {
1985       TEdgePeriodicity periodicity_i = (*it_edges_periodicity);
1986       save << " " << "__BEGIN_PERIODICITY_DESCRIPTION__";
1987       if (! periodicity_i.theFace1Entry.empty()){
1988         save << " " << "__BEGIN_FACE1__";
1989         save << " " << periodicity_i.theFace1Entry;
1990         save << " " << "__END_FACE1__";
1991       }
1992       save << " " << "__BEGIN_EDGE1__";
1993       save << " " << periodicity_i.theEdge1Entry;
1994       save << " " << "__END_EDGE1__";
1995       if (! periodicity_i.theFace2Entry.empty()){
1996         save << " " << "__BEGIN_FACE2__";
1997         save << " " << periodicity_i.theFace2Entry;
1998         save << " " << "__END_FACE2__";
1999       }
2000       save << " " << "__BEGIN_EDGE2__";
2001       save << " " << periodicity_i.theEdge2Entry;
2002       save << " " << "__END_EDGE2__";
2003       save << " " << "__BEGIN_EDGE_ORIENTATION__";
2004       save << " " << periodicity_i.edge_orientation;
2005       save << " " << "__END_EDGE_ORIENTATION__";
2006       save << " " << "__END_PERIODICITY_DESCRIPTION__";
2007     }
2008     save << " " << "__EDGES_PERIODICITY_END__";
2009     MESSAGE("__EDGES_PERIODICITY_END__");
2010   }
2011 }
2012
2013 void BLSURFPlugin_Hypothesis::SaveVerticesPeriodicity(std::ostream & save){
2014
2015   TVerticesPeriodicityVector::const_iterator it_vertices_periodicity = _verticesPeriodicityVector.begin();
2016   if (it_vertices_periodicity != _verticesPeriodicityVector.end()) {
2017     MESSAGE("__VERTICES_PERIODICITY_BEGIN__");
2018     save << " " << "__VERTICES_PERIODICITY_BEGIN__";
2019     for (; it_vertices_periodicity != _verticesPeriodicityVector.end(); ++it_vertices_periodicity) {
2020       TVertexPeriodicity periodicity_i = (*it_vertices_periodicity);
2021       save << " " << "__BEGIN_PERIODICITY_DESCRIPTION__";
2022       save << " " << "__BEGIN_EDGE1__";
2023       save << " " << periodicity_i.theEdge1Entry;
2024       save << " " << "__END_EDGE1__";
2025       save << " " << "__BEGIN_VERTEX1__";
2026       save << " " << periodicity_i.theVertex1Entry;
2027       save << " " << "__END_VERTEX1__";
2028       save << " " << "__BEGIN_EDGE2__";
2029       save << " " << periodicity_i.theEdge2Entry;
2030       save << " " << "__END_EDGE2__";
2031       save << " " << "__BEGIN_VERTEX2__";
2032       save << " " << periodicity_i.theVertex2Entry;
2033       save << " " << "__END_VERTEX2__";
2034       save << " " << "__END_PERIODICITY_DESCRIPTION__";
2035     }
2036     save << " " << "__VERTICES_PERIODICITY_END__";
2037     MESSAGE("__VERTICES_PERIODICITY_END__");
2038   }
2039 }
2040
2041 void BLSURFPlugin_Hypothesis::SavePreCADPeriodicity(std::ostream & save, const char* shapeType) {
2042   TPreCadPeriodicityVector precad_periodicity;
2043   if ( shapeType  &&  strcmp( shapeType, "FACES" ) == 0 )
2044     precad_periodicity = _preCadFacesPeriodicityVector;
2045   else
2046     precad_periodicity = _preCadEdgesPeriodicityVector;
2047   TPreCadPeriodicityVector::const_iterator it_precad_periodicity = precad_periodicity.begin();
2048   if (it_precad_periodicity != precad_periodicity.end()) {
2049     save << " " << "__PRECAD_" << shapeType << "_PERIODICITY_BEGIN__";
2050     for (; it_precad_periodicity != precad_periodicity.end(); ++it_precad_periodicity) {
2051       TPreCadPeriodicity periodicity_i = (*it_precad_periodicity);
2052       save << " " << "__BEGIN_PERIODICITY_DESCRIPTION__";
2053       if (!periodicity_i.shape1Entry.empty()) {
2054         save << " " << "__BEGIN_ENTRY1__";
2055         save << " " << periodicity_i.shape1Entry;
2056         save << " " << "__END_ENTRY1__";
2057       }
2058       if (!periodicity_i.shape2Entry.empty()) {
2059         save << " " << "__BEGIN_ENTRY2__";
2060         save << " " << periodicity_i.shape2Entry;
2061         save << " " << "__END_ENTRY2__";
2062       }
2063
2064       std::vector<std::string>::const_iterator sourceVerticesEntriesIt = periodicity_i.theSourceVerticesEntries.begin();
2065       bool hasSourceVertices = false;
2066       if (sourceVerticesEntriesIt != periodicity_i.theSourceVerticesEntries.end()) {
2067         hasSourceVertices = true;
2068         save << " " << "__BEGIN_SOURCE_VERTICES_LIST__";
2069       }
2070       for (; sourceVerticesEntriesIt != periodicity_i.theSourceVerticesEntries.end(); ++sourceVerticesEntriesIt)
2071         save << " " << (*sourceVerticesEntriesIt);
2072       if (hasSourceVertices)
2073         save << " " << "__END_SOURCE_VERTICES_LIST__";
2074
2075       std::vector<std::string>::const_iterator targetVerticesEntriesIt = periodicity_i.theTargetVerticesEntries.begin();
2076       bool hasTargetVertices = false;
2077       if (targetVerticesEntriesIt != periodicity_i.theTargetVerticesEntries.end()) {
2078         hasTargetVertices = true;
2079         save << " " << "__BEGIN_TARGET_VERTICES_LIST__";
2080       }
2081       for (; targetVerticesEntriesIt != periodicity_i.theTargetVerticesEntries.end(); ++targetVerticesEntriesIt)
2082         save << " " << (*targetVerticesEntriesIt);
2083       if (hasTargetVertices)
2084         save << " " << "__END_TARGET_VERTICES_LIST__";
2085
2086       save << " " << "__END_PERIODICITY_DESCRIPTION__";
2087     }
2088     save << " " << "__PRECAD_" << shapeType << "_PERIODICITY_END__";
2089   }
2090
2091 }
2092
2093 //=============================================================================
2094 std::istream & BLSURFPlugin_Hypothesis::LoadFrom(std::istream & load) {
2095   bool isOK = true;
2096   int i;
2097   double val;
2098   std::string option_or_sm;
2099
2100   isOK = static_cast<bool>(load >> i);
2101   if (isOK)
2102     _topology = (Topology) i;
2103   else
2104     load.clear(std::ios::badbit | load.rdstate());
2105
2106   isOK = static_cast<bool>(load >> i);
2107   if (isOK)
2108     _physicalMesh = (PhysicalMesh) i;
2109   else
2110     load.clear(std::ios::badbit | load.rdstate());
2111
2112   isOK = static_cast<bool>(load >> i);
2113   if (isOK)
2114     _geometricMesh = (GeometricMesh) i;
2115   else
2116     load.clear(std::ios::badbit | load.rdstate());
2117
2118   isOK = static_cast<bool>(load >> val);
2119   if (isOK)
2120     _phySize = val;
2121   else
2122     load.clear(std::ios::badbit | load.rdstate());
2123
2124   isOK = static_cast<bool>(load >> val);
2125   if (isOK)
2126     _angleMesh = val;
2127   else
2128     load.clear(std::ios::badbit | load.rdstate());
2129
2130   isOK = static_cast<bool>(load >> val);
2131   if (isOK)
2132     _gradation = val;
2133   else
2134     load.clear(std::ios::badbit | load.rdstate());
2135
2136   isOK = static_cast<bool>(load >> i);
2137   if (isOK)
2138     _quadAllowed = (bool) i;
2139   else
2140     load.clear(std::ios::badbit | load.rdstate());
2141
2142   isOK = static_cast<bool>(load >> i);
2143   if (isOK) {
2144     if ( i != -1) { // if value is -1, then this is no longer a standard option
2145       std::string & value = _option2value["respect_geometry"];
2146       bool _decimesh = (bool) i;
2147       value = _decimesh ? "1" : "0";
2148     }
2149   }
2150   else
2151     load.clear(std::ios::badbit | load.rdstate());
2152
2153   isOK = static_cast<bool>(load >> val);
2154   if (isOK)
2155     _minSize = val;
2156   else
2157     load.clear(std::ios::badbit | load.rdstate());
2158
2159   isOK = static_cast<bool>(load >> val);
2160   if (isOK)
2161     _maxSize = val;
2162   else
2163     load.clear(std::ios::badbit | load.rdstate());
2164
2165   isOK = static_cast<bool>(load >> val);
2166   if (isOK)
2167     // former parameter: get min value
2168     _angleMesh = std::min(val,_angleMesh);
2169   else
2170     load.clear(std::ios::badbit | load.rdstate());
2171
2172   isOK = static_cast<bool>(load >> val);
2173   if (isOK)
2174     // former parameter: get min value
2175     _minSize = std::min(val,_minSize);
2176   else
2177     load.clear(std::ios::badbit | load.rdstate());
2178
2179   isOK = static_cast<bool>(load >> val);
2180   if (isOK)
2181     // former parameter: get max value
2182     _maxSize = std::max(val,_maxSize);
2183   else
2184     load.clear(std::ios::badbit | load.rdstate());
2185
2186   isOK = static_cast<bool>(load >> i);
2187   if (isOK)
2188     _verb = i;
2189   else
2190     load.clear(std::ios::badbit | load.rdstate());
2191
2192   isOK = static_cast<bool>(load >> i);
2193   if (isOK)
2194     _preCADMergeEdges = (bool) i;
2195   else
2196     load.clear(std::ios::badbit | load.rdstate());
2197
2198   isOK = static_cast<bool>(load >> i);
2199   if (isOK) {
2200     if ( i != -1) { // if value is -1, then this is no longer a standard option
2201       std::string & value = _preCADoption2value["remove_tiny_edges"];
2202       bool _preCADRemoveNanoEdges = (bool) i;
2203       value = _preCADRemoveNanoEdges ? "1" : "0";
2204     }
2205   }
2206   else
2207     load.clear(std::ios::badbit | load.rdstate());
2208
2209   isOK = static_cast<bool>(load >> i);
2210   if (isOK)
2211     _preCADDiscardInput = (bool) i;
2212   else
2213     load.clear(std::ios::badbit | load.rdstate());
2214
2215   isOK = static_cast<bool>(load >> val);
2216   if (isOK) { // _preCADEpsNano
2217     if ( (i + 1.0) < 1e-6 ) { // if value is -1, then this is no longer a standard option: get optional value "tiny_edge_length" instead
2218       std::string & value = _preCADoption2value["tiny_edge_length"];
2219       std::ostringstream oss;
2220       oss << i;
2221       value = oss.str();
2222     }
2223   }
2224   else
2225     load.clear(std::ios::badbit | load.rdstate());
2226
2227   isOK = static_cast<bool>(load >> i);
2228   if (isOK)
2229     _enforcedInternalVerticesAllFaces = (bool) i;
2230   else
2231     load.clear(std::ios::badbit | load.rdstate());
2232
2233   // New options with MeshGems-CADSurf
2234
2235   bool hasCADSurfOptions = false;
2236   bool hasOptions = false;
2237   bool hasCustomOptions = false;
2238   bool hasPreCADOptions = false;
2239   bool hasSizeMap = false;
2240   bool hasAttractor = false;
2241   bool hasNewAttractor = false;
2242   bool hasEnforcedVertex = false;
2243   bool hasPreCADFacesPeriodicity = false;
2244   bool hasPreCADEdgesPeriodicity = false;
2245   bool hasFacesPeriodicity = false;
2246   bool hasEdgesPeriodicity = false;
2247   bool hasVerticesPeriodicity = false;
2248
2249   isOK = static_cast<bool>(load >> option_or_sm);
2250   if (isOK)
2251     if ( (option_or_sm == "1")||(option_or_sm == "0") ) {
2252       i = atoi(option_or_sm.c_str());
2253       hasCADSurfOptions = true;
2254       _phySizeRel = (bool) i;
2255     }
2256     if (option_or_sm == "__OPTIONS_BEGIN__")
2257       hasOptions = true;
2258     else if (option_or_sm == "__CUSTOM_OPTIONS_BEGIN__")
2259       hasCustomOptions = true;
2260     else if (option_or_sm == "__PRECAD_OPTIONS_BEGIN__")
2261       hasPreCADOptions = true;
2262     else if (option_or_sm == "__SIZEMAP_BEGIN__")
2263       hasSizeMap = true;
2264     else if (option_or_sm == "__ATTRACTORS_BEGIN__")
2265       hasAttractor = true;
2266     else if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2267       hasNewAttractor = true;
2268     else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2269       hasEnforcedVertex = true;
2270     else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2271       hasPreCADFacesPeriodicity = true;
2272     else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2273       hasPreCADEdgesPeriodicity = true;
2274     else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2275       hasFacesPeriodicity = true;
2276     else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2277       hasEdgesPeriodicity = true;
2278     else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2279       hasVerticesPeriodicity = true;
2280
2281   if (isOK && hasCADSurfOptions) {
2282     isOK = static_cast<bool>(load >> i);
2283     if (isOK)
2284       _minSizeRel = (bool) i;
2285     else
2286       load.clear(std::ios::badbit | load.rdstate());
2287
2288     isOK = static_cast<bool>(load >> i);
2289     if (isOK)
2290       _maxSizeRel = (bool) i;
2291     else
2292       load.clear(std::ios::badbit | load.rdstate());
2293
2294     isOK = static_cast<bool>(load >> val);
2295     if (isOK)
2296       _chordalError = val;
2297     else
2298       load.clear(std::ios::badbit | load.rdstate());
2299
2300     isOK = static_cast<bool>(load >> i);
2301     if (isOK)
2302       _anisotropic = (bool) i;
2303     else
2304       load.clear(std::ios::badbit | load.rdstate());
2305
2306     isOK = static_cast<bool>(load >> val);
2307     if (isOK)
2308       _anisotropicRatio = val;
2309     else
2310       load.clear(std::ios::badbit | load.rdstate());
2311
2312     isOK = static_cast<bool>(load >> i);
2313     if (isOK)
2314       _removeTinyEdges = (bool) i;
2315     else
2316       load.clear(std::ios::badbit | load.rdstate());
2317
2318     isOK = static_cast<bool>(load >> val);
2319     if (isOK)
2320       _tinyEdgeLength = val;
2321     else
2322       load.clear(std::ios::badbit | load.rdstate());
2323
2324     isOK = static_cast<bool>(load >> i);
2325     if (isOK)
2326       _badElementRemoval = (bool) i;
2327     else
2328       load.clear(std::ios::badbit | load.rdstate());
2329
2330     isOK = static_cast<bool>(load >> val);
2331     if (isOK)
2332       _badElementAspectRatio = val;
2333     else
2334       load.clear(std::ios::badbit | load.rdstate());
2335
2336     isOK = static_cast<bool>(load >> i);
2337     if (isOK)
2338       _optimizeMesh = (bool) i;
2339     else
2340       load.clear(std::ios::badbit | load.rdstate());
2341
2342     isOK = static_cast<bool>(load >> i);
2343     if (isOK)
2344       _quadraticMesh = (bool) i;
2345     else
2346       load.clear(std::ios::badbit | load.rdstate());
2347
2348     isOK = static_cast<bool>(load >> i);
2349     if (isOK)
2350       _preCADProcess3DTopology = (bool) i;
2351     else
2352       load.clear(std::ios::badbit | load.rdstate());
2353
2354     if (( load >> std::ws).peek() != '_' )
2355     {
2356       isOK = static_cast<bool>(load >> i);
2357       if (isOK)
2358         _preCADRemoveDuplicateCADFaces = (bool) i;
2359       else
2360         load.clear(std::ios::badbit | load.rdstate());
2361
2362       isOK = static_cast<bool>(load >> i);
2363       if (isOK)
2364         _preCADRemoveTinyUVEdges = (bool) i;
2365       else
2366         load.clear(std::ios::badbit | load.rdstate());
2367
2368       isOK = static_cast<bool>(load >> i);
2369       if (isOK)
2370         _optimiseTinyEdges = (bool) i;
2371       else
2372         load.clear(std::ios::badbit | load.rdstate());
2373
2374       isOK = static_cast<bool>(load >> val);
2375       if (isOK)
2376         _tinyEdgeOptimisationLength = val;
2377       else
2378         load.clear(std::ios::badbit | load.rdstate());
2379
2380       isOK = static_cast<bool>(load >> i);
2381       if (isOK)
2382         _correctSurfaceIntersec = (bool) i;
2383       else
2384         load.clear(std::ios::badbit | load.rdstate());
2385
2386       isOK = static_cast<bool>(load >> val);
2387       if (isOK)
2388         _corrSurfaceIntersCost = val;
2389       else
2390         load.clear(std::ios::badbit | load.rdstate());
2391
2392       isOK = static_cast<bool>(load >> i);
2393       if (isOK)
2394         _useGradation = (bool) i;
2395       else
2396         load.clear(std::ios::badbit | load.rdstate());
2397
2398       isOK = static_cast<bool>(load >> i);
2399       if (isOK)
2400         _useVolumeGradation = (bool) i;
2401       else
2402         load.clear(std::ios::badbit | load.rdstate());
2403
2404       isOK = static_cast<bool>(load >> val);
2405       if (isOK)
2406         _volumeGradation = val;
2407       else
2408         load.clear(std::ios::badbit | load.rdstate());
2409     }
2410   }
2411
2412
2413   if (hasCADSurfOptions) {
2414     isOK = static_cast<bool>(load >> option_or_sm);
2415     if (isOK) {
2416       if (option_or_sm == "__OPTIONS_BEGIN__")
2417         hasOptions = true;
2418       else if (option_or_sm == "__CUSTOM_OPTIONS_BEGIN__")
2419         hasCustomOptions = true;
2420       else if (option_or_sm == "__PRECAD_OPTIONS_BEGIN__")
2421         hasPreCADOptions = true;
2422       else if (option_or_sm == "__SIZEMAP_BEGIN__")
2423         hasSizeMap = true;
2424       else if (option_or_sm == "__ATTRACTORS_BEGIN__")
2425         hasAttractor = true;
2426       else if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2427         hasNewAttractor = true;
2428       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2429         hasEnforcedVertex = true;
2430       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2431         hasPreCADFacesPeriodicity = true;
2432       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2433         hasPreCADEdgesPeriodicity = true;
2434       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2435         hasFacesPeriodicity = true;
2436       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2437         hasEdgesPeriodicity = true;
2438       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2439         hasVerticesPeriodicity = true;
2440     }
2441   }
2442   
2443   std::string optName, optValue;
2444   while (isOK && hasOptions) {
2445     isOK = static_cast<bool>(load >> optName);
2446     if (isOK) {
2447       if (optName == "__OPTIONS_END__")
2448         break;
2449       isOK = static_cast<bool>(load >> optValue);
2450     }
2451     if (isOK) {
2452       std::string & value = _option2value[optName];
2453       value = optValue;
2454       int len = value.size();
2455       // continue reading until "%#" encountered
2456       while (value[len - 1] != '#' || value[len - 2] != '%') {
2457         isOK = static_cast<bool>(load >> optValue);
2458         if (isOK) {
2459           value += " ";
2460           value += optValue;
2461           len = value.size();
2462         } else {
2463           break;
2464         }
2465       }
2466       value.resize(len - 2); //cut off "%#"
2467     }
2468   }
2469
2470   if (hasOptions) {
2471     isOK = static_cast<bool>(load >> option_or_sm);
2472     if (isOK) {
2473       if (option_or_sm == "__CUSTOM_OPTIONS_BEGIN__")
2474         hasCustomOptions = true;
2475       else if (option_or_sm == "__PRECAD_OPTIONS_BEGIN__")
2476         hasPreCADOptions = true;
2477       else if (option_or_sm == "__SIZEMAP_BEGIN__")
2478         hasSizeMap = true;
2479       else if (option_or_sm == "__ATTRACTORS_BEGIN__")
2480         hasAttractor = true;
2481       else if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2482         hasNewAttractor = true;
2483       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2484         hasEnforcedVertex = true;
2485       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2486         hasPreCADFacesPeriodicity = true;
2487       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2488         hasPreCADEdgesPeriodicity = true;
2489       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2490         hasFacesPeriodicity = true;
2491       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2492         hasEdgesPeriodicity = true;
2493       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2494         hasVerticesPeriodicity = true;
2495     }
2496   }
2497
2498   while (isOK && hasCustomOptions) {
2499     isOK = static_cast<bool>(load >> optName);
2500     if (isOK) {
2501       if (optName == "__CUSTOM_OPTIONS_END__")
2502         break;
2503       isOK = static_cast<bool>(load >> optValue);
2504     }
2505     if (isOK) {
2506       std::string& value = optValue;
2507       int len = value.size();
2508       // continue reading until "%#" encountered
2509       while (value[len - 1] != '#' || value[len - 2] != '%') {
2510         isOK = static_cast<bool>(load >> optValue);
2511         if (isOK) {
2512           value += " ";
2513           value += optValue;
2514           len = value.size();
2515         } else {
2516           break;
2517         }
2518       }
2519       value.resize(len - 2); //cut off "%#"
2520       _customOption2value[optName] = value;
2521     }
2522   }
2523
2524   if (hasCustomOptions) {
2525     isOK = static_cast<bool>(load >> option_or_sm);
2526     if (isOK) {
2527       if (option_or_sm == "__PRECAD_OPTIONS_BEGIN__")
2528         hasPreCADOptions = true;
2529       else if (option_or_sm == "__SIZEMAP_BEGIN__")
2530         hasSizeMap = true;
2531       else if (option_or_sm == "__ATTRACTORS_BEGIN__")
2532         hasAttractor = true;
2533       else if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2534         hasNewAttractor = true;
2535       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2536         hasEnforcedVertex = true;
2537       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2538         hasPreCADFacesPeriodicity = true;
2539       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2540         hasPreCADEdgesPeriodicity = true;
2541       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2542         hasFacesPeriodicity = true;
2543       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2544         hasEdgesPeriodicity = true;
2545       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2546         hasVerticesPeriodicity = true;
2547     }
2548   }
2549
2550   while (isOK && hasPreCADOptions) {
2551     isOK = static_cast<bool>(load >> optName);
2552     if (isOK) {
2553       if (optName == "__PRECAD_OPTIONS_END__")
2554         break;
2555       isOK = static_cast<bool>(load >> optValue);
2556     }
2557     if (isOK) {
2558       std::string & value = _preCADoption2value[optName];
2559       value = optValue;
2560       int len = value.size();
2561       // continue reading until "%#" encountered
2562       while (value[len - 1] != '#' || value[len - 2] != '%') {
2563         isOK = static_cast<bool>(load >> optValue);
2564         if (isOK) {
2565           value += " ";
2566           value += optValue;
2567           len = value.size();
2568         } else {
2569           break;
2570         }
2571       }
2572       value.resize(len - 2); //cut off "%#"
2573     }
2574   }
2575
2576   if (hasPreCADOptions) {
2577     isOK = static_cast<bool>(load >> option_or_sm);
2578     if (isOK) {
2579       if (option_or_sm == "__SIZEMAP_BEGIN__")
2580         hasSizeMap = true;
2581       else if (option_or_sm == "__ATTRACTORS_BEGIN__")
2582         hasAttractor = true;
2583       else if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2584         hasNewAttractor = true;
2585       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2586         hasEnforcedVertex = true;
2587       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2588         hasPreCADFacesPeriodicity = true;
2589       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2590         hasPreCADEdgesPeriodicity = true;
2591       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2592         hasFacesPeriodicity = true;
2593       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2594         hasEdgesPeriodicity = true;
2595       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2596         hasVerticesPeriodicity = true;
2597     }
2598   }
2599  
2600   std::string smEntry, smValue;
2601   while (isOK && hasSizeMap) {
2602     isOK = static_cast<bool>(load >> smEntry);
2603     if (isOK) {
2604       if (smEntry == "__SIZEMAP_END__")
2605         break;
2606       isOK = static_cast<bool>(load >> smValue);
2607     }
2608     if (isOK) {
2609       std::string & value2 = _sizeMap[smEntry];
2610       value2 = smValue;
2611       int len2 = value2.size();
2612       // continue reading until "%#" encountered
2613       while (value2[len2 - 1] != '#' || value2[len2 - 2] != '%') {
2614         isOK = static_cast<bool>(load >> smValue);
2615         if (isOK) {
2616           value2 += " ";
2617           value2 += smValue;
2618           len2 = value2.size();
2619         } else {
2620           break;
2621         }
2622       }
2623       value2.resize(len2 - 2); //cut off "%#"
2624     }
2625   }
2626
2627   if (hasSizeMap) {
2628     isOK = static_cast<bool>(load >> option_or_sm);
2629     if (isOK)
2630       if (option_or_sm == "__ATTRACTORS_BEGIN__")
2631         hasAttractor = true;
2632       if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2633         hasNewAttractor = true;
2634       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2635         hasEnforcedVertex = true;
2636       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2637         hasPreCADFacesPeriodicity = true;
2638       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2639         hasPreCADEdgesPeriodicity = true;
2640       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2641         hasFacesPeriodicity = true;
2642       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2643         hasEdgesPeriodicity = true;
2644       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2645         hasVerticesPeriodicity = true;
2646   }
2647
2648   std::string atEntry, atValue;
2649   while (isOK && hasAttractor) {
2650     isOK = static_cast<bool>(load >> atEntry);
2651     if (isOK) {
2652       if (atEntry == "__ATTRACTORS_END__")
2653         break;
2654       isOK = static_cast<bool>(load >> atValue);
2655     }
2656     if (isOK) {
2657       std::string & value3 = _attractors[atEntry];
2658       value3 = atValue;
2659       int len3 = value3.size();
2660       // continue reading until "%#" encountered
2661       while (value3[len3 - 1] != '#' || value3[len3 - 2] != '%') {
2662         isOK = static_cast<bool>(load >> atValue);
2663         if (isOK) {
2664           value3 += " ";
2665           value3 += atValue;
2666           len3 = value3.size();
2667         } else {
2668           break;
2669         }
2670       }
2671       value3.resize(len3 - 2); //cut off "%#"
2672     }
2673   }
2674
2675   if (hasAttractor) {
2676     isOK = static_cast<bool>(load >> option_or_sm);
2677     if (isOK) {
2678       if (option_or_sm == "__NEW_ATTRACTORS_BEGIN__")
2679         hasNewAttractor = true;
2680       else if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2681         hasEnforcedVertex = true;
2682       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2683         hasPreCADFacesPeriodicity = true;
2684       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2685         hasPreCADEdgesPeriodicity = true;
2686       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2687         hasFacesPeriodicity = true;
2688       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2689         hasEdgesPeriodicity = true;
2690       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2691         hasVerticesPeriodicity = true;
2692     }
2693   }
2694
2695   std::string newAtFaceEntry, atTestString;
2696   std::string newAtShapeEntry;
2697   double attParams[4];
2698   //double step;
2699   while (isOK && hasNewAttractor) {
2700     //std::cout<<"Load new attractor"<<std::endl;
2701     isOK = static_cast<bool>(load >> newAtFaceEntry);
2702     if (isOK) {
2703       if (newAtFaceEntry == "__NEW_ATTRACTORS_END__")
2704         break;
2705       isOK = static_cast<bool>(load >> newAtShapeEntry);
2706       if (!isOK)
2707     break;
2708       isOK = static_cast<bool>(load >> attParams[0]>>attParams[1]>>attParams[2]>>attParams[3]); //>>step);
2709     }
2710     if (isOK) {
2711       MESSAGE(" LOADING ATTRACTOR HYPOTHESIS ")
2712       const TopoDS_Shape attractorShape = BLSURFPlugin_Hypothesis::entryToShape(newAtShapeEntry);
2713       const TopoDS_Face faceShape = TopoDS::Face(BLSURFPlugin_Hypothesis::entryToShape(newAtFaceEntry));
2714       BLSURFPlugin_Attractor* attractor = new BLSURFPlugin_Attractor(faceShape, attractorShape, newAtShapeEntry);//, step);
2715       attractor->SetParameters(attParams[0], attParams[1], attParams[2], attParams[3]);
2716       //attractor->BuildMap();                     
2717       _classAttractors.insert( make_pair( newAtFaceEntry, attractor ));
2718     }
2719   }
2720   
2721   
2722   if (hasNewAttractor) {
2723     isOK = static_cast<bool>(load >> option_or_sm);
2724     if (isOK) {
2725       if (option_or_sm == "__ENFORCED_VERTICES_BEGIN__")
2726         hasEnforcedVertex = true;
2727       else if (option_or_sm == "__PRECAD_FACES_PERIODICITY_BEGIN__")
2728         hasPreCADFacesPeriodicity = true;
2729       else if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2730         hasPreCADEdgesPeriodicity = true;
2731       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2732         hasFacesPeriodicity = true;
2733       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2734         hasEdgesPeriodicity = true;
2735       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2736         hasVerticesPeriodicity = true;
2737     }
2738   }
2739
2740
2741 // 
2742 // Here is a example of the saved stream:
2743 // __ENFORCED_VERTICES_BEGIN__ 
2744 // __BEGIN_VERTEX__  => no name, no entry
2745 // __BEGIN_GROUP__ mon groupe __END_GROUP__
2746 // __BEGIN_COORDS__ 10 10 10 __END_COORDS__ 
2747 // __BEGIN_FACELIST__ 0:1:1:1:1 __END_FACELIST__ 
2748 // __END_VERTEX__ 
2749 // __BEGIN_VERTEX__ => no coords
2750 // __BEGIN_NAME__ mes points __END_NAME__ 
2751 // __BEGIN_ENTRY__ 0:1:1:4 __END_ENTRY__
2752 // __BEGIN_GROUP__ mon groupe __END_GROUP__
2753 // __BEGIN_FACELIST__ 0:1:1:1:3 __END_FACELIST__
2754 // __END_VERTEX__ 
2755 // __ENFORCED_VERTICES_END__
2756 // 
2757
2758   std::string enfSeparator;
2759   std::string enfName;
2760   std::string enfGeomEntry;
2761   std::string enfGroup;
2762   TEntryList enfFaceEntryList;
2763   double enfCoords[3];
2764   bool hasCoords = false;
2765   
2766   _faceEntryEnfVertexListMap.clear();
2767   _enfVertexList.clear();
2768   _faceEntryCoordsListMap.clear();
2769   _coordsEnfVertexMap.clear();
2770   _faceEntryEnfVertexEntryListMap.clear();
2771   _enfVertexEntryEnfVertexMap.clear();
2772   
2773   
2774   while (isOK && hasEnforcedVertex) {
2775     isOK = static_cast<bool>(load >> enfSeparator); // __BEGIN_VERTEX__
2776     TEnfVertex *enfVertex = new TEnfVertex();
2777 //     MESSAGE("enfSeparator: " <<enfSeparator);
2778     if (enfSeparator == "__ENFORCED_VERTICES_END__")
2779       break; // __ENFORCED_VERTICES_END__
2780     if (enfSeparator != "__BEGIN_VERTEX__")
2781       throw std::exception();
2782     
2783     while (isOK) {
2784       isOK = static_cast<bool>(load >> enfSeparator);
2785       MESSAGE("enfSeparator: " <<enfSeparator);
2786       if (enfSeparator == "__END_VERTEX__") {
2787         
2788         enfVertex->name = enfName;
2789         enfVertex->geomEntry = enfGeomEntry;
2790         enfVertex->grpName = enfGroup;
2791         enfVertex->coords.clear();
2792         if (hasCoords)
2793           enfVertex->coords.assign(enfCoords,enfCoords+3);
2794         enfVertex->faceEntries = enfFaceEntryList;
2795         
2796         _enfVertexList.insert(enfVertex);
2797         
2798         if (enfVertex->coords.size()) {
2799           _coordsEnfVertexMap[enfVertex->coords] = enfVertex;
2800           for (TEntryList::const_iterator it = enfVertex->faceEntries.begin() ; it != enfVertex->faceEntries.end(); ++it) {
2801             _faceEntryCoordsListMap[(*it)].insert(enfVertex->coords);
2802             _faceEntryEnfVertexListMap[(*it)].insert(enfVertex);
2803           }
2804         }
2805         if (!enfVertex->geomEntry.empty()) {
2806           _enfVertexEntryEnfVertexMap[enfVertex->geomEntry] = enfVertex;
2807           for (TEntryList::const_iterator it = enfVertex->faceEntries.begin() ; it != enfVertex->faceEntries.end(); ++it) {
2808             _faceEntryEnfVertexEntryListMap[(*it)].insert(enfVertex->geomEntry);
2809             _faceEntryEnfVertexListMap[(*it)].insert(enfVertex);
2810           }
2811         }
2812         
2813         enfName.clear();
2814         enfGeomEntry.clear();
2815         enfGroup.clear();
2816         enfFaceEntryList.clear();
2817         hasCoords = false;
2818         break; // __END_VERTEX__
2819       }
2820         
2821       if (enfSeparator == "__BEGIN_NAME__") {  // __BEGIN_NAME__
2822         while (isOK && (enfSeparator != "__END_NAME__")) {
2823           isOK = static_cast<bool>(load >> enfSeparator);
2824           if (enfSeparator != "__END_NAME__") {
2825             if (!enfName.empty())
2826               enfName += " ";
2827             enfName += enfSeparator;
2828           }
2829         }
2830         MESSAGE("enfName: " <<enfName);
2831       }
2832         
2833       if (enfSeparator == "__BEGIN_ENTRY__") {  // __BEGIN_ENTRY__
2834         isOK = static_cast<bool>(load >> enfGeomEntry);
2835         isOK = static_cast<bool>(load >> enfSeparator); // __END_ENTRY__
2836         if (enfSeparator != "__END_ENTRY__")
2837           throw std::exception();
2838         MESSAGE("enfGeomEntry: " <<enfGeomEntry);
2839       }
2840         
2841       if (enfSeparator == "__BEGIN_GROUP__") {  // __BEGIN_GROUP__
2842         while (isOK && (enfSeparator != "__END_GROUP__")) {
2843           isOK = static_cast<bool>(load >> enfSeparator);
2844           if (enfSeparator != "__END_GROUP__") {
2845             if (!enfGroup.empty())
2846               enfGroup += " ";
2847             enfGroup += enfSeparator;
2848           }
2849         }
2850         MESSAGE("enfGroup: " <<enfGroup);
2851       }
2852         
2853       if (enfSeparator == "__BEGIN_COORDS__") {  // __BEGIN_COORDS__
2854         hasCoords = true;
2855         isOK = static_cast<bool>(load >> enfCoords[0] >> enfCoords[1] >> enfCoords[2]);
2856         isOK = static_cast<bool>(load >> enfSeparator); // __END_COORDS__
2857         if (enfSeparator != "__END_COORDS__")
2858           throw std::exception();
2859         MESSAGE("enfCoords: " << enfCoords[0] <<","<< enfCoords[1] <<","<< enfCoords[2]);
2860       } 
2861         
2862       if (enfSeparator == "__BEGIN_FACELIST__") {  // __BEGIN_FACELIST__
2863         while (isOK && (enfSeparator != "__END_FACELIST__")) {
2864           isOK = static_cast<bool>(load >> enfSeparator);
2865           if (enfSeparator != "__END_FACELIST__") {
2866             enfFaceEntryList.insert(enfSeparator);
2867             MESSAGE(enfSeparator << " was inserted into enfFaceEntryList");
2868           }
2869         }
2870       } 
2871     }
2872   }
2873
2874   // PERIODICITY
2875
2876   if (hasPreCADFacesPeriodicity){
2877     LoadPreCADPeriodicity(load, "FACES");
2878
2879     isOK = static_cast<bool>(load >> option_or_sm);
2880     if (isOK) {
2881       if (option_or_sm == "__PRECAD_EDGES_PERIODICITY_BEGIN__")
2882         hasPreCADEdgesPeriodicity = true;
2883       else if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2884         hasFacesPeriodicity = true;
2885       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2886         hasEdgesPeriodicity = true;
2887       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2888         hasVerticesPeriodicity = true;
2889     }
2890   }
2891
2892   if (hasPreCADEdgesPeriodicity){
2893     LoadPreCADPeriodicity(load, "EDGES");
2894
2895     isOK = static_cast<bool>(load >> option_or_sm);
2896     if (isOK) {
2897       if (option_or_sm == "__FACES_PERIODICITY_BEGIN__")
2898         hasFacesPeriodicity = true;
2899       else if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2900         hasEdgesPeriodicity = true;
2901       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2902         hasVerticesPeriodicity = true;
2903     }
2904   }
2905
2906   if (hasFacesPeriodicity){
2907       LoadFacesPeriodicity(load);
2908
2909     isOK = static_cast<bool>(load >> option_or_sm);
2910     if (isOK) {
2911       if (option_or_sm == "__EDGES_PERIODICITY_BEGIN__")
2912         hasEdgesPeriodicity = true;
2913       else if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2914         hasVerticesPeriodicity = true;
2915     }
2916   }
2917
2918   if (hasEdgesPeriodicity){
2919       LoadEdgesPeriodicity(load);
2920
2921     isOK = static_cast<bool>(load >> option_or_sm);
2922     if (isOK)
2923       if (option_or_sm == "__VERTICES_PERIODICITY_BEGIN__")
2924         hasVerticesPeriodicity = true;
2925   }
2926
2927   if (hasVerticesPeriodicity)
2928       LoadVerticesPeriodicity(load);
2929
2930   return load;
2931 }
2932
2933 void BLSURFPlugin_Hypothesis::LoadFacesPeriodicity(std::istream & load){
2934
2935   bool isOK = true;
2936
2937   std::string periodicitySeparator;
2938   TEntry shape1Entry;
2939   TEntry shape2Entry;
2940
2941   _facesPeriodicityVector.clear();
2942
2943   while (isOK) {
2944     isOK = static_cast<bool>(load >> periodicitySeparator); // __BEGIN_PERIODICITY_DESCRIPTION__
2945     MESSAGE("periodicitySeparator 1: " <<periodicitySeparator);
2946     TFacesPeriodicity *periodicity_i = new TFacesPeriodicity();
2947     if (periodicitySeparator == "__FACES_PERIODICITY_END__")
2948       break; // __FACES_PERIODICITY_END__
2949     if (periodicitySeparator != "__BEGIN_PERIODICITY_DESCRIPTION__"){
2950       MESSAGE("//" << periodicitySeparator << "//");
2951       throw std::exception();
2952     }
2953
2954     while (isOK) {
2955       isOK = static_cast<bool>(load >> periodicitySeparator);
2956       MESSAGE("periodicitySeparator 2: " <<periodicitySeparator);
2957       if (periodicitySeparator == "__END_PERIODICITY_DESCRIPTION__") {
2958
2959         periodicity_i->first = shape1Entry;
2960         periodicity_i->second = shape2Entry;
2961
2962         _facesPeriodicityVector.push_back(*periodicity_i);
2963
2964         break; // __END_PERIODICITY_DESCRIPTION__
2965       }
2966
2967       if (periodicitySeparator == "__BEGIN_ENTRY1__") {  // __BEGIN_ENTRY1__
2968         isOK = static_cast<bool>(load >> shape1Entry);
2969         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_ENTRY1__
2970         if (periodicitySeparator != "__END_ENTRY1__")
2971           throw std::exception();
2972         MESSAGE("shape1Entry: " <<shape1Entry);
2973       }
2974
2975       if (periodicitySeparator == "__BEGIN_ENTRY2__") {  // __BEGIN_ENTRY2__
2976         isOK = static_cast<bool>(load >> shape2Entry);
2977         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_ENTRY2__
2978         if (periodicitySeparator != "__END_ENTRY2__")
2979           throw std::exception();
2980         MESSAGE("shape2Entry: " <<shape2Entry);
2981       }
2982     }
2983   }
2984 }
2985
2986
2987 void BLSURFPlugin_Hypothesis::LoadEdgesPeriodicity(std::istream & load){
2988
2989   bool isOK = true;
2990
2991   std::string periodicitySeparator;
2992   TEntry theFace1Entry;
2993   TEntry theEdge1Entry;
2994   TEntry theFace2Entry;
2995   TEntry theEdge2Entry;
2996   int edge_orientation = 0;
2997
2998   _edgesPeriodicityVector.clear();
2999
3000   while (isOK) {
3001     isOK = static_cast<bool>(load >> periodicitySeparator); // __BEGIN_PERIODICITY_DESCRIPTION__
3002     MESSAGE("periodicitySeparator 1: " <<periodicitySeparator);
3003     TEdgePeriodicity *periodicity_i = new TEdgePeriodicity();
3004     if (periodicitySeparator == "__EDGES_PERIODICITY_END__")
3005       break; // __EDGES_PERIODICITY_END__
3006     if (periodicitySeparator != "__BEGIN_PERIODICITY_DESCRIPTION__"){
3007       MESSAGE("//" << periodicitySeparator << "//");
3008       throw std::exception();
3009     }
3010
3011     while (isOK) {
3012       isOK = static_cast<bool>(load >> periodicitySeparator);
3013       MESSAGE("periodicitySeparator 2: " <<periodicitySeparator);
3014       if (periodicitySeparator == "__END_PERIODICITY_DESCRIPTION__") {
3015
3016         periodicity_i->theFace1Entry = theFace1Entry;
3017         periodicity_i->theEdge1Entry = theEdge1Entry;
3018         periodicity_i->theFace2Entry = theFace2Entry;
3019         periodicity_i->theEdge2Entry = theEdge2Entry;
3020         periodicity_i->edge_orientation = edge_orientation;
3021
3022         _edgesPeriodicityVector.push_back(*periodicity_i);
3023
3024         break; // __END_PERIODICITY_DESCRIPTION__
3025       }
3026
3027       if (periodicitySeparator == "__BEGIN_FACE1__") {  // __BEGIN_FACE1__
3028         isOK = static_cast<bool>(load >> theFace1Entry);
3029         MESSAGE("//" << theFace1Entry << "//");
3030         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_FACE1__
3031         if (periodicitySeparator != "__END_FACE1__"){
3032           MESSAGE("//" << periodicitySeparator << "//");
3033           throw std::exception();
3034         }
3035         MESSAGE("theFace1Entry: " <<theFace1Entry);
3036       }
3037
3038       if (periodicitySeparator == "__BEGIN_EDGE1__") {  // __BEGIN_EDGE1__
3039         isOK = static_cast<bool>(load >> theEdge1Entry);
3040         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_EDGE1__
3041         if (periodicitySeparator != "__END_EDGE1__")
3042           throw std::exception();
3043         MESSAGE("theEdge1Entry: " <<theEdge1Entry);
3044       }
3045
3046       if (periodicitySeparator == "__BEGIN_FACE2__") {  // __BEGIN_FACE2__
3047         isOK = static_cast<bool>(load >> theFace2Entry);
3048         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_FACE2__
3049         if (periodicitySeparator != "__END_FACE2__")
3050           throw std::exception();
3051         MESSAGE("theFace2Entry: " <<theFace2Entry);
3052       }
3053
3054       if (periodicitySeparator == "__BEGIN_EDGE2__") {  // __BEGIN_EDGE2__
3055         isOK = static_cast<bool>(load >> theEdge2Entry);
3056         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_EDGE2__
3057         if (periodicitySeparator != "__END_EDGE2__")
3058           throw std::exception();
3059         MESSAGE("theEdge2Entry: " <<theEdge2Entry);
3060       }
3061
3062       if (periodicitySeparator == "__BEGIN_EDGE_ORIENTATION__") {  // __BEGIN_EDGE_ORIENTATION__
3063         isOK = static_cast<bool>(load >> edge_orientation);
3064         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_EDGE_ORIENTATION__
3065         if (periodicitySeparator != "__END_EDGE_ORIENTATION__")
3066           throw std::exception();
3067         MESSAGE("edge_orientation: " <<edge_orientation);
3068       }
3069     }
3070   }
3071 }
3072
3073 void BLSURFPlugin_Hypothesis::LoadVerticesPeriodicity(std::istream & load){
3074
3075   bool isOK = true;
3076
3077   std::string periodicitySeparator;
3078   TEntry theEdge1Entry;
3079   TEntry theVertex1Entry;
3080   TEntry theEdge2Entry;
3081   TEntry theVertex2Entry;
3082
3083   _verticesPeriodicityVector.clear();
3084
3085   while (isOK) {
3086     isOK = static_cast<bool>(load >> periodicitySeparator); // __BEGIN_PERIODICITY_DESCRIPTION__
3087     MESSAGE("periodicitySeparator 1: " <<periodicitySeparator);
3088     TVertexPeriodicity *periodicity_i = new TVertexPeriodicity();
3089     if (periodicitySeparator == "__VERTICES_PERIODICITY_END__")
3090       break; // __VERTICES_PERIODICITY_END__
3091     if (periodicitySeparator != "__BEGIN_PERIODICITY_DESCRIPTION__"){
3092       MESSAGE("//" << periodicitySeparator << "//");
3093       throw std::exception();
3094     }
3095
3096     while (isOK) {
3097       isOK = static_cast<bool>(load >> periodicitySeparator);
3098       MESSAGE("periodicitySeparator 2: " <<periodicitySeparator);
3099       if (periodicitySeparator == "__END_PERIODICITY_DESCRIPTION__") {
3100
3101         periodicity_i->theEdge1Entry = theEdge1Entry;
3102         periodicity_i->theVertex1Entry = theVertex1Entry;
3103         periodicity_i->theEdge2Entry = theEdge2Entry;
3104         periodicity_i->theVertex2Entry = theVertex2Entry;
3105
3106         _verticesPeriodicityVector.push_back(*periodicity_i);
3107
3108         break; // __END_PERIODICITY_DESCRIPTION__
3109       }
3110
3111       if (periodicitySeparator == "__BEGIN_EDGE1__") {  // __BEGIN_EDGE1__
3112         isOK = static_cast<bool>(load >> theEdge1Entry);
3113         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_EDGE1__
3114         if (periodicitySeparator != "__END_EDGE1__")
3115           throw std::exception();
3116         MESSAGE("theEdge1Entry: " <<theEdge1Entry);
3117       }
3118
3119       if (periodicitySeparator == "__BEGIN_VERTEX1__") {  // __BEGIN_VERTEX1__
3120         isOK = static_cast<bool>(load >> theVertex1Entry);
3121         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_VERTEX1__
3122         if (periodicitySeparator != "__END_VERTEX1__")
3123           throw std::exception();
3124         MESSAGE("theVertex1Entry: " <<theVertex1Entry);
3125       }
3126
3127       if (periodicitySeparator == "__BEGIN_EDGE2__") {  // __BEGIN_EDGE2__
3128         isOK = static_cast<bool>(load >> theEdge2Entry);
3129         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_EDGE2__
3130         if (periodicitySeparator != "__END_EDGE2__")
3131           throw std::exception();
3132         MESSAGE("theEdge2Entry: " <<theEdge2Entry);
3133       }
3134
3135       if (periodicitySeparator == "__BEGIN_VERTEX2__") {  // __BEGIN_VERTEX2__
3136         isOK = static_cast<bool>(load >> theVertex2Entry);
3137         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_VERTEX2__
3138         if (periodicitySeparator != "__END_VERTEX2__")
3139           throw std::exception();
3140         MESSAGE("theVertex2Entry: " <<theVertex2Entry);
3141       }
3142     }
3143   }
3144 }
3145
3146 void BLSURFPlugin_Hypothesis::LoadPreCADPeriodicity(std::istream & load, const char* shapeType) {
3147
3148   bool isOK = true;
3149
3150   std::string periodicitySeparator;
3151   TEntry shape1Entry;
3152   TEntry shape2Entry;
3153   std::vector<std::string> theSourceVerticesEntries;
3154   std::vector<std::string> theTargetVerticesEntries;
3155
3156   bool hasSourceVertices = false;
3157   bool hasTargetVertices = false;
3158
3159   if ( shapeType  &&  strcmp( shapeType, "FACES") == 0 )
3160     _preCadFacesPeriodicityVector.clear();
3161   else
3162     _preCadEdgesPeriodicityVector.clear();
3163
3164
3165   while (isOK) {
3166     isOK = static_cast<bool>(load >> periodicitySeparator); // __BEGIN_PERIODICITY_DESCRIPTION__
3167     MESSAGE("periodicitySeparator 1: " <<periodicitySeparator);
3168     TPreCadPeriodicity *periodicity_i = new TPreCadPeriodicity();
3169 //     MESSAGE("periodicitySeparator: " <<periodicitySeparator);
3170     std::string endSeparator = "__PRECAD_" + std::string(shapeType) + "_PERIODICITY_END__";
3171     if (periodicitySeparator == endSeparator)
3172       break; // __PRECAD_FACES_PERIODICITY_END__
3173     if (periodicitySeparator != "__BEGIN_PERIODICITY_DESCRIPTION__"){
3174       MESSAGE("//" << endSeparator << "//");
3175       MESSAGE("//" << periodicitySeparator << "//");
3176       throw std::exception();
3177     }
3178
3179     while (isOK) {
3180       isOK = static_cast<bool>(load >> periodicitySeparator);
3181       MESSAGE("periodicitySeparator 2: " <<periodicitySeparator);
3182       if (periodicitySeparator == "__END_PERIODICITY_DESCRIPTION__") {
3183
3184         periodicity_i->shape1Entry = shape1Entry;
3185         periodicity_i->shape2Entry = shape2Entry;
3186
3187         MESSAGE("theSourceVerticesEntries.size(): " << theSourceVerticesEntries.size());
3188         MESSAGE("theTargetVerticesEntries.size(): " << theTargetVerticesEntries.size());
3189
3190         if (hasSourceVertices)
3191           periodicity_i->theSourceVerticesEntries = theSourceVerticesEntries;
3192         if (hasTargetVertices)
3193           periodicity_i->theTargetVerticesEntries = theTargetVerticesEntries;
3194
3195         if ( shapeType  &&  strcmp( shapeType, "FACES" ))
3196           _preCadFacesPeriodicityVector.push_back(*periodicity_i);
3197         else
3198           _preCadEdgesPeriodicityVector.push_back(*periodicity_i);
3199
3200         theSourceVerticesEntries.clear();
3201         theTargetVerticesEntries.clear();
3202         hasSourceVertices = false;
3203         hasTargetVertices = false;
3204         break; // __END_PERIODICITY_DESCRIPTION__
3205       }
3206
3207       if (periodicitySeparator == "__BEGIN_ENTRY1__") {  // __BEGIN_ENTRY1__
3208         isOK = static_cast<bool>(load >> shape1Entry);
3209         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_ENTRY1__
3210         if (periodicitySeparator != "__END_ENTRY1__")
3211           throw std::exception();
3212         MESSAGE("shape1Entry: " <<shape1Entry);
3213       }
3214
3215       if (periodicitySeparator == "__BEGIN_ENTRY2__") {  // __BEGIN_ENTRY2__
3216         isOK = static_cast<bool>(load >> shape2Entry);
3217         isOK = static_cast<bool>(load >> periodicitySeparator); // __END_ENTRY2__
3218         if (periodicitySeparator != "__END_ENTRY2__")
3219           throw std::exception();
3220         MESSAGE("shape2Entry: " <<shape2Entry);
3221       }
3222
3223       if (periodicitySeparator == "__BEGIN_SOURCE_VERTICES_LIST__") {  // __BEGIN_SOURCE_VERTICES_LIST__
3224         hasSourceVertices = true;
3225         while (isOK && (periodicitySeparator != "__END_SOURCE_VERTICES_LIST__")) {
3226           isOK = static_cast<bool>(load >> periodicitySeparator);
3227           if (periodicitySeparator != "__END_SOURCE_VERTICES_LIST__") {
3228             theSourceVerticesEntries.push_back(periodicitySeparator);
3229             MESSAGE("theSourceVerticesEntries: " <<periodicitySeparator);
3230           }
3231         }
3232       }
3233
3234       if (periodicitySeparator == "__BEGIN_TARGET_VERTICES_LIST__") {  // __BEGIN_TARGET_VERTICES_LIST__
3235         hasTargetVertices = true;
3236         while (isOK && (periodicitySeparator != "__END_TARGET_VERTICES_LIST__")) {
3237           isOK = static_cast<bool>(load >> periodicitySeparator);
3238           if (periodicitySeparator != "__END_TARGET_VERTICES_LIST__") {
3239             theTargetVerticesEntries.push_back(periodicitySeparator);
3240             MESSAGE("theTargetVerticesEntries: " <<periodicitySeparator);
3241           }
3242         }
3243       }
3244     }
3245   }
3246 }
3247
3248 //=============================================================================
3249 std::ostream & operator <<(std::ostream & save, BLSURFPlugin_Hypothesis & hyp) {
3250   return hyp.SaveTo(save);
3251 }
3252
3253 //=============================================================================
3254 std::istream & operator >>(std::istream & load, BLSURFPlugin_Hypothesis & hyp) {
3255   return hyp.LoadFrom(load);
3256 }
3257
3258 //================================================================================
3259 /*!
3260  * \brief Does nothing
3261  */
3262 //================================================================================
3263
3264 bool BLSURFPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* theMesh, const TopoDS_Shape& theShape) {
3265   return false;
3266 }
3267
3268 //================================================================================
3269 /*!
3270  * \brief Returns default global constant physical size given a default value of element length ratio
3271  */
3272 //================================================================================
3273
3274 double BLSURFPlugin_Hypothesis::GetDefaultPhySize(double diagonal, double bbSegmentation) {
3275   if (bbSegmentation != 0 && diagonal != 0)
3276     return diagonal / bbSegmentation ;
3277   return 10;
3278 }
3279
3280 //================================================================================
3281 /*!
3282  * \brief Returns default min size given a default value of element length ratio
3283  */
3284 //================================================================================
3285
3286 double BLSURFPlugin_Hypothesis::GetDefaultMinSize(double diagonal) {
3287   if (diagonal != 0)
3288     return diagonal / 1000.0 ;
3289   return undefinedDouble();
3290 }
3291
3292 //================================================================================
3293 /*!
3294  * \brief Returns default max size given a default value of element length ratio
3295  */
3296 //================================================================================
3297
3298 double BLSURFPlugin_Hypothesis::GetDefaultMaxSize(double diagonal) {
3299   if (diagonal != 0)
3300     return diagonal / 5.0 ;
3301   return undefinedDouble();
3302 }
3303
3304 //================================================================================
3305 /*!
3306  * \brief Returns default chordal error given a default value of element length ratio
3307  */
3308 //================================================================================
3309
3310 double BLSURFPlugin_Hypothesis::GetDefaultChordalError(double diagonal) {
3311   if (diagonal != 0)
3312     return diagonal;
3313   return undefinedDouble();
3314 }
3315
3316 //================================================================================
3317 /*!
3318  * \brief Returns default tiny edge length given a default value of element length ratio
3319  */
3320 //================================================================================
3321
3322 double BLSURFPlugin_Hypothesis::GetDefaultTinyEdgeLength(double diagonal) {
3323   if (diagonal != 0)
3324     return diagonal * 1e-6 ;
3325   return undefinedDouble();
3326 }
3327
3328 //================================================================================
3329 /*!
3330  * \brief Returns default tiny edge optimisation length given a default value of element length ratio
3331  */
3332 //================================================================================
3333
3334 double BLSURFPlugin_Hypothesis::GetDefaultTinyEdgeOptimisationLength(double diagonal) {
3335   if (diagonal != 0)
3336     return diagonal * 1e-6 ;
3337   return undefinedDouble();
3338 }
3339
3340 //=============================================================================
3341 /*!
3342  * \brief Initialize my parameter values by default parameters.
3343  *  \retval bool - true if parameter values have been successfully defined
3344  */
3345 //=============================================================================
3346
3347 bool BLSURFPlugin_Hypothesis::SetParametersByDefaults(const TDefaults& dflts, const SMESH_Mesh* theMesh) {
3348   double diagonal = dflts._elemLength*_gen->GetBoundaryBoxSegmentation();
3349   _phySize = GetDefaultPhySize(diagonal, _gen->GetBoundaryBoxSegmentation());
3350   _minSize = GetDefaultMinSize(diagonal);
3351   _maxSize = GetDefaultMaxSize(diagonal);
3352   _chordalError = 0.5 * _phySize; //GetDefaultChordalError(diagonal); IMP 0023307
3353   _tinyEdgeLength = GetDefaultTinyEdgeLength(diagonal);
3354   _tinyEdgeOptimisationLength = GetDefaultTinyEdgeOptimisationLength(diagonal);
3355
3356   return true;
3357 }
3358
3359 //================================================================================
3360 /*!
3361  * \brief Converts a string to a bool
3362  */
3363 //================================================================================
3364
3365 bool BLSURFPlugin_Hypothesis::ToBool(const std::string& str, bool* isOk )
3366   throw (std::invalid_argument)
3367 {
3368   std::string s = str;
3369   if ( isOk ) *isOk = true;
3370
3371   for ( size_t i = 0; i <= s.size(); ++i )
3372     s[i] = tolower( s[i] );
3373
3374   if ( s == "1" || s == "true" || s == "active" || s == "yes" )
3375     return true;
3376
3377   if ( s == "0" || s == "false" || s == "inactive" || s == "no" )
3378     return false;
3379
3380   if ( isOk )
3381     *isOk = false;
3382   else {
3383     std::string msg = "Not a Boolean value:'" + str + "'";
3384     throw std::invalid_argument(msg);
3385   }
3386   return false;
3387 }
3388
3389 //================================================================================
3390 /*!
3391  * \brief Converts a string to a real value
3392  */
3393 //================================================================================
3394
3395 double BLSURFPlugin_Hypothesis::ToDbl(const std::string& str, bool* isOk )
3396   throw (std::invalid_argument)
3397 {
3398   if ( str.empty() ) throw std::invalid_argument("Empty value provided");
3399
3400   char * endPtr;
3401   double val = strtod(&str[0], &endPtr);
3402   bool ok = (&str[0] != endPtr);
3403
3404   if ( isOk ) *isOk = ok;
3405
3406   if ( !ok )
3407   {
3408     std::string msg = "Not a real value:'" + str + "'";
3409     throw std::invalid_argument(msg);
3410   }
3411   return val;
3412 }
3413
3414 //================================================================================
3415 /*!
3416  * \brief Converts a string to a integer value
3417  */
3418 //================================================================================
3419
3420 int BLSURFPlugin_Hypothesis::ToInt(const std::string& str, bool* isOk )
3421   throw (std::invalid_argument)
3422 {
3423   if ( str.empty() ) throw std::invalid_argument("Empty value provided");
3424
3425   char * endPtr;
3426   int val = (int)strtol( &str[0], &endPtr, 10);
3427   bool ok = (&str[0] != endPtr);
3428
3429   if ( isOk ) *isOk = ok;
3430
3431   if ( !ok )
3432   {
3433     std::string msg = "Not an integer value:'" + str + "'";
3434     throw std::invalid_argument(msg);
3435   }
3436   return val;
3437 }
3438