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