Salome HOME
PAL8376
[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 "QAD_Application.h"
33 #include "QAD_Desktop.h"
34 #include "QAD_Study.h"
35 #include "QAD_StudyFrame.h"
36 #include "QAD_RightFrame.h"
37 #include "SALOMEGUI_ImportOperation.h"
38
39 #include "OCCViewer_Viewer3d.h"
40 #include "OCCViewer_ViewFrame.h"
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_MapOfShape.hxx>
43 #include <TopTools_ListOfShape.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <BRepAdaptor_Surface.hxx>
46 #include <BRepAdaptor_Curve.hxx>
47 #include <GeomAbs_CurveType.hxx>
48 #include <GeomAbs_SurfaceType.hxx>
49 #include <TopoDS.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Face.hxx>
52 #include <TopoDS_Iterator.hxx>
53 #include <AIS_ListOfInteractive.hxx>
54 #include <AIS_ListIteratorOfListOfInteractive.hxx>
55 #include <V3d_Viewer.hxx>
56
57 #include "SALOME_Event.hxx"
58
59 #include "VTKViewer_RenderWindowInteractor.h"
60 #include "VTKViewer_ViewFrame.h"
61
62 #include "GEOM_Actor.h"
63 #include "GEOM_Client.hxx"
64 #include "GEOM_AISShape.hxx"
65 #include "GEOM_AssemblyBuilder.h"
66 #include "GEOM_InteractiveObject.hxx"
67
68 #include "utilities.h"
69
70 using namespace std;
71
72 static GEOM_Client ShapeReader;
73
74
75 template<class TViewFrame> inline TViewFrame* GetFrame(QAD_Study* theStudy){
76   return dynamic_cast<TViewFrame*>(SALOME::GetViewFrame(theStudy));
77 }
78
79
80 GEOM_Swig::GEOM_Swig()
81 {
82   // MESSAGE("Constructeur");
83 }
84
85 GEOM_Swig::~GEOM_Swig()
86 {
87   // MESSAGE("Destructeur");
88 }
89
90 void GEOM_Swig::createAndDisplayGO(const char* Entry)
91 {
92   // MESSAGE("createAndDisplayGO");
93   QAD_Study* ActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
94   SALOMEDS::Study_var aStudy = ActiveStudy->getStudyDocument();
95   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
96
97   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
98   GEOM::GEOM_Gen_var Geom = GEOM::GEOM_Gen::_narrow(comp);
99
100   CORBA::String_var aFatherIOR;
101   SALOMEDS::SComponent_var father = aStudy->FindComponent("GEOM");
102   aStudyBuilder->DefineComponentInstance( father, Geom );
103   father->ComponentIOR(aFatherIOR);
104
105   SALOMEDS::SObject_var fatherSF = aStudy->FindObjectID(ActiveStudy->getActiveStudyFrame()->entry());
106
107   SALOMEDS::SObject_var obj = aStudy->FindObjectID(Entry);
108   SALOMEDS::GenericAttribute_var anAttr;
109   SALOMEDS::AttributeIOR_var     anIOR;
110   // Create new actor
111   if ( !obj->FindAttribute(anAttr, "AttributeIOR")) 
112     return;
113   anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
114   CORBA::String_var anIORValue = anIOR->Value();
115   
116   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue);
117   TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
118   
119   if ( !obj->_is_nil() ) {
120     if (obj->FindAttribute(anAttr, "AttributeName")) {
121       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
122       CORBA::String_var aNameValue = aName->Value();
123       // open transaction
124       QAD_Operation* op = new SALOMEGUI_ImportOperation( ActiveStudy );
125       op->start();
126       
127       SALOMEDS::SObject_var newObj1 = aStudyBuilder->NewObject(fatherSF);
128       aStudyBuilder->Addreference(newObj1, obj);
129       // commit transaction
130       op->finish();
131                   
132       Handle(GEOM_InteractiveObject) anIO = new GEOM_InteractiveObject(const_cast<char*>(anIORValue.in()),
133                                                                        const_cast<char*>(aFatherIOR.in()),
134                                                                        "GEOM",
135                                                                        const_cast<char*>(obj->GetID()));
136       
137       class TEvent: public SALOME_Event{
138         QAD_Study* myStudy;
139         TopoDS_Shape myShape;
140         Handle(SALOME_InteractiveObject) myIO;
141         const char* myName;
142       public:
143         TEvent(QAD_Study* theStudy, TopoDS_Shape theShape,
144                const Handle(SALOME_InteractiveObject)& theIO, 
145                const char* theName):
146           myStudy(theStudy), myShape(theShape),
147                                    myIO(theIO), myName(theName)
148         {}
149         virtual void Execute(){
150           if(VTKViewer_ViewFrame* aViewFrame = GetFrame<VTKViewer_ViewFrame>(myStudy)){
151             VTKViewer_RenderWindowInteractor* myRenderInter= aViewFrame->getRWInteractor();
152             int aMode = myRenderInter->GetDisplayMode();
153             
154             vtkActorCollection* theActors = GEOM_AssemblyBuilder::BuildActors(myShape,0,aMode,true);
155             theActors->InitTraversal();
156             while(vtkActor* anActor = theActors->GetNextActor()){
157               GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
158               GActor->setName(const_cast<char*>(myName));
159               GActor->setIO(myIO);
160               myRenderInter->Display(GActor);
161             }
162             myRenderInter->Update();
163           }else if(OCCViewer_ViewFrame* aViewFrame = GetFrame<OCCViewer_ViewFrame>(myStudy)){
164             Handle(AIS_InteractiveContext) ic = aViewFrame->getViewer()->getAISContext();
165             Handle(GEOM_AISShape) aSh = new GEOM_AISShape(myShape,const_cast<char*>(myName));
166             aSh->setName(const_cast<char*>(myName));
167             aSh->setIO(myIO);
168             ic->Display(aSh);
169             ic->AddOrRemoveCurrentObject(aSh,true);
170           }
171           myStudy->updateObjBrowser(true);
172         }
173       };
174       ProcessVoidEvent(new TEvent(ActiveStudy,Shape,anIO,aNameValue.in()));
175     }
176   }
177 }
178
179
180 int  GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
181 {
182   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
183   GEOM::GEOM_Gen_var  Geom = GEOM::GEOM_Gen::_narrow(comp);
184  
185   GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
186   TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
187
188   GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
189   TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
190
191   int index = 1;
192   if(subshape.ShapeType() == TopAbs_COMPOUND) { 
193     TopoDS_Iterator it; 
194     TopTools_ListOfShape CL; 
195     CL.Append(shape); 
196     TopTools_ListIteratorOfListOfShape itC; 
197     for(itC.Initialize(CL); itC.More(); itC.Next()) {
198       for(it.Initialize(itC.Value()); it.More(); it.Next()) { 
199         if (it.Value().ShapeType() == TopAbs_COMPOUND) {
200           if (it.Value().IsSame(subshape))
201             return index; 
202           else 
203             index++; 
204           CL.Append(it.Value()); 
205         }
206       } 
207     } 
208   } 
209   else { 
210     TopExp_Explorer Exp(shape, subshape.ShapeType()); 
211     TopTools_MapOfShape M; 
212     while(Exp.More()) { 
213       if(M.Add(Exp.Current())) {
214         if(Exp.Current().IsSame(subshape)) 
215           return index; 
216         index++; 
217       } 
218       Exp.Next(); 
219     } 
220   } 
221   return -1;
222 }
223
224 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
225 {
226   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
227   GEOM::GEOM_Gen_var  Geom = GEOM::GEOM_Gen::_narrow(comp);
228  
229   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
230   TopoDS_Shape shape    = ShapeReader.GetShape(Geom, aShape);
231
232   if( shape.IsNull() ) {
233     return "Null Shape" ;
234   }
235
236   switch (shape.ShapeType() )
237   {
238   case TopAbs_COMPOUND:
239     { return "Compound" ;}
240   case  TopAbs_COMPSOLID:
241     { return "Compound Solid" ;}
242   case TopAbs_SOLID:
243     { return "Solid" ;}
244   case TopAbs_SHELL:
245     { return "Shell" ;}
246   case TopAbs_FACE:
247     { 
248       BRepAdaptor_Surface surf(TopoDS::Face(shape));
249       if ( surf.GetType() == GeomAbs_Plane ) {
250         return "Plane" ;
251       } else if ( surf.GetType() == GeomAbs_Cylinder ) {
252         return "Cylindrical Face" ;
253       } else if ( surf.GetType() == GeomAbs_Sphere ) {
254         return "Spherical Face" ;
255       } else if ( surf.GetType() == GeomAbs_Torus ) {
256         return "Toroidal Face" ;
257       } else if ( surf.GetType() == GeomAbs_Cone ) {
258         return "Conical Face" ;
259       } else {
260         return "GEOM::FACE" ;
261       }
262     }
263   case TopAbs_WIRE:
264     { return "Wire" ;}
265   case TopAbs_EDGE:
266     { 
267       BRepAdaptor_Curve curv(TopoDS::Edge(shape));
268       if ( curv.GetType() == GeomAbs_Line ) {
269         if ( (Abs(curv.FirstParameter()) >= 1E6 ) || 
270              (Abs(curv.LastParameter()) >= 1E6 )) {
271           return "Line" ;
272         } else
273           return "Edge" ;
274       } else if ( curv.GetType() == GeomAbs_Circle ) {
275         if ( curv.IsClosed() )
276           return "Circle" ;
277         else
278           return "Arc" ;
279       } else {
280         return "Edge" ;
281       }
282     }
283   case TopAbs_VERTEX:
284     { return "Vertex" ;}
285   case TopAbs_SHAPE:
286     { return "Shape" ;}
287   }
288   return 0;
289 }
290
291
292 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
293 {
294   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
295   GEOM::GEOM_Gen_var  Geom = GEOM::GEOM_Gen::_narrow(comp);
296  
297   GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
298   TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
299
300   if( shape.IsNull() ) {
301     return "None" ;
302   }
303       
304   switch (shape.ShapeType() )
305     {
306     case TopAbs_COMPOUND:
307       { return "ICON_OBJBROWSER_COMPOUND" ;}
308     case  TopAbs_COMPSOLID:
309       { return "ICON_OBJBROWSER_COMPSOLID" ;}
310     case TopAbs_SOLID:
311       { return "ICON_OBJBROWSER_SOLID" ;}
312     case TopAbs_SHELL:
313       { return "ICON_OBJBROWSER_SHELL" ;}
314     case TopAbs_FACE:
315       { return "ICON_OBJBROWSER_FACE" ;}
316     case TopAbs_WIRE:
317       { return "ICON_OBJBROWSER_WIRE" ;}
318     case TopAbs_EDGE:
319       { return "ICON_OBJBROWSER_EDGE" ;}
320     case TopAbs_VERTEX:
321       { return "ICON_OBJBROWSER_VERTEX" ;}
322     }
323   return "None";
324 }
325
326 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
327 {
328   QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
329   Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject(aStudy,theEntry);
330
331   class TEvent: public SALOME_Event{
332     QAD_Study* myStudy;
333     Handle(SALOME_InteractiveObject) myIO;
334     int myParam;
335   public:
336     TEvent(QAD_Study* theStudy, const Handle(SALOME_InteractiveObject)& theIO, int theParam):
337       myStudy(theStudy), myIO(theIO), myParam(theParam)
338     {}
339     virtual void Execute(){
340       if(VTKViewer_ViewFrame* aViewFrame = GetFrame<VTKViewer_ViewFrame>(myStudy)){
341         VTKViewer_RenderWindowInteractor* myRenderInter= aViewFrame->getRWInteractor();
342         myRenderInter->SetDisplayMode(myIO,myParam);
343         myRenderInter->Update();
344       }else if(OCCViewer_ViewFrame* aViewFrame = GetFrame<OCCViewer_ViewFrame>(myStudy)){
345         OCCViewer_Viewer3d* v3d = aViewFrame->getViewer();
346         v3d->SwitchRepresentation(myIO,myParam);
347       }
348     }
349   };
350
351   ProcessVoidEvent(new TEvent(aStudy,anIO,theMode));
352 }
353
354 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
355 {
356   QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
357   Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject(aStudy,theEntry);
358   QColor aColor(red,green,blue);
359
360   class TEvent: public SALOME_Event{
361     QAD_Study* myStudy;
362     Handle(SALOME_InteractiveObject) myIO;
363     QColor myParam;
364   public:
365     TEvent(QAD_Study* theStudy, const Handle(SALOME_InteractiveObject)& theIO, const QColor& theParam):
366       myStudy(theStudy), myIO(theIO), myParam(theParam)
367     {}
368     virtual void Execute(){
369       if(VTKViewer_ViewFrame* aViewFrame = GetFrame<VTKViewer_ViewFrame>(myStudy)){
370         VTKViewer_RenderWindowInteractor* myRenderInter= aViewFrame->getRWInteractor();
371         myRenderInter->SetColor(myIO,myParam);
372         myRenderInter->Update();
373       }else if(OCCViewer_ViewFrame* aViewFrame = GetFrame<OCCViewer_ViewFrame>(myStudy)){
374         OCCViewer_Viewer3d* v3d = aViewFrame->getViewer();
375         Handle(AIS_InteractiveContext) ic = aViewFrame->getViewer()->getAISContext();
376         AIS_ListOfInteractive List;
377         ic->DisplayedObjects(List);
378         AIS_ListIteratorOfListOfInteractive ite(List);
379         for ( ; ite.More(); ite.Next() ) {
380           Handle(SALOME_InteractiveObject) anObj =
381             Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
382           if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( myIO ) ) {
383             Quantity_Color CSFColor = Quantity_Color ( myParam.red()   / 255.,
384                                                        myParam.green() / 255.,
385                                                        myParam.blue()  / 255.,
386                                                        Quantity_TOC_RGB );
387             ite.Value()->SetColor( CSFColor );
388             if ( ite.Value()->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
389               Handle(GEOM_AISShape)::DownCast( ite.Value() )->SetShadingColor( CSFColor );
390             ite.Value()->Redisplay( Standard_True );
391             v3d->getViewer3d()->Update();
392             break;
393           }
394         }
395       }
396     }
397   };
398
399   ProcessVoidEvent(new TEvent(aStudy,anIO,aColor));
400 }
401
402 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
403 {
404   QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
405   Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject(aStudy,theEntry);
406
407   class TEvent: public SALOME_Event{
408     QAD_Study* myStudy;
409     Handle(SALOME_InteractiveObject) myIO;
410     float myParam;
411   public:
412     TEvent(QAD_Study* theStudy, const Handle(SALOME_InteractiveObject)& theIO, float theParam):
413       myStudy(theStudy), myIO(theIO), myParam(theParam)
414     {}
415     virtual void Execute(){
416       if(VTKViewer_ViewFrame* aViewFrame = GetFrame<VTKViewer_ViewFrame>(myStudy)){
417         VTKViewer_RenderWindowInteractor* myRenderInter= aViewFrame->getRWInteractor();
418         myRenderInter->SetTransparency(myIO,myParam);
419         myRenderInter->Update();
420       }else if(OCCViewer_ViewFrame* aViewFrame = GetFrame<OCCViewer_ViewFrame>(myStudy)){
421         OCCViewer_Viewer3d* v3d = aViewFrame->getViewer();
422         v3d->SetTransparency(myIO,myParam);
423       }
424     }
425   };
426
427   ProcessVoidEvent(new TEvent(aStudy,anIO,transp));
428 }