Salome HOME
615454a9b8dcab5d2934a85defc60e0d8305a412
[modules/hydro.git] / src / HYDROData / HYDROData_Iterator.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_Iterator.h"
20
21 #include "HYDROData_AltitudeObject.h"
22 #include "HYDROData_Bathymetry.h"
23 #include "HYDROData_CalculationCase.h"
24 #include "HYDROData_Channel.h"
25 #include "HYDROData_Confluence.h"
26 #include "HYDROData_Digue.h"
27 #include "HYDROData_DummyObject3D.h"
28 #include "HYDROData_Image.h"
29 #include "HYDROData_ImmersibleZone.h"
30 #include "HYDROData_LandCoverMap.h"
31 #include "HYDROData_Obstacle.h"
32 #include "HYDROData_ObstacleAltitude.h"
33 #include "HYDROData_Polyline3D.h"
34 #include "HYDROData_PolylineXY.h"
35 #include "HYDROData_Profile.h"
36 #include "HYDROData_ProfileUZ.h"
37 #include "HYDROData_Region.h"
38 #include "HYDROData_River.h"
39 #include "HYDROData_ShapesGroup.h"
40 #include "HYDROData_SplitShapesGroup.h"
41 #include "HYDROData_Stream.h"
42 #include "HYDROData_StreamAltitude.h"
43 #include "HYDROData_VisualState.h"
44 #include "HYDROData_Zone.h"
45 #include "HYDROData_StricklerTable.h"
46
47 #include <TDataStd_Name.hxx>
48 #include <TDataStd_NamedData.hxx>
49
50 #include <NCollection_DataMap.hxx>
51
52 //! Returns label by root objects kind and the kind of the object
53 static TDF_Label GetLabelByKind(TDF_Label theRoot, ObjectKind theKind)
54 {
55   if (theKind == KIND_UNKNOWN) return theRoot;
56   return theRoot.FindChild(theKind);
57 }
58
59 HYDROData_Iterator::HYDROData_Iterator( const Handle(HYDROData_Document)& theDoc,
60                                         const ObjectKind                  theKind )
61 : myIter( GetLabelByKind( theDoc->LabelOfObjects(), theKind ), 
62           TDataStd_Name::GetID(), theKind == KIND_UNKNOWN ) // iterate all sub-objects for unknown kind
63 {
64 }
65
66 void HYDROData_Iterator::Next()
67 {
68   myIter.Next();
69   // omit the properties iteration in case of UNKNOWN kind filtering
70   while ( myIter.More() && myIter.Value()->Label().Depth() < 4 )
71     myIter.Next();
72 }
73
74 bool HYDROData_Iterator::More() const
75 {
76   return myIter.More();
77 }
78
79 Handle(HYDROData_Entity) HYDROData_Iterator::Current()
80 {
81   return Object(myIter.Value()->Label());
82 }
83
84 Handle(HYDROData_Entity) HYDROData_Iterator::CreateObject(
85   const Handle(HYDROData_Document)& theDoc,
86   const ObjectKind&                 theObjectKind )
87 {
88   TDF_Label aNewLab = 
89     GetLabelByKind( theDoc->LabelOfObjects(), theObjectKind ).FindChild( theDoc->NewID() );
90   return CreateObject( aNewLab, theObjectKind );
91 }
92
93 Handle(HYDROData_Entity) HYDROData_Iterator::CreateObject( TDF_Label&        theNewLabel, 
94                                                            const ObjectKind& theObjectKind )
95 {
96   // Object exists if there is a name attribute
97   TDataStd_Name::Set( theNewLabel, "" );
98
99   // Store the type of object in data label
100   TDataStd_NamedData::Set( theNewLabel );
101
102   Handle(TDataStd_NamedData) aNamedData;
103   theNewLabel.FindAttribute( TDataStd_NamedData::GetID(), aNamedData );
104   aNamedData->SetInteger( "ObjectKind", theObjectKind );
105
106   return Object( theNewLabel );
107 }
108
109 Handle(HYDROData_Entity) HYDROData_Iterator::Object( const TDF_Label& theLabel )
110 {
111   Handle(HYDROData_Entity) aResult;
112
113   // If label has no name attribute it mean that this is not object or
114   // this object has been removed from document
115   Handle(TDataStd_Name) aNameAtt;
116   if ( !theLabel.FindAttribute( TDataStd_Name::GetID(), aNameAtt ) )
117     return aResult;
118
119   ObjectKind aKind = KIND_UNKNOWN;
120
121   // Retrieve the type of object from label
122   Handle(TDataStd_NamedData) aNamedData;
123   if ( theLabel.FindAttribute( TDataStd_NamedData::GetID(), aNamedData ) )
124     aKind = aNamedData->GetInteger( "ObjectKind" );
125
126   if ( aKind == KIND_UNKNOWN )
127     aKind = theLabel.Father().Tag(); // Try to get type from father label
128   
129   switch( aKind )
130   {
131     case KIND_IMAGE:              aResult = new HYDROData_Image();                break;
132     case KIND_POLYLINE:           aResult = new HYDROData_Polyline3D();           break;
133     case KIND_BATHYMETRY:         aResult = new HYDROData_Bathymetry();           break;
134     case KIND_ALTITUDE:           aResult = new HYDROData_AltitudeObject();       break;
135     case KIND_IMMERSIBLE_ZONE:    aResult = new HYDROData_ImmersibleZone();       break;
136     case KIND_RIVER:              aResult = new HYDROData_River();                break;
137     case KIND_STREAM:             aResult = new HYDROData_Stream();               break;
138     case KIND_CONFLUENCE:         aResult = new HYDROData_Confluence();           break;
139     case KIND_CHANNEL:            aResult = new HYDROData_Channel();              break;
140     case KIND_OBSTACLE:           aResult = new HYDROData_Obstacle();             break;
141     case KIND_DIGUE:              aResult = new HYDROData_Digue();                break;
142     case KIND_PROFILE:            aResult = new HYDROData_Profile();              break;
143     case KIND_PROFILEUZ:          aResult = new HYDROData_ProfileUZ();            break;
144     case KIND_POLYLINEXY:         aResult = new HYDROData_PolylineXY();           break;
145     case KIND_CALCULATION:        aResult = new HYDROData_CalculationCase();      break;
146     case KIND_REGION:             aResult = new HYDROData_Region();               break;
147     case KIND_ZONE:               aResult = new HYDROData_Zone();                 break;
148     case KIND_VISUAL_STATE:       aResult = new HYDROData_VisualState();          break;
149     case KIND_DUMMY_3D:           aResult = new HYDROData_DummyObject3D();        break;
150     case KIND_SHAPES_GROUP:       aResult = new HYDROData_ShapesGroup();          break;
151     case KIND_SPLIT_GROUP:        aResult = new HYDROData_SplitShapesGroup();     break;
152     case KIND_STREAM_ALTITUDE:    aResult = new HYDROData_StreamAltitude();       break;
153     case KIND_OBSTACLE_ALTITUDE:  aResult = new HYDROData_ObstacleAltitude();     break;
154     case KIND_STRICKLER_TABLE:    aResult = new HYDROData_StricklerTable();       break;
155     case KIND_LAND_COVER_MAP:     aResult = new HYDROData_LandCoverMap();         break;
156
157     case KIND_LAND_COVER_OBSOLETE: break;
158     default:                       break;
159   }
160
161   if ( !aResult.IsNull() )
162     aResult->SetLabel( theLabel );
163
164   return aResult;
165 }