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