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