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_View.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)) {
137 GeometryGUI::InitGeomGen();
138 Geom = GeometryGUI::GetGeomGen();
140 if (CORBA::is_nil(Geom))
144 _PTR(SComponent) father = aStudy->FindComponent("GEOM");
147 if (!father->ComponentIOR(aFatherIOR)) {
148 aStudyBuilder->LoadWith(father, SalomeApp_Application::orb()->object_to_string(Geom));
149 father->ComponentIOR(aFatherIOR);
152 _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
157 _PTR(GenericAttribute) anAttr;
158 if (!obj->FindAttribute(anAttr, "AttributeIOR"))
160 _PTR(AttributeIOR) anIOR(anAttr);
161 string anIORValue = anIOR->Value();
163 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
164 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
165 if (!Shape.IsNull()) {
166 if (obj->FindAttribute(anAttr, "AttributeName")) {
167 _PTR(AttributeName) aName (anAttr);
168 string aNameValue = aName->Value();
170 /*SUIT_Operation* op = new SalomeApp_ImportOperation (app);
173 _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
174 aStudyBuilder->Addreference(newObj1, obj);
175 // commit transaction
177 Handle(GEOM_InteractiveObject) anIO =
178 new GEOM_InteractiveObject (const_cast<char*>(anIORValue.c_str()),
179 const_cast<char*>(aFatherIOR.c_str()),
181 const_cast<char*>( obj->GetID().c_str()));
183 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app)) {
184 SVTK_View* aView = aViewWindow->getView();
185 int aMode = aView->GetDisplayMode();
187 vtkActorCollection* theActors =
188 GEOM_AssemblyBuilder::BuildActors(Shape,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*>(aNameValue.c_str()));
194 aView->Display(GActor);
197 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app)) {
198 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
199 Handle(GEOM_AISShape) aSh =
200 new GEOM_AISShape (Shape,const_cast<char*>(aNameValue.c_str()));
201 aSh->setName(const_cast<char*>(aNameValue.c_str()));
204 ic->AddOrRemoveCurrentObject(aSh,true);
206 // update object browser
207 SalomeApp_Application* app = NULL; //dynamic_cast<SalomeApp_Application*>(app);
209 CAM_Module* module = app->module("Geometry");
210 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
212 appMod->updateObjBrowser(true);
219 // MESSAGE("createAndDisplayGO");
220 ProcessVoidEvent(new TEvent (Entry));
222 class TEventUpdateBrowser: public SALOME_Event
225 TEventUpdateBrowser() {}
226 virtual void Execute() {
227 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
229 CAM_Module* module = app->module("Geometry");
230 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
231 if (appMod) appMod->updateObjBrowser(true);
236 ProcessVoidEvent(new TEventUpdateBrowser ());
240 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
242 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
243 if ( CORBA::is_nil( Geom ) )
246 GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
247 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
249 GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
250 TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
253 if(subshape.ShapeType() == TopAbs_COMPOUND) {
255 TopTools_ListOfShape CL;
257 TopTools_ListIteratorOfListOfShape itC;
258 for(itC.Initialize(CL); itC.More(); itC.Next()) {
259 for(it.Initialize(itC.Value()); it.More(); it.Next()) {
260 if (it.Value().ShapeType() == TopAbs_COMPOUND) {
261 if (it.Value().IsSame(subshape))
265 CL.Append(it.Value());
271 TopExp_Explorer Exp(shape, subshape.ShapeType());
272 TopTools_MapOfShape M;
274 if(M.Add(Exp.Current())) {
275 if(Exp.Current().IsSame(subshape))
286 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
288 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
289 if ( CORBA::is_nil( Geom ) )
292 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
293 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
295 if( shape.IsNull() ) {
296 return "Null Shape" ;
299 switch (shape.ShapeType() )
301 case TopAbs_COMPOUND:
302 { return "Compound" ;}
303 case TopAbs_COMPSOLID:
304 { return "Compound Solid" ;}
311 BRepAdaptor_Surface surf(TopoDS::Face(shape));
312 if ( surf.GetType() == GeomAbs_Plane ) {
314 } else if ( surf.GetType() == GeomAbs_Cylinder ) {
315 return "Cylindrical Face" ;
316 } else if ( surf.GetType() == GeomAbs_Sphere ) {
317 return "Spherical Face" ;
318 } else if ( surf.GetType() == GeomAbs_Torus ) {
319 return "Toroidal Face" ;
320 } else if ( surf.GetType() == GeomAbs_Cone ) {
321 return "Conical Face" ;
323 return "GEOM::FACE" ;
330 BRepAdaptor_Curve curv(TopoDS::Edge(shape));
331 if ( curv.GetType() == GeomAbs_Line ) {
332 if ( (Abs(curv.FirstParameter()) >= 1E6 ) ||
333 (Abs(curv.LastParameter()) >= 1E6 )) {
337 } else if ( curv.GetType() == GeomAbs_Circle ) {
338 if ( curv.IsClosed() )
355 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
357 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
358 if ( CORBA::is_nil( Geom ) )
361 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
362 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
364 if( shape.IsNull() ) {
368 switch (shape.ShapeType() )
370 case TopAbs_COMPOUND:
371 { return "ICON_OBJBROWSER_COMPOUND" ;}
372 case TopAbs_COMPSOLID:
373 { return "ICON_OBJBROWSER_COMPSOLID" ;}
375 { return "ICON_OBJBROWSER_SOLID" ;}
377 { return "ICON_OBJBROWSER_SHELL" ;}
379 { return "ICON_OBJBROWSER_FACE" ;}
381 { return "ICON_OBJBROWSER_WIRE" ;}
383 { return "ICON_OBJBROWSER_EDGE" ;}
385 { return "ICON_OBJBROWSER_VERTEX" ;}
391 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
393 SUIT_Application* app = SUIT_Session::session()->activeApplication();
396 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
398 class TEvent: public SALOME_Event{
399 SUIT_Application* myApp;
400 Handle(SALOME_InteractiveObject) myIO;
403 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, int theParam):
404 myApp(theApp), myIO(theIO), myParam(theParam)
406 virtual void Execute(){
407 if(SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(myApp)){
408 SVTK_View* aView = aViewWindow->getView();
409 aView->SetDisplayMode(myIO,myParam);
412 else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
413 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
415 soccViewer->switchRepresentation(myIO,myParam);
420 ProcessVoidEvent(new TEvent(app,anIO,theMode));
423 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
425 SUIT_Application* app = SUIT_Session::session()->activeApplication();
428 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
430 QColor aColor(red,green,blue);
432 class TEvent: public SALOME_Event{
433 SUIT_Application* myApp;
434 Handle(SALOME_InteractiveObject) myIO;
437 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, const QColor& theParam):
438 myApp(theApp), myIO(theIO), myParam(theParam)
440 virtual void Execute(){
441 if(SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(myApp)){
442 SVTK_View* aView = aViewWindow->getView();
443 aView->SetColor(myIO,myParam);
445 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
446 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
447 AIS_ListOfInteractive List;
448 ic->DisplayedObjects(List);
449 AIS_ListIteratorOfListOfInteractive ite(List);
450 for ( ; ite.More(); ite.Next() ) {
451 Handle(SALOME_InteractiveObject) anObj =
452 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
453 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( myIO ) ) {
454 Quantity_Color CSFColor = Quantity_Color ( myParam.red() / 255.,
455 myParam.green() / 255.,
456 myParam.blue() / 255.,
458 ite.Value()->SetColor( CSFColor );
459 if ( ite.Value()->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
460 Handle(GEOM_AISShape)::DownCast( ite.Value() )->SetShadingColor( CSFColor );
461 ite.Value()->Redisplay( Standard_True );
469 ProcessVoidEvent(new TEvent(app,anIO,aColor));
472 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
474 SUIT_Application* app = SUIT_Session::session()->activeApplication();
477 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
479 class TEvent: public SALOME_Event{
480 SUIT_Application* myApp;
481 Handle(SALOME_InteractiveObject) myIO;
484 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, float theParam):
485 myApp(theApp), myIO(theIO), myParam(theParam)
487 virtual void Execute(){
488 if(SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(myApp)){
489 SVTK_View* aView = aViewWindow->getView();
490 aView->SetTransparency(myIO,myParam);
492 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
493 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
495 soccViewer->setTransparency(myIO,myParam);
500 ProcessVoidEvent(new TEvent(app,anIO,transp));
504 bool GEOM_Swig::initGeomGen()
506 return GeometryGUI::InitGeomGen();