Salome HOME
Preview was added to Polyline operation
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_AISCurve.cxx
1 #include "HYDROGUI_AISCurve.h"\r
2 #include "CurveCreator_Curve.hxx"\r
3 \r
4 #include <AIS_Point.hxx>\r
5 #include <AIS_Line.hxx>\r
6 #include <gp_Pnt.hxx>\r
7 #include <gp_Lin.hxx>\r
8 #include <Geom_CartesianPoint.hxx>\r
9 \r
10 HYDROGUI_AISCurveSection::HYDROGUI_AISCurveSection( Handle_AIS_InteractiveContext theContext, \r
11                                                    CurveCreator_Curve* theCurve, int theSection) :\r
12   myCurve(theCurve), mySection(theSection), myContext(theContext), myIsHL(false)\r
13 {\r
14   buildSection();\r
15 }\r
16 \r
17 HYDROGUI_AISCurveSection::~HYDROGUI_AISCurveSection()\r
18 {\r
19   Erase();\r
20   for( int i = 0 ; i < myObjects.size() ; i++ ){\r
21     myObjects[i].Nullify();\r
22   }\r
23   myObjects.clear();\r
24 }\r
25 \r
26 Quantity_Color HYDROGUI_AISCurveSection::getActiveColor()\r
27 {\r
28   if( myIsHL ){\r
29     return Quantity_Color( 1., 0., 0., Quantity_TOC_RGB );\r
30   }\r
31   return Quantity_Color( 0., 1., 0., Quantity_TOC_RGB );\r
32 }\r
33 \r
34 void  HYDROGUI_AISCurveSection::highlight( bool isHL )\r
35 {\r
36   myIsHL = isHL;\r
37   Quantity_Color aColor = getActiveColor();\r
38   for( int i = 0 ; i < myObjects.size() ; i++ ){\r
39     myObjects[i]->SetColor(aColor);\r
40     myContext->Display(myObjects[i]);\r
41   }\r
42 }\r
43 \r
44 void HYDROGUI_AISCurveSection::Display()\r
45 {\r
46   for( int i = 0 ; i < myObjects.size() ; i++ ){\r
47     myContext->Display(myObjects[i]);\r
48   }\r
49 }\r
50 \r
51 void HYDROGUI_AISCurveSection::Erase()\r
52 {\r
53   for( int i = 0 ; i < myObjects.size() ; i++ ){\r
54     myContext->Erase(myObjects[i]);\r
55   }\r
56 }\r
57 \r
58 void HYDROGUI_AISCurveSection::buildSection()\r
59 {\r
60   int aSectSize = myCurve->getNbPoints( mySection );\r
61   double anX;\r
62   double anY;\r
63   double aZ;\r
64   int i = 0; \r
65   for( ; i < ( aSectSize - 1 ) ; i++ ){\r
66     Handle_AIS_Point anAISPnt = getAISPoint(i);\r
67     myObjects.push_back( anAISPnt );\r
68     Handle_AIS_Line aLine = getAISLine( i, i+1 );\r
69     myObjects.push_back( aLine );\r
70   }\r
71   if( aSectSize != 0 ){\r
72     Handle_AIS_Point anAISPnt = getAISPoint(i); \r
73     myObjects.push_back( anAISPnt );\r
74     if( myCurve->isClosed(mySection) && ( aSectSize > 1 ) ){\r
75       Handle_AIS_Line aLine = getAISLine( i, 0 );\r
76       myObjects.push_back( aLine );\r
77     }\r
78   }\r
79 }\r
80 \r
81 Handle_AIS_Point HYDROGUI_AISCurveSection::getAISPoint( int theIndx )\r
82 {\r
83   double anX;\r
84   double anY;\r
85   double aZ;\r
86   getPoint( theIndx, anX, anY, aZ );\r
87   gp_Pnt aPoint( anX, anY, aZ);\r
88   AIS_Point* aPnt = new AIS_Point( new Geom_CartesianPoint(aPoint));\r
89   return aPnt;\r
90 }\r
91 \r
92 Handle_AIS_Line HYDROGUI_AISCurveSection::getAISLine( int theIndx1, int theIndx2 )\r
93 {\r
94   double anX;\r
95   double anY;\r
96   double aZ;\r
97   getPoint( theIndx1, anX, anY, aZ );\r
98   gp_Pnt aPoint1( anX, anY, aZ);\r
99   double anX2;\r
100   double anY2;\r
101   double aZ2;\r
102   getPoint( theIndx2, anX2, anY2, aZ2 );\r
103 //MTN to avoid crash during line construction\r
104   if( ( anX == anX2 ) && ( anY == anY2 ) && (aZ == aZ2 ) ){\r
105     aZ2 += 1e-7;\r
106   }\r
107   gp_Pnt aPoint2( anX2, anY2, aZ2 );\r
108   AIS_Line* aLine = new AIS_Line( new Geom_CartesianPoint(aPoint1), new Geom_CartesianPoint(aPoint2) );\r
109   return aLine;\r
110 }\r
111 \r
112 void HYDROGUI_AISCurveSection::getPoint( int theIndx, double& theX, double& theY, double& theZ )\r
113 {\r
114   CurveCreator::Dimension aDim = myCurve->getDimension();\r
115   CurveCreator::Coordinates aCoords = myCurve->getCoordinates( mySection, theIndx );\r
116   theX = aCoords[0];\r
117   theY = aCoords[1];\r
118   theZ = 0.;\r
119   if( aDim == CurveCreator::Dim3d ){\r
120     theZ = aCoords[2];\r
121   }\r
122 }\r
123 \r
124 /******************************* HYDROGUI_AISCurve ********************************************/\r
125 HYDROGUI_AISCurve::HYDROGUI_AISCurve( CurveCreator_Curve* theCurve, Handle_AIS_InteractiveContext theContext ) :\r
126   CurveCreator_Listener(), myCurve(theCurve), myContext( theContext )\r
127 {\r
128   myCurve->setListener(this);\r
129   buildCurve();\r
130 }\r
131 \r
132 HYDROGUI_AISCurve::~HYDROGUI_AISCurve(void)\r
133 {\r
134 }\r
135 \r
136 void HYDROGUI_AISCurve::setCurve( CurveCreator_Curve* theCurve )\r
137 {\r
138   myCurve = theCurve;\r
139   buildCurve();\r
140 }\r
141 \r
142 void HYDROGUI_AISCurve::Display()\r
143 {\r
144   for( int i = 0 ; i < myCurveRepresentation.size() ; i++ ){\r
145     myCurveRepresentation[i]->Display();\r
146   }\r
147 }\r
148 \r
149 void HYDROGUI_AISCurve::buildCurve()\r
150 {\r
151   for( int i = 0 ; i < myCurveRepresentation.size() ; i++ ){\r
152     myCurveRepresentation[i]->Erase();\r
153     delete myCurveRepresentation[i];\r
154   }\r
155   myCurveRepresentation.clear();\r
156   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){\r
157     HYDROGUI_AISCurveSection* aSection = new HYDROGUI_AISCurveSection( myContext, myCurve, i);\r
158     myCurveRepresentation.push_back( aSection );\r
159     myCurveRepresentation[i]->Display();\r
160   }\r
161 }\r
162 \r
163 void HYDROGUI_AISCurve::pointInserted( int theSection, int theIndx )\r
164 {\r
165   buildCurve();\r
166 }\r
167 \r
168 void HYDROGUI_AISCurve::highlightSection( int theSection, bool isHL )\r
169 {\r
170   if( theSection >= myCurveRepresentation.size() )\r
171     return;\r
172   myCurveRepresentation[theSection]->highlight(isHL);\r
173 }\r