1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : GeometryGUI_Swig.cxx
25 // Author : Nicolas REJNERI, Paul RASCLE
29 #include "GeometryGUI_Swig.hxx"
30 #include "SALOMEGUI_Swig.hxx"
32 #include "GeometryGUI.h"
34 #include <SUIT_Session.h>
35 #include <SalomeApp_Application.h>
36 #include <SalomeApp_Study.h>
37 #include <SalomeApp_ImportOperation.h>
39 #include <OB_Browser.h>
41 #include <OCCViewer_ViewWindow.h>
42 #include <OCCViewer_ViewManager.h>
43 #include <SOCC_ViewModel.h>
45 #include <VTKViewer_ViewWindow.h>
46 #include <VTKViewer_ViewModel.h>
47 #include <VTKViewer_RenderWindowInteractor.h>
48 #include <SVTK_ViewWindow.h>
49 #include <SVTK_RenderWindowInteractor.h>
51 #include "GEOM_Actor.h"
52 #include "GEOM_Client.hxx"
53 #include "GEOM_AISShape.hxx"
54 #include "GEOM_AssemblyBuilder.h"
55 #include "GEOM_InteractiveObject.hxx"
57 #include <SALOME_Event.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <TopTools_MapOfShape.hxx>
61 #include <TopTools_ListOfShape.hxx>
62 #include <TopTools_ListIteratorOfListOfShape.hxx>
63 #include <BRepAdaptor_Surface.hxx>
64 #include <BRepAdaptor_Curve.hxx>
65 #include <GeomAbs_CurveType.hxx>
66 #include <GeomAbs_SurfaceType.hxx>
68 #include <TopoDS_Edge.hxx>
69 #include <TopoDS_Face.hxx>
70 #include <TopoDS_Iterator.hxx>
71 #include <AIS_ListOfInteractive.hxx>
72 #include <AIS_ListIteratorOfListOfInteractive.hxx>
73 #include <V3d_Viewer.hxx>
75 #include "utilities.h"
79 #include "SALOMEDSClient.hxx"
80 #include "SALOMEDS_SObject.hxx"
81 #include "SALOMEDS_Study.hxx"
83 static GEOM_Client ShapeReader;
85 inline OCCViewer_Viewer* GetOCCViewer(SUIT_Application* theApp){
86 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
87 if(window && window->getViewManager()->getType() == OCCViewer_Viewer::Type()){
88 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( window );
90 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
92 return vm->getOCCViewer();
99 inline SVTK_ViewWindow* GetSVTKViewWindow(SUIT_Application* theApp){
100 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
101 if(window && window->getViewManager()->getType() == VTKViewer_Viewer::Type())
102 return dynamic_cast<SVTK_ViewWindow*>( window );
107 GEOM_Swig::GEOM_Swig()
109 // MESSAGE("Constructeur");
112 GEOM_Swig::~GEOM_Swig()
114 // MESSAGE("Destructeur");
117 void GEOM_Swig::createAndDisplayGO(const char* Entry)
119 // MESSAGE("createAndDisplayGO");
121 SUIT_Application* app = SUIT_Session::session()->activeApplication();
124 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
125 if ( !ActiveStudy ) return;
127 _PTR(Study) aStudy(ActiveStudy->studyDS());
128 _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
130 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
131 if ( CORBA::is_nil( Geom ) )
135 _PTR(SComponent) father = aStudy->FindComponent("GEOM");
136 aStudyBuilder->DefineComponentInstance( father, SalomeApp_Application::orb()->object_to_string(Geom) );
137 father->ComponentIOR(aFatherIOR);
139 _PTR(SObject) fatherSF;
141 _PTR(SObject) obj = aStudy->FindObjectID(Entry);
142 _PTR(GenericAttribute) anAttr;
145 if ( !obj->FindAttribute(anAttr, "AttributeIOR"))
147 _PTR(AttributeIOR) anIOR(anAttr);
148 string anIORValue = anIOR->Value();
150 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
151 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
154 if (obj->FindAttribute(anAttr, "AttributeName")) {
155 _PTR(AttributeName) aName ( anAttr );
156 string aNameValue = aName->Value();
158 SUIT_Operation* op = new SalomeApp_ImportOperation( app );
161 _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
162 aStudyBuilder->Addreference(newObj1, obj);
163 // commit transaction
166 Handle(GEOM_InteractiveObject) anIO = new GEOM_InteractiveObject(const_cast<char*>(anIORValue.c_str()),
167 const_cast<char*>(aFatherIOR.c_str()),
169 const_cast<char*>( obj->GetID().c_str() ));
171 class TEvent: public SALOME_Event{
172 SUIT_Application* myApp;
173 TopoDS_Shape myShape;
174 Handle(SALOME_InteractiveObject) myIO;
177 TEvent(SUIT_Application* theApp, TopoDS_Shape theShape,
178 const Handle(SALOME_InteractiveObject)& theIO,
179 const char* theName):
180 myApp(theApp), myShape(theShape),
181 myIO(theIO), myName(theName)
183 virtual void Execute(){
184 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
185 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
186 int aMode = myRenderInter->GetDisplayMode();
188 vtkActorCollection* theActors = GEOM_AssemblyBuilder::BuildActors(myShape,0,aMode,true);
189 theActors->InitTraversal();
190 while(vtkActor* anActor = theActors->GetNextActor()){
191 GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
192 GActor->setName(const_cast<char*>(myName));
194 myRenderInter->Display(GActor);
196 myRenderInter->Update();
197 }else if( OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
198 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
199 Handle(GEOM_AISShape) aSh = new GEOM_AISShape(myShape,const_cast<char*>(myName));
200 aSh->setName(const_cast<char*>(myName));
203 ic->AddOrRemoveCurrentObject(aSh,true);
205 // update object browser
206 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(myApp);
208 CAM_Module* module = app->module( "Geometry" );
209 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
211 appMod->updateObjBrowser( true );
216 ProcessVoidEvent(new TEvent(app,Shape,anIO,aNameValue.c_str()));
222 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
224 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
225 if ( CORBA::is_nil( Geom ) )
228 GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
229 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
231 GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
232 TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
235 if(subshape.ShapeType() == TopAbs_COMPOUND) {
237 TopTools_ListOfShape CL;
239 TopTools_ListIteratorOfListOfShape itC;
240 for(itC.Initialize(CL); itC.More(); itC.Next()) {
241 for(it.Initialize(itC.Value()); it.More(); it.Next()) {
242 if (it.Value().ShapeType() == TopAbs_COMPOUND) {
243 if (it.Value().IsSame(subshape))
247 CL.Append(it.Value());
253 TopExp_Explorer Exp(shape, subshape.ShapeType());
254 TopTools_MapOfShape M;
256 if(M.Add(Exp.Current())) {
257 if(Exp.Current().IsSame(subshape))
268 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
270 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
271 if ( CORBA::is_nil( Geom ) )
274 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
275 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
277 if( shape.IsNull() ) {
278 return "Null Shape" ;
281 switch (shape.ShapeType() )
283 case TopAbs_COMPOUND:
284 { return "Compound" ;}
285 case TopAbs_COMPSOLID:
286 { return "Compound Solid" ;}
293 BRepAdaptor_Surface surf(TopoDS::Face(shape));
294 if ( surf.GetType() == GeomAbs_Plane ) {
296 } else if ( surf.GetType() == GeomAbs_Cylinder ) {
297 return "Cylindrical Face" ;
298 } else if ( surf.GetType() == GeomAbs_Sphere ) {
299 return "Spherical Face" ;
300 } else if ( surf.GetType() == GeomAbs_Torus ) {
301 return "Toroidal Face" ;
302 } else if ( surf.GetType() == GeomAbs_Cone ) {
303 return "Conical Face" ;
305 return "GEOM::FACE" ;
312 BRepAdaptor_Curve curv(TopoDS::Edge(shape));
313 if ( curv.GetType() == GeomAbs_Line ) {
314 if ( (Abs(curv.FirstParameter()) >= 1E6 ) ||
315 (Abs(curv.LastParameter()) >= 1E6 )) {
319 } else if ( curv.GetType() == GeomAbs_Circle ) {
320 if ( curv.IsClosed() )
337 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
339 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
340 if ( CORBA::is_nil( Geom ) )
343 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
344 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
346 if( shape.IsNull() ) {
350 switch (shape.ShapeType() )
352 case TopAbs_COMPOUND:
353 { return "ICON_OBJBROWSER_COMPOUND" ;}
354 case TopAbs_COMPSOLID:
355 { return "ICON_OBJBROWSER_COMPSOLID" ;}
357 { return "ICON_OBJBROWSER_SOLID" ;}
359 { return "ICON_OBJBROWSER_SHELL" ;}
361 { return "ICON_OBJBROWSER_FACE" ;}
363 { return "ICON_OBJBROWSER_WIRE" ;}
365 { return "ICON_OBJBROWSER_EDGE" ;}
367 { return "ICON_OBJBROWSER_VERTEX" ;}
373 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
375 SUIT_Application* app = SUIT_Session::session()->activeApplication();
378 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
380 class TEvent: public SALOME_Event{
381 SUIT_Application* myApp;
382 Handle(SALOME_InteractiveObject) myIO;
385 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, int theParam):
386 myApp(theApp), myIO(theIO), myParam(theParam)
388 virtual void Execute(){
389 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
390 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
391 myRenderInter->SetDisplayMode(myIO,myParam);
392 myRenderInter->Update();
394 else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
395 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
397 soccViewer->switchRepresentation(myIO,myParam);
402 ProcessVoidEvent(new TEvent(app,anIO,theMode));
405 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
407 SUIT_Application* app = SUIT_Session::session()->activeApplication();
410 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
412 QColor aColor(red,green,blue);
414 class TEvent: public SALOME_Event{
415 SUIT_Application* myApp;
416 Handle(SALOME_InteractiveObject) myIO;
419 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, const QColor& theParam):
420 myApp(theApp), myIO(theIO), myParam(theParam)
422 virtual void Execute(){
423 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
424 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
425 myRenderInter->SetColor(myIO,myParam);
426 myRenderInter->Update();
427 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
428 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
429 AIS_ListOfInteractive List;
430 ic->DisplayedObjects(List);
431 AIS_ListIteratorOfListOfInteractive ite(List);
432 for ( ; ite.More(); ite.Next() ) {
433 Handle(SALOME_InteractiveObject) anObj =
434 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
435 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( myIO ) ) {
436 Quantity_Color CSFColor = Quantity_Color ( myParam.red() / 255.,
437 myParam.green() / 255.,
438 myParam.blue() / 255.,
440 ite.Value()->SetColor( CSFColor );
441 if ( ite.Value()->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
442 Handle(GEOM_AISShape)::DownCast( ite.Value() )->SetShadingColor( CSFColor );
443 ite.Value()->Redisplay( Standard_True );
451 ProcessVoidEvent(new TEvent(app,anIO,aColor));
454 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
456 SUIT_Application* app = SUIT_Session::session()->activeApplication();
459 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
461 class TEvent: public SALOME_Event{
462 SUIT_Application* myApp;
463 Handle(SALOME_InteractiveObject) myIO;
466 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, float theParam):
467 myApp(theApp), myIO(theIO), myParam(theParam)
469 virtual void Execute(){
470 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
471 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
472 myRenderInter->SetTransparency(myIO,myParam);
473 myRenderInter->Update();
474 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
475 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
477 soccViewer->setTransparency(myIO,myParam);
482 ProcessVoidEvent(new TEvent(app,anIO,transp));
486 bool GEOM_Swig::initGeomGen()
488 return GeometryGUI::InitGeomGen();