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