Salome HOME
842a5a12fa361570a6226fca1b60f48e5b644eb3
[modules/geom.git] / src / OperationGUI / OperationGUI.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   : OperationGUI.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header: 
28
29 using namespace std;
30 #include "OperationGUI.h"
31
32 #include "DisplayGUI.h"
33
34 #include "QAD_RightFrame.h"
35 #include "OCCViewer_Viewer3d.h"
36 #include "OCCViewer_ViewFrame.h"
37 #include "SALOMEGUI_QtCatchCorbaException.hxx"
38
39 #include <TopTools_MapOfShape.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <Precision.hxx>
42
43 #include "OperationGUI_PartitionDlg.h"   // Method PARTITION
44 #include "OperationGUI_ArchimedeDlg.h"   // Method ARCHIMEDE
45 #include "OperationGUI_FilletDlg.h"      // Method FILLET
46 #include "OperationGUI_ChamferDlg.h"     // Method CHAMFER
47
48 //=======================================================================
49 // function : OperationGUI()
50 // purpose  : Constructor
51 //=======================================================================
52 OperationGUI::OperationGUI() :
53   QObject()
54 {
55   myGeomBase = new GEOMBase();
56   myGeomGUI = GEOMContext::GetGeomGUI();
57   myGeom = myGeomGUI->myComponentGeom;
58 }
59
60
61 //=======================================================================
62 // function : ~OperationGUI()
63 // purpose  : Destructor
64 //=======================================================================
65 OperationGUI::~OperationGUI()
66 {
67 }
68
69
70 //=======================================================================
71 // function : OnGUIEvent()
72 // purpose  : 
73 //=======================================================================
74 bool OperationGUI::OnGUIEvent(int theCommandID, QAD_Desktop* parent)
75 {
76   OperationGUI* myOperationGUI = new OperationGUI();
77   myOperationGUI->myGeomGUI->EmitSignalDeactivateDialog();
78   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
79
80   switch (theCommandID)
81     {
82     case 503: // PARTITION
83       {
84         OperationGUI_PartitionDlg *aDlg = new OperationGUI_PartitionDlg(parent, "", myOperationGUI, Sel);       
85         break;
86       }
87     case 504: // ARCHIMEDE
88       {
89         OperationGUI_ArchimedeDlg *aDlg = new OperationGUI_ArchimedeDlg(parent, "", myOperationGUI, Sel);       
90         break;
91       }
92     case 505: // FILLET
93       {
94         Handle(AIS_InteractiveContext) ic;
95         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
96           OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
97           ic = v3d->getAISContext();
98         }
99         OperationGUI_FilletDlg *aDlg = new OperationGUI_FilletDlg(parent, "", myOperationGUI, Sel, ic); 
100         break;
101       }
102     case 506: // CHAMFER
103       {
104         Handle(AIS_InteractiveContext) ic;
105         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
106           OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
107           ic = v3d->getAISContext();
108         }
109         OperationGUI_ChamferDlg *aDlg = new OperationGUI_ChamferDlg(parent, "", myOperationGUI, Sel, ic);       
110         break;
111       }
112     default:
113       {
114         parent->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
115         break;
116       }
117     }
118   return true;
119 }
120
121
122 //=====================================================================================
123 // function : MakePartitionAndDisplay()
124 // purpose  :
125 //=====================================================================================
126 void OperationGUI::MakePartitionAndDisplay(const GEOM::GEOM_Gen::ListOfIOR& listShapesIOR,
127                                            const GEOM::GEOM_Gen::ListOfIOR& listToolsIOR,
128                                            const GEOM::GEOM_Gen::ListOfIOR& listKeepInsIOR,
129                                            const GEOM::GEOM_Gen::ListOfIOR& listRemoveInsIOR,
130                                            const GEOM::shape_type limit)
131 {
132   try {
133     GEOM::GEOM_Shape_var result = myGeom->Partition(listShapesIOR, listToolsIOR,
134                                                     listKeepInsIOR, listRemoveInsIOR,
135                                                     (CORBA::Short) limit);
136     if(result->_is_nil()) {
137       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_NULLSHAPE"));
138       return;
139     }
140     result->NameType(tr("GEOM_PARTITION"));
141     if(myGeomBase->Display(result))
142       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
143     return;
144   }
145   catch(const SALOME::SALOME_Exception& S_ex) {
146     QtCatchCorbaException(S_ex);
147   }
148 }
149
150
151 //=======================================================================
152 // function : Archimede()
153 // purpose  : 
154 //=======================================================================
155 void OperationGUI::Archimede(const Handle(SALOME_InteractiveObject)& IO, const double aWeight,
156                              const double aWaterDensity, const double aMeshingDeflection)
157 {
158   try {
159     if(IO->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject))) {
160       Handle(GEOM_InteractiveObject) GIO = Handle(GEOM_InteractiveObject)::DownCast(IO);
161       GEOM::GEOM_Shape_var Shape = myGeom->GetIORFromString(GIO->getIOR());
162       
163       GEOM::GEOM_Shape_var Result = myGeom->Archimede(Shape, aWeight, aWaterDensity, aMeshingDeflection);
164       Result->NameType(tr("GEOM_PLANE"));
165       if(myGeomBase->Display(Result)) {
166         QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
167       }
168       return;
169     }
170     if(IO->hasEntry()) {
171       SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
172       SALOMEDS::SObject_var obj = aStudy->FindObjectID( IO->getEntry());
173       SALOMEDS::GenericAttribute_var anAttr;
174       SALOMEDS::AttributeIOR_var anIOR;
175       if(!obj->_is_nil()) {
176         if(obj->FindAttribute(anAttr, "AttributeIOR")) {
177           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
178           GEOM::GEOM_Shape_var Shape = myGeom->GetIORFromString(anIOR->Value());
179           GEOM::GEOM_Shape_var Result = myGeom->Archimede(Shape, aWeight, aWaterDensity, aMeshingDeflection);
180           Result->NameType(tr("GEOM_PLANE"));
181           if (myGeomBase->Display(Result)) {
182             QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
183           }
184         }
185       }
186     }
187   }
188   catch(const SALOME::SALOME_Exception& S_ex) {
189     QtCatchCorbaException(S_ex);
190   }
191 }
192
193
194 //=====================================================================================
195 // function : OnFilletGetAll()
196 // purpose  :
197 //=====================================================================================
198 bool OperationGUI::OnFilletGetAll(const TopoDS_Shape& ShapeTopo, const double Radius,
199                                   const int SubShapeType, const char* ShapeTopoIOR)
200 {
201   GEOM::GEOM_Shape::ListOfSubShapeID_var ListOfID = new GEOM::GEOM_Shape::ListOfSubShapeID;
202   ListOfID->length(0);
203
204   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
205   SALOMEDS::SObject_var theObj = aStudy->FindObjectIOR(ShapeTopoIOR);
206   if(theObj->_is_nil()) {
207     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SHAPE_IN_STUDY"));
208     return false;
209   }
210   
211   try {
212     if(Radius <= Precision::Confusion())
213       return false;
214
215     GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(ShapeTopoIOR);
216     GEOM::GEOM_Shape_var result = myGeom->MakeFillet(aShape, Radius, SubShapeType, ListOfID);
217     if(result->_is_nil()) {
218       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
219       return false;
220     }
221     TopoDS_Shape S = myGeomGUI->GetShapeReader().GetShape(myGeom, result);
222     Standard_CString type;
223     myGeomBase->GetShapeTypeString(S,type);
224     result->NameType(type);
225
226     if(myGeomBase->Display(result))
227       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
228   }
229   catch (const SALOME::SALOME_Exception& S_ex) {
230     QtCatchCorbaException(S_ex);
231   }
232   return true;
233 }
234
235
236 //=====================================================================================
237 // function : OnFilletGetSelected()
238 // purpose  :
239 //=====================================================================================
240 bool OperationGUI::OnFilletGetSelected(const TopoDS_Shape& ShapeTopo, const char* ShapeTopoIOR,
241                                        const double Radius, const int SubShapeType,
242                                        Standard_Integer& aLocalContextId, bool& myUseLocalContext)
243 {
244   if (QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
245     return false;
246
247   OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
248   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
249   
250   if(myUseLocalContext == false) {
251     /*  No local context opened for fillet method */
252     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
253     return false;
254   }
255   
256   GEOM::GEOM_Shape::ListOfSubShapeID_var ListOfID = new GEOM::GEOM_Shape::ListOfSubShapeID;
257   ic->InitSelected();
258   int nbSelected = ic->NbSelected();
259   int i = 0;
260   ic->InitSelected();
261
262   while(ic->MoreSelected()) {
263     TopoDS_Shape s = ic->SelectedShape();
264     if(s.ShapeType() == TopAbs_FACE) {
265       TopExp_Explorer Exp(s, TopAbs_EDGE);
266       TopTools_MapOfShape M;
267       while(Exp.More()) {
268         if(M.Add(Exp.Current())) { /* if a new edge : do not add doublons indices */
269           ListOfID->length(i + 1);
270           ListOfID[i] = myGeomBase->GetIndex(Exp.Current(), ShapeTopo, (int)TopAbs_EDGE);
271           i++;
272         }
273         Exp.Next(); 
274       }  
275     }
276     else {
277       ListOfID->length(i + 1);
278       ListOfID[i] = myGeomBase->GetIndex(ic->SelectedShape(), ShapeTopo, SubShapeType);
279       i++;
280     }
281     ic->NextSelected();
282   }
283
284   GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(ShapeTopoIOR);
285   GEOM::GEOM_Shape_var aResult;
286   try {
287     aResult = myGeom->MakeFillet(aShape, Radius, 6, ListOfID);
288
289     /* local context from DialogBox */
290     ic->CloseLocalContext(aLocalContextId);
291     myUseLocalContext = false;
292
293     TopoDS_Shape S = myGeomGUI->GetShapeReader().GetShape(myGeom, aResult);
294     Standard_CString type;
295     myGeomBase->GetShapeTypeString(S,type);
296     aResult->NameType(type);
297
298     if(myGeomBase->Display(aResult))
299       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
300   }
301   catch(const SALOME::SALOME_Exception& S_ex) {
302     QtCatchCorbaException(S_ex);
303   }
304
305   if(myUseLocalContext) {
306     /* local context from DialogBox */
307     ic->CloseLocalContext(aLocalContextId);
308     myUseLocalContext = false;
309   }
310   
311   DisplayGUI* myDisplayGUI = new DisplayGUI();
312   myDisplayGUI->OnDisplayAll(true) ;
313   QAD_Application::getDesktop()->getActiveStudy()->updateObjBrowser();
314   QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
315
316   return true;  
317 }
318
319
320 //=====================================================================================
321 // function : OnChamferGetAll()
322 // purpose  :
323 //=====================================================================================
324 bool OperationGUI::OnChamferGetAll(const TopoDS_Shape& ShapeTopo, const double D1, const double D2, const int SubShapeType, const char* ShapeTopoIOR)
325 {
326   GEOM::GEOM_Shape::ListOfSubShapeID_var ListOfID = new GEOM::GEOM_Shape::ListOfSubShapeID;
327   ListOfID->length(0);
328
329   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
330   SALOMEDS::SObject_var theObj = aStudy->FindObjectIOR(ShapeTopoIOR);
331   if(theObj->_is_nil()) {
332     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SHAPE_IN_STUDY"));
333     return false;
334   }
335   
336   try {
337     if(D1 <= Precision::Confusion())
338       return false;
339     if(D2 <= Precision::Confusion())
340       return false;
341
342     GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(ShapeTopoIOR);
343     GEOM::GEOM_Shape_var result = myGeom->MakeChamfer(aShape, D1, D2, SubShapeType, ListOfID);
344     if(result->_is_nil()) {
345       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
346       return false;
347     }
348
349     TopoDS_Shape S = myGeomGUI->GetShapeReader().GetShape(myGeom, result);
350     Standard_CString type;
351     myGeomBase->GetShapeTypeString(S,type);
352     result->NameType(type);
353
354     if(myGeomBase->Display(result))
355       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
356   }
357   catch (const SALOME::SALOME_Exception& S_ex) {
358     QtCatchCorbaException(S_ex);
359   }
360   return true;
361 }
362
363
364 //=====================================================================================
365 // function : OnChamferGetSelected()
366 // purpose  :
367 //=====================================================================================
368 bool OperationGUI::OnChamferGetSelected(const TopoDS_Shape& ShapeTopo, const char* ShapeTopoIOR,
369                                         const double D1, const double D2, const int SubShapeType,
370                                         Standard_Integer& aLocalContextId, bool& myUseLocalContext)
371 {
372   if (QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
373     return false;
374   
375   OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
376   Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
377   
378   if(myUseLocalContext == false) {
379     /* No local context opened for chamfer method */
380     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
381     return false;
382   }
383   
384   GEOM::GEOM_Shape::ListOfSubShapeID_var ListOfID = new GEOM::GEOM_Shape::ListOfSubShapeID;
385   ic->InitSelected();
386   int nbSelected = ic->NbSelected();
387   int i = 0;
388   ic->InitSelected();
389
390   while(ic->MoreSelected()) {
391     TopoDS_Shape s = ic->SelectedShape();
392     if (s.ShapeType() == TopAbs_FACE) {
393       TopExp_Explorer Exp(s, TopAbs_EDGE);
394       TopTools_MapOfShape M;
395       while(Exp.More()) {
396         if(M.Add(Exp.Current())) { /* if a new edge : do not add doublons indices */
397           ListOfID->length(i + 1);
398           ListOfID[i] = myGeomBase->GetIndex(Exp.Current(), ShapeTopo, (int)TopAbs_EDGE);
399           i++;
400         }
401         Exp.Next(); 
402       }  
403     }
404     else {
405       ListOfID->length(i + 1);
406       ListOfID[i] = myGeomBase->GetIndex(ic->SelectedShape(), ShapeTopo, SubShapeType);
407       i++;
408     }
409     ic->NextSelected();
410   }
411   
412   GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(ShapeTopoIOR);
413   GEOM::GEOM_Shape_var aResult;
414   try {
415     aResult = myGeom->MakeChamfer(aShape, D1, D2, 6, ListOfID);
416
417     /* local context from DialogBox */
418     ic->CloseLocalContext(aLocalContextId);
419     myUseLocalContext = false;
420
421     TopoDS_Shape S = myGeomGUI->GetShapeReader().GetShape(myGeom, aResult);
422     Standard_CString type;
423     myGeomBase->GetShapeTypeString(S,type);
424     aResult->NameType(type);
425
426     if(myGeomBase->Display(aResult))
427       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
428   }
429   catch(const SALOME::SALOME_Exception& S_ex) {
430     QtCatchCorbaException(S_ex);
431   }
432
433   if (myUseLocalContext) {
434     /* local context from DialogBox */
435     ic->CloseLocalContext(aLocalContextId);
436     myUseLocalContext = false;
437   }
438
439   DisplayGUI* myDisplayGUI = new DisplayGUI();
440   myDisplayGUI->OnDisplayAll(true) ;
441   QAD_Application::getDesktop()->getActiveStudy()->updateObjBrowser();
442   QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
443
444   return true;  
445 }
446
447
448 //=====================================================================================
449 // EXPORTED METHODS
450 //=====================================================================================
451 extern "C"
452 {
453   bool OnGUIEvent(int theCommandID, QAD_Desktop* parent)
454   {return OperationGUI::OnGUIEvent(theCommandID, parent);}
455 }