Salome HOME
6.12.2013.Fix of HasIntersection method.
[modules/hydro.git] / src / HYDROData / HYDROData_Channel.cxx
1
2 #include "HYDROData_Channel.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Polyline3D.h"
6 #include "HYDROData_Profile.h"
7
8 #include <TopoDS.hxx>
9 #include <TopoDS_Wire.hxx>
10 #include <BRepOffsetAPI_MakePipeShell.hxx>
11 #include <BRepCheck_Analyzer.hxx>
12 //#define DEB_CHANNEL 1
13 #ifdef DEB_CHANNEL
14 #include <BRepTools.hxx>
15 #endif
16
17 #include <QStringList>
18
19 #define PYTHON_CHANNEL_ID "KIND_CHANNEL"
20
21 IMPLEMENT_STANDARD_HANDLE(HYDROData_Channel,HYDROData_ArtificialObject)
22 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Channel,HYDROData_ArtificialObject)
23
24
25 HYDROData_Channel::HYDROData_Channel()
26 : HYDROData_ArtificialObject()
27 {
28 }
29
30 HYDROData_Channel::~HYDROData_Channel()
31 {
32 }
33
34 QStringList HYDROData_Channel::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
35 {
36   QStringList aResList;
37
38   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
39   if ( aDocument.IsNull() )
40     return aResList;
41
42   QString aDocName = aDocument->GetDocPyName();
43   QString aChannelName = GetName();
44
45   aResList << QString( "%1 = %2.CreateObject( %3 );" )
46               .arg( aChannelName ).arg( aDocName ).arg( PYTHON_CHANNEL_ID );
47   aResList << QString( "%1.SetName( \"%2\" );" )
48               .arg( aChannelName ).arg( aChannelName );
49   aResList << QString( "" );
50
51   // TODO
52
53   return aResList;
54 }
55
56 HYDROData_SequenceOfObjects HYDROData_Channel::GetAllReferenceObjects() const
57 {
58   HYDROData_SequenceOfObjects aResSeq = HYDROData_ArtificialObject::GetAllReferenceObjects();
59
60   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
61   if ( !aGuideLine.IsNull() )
62     aResSeq.Append( aGuideLine );
63
64   Handle(HYDROData_Profile) aProfile = GetProfile();
65   if ( !aProfile.IsNull() )
66     aResSeq.Append( aProfile );
67
68   return aResSeq;
69 }
70
71 TopoDS_Shape HYDROData_Channel::GetTopShape() const
72 {
73   return getTopShape();
74 }
75
76 TopoDS_Shape HYDROData_Channel::GetShape3D() const
77 {
78   return getShape3D();
79 }
80
81 void HYDROData_Channel::Update()
82 {
83   HYDROData_ArtificialObject::Update();
84
85   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
86   Handle(HYDROData_Profile) aProfile = GetProfile();
87   if ( aGuideLine.IsNull() || aProfile.IsNull() )
88     return;
89
90   // TODO
91   TopoDS_Wire aPathWire = TopoDS::Wire(aGuideLine->GetShape3D());
92   if(aPathWire.IsNull())
93     return;
94   TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetShape3D() ); 
95   if(aProfileWire.IsNull())
96         return;
97
98   BRepOffsetAPI_MakePipeShell aMkSweep(aPathWire);
99   aMkSweep.Add(aProfileWire,Standard_True, Standard_True);
100   aMkSweep.SetTransitionMode(BRepBuilderAPI_RightCorner);
101   aMkSweep.SetMode(Standard_True);
102   aMkSweep.Build();
103   if(aMkSweep.IsDone())  { 
104         const TopoDS_Shape& aChannel = aMkSweep.Shape();
105     BRepCheck_Analyzer aCheck(aChannel);
106     if(aCheck.IsValid()) 
107         {
108       //BRepTools::Write(aChannel, "ChanV.brep");  
109       SetShape3D( aMkSweep.Shape());
110         } else {
111 #ifdef DEB_CHANNEL
112           cout <<"NOT VALID" <<endl;
113           BRepTools::Write(aChannel, "ChanNV.brep");  
114 #endif
115         }
116   }
117 }
118
119 bool HYDROData_Channel::SetGuideLine( const Handle(HYDROData_Polyline3D)& theGuideLine )
120 {
121   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
122
123   if ( theGuideLine.IsNull() )
124   {
125     RemoveGuideLine();
126     return !aPrevGuideLine.IsNull();
127   }
128
129   if ( IsEqual( aPrevGuideLine, theGuideLine ) )
130     return false;
131
132   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theGuideLine->GetTopShape() );
133   if ( aHydraulicWire.IsNull() )
134     return false; // The polyline must be a single wire
135
136   SetReferenceObject( theGuideLine, DataTag_GuideLine );
137
138   // Indicate model of the need to update the chanel presentation
139   SetToUpdate( true );
140
141   return true;
142 }
143
144 Handle(HYDROData_Polyline3D) HYDROData_Channel::GetGuideLine() const
145 {
146   return Handle(HYDROData_Polyline3D)::DownCast( 
147            GetReferenceObject( DataTag_GuideLine ) );
148 }
149
150 void HYDROData_Channel::RemoveGuideLine()
151 {
152   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
153   if ( aPrevGuideLine.IsNull() )
154     return;
155
156   ClearReferenceObjects( DataTag_GuideLine );
157
158   // Indicate model of the need to update the chanel presentation
159   SetToUpdate( true );
160 }
161
162 bool HYDROData_Channel::SetProfile( const Handle(HYDROData_Profile)& theProfile )
163 {
164   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
165
166   if ( theProfile.IsNull() )
167   {
168     RemoveProfile();
169     return !aPrevProfile.IsNull();
170   }
171
172   if ( IsEqual( aPrevProfile, theProfile ) )
173     return false;
174
175   SetReferenceObject( theProfile, DataTag_Profile );
176
177   // Indicate model of the need to update the chanel presentation
178   SetToUpdate( true );
179
180   return true;
181 }
182
183 Handle(HYDROData_Profile) HYDROData_Channel::GetProfile() const
184 {
185   return Handle(HYDROData_Profile)::DownCast( 
186            GetReferenceObject( DataTag_Profile ) );
187 }
188
189 void HYDROData_Channel::RemoveProfile()
190 {
191   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
192   if ( aPrevProfile.IsNull() )
193     return;
194
195   ClearReferenceObjects( DataTag_Profile );
196
197   // Indicate model of the need to update the chanel presentation
198   SetToUpdate( true );
199 }