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