]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMGUI/GeometryGUI_Swig.cxx
Salome HOME
eac49114490bc7e845aee8481c0c4b9c54a66b54
[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) obj = aStudy->FindObjectID(Entry);
140   _PTR(GenericAttribute) anAttr;
141   
142   // Create new actor
143   if ( !obj->FindAttribute(anAttr, "AttributeIOR")) 
144     return;
145   _PTR(AttributeIOR) anIOR(anAttr);
146   string anIORValue = anIOR->Value();
147   
148   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
149   TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
150   
151   if ( obj ) {
152     if (obj->FindAttribute(anAttr, "AttributeName")) {
153       _PTR(AttributeName) aName ( anAttr );
154       string aNameValue = aName->Value();
155       // open transaction
156       /*jfa for bug IPAL9384 3)c):SUIT_Operation* op = new SalomeApp_ImportOperation( app );
157       op->start();
158       
159       _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
160       aStudyBuilder->Addreference(newObj1, obj);
161       // commit transaction
162       op->commit();*/
163                   
164       Handle(GEOM_InteractiveObject) anIO = new GEOM_InteractiveObject(const_cast<char*>(anIORValue.c_str()),
165                                                                        const_cast<char*>(aFatherIOR.c_str()),
166                                                                        "GEOM",
167                                                                        const_cast<char*>( obj->GetID().c_str() ));
168       
169       class TEvent: public SALOME_Event{
170         SUIT_Application* myApp;
171         TopoDS_Shape myShape;
172         Handle(SALOME_InteractiveObject) myIO;
173         const char* myName;
174       public:
175         TEvent(SUIT_Application* theApp, TopoDS_Shape theShape,
176                const Handle(SALOME_InteractiveObject)& theIO, 
177                const char* theName):
178           myApp(theApp), myShape(theShape),
179           myIO(theIO), myName(theName)
180         {}
181         virtual void Execute(){
182           if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
183             SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
184             int aMode = myRenderInter->GetDisplayMode();
185             
186             vtkActorCollection* theActors = GEOM_AssemblyBuilder::BuildActors(myShape,0,aMode,true);
187             theActors->InitTraversal();
188             while(vtkActor* anActor = theActors->GetNextActor()){
189               GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
190               GActor->setName(const_cast<char*>(myName));
191               GActor->setIO(myIO);
192               myRenderInter->Display(GActor);
193             }
194             myRenderInter->Update();
195           }else if( OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
196             Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
197             Handle(GEOM_AISShape) aSh = new GEOM_AISShape(myShape,const_cast<char*>(myName));
198             aSh->setName(const_cast<char*>(myName));
199             aSh->setIO(myIO);
200             ic->Display(aSh);
201             ic->AddOrRemoveCurrentObject(aSh,true);
202           }
203           // update object browser
204           SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(myApp);
205           if (app) {
206             CAM_Module* module = app->module( "Geometry" );
207             SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
208             if ( appMod )
209               appMod->updateObjBrowser( true );
210           }
211           
212         }
213       };
214       ProcessVoidEvent(new TEvent(app,Shape,anIO,aNameValue.c_str()));
215     }
216   }
217 }
218
219
220 int  GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
221 {
222   GEOM::GEOM_Gen_var Geom   = GeometryGUI::GetGeomGen();
223   if ( CORBA::is_nil( Geom ) )
224     return -1;
225   
226   GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
227   TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
228
229   GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
230   TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
231
232   int index = 1;
233   if(subshape.ShapeType() == TopAbs_COMPOUND) { 
234     TopoDS_Iterator it; 
235     TopTools_ListOfShape CL; 
236     CL.Append(shape); 
237     TopTools_ListIteratorOfListOfShape itC; 
238     for(itC.Initialize(CL); itC.More(); itC.Next()) {
239       for(it.Initialize(itC.Value()); it.More(); it.Next()) { 
240         if (it.Value().ShapeType() == TopAbs_COMPOUND) {
241           if (it.Value().IsSame(subshape))
242             return index; 
243           else 
244             index++; 
245           CL.Append(it.Value()); 
246         }
247       } 
248     } 
249   } 
250   else { 
251     TopExp_Explorer Exp(shape, subshape.ShapeType()); 
252     TopTools_MapOfShape M; 
253     while(Exp.More()) { 
254       if(M.Add(Exp.Current())) {
255         if(Exp.Current().IsSame(subshape)) 
256           return index; 
257         index++; 
258       } 
259       Exp.Next(); 
260     } 
261   } 
262    
263   return -1;
264 }
265
266 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
267 {
268   GEOM::GEOM_Gen_var Geom   = GeometryGUI::GetGeomGen();
269   if ( CORBA::is_nil( Geom ) )
270     return 0;
271  
272   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
273   TopoDS_Shape shape    = ShapeReader.GetShape(Geom, aShape);
274
275   if( shape.IsNull() ) {
276     return "Null Shape" ;
277   }
278
279   switch (shape.ShapeType() )
280   {
281   case TopAbs_COMPOUND:
282     { return "Compound" ;}
283   case  TopAbs_COMPSOLID:
284     { return "Compound Solid" ;}
285   case TopAbs_SOLID:
286     { return "Solid" ;}
287   case TopAbs_SHELL:
288     { return "Shell" ;}
289   case TopAbs_FACE:
290     { 
291       BRepAdaptor_Surface surf(TopoDS::Face(shape));
292       if ( surf.GetType() == GeomAbs_Plane ) {
293         return "Plane" ;
294       } else if ( surf.GetType() == GeomAbs_Cylinder ) {
295         return "Cylindrical Face" ;
296       } else if ( surf.GetType() == GeomAbs_Sphere ) {
297         return "Spherical Face" ;
298       } else if ( surf.GetType() == GeomAbs_Torus ) {
299         return "Toroidal Face" ;
300       } else if ( surf.GetType() == GeomAbs_Cone ) {
301         return "Conical Face" ;
302       } else {
303         return "GEOM::FACE" ;
304       }
305     }
306   case TopAbs_WIRE:
307     { return "Wire" ;}
308   case TopAbs_EDGE:
309     { 
310       BRepAdaptor_Curve curv(TopoDS::Edge(shape));
311       if ( curv.GetType() == GeomAbs_Line ) {
312         if ( (Abs(curv.FirstParameter()) >= 1E6 ) || 
313              (Abs(curv.LastParameter()) >= 1E6 )) {
314           return "Line" ;
315         } else
316           return "Edge" ;
317       } else if ( curv.GetType() == GeomAbs_Circle ) {
318         if ( curv.IsClosed() )
319           return "Circle" ;
320         else
321           return "Arc" ;
322       } else {
323         return "Edge" ;
324       }
325     }
326   case TopAbs_VERTEX:
327     { return "Vertex" ;}
328   case TopAbs_SHAPE:
329     { return "Shape" ;}
330   }
331   return 0;
332 }
333
334
335 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
336 {
337   GEOM::GEOM_Gen_var Geom   = GeometryGUI::GetGeomGen();
338   if ( CORBA::is_nil( Geom ) )
339     return "None";
340   
341   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
342   TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
343
344   if( shape.IsNull() ) {
345     return "None" ;
346   }
347       
348   switch (shape.ShapeType() )
349     {
350     case TopAbs_COMPOUND:
351       { return "ICON_OBJBROWSER_COMPOUND" ;}
352     case  TopAbs_COMPSOLID:
353       { return "ICON_OBJBROWSER_COMPSOLID" ;}
354     case TopAbs_SOLID:
355       { return "ICON_OBJBROWSER_SOLID" ;}
356     case TopAbs_SHELL:
357       { return "ICON_OBJBROWSER_SHELL" ;}
358     case TopAbs_FACE:
359       { return "ICON_OBJBROWSER_FACE" ;}
360     case TopAbs_WIRE:
361       { return "ICON_OBJBROWSER_WIRE" ;}
362     case TopAbs_EDGE:
363       { return "ICON_OBJBROWSER_EDGE" ;}
364     case TopAbs_VERTEX:
365       { return "ICON_OBJBROWSER_VERTEX" ;}
366     }
367   
368   return "None";
369 }
370
371 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
372 {
373   SUIT_Application* app = SUIT_Session::session()->activeApplication();
374   if ( !app ) return;
375   
376   Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
377   
378   class TEvent: public SALOME_Event{
379     SUIT_Application* myApp;
380     Handle(SALOME_InteractiveObject) myIO;
381     int myParam;
382   public:
383     TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, int theParam):
384       myApp(theApp), myIO(theIO), myParam(theParam)
385     {}
386     virtual void Execute(){
387       if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
388         SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
389         myRenderInter->SetDisplayMode(myIO,myParam);
390         myRenderInter->Update();
391       }
392       else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
393         SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
394         if (soccViewer)
395           soccViewer->switchRepresentation(myIO,myParam);
396       }
397     }
398   };
399
400   ProcessVoidEvent(new TEvent(app,anIO,theMode));
401 }
402
403 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
404 {
405   SUIT_Application* app = SUIT_Session::session()->activeApplication();
406   if ( !app ) return;
407
408   Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
409
410   QColor aColor(red,green,blue);
411
412   class TEvent: public SALOME_Event{
413     SUIT_Application* myApp;
414     Handle(SALOME_InteractiveObject) myIO;
415     QColor myParam;
416   public:
417     TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, const QColor& theParam):
418       myApp(theApp), myIO(theIO), myParam(theParam)
419     {}
420     virtual void Execute(){
421       if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
422         SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
423         myRenderInter->SetColor(myIO,myParam);
424         myRenderInter->Update();
425       }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
426         Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
427         AIS_ListOfInteractive List;
428         ic->DisplayedObjects(List);
429         AIS_ListIteratorOfListOfInteractive ite(List);
430         for ( ; ite.More(); ite.Next() ) {
431           Handle(SALOME_InteractiveObject) anObj =
432             Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
433           if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( myIO ) ) {
434             Quantity_Color CSFColor = Quantity_Color ( myParam.red()   / 255.,
435                                                        myParam.green() / 255.,
436                                                        myParam.blue()  / 255.,
437                                                        Quantity_TOC_RGB );
438             ite.Value()->SetColor( CSFColor );
439             if ( ite.Value()->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
440               Handle(GEOM_AISShape)::DownCast( ite.Value() )->SetShadingColor( CSFColor );
441             ite.Value()->Redisplay( Standard_True );
442             occViewer->update();
443             break;
444           }
445         }
446       }
447     }
448   };
449   ProcessVoidEvent(new TEvent(app,anIO,aColor));
450 }
451
452 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
453 {
454   SUIT_Application* app = SUIT_Session::session()->activeApplication();
455   if ( !app ) return;
456   
457   Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
458   
459   class TEvent: public SALOME_Event{
460     SUIT_Application* myApp;
461     Handle(SALOME_InteractiveObject) myIO;
462     float myParam;
463   public:
464     TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, float theParam):
465       myApp(theApp), myIO(theIO), myParam(theParam)
466     {}
467     virtual void Execute(){
468       if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
469         SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
470         myRenderInter->SetTransparency(myIO,myParam);
471         myRenderInter->Update();
472       }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
473         SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
474         if (soccViewer)
475           soccViewer->setTransparency(myIO,myParam);
476       }
477     }
478   };
479   
480   ProcessVoidEvent(new TEvent(app,anIO,transp));
481 }
482
483
484 bool GEOM_Swig::initGeomGen()
485 {
486   return GeometryGUI::InitGeomGen();
487 }