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