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) obj = aStudy->FindObjectID(Entry);
140 _PTR(GenericAttribute) anAttr;
143 if ( !obj->FindAttribute(anAttr, "AttributeIOR"))
145 _PTR(AttributeIOR) anIOR(anAttr);
146 string anIORValue = anIOR->Value();
148 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
149 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
152 if (obj->FindAttribute(anAttr, "AttributeName")) {
153 _PTR(AttributeName) aName ( anAttr );
154 string aNameValue = aName->Value();
156 /*jfa for bug IPAL9384 3)c):SUIT_Operation* op = new SalomeApp_ImportOperation( app );
159 _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
160 aStudyBuilder->Addreference(newObj1, obj);
161 // commit transaction
164 Handle(GEOM_InteractiveObject) anIO = new GEOM_InteractiveObject(const_cast<char*>(anIORValue.c_str()),
165 const_cast<char*>(aFatherIOR.c_str()),
167 const_cast<char*>( obj->GetID().c_str() ));
169 class TEvent: public SALOME_Event{
170 SUIT_Application* myApp;
171 TopoDS_Shape myShape;
172 Handle(SALOME_InteractiveObject) myIO;
175 TEvent(SUIT_Application* theApp, TopoDS_Shape theShape,
176 const Handle(SALOME_InteractiveObject)& theIO,
177 const char* theName):
178 myApp(theApp), myShape(theShape),
179 myIO(theIO), myName(theName)
181 virtual void Execute(){
182 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
183 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
184 int aMode = myRenderInter->GetDisplayMode();
186 vtkActorCollection* theActors = GEOM_AssemblyBuilder::BuildActors(myShape,0,aMode,true);
187 theActors->InitTraversal();
188 while(vtkActor* anActor = theActors->GetNextActor()){
189 GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
190 GActor->setName(const_cast<char*>(myName));
192 myRenderInter->Display(GActor);
194 myRenderInter->Update();
195 }else if( OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
196 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
197 Handle(GEOM_AISShape) aSh = new GEOM_AISShape(myShape,const_cast<char*>(myName));
198 aSh->setName(const_cast<char*>(myName));
201 ic->AddOrRemoveCurrentObject(aSh,true);
203 // update object browser
204 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(myApp);
206 CAM_Module* module = app->module( "Geometry" );
207 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
209 appMod->updateObjBrowser( true );
214 ProcessVoidEvent(new TEvent(app,Shape,anIO,aNameValue.c_str()));
220 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
222 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
223 if ( CORBA::is_nil( Geom ) )
226 GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
227 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
229 GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
230 TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
233 if(subshape.ShapeType() == TopAbs_COMPOUND) {
235 TopTools_ListOfShape CL;
237 TopTools_ListIteratorOfListOfShape itC;
238 for(itC.Initialize(CL); itC.More(); itC.Next()) {
239 for(it.Initialize(itC.Value()); it.More(); it.Next()) {
240 if (it.Value().ShapeType() == TopAbs_COMPOUND) {
241 if (it.Value().IsSame(subshape))
245 CL.Append(it.Value());
251 TopExp_Explorer Exp(shape, subshape.ShapeType());
252 TopTools_MapOfShape M;
254 if(M.Add(Exp.Current())) {
255 if(Exp.Current().IsSame(subshape))
266 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
268 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
269 if ( CORBA::is_nil( Geom ) )
272 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
273 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
275 if( shape.IsNull() ) {
276 return "Null Shape" ;
279 switch (shape.ShapeType() )
281 case TopAbs_COMPOUND:
282 { return "Compound" ;}
283 case TopAbs_COMPSOLID:
284 { return "Compound Solid" ;}
291 BRepAdaptor_Surface surf(TopoDS::Face(shape));
292 if ( surf.GetType() == GeomAbs_Plane ) {
294 } else if ( surf.GetType() == GeomAbs_Cylinder ) {
295 return "Cylindrical Face" ;
296 } else if ( surf.GetType() == GeomAbs_Sphere ) {
297 return "Spherical Face" ;
298 } else if ( surf.GetType() == GeomAbs_Torus ) {
299 return "Toroidal Face" ;
300 } else if ( surf.GetType() == GeomAbs_Cone ) {
301 return "Conical Face" ;
303 return "GEOM::FACE" ;
310 BRepAdaptor_Curve curv(TopoDS::Edge(shape));
311 if ( curv.GetType() == GeomAbs_Line ) {
312 if ( (Abs(curv.FirstParameter()) >= 1E6 ) ||
313 (Abs(curv.LastParameter()) >= 1E6 )) {
317 } else if ( curv.GetType() == GeomAbs_Circle ) {
318 if ( curv.IsClosed() )
335 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
337 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
338 if ( CORBA::is_nil( Geom ) )
341 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
342 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
344 if( shape.IsNull() ) {
348 switch (shape.ShapeType() )
350 case TopAbs_COMPOUND:
351 { return "ICON_OBJBROWSER_COMPOUND" ;}
352 case TopAbs_COMPSOLID:
353 { return "ICON_OBJBROWSER_COMPSOLID" ;}
355 { return "ICON_OBJBROWSER_SOLID" ;}
357 { return "ICON_OBJBROWSER_SHELL" ;}
359 { return "ICON_OBJBROWSER_FACE" ;}
361 { return "ICON_OBJBROWSER_WIRE" ;}
363 { return "ICON_OBJBROWSER_EDGE" ;}
365 { return "ICON_OBJBROWSER_VERTEX" ;}
371 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
373 SUIT_Application* app = SUIT_Session::session()->activeApplication();
376 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
378 class TEvent: public SALOME_Event{
379 SUIT_Application* myApp;
380 Handle(SALOME_InteractiveObject) myIO;
383 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, int theParam):
384 myApp(theApp), myIO(theIO), myParam(theParam)
386 virtual void Execute(){
387 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
388 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
389 myRenderInter->SetDisplayMode(myIO,myParam);
390 myRenderInter->Update();
392 else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
393 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
395 soccViewer->switchRepresentation(myIO,myParam);
400 ProcessVoidEvent(new TEvent(app,anIO,theMode));
403 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
405 SUIT_Application* app = SUIT_Session::session()->activeApplication();
408 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
410 QColor aColor(red,green,blue);
412 class TEvent: public SALOME_Event{
413 SUIT_Application* myApp;
414 Handle(SALOME_InteractiveObject) myIO;
417 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, const QColor& theParam):
418 myApp(theApp), myIO(theIO), myParam(theParam)
420 virtual void Execute(){
421 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
422 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
423 myRenderInter->SetColor(myIO,myParam);
424 myRenderInter->Update();
425 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
426 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
427 AIS_ListOfInteractive List;
428 ic->DisplayedObjects(List);
429 AIS_ListIteratorOfListOfInteractive ite(List);
430 for ( ; ite.More(); ite.Next() ) {
431 Handle(SALOME_InteractiveObject) anObj =
432 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
433 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( myIO ) ) {
434 Quantity_Color CSFColor = Quantity_Color ( myParam.red() / 255.,
435 myParam.green() / 255.,
436 myParam.blue() / 255.,
438 ite.Value()->SetColor( CSFColor );
439 if ( ite.Value()->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
440 Handle(GEOM_AISShape)::DownCast( ite.Value() )->SetShadingColor( CSFColor );
441 ite.Value()->Redisplay( Standard_True );
449 ProcessVoidEvent(new TEvent(app,anIO,aColor));
452 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
454 SUIT_Application* app = SUIT_Session::session()->activeApplication();
457 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
459 class TEvent: public SALOME_Event{
460 SUIT_Application* myApp;
461 Handle(SALOME_InteractiveObject) myIO;
464 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, float theParam):
465 myApp(theApp), myIO(theIO), myParam(theParam)
467 virtual void Execute(){
468 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
469 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
470 myRenderInter->SetTransparency(myIO,myParam);
471 myRenderInter->Update();
472 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
473 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
475 soccViewer->setTransparency(myIO,myParam);
480 ProcessVoidEvent(new TEvent(app,anIO,transp));
484 bool GEOM_Swig::initGeomGen()
486 return GeometryGUI::InitGeomGen();