Salome HOME
0e75e9aeca11d825fc9fb7a9967ac1f29ee362d3
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 #include "GEOM_Displayer.h"
56
57 #include "SALOME_Event.hxx"
58
59 #include "utilities.h"
60
61 #include "SALOMEDSClient.hxx"
62
63 // OCCT Includes
64 #include <TopAbs.hxx>
65 #include <TopoDS_Shape.hxx>
66 #include <AIS_ListOfInteractive.hxx>
67 #include <AIS_ListIteratorOfListOfInteractive.hxx>
68 #include <AIS_Drawer.hxx>
69
70 // IDL Headers
71 #include <SALOMEconfig.h>
72 #include CORBA_SERVER_HEADER(GEOM_Gen)
73
74 #include <vtkRenderer.h>
75
76 using namespace std;
77
78
79 static GEOM_Client ShapeReader;
80
81 inline OCCViewer_Viewer* GetOCCViewer(SUIT_Application* theApp){
82   SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
83   if(window && window->getViewManager()->getType() == OCCViewer_Viewer::Type()){
84     OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( window );
85     if ( vw ) {
86       OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
87       if ( vm )
88         return vm->getOCCViewer();
89     }
90   }
91
92   return 0;
93 }
94
95 inline SVTK_ViewWindow* GetSVTKViewWindow(SUIT_Application* theApp){
96   SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
97   if(window && window->getViewManager()->getType() == SVTK_Viewer::Type())
98     return dynamic_cast<SVTK_ViewWindow*>( window );
99
100   return 0;
101 }
102
103 GEOM_Swig::GEOM_Swig()
104 {
105   // MESSAGE("Constructeur");
106 }
107
108 GEOM_Swig::~GEOM_Swig()
109 {
110   // MESSAGE("Destructeur");
111 }
112
113 void GEOM_Swig::createAndDisplayGO (const char* Entry)
114 {
115   class TEvent: public SALOME_Event
116   {
117     std::string myEntry;
118   public:
119     TEvent(const char* theEntry):
120       myEntry(theEntry)
121     {}
122     virtual void Execute()
123     {
124       SUIT_Application* app = SUIT_Session::session()->activeApplication();
125       if (!app) return;
126
127       SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
128       if (!ActiveStudy) return;
129
130       _PTR(Study) aStudy(ActiveStudy->studyDS());
131       _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
132
133       GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
134       if (CORBA::is_nil(Geom)) {
135         GeometryGUI::InitGeomGen();
136         Geom = GeometryGUI::GetGeomGen();
137       }
138       if (CORBA::is_nil(Geom))
139         return;
140
141       string aFatherIOR;
142       _PTR(SComponent) father = aStudy->FindComponent("GEOM");
143       if (!father)
144         return;
145       if (!father->ComponentIOR(aFatherIOR)) {
146         aStudyBuilder->LoadWith(father, SalomeApp_Application::orb()->object_to_string(Geom));
147         father->ComponentIOR(aFatherIOR);
148       }
149
150       _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
151       if (!obj)
152         return;
153
154       // Create new actor
155       _PTR(GenericAttribute) anAttr;
156       if (!obj->FindAttribute(anAttr, "AttributeIOR"))
157         return;
158       _PTR(AttributeIOR) anIOR(anAttr);
159       string anIORValue = anIOR->Value();
160
161       GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
162       TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
163       if (!Shape.IsNull()) {
164         if (obj->FindAttribute(anAttr, "AttributeName")) {
165           _PTR(AttributeName) aName (anAttr);
166           string aNameValue = aName->Value();
167           // open transaction
168           /*SUIT_Operation* op = new SalomeApp_ImportOperation (app);
169           op->start();
170
171           _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
172           aStudyBuilder->Addreference(newObj1, obj);
173           // commit transaction
174           op->commit();*/
175           Handle(GEOM_InteractiveObject) anIO =
176             new GEOM_InteractiveObject (const_cast<char*>(anIORValue.c_str()),
177                                         const_cast<char*>(aFatherIOR.c_str()),
178                                         "GEOM",
179                                         const_cast<char*>( obj->GetID().c_str()));
180
181           GEOM_Displayer(ActiveStudy).Display(anIO, true);
182           /*if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app)) {
183             SVTK_View* aView = aViewWindow->getView();
184             int aMode = aView->GetDisplayMode();
185
186             vtkActorCollection* theActors =
187               GEOM_AssemblyBuilder::BuildActors(Shape,0,aMode,true);
188             theActors->InitTraversal();
189             while (vtkActor* anActor = theActors->GetNextActor()) {
190               GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
191               GActor->setName(const_cast<char*>(aNameValue.c_str()));
192               GActor->setIO(anIO);
193               aView->Display(GActor);
194             }
195             aView->Repaint();
196           } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app)) {
197             Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
198             Handle(GEOM_AISShape) aSh =
199               new GEOM_AISShape (Shape,const_cast<char*>(aNameValue.c_str()));
200             aSh->setName(const_cast<char*>(aNameValue.c_str()));
201             aSh->setIO(anIO);
202             ic->Display(aSh);
203             ic->AddOrRemoveCurrentObject(aSh,true);
204             }*/
205           // update object browser
206           SalomeApp_Application* app = NULL; //dynamic_cast<SalomeApp_Application*>(app);
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   };
217
218   // MESSAGE("createAndDisplayGO");
219   ProcessVoidEvent(new TEvent (Entry));
220
221   class TEventUpdateBrowser: public SALOME_Event
222     {
223       public:
224         TEventUpdateBrowser() {}
225         virtual void Execute() {
226           SalomeApp_Application* app =
227             dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
228           if (app) {
229             CAM_Module* module = app->module("Geometry");
230             SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
231             if (appMod) appMod->updateObjBrowser(true);
232           }
233         }
234     };
235
236   ProcessVoidEvent(new TEventUpdateBrowser ());
237 }
238
239 void GEOM_Swig::createAndDisplayFitAllGO (const char* Entry)
240 {
241   class TEventFitAll: public SALOME_Event
242   {
243     public:
244       TEventFitAll() {}
245       virtual void Execute() {
246         SUIT_Application* app = SUIT_Session::session()->activeApplication();
247         if (!app) return;
248         
249         if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app))
250           {
251             SVTK_View* aView = aViewWindow->getView();
252             aView->GetRenderer()->OnFitAll();
253           }
254         else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app))
255           {  
256             Handle(V3d_Viewer) aViewer3d = occViewer->getViewer3d();
257             aViewer3d->InitActiveViews();
258             
259             if (aViewer3d->MoreActiveViews())
260               aViewer3d->ActiveView()->FitAll();
261           }
262       }
263   };
264
265   createAndDisplayGO(Entry);
266   ProcessVoidEvent(new TEventFitAll());
267 }
268
269 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
270 {
271   GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
272   if (CORBA::is_nil(aGeomGen))
273     return -1;
274
275   GEOM::GEOM_Object_var aMainShape = aGeomGen->GetIORFromString(IOR);
276   GEOM::GEOM_Object_var aSubShape  = aGeomGen->GetIORFromString(SubIOR);
277   if (CORBA::is_nil(aMainShape) || CORBA::is_nil(aSubShape))
278     return -1;
279
280   GEOM::GEOM_IShapesOperations_var anIShapesOperations =
281     aGeomGen->GetIShapesOperations(aMainShape->GetStudyID());
282   if (CORBA::is_nil(anIShapesOperations))
283     return -1;
284
285   return anIShapesOperations->GetTopologyIndex(aMainShape, aSubShape);
286 }
287
288 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
289 {
290   TCollection_AsciiString aTypeName ("Shape of unknown type");
291
292   GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
293   if (!CORBA::is_nil(aGeomGen))
294   {
295     GEOM::GEOM_Object_var aShape = aGeomGen->GetIORFromString(IOR);
296     if (!CORBA::is_nil(aShape))
297     {
298       GEOM::GEOM_IShapesOperations_var anIShapesOperations =
299         aGeomGen->GetIShapesOperations(aShape->GetStudyID());
300       if (!CORBA::is_nil(anIShapesOperations))
301       {
302         aTypeName = anIShapesOperations->GetShapeTypeString(aShape);
303       }
304     }
305   }
306
307   return CORBA::string_dup(aTypeName.ToCString());
308 }
309
310
311 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
312 {
313   GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
314   if ( CORBA::is_nil( Geom ) )
315     return "None";
316
317   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
318   TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
319
320   if( shape.IsNull() ) {
321     return "None" ;
322   }
323
324   switch (shape.ShapeType() )
325     {
326     case TopAbs_COMPOUND:
327       { return "ICON_OBJBROWSER_COMPOUND" ;}
328     case  TopAbs_COMPSOLID:
329       { return "ICON_OBJBROWSER_COMPSOLID" ;}
330     case TopAbs_SOLID:
331       { return "ICON_OBJBROWSER_SOLID" ;}
332     case TopAbs_SHELL:
333       { return "ICON_OBJBROWSER_SHELL" ;}
334     case TopAbs_FACE:
335       { return "ICON_OBJBROWSER_FACE" ;}
336     case TopAbs_WIRE:
337       { return "ICON_OBJBROWSER_WIRE" ;}
338     case TopAbs_EDGE:
339       { return "ICON_OBJBROWSER_EDGE" ;}
340     case TopAbs_VERTEX:
341       { return "ICON_OBJBROWSER_VERTEX" ;}
342     }
343
344   return "None";
345 }
346
347 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
348 {
349   class TEvent: public SALOME_Event {
350     std::string myEntry;
351     int myMode;
352   public:
353     TEvent(const char* theEntryArg, int theModeArg):
354       myEntry(theEntryArg), myMode(theModeArg)
355     {}
356     virtual void Execute() {
357       SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
358       if (!anApp) return;
359
360       Handle(SALOME_InteractiveObject) anIO =
361         new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
362
363       if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
364         SVTK_View* aView = aViewWindow->getView();
365         aView->SetDisplayMode(anIO, myMode);
366         aView->Repaint();
367       }
368       else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
369         SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
370         if (soccViewer)
371           soccViewer->switchRepresentation(anIO, myMode);
372       }
373     }
374   };
375
376   ProcessVoidEvent(new TEvent (theEntry, theMode));
377 }
378
379 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
380 {
381   class TEvent: public SALOME_Event {
382     std::string myEntry;
383     int myRed;
384     int myGreen;
385     int myBlue;
386   public:
387     TEvent(const char* theEntryArg, int theR, int theG, int theB):
388       myEntry(theEntryArg), myRed(theR), myGreen(theG), myBlue(theB)
389     {}
390     virtual void Execute() {
391       SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
392       if (!anApp) return;
393
394       Handle(SALOME_InteractiveObject) anIO =
395         new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
396
397       if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)){
398         SVTK_View* aView = aViewWindow->getView();
399         QColor aColor (myRed, myGreen, myBlue);
400         aView->SetColor(anIO, aColor);
401         aView->Repaint();
402       } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
403         Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
404         AIS_ListOfInteractive List;
405         ic->DisplayedObjects(List);
406         AIS_ListIteratorOfListOfInteractive ite (List);
407         for (; ite.More(); ite.Next()) {
408           Handle(SALOME_InteractiveObject) anObj =
409             Handle(SALOME_InteractiveObject)::DownCast(ite.Value()->GetOwner());
410           if (!anObj.IsNull() && anObj->hasEntry() && anObj->isSame(anIO)) {
411             Quantity_Color CSFColor =
412               Quantity_Color(myRed/255., myGreen/255., myBlue/255., Quantity_TOC_RGB);
413             ite.Value()->SetColor(CSFColor);
414             if (ite.Value()->IsKind(STANDARD_TYPE(GEOM_AISShape)))
415               Handle(GEOM_AISShape)::DownCast(ite.Value())->SetShadingColor(CSFColor);
416             ite.Value()->Redisplay(Standard_True);
417             occViewer->update();
418             break;
419           }
420         }
421       }
422     }
423   };
424   ProcessVoidEvent(new TEvent(theEntry, red, green, blue));
425 }
426
427 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
428 {
429   class TEvent: public SALOME_Event {
430     std::string myEntry;
431     float myParam;
432   public:
433     TEvent(const char* theEntryArg, float theParam):
434       myEntry(theEntryArg), myParam(theParam)
435     {}
436     virtual void Execute() {
437       SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
438       if (!anApp) return;
439
440       Handle(SALOME_InteractiveObject) anIO =
441         new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
442
443       if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
444         SVTK_View* aView = aViewWindow->getView();
445         aView->SetTransparency(anIO, myParam);
446         aView->Repaint();
447       } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
448         SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
449         if (soccViewer)
450           soccViewer->setTransparency(anIO, myParam);
451       }
452     }
453   };
454
455   ProcessVoidEvent(new TEvent (theEntry, transp));
456 }
457
458
459 class TInitGeomGenEvent: public SALOME_Event {
460 public:
461   typedef bool TResult;
462   TResult myResult;
463   TInitGeomGenEvent() : myResult(false) {}
464   virtual void Execute() {
465     myResult = GeometryGUI::InitGeomGen();
466   }
467 };
468 bool GEOM_Swig::initGeomGen()
469 {
470   return ProcessEvent(new TInitGeomGenEvent());
471 }
472
473
474
475 void GEOM_Swig::eraseGO (const char* Entry, bool allWindows)
476 {
477   class TEvent: public SALOME_Event
478   {
479     std::string myEntry;
480     bool myFromAllWindows;
481   public:
482     TEvent(const char* theEntry, bool fromAllWindows):
483       myEntry(theEntry), myFromAllWindows(fromAllWindows)
484     {}
485     virtual void Execute()
486     {
487       SUIT_Application* app = SUIT_Session::session()->activeApplication();
488       if (!app) return;
489       SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
490       if (!ActiveStudy) return;
491
492       Handle (SALOME_InteractiveObject) aIO = new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
493
494       GEOM_Displayer(ActiveStudy).Erase(aIO, true);
495       /*      if (myFromAllWindows) {
496         QPtrList<SUIT_ViewWindow> aWindows = app->desktop()->windows();
497         SUIT_ViewWindow* aWin = 0;
498         for (aWin = aWindows.first(); aWin; aWin = aWindows.next()) {
499           EraseObject(aWin, aIO);
500         }
501       } else {
502         SUIT_ViewWindow* aWin = app->desktop()->activeWindow();
503         if (aWin)
504           EraseObject(aWin, aIO);
505           }*/
506     }
507
508     /*  private:
509     void EraseObject(SUIT_ViewWindow* theWin, Handle (SALOME_InteractiveObject) theIO)
510     {
511       if (theWin->getViewManager()->getType() == OCCViewer_Viewer::Type()){
512         OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( theWin );
513         if ( vw ) {
514           OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
515           if ( vm ) {
516             SOCC_Viewer* aViewer = dynamic_cast<SOCC_Viewer*>(vm->getOCCViewer());
517             if (aViewer) {
518               SALOME_Prs* aPrs = aViewer->CreatePrs(myEntry.c_str());
519               if (aPrs) {
520                 SALOME_OCCPrs* aOccPrs =  dynamic_cast<SALOME_OCCPrs*>(aPrs);
521                 if (aOccPrs) {
522                   aViewer->Erase(aOccPrs);
523                   aViewer->Repaint();
524                 }
525               }
526             }
527           }
528         }
529       } else if (theWin->getViewManager()->getType() == SVTK_Viewer::Type()){
530         SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>( theWin );
531         if (aViewWindow) {
532           aViewWindow->Erase(theIO);
533         }
534       }
535       }*/
536
537   };
538   ProcessVoidEvent(new TEvent(Entry, allWindows));
539 }
540
541
542
543 void GEOM_Swig::setDeflection(const char* theEntry, float theDeflect)
544 {
545   class TEvent: public SALOME_Event {
546     std::string myEntry;
547     float myParam;
548   public:
549     TEvent(const char* theEntryArg, float theParam):
550       myEntry(theEntryArg), myParam(theParam)
551     {}
552     virtual void Execute() {
553       SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
554       if (!anApp) return;
555
556       Handle(SALOME_InteractiveObject) anIO =
557         new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
558
559       if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
560         vtkActorCollection* aActors = aViewWindow->getRenderer()->GetActors();
561         aActors->InitTraversal();
562         while (vtkActor* aAct = aActors->GetNextActor()) {
563           if (GEOM_Actor* aGeomActor = dynamic_cast<GEOM_Actor*>(aAct)) {
564             if (aGeomActor->hasIO()) {
565               Handle(SALOME_InteractiveObject) aNextIO = aGeomActor->getIO();
566               if (aNextIO->isSame(anIO)) {
567                 aGeomActor->setDeflection(myParam);
568                 aViewWindow->Repaint();
569                 return;
570               }
571             }
572           }
573         }
574         //      aView->SetTransparency(anIO, myParam);
575         //aView->Repaint();
576       } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
577         Handle(AIS_InteractiveContext) aContext = occViewer->getAISContext();
578         AIS_ListOfInteractive aAISList;
579         aContext->DisplayedObjects(aAISList);
580         AIS_ListIteratorOfListOfInteractive it(aAISList);
581         for (; it.More(); it.Next()) {
582           Handle(SALOME_InteractiveObject) aObj = 
583             Handle(SALOME_InteractiveObject)::DownCast(it.Value()->GetOwner());
584           if ((!aObj.IsNull()) && aObj->hasEntry() && aObj->isSame(anIO)) {
585             Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(it.Value());
586             if (!aShape.IsNull()) {
587               Handle(AIS_Drawer) aDrawer = aShape->Attributes();
588               if (aDrawer.IsNull())
589                 aDrawer = new AIS_Drawer();
590               aDrawer->SetDeviationCoefficient(myParam);
591               aShape->SetAttributes(aDrawer);
592               aContext->Redisplay(aShape, true, true);
593               aContext->UpdateCurrentViewer();
594               return;
595             }
596           }
597         }
598       }
599     }
600   };
601
602   ProcessVoidEvent(new TEvent (theEntry, theDeflect));
603 }
604