Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / GEOMGUI / GeometryGUI_Swig.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   : GeometryGUI_Swig.cxx
25 //  Author : Nicolas REJNERI, Paul RASCLE
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "GeometryGUI_Swig.hxx"
31 #include "utilities.h"
32
33 #include "QAD_Application.h"
34 #include "QAD_Desktop.h"
35 #include "QAD_Study.h"
36 #include "QAD_StudyFrame.h"
37 #include "QAD_RightFrame.h"
38 #include "SALOMEGUI_ImportOperation.h"
39
40 #include "OCCViewer_Viewer3d.h"
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_MapOfShape.hxx>
43 #include <TopTools_ListOfShape.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <BRepAdaptor_Surface.hxx>
46 #include <BRepAdaptor_Curve.hxx>
47 #include <GeomAbs_CurveType.hxx>
48 #include <GeomAbs_SurfaceType.hxx>
49 #include <TopoDS.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Face.hxx>
52 #include <TopoDS_Iterator.hxx>
53
54 #include "VTKViewer_RenderWindowInteractor.h"
55 #include "VTKViewer_ViewFrame.h"
56
57 #include "GEOM_Actor.h"
58 #include "GEOM_Client.hxx"
59 #include "GEOM_AISShape.hxx"
60 #include "GEOM_AssemblyBuilder.h"
61 #include "GEOM_InteractiveObject.hxx"
62
63 static GEOM_Client ShapeReader;
64
65 GEOM_Swig::GEOM_Swig()
66 {
67   // MESSAGE("Constructeur");
68 }
69
70 GEOM_Swig::~GEOM_Swig()
71 {
72   // MESSAGE("Destructeur");
73 }
74
75 void GEOM_Swig::createAndDisplayGO(const char* Entry)
76 {
77   // MESSAGE("createAndDisplayGO");
78   QAD_Study* ActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
79   SALOMEDS::Study_var aStudy = ActiveStudy->getStudyDocument();
80   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
81
82   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
83   GEOM::GEOM_Gen_var Geom = GEOM::GEOM_Gen::_narrow(comp);
84
85   Standard_CString Fatherior = "";
86   SALOMEDS::SComponent_var father = aStudy->FindComponent("GEOM");
87   aStudyBuilder->DefineComponentInstance( father, Geom );
88   father->ComponentIOR( Fatherior );
89
90   SALOMEDS::SObject_var fatherSF = aStudy->FindObjectID(ActiveStudy->getActiveStudyFrame()->entry());
91
92   SALOMEDS::SObject_var obj = aStudy->FindObjectID(Entry);
93   SALOMEDS::GenericAttribute_var anAttr;
94   SALOMEDS::AttributeName_var    aName;
95   SALOMEDS::AttributeIOR_var     anIOR;
96   // Create new actor
97   if ( !obj->FindAttribute(anAttr, "AttributeIOR")) 
98     return;
99   anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
100   GEOM::GEOM_Shape_var aShape = Geom->GetIORFromString(anIOR->Value());
101   TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
102   
103   if ( !obj->_is_nil() ) {
104     if (obj->FindAttribute(anAttr, "AttributeName")) {
105       aName = SALOMEDS::AttributeName::_narrow(anAttr);
106       // open transaction
107       QAD_Operation* op = new SALOMEGUI_ImportOperation( ActiveStudy );
108       op->start();
109       
110       SALOMEDS::SObject_var newObj1 = aStudyBuilder->NewObject(fatherSF);
111       aStudyBuilder->Addreference(newObj1, obj);
112       // commit transaction
113       op->finish();
114                   
115       if ( ActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK )  { // VTK
116         //vtkQGLRenderWindowInteractor* myRenderInter = ActiveStudy->getActiveStudyFrame()->getRightFrame()->getVTKView()->getRWInteractor();
117         VTKViewer_RenderWindowInteractor* myRenderInter= ((VTKViewer_ViewFrame*)ActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
118         int themode = myRenderInter->GetDisplayMode();
119       
120         vtkActorCollection* theActors = 
121           GEOM_AssemblyBuilder::BuildActors(Shape,0,themode,Standard_True);
122         theActors->InitTraversal();
123         vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
124         while(!(anActor==NULL)) {
125           GEOM_Actor* GActor = GEOM_Actor::SafeDownCast( anActor );
126           Handle(GEOM_InteractiveObject) IO = new GEOM_InteractiveObject(anIOR->Value(),
127                                                                          Fatherior,
128                                                                          "GEOM");
129           IO->setEntry(obj->GetID());
130           GActor->setIO( IO );
131           GActor->setName( aName->Value() );
132           
133           myRenderInter->Display(GActor);
134           anActor = (vtkActor*)theActors->GetNextActor();
135         }
136         myRenderInter->Update();
137       } 
138       else if ( ActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_OCC ) // OCC
139         {
140           OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)ActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
141           //      QAD_Viewer3d* v3d = ActiveStudy->getActiveStudyFrame()->getViewerOCC();
142           Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
143           Handle(GEOM_AISShape) aSh = new GEOM_AISShape(Shape, aName->Value());
144           Handle(GEOM_InteractiveObject) IO = new GEOM_InteractiveObject(anIOR->Value(),
145                                                                          Fatherior,
146                                                                          "GEOM");
147           
148           IO->setEntry(obj->GetID());
149           aSh->setIO( IO );
150           aSh->setName( aName->Value() );
151           ic->Display (aSh);
152           ic->AddOrRemoveCurrentObject(aSh, true);
153         }
154     }
155   }
156   ActiveStudy->updateObjBrowser( true );
157 }
158
159 int  GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
160 {
161   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
162   GEOM::GEOM_Gen_var  Geom = GEOM::GEOM_Gen::_narrow(comp);
163  
164   GEOM::GEOM_Shape_var aMainShape = Geom->GetIORFromString(IOR);
165   TopoDS_Shape shape        = ShapeReader.GetShape(Geom, aMainShape);
166
167   GEOM::GEOM_Shape_var aSubShape = Geom->GetIORFromString(SubIOR);
168   TopoDS_Shape subshape    = ShapeReader.GetShape(Geom, aSubShape);
169
170   int index = 1;
171   if(subshape.ShapeType() == TopAbs_COMPOUND) { 
172     TopoDS_Iterator it; 
173     TopTools_ListOfShape CL; 
174     CL.Append(shape); 
175     TopTools_ListIteratorOfListOfShape itC; 
176     for(itC.Initialize(CL); itC.More(); itC.Next()) {
177       for(it.Initialize(itC.Value()); it.More(); it.Next()) { 
178         if (it.Value().ShapeType() == TopAbs_COMPOUND) {
179           if (it.Value().IsSame(subshape))
180             return index; 
181           else 
182             index++; 
183           CL.Append(it.Value()); 
184         }
185       } 
186     } 
187   } 
188   else { 
189     TopExp_Explorer Exp(shape, subshape.ShapeType()); 
190     TopTools_MapOfShape M; 
191     while(Exp.More()) { 
192       if(M.Add(Exp.Current())) {
193         if(Exp.Current().IsSame(subshape)) 
194           return index; 
195         index++; 
196       } 
197       Exp.Next(); 
198     } 
199   } 
200   return -1;
201 }
202
203 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
204 {
205   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
206   GEOM::GEOM_Gen_var  Geom = GEOM::GEOM_Gen::_narrow(comp);
207  
208   GEOM::GEOM_Shape_var aShape = Geom->GetIORFromString(IOR);
209   TopoDS_Shape shape    = ShapeReader.GetShape(Geom, aShape);
210
211   if( shape.IsNull() ) {
212     return "Null Shape" ;
213   }
214
215   switch (shape.ShapeType() )
216   {
217   case TopAbs_COMPOUND:
218     { return "Compound" ;}
219   case  TopAbs_COMPSOLID:
220     { return "Compound Solid" ;}
221   case TopAbs_SOLID:
222     { return "Solid" ;}
223   case TopAbs_SHELL:
224     { return "Shell" ;}
225   case TopAbs_FACE:
226     { 
227       BRepAdaptor_Surface surf(TopoDS::Face(shape));
228       if ( surf.GetType() == GeomAbs_Plane ) {
229         return "Plane" ;
230       } else if ( surf.GetType() == GeomAbs_Cylinder ) {
231         return "Cylindrical Face" ;
232       } else if ( surf.GetType() == GeomAbs_Sphere ) {
233         return "Spherical Face" ;
234       } else if ( surf.GetType() == GeomAbs_Torus ) {
235         return "Toroidal Face" ;
236       } else if ( surf.GetType() == GeomAbs_Cone ) {
237         return "Conical Face" ;
238       } else {
239         return "GEOM::FACE" ;
240       }
241     }
242   case TopAbs_WIRE:
243     { return "Wire" ;}
244   case TopAbs_EDGE:
245     { 
246       BRepAdaptor_Curve curv(TopoDS::Edge(shape));
247       if ( curv.GetType() == GeomAbs_Line ) {
248         if ( (Abs(curv.FirstParameter()) >= 1E6 ) || 
249              (Abs(curv.LastParameter()) >= 1E6 )) {
250           return "Line" ;
251         } else
252           return "Edge" ;
253       } else if ( curv.GetType() == GeomAbs_Circle ) {
254         if ( curv.IsClosed() )
255           return "Circle" ;
256         else
257           return "Arc" ;
258       } else {
259         return "Edge" ;
260       }
261     }
262   case TopAbs_VERTEX:
263     { return "Vertex" ;}
264   case TopAbs_SHAPE:
265     { return "Shape" ;}
266   }
267   return 0;
268 }
269
270
271 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
272 {
273   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
274   GEOM::GEOM_Gen_var  Geom = GEOM::GEOM_Gen::_narrow(comp);
275  
276   GEOM::GEOM_Shape_var aShape = Geom->GetIORFromString(IOR);
277
278   if( aShape->_is_nil() ) {
279     return "None" ;
280   }
281       
282   switch (aShape->ShapeType() )
283     {
284     case TopAbs_COMPOUND:
285       { return "ICON_OBJBROWSER_COMPOUND" ;}
286     case  TopAbs_COMPSOLID:
287       { return "ICON_OBJBROWSER_COMPSOLID" ;}
288     case TopAbs_SOLID:
289       { return "ICON_OBJBROWSER_SOLID" ;}
290     case TopAbs_SHELL:
291       { return "ICON_OBJBROWSER_SHELL" ;}
292     case TopAbs_FACE:
293       { return "ICON_OBJBROWSER_FACE" ;}
294     case TopAbs_WIRE:
295       { return "ICON_OBJBROWSER_WIRE" ;}
296     case TopAbs_EDGE:
297       { return "ICON_OBJBROWSER_EDGE" ;}
298     case TopAbs_VERTEX:
299       { return "ICON_OBJBROWSER_VERTEX" ;}
300     }
301   return "None";
302 }
303
304 void GEOM_Swig::setDisplayMode(const char* Entry, int mode)
305 {
306   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
307   Handle(SALOME_InteractiveObject) IO = 
308     myStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame()->FindIObject( Entry );
309
310   if ( myStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK )  { // VTK
311     VTKViewer_RenderWindowInteractor* myRenderInter = 
312       ((VTKViewer_ViewFrame*)myStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
313     
314     myRenderInter->SwitchRepresentation(IO, mode);
315     myRenderInter->Update();
316   } 
317   else if ( myStudy->getActiveStudyFrame()->getTypeView() == VIEW_OCC ) // OCC
318     {
319       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)myStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
320       v3d->SwitchRepresentation(IO, mode);
321     }
322 }
323
324 void GEOM_Swig::setColor(const char* Entry, int red, int green, int blue)
325 {
326   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
327   QColor c = QColor (red, green, blue);
328   Handle(SALOME_InteractiveObject) IO = 
329     myStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame()->FindIObject( Entry );
330  
331   if ( myStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK )  { // VTK
332     VTKViewer_RenderWindowInteractor* myRenderInter = 
333       ((VTKViewer_ViewFrame*)myStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
334     myRenderInter->SetColor(IO,c);
335     myRenderInter->Update();
336   } 
337   else if ( myStudy->getActiveStudyFrame()->getTypeView() == VIEW_OCC ) // OCC
338     {
339       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)myStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
340       v3d->SetColor(IO,c);
341     }
342 }
343
344 void GEOM_Swig::setTransparency(const char* Entry, float transp)
345 {
346   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
347   Handle(SALOME_InteractiveObject) IO = 
348     myStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame()->FindIObject( Entry );
349  
350   if ( myStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK )  { // VTK
351     VTKViewer_RenderWindowInteractor* myRenderInter = 
352       ((VTKViewer_ViewFrame*)myStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
353     myRenderInter->SetTransparency(IO,transp);
354     myRenderInter->Update();
355   } 
356   else if ( myStudy->getActiveStudyFrame()->getTypeView() == VIEW_OCC ) // OCC
357     {
358       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)myStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
359       v3d->SetTransparency(IO,transp);
360     }
361 }