Salome HOME
force update of channel - delete groups
[modules/hydro.git] / src / HYDROData / HYDROData_Channel.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_Channel.h"
20
21 #include "HYDROData_Document.h"
22 #include "HYDROData_Polyline3D.h"
23 #include "HYDROData_Profile.h"
24 #include "HYDROData_PolylineXY.h"
25 #include "HYDROData_Projection.h"
26 #include "HYDROData_ShapesGroup.h"
27 #include "HYDROData_ShapesTool.h"
28 #include "HYDROData_Stream.h"
29 #include "HYDROData_Tool.h"
30
31 #include <BRepBuilderAPI_MakeWire.hxx>
32
33 #include <BRepOffsetAPI_MakePipeShell.hxx>
34 #include <BRepOffsetAPI_MakePipe.hxx>
35 #include <BRepCheck_Analyzer.hxx>
36
37 #include <BRep_Tool.hxx>
38
39 #include <BRepBuilderAPI_Transform.hxx>
40
41 #include <BRepLib_MakeEdge.hxx>
42 #include <BRepLib_MakeWire.hxx>
43
44 #include <GeomAPI_ProjectPointOnCurve.hxx>
45
46 #include <gp_Ax1.hxx>
47
48 #include <TopExp.hxx>
49 #include <TopExp_Explorer.hxx>
50
51 #include <TColgp_HArray1OfPnt.hxx>
52 #include <TColgp_Array1OfDir.hxx>
53
54 #include <TColStd_Array1OfReal.hxx>
55
56 #include <TopTools_HArray1OfShape.hxx>
57
58 #include <TopoDS.hxx>
59 #include <TopoDS_Wire.hxx>
60 #include <TopoDS_Vertex.hxx>
61
62 #define DEB_CHANNEL 1
63 #ifdef DEB_CHANNEL
64 #include <BRepTools.hxx>
65 #endif
66
67 #include <QColor>
68 #include <QStringList>
69
70 #define _DEVDEBUG_
71 #include "HYDRO_trace.hxx"
72
73 IMPLEMENT_STANDARD_HANDLE(HYDROData_Channel,HYDROData_ArtificialObject)
74 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Channel,HYDROData_ArtificialObject)
75
76
77 HYDROData_Channel::HYDROData_Channel()
78 : HYDROData_ArtificialObject( Geom_3d )
79 {
80 }
81
82 HYDROData_Channel::~HYDROData_Channel()
83 {
84 }
85
86 QStringList HYDROData_Channel::DumpToPython( const QString& thePyScriptPath,
87                                              MapOfTreatedObjects& theTreatedObjects ) const
88 {
89   QStringList aResList = dumpObjectCreation( theTreatedObjects );
90   QString aName = GetObjPyName();
91
92   Handle(HYDROData_Polyline3D) aRefGideLine = GetGuideLine();
93   setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefGideLine, "SetGuideLine" );
94
95   Handle(HYDROData_Profile) aRefProfile = GetProfile();
96   setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefProfile, "SetProfile" );
97
98   aResList << QString( "%1.SetEquiDistance( %2 )" ).arg( aName ).arg( GetEquiDistance() );
99
100   aResList << QString( "" );
101   aResList << QString( "%1.Update()" ).arg( aName );
102   aResList << QString( "" );
103
104   return aResList;
105 }
106
107 HYDROData_SequenceOfObjects HYDROData_Channel::GetAllReferenceObjects() const
108 {
109   HYDROData_SequenceOfObjects aResSeq = HYDROData_ArtificialObject::GetAllReferenceObjects();
110
111   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
112   if ( !aGuideLine.IsNull() )
113     aResSeq.Append( aGuideLine );
114
115   Handle(HYDROData_Profile) aProfile = GetProfile();
116   if ( !aProfile.IsNull() )
117     aResSeq.Append( aProfile );
118
119   return aResSeq;
120 }
121
122 bool HYDROData_Channel::CreatePresentations( const Handle(HYDROData_Polyline3D)& theGuideLine,
123                                              const Handle(HYDROData_Profile)&    theProfile,
124                                              PrsDefinition&                      thePrs,
125                                              double                              theEquiDistance )
126 {
127   // Check input parameters
128   if ( theGuideLine.IsNull() || theProfile.IsNull() ) {
129     return false;
130   }
131
132   TopoDS_Wire aPathWire = TopoDS::Wire( theGuideLine->GetShape3D() );
133   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetShape3D() );
134   if ( aPathWire.IsNull() || aProfileWire.IsNull() ) {
135     return false;
136   }
137
138 #ifdef DEB_CHANNEL
139   std::string brepName = "guideline_";
140   brepName += theGuideLine->GetName().toStdString();
141   brepName += ".brep";
142   BRepTools::Write( aPathWire, brepName.c_str() );
143   brepName = "profile_";
144   brepName += theGuideLine->GetName().toStdString();
145   brepName += ".brep";
146   BRepTools::Write( aProfileWire, brepName.c_str() );
147 #endif
148
149   // Pre-processing
150   Handle(HYDROData_PolylineXY) aPolylineXY = theGuideLine->GetPolylineXY();
151   if ( aPolylineXY.IsNull() ) {
152     return false;
153   }
154
155   /*
156   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
157   HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
158   int aNbPoints = aPolylinePoints.Length();
159   */
160
161   HYDROData_Polyline3D::Polyline3DPoints aPolylinePoints3D = theGuideLine->GetPoints( theEquiDistance );
162   int aNbPoints = aPolylinePoints3D.Length();
163   if ( aNbPoints < 2 )
164     return false;
165   
166   // Get tangent in each point of the guide line ( 2D )
167   TColgp_Array1OfDir aTangents( 1, aNbPoints );
168
169   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
170   
171   if( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE )
172   {
173     for ( int i = 1; i <= aNbPoints; ++i ) {
174       gp_XYZ aPnt = aPolylinePoints3D.Value( i );
175       aPnt.SetZ( 0. );
176       gp_XYZ aPrevPnt;
177       if ( i > 1 ) {
178         aPrevPnt = aPolylinePoints3D.Value( i - 1 );
179         aPrevPnt.SetZ( 0. );
180       }
181
182       gp_Vec aDir;
183       if ( i < aNbPoints ) {
184         gp_XYZ aNextPnt = aPolylinePoints3D.Value( i + 1 );
185         aNextPnt.SetZ( 0. );
186
187         gp_Vec anEdgeVec( aPnt, aNextPnt );
188
189         if ( i == 1 ) {
190           aDir = anEdgeVec;
191         } else {
192           gp_Vec aPrevVec( aPrevPnt, aPnt );
193           aDir = aPrevVec.Normalized() + anEdgeVec.Normalized();
194         }
195       } else {
196         aDir = gp_Vec( aPrevPnt, aPnt );
197       }
198             
199       aTangents.SetValue( i, aDir );
200     }
201   } else {
202     // Get curve from the first edge ( 2D )
203     TopTools_SequenceOfShape anEdges;
204     HYDROData_ShapesTool::ExploreShapeToShapes( aPolylineXY->GetShape(), TopAbs_EDGE, anEdges );
205     Standard_Real aStart, anEnd;
206
207     Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
208     GeomAPI_ProjectPointOnCurve aProject;
209
210     // Get tangents
211     for ( int i = 1; i <= aNbPoints; ++i ) {
212       gp_XYZ aPointToTest = aPolylinePoints3D.Value( i );
213       aPointToTest.SetZ( 0. );
214
215       aProject.Init( aPointToTest, aCurve );
216       Quantity_Parameter aParam = aProject.LowerDistanceParameter();
217       gp_Pnt aPnt;
218       gp_Vec aDir;
219       aCurve->D1( aParam, aPnt, aDir);
220
221       aTangents.SetValue( i, aDir );
222     }
223   }
224
225   // Get the profile middle point ( 3D )
226   gp_Pnt aMiddlePoint( theProfile->GetMiddlePoint( true ) );
227
228   // Translate the profile to each point on the guide line ( 3D )
229   Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints );
230   Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints );  
231   Handle(TopTools_HArray1OfShape) anArrOfProfiles = new TopTools_HArray1OfShape( 1, aNbPoints );
232
233   for ( int i = 1; i <= aNbPoints; ++i ) {
234     // Get point on the guide line
235     gp_Pnt aPointOnGuide( aPolylinePoints3D.Value( i ) );
236
237     // Define translation and rotation:
238     gp_Trsf Translation, Rotation;
239
240     // Translation
241     Translation.SetTranslation( aMiddlePoint, aPointOnGuide );
242     TopoDS_Wire aTransformedProfile = 
243       TopoDS::Wire( BRepBuilderAPI_Transform( aProfileWire, Translation, Standard_True ) );
244
245     // Rotation
246     gp_Vec aVertical( 0., 0., 1. );
247     TopoDS_Vertex aLeftVertex, aRightVertex;
248     TopExp::Vertices( aTransformedProfile, aLeftVertex, aRightVertex );
249     gp_Pnt aLeftPoint  = BRep_Tool::Pnt( aLeftVertex );
250     gp_Pnt aRightPoint = BRep_Tool::Pnt( aRightVertex );
251     gp_Vec aLeftToRight( aLeftPoint, aRightPoint);
252     gp_Vec NormalToProfile = aVertical ^ aLeftToRight;
253
254     gp_Vec aDir = aTangents.Value( i );
255     gp_Vec AxisOfRotation = NormalToProfile ^ aDir;
256     if (AxisOfRotation.Magnitude() <= gp::Resolution()) {
257       if ( aVertical * aLeftToRight < 0. ) {
258         gp_Ax1 theVertical(aPointOnGuide, gp::DZ() );
259         Rotation.SetRotation(theVertical, M_PI);
260       }
261     } else {
262       gp_Ax1 theAxis(aPointOnGuide, AxisOfRotation);
263       Standard_Real theAngle = NormalToProfile.AngleWithRef(aDir, AxisOfRotation);
264       Rotation.SetRotation(theAxis, theAngle);
265     }
266
267     aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform( aTransformedProfile, Rotation, Standard_True) );
268
269     // Get the first and the last points of the transformed profile
270     TopoDS_Vertex V1, V2;
271     TopExp::Vertices( aTransformedProfile, V1, V2 );
272
273     // Fill the data
274     anArrayOfFPnt->SetValue( i, BRep_Tool::Pnt( V1 ) );
275     anArrayOfLPnt->SetValue( i, BRep_Tool::Pnt( V2 ) );
276        
277     anArrOfProfiles->SetValue( i, aTransformedProfile );
278   }
279
280   // Create presentation
281   HYDROData_Stream::PrsDefinition aPrs;
282   Handle(TopTools_HArray1OfShape) anArrOf2DProfiles; // we don't need 2D profiles for channel/digue presentation
283
284   HYDROData_Stream::CreatePresentations( anArrayOfFPnt, anArrayOfLPnt, anArrOfProfiles, aPrs );
285   thePrs.myInlet =  aPrs.myInlet;       
286   thePrs.myOutlet =  aPrs.myOutlet; 
287   thePrs.myLeftBank = aPrs.myLeftBank; 
288   thePrs.myRightBank = aPrs.myRightBank;
289   thePrs.myPrs2D = TopoDS::Face(aPrs.myPrs2D); 
290   thePrs.myPrs3D = aPrs.myPrs3D; 
291
292   //thePrs.myPrs2D = TopoDS::Face( aPrs.myPrs2D );
293   //BRepBuilderAPI_MakeWire aMakeWire( aPrs.myLeftBank ) ;
294   //thePrs.myLeftBank = aMakeWire.Wire();
295   //aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myRightBank );
296   //thePrs.myRightBank = aMakeWire.Wire();
297   //aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myInlet );
298   //thePrs.myInlet = aMakeWire.Wire();
299   //aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myOutlet );
300   //thePrs.myOutlet = aMakeWire.Wire();
301
302   return true;
303 }
304
305 void HYDROData_Channel::Update()
306 {
307   HYDROData_ArtificialObject::Update();
308
309   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
310   Handle(HYDROData_Profile) aProfile = GetProfile();
311
312   PrsDefinition aResultPrs;
313   double anEquiDistance = GetEquiDistance();
314   if ( !CreatePresentations( aGuideLine, aProfile, aResultPrs, anEquiDistance ) )
315     return;
316
317   SetShape3D( aResultPrs.myPrs3D );
318   SetTopShape( aResultPrs.myPrs2D );
319
320   // Create groups for channel
321   TopTools_SequenceOfShape aLeftBankEdges;
322   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myLeftBank, TopAbs_EDGE, aLeftBankEdges );
323
324   TopTools_SequenceOfShape aRightBankEdges;
325   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myRightBank, TopAbs_EDGE, aRightBankEdges );
326
327   TopTools_SequenceOfShape anInletEdges;
328   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myInlet, TopAbs_EDGE, anInletEdges );
329
330   TopTools_SequenceOfShape anOutletEdges;
331   HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myOutlet, TopAbs_EDGE, anOutletEdges );
332     
333   RemoveGroupObjects();
334   QString aLeftGroupName = GetName() + "_Left_Bank";
335   Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
336   aLeftGroup->SetName( aLeftGroupName );
337   aLeftGroup->SetShapes( aLeftBankEdges );
338
339   QString aRightGroupName = GetName() + "_Right_Bank";
340
341   Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
342   aRightGroup->SetName( aRightGroupName );
343   aRightGroup->SetShapes( aRightBankEdges );
344
345   QString anInGroupName = GetName() + "_Inlet";
346
347   Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
348   anInGroup->SetName( anInGroupName );
349   anInGroup->SetShapes( anInletEdges );
350
351   QString anOutGroupName = GetName() + "_Outlet";
352
353   Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
354   anOutGroup->SetName( anOutGroupName );
355   anOutGroup->SetShapes( anOutletEdges );
356 }
357
358 bool HYDROData_Channel::IsHas2dPrs() const
359 {
360   return true;
361 }
362
363 QColor HYDROData_Channel::DefaultFillingColor() const
364 {
365   return QColor( Qt::blue );
366 }
367
368 QColor HYDROData_Channel::DefaultBorderColor() const
369 {
370   return QColor( Qt::transparent );
371 }
372
373 bool HYDROData_Channel::SetGuideLine( const Handle(HYDROData_Polyline3D)& theGuideLine )
374 {
375   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
376
377   if ( theGuideLine.IsNull() )
378   {
379     RemoveGuideLine();
380     return !aPrevGuideLine.IsNull();
381   }
382
383   if ( IsEqual( aPrevGuideLine, theGuideLine ) )
384     return false;
385
386   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theGuideLine->GetTopShape() );
387   if ( aHydraulicWire.IsNull() )
388     return false; // The polyline must be a single wire
389
390   SetReferenceObject( theGuideLine, DataTag_GuideLine );
391
392   // Indicate model of the need to update the chanel presentation
393   Changed( Geom_3d );
394
395   return true;
396 }
397
398 Handle(HYDROData_Polyline3D) HYDROData_Channel::GetGuideLine() const
399 {
400   return Handle(HYDROData_Polyline3D)::DownCast( 
401            GetReferenceObject( DataTag_GuideLine ) );
402 }
403
404 void HYDROData_Channel::RemoveGuideLine()
405 {
406   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
407   if ( aPrevGuideLine.IsNull() )
408     return;
409
410   ClearReferenceObjects( DataTag_GuideLine );
411
412   // Indicate model of the need to update the chanel presentation
413   Changed( Geom_3d );
414 }
415
416 bool HYDROData_Channel::SetProfile( const Handle(HYDROData_Profile)& theProfile )
417 {
418   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
419
420   if ( theProfile.IsNull() )
421   {
422     RemoveProfile();
423     return !aPrevProfile.IsNull();
424   }
425
426   if ( IsEqual( aPrevProfile, theProfile ) )
427     return false;
428
429   SetReferenceObject( theProfile, DataTag_Profile );
430
431   // Indicate model of the need to update the chanel presentation
432   Changed( Geom_3d );
433
434   return true;
435 }
436
437 Handle(HYDROData_Profile) HYDROData_Channel::GetProfile() const
438 {
439   return Handle(HYDROData_Profile)::DownCast( 
440            GetReferenceObject( DataTag_Profile ) );
441 }
442
443 void HYDROData_Channel::RemoveProfile()
444 {
445   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
446   if ( aPrevProfile.IsNull() )
447     return;
448
449   ClearReferenceObjects( DataTag_Profile );
450
451   // Indicate model of the need to update the chanel presentation
452   Changed( Geom_3d );
453 }
454
455 ObjectKind HYDROData_Channel::getAltitudeObjectType() const
456 {
457   //DEBTRACE("HYDROData_Channel::getAltitudeObjectType");
458   return KIND_CHANNEL_ALTITUDE;
459   //return KIND_STREAM_ALTITUDE;
460 }
461
462 TopoDS_Shape HYDROData_Channel::GetLeftShape() const
463 {
464   HYDROData_SequenceOfObjects aGroups = GetGroups();
465   return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 1);
466 }
467
468 TopoDS_Shape HYDROData_Channel::GetRightShape() const
469 {
470   HYDROData_SequenceOfObjects aGroups = GetGroups();
471   return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 2);
472 }
473
474 void HYDROData_Channel::SetEquiDistance( double theEquiDistance )
475 {
476   double anEquiDistance = theEquiDistance > 0 ? theEquiDistance : 1E-3;
477   SetDouble( DataTag_EquiDistance, theEquiDistance );
478 }
479
480 double HYDROData_Channel::GetEquiDistance() const
481 {
482   return GetDouble( DataTag_EquiDistance, 1.0 );
483 }