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