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