]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Object.cxx
Salome HOME
4a094521720404b1eeac7ef0fc7a0920a941e0cb
[modules/hydro.git] / src / HYDROData / HYDROData_Object.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_Object.h"
20
21 #include "HYDROData_DummyObject3D.h"
22 #include "HYDROData_ShapesGroup.h"
23 #include "HYDROData_Tool.h"
24 #include "HYDROData_Iterator.h"
25 #include "HYDROData_IAltitudeObject.h"
26 #include <TopoDS_Shape.hxx>
27 #include <TDataStd_Integer.hxx>
28
29 #include <QColor>
30
31 IMPLEMENT_STANDARD_HANDLE(HYDROData_Object,HYDROData_Entity)
32 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Object,HYDROData_Entity)
33
34 HYDROData_Object::HYDROData_Object()
35 : HYDROData_Entity()
36 {
37 }
38
39 HYDROData_Object::~HYDROData_Object()
40 {
41 }
42
43 void HYDROData_Object::SetName( const QString& theName )
44 {
45   QString anOldObjName = GetName();
46   if ( anOldObjName != theName )
47   {
48     HYDROData_SequenceOfObjects aGroups = GetGroups();
49     HYDROData_SequenceOfObjects::Iterator anIter( aGroups );
50     for ( ; anIter.More(); anIter.Next() )
51     {
52       Handle(HYDROData_ShapesGroup) aGroup =
53         Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
54       if ( aGroup.IsNull() )
55         continue;
56
57       HYDROData_Tool::UpdateChildObjectName( anOldObjName, theName, aGroup );
58     }
59
60     Handle(HYDROData_DummyObject3D) anObject3D = GetObject3D();
61     if ( !anObject3D.IsNull() )
62       HYDROData_Tool::UpdateChildObjectName( anOldObjName, theName, anObject3D );
63
64     Handle(HYDROData_IAltitudeObject) anAltitudeObj = getChildAltitudeObject();
65     if ( !anAltitudeObj.IsNull() )
66       HYDROData_Tool::UpdateChildObjectName( anOldObjName, theName, anAltitudeObj );
67   }
68
69   HYDROData_Entity::SetName( theName );
70 }
71
72 void HYDROData_Object::Update()
73 {
74   RemoveTopShape();
75   RemoveShape3D();
76   RemoveGroupObjects();
77   checkAndSetAltitudeObject();
78   HYDROData_Entity::Update();
79 }
80
81 HYDROData_SequenceOfObjects HYDROData_Object::GetAllReferenceObjects() const
82 {
83   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
84
85   Handle(HYDROData_IAltitudeObject) aRefAltitude = GetAltitudeObject();
86   if ( !aRefAltitude.IsNull() )
87     aResSeq.Append( aRefAltitude );
88
89   return aResSeq;
90 }
91
92 void HYDROData_Object::SetToUpdate( bool theFlag )
93 {
94   HYDROData_Entity::SetToUpdate( theFlag );
95
96   Handle(HYDROData_DummyObject3D) anObject3D = GetObject3D();
97   if ( !anObject3D.IsNull() )
98     anObject3D->SetToUpdate( theFlag );
99 }
100
101 void HYDROData_Object::SetTopShape( const TopoDS_Shape& theShape )
102 {
103   HYDROData_Entity::SetShape( DataTag_TopShape, theShape );
104 }
105
106 TopoDS_Shape HYDROData_Object::GetTopShape() const
107 {
108   return HYDROData_Entity::GetShape( DataTag_TopShape );
109 }
110
111 void HYDROData_Object::SetShape3D( const TopoDS_Shape& theShape )
112 {
113   HYDROData_Entity::SetShape( DataTag_Shape3D, theShape );
114   // Check the object 3D existance
115   checkAndSetObject3D();
116 }
117
118 TopoDS_Shape HYDROData_Object::GetShape3D() const
119 {
120   return HYDROData_Entity::GetShape( DataTag_Shape3D );
121 }
122
123 Handle(HYDROData_DummyObject3D) HYDROData_Object::GetObject3D() const
124 {
125   Handle(HYDROData_DummyObject3D) anObject;
126   
127   TDF_Label aLabel = myLab.FindChild( DataTag_Object3D, false );
128   if ( !aLabel.IsNull() )
129   {
130     TDF_Label aChildLabel = aLabel.FindChild( 0, false );
131     if ( !aChildLabel.IsNull() )
132     {
133       anObject = Handle(HYDROData_DummyObject3D)::DownCast(
134         HYDROData_Iterator::Object( aChildLabel ) );
135     }
136   }
137
138   return anObject;
139 }
140
141 void HYDROData_Object::checkAndSetObject3D()
142 {
143   TDF_Label aLabel = myLab.FindChild( DataTag_Object3D, false );
144   if ( !aLabel.IsNull() )
145     return;
146
147   TDF_Label aChildLabel = myLab.FindChild( DataTag_Object3D ).FindChild( 0 );
148   HYDROData_Iterator::CreateObject( aChildLabel, KIND_DUMMY_3D );
149 }
150
151 HYDROData_SequenceOfObjects HYDROData_Object::GetGroups() const
152 {
153   return GetReferenceObjects( DataTag_EdgesGroup );
154 }
155
156 Handle(HYDROData_ShapesGroup) HYDROData_Object::GetGroup( const int theGroupId ) const
157 {
158   Handle(HYDROData_ShapesGroup) aResGroup;
159
160   HYDROData_SequenceOfObjects aGroups = GetGroups();
161   if ( theGroupId < 0 || theGroupId >= aGroups.Length() )
162     return aResGroup;
163
164   aResGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( theGroupId + 1 ) );
165
166   return aResGroup;
167 }
168
169 int HYDROData_Object::GetGroupId( const Handle(HYDROData_ShapesGroup)& theGroup ) const
170 {
171   int aRes = -1;
172
173   HYDROData_SequenceOfObjects aGroups = GetGroups();
174   for ( int i = 1, n = aGroups.Length(); i <= n; ++i )
175   {
176     Handle(HYDROData_ShapesGroup) aGroup =
177       Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( i ) );
178     if ( IsEqual( theGroup, aGroup ) )
179     {
180       aRes = i - 1;
181       break;
182     }
183   }
184
185   return aRes;
186 }
187
188 bool HYDROData_Object::SetAltitudeObject( 
189   const Handle(HYDROData_IAltitudeObject)& theAltitude )
190 {
191   if ( theAltitude.IsNull() )
192     return false;
193   
194   Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
195   if ( IsEqual( aPrevAltitude, theAltitude ) )
196     return true;
197
198   SetReferenceObject( theAltitude, DataTag_AltitudeObject );
199
200   // #636: In the case of the altitude object change the geometry of the main object is not changed,
201   // to the object should not be marked as updated
202   //SetToUpdate( true );
203
204   return true;
205 }
206
207 Handle(HYDROData_IAltitudeObject) HYDROData_Object::GetAltitudeObject() const
208 {
209   return Handle(HYDROData_IAltitudeObject)::DownCast( 
210            GetReferenceObject( DataTag_AltitudeObject ) );
211 }
212
213 void HYDROData_Object::RemoveAltitudeObject()
214 {
215   Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
216   if ( aPrevAltitude.IsNull() )
217     return;
218
219   ClearReferenceObjects( DataTag_AltitudeObject );
220
221   // #636: In the case of the altitude object change the geometry of the main object is not changed,
222   // to the object should not be marked as updated
223   //SetToUpdate( true );
224 }
225
226 void HYDROData_Object::SetFillingColor( const QColor& theColor )
227 {
228   SetColor( theColor, DataTag_FillingColor );
229 }
230
231 QColor HYDROData_Object::GetFillingColor() const
232 {
233   return GetColor( DefaultFillingColor(), DataTag_FillingColor );
234 }
235
236 void HYDROData_Object::SetBorderColor( const QColor& theColor )
237 {
238   SetColor( theColor, DataTag_BorderColor );
239 }
240
241 QColor HYDROData_Object::GetBorderColor() const
242 {
243   return GetColor( DefaultBorderColor(), DataTag_BorderColor );
244 }
245
246 QColor HYDROData_Object::DefaultFillingColor() const
247 {
248   return QColor( Qt::yellow );
249 }
250
251 QColor HYDROData_Object::DefaultBorderColor() const
252 {
253   return QColor( Qt::transparent );
254 }
255
256 QStringList HYDROData_Object::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
257 {
258   QStringList aResList = HYDROData_Entity::dumpObjectCreation( theTreatedObjects );
259   if ( aResList.isEmpty() )
260     return aResList; //Object was not created
261
262   QStringList aColorsDef;
263
264   QColor aFillingColor = GetFillingColor();
265   setPythonObjectColor( aColorsDef, aFillingColor, DefaultFillingColor(), "SetFillingColor" );
266
267   QColor aBorderColor = GetBorderColor();
268   setPythonObjectColor( aColorsDef, aBorderColor, DefaultBorderColor(), "SetBorderColor" );
269
270   if ( !aColorsDef.isEmpty() )
271   {
272     aResList << aColorsDef;
273     aResList << QString( "" );
274   }
275
276   return aResList;
277 }
278
279 ObjectKind HYDROData_Object::getAltitudeObjectType() const
280 {
281   return KIND_UNKNOWN;
282 }
283
284 Handle(HYDROData_IAltitudeObject) HYDROData_Object::getChildAltitudeObject() const
285 {
286   Handle(HYDROData_IAltitudeObject) anObject;
287   
288   TDF_Label aLabel = myLab.FindChild( DataTag_ChildAltitudeObject, false );
289   if ( !aLabel.IsNull() )
290   {
291     TDF_Label aChildLabel = aLabel.FindChild( 0, false );
292     if ( !aChildLabel.IsNull() )
293     {
294       anObject = Handle(HYDROData_IAltitudeObject)::DownCast(
295         HYDROData_Iterator::Object( aChildLabel ) );
296     }
297   }
298
299   return anObject;
300 }
301
302 void HYDROData_Object::checkAndSetAltitudeObject()
303 {
304   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
305
306   ObjectKind anAltitudeObjectType = getAltitudeObjectType();
307   if ( anAltitudeObjectType == KIND_UNKNOWN )
308     return; // No need to create altitude object
309
310   TDF_Label aLabel = myLab.FindChild( DataTag_ChildAltitudeObject, false );
311   if ( !aLabel.IsNull() )
312     return;
313
314   TDF_Label aChildLabel = myLab.FindChild( DataTag_ChildAltitudeObject ).FindChild( 0 );
315
316   Handle(HYDROData_IAltitudeObject) anAltitudeObject =
317     Handle(HYDROData_IAltitudeObject)::DownCast(
318       HYDROData_Iterator::CreateObject( aChildLabel, anAltitudeObjectType ) );
319
320   QString anAltitudePref = GetName() + "_Altitude";
321   QString anAltitudeName = HYDROData_Tool::GenerateObjectName( aDocument, anAltitudePref );
322   anAltitudeObject->SetName( anAltitudeName );
323
324   SetAltitudeObject( anAltitudeObject );
325 }
326
327 Handle(HYDROData_ShapesGroup) HYDROData_Object::createGroupObject()
328 {
329   TDF_Label aNewLab = myLab.FindChild( DataTag_EdgesGroup ).NewChild();
330
331   Handle(HYDROData_ShapesGroup) aNewGroup =
332     Handle(HYDROData_ShapesGroup)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_SHAPES_GROUP ) );
333   AddReferenceObject( aNewGroup, DataTag_EdgesGroup );
334
335   return aNewGroup;
336 }
337
338 void HYDROData_Object::RemoveGroupObjects()
339 {
340   TDF_Label aLabel = myLab.FindChild( DataTag_EdgesGroup, false );
341   if ( !aLabel.IsNull() )
342     aLabel.ForgetAllAttributes();
343 }
344
345 void HYDROData_Object::RemoveTopShape()
346 {
347   HYDROData_Entity::SetShape( DataTag_TopShape, TopoDS_Shape() );
348 }
349
350 void HYDROData_Object::RemoveShape3D()
351 {
352   HYDROData_Entity::SetShape( DataTag_Shape3D, TopoDS_Shape() );
353 }
354
355 bool HYDROData_Object::IsSubmersible() const
356 {
357   Handle(TDataStd_Integer) aSubMersibleAttr;
358
359   bool isSubmersible = true; //default
360   if( myLab.FindAttribute(TDataStd_Integer::GetID(), aSubMersibleAttr ) )
361   {
362     int aValue = aSubMersibleAttr->Get();
363     isSubmersible = ( aValue != 0 );
364   }
365   return isSubmersible;
366 }
367
368 void HYDROData_Object::SetIsSubmersible( bool isSubmersible ) const
369 {
370   TDataStd_Integer::Set( myLab, isSubmersible ? 1 : 0 );
371 }