1 #include <HYDROData_Polyline.h>
2 #include <HYDROData_Iterator.h>
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>
14 #include <QPainterPath>
15 #include <QStringList>
17 // tage of the child of my label that contains information about the operator
18 static const Standard_GUID GUID_MUST_BE_UPDATED("6647e1f7-1971-4c5a-86c7-11ff0291452d");
20 #define PYTHON_POLYLINE_ID "2"
22 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline, HYDROData_Object)
23 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline, HYDROData_Object)
25 HYDROData_Polyline::HYDROData_Polyline()
29 HYDROData_Polyline::~HYDROData_Polyline()
34 * Dump object to Python script representation.
36 QStringList HYDROData_Polyline::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
40 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
41 if ( aDocument.IsNull() )
44 QString aDocName = aDocument->GetDocPyName();
45 QString aPolylineName = GetName();
47 aResList << QString( "%1 = %2.CreateObject( %3 );" )
48 .arg( aPolylineName ).arg( aDocName ).arg( PYTHON_POLYLINE_ID );
49 aResList << QString( "%1.SetName( \"%1\" );" ).arg( aPolylineName );
51 // Set polilyne dimension
53 aResList << QString( "" );
55 int aDim = getDimension();
56 aResList << QString( "%1.setDimension( %2 );" )
57 .arg( aPolylineName ).arg( aDim );
61 PolylineData aPolylineData = getPolylineData();
62 if ( !aPolylineData.isEmpty() )
64 QString aPolylineDataName = "polyline_data";
66 aResList << QString( "" );
67 aResList << QString( "%1 = [];" ).arg( aPolylineDataName );
69 PolylineData::const_iterator aDataIt = aPolylineData.constBegin();
70 for ( ; aDataIt != aPolylineData.constEnd(); ++aDataIt )
72 const PolylineSection& aSection = *aDataIt;
74 QString aPolylineSectName = "polyline_section";
76 aResList << QString( "" );
77 aResList << QString( "%1 = PolylineSection();" ).arg( aPolylineSectName );
80 foreach( const double& aCoordVal, aSection.myCoords )
81 aCoordsStr += QString::number( aCoordVal ) + ", ";
82 aCoordsStr.remove( aCoordsStr.length() - 2, 2 );
84 aResList << QString( "" );
86 aResList << QString( "%1.mySectionName = \"%2\";" )
87 .arg( aPolylineSectName )
88 .arg( TCollection_AsciiString( aSection.mySectionName ).ToCString() );
89 aResList << QString( "%1.myType = %2;" )
90 .arg( aPolylineSectName ).arg( aSection.myType );
91 aResList << QString( "%1.myIsClosed = %2;" )
92 .arg( aPolylineSectName ).arg( aSection.myIsClosed );
93 aResList << QString( "%1.myCoords = [ %2 ];" )
94 .arg( aPolylineSectName ).arg( aCoordsStr );
96 aResList << QString( "%1.append( %2 );" )
97 .arg( aPolylineDataName ).arg( aPolylineSectName );
100 aResList << QString( "%1.setPolylineData( %2 );" )
101 .arg( aPolylineName ).arg( aPolylineDataName );
108 * Return polyline dimension
109 * \return polyline dimension. 2 or 3 is valid. 0 is invalid.
111 int HYDROData_Polyline::getDimension() const
113 Handle(TDataStd_Integer) aDim;
114 if(!myLab.FindAttribute(TDataStd_Integer::GetID(), aDim))
120 * Set polyline dimension. Should be 2 or 3.
121 * \param theDimension the polyline dimension
123 void HYDROData_Polyline::setDimension( int theDimension )
127 if( theDimension == 2 || theDimension == 3){
130 TDataStd_Integer::Set(myLab, aDim);
134 * Replace current polyline data by new sections list
135 * \param theSections the sections list
137 void HYDROData_Polyline::setPolylineData( const PolylineData& theSections )
140 int aDim = getDimension();
146 if( theSections.size() == 0 )
148 int aSectionsSize = theSections.size();
152 TDF_Label aNameLab = myLab.FindChild(DataTag_SectionsName);
153 Handle(TDataStd_ExtStringArray) aSectsNameArray;
154 aSectsNameArray = TDataStd_ExtStringArray::Set(aNameLab, 0, aSectionsSize-1, false );
156 TDF_Label aSizeLab = myLab.FindChild(DataTag_SectionsSize);
157 Handle(TDataStd_IntegerArray) aSizeArray;
158 aSizeArray = TDataStd_IntegerArray::Set(aSizeLab, 0, aSectionsSize-1, false );
160 TDF_Label aClosedLab = myLab.FindChild(DataTag_SectionsClosed);
161 Handle(TDataStd_BooleanArray) aClosedArray;
162 aClosedArray = TDataStd_BooleanArray::Set(aClosedLab, 0, aSectionsSize-1 );
164 TDF_Label aTypeLab = myLab.FindChild(DataTag_SectionsType);
165 Handle(TDataStd_ByteArray) aTypeArray;
166 aTypeArray = TDataStd_ByteArray::Set(aTypeLab, 0, aSectionsSize-1, false );
168 //Extract sections parameters and count points
169 for( int i = 0 ; i < theSections.size() ; i++ ){
170 int aSectSize = theSections[i].myCoords.size();
171 aSectsNameArray->SetValue( i, theSections[i].mySectionName );
172 aSizeArray->SetValue( i, aSectSize );
173 aClosedArray->SetValue( i, theSections[i].myIsClosed );
174 char aType = (char)theSections[i].myType;
175 aTypeArray->SetValue( i, aType );
176 aPointsCnt += aSectSize;
178 //Don't create a points array
179 if( aPointsCnt == 0 )
182 Handle(TDataStd_RealArray) anArray;
183 anArray = TDataStd_RealArray::Set( myLab, 0, aPointsCnt*aDim - 1 );
185 for( int i = 0 ; i < theSections.size() ; i++ ){
186 for( int j = 0 ; j < theSections[i].myCoords.size() ; j++ ){
187 anArray->SetValue(aPtr, theSections[i].myCoords[j]);
194 * Return polyline data
195 * \return polyline section list
197 HYDROData_Polyline::PolylineData HYDROData_Polyline::getPolylineData() const
201 //Get sections size array handle
202 TDF_Label aLab = myLab.FindChild( DataTag_SectionsSize );
203 Handle(TDataStd_IntegerArray) aSizeArray;
204 if (!aLab.FindAttribute(TDataStd_IntegerArray::GetID(), aSizeArray))
205 return aRes; // return empty if no array
206 aSectCnt = aSizeArray->Length();
209 //Get section type array handle
210 aLab = myLab.FindChild( DataTag_SectionsType );
211 Handle(TDataStd_ByteArray) aTypeArray;
212 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aTypeArray))
214 int aLen = aTypeArray->Length();
215 if( aLen != aSectCnt )
217 //Get section closed array handle
218 aLab = myLab.FindChild( DataTag_SectionsClosed );
219 Handle(TDataStd_BooleanArray) aClosedArray;
220 if (!aLab.FindAttribute(TDataStd_BooleanArray::GetID(), aClosedArray))
222 aLen = aClosedArray->Length();
223 if( aLen != aSectCnt )
226 TDF_Label aNameLab = myLab.FindChild(DataTag_SectionsName);
227 Handle(TDataStd_ExtStringArray) aSectNamesArray;
228 if(!aNameLab.FindAttribute(TDataStd_ExtStringArray::GetID(), aSectNamesArray))
230 aLen = aSectNamesArray->Length();
231 if( aLen != aSectCnt )
233 //Get coordinates array
234 Handle(TDataStd_RealArray) aCoordsArray;
235 myLab.FindAttribute(TDataStd_RealArray::GetID(), aCoordsArray);
238 for( int i = 0 ; i < aSectCnt ; i++ ){
239 PolylineSection aSect;
240 aSect.myIsClosed = aClosedArray->Value(i);
241 aSect.myType = (PolylineSection::SectionType)aTypeArray->Value(i);
242 aSect.mySectionName = aSectNamesArray->Value(i);
243 int aSectSize = aSizeArray->Value(i);
244 for( int j = 0 ; j < aSectSize ; j++ ){
245 double aCoord = aCoordsArray->Value(aCoordPtr);
246 aSect.myCoords << aCoord;
255 * Remove all polyline attributes except dimension.
257 void HYDROData_Polyline::removeAll()
259 //Remove only section data
260 TDF_Label aLab = myLab.FindChild( DataTag_SectionsSize );
261 aLab.ForgetAllAttributes();
263 aLab = myLab.FindChild( DataTag_SectionsType );
264 aLab.ForgetAllAttributes();
266 aLab = myLab.FindChild( DataTag_SectionsClosed );
267 aLab.ForgetAllAttributes();
269 myLab.ForgetAttribute(TDataStd_RealArray::GetID());
274 * Return polyline painter path. Current implementation
275 * is ignored section type.
276 * \return polyline painter path.
278 QPainterPath HYDROData_Polyline::painterPath()
281 int aDim = getDimension();
282 if( ( aDim != 2 ) && ( aDim != 3) )
284 PolylineData aSects = getPolylineData();
285 for( int i = 0 ; i < aSects.size() ; i++ ){
286 int aPntCnt = aSects[i].myCoords.size()/aDim;
288 aPath.moveTo(aSects[i].myCoords[0], aSects[i].myCoords[1] );
290 for( int j = 1 ; j < aPntCnt ; j++ ){
292 aPath.lineTo(aSects[i].myCoords[anIndx], aSects[i].myCoords[anIndx+1]);
294 if( aSects[i].myIsClosed ){
295 aPath.closeSubpath();