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