Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / RepairGUI / RepairGUI.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   : RepairGUI.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header: 
28
29 #include "RepairGUI.h"
30
31 #include "QAD_RightFrame.h"
32 #include "OCCViewer_Viewer3d.h"
33 #include "SALOMEGUI_QtCatchCorbaException.hxx"
34
35 #include "RepairGUI_SewingDlg.h"        // Method SEWING
36 #include "RepairGUI_OrientationDlg.h"   // Method ORIENTATION
37 #include "RepairGUI_SuppressFacesDlg.h" // Method SUPPRESS FACES
38 #include "RepairGUI_SuppressHoleDlg.h"  // Method SUPPRESS HOLE
39
40 #include "utilities.h"
41
42 using namespace std;
43
44 //=======================================================================
45 // function : RepairGUI()
46 // purpose  : Constructor
47 //=======================================================================
48 RepairGUI::RepairGUI() :
49   QObject()
50 {
51   myGeomBase = new GEOMBase();
52   myGeomGUI = GEOMContext::GetGeomGUI();
53 //   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
54 //   myGeom = GEOM::GEOM_Gen::_narrow(comp);
55   myGeom = myGeomGUI->myComponentGeom;
56 }
57
58
59 //=======================================================================
60 // function : ~RepairGUI()
61 // purpose  : Destructor
62 //=======================================================================
63 RepairGUI::~RepairGUI()
64 {
65 }
66
67
68 //=======================================================================
69 // function : OnGUIEvent()
70 // purpose  : 
71 //=======================================================================
72 bool RepairGUI::OnGUIEvent(int theCommandID, QAD_Desktop* parent)
73 {
74   RepairGUI* myRepairGUI = new RepairGUI();
75   myRepairGUI->myGeomGUI->EmitSignalDeactivateDialog();
76   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
77
78   switch (theCommandID)
79     {
80     case 601: // SEWING
81       {
82         RepairGUI_SewingDlg *aDlg = new RepairGUI_SewingDlg(parent, "", myRepairGUI, Sel);
83         break;
84       }
85     case 602: // ORIENTATION
86       {
87         RepairGUI_OrientationDlg *aDlg = new RepairGUI_OrientationDlg(parent, "", myRepairGUI, Sel);
88         break;
89       }
90     case 603: // SUPPRESS FACES : use ic
91       {
92         Handle(AIS_InteractiveContext) ic;
93         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
94           OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
95           ic = v3d->getAISContext();
96         }
97         RepairGUI_SuppressFacesDlg *aDlg = new RepairGUI_SuppressFacesDlg(parent, "", myRepairGUI, Sel, ic);
98         break;
99       }
100     case 604: // SUPPRESS HOLES : use ic
101       {
102         Handle(AIS_InteractiveContext) ic;
103         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
104           OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
105           ic = v3d->getAISContext();
106         }
107         RepairGUI_SuppressHoleDlg *aDlg = new RepairGUI_SuppressHoleDlg(parent, "", myRepairGUI, Sel, ic);
108         break;
109       }
110     default:
111       {
112         parent->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
113         break;
114       }
115     }
116   return true;
117 }
118
119
120 //=====================================================================================
121 // function : MakeSewingAndDisplay()
122 // purpose  :
123 //=====================================================================================
124 void RepairGUI::MakeSewingAndDisplay(GEOM::GEOM_Gen::ListOfIOR& listShapesIOR,
125                                      const Standard_Real precision)
126 {
127   try {
128     GEOM::GEOM_Shape_var result = myGeom->MakeSewing(listShapesIOR, precision);
129     if(result->_is_nil()) {
130       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_NULLSHAPE"));
131       return;
132     }
133
134     TopoDS_Shape S = myGeomGUI->GetShapeReader().GetShape(myGeom, result);
135     Standard_CString type;
136     myGeomBase->GetShapeTypeString(S,type);
137     result->NameType(type);
138
139     if(myGeomBase->Display(result))
140       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
141     return;
142   }
143   catch(const SALOME::SALOME_Exception& S_ex) {
144     QtCatchCorbaException(S_ex);
145   }
146   return;
147 }
148
149
150 //=====================================================================================
151 // function : MakeOrientationChangeAndDisplay()
152 // purpose  :
153 //=====================================================================================
154 void RepairGUI::MakeOrientationChangeAndDisplay(GEOM::GEOM_Shape_ptr Shape)
155 {
156   try {
157     GEOM::GEOM_Shape_var result = myGeom->OrientationChange(Shape);
158     if(result->_is_nil()) {
159       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_NULLSHAPE"));
160       return;
161     }
162     result->NameType(Shape->NameType());
163     if(myGeomBase->Display(result))
164       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE")); 
165     return;
166   }
167   catch(const SALOME::SALOME_Exception& S_ex) {
168     QtCatchCorbaException(S_ex);
169   }
170   return;
171 }
172
173
174 //=====================================================================================
175 // function : OnSuppressHole() 
176 // purpose  : To suppress an hole on a shape 'ShapeTopo'.
177 //          : 'ListOfIdEndFace' may be an empty list.
178 //          : This means that hole do not traverse ShapeTopo.
179 //          : Warning : the hole to be suppressed must be defined by one or two single closed wires !
180 //=====================================================================================
181 bool RepairGUI::OnSuppressHole(const char* ShapeTopoIOR,
182                                const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfIdFace,
183                                const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfIdWire,
184                                const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfIdEndFace)
185 {
186   /* Test the type of viewer */
187   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC )
188     return false;
189   
190   try {
191     GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(ShapeTopoIOR);    
192     GEOM::GEOM_Shape_var aResult = myGeom->SuppressHole(aShape, ListOfIdFace, ListOfIdWire, ListOfIdEndFace);
193   
194     TopoDS_Shape S = myGeomGUI->GetShapeReader().GetShape(myGeom, aResult);
195     Standard_CString type;
196     myGeomBase->GetShapeTypeString(S,type);
197     aResult->NameType(type);
198
199     if(myGeomBase->Display(aResult))
200       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
201   }
202   catch(const SALOME::SALOME_Exception& S_ex) {
203     QtCatchCorbaException(S_ex);
204   }
205   return true;
206 }
207
208
209 //=====================================================================================
210 // function : OnSuppressHolesInFaceOrShell() 
211 // purpose  : To suppress one or more holes on a face
212 //          : 'ListOfIdWires' contains indices or wires/holes.
213 //=====================================================================================
214 bool RepairGUI::OnSuppressHolesInFaceOrShell(const char* ShapeTopoIOR,
215                                              const GEOM::GEOM_Shape::ListOfSubShapeID& ListOfIdWires)
216 {
217   /* Test the type of viewer */
218   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
219     return false;
220   
221   try {
222     GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(ShapeTopoIOR);    
223     GEOM::GEOM_Shape_var aResult = myGeom->SuppressHolesInFaceOrShell(aShape, ListOfIdWires);
224     
225     TopoDS_Shape S = myGeomGUI->GetShapeReader().GetShape(myGeom, aResult);
226     Standard_CString type;
227     myGeomBase->GetShapeTypeString(S,type);
228     aResult->NameType(type);
229     
230     if(myGeomBase->Display(aResult))
231       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
232   }
233   catch(const SALOME::SALOME_Exception& S_ex) {
234     QtCatchCorbaException(S_ex);
235   }
236   return true;
237 }
238
239
240 //=====================================================================================
241 // function : OnSuppressFaces() 
242 // purpose  : To suppress faces from a shape
243 //          : The result is one or more shells/faces as main shapes !
244 //=====================================================================================
245 bool RepairGUI::OnSuppressFaces(const TopoDS_Shape& ShapeTopo, const char* ShapeTopoIOR,
246                                 const Standard_Integer& aLocalContextId, bool& myUseLocalContext)
247 {
248   /* Test the type of viewer */
249   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
250     return false;
251   
252   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
253   SALOMEDS::SObject_var theObj = aStudy->FindObjectIOR(ShapeTopoIOR);
254   if(theObj->_is_nil()) {
255     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SHAPE_IN_STUDY"));
256     return false;
257   }
258   
259   OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
260   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
261   
262   if(myUseLocalContext == false) {
263     /* local context is from DialogBox */
264     MESSAGE("Error : No local context opened for suppress faces method" << endl);
265     return false;
266   }
267   
268   GEOM::GEOM_Shape::ListOfSubShapeID_var ListOfID = new GEOM::GEOM_Shape::ListOfSubShapeID;
269   ic->InitSelected();
270   int nbSelected = ic->NbSelected();
271   ListOfID->length(nbSelected);
272
273   /* Create a list of indices of faces to be suppressed */
274   int i = 0;
275   const int SubShapeType = 4; /* GEOM::FACE type */
276   ic->InitSelected();          /* to repositioning at beginning */
277   while(ic->MoreSelected()) {
278     int index = myGeomBase->GetIndex(ic->SelectedShape(), ShapeTopo, SubShapeType);
279     ListOfID[i] = index;
280     i++;
281     ic->NextSelected();
282   }
283   
284   /* Close local context opened in DialogBox */
285   ic->CloseLocalContext(aLocalContextId);
286   myUseLocalContext = false;
287   
288   /* Here is the main shape */
289   GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(ShapeTopoIOR);  
290   GEOM::GEOM_Gen::ListOfGeomShapes_var listGeomShapes = new GEOM::GEOM_Gen::ListOfGeomShapes;
291   
292   /* Call geom method that return a list of shells/faces as result of suppress */
293   try {
294     listGeomShapes = myGeom->SuppressFaces(aShape, ListOfID);
295   }
296   catch(const SALOME::SALOME_Exception& S_ex) {
297     QtCatchCorbaException(S_ex);
298   }
299   
300   /* Test list of shells/faces */
301   if(listGeomShapes->length() < 1) {
302     return false;
303   }
304
305   /* Loop on each object created */
306   for(int i=0; i<listGeomShapes->length(); i++) {
307     GEOM::GEOM_Shape_var aShellOrFace = listGeomShapes[i];
308     TopoDS_Shape S = myGeomGUI->GetShapeReader().GetShape(myGeom, aShellOrFace);
309     
310     if(S.IsNull())
311       return false;
312     
313     char* nameG = (char *)malloc(20);
314     Standard_CString Type;
315     if(myGeomBase->GetShapeTypeString(S, Type)) {
316       aShellOrFace->NameType(Type);
317       sprintf (nameG, "%s_%d", Type, myGeomGUI->myNbGeom++);
318     }
319     else {
320       aShellOrFace->NameType(tr("GEOM_SHAPE"));
321       sprintf (nameG, "%s_%d", tr("GEOM_SHAPE").latin1(), myGeomGUI->myNbGeom++);
322     }
323     
324     /* Display with name */
325     if(!myGeomBase->Display(aShellOrFace, nameG)) {
326       QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
327       return false;
328     }
329   }
330   
331   QAD_Application::getDesktop()->putInfo (tr("GEOM_PRP_READY"));
332   return true;
333 }
334
335
336 //=====================================================================================
337 // EXPORTED METHODS
338 //=====================================================================================
339 extern "C"
340 {
341   bool OnGUIEvent(int theCommandID, QAD_Desktop* parent)
342   {return RepairGUI::OnGUIEvent(theCommandID, parent);}
343 }