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