Salome HOME
9bea62647310c65f3235c134c6fe4eabad555b64
[modules/hydro.git] / src / HYDROData / HYDROData_CalculationCase.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_CalculationCase.h"
20 #include "HYDROData_ArtificialObject.h"
21 #include "HYDROData_IAltitudeObject.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_ShapesGroup.h"
24 #include "HYDROData_Iterator.h"
25 #include "HYDROData_NaturalObject.h"
26 #include "HYDROData_PolylineXY.h"
27 #include "HYDROData_StricklerTable.h"
28 #include "HYDROData_LandCoverMap.h"
29 #include "HYDROData_SplitShapesGroup.h"
30 #include "HYDROData_Region.h"
31 #include "HYDROData_Tool.h"
32 #include "HYDROData_GeomTool.h"
33
34 #ifdef WIN32
35   #pragma warning ( disable: 4251 )
36 #endif
37
38 #ifndef LIGHT_MODE
39 #include <GEOMBase.h>
40 #endif
41
42 #ifdef WIN32
43   #pragma warning ( default: 4251 )
44 #endif
45
46 #include <QSet>
47
48 #include <TopoDS.hxx>
49 #include <TopoDS_Shell.hxx>
50 #include <TopoDS_Edge.hxx>
51
52 #include <BRep_Builder.hxx>
53 #include <BRepBuilderAPI_Sewing.hxx>
54 #include <BRepTopAdaptor_FClass2d.hxx>
55
56 #include <BRepTools.hxx>
57
58 #include <TopAbs.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <TopExp.hxx>
61 #include <TopTools_ListOfShape.hxx>
62 #include <TopTools_ListIteratorOfListOfShape.hxx>
63 #include <TDataStd_Integer.hxx>
64
65 //#define  DEB_CALCULATION 1
66 #ifdef DEB_CALCULATION
67 #include <BRepTools.hxx>
68 #include <TopLoc_Location.hxx>
69 #endif 
70 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
71 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
72 #define CALCULATION_GROUPS_PREF GetName() + "_"
73 //#define DEB_CLASS2D 1
74 #ifdef DEB_CLASS2D
75 #include <BRepBuilderAPI_MakeVertex.hxx>
76 #endif
77
78 #define EXPORT_NAME "HYDRO_" + GetName()
79
80 #include <SALOME_NamingService.hxx>
81 #include <SALOME_LifeCycleCORBA.hxx>
82
83 #define _DEVDEBUG_
84 #include "HYDRO_trace.hxx"
85
86 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
87
88 HYDROData_CalculationCase::HYDROData_CalculationCase()
89 : HYDROData_Entity( Geom_2d_and_groups )
90 {
91 }
92
93 HYDROData_CalculationCase::~HYDROData_CalculationCase()
94 {
95 }
96
97 void HYDROData_CalculationCase::SetName( const QString& theName )
98 {
99   QString anOldCaseName = GetName();
100   if ( anOldCaseName != theName )
101   {
102     // Update names of regions and its zones
103     UpdateRegionsNames( GetRegions(), anOldCaseName, theName );
104
105     HYDROData_SequenceOfObjects aGroups = GetSplitGroups();
106
107     HYDROData_SequenceOfObjects::Iterator anIter;
108     anIter.Init( aGroups );
109     for ( ; anIter.More(); anIter.Next() )
110     {
111       Handle(HYDROData_SplitShapesGroup) aGroup =
112         Handle(HYDROData_SplitShapesGroup)::DownCast( anIter.Value() );
113       if ( aGroup.IsNull() )
114         continue;
115
116       HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
117     }
118   }
119
120   HYDROData_Entity::SetName( theName );
121 }
122
123 QStringList HYDROData_CalculationCase::DumpToPython( const QString&       thePyScriptPath,
124                                                      MapOfTreatedObjects& theTreatedObjects ) const
125 {
126   QStringList aResList = dumpObjectCreation( theTreatedObjects );
127   aResList.prepend( "# Calculation case" );
128
129   QString aCalculName = GetObjPyName();
130
131   AssignmentMode aMode = GetAssignmentMode();  
132   QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
133   aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
134
135   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
136   HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
137   for ( ; anIter.More(); anIter.Next() )
138   {
139     Handle(HYDROData_Object) aRefGeomObj =
140       Handle(HYDROData_Object)::DownCast( anIter.Value() );
141     setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
142   }
143   aResList << QString( "" );
144
145   QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
146
147   HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
148   anIter.Init( aGeomGroups );
149   for ( ; anIter.More(); anIter.Next() )
150   {
151     Handle(HYDROData_ShapesGroup) aGeomGroup =
152       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
153     if ( aGeomGroup.IsNull() )
154       continue;
155
156     Handle(HYDROData_Object) aFatherGeom =
157       Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
158     if ( aFatherGeom.IsNull() )
159       continue;
160
161     int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
162     aResList << QString( "%1 = %2.GetGroup( %3 )" )
163               .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
164
165     aResList << QString( "%1.AddGeometryGroup( %2 )" ).arg( aCalculName ).arg( aGroupName );
166   }
167
168   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
169   setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
170
171   if( aMode==AUTOMATIC )
172     DumpRulesToPython( aCalculName, aResList );
173
174   aResList << QString( "" );
175   aResList << "# Start the algorithm of the partition and assignment";
176   aResList << QString( "%1.Update()" ).arg( aCalculName );
177
178   if( aMode==MANUAL )
179   {
180     // Now we restore the
181     // - regions and zones order
182     DumpRegionsToPython( aResList, thePyScriptPath, theTreatedObjects, GetRegions() );   
183   }
184
185   // Export calculation case
186   aResList << QString( "" );
187   aResList << "# Export of the calculation case";
188   QString aStudyName = "theStudy";
189   QString anEntryVar = aCalculName + "_entry";
190   aResList << QString( "%1 = %2.Export( %3._get_StudyId() )" ).arg( anEntryVar ).arg( aCalculName ).arg( aStudyName );
191
192   // Get geometry object and print debug information
193   aResList << "";
194   aResList << "# Get geometry shape and print debug information";
195   aResList << "import GEOM";
196   aResList << QString( "print \"Entry:\", %1" ).arg( anEntryVar );
197   QString aGeomShapeName = aCalculName + "_geom";
198   aResList << QString( "HYDRO_%1 = salome.IDToObject( str( %2 ) )" ).arg( GetName() ).arg( anEntryVar );
199   aResList << QString( "print \"Geom shape:\", HYDRO_%1" ).arg( GetName() );
200   aResList << QString( "print \"Geom shape name:\", HYDRO_%1.GetName()" ).arg( GetName() );
201
202
203   //DumpSampleMeshing( aResList, aStudyName, aGeomShapeName, aCalculName+"_mesh" );
204
205   aResList << QString( "" );
206   return aResList;
207 }
208
209 void HYDROData_CalculationCase::DumpSampleMeshing( QStringList& theResList,
210                                                    const QString& theStudyName,
211                                                    const QString& theGeomShapeName,
212                                                    const QString& theMeshName ) const
213 {
214   theResList << "";
215   theResList << "# Meshing";
216   theResList << "import SMESH, SALOMEDS";
217   theResList << "from salome.smesh import smeshBuilder";
218   theResList << "from salome.geom import geomBuilder";
219
220   theResList << QString( "smesh = smeshBuilder.New( %1 )" ).arg( theStudyName );
221   theResList << QString( "%1 = smesh.Mesh( %2 )" ).arg( theMeshName ).arg( theGeomShapeName );
222   theResList << QString( "MEFISTO_2D = %1.Triangle( algo=smeshBuilder.MEFISTO )" ).arg( theMeshName );
223   theResList << "Max_Element_Area_1 = MEFISTO_2D.MaxElementArea( 10 )";
224   theResList << QString( "Regular_1D = %1.Segment()" ).arg( theMeshName );
225   theResList << "Max_Size_1 = Regular_1D.MaxSize(10)";
226   theResList << QString( "isDone = %1.Compute()" ).arg( theMeshName );
227
228   theResList << "";
229   theResList << "# Set names of Mesh objects";
230   theResList << "smesh.SetName( MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D' )";
231   theResList << "smesh.SetName( Regular_1D.GetAlgorithm(), 'Regular_1D' )";
232   theResList << "smesh.SetName( Max_Size_1, 'Max Size_1' )";
233   theResList << "smesh.SetName( Max_Element_Area_1, 'Max. Element Area_1' )";
234   theResList << QString( "smesh.SetName( %1.GetMesh(), '%1' )" ).arg( theMeshName );
235
236   theResList << "";
237   theResList << "# Greate SMESH groups";
238   theResList << QString( "geompy = geomBuilder.New( %1 )" ).arg( theStudyName );
239   theResList << QString( "geom_groups = geompy.GetGroups( %1 )" ).arg( theGeomShapeName );
240   theResList << QString( "for group in geom_groups:" );
241   theResList << QString( "    smesh_group = %1.GroupOnGeom(group, group.GetName(), SMESH.EDGE)" )
242                 .arg( theMeshName );
243   theResList << QString( "    smesh.SetName(smesh_group, group.GetName())" );
244   theResList << QString( "    print \"SMESH group '%s': %s\" % (smesh_group.GetName(), smesh_group)" );
245 }
246
247 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
248 {
249   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
250
251   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
252   if ( !aBoundaryPolyline.IsNull() )
253     aResSeq.Append( aBoundaryPolyline );
254
255   HYDROData_SequenceOfObjects aSeqOfGeomObjs = GetGeometryObjects();
256   aResSeq.Append( aSeqOfGeomObjs );
257
258   // Regions
259   HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
260   aResSeq.Append( aSeqOfRegions );
261
262   return aResSeq;
263 }
264
265 void HYDROData_CalculationCase::Update()
266 {
267   HYDROData_Entity::Update();
268   SetWarning();
269
270   // At first we remove previously created objects
271   RemoveRegions();
272   RemoveSplitGroups();
273
274   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
275   if ( aDocument.IsNull() )
276     return;
277
278   // Split to zones
279   HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
280
281   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
282   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
283   if ( !aGeomObjects.IsEmpty() ) {
284     HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
285
286     HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
287       HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
288     if ( !aSplitObjects.isEmpty() ) {
289       HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
290       while( anIter.hasNext() ) {
291         const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
292         if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
293           aZonesList.append( aSplitData );
294         else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
295           anEdgesList.append( aSplitData );
296       }
297     }
298   }
299
300   switch( GetAssignmentMode() )
301   {
302   case MANUAL:
303     CreateRegionsDef( aDocument, aZonesList );    
304     break;
305   case AUTOMATIC:
306     CreateRegionsAuto( aDocument, aZonesList );    
307     break;
308   }
309
310   CreateEdgeGroupsDef( aDocument, anEdgesList );
311 }
312
313 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
314                                                   const HYDROData_SplitToZonesTool::SplitDataList& theZones )
315 {
316   // Create result regions for case, by default one zone for one region
317   QString aRegsPref = CALCULATION_REGIONS_PREF;
318   QString aZonesPref = CALCULATION_ZONES_PREF;
319
320   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
321   while( anIter.hasNext() )
322   {
323     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
324     // Create new region
325     Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
326
327     // Add the zone for region
328     Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
329   }
330 }
331
332 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
333                                                    const HYDROData_SplitToZonesTool::SplitDataList& theZones )
334 {
335   DEBTRACE("HYDROData_CalculationCase::CreateRegionsAuto");
336   QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
337   QMap<QString, QString> aRegionNameToObjNameMap;
338   QString aZonesPref = CALCULATION_ZONES_PREF;
339   HYDROData_PriorityQueue aPr( this, DataTag_CustomRules );
340
341   // 1. First we create a default region for each object included into the calculation case
342   HYDROData_SequenceOfObjects anObjects = GetGeometryObjects();
343   for( int i = anObjects.Lower(), n = anObjects.Upper(); i<=n; i++ )
344   {
345     Handle(HYDROData_Entity) anObj = anObjects.Value( i );
346     if( anObj.IsNull() )
347       continue;
348     QString anObjName = anObj->GetName();
349     QString aRegName = anObjName + "_reg";
350     Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
351     aRegionsMap.insert( anObjName, aRegion );
352     aRegionNameToObjNameMap.insert( aRegName, anObjName );
353   }
354
355   // 2. Now for each zone it is necessary to determine the most priority object
356   //    and assign to zone to corresponding region
357   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
358   while( anIter.hasNext() )
359   {
360     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
361     HYDROData_Zone::MergeType aMergeType;
362     Handle(HYDROData_Entity) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
363     if( aRegObj.IsNull() )
364       continue;
365     Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
366     if( aRegion.IsNull() )
367       continue;
368     Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
369
370     if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
371     {
372       qDebug( "Error in algorithm: unresolved conflicts" );
373     }
374
375     Handle(HYDROData_Entity) aMergeEntity = aRegObj;
376     Handle(HYDROData_Object) aMergeObject = Handle(HYDROData_Object)::DownCast( aMergeEntity );
377     if ( !aMergeObject.IsNull() ) {
378       DEBTRACE("aMergeEntity " << aMergeEntity->GetName().toStdString());
379       aMergeEntity = aMergeObject->GetAltitudeObject();
380     }
381
382     switch( aMergeType )
383     {
384     case HYDROData_Zone::Merge_ZMIN:
385     case HYDROData_Zone::Merge_ZMAX:
386       aRegionZone->SetMergeType( aMergeType );
387       break;
388     case HYDROData_Zone::Merge_Object:
389       aRegionZone->SetMergeType( aMergeType );
390       aRegionZone->RemoveMergeObject();
391       aRegionZone->SetMergeObject( aMergeEntity );
392       break;
393     }
394   }
395
396   QStringList anObjectsWithEmptyRegions;
397   QMap<QString, Handle(HYDROData_Region)>::const_iterator
398     anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
399   for( ; anIt!=aLast; anIt++ )
400   {
401     Handle(HYDROData_Region) aRegion = anIt.value();
402     if( aRegion->GetZones().IsEmpty() )
403     {
404       QString aRegName = aRegion->GetName();
405       QString anObjName = aRegionNameToObjNameMap[aRegName];
406       anObjectsWithEmptyRegions.append( anObjName );
407     }
408   }
409   
410   if( !anObjectsWithEmptyRegions.empty() )
411   {
412     QString aData = anObjectsWithEmptyRegions.join( ", " );
413     SetWarning( WARN_EMPTY_REGIONS, aData );
414   }
415 }
416
417 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
418                                                      const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
419 {
420   QMap<QString,Handle(HYDROData_SplitShapesGroup)> aSplitEdgesGroupsMap;
421
422   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
423   while( anIter.hasNext() )
424   {
425     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
426     // Create new edges group
427     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
428       continue;
429
430     QString anObjName = aSplitData.ObjectNames.first();
431     if ( anObjName.isEmpty() )
432       continue;
433 #ifdef DEB_CALCULATION
434     QString aStr = aSplitData.ObjectNames.join(" "); 
435           cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl; 
436 #endif
437     Handle(HYDROData_SplitShapesGroup) aSplitGroup;
438     if ( !aSplitEdgesGroupsMap.contains( anObjName ) )
439     {
440       aSplitGroup = addNewSplitGroup( CALCULATION_GROUPS_PREF + anObjName );
441       aSplitEdgesGroupsMap.insert( anObjName, aSplitGroup );
442     }
443     else
444     {
445       aSplitGroup = aSplitEdgesGroupsMap[ anObjName ];
446     }
447     if ( aSplitGroup.IsNull() )
448       continue;
449
450     aSplitGroup->AddShape( aSplitData.Shape );
451   }
452 }
453
454 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
455 {
456   if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
457     return false; // Wrong type of object
458
459   if ( HasReference( theObject, DataTag_GeometryObject ) )
460     return false; // Object is already in reference list
461
462   AddReferenceObject( theObject, DataTag_GeometryObject );
463   
464   // Indicate model of the need to update splitting
465   Changed( Geom_2d );
466
467   return true;
468 }
469
470 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
471 {
472   return GetReferenceObjects( DataTag_GeometryObject );
473 }
474
475 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
476 {
477   if ( theObject.IsNull() )
478     return;
479
480   RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
481
482   // Indicate model of the need to update splitting
483   Changed( Geom_2d );
484 }
485
486 void HYDROData_CalculationCase::RemoveGeometryObjects()
487 {
488   ClearReferenceObjects( DataTag_GeometryObject );
489
490   // Indicate model of the need to update splitting
491   Changed( Geom_2d );
492 }
493
494 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
495 {
496   if ( theGroup.IsNull() )
497     return false;
498
499   if ( HasReference( theGroup, DataTag_GeometryGroup ) )
500     return false; // Object is already in reference list
501
502   AddReferenceObject( theGroup, DataTag_GeometryGroup );
503   
504   // Indicate model of the need to update splitting
505   Changed( Geom_Groups );
506
507   return true;
508 }
509
510 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
511 {
512   return GetReferenceObjects( DataTag_GeometryGroup );
513 }
514
515 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
516 {
517   if ( theGroup.IsNull() )
518     return;
519
520   RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
521
522   // Indicate model of the need to update splitting
523   Changed( Geom_Groups );
524 }
525
526 void HYDROData_CalculationCase::RemoveGeometryGroups()
527 {
528   ClearReferenceObjects( DataTag_GeometryGroup );
529
530   // Indicate model of the need to update splitting
531   Changed( Geom_Groups );
532 }
533
534 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
535 {
536   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
537
538   SetReferenceObject( thePolyline, DataTag_Polyline );
539
540   // Indicate model of the need to update zones splitting
541   if( !IsEqual( aPrevPolyline, thePolyline ) )
542     Changed( Geom_2d );
543 }
544
545 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
546 {
547   return Handle(HYDROData_PolylineXY)::DownCast( 
548            GetReferenceObject( DataTag_Polyline ) );
549 }
550
551 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
552 {
553   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
554
555   ClearReferenceObjects( DataTag_Polyline );
556
557   // Indicate model of the need to update zones splitting
558   Changed( Geom_2d );
559 }
560
561 void HYDROData_CalculationCase::SetStricklerTable( const Handle(HYDROData_StricklerTable)& theStricklerTable )
562 {
563   Handle(HYDROData_StricklerTable) aPrevStricklerTable = GetStricklerTable();
564
565   SetReferenceObject( theStricklerTable, DataTag_StricklerTable );
566
567   // Indicate model of the need to update land covers partition
568   if( !IsEqual( aPrevStricklerTable, theStricklerTable ) )
569     Changed( Geom_No );
570 }
571
572 Handle(HYDROData_StricklerTable) HYDROData_CalculationCase::GetStricklerTable() const
573 {
574   return Handle(HYDROData_StricklerTable)::DownCast( 
575            GetReferenceObject( DataTag_StricklerTable ) );
576 }
577
578 void HYDROData_CalculationCase::RemoveStricklerTable()
579 {
580   Handle(HYDROData_StricklerTable) aPrevStricklerTable = GetStricklerTable();
581
582   ClearReferenceObjects( DataTag_StricklerTable );
583
584   // Indicate model of the need to update land covers partition
585   Changed( Geom_No );
586 }
587
588 Handle(HYDROData_LandCoverMap) HYDROData_CalculationCase::GetLandCoverMap() const
589 {
590   Handle(HYDROData_LandCoverMap) aMap = Handle(HYDROData_LandCoverMap)::DownCast(
591     GetReferenceObject( DataTag_LandCoverMap ) );
592   return aMap;
593 }
594
595 void HYDROData_CalculationCase::SetLandCoverMap( const Handle(HYDROData_LandCoverMap)& theMap )
596 {
597   SetReferenceObject( theMap, DataTag_LandCoverMap );
598 }
599
600 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
601 {
602   Changed( Geom_No );
603   Changed( Geom_No );
604   Changed( Geom_No );
605   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
606   Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
607   if ( aNewRegion.IsNull() )
608     return aNewRegion;
609
610   aNewRegion->AddZone( theZone );
611
612   return aNewRegion;
613 }
614
615 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
616 {
617   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
618
619   if ( theRegion.IsNull() )
620     return false;
621   
622   HYDROData_CalculationCase::DataTag aDataTag = DataTag_Region;
623
624   if ( HasReference( theRegion, aDataTag ) )
625     return false; // Object is already in reference list
626
627   // Move the region from other calculation
628   Handle(HYDROData_CalculationCase) aFatherCalc = 
629     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
630   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
631   {
632     Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
633     theRegion->CopyTo( aNewRegion, true );
634
635     aFatherCalc->RemoveRegion( theRegion );
636
637     theRegion->SetLabel( aNewRegion->Label() );
638   }
639   else
640   {
641     AddReferenceObject( theRegion, aDataTag );
642   }
643
644   return true;
645 }
646
647 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
648 {
649   return GetReferenceObjects( DataTag_Region );
650 }
651
652 void HYDROData_CalculationCase::UpdateRegionsOrder()
653 {
654   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
655   if ( aDocument.IsNull() )
656     return;
657
658   HYDROData_SequenceOfObjects aRegions = GetRegions();
659   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
660   for ( ; anIter.More(); anIter.Next() )
661   {
662     Handle(HYDROData_Region) aRegion =
663       Handle(HYDROData_Region)::DownCast( anIter.Value() );
664     if ( aRegion.IsNull() )
665       continue;
666
667     aRegion->SetName( "" );
668   }
669
670   QString aRegsPref = CALCULATION_REGIONS_PREF;
671
672   anIter.Init( aRegions );
673   for ( ; anIter.More(); anIter.Next() )
674   {
675     Handle(HYDROData_Region) aRegion =
676       Handle(HYDROData_Region)::DownCast( anIter.Value() );
677     if ( aRegion.IsNull() )
678       continue;
679
680     QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
681     aRegion->SetName( aRegionName );
682   }
683 }
684
685 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
686 {
687   if ( theRegion.IsNull() )
688     return;
689
690   HYDROData_CalculationCase::DataTag aDataTag = DataTag_Region;
691   RemoveReferenceObject( theRegion->Label(), aDataTag );
692
693   // Remove region from data model
694   Handle(HYDROData_CalculationCase) aFatherCalc = 
695     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
696   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
697     theRegion->Remove();
698 }
699
700 void HYDROData_CalculationCase::RemoveRegions()
701 {
702   myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
703 }
704
705 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplitGroups() const
706 {
707   return GetReferenceObjects( DataTag_SplitGroups );
708 }
709
710 void HYDROData_CalculationCase::RemoveSplitGroups()
711 {
712   myLab.FindChild( DataTag_SplitGroups ).ForgetAllAttributes();
713 }
714
715 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
716 {
717   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
718   return GetAltitudeForPoint( thePoint, aZone );
719 }
720
721 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                    thePoint,
722                                                        const Handle(HYDROData_Region)& theRegion,
723                                                        int theMethod) const
724 {
725   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
726
727   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
728   if ( !aZone.IsNull() )
729   {
730     //DEBTRACE("GetAltitudeForPoint Region " << theRegion->GetName().toStdString() << " Zone " << aZone->GetName().toStdString());
731     Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
732     if ( IsEqual( aRefRegion, theRegion ) )
733       aResAltitude = GetAltitudeForPoint( thePoint, aZone, theMethod );
734     else
735       {
736         DEBTRACE("GetAltitudeForPoint Region " << aRefRegion->GetName().toStdString() << " Zone " << aZone->GetName().toStdString() << " ---------------------------");
737         aResAltitude = GetAltitudeForPoint( thePoint, aZone, theMethod );
738       }
739   }
740   else
741     {
742       DEBTRACE(" --- GetAltitudeForPoint No Zone ---");
743     }
744
745   return aResAltitude;
746 }
747
748 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
749                                                        const Handle(HYDROData_Zone)& theZone,
750                                                        int theMethod) const
751 {
752   //DEBTRACE("GetAltitudeForPoint Zone " << theZone->GetName().toStdString());
753   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
754   if ( theZone.IsNull() )
755   {
756         DEBTRACE("Zone nulle");
757     return aResAltitude;
758   }
759
760   HYDROData_Zone::MergeType aZoneMergeType = theZone->GetMergeType();
761   //DEBTRACE("aZoneMergeType " << aZoneMergeType);
762   if ( !theZone->IsMergingNeed() )
763   {
764     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
765     //DEBTRACE("---");
766   }
767   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
768   {
769         DEBTRACE("GetAltitudeForPoint Zone " << theZone->GetName().toStdString() << " Merge_UNKNOWN");
770     return aResAltitude;
771   }
772
773   HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
774   if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
775   {
776     Handle(HYDROData_IAltitudeObject) aMergeAltitude = 
777       Handle(HYDROData_IAltitudeObject)::DownCast( theZone->GetMergeObject() );
778     if ( !aMergeAltitude.IsNull() )
779     {
780       if ( aZoneInterpolator != NULL )
781       {
782         DEBTRACE("aZoneInterpolator != NULL");
783         aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
784         aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
785       }
786       else
787       {
788         DEBTRACE("aZoneInterpolator == NULL");
789         aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
790       }
791     }
792   }
793   else
794   {
795         //DEBTRACE("aZoneMergeType != HYDROData_Zone::Merge_Object");
796     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetObjects();
797     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
798     for ( ; anIter.More(); anIter.Next() )
799     {
800       Handle(HYDROData_Object) aZoneObj =
801         Handle(HYDROData_Object)::DownCast( anIter.Value() );
802       if ( aZoneObj.IsNull() )
803         continue;
804
805       Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
806       if ( anObjAltitude.IsNull() )
807         continue;
808
809       double aPointAltitude = 0.0;
810       if ( aZoneInterpolator != NULL )
811       {
812         DEBTRACE("aZoneInterpolator != NULL");
813         aZoneInterpolator->SetAltitudeObject( anObjAltitude );
814         aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
815       }
816       else
817       {
818         //DEBTRACE("aZoneInterpolator == NULL");
819         aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint, theMethod );
820       }
821
822       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
823         continue;
824
825       if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
826       {
827         aResAltitude = aPointAltitude;
828         break;
829       }
830       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
831       {
832         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
833              aResAltitude > aPointAltitude )
834         {
835           aResAltitude = aPointAltitude;
836         }
837       }
838       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
839       {
840         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
841              aResAltitude < aPointAltitude )
842         {
843           aResAltitude = aPointAltitude;
844         }
845       }
846     }
847   }
848
849   return aResAltitude;
850 }
851
852 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
853   const NCollection_Sequence<gp_XY>& thePoints,
854   const Handle(HYDROData_Region)&    theRegion,
855   int theMethod) const
856 {
857   DEBTRACE("HYDROData_CalculationCase::GetAltitudesForPoints " << theRegion->GetName().toStdString());
858   NCollection_Sequence<double> aResSeq;
859
860   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
861   {
862     const gp_XY& thePnt = thePoints.Value( i );
863     
864     double anAltitude = GetAltitudeForPoint( thePnt, theRegion, theMethod );
865     aResSeq.Append( anAltitude );
866   }
867
868   return aResSeq;
869 }
870
871 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
872   const NCollection_Sequence<gp_XY>& thePoints,
873   const Handle(HYDROData_Zone)&      theZone,
874   int theMethod) const
875 {
876   NCollection_Sequence<double> aResSeq;
877
878   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
879   {
880     const gp_XY& thePnt = thePoints.Value( i );
881     
882     double anAltitude = GetAltitudeForPoint( thePnt, theZone, theMethod );
883     aResSeq.Append( anAltitude );
884   }
885
886   return aResSeq;
887 }
888
889 double HYDROData_CalculationCase::GetStricklerCoefficientForPoint( const gp_XY& thePoint ) const
890 {
891   Handle( HYDROData_LandCoverMap ) aMap = GetLandCoverMap();
892   Handle( HYDROData_StricklerTable ) aTable = GetStricklerTable();
893   if( aMap.IsNull() )
894     return 0.0;
895
896   QString aType;
897   aMap->FindByPoint( thePoint, aType );
898   double aCoeff = aTable->Get( aType, 0.0 );
899   return aCoeff;
900 }
901
902 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
903 {
904   Handle(HYDROData_Region) aResRegion;
905
906   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
907   if ( !aZone.IsNull() )
908     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
909
910   return aResRegion;
911 }
912
913 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
914 {
915   Handle(HYDROData_Zone) aResZone;
916
917   HYDROData_SequenceOfObjects aRegions = GetRegions();
918
919   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
920   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
921   {
922     Handle(HYDROData_Region) aRegion =
923       Handle(HYDROData_Region)::DownCast( anIter.Value() );
924     if ( aRegion.IsNull() )
925       continue;
926
927     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
928     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
929     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
930     {
931       Handle(HYDROData_Zone) aRegZone =
932         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
933       if ( aRegZone.IsNull() )
934         continue;
935
936       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
937       if ( aPointRelation != POINT_OUT )
938         aResZone = aRegZone; // We found the desired zone
939     }
940   }
941
942   return aResZone;
943 }
944
945 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
946   const gp_XY&                  thePoint,
947   const Handle(HYDROData_Zone)& theZone ) const
948 {
949   PointClassification aRes = POINT_OUT;
950   if ( theZone.IsNull() )
951     return aRes;
952
953   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
954   if ( aZoneFace.IsNull() )
955     return aRes;
956 #ifdef DEB_CLASS2D      
957           TopoDS_Compound aCmp;
958       BRep_Builder aBB;
959       aBB.MakeCompound(aCmp);
960           aBB.Add(aCmp, aZoneFace);
961           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
962           BRepBuilderAPI_MakeVertex aMk(aPnt);
963           aBB.Add(aCmp, aMk.Vertex());
964           BRepTools::Write(aCmp, "FCL2d.brep");
965 #endif  
966   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
967   if (State == TopAbs_OUT)
968     aRes =  POINT_OUT;
969   else if(State == TopAbs_IN)
970     aRes =  POINT_IN;
971   else if(State == TopAbs_ON)
972     aRes =  POINT_ON;
973   return aRes;
974 }
975
976 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
977                                                                   const QString& thePrefixOrName,
978                                                                   bool isPrefix )
979 {
980   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
981   int aTag = aNewLab.Tag();
982
983   Handle(HYDROData_Region) aNewRegion =
984     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
985   AddRegion( aNewRegion );
986
987   QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
988   aNewRegion->SetName( aRegionName, true );
989
990   return aNewRegion;
991 }
992
993 Handle(HYDROData_SplitShapesGroup) HYDROData_CalculationCase::addNewSplitGroup( const QString& theName )
994 {
995   TDF_Label aNewLab = myLab.FindChild( DataTag_SplitGroups ).NewChild();
996
997   Handle(HYDROData_SplitShapesGroup) aNewGroup =
998     Handle(HYDROData_SplitShapesGroup)::DownCast( 
999       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLIT_GROUP ) );
1000   AddReferenceObject( aNewGroup, DataTag_SplitGroups );
1001
1002   aNewGroup->SetName( theName );
1003
1004   return aNewGroup;
1005 }
1006
1007 QString HYDROData_CalculationCase::Export( int theStudyId ) const
1008 {
1009 #ifdef LIGHT_MODE
1010   return "";
1011 #else
1012   GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
1013   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
1014   
1015   QString aGeomObjEntry, anErrorMsg;
1016   bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry, anErrorMsg );
1017   return isOK ? aGeomObjEntry : QString();
1018 #endif
1019 }
1020
1021 #ifndef LIGHT_MODE
1022 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
1023                                         SALOMEDS::Study_ptr theStudy,
1024                                         QString& theGeomObjEntry,
1025                                         QString& theErrorMsg ) const
1026 {
1027   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
1028
1029   // Get groups definitions
1030   HYDROData_SequenceOfObjects aSplitGroups = GetSplitGroups();
1031
1032   HYDROData_SequenceOfObjects::Iterator anIter( aSplitGroups );
1033   for ( ; anIter.More(); anIter.Next() )
1034   {
1035     // Get shapes group
1036     Handle(HYDROData_ShapesGroup) aGroup =
1037       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
1038     if ( aGroup.IsNull() )
1039       continue;
1040
1041     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
1042
1043     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
1044     aGroup->GetShapes( aGroupDef.Shapes );
1045
1046     aSeqOfGroupsDefs.Append( aGroupDef );
1047   }
1048   
1049   // Get faces
1050   bool isAllNotSubmersible = true;
1051   TopTools_ListOfShape aFaces;
1052   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
1053   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
1054   for ( ; aRegionIter.More(); aRegionIter.Next() )
1055   {
1056     Handle(HYDROData_Region) aRegion =
1057       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
1058     if( aRegion.IsNull() || !aRegion->IsSubmersible() )
1059       continue;
1060     
1061     if ( isAllNotSubmersible )
1062       isAllNotSubmersible = false;
1063
1064     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
1065     aFaces.Append( aRegionShape );
1066   }
1067
1068   bool aRes = false;
1069
1070   if ( aCaseRegions.IsEmpty() ) {
1071     theErrorMsg = QString("the list of regions is empty.");
1072   } else if ( isAllNotSubmersible ) {
1073     theErrorMsg = QString("there are no submersible regions.");
1074   } else {
1075     aRes = Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );;
1076   }
1077
1078   if( aRes && !GetLandCoverMap().IsNull() && !GetStricklerTable().IsNull() )
1079   {
1080     QString aTelemacFileName = GetName() + ".telemac";
1081     aRes = GetLandCoverMap()->ExportTelemac( aTelemacFileName, 1E-2, GetStricklerTable() );
1082   }
1083   return aRes;
1084 }
1085
1086 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
1087                                         SALOMEDS::Study_ptr                           theStudy,
1088                                         const TopTools_ListOfShape&                   theFaces,
1089                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
1090                                         QString& theGeomObjEntry ) const
1091 {
1092   // Sew faces
1093   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
1094   aSewing.SetNonManifoldMode( Standard_False );
1095 #ifdef DEB_CALCULATION
1096   TCollection_AsciiString aNam("Sh_");
1097   int i=1;
1098 #endif
1099   TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
1100   for ( ; aFaceIter.More(); aFaceIter.Next() )
1101   {
1102     TopoDS_Shape aShape = aFaceIter.Value();
1103     if ( aShape.IsNull() )
1104       continue;
1105
1106     if ( aShape.ShapeType() == TopAbs_FACE )
1107     {
1108       aSewing.Add( aShape );
1109 #ifdef DEB_CALCULATION
1110       TCollection_AsciiString aName = aNam + ++i + ".brep";
1111       BRepTools::Write(aShape ,aName.ToCString());
1112 #endif
1113     }
1114     else
1115     {
1116 #ifdef DEB_CALCULATION
1117       int j = 1;
1118 #endif
1119       TopExp_Explorer anExp( aShape, TopAbs_FACE );
1120       for (; anExp.More(); anExp.Next() ) {
1121         aSewing.Add( anExp.Current() );
1122 #ifdef DEB_CALCULATION
1123
1124         TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
1125         BRepTools::Write(anExp.Current() ,aName.ToCString());
1126 #endif
1127       }
1128     }
1129   } // faces iterator
1130   
1131   aSewing.Perform();
1132   TopoDS_Shape aSewedShape = aSewing.SewedShape();
1133
1134   // If the sewed shape is empty - return false
1135   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1136     return false;
1137
1138 #ifdef DEB_CALCULATION
1139   BRepTools::Write(aSewedShape ,"Sew.brep");
1140 #endif
1141   // Publish the sewed shape
1142   QString aName = EXPORT_NAME;
1143   GEOM::GEOM_Object_ptr aMainShape = 
1144     HYDROData_GeomTool::publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1145
1146   if ( aMainShape->_is_nil() )  
1147     return false;
1148
1149   if ( theGroupsDefs.IsEmpty() )
1150     return true;
1151
1152   // Create groups
1153   TopTools_IndexedMapOfShape aMapOfSubShapes;
1154   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1155
1156   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1157
1158   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1159   {
1160     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1161
1162     NCollection_Sequence<int> aGroupIndexes;
1163     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1164     {
1165       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1166 #ifdef DEB_CALCULATION
1167       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1168 #endif
1169       
1170       TopoDS_Shape aModifiedShape = aShape;
1171       if ( aSewing.IsModified( aShape ) )
1172         aModifiedShape = aSewing.Modified( aShape );
1173       else if ( aSewing.IsModifiedSubShape( aShape ) )
1174         aModifiedShape = aSewing.ModifiedSubShape( aShape );
1175
1176 #ifdef DEB_CALCULATION
1177       const TopLoc_Location& aL1 = aShape.Location();
1178       const TopLoc_Location& aL2 = aModifiedShape.Location();
1179       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1180 #endif
1181
1182       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1183       if ( anIndex > 0 ) {
1184         aGroupIndexes.Append( anIndex );
1185       } else {
1186 #ifdef DEB_CALCULATION    
1187         TCollection_AsciiString aNam("Lost_");
1188         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1189         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1190         {
1191            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1192            if ( aModifiedShape.IsPartner( aS ) )
1193            {
1194              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1195              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1196              BRepTools::Write(aS ,aName.ToCString());
1197             break;
1198            }
1199          }
1200         }
1201 #endif
1202       }
1203     }
1204     if ( !aGroupIndexes.IsEmpty() )
1205       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1206   }
1207  
1208   if ( !aGroupsData.IsEmpty() )
1209   {
1210     GEOM::GEOM_IGroupOperations_var aGroupOp = 
1211       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
1212
1213     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1214     for ( ; aMapIt.More(); aMapIt.Next() )
1215     {
1216       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
1217       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1218
1219       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1220       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1221         continue;
1222
1223       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1224       aGeomIndexes->length( aGroupIndexes.Length() );
1225
1226       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1227         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1228
1229       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1230       if ( aGroupOp->IsDone() )
1231       {
1232         SALOMEDS::SObject_var aGroupSO = 
1233           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1234       }
1235     }
1236   }
1237
1238   return true;
1239 }
1240 #endif
1241
1242 void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag theDataTag,
1243                                             const bool theIsSetToUpdate )
1244 {
1245   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1246   HYDROData_PriorityQueue::ClearRules( aRulesLab );
1247
1248   // Indicate model of the need to update splitting
1249   if ( theIsSetToUpdate ) {
1250     Changed( Geom_2d );
1251   }
1252 }
1253
1254 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Entity)&    theObject1,
1255                                          HYDROData_PriorityType             thePriority,
1256                                          const Handle(HYDROData_Entity)&    theObject2,
1257                                          HYDROData_Zone::MergeType          theMergeType,
1258                                          HYDROData_CalculationCase::DataTag theDataTag )
1259 {
1260   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1261   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1262
1263   // Indicate model of the need to update splitting
1264   Changed( Geom_2d );
1265 }
1266
1267 QString HYDROData_CalculationCase::DumpRules() const
1268 {
1269   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1270   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1271 }
1272
1273 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1274 {
1275   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1276   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1277
1278   // Indicate model of the need to update splitting
1279   Changed( Geom_2d );
1280 }
1281
1282 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1283 {
1284   Handle(TDataStd_Integer) aModeAttr;
1285   bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1286   if( isOK )
1287     return ( AssignmentMode ) aModeAttr->Get();
1288   else
1289     return MANUAL;
1290 }
1291
1292 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1293                                                    QStringList& theScript ) const
1294 {
1295   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1296   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1297 }
1298
1299 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1300 {
1301   return myLastWarning;
1302 }
1303
1304 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1305 {
1306   myLastWarning.Type = theType;
1307   myLastWarning.Data = theData;
1308 }
1309
1310 void HYDROData_CalculationCase::UpdateRegionsNames( const HYDROData_SequenceOfObjects& theRegions,
1311                                                     const QString& theOldCaseName,
1312                                                     const QString& theName )
1313 {
1314   HYDROData_SequenceOfObjects::Iterator anIter( theRegions );
1315   for ( ; anIter.More(); anIter.Next() )
1316   {
1317     Handle(HYDROData_Region) aRegion =
1318       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1319     if ( aRegion.IsNull() )
1320       continue;
1321
1322     HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegion );
1323
1324     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
1325     HYDROData_SequenceOfObjects::Iterator anIter( aZones );
1326     for ( ; anIter.More(); anIter.Next() )
1327     {
1328       Handle(HYDROData_Zone) aRegZone =
1329         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1330       if ( aRegZone.IsNull() )
1331         continue;
1332
1333       HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegZone );
1334     }
1335   }
1336 }
1337
1338 void HYDROData_CalculationCase::DumpRegionsToPython( QStringList& theResList,
1339                                                      const QString& thePyScriptPath,
1340                                                      MapOfTreatedObjects& theTreatedObjects,
1341                                                      const HYDROData_SequenceOfObjects& theRegions ) const
1342 {
1343   HYDROData_SequenceOfObjects::Iterator anIter;
1344   anIter.Init(theRegions);
1345   for (int ireg = 1; anIter.More(); anIter.Next(), ireg++)
1346     {
1347       Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast(anIter.Value());
1348       if (aRegion.IsNull())
1349         continue;
1350       QString defRegName = this->GetName();
1351       QString regSuffix = QString("_Reg_%1").arg(ireg);
1352       defRegName += regSuffix;
1353       theTreatedObjects.insert(aRegion->GetName(), aRegion);
1354       QStringList aRegDump = aRegion->DumpToPython(thePyScriptPath, theTreatedObjects, defRegName);
1355       theResList << aRegDump;
1356     }
1357   for (anIter.Init(theRegions); anIter.More(); anIter.Next())
1358     {
1359       Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast(anIter.Value());
1360       if (aRegion.IsNull())
1361         continue;
1362       QStringList aRegDump;
1363       aRegion->SetNameInDumpPython(aRegDump);
1364       theResList << aRegDump;
1365     }
1366 }
1367
1368 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1369                                          Handle(HYDROData_Entity)&           theObject1,
1370                                          HYDROData_PriorityType&             thePriority,
1371                                          Handle(HYDROData_Entity)&           theObject2,
1372                                          HYDROData_Zone::MergeType&          theMergeType,
1373                                          HYDROData_CalculationCase::DataTag& theDataTag) const
1374 {
1375   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1376   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1377     theObject1, thePriority, theObject2, theMergeType );
1378 }