]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Iterator.cxx
Salome HOME
a9486d07ff96d7e74402af371f34e50fefce2a00
[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   Handle(HYDROData_Entity) aResult;
77
78   // If label has no name attribute it mean that this is not object or
79   // this object has been removed from document
80   Handle(TDataStd_Name) aNameAtt;
81   if ( !theLabel.FindAttribute( TDataStd_Name::GetID(), aNameAtt ) )
82     return aResult;
83
84   ObjectKind aKind = KIND_UNKNOWN;
85
86   // Retrieve the type of object from label
87   Handle(TDataStd_NamedData) aNamedData;
88   if ( theLabel.FindAttribute( TDataStd_NamedData::GetID(), aNamedData ) )
89     aKind = aNamedData->GetInteger( "ObjectKind" );
90
91   if ( aKind == KIND_UNKNOWN )
92     aKind = theLabel.Father().Tag(); // Try to get type from father label
93   
94   switch( aKind )
95   {
96     case KIND_IMAGE:
97       aResult = new HYDROData_Image();
98       break;
99     case KIND_POLYLINE:
100       aResult = new HYDROData_Polyline();
101       break;
102     case KIND_BATHYMETRY:
103       aResult = new HYDROData_Bathymetry();
104       break;
105     case KIND_IMMERSIBLE_ZONE:
106       aResult = new HYDROData_ImmersibleZone();
107       break;
108     case KIND_CALCULATION:
109       aResult = new HYDROData_Calculation();
110       break;
111     case KIND_REGION:
112       aResult = new HYDROData_Region();
113       break;
114     case KIND_ZONE:
115       aResult = new HYDROData_Zone();
116       break;
117     case KIND_VISUAL_STATE:
118       aResult = new HYDROData_VisualState();
119       break;
120     default:
121       break;
122   }
123
124   if ( !aResult.IsNull() )
125     aResult->SetLabel( theLabel );
126
127   return aResult;
128 }