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