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"
62 #include "SALOMEDS_SObject.hxx"
63 #include "SALOMEDS_Study.hxx"
66 #include <TopExp_Explorer.hxx>
67 #include <TopTools_MapOfShape.hxx>
68 #include <TopTools_ListOfShape.hxx>
69 #include <TopTools_ListIteratorOfListOfShape.hxx>
70 #include <BRepAdaptor_Surface.hxx>
71 #include <BRepAdaptor_Curve.hxx>
72 #include <GeomAbs_CurveType.hxx>
73 #include <GeomAbs_SurfaceType.hxx>
75 #include <TopoDS_Edge.hxx>
76 #include <TopoDS_Face.hxx>
77 #include <TopoDS_Iterator.hxx>
78 #include <AIS_ListOfInteractive.hxx>
79 #include <AIS_ListIteratorOfListOfInteractive.hxx>
80 #include <V3d_Viewer.hxx>
85 static GEOM_Client ShapeReader;
87 inline OCCViewer_Viewer* GetOCCViewer(SUIT_Application* theApp){
88 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
89 if(window && window->getViewManager()->getType() == OCCViewer_Viewer::Type()){
90 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( window );
92 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
94 return vm->getOCCViewer();
101 inline SVTK_ViewWindow* GetSVTKViewWindow(SUIT_Application* theApp){
102 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
103 if(window && window->getViewManager()->getType() == VTKViewer_Viewer::Type())
104 return dynamic_cast<SVTK_ViewWindow*>( window );
109 GEOM_Swig::GEOM_Swig()
111 // MESSAGE("Constructeur");
114 GEOM_Swig::~GEOM_Swig()
116 // MESSAGE("Destructeur");
119 void GEOM_Swig::createAndDisplayGO (const char* Entry)
121 class TEvent: public SALOME_Event
125 TEvent(const char* theEntry):
128 virtual void Execute()
130 SUIT_Application* app = SUIT_Session::session()->activeApplication();
133 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
134 if (!ActiveStudy) return;
136 _PTR(Study) aStudy(ActiveStudy->studyDS());
137 _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
139 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
140 if (CORBA::is_nil(Geom))
144 _PTR(SComponent) father = aStudy->FindComponent("GEOM");
145 aStudyBuilder->DefineComponentInstance
146 (father, SalomeApp_Application::orb()->object_to_string(Geom));
147 father->ComponentIOR(aFatherIOR);
149 _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
150 _PTR(GenericAttribute) anAttr;
153 if (!obj->FindAttribute(anAttr, "AttributeIOR"))
155 _PTR(AttributeIOR) anIOR(anAttr);
156 string anIORValue = anIOR->Value();
158 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
159 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
162 if (obj->FindAttribute(anAttr, "AttributeName")) {
163 _PTR(AttributeName) aName (anAttr);
164 string aNameValue = aName->Value();
166 /*SUIT_Operation* op = new SalomeApp_ImportOperation (app);
169 _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
170 aStudyBuilder->Addreference(newObj1, obj);
171 // commit transaction
174 Handle(GEOM_InteractiveObject) anIO =
175 new GEOM_InteractiveObject (const_cast<char*>(anIORValue.c_str()),
176 const_cast<char*>(aFatherIOR.c_str()),
178 const_cast<char*>( obj->GetID().c_str()));
180 if (SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(app)) {
181 SVTK_RenderWindowInteractor* aRenderInter= svtkViewWindow->getRWInteractor();
182 int aMode = aRenderInter->GetDisplayMode();
184 vtkActorCollection* theActors =
185 GEOM_AssemblyBuilder::BuildActors(Shape,0,aMode,true);
186 theActors->InitTraversal();
187 while (vtkActor* anActor = theActors->GetNextActor()) {
188 GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
189 GActor->setName(const_cast<char*>(aNameValue.c_str()));
191 aRenderInter->Display(GActor);
193 aRenderInter->Update();
194 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app)) {
195 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
196 Handle(GEOM_AISShape) aSh =
197 new GEOM_AISShape (Shape,const_cast<char*>(aNameValue.c_str()));
198 aSh->setName(const_cast<char*>(aNameValue.c_str()));
201 ic->AddOrRemoveCurrentObject(aSh,true);
203 // update object browser
204 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(app);
206 CAM_Module* module = app->module("Geometry");
207 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
209 appMod->updateObjBrowser(true);
216 // MESSAGE("createAndDisplayGO");
217 ProcessVoidEvent(new TEvent (Entry));
221 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
223 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
224 if ( CORBA::is_nil( Geom ) )
227 GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
228 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
230 GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
231 TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
234 if(subshape.ShapeType() == TopAbs_COMPOUND) {
236 TopTools_ListOfShape CL;
238 TopTools_ListIteratorOfListOfShape itC;
239 for(itC.Initialize(CL); itC.More(); itC.Next()) {
240 for(it.Initialize(itC.Value()); it.More(); it.Next()) {
241 if (it.Value().ShapeType() == TopAbs_COMPOUND) {
242 if (it.Value().IsSame(subshape))
246 CL.Append(it.Value());
252 TopExp_Explorer Exp(shape, subshape.ShapeType());
253 TopTools_MapOfShape M;
255 if(M.Add(Exp.Current())) {
256 if(Exp.Current().IsSame(subshape))
267 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
269 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
270 if ( CORBA::is_nil( Geom ) )
273 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
274 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
276 if( shape.IsNull() ) {
277 return "Null Shape" ;
280 switch (shape.ShapeType() )
282 case TopAbs_COMPOUND:
283 { return "Compound" ;}
284 case TopAbs_COMPSOLID:
285 { return "Compound Solid" ;}
292 BRepAdaptor_Surface surf(TopoDS::Face(shape));
293 if ( surf.GetType() == GeomAbs_Plane ) {
295 } else if ( surf.GetType() == GeomAbs_Cylinder ) {
296 return "Cylindrical Face" ;
297 } else if ( surf.GetType() == GeomAbs_Sphere ) {
298 return "Spherical Face" ;
299 } else if ( surf.GetType() == GeomAbs_Torus ) {
300 return "Toroidal Face" ;
301 } else if ( surf.GetType() == GeomAbs_Cone ) {
302 return "Conical Face" ;
304 return "GEOM::FACE" ;
311 BRepAdaptor_Curve curv(TopoDS::Edge(shape));
312 if ( curv.GetType() == GeomAbs_Line ) {
313 if ( (Abs(curv.FirstParameter()) >= 1E6 ) ||
314 (Abs(curv.LastParameter()) >= 1E6 )) {
318 } else if ( curv.GetType() == GeomAbs_Circle ) {
319 if ( curv.IsClosed() )
336 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
338 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
339 if ( CORBA::is_nil( Geom ) )
342 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
343 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
345 if( shape.IsNull() ) {
349 switch (shape.ShapeType() )
351 case TopAbs_COMPOUND:
352 { return "ICON_OBJBROWSER_COMPOUND" ;}
353 case TopAbs_COMPSOLID:
354 { return "ICON_OBJBROWSER_COMPSOLID" ;}
356 { return "ICON_OBJBROWSER_SOLID" ;}
358 { return "ICON_OBJBROWSER_SHELL" ;}
360 { return "ICON_OBJBROWSER_FACE" ;}
362 { return "ICON_OBJBROWSER_WIRE" ;}
364 { return "ICON_OBJBROWSER_EDGE" ;}
366 { return "ICON_OBJBROWSER_VERTEX" ;}
372 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
374 SUIT_Application* app = SUIT_Session::session()->activeApplication();
377 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
379 class TEvent: public SALOME_Event{
380 SUIT_Application* myApp;
381 Handle(SALOME_InteractiveObject) myIO;
384 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, int theParam):
385 myApp(theApp), myIO(theIO), myParam(theParam)
387 virtual void Execute(){
388 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
389 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
390 myRenderInter->SetDisplayMode(myIO,myParam);
391 myRenderInter->Update();
393 else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
394 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
396 soccViewer->switchRepresentation(myIO,myParam);
401 ProcessVoidEvent(new TEvent(app,anIO,theMode));
404 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
406 SUIT_Application* app = SUIT_Session::session()->activeApplication();
409 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
411 QColor aColor(red,green,blue);
413 class TEvent: public SALOME_Event{
414 SUIT_Application* myApp;
415 Handle(SALOME_InteractiveObject) myIO;
418 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, const QColor& theParam):
419 myApp(theApp), myIO(theIO), myParam(theParam)
421 virtual void Execute(){
422 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
423 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
424 myRenderInter->SetColor(myIO,myParam);
425 myRenderInter->Update();
426 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)){
427 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
428 AIS_ListOfInteractive List;
429 ic->DisplayedObjects(List);
430 AIS_ListIteratorOfListOfInteractive ite(List);
431 for ( ; ite.More(); ite.Next() ) {
432 Handle(SALOME_InteractiveObject) anObj =
433 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
434 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( myIO ) ) {
435 Quantity_Color CSFColor = Quantity_Color ( myParam.red() / 255.,
436 myParam.green() / 255.,
437 myParam.blue() / 255.,
439 ite.Value()->SetColor( CSFColor );
440 if ( ite.Value()->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
441 Handle(GEOM_AISShape)::DownCast( ite.Value() )->SetShadingColor( CSFColor );
442 ite.Value()->Redisplay( Standard_True );
450 ProcessVoidEvent(new TEvent(app,anIO,aColor));
453 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
455 SUIT_Application* app = SUIT_Session::session()->activeApplication();
458 Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(theEntry, "GEOM", "");
460 class TEvent: public SALOME_Event{
461 SUIT_Application* myApp;
462 Handle(SALOME_InteractiveObject) myIO;
465 TEvent(SUIT_Application* theApp, const Handle(SALOME_InteractiveObject)& theIO, float theParam):
466 myApp(theApp), myIO(theIO), myParam(theParam)
468 virtual void Execute(){
469 if(SVTK_ViewWindow* svtkViewWindow = GetSVTKViewWindow(myApp)){
470 SVTK_RenderWindowInteractor* myRenderInter= svtkViewWindow->getRWInteractor();
471 myRenderInter->SetTransparency(myIO,myParam);
472 myRenderInter->Update();
473 }else if(OCCViewer_Viewer* occViewer = GetOCCViewer(myApp)) {
474 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( occViewer );
476 soccViewer->setTransparency(myIO,myParam);
481 ProcessVoidEvent(new TEvent(app,anIO,transp));
485 bool GEOM_Swig::initGeomGen()
487 return GeometryGUI::InitGeomGen();