Salome HOME
Drawing of zones in OCC view improved.
[modules/hydro.git] / src / HYDROData / HYDROData_Polyline.cxx
index 294f0e988e0e20b673786b8eea732d591b53825a..fb723700621300e61e47ae44f4acbaadfd280279 100755 (executable)
@@ -1,6 +1,8 @@
 #include <HYDROData_Polyline.h>
 #include <HYDROData_Iterator.h>
 
+#include <ImageComposer_MetaTypes.h>
+
 #include <TDataStd_Name.hxx>
 #include <TDataStd_Integer.hxx>
 #include <TDataStd_ByteArray.hxx>
 #include <TDataStd_UAttribute.hxx>
 #include <TDF_ListIteratorOfLabelList.hxx>
 
-#include <QPainterPath>
+#include <QStringList>
 
 // tage of the child of my label that contains information about the operator
 static const Standard_GUID GUID_MUST_BE_UPDATED("6647e1f7-1971-4c5a-86c7-11ff0291452d");
 
-#define TAG_POINTS          1
-#define TAG_SECTIONS_NAME   2
-#define TAG_SECTIONS_CLOSED 3
-#define TAG_SECTIONS_SIZE   4
-#define TAG_SECTIONS_TYPE   5
+#define PYTHON_POLYLINE_ID "KIND_POLYLINE"
 
 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline, HYDROData_Object)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline, HYDROData_Object)
@@ -33,6 +31,90 @@ HYDROData_Polyline::~HYDROData_Polyline()
 {
 }
 
+/**
+ * Dump object to Python script representation.
+ */
+QStringList HYDROData_Polyline::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
+{
+  QStringList aResList;
+
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
+  if ( aDocument.IsNull() )
+    return aResList;
+                             
+  QString aDocName = aDocument->GetDocPyName();
+  QString aPolylineName = GetName();
+
+  aResList << QString( "%1 = %2.CreateObject( %3 );" )
+              .arg( aPolylineName ).arg( aDocName ).arg( PYTHON_POLYLINE_ID );
+  aResList << QString( "%1.SetName( \"%1\" );" ).arg( aPolylineName );
+
+  // Set polilyne dimension
+
+  aResList << QString( "" );
+
+  int aDim = getDimension();
+  aResList << QString( "%1.setDimension( %2 );" )
+              .arg( aPolylineName ).arg( aDim );
+
+  // Set polilyne data
+
+  PolylineData aPolylineData = getPolylineData();
+  if ( !aPolylineData.isEmpty() )
+  {
+    QString aPolylineDataName = "polyline_data";
+
+    aResList << QString( "" );
+    aResList << QString( "%1 = [];" ).arg( aPolylineDataName );
+
+    PolylineData::const_iterator aDataIt = aPolylineData.constBegin();
+    for ( ; aDataIt != aPolylineData.constEnd(); ++aDataIt )
+    {
+      const PolylineSection& aSection = *aDataIt;
+
+      QString aPolylineSectName = "polyline_section";
+
+      aResList << QString( "" );
+      aResList << QString( "%1 = PolylineSection();" ).arg( aPolylineSectName );
+
+      QString aCoordsStr;
+      foreach( const double& aCoordVal, aSection.myCoords )
+        aCoordsStr += QString::number( aCoordVal ) + ", ";
+      aCoordsStr.remove( aCoordsStr.length() - 2, 2 );
+
+      aResList << QString( "" );
+
+      aResList << QString( "%1.mySectionName = \"%2\";" )
+                  .arg( aPolylineSectName )
+                  .arg( TCollection_AsciiString( aSection.mySectionName ).ToCString() );
+      aResList << QString( "%1.myType = %2;" )
+                  .arg( aPolylineSectName ).arg( aSection.myType );
+      aResList << QString( "%1.myIsClosed = %2;" )
+                  .arg( aPolylineSectName ).arg( aSection.myIsClosed );
+      aResList << QString( "%1.myCoords = [ %2 ];" )
+                  .arg( aPolylineSectName ).arg( aCoordsStr );
+
+      aResList << QString( "%1.append( %2 );" )
+                  .arg( aPolylineDataName ).arg( aPolylineSectName );
+    }
+
+    aResList << QString( "%1.setPolylineData( %2 );" )
+                  .arg( aPolylineName ).arg( aPolylineDataName );
+  }
+
+  return aResList;
+}
+
+QVariant HYDROData_Polyline::GetDataVariant()
+{
+  QPainterPath aPath = painterPath();
+
+  QVariant aVarData;
+  aVarData.setValue<QPainterPath>( aPath );
+  
+  return aVarData;
+}
+
 /**
  * Return polyline dimension
  * \return polyline dimension. 2 or 3 is valid. 0 is invalid.
@@ -63,7 +145,7 @@ void HYDROData_Polyline::setDimension( int theDimension )
  * Replace current polyline data by new sections list
  * \param theSections the sections list
  */
