Salome HOME
Fix for the feature #6: Update of objects (T 1.2): For objects which need updating...
[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 bool HYDROData_Zone::CanRemove()
67 {
68   return false;
69 }
70
71 HYDROData_SequenceOfObjects HYDROData_Zone::GetAllReferenceObjects() const
72 {
73   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
74   aResSeq.Append( GetGeometryObjects() );
75   return aResSeq;
76 }
77
78 void HYDROData_Zone::SetShape( const TopoDS_Shape& theShape )
79 {
80   TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
81   aBuilder.Generated( theShape );
82 }
83
84 TopoDS_Shape HYDROData_Zone::GetShape() const
85 {
86   Handle(TNaming_NamedShape) aNamedShape;
87   if( myLab.FindChild( DataTag_Shape ).FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
88     return aNamedShape->Get();
89   return TopoDS_Shape();
90 }
91
92 bool HYDROData_Zone::IsMergingNeed() const
93 {
94   Handle(HYDROData_Bathymetry) aRefBathymetry;
95
96   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
97   HYDROData_SequenceOfObjects::Iterator aGeomObjsIter( aGeomObjects );
98   for ( ; aGeomObjsIter.More(); aGeomObjsIter.Next() )
99   {
100     Handle(HYDROData_Object) aRefGeomObj =
101       Handle(HYDROData_Object)::DownCast( aGeomObjsIter.Value() );
102     if ( aRefGeomObj.IsNull() )
103       continue;
104
105     Handle(HYDROData_Bathymetry) anObjBathymetry = aRefGeomObj->GetBathymetry();
106     if ( anObjBathymetry.IsNull() )
107       continue;
108
109     if ( aRefBathymetry.IsNull() )
110     {
111       aRefBathymetry = anObjBathymetry;
112       continue;
113     }
114
115     if ( !IsEqual( aRefBathymetry, anObjBathymetry ) )
116       return true;
117   }
118
119   return false;
120 }
121
122 void HYDROData_Zone::SetMergeType( const MergeBathymetriesType& theType )
123 {
124   Handle(TDataStd_Integer) anInt;
125   if ( myLab.FindChild( DataTag_MergeType ).FindAttribute( TDataStd_Integer::GetID(), anInt ) )
126   {
127     anInt->Set( (int)theType );
128   }
129   else
130   {
131     anInt = TDataStd_Integer::Set( myLab.FindChild( DataTag_MergeType ), (int)theType );
132   }
133 }
134
135 HYDROData_Zone::MergeBathymetriesType HYDROData_Zone::GetMergeType() const
136 {
137   MergeBathymetriesType aMergeType = Merge_UNKNOWN;
138   
139   Handle(TDataStd_Integer) anInt;
140   if ( myLab.FindChild( DataTag_MergeType ).FindAttribute( TDataStd_Integer::GetID(), anInt ) )
141     aMergeType = (MergeBathymetriesType)anInt->Get();
142
143   return aMergeType;
144 }
145
146 void HYDROData_Zone::SetMergeBathymetry( const Handle(HYDROData_Bathymetry)& theBathymetry )
147 {
148   SetReferenceObject( theBathymetry, DataTag_Bathymetry );
149 }
150
151 Handle(HYDROData_Bathymetry) HYDROData_Zone::GetMergeBathymetry() const
152 {
153   return Handle(HYDROData_Bathymetry)::DownCast( 
154            GetReferenceObject( DataTag_Bathymetry ) );
155 }
156
157 void HYDROData_Zone::RemoveMergeBathymetry()
158 {
159   ClearReferenceObjects( DataTag_Bathymetry );
160 }
161
162 bool HYDROData_Zone::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
163 {
164   if ( theObject.IsNull() )
165     return false;
166   
167   if ( !theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) &&
168        !theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) ) )
169     return false; // Wrong type of object
170
171   if ( HasReference( theObject, DataTag_GeometryObject ) )
172     return false; // Object is already in reference list
173
174   AddReferenceObject( theObject, DataTag_GeometryObject );
175   return true;
176 }
177
178 HYDROData_SequenceOfObjects HYDROData_Zone::GetGeometryObjects() const
179 {
180   return GetReferenceObjects( DataTag_GeometryObject );
181 }
182
183 void HYDROData_Zone::RemoveGeometryObjects()
184 {
185   ClearReferenceObjects( DataTag_GeometryObject );
186 }
187
188
189