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