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