Salome HOME
Fix for the bug #56: Show, Show only, Hide for Region and Zone under Region.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_AISCurve.cxx
1 #include "HYDROGUI_AISCurve.h"
2
3 #include <HYDROData_BSplineOperation.h>
4
5 #include <CurveCreator.hxx>
6 #include <CurveCreator_Curve.hxx>
7
8 #include <AIS_Point.hxx>
9 #include <AIS_Line.hxx>
10 #include <AIS_Shape.hxx>
11 #include <BRepBuilderAPI_MakeEdge.hxx>
12 #include <BRepBuilderAPI_MakeWire.hxx>
13 #include <Geom_CartesianPoint.hxx>
14 #include <gp_Pnt.hxx>
15 #include <gp_Lin.hxx>
16 #include <TopoDS_Edge.hxx>
17 #include <TopoDS_Face.hxx>
18 #include <TopoDS_Wire.hxx>
19
20 HYDROGUI_AISCurveSection::HYDROGUI_AISCurveSection( Handle_AIS_InteractiveContext theContext, 
21                                                    CurveCreator_Curve* theCurve, int theSection) :
22   myCurve(theCurve), mySection(theSection), myContext(theContext), myIsHL(false)
23 {
24   buildSection();
25 }
26
27 HYDROGUI_AISCurveSection::~HYDROGUI_AISCurveSection()
28 {
29   Erase();
30   for( int i = 0 ; i < myObjects.size() ; i++ ){
31     myObjects[i].Nullify();
32   }
33   myObjects.clear();
34 }
35
36 Quantity_Color HYDROGUI_AISCurveSection::getActiveColor()
37 {
38   if( myIsHL ){
39     return Quantity_Color( 1., 0., 0., Quantity_TOC_RGB );
40   }
41   return Quantity_Color( 0., 1., 0., Quantity_TOC_RGB );
42 }
43
44 void  HYDROGUI_AISCurveSection::highlight( bool isHL )
45 {
46   myIsHL = isHL;
47   Quantity_Color aColor = getActiveColor();
48   for( int i = 0 ; i < myObjects.size() ; i++ ){
49     myObjects[i]->SetColor(aColor);
50     myContext->Display(myObjects[i], Standard_False);
51   }
52   myContext->UpdateCurrentViewer();
53 }
54
55 void HYDROGUI_AISCurveSection::Display()
56 {
57   for( int i = 0 ; i < myObjects.size() ; i++ ){
58     myContext->Display(myObjects[i], Standard_False);
59   }
60   myContext->UpdateCurrentViewer();
61 }
62
63 void HYDROGUI_AISCurveSection::Erase()
64 {
65   for( int i = 0 ; i < myObjects.size() ; i++ ){
66     myContext->Erase(myObjects[i], Standard_False);
67   }
68   myContext->UpdateCurrentViewer();
69 }
70
71 void HYDROGUI_AISCurveSection::buildSection()
72 {
73   CurveCreator::SectionType aSectType = myCurve->getSectionType( mySection );
74   int aSectSize = myCurve->getNbPoints( mySection );
75   bool aSectIsClosed = myCurve->isClosed( mySection );
76
77   if( aSectType == CurveCreator::Polyline )
78   {
79     int i = 0; 
80     for( ; i < ( aSectSize - 1 ) ; i++ ){
81       Handle_AIS_Point anAISPnt = getAISPoint(i);
82       myObjects.push_back( anAISPnt );
83       Handle_AIS_Line aLine = getAISLine( i, i+1 );
84       myObjects.push_back( aLine );
85     }
86     if( aSectSize != 0 ){
87       Handle_AIS_Point anAISPnt = getAISPoint(i); 
88       myObjects.push_back( anAISPnt );
89       if( myCurve->isClosed(mySection) && ( aSectSize > 1 ) ){
90         Handle_AIS_Line aLine = getAISLine( i, 0 );
91         myObjects.push_back( aLine );
92       }
93     }
94   }
95   else if( aSectType == CurveCreator::Spline )
96   {
97     QList<double> aPoints;
98     for( int i = 0; i < aSectSize; i++ )
99     {
100       Handle_AIS_Point anAISPnt = getAISPoint( i );
101       myObjects.push_back( anAISPnt );
102
103       double aX = 0, aY = 0, aZ = 0;
104       getPoint( i, aX, aY, aZ );
105       aPoints << aX << aY;
106     }
107
108     if( aSectSize > 1 )
109     {
110       HYDROData_BSplineOperation aBSpline( aPoints, 0, aSectIsClosed );
111       TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
112
113       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
114
115       Handle(AIS_Shape) aShape = new AIS_Shape( aWire );
116       myObjects.push_back( aShape );
117     }
118   }
119 }
120
121 Handle_AIS_Point HYDROGUI_AISCurveSection::getAISPoint( int theIndx )
122 {
123   double anX;
124   double anY;
125   double aZ;
126   getPoint( theIndx, anX, anY, aZ );
127   gp_Pnt aPoint( anX, anY, aZ);
128
129   AIS_Point* aPnt = new AIS_Point( new Geom_CartesianPoint(aPoint));
130   return aPnt;
131 }
132
133 Handle_AIS_Line HYDROGUI_AISCurveSection::getAISLine( int theIndx1, int theIndx2 )
134 {
135   double anX;
136   double anY;
137   double aZ;
138   getPoint( theIndx1, anX, anY, aZ );
139   gp_Pnt aPoint1( anX, anY, aZ);
140
141   double anX2;
142   double anY2;
143   double aZ2;
144   getPoint( theIndx2, anX2, anY2, aZ2 );
145 //MTN to avoid crash during line construction
146   if( ( anX == anX2 ) && ( anY == anY2 ) && (aZ == aZ2 ) ){
147     aZ2 += 1e-7;
148   }
149
150   gp_Pnt aPoint2( anX2, anY2, aZ2 );
151
152   AIS_Line* aLine = new AIS_Line( new Geom_CartesianPoint(aPoint1), new Geom_CartesianPoint(aPoint2) );
153   return aLine;
154 }
155
156 void HYDROGUI_AISCurveSection::getPoint( int theIndx, double& theX, double& theY, double& theZ )
157 {
158   CurveCreator::Dimension aDim = myCurve->getDimension();
159   const CurveCreator::Coordinates aCoords = myCurve->getPoint( mySection, theIndx );
160   theX = aCoords[0];
161   theY = aCoords[1];
162   theZ = 0.;
163   if( aDim == CurveCreator::Dim3d ){
164     theZ = aCoords[2];
165   }
166 }
167
168 /******************************* HYDROGUI_AISCurve ********************************************/
169 HYDROGUI_AISCurve::HYDROGUI_AISCurve( CurveCreator_Curve* theCurve, Handle_AIS_InteractiveContext theContext ) :
170   CurveCreator_Listener(), myCurve(theCurve), myContext( theContext )
171 {
172   myCurve->setListener(this);
173   buildCurve();
174 }
175
176 HYDROGUI_AISCurve::~HYDROGUI_AISCurve(void)
177 {
178   myCurve->removeListener();
179 }
180
181 void HYDROGUI_AISCurve::setCurve( CurveCreator_Curve* theCurve )
182 {
183   myCurve = theCurve;
184   buildCurve();
185 }
186
187 void HYDROGUI_AISCurve::Display()
188 {
189   for( int i = 0 ; i < myCurveRepresentation.size() ; i++ ){
190     myCurveRepresentation[i]->Display();
191   }
192 }
193
194 void HYDROGUI_AISCurve::Erase()
195 {
196   for( int i = 0 ; i < myCurveRepresentation.size() ; i++ ){
197     myCurveRepresentation[i]->Erase();
198   }
199 }
200
201 void HYDROGUI_AISCurve::buildCurve()
202 {
203   for( int i = 0 ; i < myCurveRepresentation.size() ; i++ ){
204     myCurveRepresentation[i]->Erase();
205     delete myCurveRepresentation[i];
206   }
207   myCurveRepresentation.clear();
208
209   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
210     HYDROGUI_AISCurveSection* aSection = new HYDROGUI_AISCurveSection( myContext, myCurve, i);
211     myCurveRepresentation.push_back( aSection );
212     myCurveRepresentation[i]->Display();
213   }
214 }
215
216 void HYDROGUI_AISCurve::pointChanged( int theSection, int thePoint )
217 {
218   buildCurve();
219 }
220
221 void HYDROGUI_AISCurve::pointInserted( int theSection, int theIndx )
222 {
223   buildCurve();
224 }
225
226 void HYDROGUI_AISCurve::highlightSection( int theSection, bool isHL )
227 {
228   if( theSection >= myCurveRepresentation.size() )
229     return;
230   myCurveRepresentation[theSection]->highlight(isHL);
231 }