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