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