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