Salome HOME
Update the names of Regions and Zones if case name changed (Bug #110).
[modules/hydro.git] / src / HYDROData / HYDROData_Zone.cxx
1
2 #include "HYDROData_Zone.h"
3
4 #include "HYDROData_ArtificialObject.h"
5 #include "HYDROData_Bathymetry.h"
6 #include "HYDROData_Document.h"
7 #include "HYDROData_NaturalObject.h"
8
9 #include <TNaming_Builder.hxx>
10 #include <TNaming_NamedShape.hxx>
11
12 #include <TopoDS_Shape.hxx>
13
14 #include <QStringList>
15
16 #include <TDataStd_Integer.hxx>
17
18 #define PYTHON_ZONE_ID "KIND_ZONE"
19
20 IMPLEMENT_STANDARD_HANDLE(HYDROData_Zone, HYDROData_Entity)
21 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Zone, HYDROData_Entity)
22
23
24 HYDROData_Zone::HYDROData_Zone()
25 : HYDROData_Entity()
26 {
27 }
28
29 HYDROData_Zone::~HYDROData_Zone()
30 {
31 }
32
33 QStringList HYDROData_Zone::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
34 {
35   QStringList aResList;
36
37   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
38   if ( aDocument.IsNull() )
39     return aResList;
40
41   QString aDocName = aDocument->GetDocPyName();
42   QString aZoneName = GetName();
43
44   aResList << QString( "%1 = %2.CreateObject( %3 );" )
45               .arg( aZoneName ).arg( aDocName ).arg( PYTHON_ZONE_ID );
46   aResList << QString( "%1.SetName( \"%2\" );" )
47               .arg( aZoneName ).arg( aZoneName );
48   aResList << QString( "" );
49
50   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
51   HYDROData_SequenceOfObjects::Iterator aGeomObjsIter( aGeomObjects );
52   for ( ; aGeomObjsIter.More(); aGeomObjsIter.Next() )
53   {
54     Handle(HYDROData_Object) aRefGeomObj =
55       Handle(HYDROData_Object)::DownCast( aGeomObjsIter.Value() );
56     if ( !aRefGeomObj.IsNull() )
57       setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
58   }
59
60   // How can we get the shape? Mb Update() method to intersect the shapes of reference objects?
61   // TODO:  TopoDS_Shape aRefShape = GetShape();
62
63   return aResList;
64 }
65
66 void HYDROData_Zone::SetShape( const TopoDS_Shape& theShape )
67 {
68   TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
69   aBuilder.Generated( theShape );
70 }
71
72 TopoDS_Shape HYDROData_Zone::GetShape() const
73 {
74   Handle(TNaming_NamedShape) aNamedShape;
75   if( myLab.FindChild( DataTag_Shape ).FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
76     return aNamedShape->Get();
77   return TopoDS_Shape();
78 }
79
80 bool HYDROData_Zone::IsMergingNeed() const
81 {
82   Handle(HYDROData_Bathymetry) aRefBathymetry;
83
84   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
85   HYDROData_SequenceOfObjects::Iterator aGeomObjsIter( aGeomObjects );
86   for ( ; aGeomObjsIter.More(); aGeomObjsIter.Next() )
87   {
88     Handle(HYDROData_Object) aRefGeomObj =
89       Handle(HYDROData_Object)::DownCast( aGeomObjsIter.Value() );
90     if ( aRefGeomObj.IsNull() )
91       continue;
92
93     Handle(HYDROData_Bathymetry) anObjBathymetry = aRefGeomObj->GetBathymetry();
94     if ( anObjBathymetry.IsNull() )
95       continue;
96
97     if ( aRefBathymetry.IsNull() )
98     {
99       aRefBathymetry = anObjBathymetry;
100       continue;
101     }
102
103     if ( !IsEqual( aRefBathymetry, anObjBathymetry ) )
104       return true;
105   }
106
107   return false;
108 }
109
110 void HYDROData_Zone::SetMergeType( const MergeBathymetriesType& theType )
111 {
112   Handle(TDataStd_Integer) anInt;
113   if ( myLab.FindChild( DataTag_MergeType ).FindAttribute( TDataStd_Integer::GetID(), anInt ) )
114   {
115     anInt->Set( (int)theType );
116   }
117   else
118   {
119     anInt = TDataStd_Integer::Set( myLab.FindChild( DataTag_MergeType ), (int)theType );
120   }
121 }
122
123 HYDROData_Zone::MergeBathymetriesType HYDROData_Zone::GetMergeType() const
124 {
125   MergeBathymetriesType aMergeType = Merge_UNKNOWN;
126   
127   Handle(TDataStd_Integer) anInt;
128   if ( myLab.FindChild( DataTag_MergeType ).FindAttribute( TDataStd_Integer::GetID(), anInt ) )
129     aMergeType = (MergeBathymetriesType)anInt->Get();
130
131   return aMergeType;
132 }
133
134 void HYDROData_Zone::SetMergeBathymetry( const Handle(HYDROData_Bathymetry)& theBathymetry )
135 {
136   SetReferenceObject( theBathymetry, DataTag_Bathymetry );
137 }
138
139 Handle(HYDROData_Bathymetry) HYDROData_Zone::GetMergeBathymetry() const
140 {
141   return Handle(HYDROData_Bathymetry)::DownCast( 
142            GetReferenceObject( DataTag_Bathymetry ) );
143 }
144
145 void HYDROData_Zone::RemoveMergeBathymetry()
146 {
147   ClearReferenceObjects( DataTag_Bathymetry );
148 }
149
150 bool HYDROData_Zone::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
151 {
152   if ( theObject.IsNull() )
153     return false;
154   
155   if ( !theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) &&
156        !theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) ) )
157     return false; // Wrong type of object
158
159   if ( HasReference( theObject, DataTag_GeometryObject ) )
160     return false; // Object is already in reference list
161
162   AddReferenceObject( theObject, DataTag_GeometryObject );
163   return true;
164 }
165
166 HYDROData_SequenceOfObjects HYDROData_Zone::GetGeometryObjects() const
167 {
168   return GetReferenceObjects( DataTag_GeometryObject );
169 }
170
171 void HYDROData_Zone::RemoveGeometryObjects()
172 {
173   ClearReferenceObjects( DataTag_GeometryObject );
174 }
175
176
177