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