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