Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / BasicGUI / BasicGUI.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : BasicGUI.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header: 
28
29 using namespace std;
30 #include "BasicGUI.h"
31
32 #include "QAD_RightFrame.h"
33 #include "OCCViewer_Viewer3d.h"
34 #include "OCCViewer_ViewPort3d.h"
35 #include "SALOMEGUI_QtCatchCorbaException.hxx"
36
37 #include <Precision.hxx>
38 #include <BRep_Tool.hxx>
39 #include <ProjLib.hxx>
40 #include <ElSLib.hxx>
41
42 #include "BasicGUI_PointDlg.h"        // Method POINT
43 #include "BasicGUI_LineDlg.h"         // Method LINE
44 #include "BasicGUI_CircleDlg.h"       // Method CIRCLE
45 #include "BasicGUI_EllipseDlg.h"      // Method ELLIPSE
46 #include "BasicGUI_ArcDlg.h"          // Method ARC
47 #include "BasicGUI_VectorDlg.h"       // Method VECTOR
48 #include "BasicGUI_PlaneDlg.h"        // Method PLANE
49 #include "BasicGUI_WorkingPlaneDlg.h" // Method WORKING PLANE
50
51 //=======================================================================
52 // function : BasicGUI()
53 // purpose  : Constructor
54 //=======================================================================
55 BasicGUI::BasicGUI() :
56   QObject()
57 {
58   myGeomBase = new GEOMBase();
59   myGeomGUI = GEOMContext::GetGeomGUI();
60   myGeom = myGeomGUI->myComponentGeom;
61 }
62
63
64 //=======================================================================
65 // function : ~BasicGUI()
66 // purpose  : Destructor
67 //=======================================================================
68 BasicGUI::~BasicGUI()
69 {
70 }
71
72
73 //=======================================================================
74 // function : OnGUIEvent()
75 // purpose  : 
76 //=======================================================================
77 bool BasicGUI::OnGUIEvent(int theCommandID, QAD_Desktop* parent)
78 {
79   BasicGUI* myBasicGUI = new BasicGUI();
80   myBasicGUI->myGeomGUI->EmitSignalDeactivateDialog();
81   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
82
83   switch (theCommandID)
84     {
85     case 4011: // POINT
86       {
87         Handle(AIS_InteractiveContext) ic;
88         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
89           OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
90           ic = v3d->getAISContext();
91         }
92         BasicGUI_PointDlg *aDlg = new BasicGUI_PointDlg(parent, "", myBasicGUI, Sel, ic); 
93         break;
94       }
95     case 4012:  // LINE
96       {
97         BasicGUI_LineDlg *aDlg = new BasicGUI_LineDlg(parent, "", myBasicGUI, Sel);
98         break;
99       }
100     case 4013:  // CIRCLE
101       {
102         BasicGUI_CircleDlg *aDlg = new BasicGUI_CircleDlg(parent, "", myBasicGUI, Sel);
103         break;
104       }
105     case 4014:  // ELLIPSE
106       {
107         BasicGUI_EllipseDlg *aDlg = new BasicGUI_EllipseDlg(parent, "", myBasicGUI, Sel);
108         break;
109       }
110     case 4015:  // ARC
111       {
112         BasicGUI_ArcDlg *aDlg = new BasicGUI_ArcDlg(parent, "", myBasicGUI, Sel);
113         break ;
114       }
115     case 4016: // VECTOR
116       { 
117         BasicGUI_VectorDlg *aDlg = new BasicGUI_VectorDlg(parent, "", myBasicGUI, Sel);
118         break;
119       }
120     case 4017: // PLANE
121       { 
122         BasicGUI_PlaneDlg *aDlg = new BasicGUI_PlaneDlg(parent, "", myBasicGUI, Sel);
123         break;
124       }
125     case 4018: // WORKING PLANE
126       { 
127         BasicGUI_WorkingPlaneDlg *aDlg = new BasicGUI_WorkingPlaneDlg(parent, "", myBasicGUI, Sel);
128         break;
129       }
130     default:
131       {
132         parent->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
133         break;
134       }
135     }
136   return true;
137 }
138
139
140 //=================================================================================
141 // function : 0nMousePress()
142 // purpose  : [static] manage mouse events
143 //=================================================================================
144 bool BasicGUI::OnMousePress(QMouseEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
145 {
146   BasicGUI* myBasicGUI = new BasicGUI();
147
148   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
149     return false;
150
151   OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
152   Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
153   OCCViewer_ViewPort* vp = ((OCCViewer_ViewFrame*)studyFrame->getRightFrame()->getViewFrame())->getViewPort();
154   
155   /* Get the clicked or selected point */
156   gp_Pnt thePoint;
157
158   myBasicGUI->myGeomBase->EraseSimulationShape();
159   BasicGUI_PointDlg *DialogPt = (BasicGUI_PointDlg*)(myBasicGUI->myGeomGUI->GetActiveDialogBox());
160
161   if(DialogPt->UseLocalContext()) {
162     ic->InitSelected();
163     if(pe->state() == Qt::ShiftButton)
164       v3d->getAISSelector()->shiftSelect();  /* Append selection */
165     else
166       v3d->getAISSelector()->select();       /* New selection    */
167       
168     ic->InitSelected();
169     if(ic->MoreSelected())
170       thePoint = BRep_Tool::Pnt(TopoDS::Vertex(ic->SelectedShape()));
171     else
172       thePoint = myBasicGUI->ConvertClickToPoint(pe->x(), pe->y(), ((OCCViewer_ViewPort3d*)vp)->getView());
173   } 
174   else
175     thePoint = myBasicGUI->ConvertClickToPoint(pe->x(), pe->y(), ((OCCViewer_ViewPort3d*)vp)->getView());
176
177   if(DialogPt != 0)
178     DialogPt->PointIntoCoordinates(thePoint, true);  /* display point */
179   else
180     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
181
182   return false;
183 }
184
185
186 //=======================================================================
187 // function : ConvertClickToPoint()
188 // purpose  : Returns the point clicked in 3D view
189 //=======================================================================
190 gp_Pnt BasicGUI::ConvertClickToPoint(Standard_Real x, Standard_Real y, Handle(V3d_View) aView)
191 {
192   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
193   aView->Eye(XEye, YEye, ZEye);
194
195   aView->At(XAt, YAt, ZAt);
196   gp_Pnt EyePoint(XEye, YEye, ZEye);
197   gp_Pnt AtPoint(XAt, YAt, ZAt);
198
199   gp_Vec EyeVector(EyePoint, AtPoint);
200   gp_Dir EyeDir(EyeVector);
201
202   gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir);
203   Standard_Real X, Y, Z;
204   aView->Convert(int(x), int(y), X, Y, Z);
205   gp_Pnt ConvertedPoint(X, Y, Z);
206
207   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
208   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
209   return ResultPoint;
210 }
211
212
213 //=======================================================================
214 // function : MakePointAndDisplay
215 // purpose  : 
216 //=======================================================================
217 void BasicGUI::MakePointAndDisplay(const double x, const double y, const double z) 
218 {
219   try {
220     GEOM::GEOM_Shape_var P = myGeom->MakeVertex(x, y, z);
221     P->NameType(tr("GEOM_VERTEX"));
222     if (myGeomBase->Display(P))
223       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
224   }
225   catch(const SALOME::SALOME_Exception& S_ex) {
226     QtCatchCorbaException(S_ex);
227   }
228   return;
229 }
230
231
232 //=====================================================================================
233 // function : MakeLineAndDisplay()
234 // purpose  : Create an infinite oriented line (linear edge in fact) 
235 //=====================================================================================
236 void BasicGUI::MakeLineAndDisplay(const gp_Pnt InitPoint, const gp_Pnt LastPoint)
237 {
238   gp_Pnt P1, P2;  
239   double dx, dy, dz;
240   myGeomBase->GetBipointDxDyDz(InitPoint, LastPoint, dx, dy, dz);
241   Standard_Real length = InitPoint.Distance(LastPoint);
242   if(length <= Precision::Confusion()) {
243     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
244     return;
245   }
246   
247   Standard_Real coeff = 1E6 / length;
248   
249   /* To create a line with length = 1E6     */
250   /* Precision::Infinite() is 1E100 in OCC  */
251   P1.SetX(InitPoint.X() - (coeff * dx));
252   P1.SetY(InitPoint.Y() - (coeff * dy));
253   P1.SetZ(InitPoint.Z() - (coeff * dz));
254   
255   P2.SetX(LastPoint.X() + (coeff * dx));
256   P2.SetY(LastPoint.Y() + (coeff * dy));
257   P2.SetZ(LastPoint.Z() + (coeff * dz));
258   
259   try {
260     GEOM::PointStruct pstruct = myGeom->MakePointStruct(P1.X(), P1.Y(), P1.Z());
261     GEOM::PointStruct d = myGeom->MakePointStruct(P2.X(), P2.Y(), P2.Z());
262     GEOM::DirStruct dstruct = myGeom->MakeDirection(d);
263     
264     GEOM::GEOM_Shape_ptr result = myGeom->MakeLine(pstruct, dstruct);
265     if(result->_is_nil()) {
266       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
267       return;
268     }
269     result->NameType(tr("GEOM_LINE"));
270     
271     if(myGeomBase->Display(result))
272       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
273   }
274   catch(const SALOME::SALOME_Exception& S_ex) {
275     QtCatchCorbaException(S_ex);
276   }
277   return;
278 }
279
280
281 //=====================================================================================
282 // function : MakeCircleAndDisplay()
283 // purpose  :
284 //=====================================================================================
285 void BasicGUI::MakeCircleAndDisplay(const gp_Pnt CenterPoint, const gp_Dir dir, const Standard_Real Radius)
286 {
287   try {
288     GEOM::PointStruct pstruct = myGeom->MakePointStruct(CenterPoint.X(), CenterPoint.Y(), CenterPoint.Z());
289     GEOM::PointStruct d = myGeom->MakePointStruct(dir.X(), dir.Y(), dir.Z());
290     GEOM::DirStruct dstruct = myGeom->MakeDirection(d);
291     
292     GEOM::GEOM_Shape_var result = myGeom->MakeCircle(pstruct, dstruct, Radius);
293     if(result->_is_nil()) {
294       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
295       return;
296     }
297     result->NameType(tr("GEOM_CIRCLE"));
298     if(myGeomBase->Display(result))
299       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
300   }
301   catch(const SALOME::SALOME_Exception& S_ex) {
302     QtCatchCorbaException(S_ex);
303   }
304   return;
305 }
306
307
308 //=====================================================================================
309 // function : MakeEllipseAndDisplay()
310 // purpose  :
311 //=====================================================================================
312 void BasicGUI::MakeEllipseAndDisplay(const gp_Pnt CenterPoint, const gp_Dir dir,
313                                      const Standard_Real Major_Radius, const Standard_Real Minor_Radius)
314 {
315   try {
316     GEOM::PointStruct pstruct = myGeom->MakePointStruct(CenterPoint.X(), CenterPoint.Y(), CenterPoint.Z());
317     GEOM::PointStruct d = myGeom->MakePointStruct(dir.X(), dir.Y(), dir.Z());
318     GEOM::DirStruct dstruct = myGeom->MakeDirection(d) ;
319
320     GEOM::GEOM_Shape_var result = myGeom->MakeEllipse(pstruct, dstruct, Major_Radius, Minor_Radius);
321     if(result->_is_nil()) {
322       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
323       return;
324     }
325     result->NameType(tr("GEOM_ELLIPSE"));
326     if(myGeomBase->Display(result))
327       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
328   }
329   catch(const SALOME::SALOME_Exception& S_ex) {
330     QtCatchCorbaException(S_ex);
331   }
332   return;
333 }
334
335
336 //=======================================================================================
337 // function : MakeArcAndDisplay()
338 // purpose  : Make an arc of circle from InitPoint to CirclePoint and passing on EndPoint
339 //=======================================================================================
340 void BasicGUI::MakeArcAndDisplay(gp_Pnt InitPoint, gp_Pnt CirclePoint, gp_Pnt EndPoint)
341 {
342   gp_Vec v1(CirclePoint, InitPoint);
343   gp_Vec v2(CirclePoint, EndPoint);
344   if(v1.IsParallel(v2, Precision::Angular()))
345     return;
346
347   try { 
348     GEOM::PointStruct pI = myGeom->MakePointStruct(InitPoint.X(), InitPoint.Y(), InitPoint.Z());
349     GEOM::PointStruct pC = myGeom->MakePointStruct(CirclePoint.X(), CirclePoint.Y(), CirclePoint.Z());
350     GEOM::PointStruct pE = myGeom->MakePointStruct(EndPoint.X(), EndPoint.Y(), EndPoint.Z());
351     GEOM::GEOM_Shape_var result = myGeom->MakeArc(pI, pC, pE);
352     if(result->_is_nil()) {
353       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
354       return;
355     }
356     result->NameType(tr("GEOM_ARC"));
357     if (myGeomBase->Display(result))
358       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
359   }
360   catch(const SALOME::SALOME_Exception& S_ex) {
361     QtCatchCorbaException(S_ex);
362   }    
363   return;
364 }
365
366
367 //=======================================================================
368 // function : MakeVectorAndDisplay()
369 // purpose  : 
370 //=======================================================================
371 void BasicGUI::MakeVectorAndDisplay(const gp_Pnt P1, const gp_Pnt P2)
372 {
373   try {
374     GEOM::PointStruct pstruct1 = myGeom->MakePointStruct(P1.X(), P1.Y(), P1.Z());
375     GEOM::PointStruct pstruct2 = myGeom->MakePointStruct(P2.X(), P2.Y(), P2.Z());
376     GEOM::GEOM_Shape_var Vector = myGeom->MakeVector(pstruct1, pstruct2);
377     Vector->NameType(tr("GEOM_VECTOR"));
378     if(myGeomBase->Display(Vector))
379       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
380   }
381   catch(const SALOME::SALOME_Exception& S_ex) {
382     QtCatchCorbaException(S_ex);
383   }
384   return;
385 }
386
387
388 //=======================================================================
389 // function : MakePlaneAndDisplay()
390 // purpose  : Plane point is P1 and dx, dy, dz define a normal vector
391 //=======================================================================
392 void BasicGUI::MakePlaneAndDisplay(const gp_Pnt P1, const Standard_Real dx, const Standard_Real dy,
393                                    const Standard_Real dz, const Standard_Real TrimSize)
394 {
395   try {
396     gp_Dir aDirection;
397     aDirection.SetCoord(dx, dy, dz);
398     gp_Ax2 Axis(P1, aDirection);
399     
400     GEOM::PointStruct pstruct = myGeom->MakePointStruct(P1.X(), P1.Y(), P1.Z());
401     GEOM::PointStruct d = myGeom->MakePointStruct(aDirection.X(), aDirection.Y(), aDirection.Z());
402     GEOM::DirStruct dstruct = myGeom->MakeDirection(d);
403     GEOM::GEOM_Shape_ptr plane = myGeom->MakePlane(pstruct, dstruct, TrimSize);
404     plane->NameType(tr("GEOM_PLANE"));
405     if(myGeomBase->Display(plane))
406       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
407   }
408   catch(const SALOME::SALOME_Exception& S_ex) {
409     QtCatchCorbaException(S_ex);
410   }
411   return;
412 }
413
414
415 //=======================================================================
416 // function : MakeWorkingPlane()
417 // purpose  : Change the point of view3d
418 //=======================================================================
419 void BasicGUI::MakeWorkingPlane(const gp_Pnt P, const gp_Dir D)
420 {
421   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_OCC) {
422     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_NOT_FOR_VTK_VIEWER"));
423     return;
424   }
425
426   OCCViewer_ViewPort* vp = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewPort();
427   Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)vp)->getView();
428   view3d->SetAt(P.X(), P.Y(), P.Z());
429   view3d->SetProj(D.X(), D.Y(), D.Z());
430   QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
431   return;
432 }
433
434
435 //=====================================================================================
436 // EXPORTED METHODS
437 //=====================================================================================
438 extern "C"
439 {
440   bool OnGUIEvent(int theCommandID, QAD_Desktop* parent)
441   {return BasicGUI::OnGUIEvent(theCommandID, parent);}
442
443   bool OnMousePress(QMouseEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
444   {return BasicGUI::OnMousePress(pe, parent, studyFrame);}
445 }