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 <SVTK_ViewModel.h>
46 #include <SVTK_ViewWindow.h>
47 #include <SVTK_RenderWindowInteractor.h>
49 #include "GEOM_Actor.h"
50 #include "GEOM_Client.hxx"
51 #include "GEOM_AISShape.hxx"
52 #include "GEOM_AssemblyBuilder.h"
53 #include "GEOM_InteractiveObject.hxx"
55 #include <SALOME_Event.hxx>
57 #include "utilities.h"
59 #include "SALOMEDSClient.hxx"
62 #include <TopExp_Explorer.hxx>
63 #include <TopTools_MapOfShape.hxx>
64 #include <TopTools_ListOfShape.hxx>
65 #include <TopTools_ListIteratorOfListOfShape.hxx>
66 #include <BRepAdaptor_Surface.hxx>
67 #include <BRepAdaptor_Curve.hxx>
68 #include <GeomAbs_CurveType.hxx>
69 #include <GeomAbs_SurfaceType.hxx>
71 #include <TopoDS_Edge.hxx>
72 #include <TopoDS_Face.hxx>
73 #include <TopoDS_Iterator.hxx>
74 #include <AIS_ListOfInteractive.hxx>
75 #include <AIS_ListIteratorOfListOfInteractive.hxx>
76 #include <V3d_Viewer.hxx>
81 static GEOM_Client ShapeReader;
83 inline OCCViewer_Viewer* GetOCCViewer(SUIT_Application* theApp){
84 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
85 if(window && window->getViewManager()->getType() == OCCViewer_Viewer::Type()){
86 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( window );
88 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
90 return vm->getOCCViewer();
97 inline SVTK_ViewWindow* GetSVTKViewWindow(SUIT_Application* theApp){
98 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
99 if(window && window->getViewManager()->getType() == SVTK_Viewer::Type())
100 return dynamic_cast<SVTK_ViewWindow*>( window );
105 GEOM_Swig::GEOM_Swig()
107 // MESSAGE("Constructeur");
110 GEOM_Swig::~GEOM_Swig()
112 // MESSAGE("Destructeur");
115 void GEOM_Swig::createAndDisplayGO (const char* Entry)
117 class TEvent: public SALOME_Event
121 TEvent(const char* theEntry):
124 virtual void Execute()
126 SUIT_Application* app = SUIT_Session::session()->activeApplication();
129 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
130 if (!ActiveStudy) return;
132 _PTR(Study) aStudy(ActiveStudy->studyDS());
133 _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
135 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
136 if (CORBA::is_nil(Geom))
140 _PTR(SComponent) father = aStudy->FindComponent("GEOM");
141 aStudyBuilder->DefineComponentInstance
142 (father, SalomeApp_Application::orb()->object_to_string(Geom));
143 father->ComponentIOR(aFatherIOR);
145 _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
146 _PTR(GenericAttribute) anAttr;
149 if (!obj->FindAttribute(anAttr, "AttributeIOR"))
151 _PTR(AttributeIOR) anIOR(anAttr);
152 string anIORValue = anIOR->Value();
154 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
155 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
157 if (obj->FindAttribute(anAttr, "AttributeName")) {
158 _PTR(AttributeName) aName (anAttr);
159 string aNameValue = aName->Value();
161 /*SUIT_Operation* op = new SalomeApp_ImportOperation (app);
164 _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
165 aStudyBuilder->Addreference(newObj1, obj);
166 // commit transaction
168 Handle(GEOM_InteractiveObject) anIO =
169 new GEOM_InteractiveObject (const_cast<char*>(anIORValue.c_str()),
170 const_cast<char*>(aFatherIOR.c_str()),
172 const_cast<char*>( obj->GetID().c_str()));
174 if (SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(app)) {
176 SVTK_RenderWindowInteractor* aRenderInter= svtkViewWindow->getRWInteractor();
177 int aMode = aRenderInter->GetDisplayMode();
179 vtkActorCollection* theActors =
180 GEOM_AssemblyBuilder::BuildActors(Shape,0,aMode,true);
181 theActors->InitTraversal();
182 while (vtkActor* anActor = theActors->GetNextActor()) {
183 GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
184 GActor->setName(const_cast<char*>(aNameValue.c_str()));
186 aRenderInter->Display(GActor);
188 aRenderInter->Update();
190 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app)) {
191 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
192 Handle(GEOM_AISShape) aSh =
193 new GEOM_AISShape (Shape,const_cast<char*>(aNameValue.c_str()));
194 aSh->setName(const_cast<char*>(aNameValue.c_str()));
197 ic->AddOrRemoveCurrentObject(aSh,true);
199 // update object browser
200 SalomeApp_Application* app = NULL; //dynamic_cast<SalomeApp_Application*>(app);
202 CAM_Module* module = app->module("Geometry");
203 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
205 appMod->updateObjBrowser(true);
212 // MESSAGE("createAndDisplayGO");
213 ProcessVoidEvent(new TEvent (Entry));
215 class TEventUpdateBrowser: public SALOME_Event
218 TEventUpdateBrowser() {}
219 virtual void Execute() {
220 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
222 CAM_Module* module = app->module("Geometry");
223 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
224 if (appMod) appMod->updateObjBrowser(true);
229 ProcessVoidEvent(new TEventUpdateBrowser ());
233 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
235 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
236 if ( CORBA::is_nil( Geom ) )
239 GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
240 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
242 GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
243 TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
246 if(subshape.ShapeType() == TopAbs_COMPOUND) {
248 TopTools_ListOfShape CL;
250 TopTools_ListIteratorOfListOfShape itC;
251 for(itC.Initialize(CL); itC.More(); itC.Next()) {
252 for(it.Initialize(itC.Value()); it.More(); it.Next()) {
253 if (it.Value().ShapeType() == TopAbs_COMPOUND) {
254 if (it.Value().IsSame(subshape))
258 CL.Append(it.Value());
264 TopExp_Explorer Exp(shape, subshape.ShapeType());
265 TopTools_MapOfShape M;
267 if(M.Add(Exp.Current())) {
268 if(Exp.Current().IsSame(subshape))
279 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
281 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
282 if ( CORBA::is_nil( Geom ) )
285 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
286 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
288 if( shape.IsNull() ) {
289 return "Null Shape" ;
292 switch (shape.ShapeType() )
294 case TopAbs_COMPOUND:
295 { return "Compound" ;}
296 case TopAbs_COMPSOLID:
297 { return "Compound Solid" ;}
304 BRepAdaptor_Surface surf(TopoDS::Face(shape));
305 if ( surf.GetType() == GeomAbs_Plane ) {
307 } else if ( surf.GetType() == GeomAbs_Cylinder ) {
308 return "Cylindrical Face" ;
309 } else if ( surf.GetType() == GeomAbs_Sphere ) {
310 return "Spherical Face" ;
311 } else if ( surf.GetType() == GeomAbs_Torus ) {
312 return "Toroidal Face" ;
313 } else if ( surf.GetType() == GeomAbs_Cone ) {
314 return "Conical Face" ;
316 return "GEOM::FACE" ;
323 BRepAdaptor_Curve curv(TopoDS::Edge(shape));
324 if ( curv.GetType() == GeomAbs_Line ) {
325 if ( (Abs(curv.FirstParameter()) >= 1E6 ) ||
326 (Abs(curv.LastParameter()) >= 1E6 )) {
330 } else if ( curv.GetType() == GeomAbs_Circle ) {
331 if ( curv.IsClosed() )
348 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
350 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
351 if ( CORBA::is_nil( Geom ) )
354 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
355 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
357 if( shape.IsNull() ) {
361 switch (shape.ShapeType() )
363 case TopAbs_COMPOUND:
364 { return "ICON_OBJBROWSER_COMPOUND" ;}
365 case TopAbs_COMPSOLID:
366 { return "ICON_OBJBROWSER_COMPSOLID" ;}
368 { return "ICON_OBJBROWSER_SOLID" ;}
370 { return "ICON_OBJBROWSER_SHELL" ;}
372 { return "ICON_OBJBROWSER_FACE" ;}
374 { return "ICON_OBJBROWSER_WIRE" ;}
376 { return "ICON_OBJBROWSER_EDGE" ;}
378 { return "ICON_OBJBROWSER_VERTEX" ;}
384 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
386 SUIT_Application* app = SUIT_Session::session()->activeApplication();
389 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
391 class TEvent: public SALOME_Event{
392 SUIT_Application* myApp;
393 Handle(SALOME_InteractiveObject) myIO;
396 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, int theParam):
397 myApp(theApp), myIO(theIO), myParam(theParam)
399 virtual void Execute(){
400 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
401 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
402 myRenderInter->SetDisplayMode(myIO,myParam);
403 myRenderInter->Update();
405 else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
406 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
408 soccViewer->switchRepresentation(myIO,myParam);
413 ProcessVoidEvent(new TEvent(app,anIO,theMode));
416 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
418 SUIT_Application* app = SUIT_Session::session()->activeApplication();
421 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
423 QColor aColor(red,green,blue);
425 class TEvent: public SALOME_Event{
426 SUIT_Application* myApp;
427 Handle(SALOME_InteractiveObject) myIO;
430 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, const QColor& theParam):
431 myApp(theApp), myIO(theIO), myParam(theParam)
433 virtual void Execute(){
434 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
435 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
436 myRenderInter->SetColor(myIO,myParam);
437 myRenderInter->Update();
438 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
439 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
440 AIS_ListOfInteractive List;
441 ic->DisplayedObjects(List);
442 AIS_ListIteratorOfListOfInteractive ite(List);
443 for ( ; ite.More(); ite.Next() ) {
444 Handle(SALOME_InteractiveObject) anObj =
445 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
446 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( myIO ) ) {
447 Quantity_Color CSFColor = Quantity_Color ( myParam.red() / 255.,
448 myParam.green() / 255.,
449 myParam.blue() / 255.,
451 ite.Value()->SetColor( CSFColor );
452 if ( ite.Value()->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
453 Handle(GEOM_AISShape)::DownCast( ite.Value() )->SetShadingColor( CSFColor );
454 ite.Value()->Redisplay( Standard_True );
462 ProcessVoidEvent(new TEvent(app,anIO,aColor));
465 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
467 SUIT_Application* app = SUIT_Session::session()->activeApplication();
470 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
472 class TEvent: public SALOME_Event{
473 SUIT_Application* myApp;
474 Handle(SALOME_InteractiveObject) myIO;
477 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, float theParam):
478 myApp(theApp), myIO(theIO), myParam(theParam)
480 virtual void Execute(){
481 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
482 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
483 myRenderInter->SetTransparency(myIO,myParam);
484 myRenderInter->Update();
485 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
486 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
488 soccViewer->setTransparency(myIO,myParam);
493 ProcessVoidEvent(new TEvent(app,anIO,transp));
497 bool GEOM_Swig::initGeomGen()
499 return GeometryGUI::InitGeomGen();