Salome HOME
Small correction for writing of data to file.
[modules/hydro.git] / src / HYDROData / HYDROData_Polyline.cxx
1 #include <HYDROData_Polyline.h>
2 #include <HYDROData_Iterator.h>
3
4 #include <TDataStd_Name.hxx>
5 #include <TDataStd_Integer.hxx>
6 #include <TDataStd_ByteArray.hxx>
7 #include <TDataStd_BooleanArray.hxx>
8 #include <TDataStd_IntegerArray.hxx>
9 #include <TDataStd_RealArray.hxx>
10 #include <TDataStd_ExtStringArray.hxx>
11 #include <TDataStd_UAttribute.hxx>
12 #include <TDF_ListIteratorOfLabelList.hxx>
13
14 #include <QPainterPath>
15
16 // tage of the child of my label that contains information about the operator
17 static const Standard_GUID GUID_MUST_BE_UPDATED("6647e1f7-1971-4c5a-86c7-11ff0291452d");
18
19 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline, HYDROData_Object)
20 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline, HYDROData_Object)
21
22 HYDROData_Polyline::HYDROData_Polyline()
23 {
24 }
25
26 HYDROData_Polyline::~HYDROData_Polyline()
27 {
28 }
29
30 /**
31  * Return polyline dimension
32  * \return polyline dimension. 2 or 3 is valid. 0 is invalid.
33  */
34 int HYDROData_Polyline::getDimension() const
35 {
36     Handle(TDataStd_Integer) aDim;
37     if(!myLab.FindAttribute(TDataStd_Integer::GetID(), aDim))
38       return 0;
39     return aDim->Get();
40 }
41
42 /**
43  * Set polyline dimension. Should be 2 or 3.
44  * \param theDimension the polyline dimension
45  */
46 void HYDROData_Polyline::setDimension( int theDimension )
47 {
48     removeAll();
49     int aDim=0;
50     if( theDimension == 2 || theDimension == 3){
51         aDim = theDimension;
52     }
53     TDataStd_Integer::Set(myLab, aDim);
54 }
55
56 /**
57  * Replace current polyline data by new sections list
58  * \param theSections the sections list
59  */
60 void HYDROData_Polyline::setPolylineData( const QList<PolylineSection>& theSections )
61 {
62 //Keep dimension
63   int aDim = getDimension();
64   if( aDim == 0 )
65       return;
66   removeAll();
67   setDimension(aDim);
68
69   if( theSections.size() == 0 )
70     return;
71   int aSectionsSize = theSections.size();
72
73   int aPointsCnt = 0;
74
75   TDF_Label aNameLab = myLab.FindChild(DataTag_SectionsName);
76   Handle(TDataStd_ExtStringArray) aSectsNameArray;
77   aSectsNameArray = TDataStd_ExtStringArray::Set(aNameLab, 0, aSectionsSize-1, false );
78
79   TDF_Label aSizeLab = myLab.FindChild(DataTag_SectionsSize);
80   Handle(TDataStd_IntegerArray) aSizeArray;
81   aSizeArray = TDataStd_IntegerArray::Set(aSizeLab, 0, aSectionsSize-1, false );
82
83   TDF_Label aClosedLab = myLab.FindChild(DataTag_SectionsClosed);
84   Handle(TDataStd_BooleanArray) aClosedArray;
85   aClosedArray = TDataStd_BooleanArray::Set(aClosedLab, 0, aSectionsSize-1 );
86
87   TDF_Label aTypeLab = myLab.FindChild(DataTag_SectionsType);
88   Handle(TDataStd_ByteArray) aTypeArray;
89   aTypeArray = TDataStd_ByteArray::Set(aTypeLab, 0, aSectionsSize-1, false );
90
91 //Extract sections parameters and count points
92   for( int i = 0 ; i < theSections.size() ; i++ ){
93     int aSectSize = theSections[i].myCoords.size();
94     aSectsNameArray->SetValue( i, theSections[i].mySectionName );
95     aSizeArray->SetValue( i, aSectSize );
96     aClosedArray->SetValue( i, theSections[i].myIsClosed );
97     char aType = (char)theSections[i].myType;
98     aTypeArray->SetValue( i, aType );
99     aPointsCnt += aSectSize;
100   }
101 //Don't create a points array
102   if( aPointsCnt == 0 )
103     return;
104 //Save coordinates
105   Handle(TDataStd_RealArray) anArray;
106   anArray = TDataStd_RealArray::Set( myLab, 0, aPointsCnt*aDim - 1 );
107   int aPtr = 0;
108   for( int i = 0 ; i < theSections.size() ; i++ ){
109     for( int j = 0 ; j < theSections[i].myCoords.size() ; j++ ){
110       anArray->SetValue(aPtr, theSections[i].myCoords[j]);
111       aPtr++;
112     }
113   }
114 }
115
116 /**
117  * Return polyline data
118  * \return polyline section list
119  */
120 QList<PolylineSection> HYDROData_Polyline::getPolylineData()
121 {
122   int aSectCnt;
123   QList<PolylineSection> aRes;
124 //Get sections size array handle
125   TDF_Label aLab = myLab.FindChild( DataTag_SectionsSize );
126   Handle(TDataStd_IntegerArray) aSizeArray;
127   if (!aLab.FindAttribute(TDataStd_IntegerArray::GetID(), aSizeArray))
128     return aRes; // return empty if no array
129   aSectCnt = aSizeArray->Length();
130   if( aSectCnt == 0 )
131     return aRes;
132 //Get section type array handle
133   aLab = myLab.FindChild( DataTag_SectionsType );
134   Handle(TDataStd_ByteArray) aTypeArray;
135   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aTypeArray))
136     return aRes;
137   int aLen = aTypeArray->Length();
138   if( aLen != aSectCnt )
139     return aRes;
140 //Get section closed array handle
141   aLab = myLab.FindChild( DataTag_SectionsClosed );
142   Handle(TDataStd_BooleanArray) aClosedArray;
143   if (!aLab.FindAttribute(TDataStd_BooleanArray::GetID(), aClosedArray))
144     return aRes;
145   aLen = aClosedArray->Length();
146   if( aLen != aSectCnt )
147     return aRes;
148 //Get sections names
149   TDF_Label aNameLab = myLab.FindChild(DataTag_SectionsName);
150   Handle(TDataStd_ExtStringArray) aSectNamesArray;
151   if(!aNameLab.FindAttribute(TDataStd_ExtStringArray::GetID(), aSectNamesArray))
152     return aRes;
153   aLen = aSectNamesArray->Length();
154   if( aLen != aSectCnt )
155     return aRes;
156 //Get coordinates array
157   Handle(TDataStd_RealArray) aCoordsArray;
158   myLab.FindAttribute(TDataStd_RealArray::GetID(), aCoordsArray);
159
160   int aCoordPtr = 0;
161   for( int i = 0 ; i < aSectCnt ; i++ ){
162     PolylineSection aSect;
163     aSect.myIsClosed = aClosedArray->Value(i);
164     aSect.myType = (PolylineSection::SectionType)aTypeArray->Value(i);
165     aSect.mySectionName = aSectNamesArray->Value(i);
166     int aSectSize = aSizeArray->Value(i);
167     for( int j = 0 ; j < aSectSize ; j++ ){
168       double aCoord = aCoordsArray->Value(aCoordPtr);
169       aSect.myCoords << aCoord;
170       aCoordPtr++;
171     }
172     aRes << aSect;
173   }
174   return aRes;
175 }
176
177 /**
178  * Remove all polyline attributes except dimension.
179  */
180 void HYDROData_Polyline::removeAll()
181 {
182 //Remove only section data
183   TDF_Label aLab = myLab.FindChild( DataTag_SectionsSize );
184   aLab.ForgetAllAttributes();
185
186   aLab = myLab.FindChild( DataTag_SectionsType );
187   aLab.ForgetAllAttributes();
188
189   aLab = myLab.FindChild( DataTag_SectionsClosed );
190   aLab.ForgetAllAttributes();
191
192   myLab.ForgetAttribute(TDataStd_RealArray::GetID());
193   return;
194 }
195
196 /**
197  * Return polyline painter path. Current implementation
198  * is ignored section type.
199  * \return polyline painter path.
200  */
201 QPainterPath HYDROData_Polyline::painterPath()
202 {
203   QPainterPath aPath;
204   int aDim = getDimension();
205   if( ( aDim != 2 ) || ( aDim != 3) )
206       return aPath;
207   QList<PolylineSection> aSects = getPolylineData();
208   for( int i = 0 ; aSects.size() ; i++ ){
209     int aPntCnt = aSects[i].myCoords.size()/aDim;
210     if( aPntCnt ){
211       aPath.moveTo(aSects[i].myCoords[0], aSects[i].myCoords[1] );
212     }
213     for( int j = 1 ; j < aPntCnt ; j++ ){
214       int anIndx = j*aDim;
215       aPath.lineTo(aSects[i].myCoords[anIndx], aSects[i].myCoords[anIndx+1]);
216     }
217     if( aSects[i].myIsClosed ){
218       aPath.closeSubpath();
219     }
220   }
221   return aPath;
222 }