]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMGUI/GeometryGUI_Swig.cxx
Salome HOME
Fix for Bug IPAL9161(3.0.0 (09 June): It is impossible to perform Fillet and Chamfer...
[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) fatherSF;
140
141   _PTR(SObject) obj = aStudy->FindObjectID(Entry);
142   _PTR(GenericAttribute) anAttr;
143   
144   // Create new actor
145   if ( !obj->FindAttribute(anAttr, "AttributeIOR")) 
146     return;
147   _PTR(AttributeIOR) anIOR(anAttr);
148   string anIORValue = anIOR->Value();
149   
150   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
151   TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
152   
153   if ( obj ) {
154     if (obj->FindAttribute(anAttr, "AttributeName")) {
155       _PTR(AttributeName) aName ( anAttr );
156       string aNameValue = aName->Value();
157       // open transaction
158       SUIT_Operation* op = new SalomeApp_ImportOperation( app );
159       op->start();
160       
161       _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
162       aStudyBuilder->Addreference(newObj1, obj);
163       // commit transaction
164       op->commit();
165                   
166       Handle(GEOM_InteractiveObject) anIO = new GEOM_InteractiveObject(const_cast<char*>(anIORValue.c_str()),
167                                                                        const_cast<char*>(aFatherIOR.c_str()),
168                                                                        "GEOM",
169                                                                        const_cast<char*>( obj->GetID().c_str() ));
170       
171       class TEvent: public SALOME_Event{
172         SUIT_Application* myApp;
173         TopoDS_Shape myShape;
174         Handle(SALOME_InteractiveObject) myIO;
175         const char* myName;
176       public:
177         TEvent(SUIT_Application* theApp, TopoDS_Shape theShape,
178                const Handle(SALOME_InteractiveObject)& theIO, 
179                const char* theName):
180           myApp(theApp), myShape(theShape),
181           myIO(theIO), myName(theName)
182         {}
183         virtual void Execute(){
184           if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
185             SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
186             int aMode = myRenderInter->GetDisplayMode();
187             
188             vtkActorCollection* theActors = GEOM_AssemblyBuilder::BuildActors(myShape,0,aMode,true);
189             theActors->InitTraversal();
190             while(vtkActor* anActor = theActors->GetNextActor()){
191               GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
192               GActor->setName(const_cast<char*>(myName));
193               GActor->setIO(myIO);
194               myRenderInter->Display(GActor);
195             }
196             myRenderInter->Update();
197           }else if( OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
198             Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
199             Handle(GEOM_AISShape) aSh = new GEOM_AISShape(myShape,const_cast<char*>(myName));
200             aSh->setName(const_cast<char*>(myName));
201             aSh->setIO(myIO);
202             ic->Display(aSh);
203             ic->AddOrRemoveCurrentObject(aSh,true);
204           }
205           // update object browser
206           SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(myApp);
207           if (app) {
208             CAM_Module* module = app->module( "Geometry" );
209             SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
210             if ( appMod )
211               appMod->updateObjBrowser( true );
212           }
213           
214         }
215       };
216       ProcessVoidEvent(new TEvent(app,Shape,anIO,aNameValue.c_str()));
217     }
218   }
219 }
220
221
222 int  GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
223 {
224   GEOM::GEOM_Gen_var Geom   = GeometryGUI::GetGeomGen();
225   if ( CORBA::is_nil( Geom ) )
226     return -1;
227   
228   GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
229   TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
230
231   GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
232   TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
233
234   int index = 1;
235   if(subshape.ShapeType() == TopAbs_COMPOUND) { 
236     TopoDS_Iterator it; 
237     TopTools_ListOfShape CL; 
238     CL.Append(shape); 
239     TopTools_ListIteratorOfListOfShape itC; 
240     for(itC.Initialize(CL); itC.More(); itC.Next()) {
241       for(it.Initialize(itC.Value()); it.More(); it.Next()) { 
242         if (it.Value().ShapeType() == TopAbs_COMPOUND) {
243           if (it.Value().IsSame(subshape))
244             return index; 
245           else 
246             index++; 
247           CL.Append(it.Value()); 
248         }
249       } 
250     } 
251   } 
252   else { 
253     TopExp_Explorer Exp(shape, subshape.ShapeType()); 
254     TopTools_MapOfShape M; 
255     while(Exp.More()) { 
256       if(M.Add(Exp.Current())) {
257         if(Exp.Current().IsSame(subshape)) 
258           return index; 
259         index++; 
260       } 
261       Exp.Next(); 
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 }