Salome HOME
16f25745525c773a714e01df16afa2bc2794b6e7
[modules/hydro.git] / src / HYDROData / HYDROData_CalculationCase.cxx
1
2 #include "HYDROData_CalculationCase.h"
3
4 #include "HYDROData_ArtificialObject.h"
5 #include "HYDROData_Document.h"
6 #include "HYDROData_Iterator.h"
7 #include "HYDROData_NaturalObject.h"
8 #include "HYDROData_Polyline.h"
9 #include "HYDROData_SplitToZonesTool.h"
10 #include "HYDROData_Region.h"
11 #include "HYDROData_Tool.h"
12 #include "HYDROData_Zone.h"
13
14 #define PYTHON_CALCULATION_ID "KIND_CALCULATION"
15
16 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
17 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
18
19 HYDROData_CalculationCase::HYDROData_CalculationCase()
20 : HYDROData_Entity()
21 {
22 }
23
24 HYDROData_CalculationCase::~HYDROData_CalculationCase()
25 {
26 }
27
28 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
29 {
30   QStringList aResList;
31
32   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
33   if ( aDocument.IsNull() )
34     return aResList;
35                              
36   QString aDocName = aDocument->GetDocPyName();
37   QString aCalculName = GetName();
38
39   aResList << QString( "%1 = %2.CreateObject( %3 );" )
40               .arg( aCalculName ).arg( aDocName ).arg( PYTHON_CALCULATION_ID );
41   aResList << QString( "%1.SetName( \"%2\" );" )
42               .arg( aCalculName ).arg( aCalculName );
43   aResList << QString( "" );
44
45   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
46   HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
47   for ( ; anIter.More(); anIter.Next() )
48   {
49     Handle(HYDROData_Object) aRefGeomObj =
50       Handle(HYDROData_Object)::DownCast( anIter.Value() );
51     if ( !aRefGeomObj.IsNull() )
52       setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
53   }
54   aResList << QString( "" );
55
56   aResList << QString( "%1.SplitGeometryObjects();" ).arg( aCalculName );
57   aResList << QString( "" );
58
59   // Now we restore the regions and zones order
60   HYDROData_SequenceOfObjects aRegions = GetRegions();
61   anIter.Init( aRegions );
62   for ( ; anIter.More(); anIter.Next() )
63   {
64     Handle(HYDROData_Region) aRegion =
65       Handle(HYDROData_Region)::DownCast( anIter.Value() );
66     if ( aRegion.IsNull() )
67       continue;
68
69     QString aRegionName = aRegion->GetName();
70     // TODO
71   }
72
73   return aResList;
74 }
75
76 void HYDROData_CalculationCase::SplitGeometryObjects()
77 {
78   // At first we remove previously created regions
79   RemoveRegions();
80
81   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
82   if ( aDocument.IsNull() )
83     return;
84
85   Handle(HYDROData_Polyline) aBoundaryPolyline = GetBoundaryPolyline();
86   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
87   if ( aGeomObjects.IsEmpty() )
88     return;
89
90   HYDROData_SplitToZonesTool::SplitDataList aSplitedZones =
91     HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects, aBoundaryPolyline );
92   if ( aSplitedZones.isEmpty() )
93     return;
94
95   // Create result regions for case, by default one zone for one region
96   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitedZones );
97   while( anIter.hasNext() )
98   {
99     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
100
101     // Create new region
102     Handle(HYDROData_Region) aRegion = addNewRegion();
103
104     QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, "Region" );
105     aRegion->SetName( aRegionName );
106
107     // Add the zone for region
108     Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone();
109
110     QString aZoneName = HYDROData_Tool::GenerateObjectName( aDocument, "Zone" );
111     aRegionZone->SetName( aZoneName );
112
113     aRegionZone->SetShape( aSplitData.Face() );
114
115     // Add the reference object for zone
116     for ( int i = 0, n = aSplitData.ObjectNames.length(); i < n; ++i )
117     {
118       const QString& anObjName = aSplitData.ObjectNames.at( i );
119       
120       Handle(HYDROData_Object) aRefObject = Handle(HYDROData_Object)::DownCast(
121         HYDROData_Tool::FindObjectByName( aDocument, anObjName ) );
122       if ( aRefObject.IsNull() )
123         continue;
124
125       aRegionZone->AddGeometryObject( aRefObject );
126     }
127   }
128
129   // The splitted data is up to date
130   SetToUpdate( false );
131 }
132
133 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
134 {
135   if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
136     return false; // Wrong type of object
137
138   if ( HasReference( theObject, DataTag_GeometryObject ) )
139     return false; // Object is already in reference list
140
141   AddReferenceObject( theObject, DataTag_GeometryObject );
142   
143   // Indicate model of the need to update zones splitting
144   SetToUpdate( true );
145
146   return true;
147 }
148
149 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
150 {
151   return GetReferenceObjects( DataTag_GeometryObject );
152 }
153
154 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
155 {
156   if ( theObject.IsNull() )
157     return;
158
159   RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
160
161   // Indicate model of the need to update zones splitting
162   SetToUpdate( true );
163 }
164
165 void HYDROData_CalculationCase::RemoveGeometryObjects()
166 {
167   ClearReferenceObjects( DataTag_GeometryObject );
168
169   // Indicate model of the need to update zones splitting
170   SetToUpdate( true );
171 }
172
173 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_Polyline)& thePolyline )
174 {
175   SetReferenceObject( thePolyline, DataTag_Polyline );
176 }
177
178 Handle(HYDROData_Polyline) HYDROData_CalculationCase::GetBoundaryPolyline() const
179 {
180   return Handle(HYDROData_Polyline)::DownCast( 
181            GetReferenceObject( DataTag_Polyline ) );
182 }
183
184 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
185 {
186   ClearReferenceObjects( DataTag_Polyline );
187 }
188
189 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
190 {
191   Handle(HYDROData_Region) aNewRegion = addNewRegion();
192   if ( aNewRegion.IsNull() )
193     return aNewRegion;
194
195   // Generate new name for new region
196   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
197   if ( !aDocument.IsNull() )
198   {
199     QString aNewRegionName = HYDROData_Tool::GenerateObjectName( aDocument, "Region" );
200     aNewRegion->SetName( aNewRegionName );
201   }
202
203   aNewRegion->AddZone( theZone );
204
205   return aNewRegion;
206 }
207
208 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
209 {
210   if ( theRegion.IsNull() )
211     return false;
212   
213   if ( HasReference( theRegion, DataTag_Region ) )
214     return false; // Object is already in reference list
215
216   // Move the region from other calculation
217   Handle(HYDROData_CalculationCase) aFatherCalc = 
218     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
219   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
220   {
221     Handle(HYDROData_Region) aNewRegion = addNewRegion();
222     theRegion->CopyTo( aNewRegion );
223
224     aFatherCalc->RemoveRegion( theRegion );
225
226     theRegion->SetLabel( aNewRegion->Label() );
227   }
228   else
229   {
230     AddReferenceObject( theRegion, DataTag_Region );
231   }
232
233   return true;
234 }
235
236 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
237 {
238   return GetReferenceObjects( DataTag_Region );
239 }
240
241 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
242 {
243   if ( theRegion.IsNull() )
244     return;
245
246   RemoveReferenceObject( theRegion->Label(), DataTag_Region );
247
248   // Remove region from data model
249   Handle(HYDROData_CalculationCase) aFatherCalc = 
250     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
251   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
252     theRegion->Remove();
253 }
254
255 void HYDROData_CalculationCase::RemoveRegions()
256 {
257   ClearReferenceObjects( DataTag_Region );
258   myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes( true );
259 }
260
261 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion()
262 {
263   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
264
265   Handle(HYDROData_Region) aNewRegion =
266     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
267   AddRegion( aNewRegion );
268
269   return aNewRegion;
270 }
271