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