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