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 "utilities.h"
61 #include "SALOMEDSClient.hxx"
64 #include <TopExp_Explorer.hxx>
65 #include <TopTools_MapOfShape.hxx>
66 #include <TopTools_ListOfShape.hxx>
67 #include <TopTools_ListIteratorOfListOfShape.hxx>
68 #include <BRepAdaptor_Surface.hxx>
69 #include <BRepAdaptor_Curve.hxx>
70 #include <GeomAbs_CurveType.hxx>
71 #include <GeomAbs_SurfaceType.hxx>
73 #include <TopoDS_Edge.hxx>
74 #include <TopoDS_Face.hxx>
75 #include <TopoDS_Iterator.hxx>
76 #include <AIS_ListOfInteractive.hxx>
77 #include <AIS_ListIteratorOfListOfInteractive.hxx>
78 #include <V3d_Viewer.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 class TEvent: public SALOME_Event
123 TEvent(const char* theEntry):
126 virtual void Execute()
128 SUIT_Application* app = SUIT_Session::session()->activeApplication();
131 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
132 if (!ActiveStudy) return;
134 _PTR(Study) aStudy(ActiveStudy->studyDS());
135 _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
137 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
138 if (CORBA::is_nil(Geom))
142 _PTR(SComponent) father = aStudy->FindComponent("GEOM");
143 aStudyBuilder->DefineComponentInstance
144 (father, SalomeApp_Application::orb()->object_to_string(Geom));
145 father->ComponentIOR(aFatherIOR);
147 _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
148 _PTR(GenericAttribute) anAttr;
151 if (!obj->FindAttribute(anAttr, "AttributeIOR"))
153 _PTR(AttributeIOR) anIOR(anAttr);
154 string anIORValue = anIOR->Value();
156 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
157 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
159 if (obj->FindAttribute(anAttr, "AttributeName")) {
160 _PTR(AttributeName) aName (anAttr);
161 string aNameValue = aName->Value();
163 /*SUIT_Operation* op = new SalomeApp_ImportOperation (app);
166 _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
167 aStudyBuilder->Addreference(newObj1, obj);
168 // commit transaction
170 Handle(GEOM_InteractiveObject) anIO =
171 new GEOM_InteractiveObject (const_cast<char*>(anIORValue.c_str()),
172 const_cast<char*>(aFatherIOR.c_str()),
174 const_cast<char*>( obj->GetID().c_str()));
176 if (SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(app)) {
178 SVTK_RenderWindowInteractor* aRenderInter= svtkViewWindow->getRWInteractor();
179 int aMode = aRenderInter->GetDisplayMode();
181 vtkActorCollection* theActors =
182 GEOM_AssemblyBuilder::BuildActors(Shape,0,aMode,true);
183 theActors->InitTraversal();
184 while (vtkActor* anActor = theActors->GetNextActor()) {
185 GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
186 GActor->setName(const_cast<char*>(aNameValue.c_str()));
188 aRenderInter->Display(GActor);
190 aRenderInter->Update();
192 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app)) {
193 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
194 Handle(GEOM_AISShape) aSh =
195 new GEOM_AISShape (Shape,const_cast<char*>(aNameValue.c_str()));
196 aSh->setName(const_cast<char*>(aNameValue.c_str()));
199 ic->AddOrRemoveCurrentObject(aSh,true);
201 // update object browser
202 SalomeApp_Application* app = NULL; //dynamic_cast<SalomeApp_Application*>(app);
204 CAM_Module* module = app->module("Geometry");
205 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
207 appMod->updateObjBrowser(true);
214 // MESSAGE("createAndDisplayGO");
215 ProcessVoidEvent(new TEvent (Entry));
217 class TEventUpdateBrowser: public SALOME_Event
220 TEventUpdateBrowser() {}
221 virtual void Execute() {
222 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
224 CAM_Module* module = app->module("Geometry");
225 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
226 if (appMod) appMod->updateObjBrowser(true);
231 ProcessVoidEvent(new TEventUpdateBrowser ());
235 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
237 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
238 if ( CORBA::is_nil( Geom ) )
241 GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
242 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
244 GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
245 TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
248 if(subshape.ShapeType() == TopAbs_COMPOUND) {
250 TopTools_ListOfShape CL;
252 TopTools_ListIteratorOfListOfShape itC;
253 for(itC.Initialize(CL); itC.More(); itC.Next()) {
254 for(it.Initialize(itC.Value()); it.More(); it.Next()) {
255 if (it.Value().ShapeType() == TopAbs_COMPOUND) {
256 if (it.Value().IsSame(subshape))
260 CL.Append(it.Value());
266 TopExp_Explorer Exp(shape, subshape.ShapeType());
267 TopTools_MapOfShape M;
269 if(M.Add(Exp.Current())) {
270 if(Exp.Current().IsSame(subshape))
281 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
283 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
284 if ( CORBA::is_nil( Geom ) )
287 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
288 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
290 if( shape.IsNull() ) {
291 return "Null Shape" ;
294 switch (shape.ShapeType() )
296 case TopAbs_COMPOUND:
297 { return "Compound" ;}
298 case TopAbs_COMPSOLID:
299 { return "Compound Solid" ;}
306 BRepAdaptor_Surface surf(TopoDS::Face(shape));
307 if ( surf.GetType() == GeomAbs_Plane ) {
309 } else if ( surf.GetType() == GeomAbs_Cylinder ) {
310 return "Cylindrical Face" ;
311 } else if ( surf.GetType() == GeomAbs_Sphere ) {
312 return "Spherical Face" ;
313 } else if ( surf.GetType() == GeomAbs_Torus ) {
314 return "Toroidal Face" ;
315 } else if ( surf.GetType() == GeomAbs_Cone ) {
316 return "Conical Face" ;
318 return "GEOM::FACE" ;
325 BRepAdaptor_Curve curv(TopoDS::Edge(shape));
326 if ( curv.GetType() == GeomAbs_Line ) {
327 if ( (Abs(curv.FirstParameter()) >= 1E6 ) ||
328 (Abs(curv.LastParameter()) >= 1E6 )) {
332 } else if ( curv.GetType() == GeomAbs_Circle ) {
333 if ( curv.IsClosed() )
350 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
352 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
353 if ( CORBA::is_nil( Geom ) )
356 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
357 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
359 if( shape.IsNull() ) {
363 switch (shape.ShapeType() )
365 case TopAbs_COMPOUND:
366 { return "ICON_OBJBROWSER_COMPOUND" ;}
367 case TopAbs_COMPSOLID:
368 { return "ICON_OBJBROWSER_COMPSOLID" ;}
370 { return "ICON_OBJBROWSER_SOLID" ;}
372 { return "ICON_OBJBROWSER_SHELL" ;}
374 { return "ICON_OBJBROWSER_FACE" ;}
376 { return "ICON_OBJBROWSER_WIRE" ;}
378 { return "ICON_OBJBROWSER_EDGE" ;}
380 { return "ICON_OBJBROWSER_VERTEX" ;}
386 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
388 SUIT_Application* app = SUIT_Session::session()->activeApplication();
391 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
393 class TEvent: public SALOME_Event{
394 SUIT_Application* myApp;
395 Handle(SALOME_InteractiveObject) myIO;
398 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, int theParam):
399 myApp(theApp), myIO(theIO), myParam(theParam)
401 virtual void Execute(){
402 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
403 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
404 myRenderInter->SetDisplayMode(myIO,myParam);
405 myRenderInter->Update();
407 else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
408 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
410 soccViewer->switchRepresentation(myIO,myParam);
415 ProcessVoidEvent(new TEvent(app,anIO,theMode));
418 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
420 SUIT_Application* app = SUIT_Session::session()->activeApplication();
423 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
425 QColor aColor(red,green,blue);
427 class TEvent: public SALOME_Event{
428 SUIT_Application* myApp;
429 Handle(SALOME_InteractiveObject) myIO;
432 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, const QColor& theParam):
433 myApp(theApp), myIO(theIO), myParam(theParam)
435 virtual void Execute(){
436 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
437 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
438 myRenderInter->SetColor(myIO,myParam);
439 myRenderInter->Update();
440 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
441 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
442 AIS_ListOfInteractive List;
443 ic->DisplayedObjects(List);
444 AIS_ListIteratorOfListOfInteractive ite(List);
445 for ( ; ite.More(); ite.Next() ) {
446 Handle(SALOME_InteractiveObject) anObj =
447 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
448 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( myIO ) ) {
449 Quantity_Color CSFColor = Quantity_Color ( myParam.red() / 255.,
450 myParam.green() / 255.,
451 myParam.blue() / 255.,
453 ite.Value()->SetColor( CSFColor );
454 if ( ite.Value()->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
455 Handle(GEOM_AISShape)::DownCast( ite.Value() )->SetShadingColor( CSFColor );
456 ite.Value()->Redisplay( Standard_True );
464 ProcessVoidEvent(new TEvent(app,anIO,aColor));
467 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
469 SUIT_Application* app = SUIT_Session::session()->activeApplication();
472 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
474 class TEvent: public SALOME_Event{
475 SUIT_Application* myApp;
476 Handle(SALOME_InteractiveObject) myIO;
479 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, float theParam):
480 myApp(theApp), myIO(theIO), myParam(theParam)
482 virtual void Execute(){
483 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
484 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
485 myRenderInter->SetTransparency(myIO,myParam);
486 myRenderInter->Update();
487 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
488 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
490 soccViewer->setTransparency(myIO,myParam);
495 ProcessVoidEvent(new TEvent(app,anIO,transp));
499 bool GEOM_Swig::initGeomGen()
501 return GeometryGUI::InitGeomGen();