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