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