Salome HOME
f1dde62f1b67520606ec235e12bf5fa933df22b3
[modules/hydro.git] / src / HYDROData / HYDROData_Iterator.cxx
1
2 #include "HYDROData_Iterator.h"
3
4 #include "HYDROData_AltitudeObject.h"
5 #include "HYDROData_Bathymetry.h"
6 #include "HYDROData_CalculationCase.h"
7 #include "HYDROData_Channel.h"
8 #include "HYDROData_Confluence.h"
9 #include "HYDROData_DummyObject3D.h"
10 #include "HYDROData_Digue.h"
11 #include "HYDROData_EdgesGroup.h"
12 #include "HYDROData_Image.h"
13 #include "HYDROData_ImmersibleZone.h"
14 #include "HYDROData_Obstacle.h"
15 #include "HYDROData_Polyline3D.h"
16 #include "HYDROData_PolylineXY.h"
17 #include "HYDROData_Profile.h"
18 #include "HYDROData_ProfileUZ.h"
19 #include "HYDROData_VisualState.h"
20 #include "HYDROData_Region.h"
21 #include "HYDROData_River.h"
22 #include "HYDROData_Stream.h"
23 #include "HYDROData_Zone.h"
24
25 #include <TDataStd_Name.hxx>
26 #include <TDataStd_NamedData.hxx>
27
28 #include <NCollection_DataMap.hxx>
29
30 //! Returns label by root objects kind and the kind of the object
31 static TDF_Label GetLabelByKind(TDF_Label theRoot, ObjectKind theKind)
32 {
33   if (theKind == KIND_UNKNOWN) return theRoot;
34   return theRoot.FindChild(theKind);
35 }
36
37 HYDROData_Iterator::HYDROData_Iterator( const Handle(HYDROData_Document)& theDoc,
38                                         const ObjectKind                  theKind )
39 : myIter( GetLabelByKind( theDoc->LabelOfObjects(), theKind ), 
40           TDataStd_Name::GetID(), theKind == KIND_UNKNOWN ) // iterate all sub-objects for unknown kind
41 {
42 }
43
44 void HYDROData_Iterator::Next()
45 {
46   myIter.Next();
47   // omit the properties iteration in case of UNKNOWN kind filtering
48   while ( myIter.More() && myIter.Value()->Label().Depth() < 4 )
49     myIter.Next();
50 }
51
52 bool HYDROData_Iterator::More() const
53 {
54   return myIter.More();
55 }
56
57 Handle(HYDROData_Entity) HYDROData_Iterator::Current()
58 {
59   return Object(myIter.Value()->Label());
60 }
61
62 Handle(HYDROData_Entity) HYDROData_Iterator::CreateObject(
63   const Handle(HYDROData_Document)& theDoc,
64   const ObjectKind&                 theObjectKind )
65 {
66   TDF_Label aNewLab = 
67     GetLabelByKind( theDoc->LabelOfObjects(), theObjectKind ).FindChild( theDoc->NewID() );
68   return CreateObject( aNewLab, theObjectKind );
69 }
70
71 Handle(HYDROData_Entity) HYDROData_Iterator::CreateObject( TDF_Label&        theNewLabel, 
72                                                            const ObjectKind& theObjectKind )
73 {
74   // Object exists if there is a name attribute
75   TDataStd_Name::Set( theNewLabel, "" );
76
77   // Store the type of object in data label
78   TDataStd_NamedData::Set( theNewLabel );
79
80   Handle(TDataStd_NamedData) aNamedData;
81   theNewLabel.FindAttribute( TDataStd_NamedData::GetID(), aNamedData );
82   aNamedData->SetInteger( "ObjectKind", theObjectKind );
83
84   return Object( theNewLabel );
85 }
86
87 Handle(HYDROData_Entity) HYDROData_Iterator::Object( const TDF_Label& theLabel )
88 {
89   Handle(HYDROData_Entity) aResult;
90
91   // If label has no name attribute it mean that this is not object or
92   // this object has been removed from document
93   Handle(TDataStd_Name) aNameAtt;
94   if ( !theLabel.FindAttribute( TDataStd_Name::GetID(), aNameAtt ) )
95     return aResult;
96
97   ObjectKind aKind = KIND_UNKNOWN;
98
99   // Retrieve the type of object from label
100   Handle(TDataStd_NamedData) aNamedData;
101   if ( theLabel.FindAttribute( TDataStd_NamedData::GetID(), aNamedData ) )
102     aKind = aNamedData->GetInteger( "ObjectKind" );
103
104   if ( aKind == KIND_UNKNOWN )
105     aKind = theLabel.Father().Tag(); // Try to get type from father label
106   
107   switch( aKind )
108   {
109     case KIND_IMAGE:            aResult = new HYDROData_Image();             break;
110     case KIND_POLYLINE:         aResult = new HYDROData_Polyline3D();        break;
111     case KIND_BATHYMETRY:       aResult = new HYDROData_Bathymetry();        break;
112     case KIND_ALTITUDE:         aResult = new HYDROData_AltitudeObject();    break;
113     case KIND_IMMERSIBLE_ZONE:  aResult = new HYDROData_ImmersibleZone();    break;
114     case KIND_RIVER:            aResult = new HYDROData_River();             break;
115     case KIND_STREAM:           aResult = new HYDROData_Stream();            break;
116     case KIND_CONFLUENCE:       aResult = new HYDROData_Confluence();        break;
117     case KIND_CHANNEL:          aResult = new HYDROData_Channel();           break;
118     case KIND_OBSTACLE:         aResult = new HYDROData_Obstacle();          break;
119     case KIND_DIGUE:            aResult = new HYDROData_Digue();             break;
120     case KIND_PROFILE:          aResult = new HYDROData_Profile();           break;
121     case KIND_PROFILEUZ:        aResult = new HYDROData_ProfileUZ();         break;
122     case KIND_POLYLINEXY:       aResult = new HYDROData_PolylineXY();        break;
123     case KIND_CALCULATION:      aResult = new HYDROData_CalculationCase();   break;
124     case KIND_REGION:           aResult = new HYDROData_Region();            break;
125     case KIND_ZONE:             aResult = new HYDROData_Zone();              break;
126     case KIND_VISUAL_STATE:     aResult = new HYDROData_VisualState();       break;
127     case KIND_DUMMY_3D:         aResult = new HYDROData_DummyObject3D();     break;
128     case KIND_EDGES_GROUP:      aResult = new HYDROData_EdgesGroup();        break;
129     default:                                                                 break;
130   }
131
132   if ( !aResult.IsNull() )
133     aResult->SetLabel( theLabel );
134
135   return aResult;
136 }