-void HYDROData_Polyline::setPolylineData( const QList<PolylineSection>& theSections )
+void HYDROData_Polyline::setPolylineData( const PolylineData& theSections )
 {
 //Keep dimension
   int aDim = getDimension();
@@ -78,19 +160,19 @@ void HYDROData_Polyline::setPolylineData( const QList<PolylineSection>& theSecti
 
   int aPointsCnt = 0;
 
-  TDF_Label aNameLab = myLab.FindChild(TAG_SECTIONS_NAME);
+  TDF_Label aNameLab = myLab.FindChild(DataTag_SectionsName);
   Handle(TDataStd_ExtStringArray) aSectsNameArray;
   aSectsNameArray = TDataStd_ExtStringArray::Set(aNameLab, 0, aSectionsSize-1, false );
 
-  TDF_Label aSizeLab = myLab.FindChild(TAG_SECTIONS_SIZE);
+  TDF_Label aSizeLab = myLab.FindChild(DataTag_SectionsSize);
   Handle(TDataStd_IntegerArray) aSizeArray;
   aSizeArray = TDataStd_IntegerArray::Set(aSizeLab, 0, aSectionsSize-1, false );
 
-  TDF_Label aClosedLab = myLab.FindChild(TAG_SECTIONS_CLOSED);
+  TDF_Label aClosedLab = myLab.FindChild(DataTag_SectionsClosed);
   Handle(TDataStd_BooleanArray) aClosedArray;
   aClosedArray = TDataStd_BooleanArray::Set(aClosedLab, 0, aSectionsSize-1 );
 
-  TDF_Label aTypeLab = myLab.FindChild(TAG_SECTIONS_TYPE);
+  TDF_Label aTypeLab = myLab.FindChild(DataTag_SectionsType);
   Handle(TDataStd_ByteArray) aTypeArray;
   aTypeArray = TDataStd_ByteArray::Set(aTypeLab, 0, aSectionsSize-1, false );
 
@@ -123,12 +205,12 @@ void HYDROData_Polyline::setPolylineData( const QList<PolylineSection>& theSecti
  * Return polyline data
  * \return polyline section list
  */
-QList<PolylineSection> HYDROData_Polyline::getPolylineData()
+HYDROData_Polyline::PolylineData HYDROData_Polyline::getPolylineData() const
 {
   int aSectCnt;
-  QList<PolylineSection> aRes;
+  PolylineData aRes;
 //Get sections size array handle
-  TDF_Label aLab = myLab.FindChild( TAG_SECTIONS_SIZE );
+  TDF_Label aLab = myLab.FindChild( DataTag_SectionsSize );
   Handle(TDataStd_IntegerArray) aSizeArray;
   if (!aLab.FindAttribute(TDataStd_IntegerArray::GetID(), aSizeArray))
     return aRes; // return empty if no array
@@ -136,7 +218,7 @@ QList<PolylineSection> HYDROData_Polyline::getPolylineData()
   if( aSectCnt == 0 )
     return aRes;
 //Get section type array handle
-  aLab = myLab.FindChild( TAG_SECTIONS_TYPE );
+  aLab = myLab.FindChild( DataTag_SectionsType );
   Handle(TDataStd_ByteArray) aTypeArray;
   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aTypeArray))
     return aRes;
@@ -144,7 +226,7 @@ QList<PolylineSection> HYDROData_Polyline::getPolylineData()
   if( aLen != aSectCnt )
     return aRes;
 //Get section closed array handle
-  aLab = myLab.FindChild( TAG_SECTIONS_CLOSED );
+  aLab = myLab.FindChild( DataTag_SectionsClosed );
   Handle(TDataStd_BooleanArray) aClosedArray;
   if (!aLab.FindAttribute(TDataStd_BooleanArray::GetID(), aClosedArray))
     return aRes;
@@ -152,7 +234,7 @@ QList<PolylineSection> HYDROData_Polyline::getPolylineData()
   if( aLen != aSectCnt )
     return aRes;
 //Get sections names
-  TDF_Label aNameLab = myLab.FindChild(TAG_SECTIONS_NAME);
+  TDF_Label aNameLab = myLab.FindChild(DataTag_SectionsName);
   Handle(TDataStd_ExtStringArray) aSectNamesArray;
   if(!aNameLab.FindAttribute(TDataStd_ExtStringArray::GetID(), aSectNamesArray))
     return aRes;
@@ -180,19 +262,41 @@ QList<PolylineSection> HYDROData_Polyline::getPolylineData()
   return aRes;
 }
 
+/**
+ * Returns true if polyline is closed
+ */
+bool HYDROData_Polyline::isClosed() const
+{
+  int aDim = getDimension();
+  PolylineData aPolylineData = getPolylineData();
+
+  if ( aDim == 0 || aPolylineData.isEmpty() )
+    return false;
+
+  PolylineData::const_iterator anIt = aPolylineData.constBegin();
+  for ( ; anIt != aPolylineData.constEnd(); ++anIt )
+  {
+    const PolylineSection& aSection = *anIt;
+    if ( !aSection.myIsClosed )
+      return false;
+  }
+
+  return true;
+}
+
 /**
  * Remove all polyline attributes except dimension.
  */
 void HYDROData_Polyline::removeAll()
 {
 //Remove only section data
-  TDF_Label aLab = myLab.FindChild( TAG_SECTIONS_SIZE );
+  TDF_Label aLab = myLab.FindChild( DataTag_SectionsSize );
   aLab.ForgetAllAttributes();
 
-  aLab = myLab.FindChild( TAG_SECTIONS_TYPE );
+  aLab = myLab.FindChild( DataTag_SectionsType );
   aLab.ForgetAllAttributes();
 
-  aLab = myLab.FindChild( TAG_SECTIONS_CLOSED );
+  aLab = myLab.FindChild( DataTag_SectionsClosed );
   aLab.ForgetAllAttributes();
 
   myLab.ForgetAttribute(TDataStd_RealArray::GetID());
@@ -204,14 +308,14 @@ void HYDROData_Polyline::removeAll()
  * is ignored section type.
  * \return polyline painter path.
  */
-QPainterPath HYDROData_Polyline::painterPath()
+QPainterPath HYDROData_Polyline::painterPath() const
 {
   QPainterPath aPath;
   int aDim = getDimension();
-  if( ( aDim != 2 ) || ( aDim != 3) )
+  if( ( aDim != 2 ) && ( aDim != 3) )
       return aPath;
-  QList<PolylineSection> aSects = getPolylineData();
-  for( int i = 0 ; aSects.size() ; i++ ){
+  PolylineData aSects = getPolylineData();
+  for( int i = 0 ; i < aSects.size() ; i++ ){
     int aPntCnt = aSects[i].myCoords.size()/aDim;
     if( aPntCnt ){
       aPath.moveTo(aSects[i].myCoords[0], aSects[i].myCoords[1] );