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