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