Salome HOME
Import/Export new corrections
[modules/hydro.git] / src / HYDROData / HYDROData_Zone.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_Zone.h"
20
21 #include "HYDROData_ArtificialObject.h"
22 #include "HYDROData_IAltitudeObject.h"
23 #include "HYDROData_Document.h"
24 #include "HYDROData_NaturalObject.h"
25 #include "HYDROData_LandCover.h"
26
27 #include <TNaming_Builder.hxx>
28 #include <TNaming_NamedShape.hxx>
29
30 #include <TopoDS_Shape.hxx>
31
32 #include <QStringList>
33
34 #include <TDataStd_Integer.hxx>
35
36 IMPLEMENT_STANDARD_HANDLE(HYDROData_Zone, HYDROData_Entity)
37 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Zone, HYDROData_Entity)
38
39
40 HYDROData_Zone::HYDROData_Zone()
41 : HYDROData_Entity()
42 {
43   myInterpolator = NULL;
44 }
45
46 HYDROData_Zone::~HYDROData_Zone()
47 {
48 }
49
50 bool HYDROData_Zone::CanBeUpdated() const
51 {
52   return false;
53 }
54
55 bool HYDROData_Zone::IsHas2dPrs() const
56 {
57   return true;
58 }
59
60 bool HYDROData_Zone::CanRemove()
61 {
62   return false;
63 }
64
65 HYDROData_SequenceOfObjects HYDROData_Zone::GetAllReferenceObjects() const
66 {
67   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
68
69   HYDROData_SequenceOfObjects aSeqOfObjects = GetObjects();
70   aResSeq.Append( aSeqOfObjects );
71
72   return aResSeq;
73 }
74
75 void HYDROData_Zone::SetShape( const TopoDS_Shape& theShape )
76 {
77   TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
78   aBuilder.Generated( theShape );
79 }
80
81 TopoDS_Shape HYDROData_Zone::GetShape() const
82 {
83   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
84   if ( !aLabel.IsNull() )
85   {
86     Handle(TNaming_NamedShape) aNamedShape;
87     if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
88       return aNamedShape->Get();
89   }
90
91   return TopoDS_Shape();
92 }
93
94 bool HYDROData_Zone::IsMergingNeed() const
95 {
96   // Zones based on geometry objects (compare altitudes)
97   Handle(HYDROData_IAltitudeObject) aRefAltitude;
98
99   HYDROData_SequenceOfObjects anObjects = GetObjects();
100   HYDROData_SequenceOfObjects::Iterator anObjsIter( anObjects );
101   for ( ; anObjsIter.More(); anObjsIter.Next() )
102   {
103     Handle(HYDROData_Object) aRefGeomObj =
104       Handle(HYDROData_Object)::DownCast( anObjsIter.Value() );
105     if ( aRefGeomObj.IsNull() )
106       continue;
107
108     Handle(HYDROData_IAltitudeObject) anObjAltitude = aRefGeomObj->GetAltitudeObject();
109     if ( anObjAltitude.IsNull() )
110       continue;
111
112     if ( aRefAltitude.IsNull() )
113     {
114       aRefAltitude = anObjAltitude;
115       continue;
116     }
117
118     if ( !IsEqual( aRefAltitude, anObjAltitude ) )
119       return true;
120   }
121
122   // Zones based on land cover objects (compare Strickler types)
123   QString aRefStricklerType;
124
125   anObjsIter.Init( anObjects );
126   for ( ; anObjsIter.More(); anObjsIter.Next() )
127   {
128     Handle(HYDROData_LandCover) aRefLandCoverObj =
129       Handle(HYDROData_LandCover)::DownCast( anObjsIter.Value() );
130     if ( aRefLandCoverObj.IsNull() )
131       continue;
132
133     QString aStricklerType = aRefLandCoverObj->GetStricklerType();
134     
135     if ( aRefStricklerType.isNull() )
136     {
137       aRefStricklerType = aStricklerType;
138       continue;
139     }
140
141     if ( aRefStricklerType != aStricklerType )
142       return true;
143   }
144
145
146   return false;
147 }
148
149 void HYDROData_Zone::SetInterpolator( HYDROData_IInterpolator* theInter )
150 {
151   myInterpolator = theInter;
152 }
153
154 HYDROData_IInterpolator* HYDROData_Zone::GetInterpolator() const
155 {
156   return myInterpolator;
157 }
158
159 void HYDROData_Zone::SetMergeType( const MergeType& theType )
160 {
161   TDataStd_Integer::Set( myLab.FindChild( DataTag_MergeType ), (int)theType );
162 }
163
164 HYDROData_Zone::MergeType HYDROData_Zone::GetMergeType() const
165 {
166   MergeType aMergeType = Merge_UNKNOWN;
167   
168   TDF_Label aLabel = myLab.FindChild( DataTag_MergeType, false );
169   if ( !aLabel.IsNull() )
170   {
171     Handle(TDataStd_Integer) anInt;
172     if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anInt ) )
173       aMergeType = (MergeType)anInt->Get();
174   }
175
176   return aMergeType;
177 }
178
179 void HYDROData_Zone::SetMergeObject( const Handle(HYDROData_Entity)& theObject )
180 {
181   SetReferenceObject( theObject, DataTag_MergeObject );
182 }
183
184 Handle(HYDROData_Entity) HYDROData_Zone::GetMergeObject() const
185 {
186   return Handle(HYDROData_Entity)::DownCast( 
187            GetReferenceObject( DataTag_MergeObject ) );
188 }
189
190 void HYDROData_Zone::RemoveMergeObject()
191 {
192   ClearReferenceObjects( DataTag_MergeObject );
193 }
194
195 bool HYDROData_Zone::AddObject( const Handle(HYDROData_Entity)& theObject )
196 {
197   if ( theObject.IsNull() )
198     return false;
199   
200   if ( !theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) &&
201        !theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) ) && 
202        !theObject->IsKind( STANDARD_TYPE(HYDROData_LandCover) ) )
203     return false; // Wrong type of object
204
205   if ( HasReference( theObject, DataTag_Object ) )
206     return false; // Object is already in reference list
207
208   AddReferenceObject( theObject, DataTag_Object );
209
210   return true;
211 }
212
213 HYDROData_SequenceOfObjects HYDROData_Zone::GetObjects() const
214 {
215   return GetReferenceObjects( DataTag_Object );
216 }
217
218 void HYDROData_Zone::RemoveObjects()
219 {
220   ClearReferenceObjects( DataTag_Object );
221 }
222
223 bool HYDROData_Zone::IsSubmersible() const
224 {
225   HYDROData_SequenceOfObjects anObjects = GetObjects();
226   HYDROData_SequenceOfObjects::Iterator anObjsIter( anObjects );
227   for ( ; anObjsIter.More(); anObjsIter.Next() )
228   {
229     Handle(HYDROData_Object) aRefGeomObj =
230       Handle(HYDROData_Object)::DownCast( anObjsIter.Value() );
231     if ( aRefGeomObj.IsNull() )
232       continue;
233
234     if( !aRefGeomObj->IsSubmersible() )
235       return false; //if one of geometry objects is not submersible the zone is considered as not submersible
236   }
237   return true;
238 }
239