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