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