Salome HOME
Update the names of Regions and Zones if case name changed (Bug #110).
[modules/hydro.git] / src / HYDROData / HYDROData_IPolyline.cxx
1
2 #include "HYDROData_IPolyline.h"
3
4 #include "HYDROData_Document.h"
5
6 #include <TDataStd_BooleanList.hxx>
7 #include <TDataStd_ExtStringList.hxx>
8 #include <TDataStd_Integer.hxx>
9 #include <TDataStd_IntegerList.hxx>
10 #include <TDataStd_RealList.hxx>
11
12 IMPLEMENT_STANDARD_HANDLE(HYDROData_IPolyline, HYDROData_Entity)
13 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_IPolyline, HYDROData_Entity)
14
15 HYDROData_IPolyline::HYDROData_IPolyline()
16 : HYDROData_Entity(),
17   myIsOperation( false )
18 {
19 }
20
21 HYDROData_IPolyline::~HYDROData_IPolyline()
22 {
23 }
24
25 int HYDROData_IPolyline::getNbUndo() const
26 {
27   return getNbOperations();
28 }
29
30 bool HYDROData_IPolyline::undo()
31 {
32   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
33   if ( aDocument.IsNull() )
34     return false;
35
36   if ( getNbUndo() < 1 )
37     return false;
38
39   aDocument->Undo();
40
41   int aNbUndoedOperations = getNbUndoedOperations();
42   setNbUndoedOperations( aNbUndoedOperations + 1 ); 
43
44   return true;
45 }
46
47 int HYDROData_IPolyline::getNbRedo() const
48 {
49   return getNbUndoedOperations();
50 }
51
52 bool HYDROData_IPolyline::redo()
53 {
54   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
55   if ( aDocument.IsNull() )
56     return false;
57
58   if ( getNbRedo() < 1 )
59     return false;
60
61   aDocument->Redo();
62
63   int aNbUndoedOperations = getNbUndoedOperations();
64   setNbUndoedOperations( aNbUndoedOperations - 1 ); 
65
66   return true;
67 }
68
69 CurveCreator::Dimension HYDROData_IPolyline::getDimension() const
70 {
71   return CurveCreator::Dim2d;
72 }
73
74 CurveCreator::Coordinates HYDROData_IPolyline::getPoint( const int theISection, 
75                                                          const int theIPnt ) const
76 {
77   CurveCreator::Coordinates aResPoint;
78   if ( theISection < 0 || theIPnt < 0 )
79     return aResPoint;
80
81   CurveCreator::Coordinates aSectPoints = getPoints( theISection );
82   if ( theIPnt >= aSectPoints.size() )
83     return aResPoint;
84
85   CurveCreator::Coordinates::reverse_iterator anIt = aSectPoints.rbegin();
86
87   aResPoint.push_back( *anIt++ );
88   aResPoint.push_back( *anIt );
89
90   return aResPoint;
91 }
92
93
94 int HYDROData_IPolyline::getNbPoints( const int theSectionIndex ) const
95 {
96   return getPoints( theSectionIndex ).size();
97 }
98
99 void HYDROData_IPolyline::startOperation()
100 {
101   if ( myIsOperation )
102     return;
103
104   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
105   if ( aDocument.IsNull() )
106     return;
107   
108   aDocument->StartOperation();
109   
110   myIsOperation = true;
111 }
112
113 void HYDROData_IPolyline::commitOperation()
114 {
115   if ( !myIsOperation )
116     return;
117
118   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
119   if ( aDocument.IsNull() )
120     return;
121
122   int aNbOperations = getNbOperations();
123   setNbOperations( aNbOperations + 1 ); 
124
125   aDocument->CommitOperation();
126
127   myIsOperation = false;
128 }
129
130 void HYDROData_IPolyline::abortOperation()
131 {
132   if ( !myIsOperation )
133     return;
134
135   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
136   if ( aDocument.IsNull() )
137     return;
138
139   aDocument->AbortOperation();
140
141   myIsOperation = false;
142 }
143
144 void HYDROData_IPolyline::setNbOperations( const int theNb )
145 {
146   TDF_Label anOpLabel = myLab.FindChild( DataTag_Operations ).FindChild( OperationsTag_Commited );
147   TDataStd_Integer::Set( anOpLabel, theNb );
148 }
149
150 int HYDROData_IPolyline::getNbOperations() const
151 {
152   TDF_Label aLabel = myLab.FindChild( DataTag_Operations, false );
153   if ( aLabel.IsNull() )
154     return 0;
155
156   TDF_Label anOpLabel = aLabel.FindChild( OperationsTag_Commited, false );
157   if ( anOpLabel.IsNull() )
158     return 0;
159
160   Handle(TDataStd_Integer) anOpCount;
161   if ( !anOpLabel.FindAttribute( TDataStd_Integer::GetID(), anOpCount ) )
162     return 0;
163
164   return anOpCount->Get();
165 }
166
167 void HYDROData_IPolyline::setNbUndoedOperations( const int theNb )
168 {
169   TDF_Label anUndoedLabel = myLab.FindChild( DataTag_Operations ).FindChild( OperationsTag_Undoed );
170   TDataStd_Integer::Set( anUndoedLabel, theNb );
171 }
172
173 int HYDROData_IPolyline::getNbUndoedOperations() const
174 {
175   TDF_Label aLabel = myLab.FindChild( DataTag_Operations, false );
176   if ( aLabel.IsNull() )
177     return 0;
178
179   TDF_Label anUndoedLabel = aLabel.FindChild( OperationsTag_Undoed, false );
180   if ( anUndoedLabel.IsNull() )
181     return 0;
182
183   Handle(TDataStd_Integer) anUndoedCount;
184   if ( !anUndoedLabel.FindAttribute( TDataStd_Integer::GetID(), anUndoedCount ) )
185     return 0;
186
187   return anUndoedCount->Get();
188 }
189
190 void HYDROData_IPolyline::getSectionsLists( Handle(TDataStd_ExtStringList)& theNamesList,
191                                             Handle(TDataStd_IntegerList)&   theTypesList,
192                                             Handle(TDataStd_BooleanList)&   theClosuresList,
193                                             const bool                      theIsCreate ) const
194 {
195   TDF_Label aSectsLabel = myLab.FindChild( DataTag_Sections, theIsCreate );
196   if ( aSectsLabel.IsNull() )
197     return;
198
199   Handle(TDataStd_ExtStringList) aNamesList;
200   if ( !aSectsLabel.FindAttribute( TDataStd_ExtStringList::GetID(), aNamesList ) && theIsCreate )
201     aNamesList = TDataStd_ExtStringList::Set( aSectsLabel );
202
203   Handle(TDataStd_IntegerList) aTypesList;
204   if ( !aSectsLabel.FindAttribute( TDataStd_IntegerList::GetID(), aTypesList ) && theIsCreate )
205     aTypesList = TDataStd_IntegerList::Set( aSectsLabel );
206
207   Handle(TDataStd_BooleanList) aClosuresList;
208   if ( !aSectsLabel.FindAttribute( TDataStd_BooleanList::GetID(), aClosuresList ) && theIsCreate )
209     aClosuresList = TDataStd_BooleanList::Set( aSectsLabel );
210 }
211
212 void HYDROData_IPolyline::removeSectionsLists()
213 {
214   TDF_Label aSectsLabel = myLab.FindChild( DataTag_Sections, false );
215   if ( !aSectsLabel.IsNull() )
216     aSectsLabel.ForgetAllAttributes();
217 }
218
219 void HYDROData_IPolyline::getPointsLists( const int                  theSectionIndex,
220                                           Handle(TDataStd_RealList)& theListX,
221                                           Handle(TDataStd_RealList)& theListY,
222                                           const bool                 theIsCreate ) const
223 {
224   theListX.Nullify();
225   theListY.Nullify();
226
227   TDF_Label aLabel = myLab.FindChild( DataTag_Points, theIsCreate );
228   if ( aLabel.IsNull() )
229     return;
230
231   TDF_Label aSectLabel = aLabel.FindChild( theSectionIndex, theIsCreate );
232   if ( aSectLabel.IsNull() )
233     return;
234
235   TDF_Label aLabelX = aSectLabel.FindChild( 0, theIsCreate );
236   if ( !aLabelX.IsNull() )
237   {
238     if ( !aLabelX.FindAttribute( TDataStd_RealList::GetID(), theListX ) && theIsCreate )
239       theListX = TDataStd_RealList::Set( aLabelX );
240   }
241
242   TDF_Label aLabelY = aSectLabel.FindChild( 1, theIsCreate );
243   if ( !aLabelY.IsNull() )
244   {
245     if ( !aLabelY.FindAttribute( TDataStd_RealList::GetID(), theListY ) && theIsCreate )
246       theListY = TDataStd_RealList::Set( aLabelY );
247   }
248 }
249
250 void HYDROData_IPolyline::removePointsLists( const int theSectionIndex )
251 {
252   TDF_Label aLabel = myLab.FindChild( DataTag_Points, false );
253   if ( aLabel.IsNull() )
254     return;
255
256   if ( theSectionIndex == -1 )
257   {
258     aLabel.ForgetAllAttributes();
259   }
260   else
261   {
262     TDF_Label aSectLabel = aLabel.FindChild( theSectionIndex, false );
263     if ( !aSectLabel.IsNull() )
264       aSectLabel.ForgetAllAttributes();
265   }
266 }