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