Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[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_View.h"
48 #include "SVTK_Renderer.h"
49
50 #include "GEOM_Actor.h"
51 #include "GEOM_Client.hxx"
52 #include "GEOM_AISShape.hxx"
53 #include "GEOM_AssemblyBuilder.h"
54 #include "GEOM_InteractiveObject.hxx"
55
56 #include "SALOME_Event.hxx"
57
58 #include "utilities.h"
59
60 #include "SALOMEDSClient.hxx"
61
62 // OCCT Includes
63 #include <TopAbs.hxx>
64 #include <TopoDS_Shape.hxx>
65 #include <AIS_ListOfInteractive.hxx>
66 #include <AIS_ListIteratorOfListOfInteractive.hxx>
67
68 // IDL Headers
69 #include <SALOMEconfig.h>
70 #include CORBA_SERVER_HEADER(GEOM_Gen)
71
72 using namespace std;
73
74
75 static GEOM_Client ShapeReader;
76
77 inline OCCViewer_Viewer* GetOCCViewer(SUIT_Application* theApp){
78   SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
79   if(window && window->getViewManager()->getType() == OCCViewer_Viewer::Type()){
80     OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( window );
81     if ( vw ) {
82       OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
83       if ( vm )
84         return vm->getOCCViewer();
85     }
86   }
87
88   return 0;
89 }
90
91 inline SVTK_ViewWindow* GetSVTKViewWindow(SUIT_Application* theApp){
92   SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
93   if(window && window->getViewManager()->getType() == SVTK_Viewer::Type())
94     return dynamic_cast<SVTK_ViewWindow*>( window );
95
96   return 0;
97 }
98
99 GEOM_Swig::GEOM_Swig()
100 {
101   // MESSAGE("Constructeur");
102 }
103
104 GEOM_Swig::~GEOM_Swig()
105 {
106   // MESSAGE("Destructeur");
107 }
108
109 void GEOM_Swig::createAndDisplayGO (const char* Entry)
110 {
111   class TEvent: public SALOME_Event
112   {
113     std::string myEntry;
114   public:
115     TEvent(const char* theEntry):
116       myEntry(theEntry)
117     {}
118     virtual void Execute()
119     {
120       SUIT_Application* app = SUIT_Session::session()->activeApplication();
121       if (!app) return;
122
123       SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
124       if (!ActiveStudy) return;
125
126       _PTR(Study) aStudy(ActiveStudy->studyDS());
127       _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
128
129       GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
130       if (CORBA::is_nil(Geom)) {
131         GeometryGUI::InitGeomGen();
132         Geom = GeometryGUI::GetGeomGen();
133       }
134       if (CORBA::is_nil(Geom))
135         return;
136
137       string aFatherIOR;
138       _PTR(SComponent) father = aStudy->FindComponent("GEOM");
139       if (!father)
140         return;
141       if (!father->ComponentIOR(aFatherIOR)) {
142         aStudyBuilder->LoadWith(father, SalomeApp_Application::orb()->object_to_string(Geom));
143         father->ComponentIOR(aFatherIOR);
144       }
145
146       _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
147       if (!obj)
148         return;
149
150       // Create new actor
151       _PTR(GenericAttribute) anAttr;
152       if (!obj->FindAttribute(anAttr, "AttributeIOR"))
153         return;
154       _PTR(AttributeIOR) anIOR(anAttr);
155       string anIORValue = anIOR->Value();
156
157       GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
158       TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
159       if (!Shape.IsNull()) {
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           Handle(GEOM_InteractiveObject) anIO =
172             new GEOM_InteractiveObject (const_cast<char*>(anIORValue.c_str()),
173                                         const_cast<char*>(aFatherIOR.c_str()),
174                                         "GEOM",
175                                         const_cast<char*>( obj->GetID().c_str()));
176
177           if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app)) {
178             SVTK_View* aView = aViewWindow->getView();
179             int aMode = aView->GetDisplayMode();
180
181             vtkActorCollection* theActors =
182               GEOM_AssemblyBuilder::BuildActors(Shape,0,aMode,true);
183             theActors->InitTraversal();
184             while (vtkActor* anActor = theActors->GetNextActor()) {
185               GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
186               GActor->setName(const_cast<char*>(aNameValue.c_str()));
187               GActor->setIO(anIO);
188               aView->Display(GActor);
189             }
190             aView->Repaint();
191           } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app)) {
192             Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
193             Handle(GEOM_AISShape) aSh =
194               new GEOM_AISShape (Shape,const_cast<char*>(aNameValue.c_str()));
195             aSh->setName(const_cast<char*>(aNameValue.c_str()));
196             aSh->setIO(anIO);
197             ic->Display(aSh);
198             ic->AddOrRemoveCurrentObject(aSh,true);
199           }
200           // update object browser
201           SalomeApp_Application* app = NULL; //dynamic_cast<SalomeApp_Application*>(app);
202           if (app) {
203             CAM_Module* module = app->module("Geometry");
204             SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
205             if (appMod)
206               appMod->updateObjBrowser(true);
207           }
208         }
209       }
210     }
211   };
212
213   // MESSAGE("createAndDisplayGO");
214   ProcessVoidEvent(new TEvent (Entry));
215
216   class TEventUpdateBrowser: public SALOME_Event
217     {
218       public:
219         TEventUpdateBrowser() {}
220         virtual void Execute() {
221           SalomeApp_Application* app =
222             dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
223           if (app) {
224             CAM_Module* module = app->module("Geometry");
225             SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
226             if (appMod) appMod->updateObjBrowser(true);
227           }
228         }
229     };
230
231   ProcessVoidEvent(new TEventUpdateBrowser ());
232 }
233
234 void GEOM_Swig::createAndDisplayFitAllGO (const char* Entry)
235 {
236   class TEventFitAll: public SALOME_Event
237   {
238     public:
239       TEventFitAll() {}
240       virtual void Execute() {
241         SUIT_Application* app = SUIT_Session::session()->activeApplication();
242         if (!app) return;
243         
244         if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app))
245           {
246             SVTK_View* aView = aViewWindow->getView();
247             aView->GetRenderer()->OnFitAll();
248           }
249         else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app))
250           {  
251             Handle(V3d_Viewer) aViewer3d = occViewer->getViewer3d();
252             aViewer3d->InitActiveViews();
253             
254             if (aViewer3d->MoreActiveViews())
255               aViewer3d->ActiveView()->FitAll();
256           }
257       }
258   };
259
260   createAndDisplayGO(Entry);
261   ProcessVoidEvent(new TEventFitAll());
262 }
263
264 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
265 {
266   GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
267   if (CORBA::is_nil(aGeomGen))
268     return -1;
269
270   GEOM::GEOM_Object_var aMainShape = aGeomGen->GetIORFromString(IOR);
271   GEOM::GEOM_Object_var aSubShape  = aGeomGen->GetIORFromString(SubIOR);
272   if (CORBA::is_nil(aMainShape) || CORBA::is_nil(aSubShape))
273     return -1;
274
275   GEOM::GEOM_IShapesOperations_var anIShapesOperations =
276     aGeomGen->GetIShapesOperations(aMainShape->GetStudyID());
277   if (CORBA::is_nil(anIShapesOperations))
278     return -1;
279
280   return anIShapesOperations->GetTopologyIndex(aMainShape, aSubShape);
281 }
282
283 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
284 {
285   TCollection_AsciiString aTypeName ("Shape of unknown type");
286
287   GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
288   if (!CORBA::is_nil(aGeomGen))
289   {
290     GEOM::GEOM_Object_var aShape = aGeomGen->GetIORFromString(IOR);
291     if (!CORBA::is_nil(aShape))
292     {
293       GEOM::GEOM_IShapesOperations_var anIShapesOperations =
294         aGeomGen->GetIShapesOperations(aShape->GetStudyID());
295       if (!CORBA::is_nil(anIShapesOperations))
296       {
297         aTypeName = anIShapesOperations->GetShapeTypeString(aShape);
298       }
299     }
300   }
301
302   return CORBA::string_dup(aTypeName.ToCString());
303 }
304
305
306 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
307 {
308   GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
309   if ( CORBA::is_nil( Geom ) )
310     return "None";
311
312   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
313   TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
314
315   if( shape.IsNull() ) {
316     return "None" ;
317   }
318
319   switch (shape.ShapeType() )
320     {
321     case TopAbs_COMPOUND:
322       { return "ICON_OBJBROWSER_COMPOUND" ;}
323     case  TopAbs_COMPSOLID:
324       { return "ICON_OBJBROWSER_COMPSOLID" ;}
325     case TopAbs_SOLID:
326       { return "ICON_OBJBROWSER_SOLID" ;}
327     case TopAbs_SHELL:
328       { return "ICON_OBJBROWSER_SHELL" ;}
329     case TopAbs_FACE:
330       { return "ICON_OBJBROWSER_FACE" ;}
331     case TopAbs_WIRE:
332       { return "ICON_OBJBROWSER_WIRE" ;}
333     case TopAbs_EDGE:
334       { return "ICON_OBJBROWSER_EDGE" ;}
335     case TopAbs_VERTEX:
336       { return "ICON_OBJBROWSER_VERTEX" ;}
337     }
338
339   return "None";
340 }
341
342 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
343 {
344   class TEvent: public SALOME_Event {
345     std::string myEntry;
346     int myMode;
347   public:
348     TEvent(const char* theEntryArg, int theModeArg):
349       myEntry(theEntryArg), myMode(theModeArg)
350     {}
351     virtual void Execute() {
352       SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
353       if (!anApp) return;
354
355       Handle(SALOME_InteractiveObject) anIO =
356         new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
357
358       if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
359         SVTK_View* aView = aViewWindow->getView();
360         aView->SetDisplayMode(anIO, myMode);
361         aView->Repaint();
362       }
363       else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
364         SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
365         if (soccViewer)
366           soccViewer->switchRepresentation(anIO, myMode);
367       }
368     }
369   };
370
371   ProcessVoidEvent(new TEvent (theEntry, theMode));
372 }
373
374 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
375 {
376   class TEvent: public SALOME_Event {
377     std::string myEntry;
378     int myRed;
379     int myGreen;
380     int myBlue;
381   public:
382     TEvent(const char* theEntryArg, int theR, int theG, int theB):
383       myEntry(theEntryArg), myRed(theR), myGreen(theG), myBlue(theB)
384     {}
385     virtual void Execute() {
386       SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
387       if (!anApp) return;
388
389       Handle(SALOME_InteractiveObject) anIO =
390         new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
391
392       if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)){
393         SVTK_View* aView = aViewWindow->getView();
394         QColor aColor (myRed, myGreen, myBlue);
395         aView->SetColor(anIO, aColor);
396         aView->Repaint();
397       } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
398         Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
399         AIS_ListOfInteractive List;
400         ic->DisplayedObjects(List);
401         AIS_ListIteratorOfListOfInteractive ite (List);
402         for (; ite.More(); ite.Next()) {
403           Handle(SALOME_InteractiveObject) anObj =
404             Handle(SALOME_InteractiveObject)::DownCast(ite.Value()->GetOwner());
405           if (!anObj.IsNull() && anObj->hasEntry() && anObj->isSame(anIO)) {
406             Quantity_Color CSFColor =
407               Quantity_Color(myRed/255., myGreen/255., myBlue/255., Quantity_TOC_RGB);
408             ite.Value()->SetColor(CSFColor);
409             if (ite.Value()->IsKind(STANDARD_TYPE(GEOM_AISShape)))
410               Handle(GEOM_AISShape)::DownCast(ite.Value())->SetShadingColor(CSFColor);
411             ite.Value()->Redisplay(Standard_True);
412             occViewer->update();
413             break;
414           }
415         }
416       }
417     }
418   };
419   ProcessVoidEvent(new TEvent(theEntry, red, green, blue));
420 }
421
422 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
423 {
424   class TEvent: public SALOME_Event {
425     std::string myEntry;
426     float myParam;
427   public:
428     TEvent(const char* theEntryArg, float theParam):
429       myEntry(theEntryArg), myParam(theParam)
430     {}
431     virtual void Execute() {
432       SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
433       if (!anApp) return;
434
435       Handle(SALOME_InteractiveObject) anIO =
436         new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
437
438       if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
439         SVTK_View* aView = aViewWindow->getView();
440         aView->SetTransparency(anIO, myParam);
441         aView->Repaint();
442       } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
443         SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
444         if (soccViewer)
445           soccViewer->setTransparency(anIO, myParam);
446       }
447     }
448   };
449
450   ProcessVoidEvent(new TEvent (theEntry, transp));
451 }
452
453
454 class TInitGeomGenEvent: public SALOME_Event {
455 public:
456   typedef bool TResult;
457   TResult myResult;
458   TInitGeomGenEvent() : myResult(false) {}
459   virtual void Execute() {
460     myResult = GeometryGUI::InitGeomGen();
461   }
462 };
463 bool GEOM_Swig::initGeomGen()
464 {
465   return ProcessEvent(new TInitGeomGenEvent());
466 }