Salome HOME
fb2b0e1e070e04e7e692b8f9c8990dd4c87b67cf
[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 #include "GeometryGUI_Swig.hxx"
30 #include "SALOMEGUI_Swig.hxx"
31
32 #include "GeometryGUI.h"
33
34 #include <SUIT_Session.h>
35 #include <SalomeApp_Application.h>
36 #include <SalomeApp_Study.h>
37 #include <SalomeApp_ImportOperation.h>
38
39 #include <OB_Browser.h>
40
41 #include <OCCViewer_ViewWindow.h>
42 #include <OCCViewer_ViewManager.h>
43 #include <SOCC_ViewModel.h>
44
45 #include <VTKViewer_ViewWindow.h>
46 #include <VTKViewer_ViewModel.h>
47 #include <VTKViewer_RenderWindowInteractor.h>
48 #include <SVTK_ViewWindow.h>
49 #include <SVTK_RenderWindowInteractor.h>
50
51 #include "GEOM_Actor.h"
52 #include "GEOM_Client.hxx"
53 #include "GEOM_AISShape.hxx"
54 #include "GEOM_AssemblyBuilder.h"
55 #include "GEOM_InteractiveObject.hxx"
56
57 #include <SALOME_Event.hxx>
58
59 #include <TopExp_Explorer.hxx>
60 #include <TopTools_MapOfShape.hxx>
61 #include <TopTools_ListOfShape.hxx>
62 #include <TopTools_ListIteratorOfListOfShape.hxx>
63 #include <BRepAdaptor_Surface.hxx>
64 #include <BRepAdaptor_Curve.hxx>
65 #include <GeomAbs_CurveType.hxx>
66 #include <GeomAbs_SurfaceType.hxx>
67 #include <TopoDS.hxx>
68 #include <TopoDS_Edge.hxx>
69 #include <TopoDS_Face.hxx>
70 #include <TopoDS_Iterator.hxx>
71 #include <AIS_ListOfInteractive.hxx>
72 #include <AIS_ListIteratorOfListOfInteractive.hxx>
73 #include <V3d_Viewer.hxx>
74
75 #include "utilities.h"
76
77 using namespace std;
78
79 #include "SALOMEDSClient.hxx"
80 #include "SALOMEDS_SObject.hxx"
81 #include "SALOMEDS_Study.hxx"
82
83 static GEOM_Client ShapeReader;
84
85 inline OCCViewer_Viewer* GetOCCViewer(SUIT_Application* theApp){
86   SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
87   if(window && window->getViewManager()->getType() == OCCViewer_Viewer::Type()){
88     OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( window );
89     if ( vw ) {
90       OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
91       if ( vm ) 
92         return vm->getOCCViewer();
93     }
94   }
95   
96   return 0;
97 }
98
99 inline SVTK_ViewWindow* GetSVTKViewWindow(SUIT_Application* theApp){
100   SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
101   if(window && window->getViewManager()->getType() == VTKViewer_Viewer::Type())
102     return dynamic_cast<SVTK_ViewWindow*>( window );
103   
104   return 0;
105 }
106
107 GEOM_Swig::GEOM_Swig()
108 {
109   // MESSAGE("Constructeur");
110 }
111
112 GEOM_Swig::~GEOM_Swig()
113 {
114   // MESSAGE("Destructeur");
115 }
116
117 void GEOM_Swig::createAndDisplayGO(const char* Entry)
118 {
119   // MESSAGE("createAndDisplayGO");
120   
121   SUIT_Application* app = SUIT_Session::session()->activeApplication();
122   if ( !app ) return;
123   
124   SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
125   if ( !ActiveStudy ) return;
126   
127   _PTR(Study) aStudy(ActiveStudy->studyDS());
128   _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
129   
130   GEOM::GEOM_Gen_var Geom   = GeometryGUI::GetGeomGen();
131   if ( CORBA::is_nil( Geom ) )
132     return;
133
134   string aFatherIOR;
135   _PTR(SComponent) father = aStudy->FindComponent("GEOM");
136   aStudyBuilder->DefineComponentInstance( father, SalomeApp_Application::orb()->object_to_string(Geom) );
137   father->ComponentIOR(aFatherIOR);
138   
139   _PTR(SObject) fatherSF;
140
141   _PTR(SObject) obj = aStudy->FindObjectID(Entry);
142   _PTR(GenericAttribute) anAttr;
143   
144   // Create new actor
145   if ( !obj->FindAttribute(anAttr, "AttributeIOR")) 
146     return;
147   _PTR(AttributeIOR) anIOR(anAttr);
148   string anIORValue = anIOR->Value();
149   
150   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
151   TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
152   
153   if ( obj ) {
154     if (obj->FindAttribute(anAttr, "AttributeName")) {
155       _PTR(AttributeName) aName ( anAttr );
156       string aNameValue = aName->Value();
157       // open transaction
158       SUIT_Operation* op = new SalomeApp_ImportOperation( app );
159       op->start();
160       
161       _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
162       aStudyBuilder->Addreference(newObj1, obj);
163       // commit transaction
164       op->commit();
165                   
166       Handle(GEOM_InteractiveObject) anIO = new GEOM_InteractiveObject(const_cast<char*>(anIORValue.c_str()),
167                                                                        const_cast<char*>(aFatherIOR.c_str()),
168                                                                        "GEOM",
169                                                                        const_cast<char*>( obj->GetID().c_str() ));
170       
171       class TEvent: public SALOME_Event{
172         SUIT_Application* myApp;
173         TopoDS_Shape myShape;
174         Handle(SALOME_InteractiveObject) myIO;
175         const char* myName;
176       public:
177         TEvent(SUIT_Application* theApp, TopoDS_Shape theShape,
178                const Handle(SALOME_InteractiveObject)& theIO, 
179                const char* theName):
180           myApp(theApp), myShape(theShape),
181           myIO(theIO), myName(theName)
182         {}
183         virtual void Execute(){
184           if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
185             SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
186             int aMode = myRenderInter->GetDisplayMode();
187             
188             vtkActorCollection* theActors = GEOM_AssemblyBuilder::BuildActors(myShape,0,aMode,true);
189             theActors->InitTraversal();
190             while(vtkActor* anActor = theActors->GetNextActor()){
191               GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
192               GActor->setName(const_cast<char*>(myName));
193               GActor->setIO(myIO);
194               myRenderInter->Display(GActor);
195             }
196             myRenderInter->Update();
197           }else if( OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
198             Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
199             Handle(GEOM_AISShape) aSh = new GEOM_AISShape(myShape,const_cast<char*>(myName));
200             aSh->setName(const_cast<char*>(myName));
201             aSh->setIO(myIO);
202             ic->Display(aSh);
203             ic->AddOrRemoveCurrentObject(aSh,true);
204           }
205           // update object browser
206           SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(myApp);
207           if (app) {
208             CAM_Module* module = app->module( "Geometry" );
209             SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
210             if ( appMod )
211               appMod->updateObjBrowser( true );
212           }
213           
214         }
215       };
216       ProcessVoidEvent(new TEvent(app,Shape,anIO,aNameValue.c_str()));
217     }
218   }
219 }
220
221
222 int  GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
223 {
224   GEOM::GEOM_Gen_var Geom   = GeometryGUI::GetGeomGen();
225   if ( CORBA::is_nil( Geom ) )
226     return -1;
227   
228   GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
229   TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
230
231   GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
232   TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
233
234   int index = 1;
235   if(subshape.ShapeType() == TopAbs_COMPOUND) { 
236     TopoDS_Iterator it; 
237     TopTools_ListOfShape CL; 
238     CL.Append(shape); 
239     TopTools_ListIteratorOfListOfShape itC; 
240     for(itC.Initialize(CL); itC.More(); itC.Next()) {
241       for(it.Initialize(itC.Value()); it.More(); it.Next()) { 
242         if (it.Value().ShapeType() == TopAbs_COMPOUND) {
243           if (it.Value().IsSame(subshape))
244             return index; 
245           else 
246             index++; 
247           CL.Append(it.Value()); 
248         }
249       } 
250     } 
251   } 
252   else { 
253     TopExp_Explorer Exp(shape, subshape.ShapeType()); 
254     TopTools_MapOfShape M; 
255     while(Exp.More()) { 
256       if(M.Add(Exp.Current())) {
257         if(Exp.Current().IsSame(subshape)) 
258           return index; 
259         index++; 
260       } 
261       Exp.Next(); 
262     } 
263   } 
264    
265   return -1;
266 }
267
268 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
269 {
270   GEOM::GEOM_Gen_var Geom   = GeometryGUI::GetGeomGen();
271   if ( CORBA::is_nil( Geom ) )
272     return 0;
273  
274   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
275   TopoDS_Shape shape    = ShapeReader.GetShape(Geom, aShape);
276
277   if( shape.IsNull() ) {
278     return "Null Shape" ;
279   }
280
281   switch (shape.ShapeType() )
282   {
283   case TopAbs_COMPOUND:
284     { return "Compound" ;}
285   case  TopAbs_COMPSOLID:
286     { return "Compound Solid" ;}
287   case TopAbs_SOLID:
288     { return "Solid" ;}
289   case TopAbs_SHELL:
290     { return "Shell" ;}
291   case TopAbs_FACE:
292     { 
293       BRepAdaptor_Surface surf(TopoDS::Face(shape));
294       if ( surf.GetType() == GeomAbs_Plane ) {
295         return "Plane" ;
296       } else if ( surf.GetType() == GeomAbs_Cylinder ) {
297         return "Cylindrical Face" ;
298       } else if ( surf.GetType() == GeomAbs_Sphere ) {
299         return "Spherical Face" ;
300       } else if ( surf.GetType() == GeomAbs_Torus ) {
301         return "Toroidal Face" ;
302       } else if ( surf.GetType() == GeomAbs_Cone ) {
303         return "Conical Face" ;
304       } else {
305         return "GEOM::FACE" ;
306       }
307     }
308   case TopAbs_WIRE:
309     { return "Wire" ;}
310   case TopAbs_EDGE:
311     { 
312       BRepAdaptor_Curve curv(TopoDS::Edge(shape));
313       if ( curv.GetType() == GeomAbs_Line ) {
314         if ( (Abs(curv.FirstParameter()) >= 1E6 ) || 
315              (Abs(curv.LastParameter()) >= 1E6 )) {
316           return "Line" ;
317         } else
318           return "Edge" ;
319       } else if ( curv.GetType() == GeomAbs_Circle ) {
320         if ( curv.IsClosed() )
321           return "Circle" ;
322         else
323           return "Arc" ;
324       } else {
325         return "Edge" ;
326       }
327     }
328   case TopAbs_VERTEX:
329     { return "Vertex" ;}
330   case TopAbs_SHAPE:
331     { return "Shape" ;}
332   }
333   return 0;
334 }
335
336
337 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
338 {
339   GEOM::GEOM_Gen_var Geom   = GeometryGUI::GetGeomGen();
340   if ( CORBA::is_nil( Geom ) )
341     return "None";
342   
343   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
344   TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
345
346   if( shape.IsNull() ) {
347     return "None" ;
348   }
349       
350   switch (shape.ShapeType() )
351     {
352     case TopAbs_COMPOUND:
353       { return "ICON_OBJBROWSER_COMPOUND" ;}
354     case  TopAbs_COMPSOLID:
355       { return "ICON_OBJBROWSER_COMPSOLID" ;}
356     case TopAbs_SOLID:
357       { return "ICON_OBJBROWSER_SOLID" ;}
358     case TopAbs_SHELL:
359       { return "ICON_OBJBROWSER_SHELL" ;}
360     case TopAbs_FACE:
361       { return "ICON_OBJBROWSER_FACE" ;}
362     case TopAbs_WIRE:
363       { return "ICON_OBJBROWSER_WIRE" ;}
364     case TopAbs_EDGE:
365       { return "ICON_OBJBROWSER_EDGE" ;}
366     case TopAbs_VERTEX:
367       { return "ICON_OBJBROWSER_VERTEX" ;}
368     }
369   
370   return "None";
371 }
372
373 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
374 {
375   SUIT_Application* app = SUIT_Session::session()->activeApplication();
376   if ( !app ) return;
377   
378   Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
379   
380   class TEvent: public SALOME_Event{
381     SUIT_Application* myApp;
382     Handle(SALOME_InteractiveObject) myIO;
383     int myParam;
384   public:
385     TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, int theParam):
386       myApp(theApp), myIO(theIO), myParam(theParam)
387     {}
388     virtual void Execute(){
389       if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
390         SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
391         myRenderInter->SetDisplayMode(myIO,myParam);
392         myRenderInter->Update();
393       }
394       else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
395         SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
396         if (soccViewer)
397           soccViewer->switchRepresentation(myIO,myParam);
398       }
399     }
400   };
401
402   ProcessVoidEvent(new TEvent(app,anIO,theMode));
403 }
404
405 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
406 {
407   SUIT_Application* app = SUIT_Session::session()->activeApplication();
408   if ( !app ) return;
409
410   Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
411
412   QColor aColor(red,green,blue);
413
414   class TEvent: public SALOME_Event{
415     SUIT_Application* myApp;
416     Handle(SALOME_InteractiveObject) myIO;
417     QColor myParam;
418   public:
419     TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, const QColor& theParam):
420       myApp(theApp), myIO(theIO), myParam(theParam)
421     {}
422     virtual void Execute(){
423       if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
424         SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
425         myRenderInter->SetColor(myIO,myParam);
426         myRenderInter->Update();
427       }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
428         Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
429         AIS_ListOfInteractive List;
430         ic->DisplayedObjects(List);
431         AIS_ListIteratorOfListOfInteractive ite(List);
432         for ( ; ite.More(); ite.Next() ) {
433           Handle(SALOME_InteractiveObject) anObj =
434             Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
435           if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( myIO ) ) {
436             Quantity_Color CSFColor = Quantity_Color ( myParam.red()   / 255.,
437                                                        myParam.green() / 255.,
438                                                        myParam.blue()  / 255.,
439                                                        Quantity_TOC_RGB );
440             ite.Value()->SetColor( CSFColor );
441             if ( ite.Value()->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
442               Handle(GEOM_AISShape)::DownCast( ite.Value() )->SetShadingColor( CSFColor );
443             ite.Value()->Redisplay( Standard_True );
444             occViewer->update();
445             break;
446           }
447         }
448       }
449     }
450   };
451   ProcessVoidEvent(new TEvent(app,anIO,aColor));
452 }
453
454 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
455 {
456   SUIT_Application* app = SUIT_Session::session()->activeApplication();
457   if ( !app ) return;
458   
459   Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
460   
461   class TEvent: public SALOME_Event{
462     SUIT_Application* myApp;
463     Handle(SALOME_InteractiveObject) myIO;
464     float myParam;
465   public:
466     TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, float theParam):
467       myApp(theApp), myIO(theIO), myParam(theParam)
468     {}
469     virtual void Execute(){
470       if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
471         SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
472         myRenderInter->SetTransparency(myIO,myParam);
473         myRenderInter->Update();
474       }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
475         SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
476         if (soccViewer)
477           soccViewer->setTransparency(myIO,myParam);
478       }
479     }
480   };
481   
482   ProcessVoidEvent(new TEvent(app,anIO,transp));
483 }
484
485
486 bool GEOM_Swig::initGeomGen()
487 {
488   return GeometryGUI::InitGeomGen();
489 }