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.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : GeometryGUI_Swig.cxx
23 // Author : Nicolas REJNERI, Paul RASCLE
26 #include "GeometryGUI_Swig.hxx"
28 #include "GeometryGUI.h"
30 #include "SUIT_Desktop.h"
31 #include "SUIT_Session.h"
32 #include "SalomeApp_Application.h"
33 #include "SalomeApp_Study.h"
35 #include "OB_Browser.h"
37 #include "OCCViewer_ViewWindow.h"
38 #include "OCCViewer_ViewManager.h"
39 #include "SOCC_ViewModel.h"
41 #include "SVTK_ViewModel.h"
42 #include "SVTK_ViewWindow.h"
43 #include "SVTK_View.h"
44 #include "SVTK_Renderer.h"
46 #include "GEOM_Actor.h"
47 #include "GEOM_Client.hxx"
48 #include "GEOM_AISShape.hxx"
49 #include "GEOM_InteractiveObject.hxx"
50 #include "GEOM_Displayer.h"
52 #include "SALOME_Event.h"
56 #include <TopoDS_Shape.hxx>
57 #include <AIS_ListOfInteractive.hxx>
58 #include <AIS_ListIteratorOfListOfInteractive.hxx>
59 #include <AIS_Drawer.hxx>
60 #include <BRepTools.hxx>
63 #include <SALOMEconfig.h>
64 #include CORBA_SERVER_HEADER(GEOM_Gen)
66 #include <vtkRenderer.h>
71 static GEOM_Client ShapeReader;
73 inline OCCViewer_Viewer* GetOCCViewer(SUIT_Application* theApp){
74 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
75 if(window && window->getViewManager()->getType() == OCCViewer_Viewer::Type()){
76 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( window );
78 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
80 return vm->getOCCViewer();
87 inline SVTK_ViewWindow* GetSVTKViewWindow(SUIT_Application* theApp){
88 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
89 if(window && window->getViewManager()->getType() == SVTK_Viewer::Type())
90 return dynamic_cast<SVTK_ViewWindow*>( window );
95 GEOM_Swig::GEOM_Swig()
97 // MESSAGE("Constructeur");
100 GEOM_Swig::~GEOM_Swig()
102 // MESSAGE("Destructeur");
105 void GEOM_Swig::createAndDisplayGO (const char* Entry, bool isUpdated)
107 class TEvent: public SALOME_Event
112 TEvent(const char* theEntry, bool toUpdateViewer):
114 myUpdateViewer(toUpdateViewer)
116 virtual void Execute()
118 SUIT_Application* app = SUIT_Session::session()->activeApplication();
121 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
122 if (!ActiveStudy) return;
124 _PTR(Study) aStudy(ActiveStudy->studyDS());
125 _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
127 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
128 if (CORBA::is_nil(Geom)) {
129 GeometryGUI::InitGeomGen();
130 Geom = GeometryGUI::GetGeomGen();
132 if (CORBA::is_nil(Geom))
136 _PTR(SComponent) father = aStudy->FindComponent("GEOM");
139 if (!father->ComponentIOR(aFatherIOR)) {
140 CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string(Geom);
141 aStudyBuilder->LoadWith(father, objStr.in());
142 father->ComponentIOR(aFatherIOR);
145 _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
150 _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);
158 if (!Shape.IsNull()) {
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 GEOM_Displayer(ActiveStudy).Display(anIO, myUpdateViewer);
177 /*if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app)) {
178 SVTK_View* aView = aViewWindow->getView();
179 int aMode = aView->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 aView->Display(GActor);
191 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app)) {
192 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
193 Handle(GEOM_AISShape) aSh =
194 new GEOM_AISShape (Shape,const_cast<char*>(aNameValue.c_str()));
195 aSh->setName(const_cast<char*>(aNameValue.c_str()));
198 ic->AddOrRemoveCurrentObject(aSh,true);
200 // update object browser
201 SalomeApp_Application* app = NULL; //dynamic_cast<SalomeApp_Application*>(app);
203 CAM_Module* module = app->module("Geometry");
204 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
206 appMod->updateObjBrowser(true);
213 // MESSAGE("createAndDisplayGO");
214 ProcessVoidEvent(new TEvent (Entry, isUpdated));
216 class TEventUpdateBrowser: public SALOME_Event
219 TEventUpdateBrowser() {}
220 virtual void Execute() {
221 SalomeApp_Application* app =
222 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);
232 ProcessVoidEvent(new TEventUpdateBrowser ());
235 void GEOM_Swig::createAndDisplayFitAllGO (const char* Entry)
237 class TEventFitAll: public SALOME_Event
241 virtual void Execute() {
242 SUIT_Application* app = SUIT_Session::session()->activeApplication();
245 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app))
247 SVTK_View* aView = aViewWindow->getView();
248 aView->GetRenderer()->OnFitAll();
250 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app))
252 Handle(V3d_Viewer) aViewer3d = occViewer->getViewer3d();
253 aViewer3d->InitActiveViews();
255 if (aViewer3d->MoreActiveViews())
256 aViewer3d->ActiveView()->FitAll();
261 createAndDisplayGO(Entry);
262 ProcessVoidEvent(new TEventFitAll());
265 void GEOM_Swig::UpdateViewer()
267 class TEventUpdateViewer: public SALOME_Event
270 TEventUpdateViewer() {}
271 virtual void Execute() {
272 SUIT_Application* app = SUIT_Session::session()->activeApplication();
274 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
275 if (!ActiveStudy) return;
277 GEOM_Displayer(ActiveStudy).UpdateViewer();
281 ProcessVoidEvent(new TEventUpdateViewer());
284 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
286 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
287 if (CORBA::is_nil(aGeomGen))
290 GEOM::GEOM_Object_var aMainShape = aGeomGen->GetIORFromString(IOR);
291 GEOM::GEOM_Object_var aSubShape = aGeomGen->GetIORFromString(SubIOR);
292 if (CORBA::is_nil(aMainShape) || CORBA::is_nil(aSubShape))
295 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
296 aGeomGen->GetIShapesOperations(aMainShape->GetStudyID());
297 if (CORBA::is_nil(anIShapesOperations))
300 return anIShapesOperations->GetTopologyIndex(aMainShape, aSubShape);
303 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
305 TCollection_AsciiString aTypeName ("Shape of unknown type");
307 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
308 if (!CORBA::is_nil(aGeomGen))
310 GEOM::GEOM_Object_var aShape = aGeomGen->GetIORFromString(IOR);
311 if (!CORBA::is_nil(aShape))
313 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
314 aGeomGen->GetIShapesOperations(aShape->GetStudyID());
315 if (!CORBA::is_nil(anIShapesOperations))
317 aTypeName = anIShapesOperations->GetShapeTypeString(aShape);
322 return CORBA::string_dup(aTypeName.ToCString());
326 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
328 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
329 if ( CORBA::is_nil( Geom ) )
332 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
333 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
335 if( shape.IsNull() ) {
339 switch (shape.ShapeType() )
341 case TopAbs_COMPOUND:
342 { return "ICON_OBJBROWSER_COMPOUND" ;}
343 case TopAbs_COMPSOLID:
344 { return "ICON_OBJBROWSER_COMPSOLID" ;}
346 { return "ICON_OBJBROWSER_SOLID" ;}
348 { return "ICON_OBJBROWSER_SHELL" ;}
350 { return "ICON_OBJBROWSER_FACE" ;}
352 { return "ICON_OBJBROWSER_WIRE" ;}
354 { return "ICON_OBJBROWSER_EDGE" ;}
356 { return "ICON_OBJBROWSER_VERTEX" ;}
362 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode, bool isUpdated)
364 class TEvent: public SALOME_Event {
369 TEvent(const char* theEntryArg, int theModeArg, bool theUpdated):
370 myEntry(theEntryArg), myMode(theModeArg), myUpdateViewer(theUpdated)
372 virtual void Execute() {
373 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
376 Handle(SALOME_InteractiveObject) anIO =
377 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
379 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
380 SVTK_View* aView = aViewWindow->getView();
381 aView->SetDisplayMode(anIO, myMode);
385 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
386 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
388 soccViewer->switchRepresentation(anIO, myMode, myUpdateViewer);
393 ProcessVoidEvent(new TEvent (theEntry, theMode, isUpdated));
396 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue, bool isUpdated)
398 class TEvent: public SALOME_Event {
405 TEvent(const char* theEntryArg, int theR, int theG, int theB, bool theUpdated):
406 myEntry(theEntryArg), myRed(theR), myGreen(theG), myBlue(theB), myUpdateViewer(theUpdated)
408 virtual void Execute() {
409 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
412 Handle(SALOME_InteractiveObject) anIO =
413 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
415 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)){
416 SVTK_View* aView = aViewWindow->getView();
417 QColor aColor (myRed, myGreen, myBlue);
418 aView->SetColor(anIO, aColor);
421 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
422 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
423 AIS_ListOfInteractive List;
424 ic->DisplayedObjects(List);
425 AIS_ListIteratorOfListOfInteractive ite (List);
426 for (; ite.More(); ite.Next()) {
427 Handle(SALOME_InteractiveObject) anObj =
428 Handle(SALOME_InteractiveObject)::DownCast(ite.Value()->GetOwner());
429 if (!anObj.IsNull() && anObj->hasEntry() && anObj->isSame(anIO)) {
430 Quantity_Color CSFColor =
431 Quantity_Color(myRed/255., myGreen/255., myBlue/255., Quantity_TOC_RGB);
432 ite.Value()->SetColor(CSFColor);
433 if (ite.Value()->IsKind(STANDARD_TYPE(GEOM_AISShape)))
434 Handle(GEOM_AISShape)::DownCast(ite.Value())->SetShadingColor(CSFColor);
435 ic->Redisplay(ite.Value(), true, true);
444 ProcessVoidEvent(new TEvent(theEntry, red, green, blue, isUpdated));
447 void GEOM_Swig::setTransparency(const char* theEntry, float transp, bool isUpdated)
449 class TEvent: public SALOME_Event {
454 TEvent(const char* theEntryArg, float theParam, bool theUpdated):
455 myEntry(theEntryArg), myParam(theParam), myUpdateViewer(theUpdated)
457 virtual void Execute() {
458 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
461 Handle(SALOME_InteractiveObject) anIO =
462 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
464 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
465 SVTK_View* aView = aViewWindow->getView();
466 aView->SetTransparency(anIO, myParam);
469 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
470 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
472 soccViewer->setTransparency(anIO, myParam, myUpdateViewer);
477 ProcessVoidEvent(new TEvent (theEntry, transp, isUpdated));
481 class TInitGeomGenEvent: public SALOME_Event {
483 typedef bool TResult;
485 TInitGeomGenEvent() : myResult(false) {}
486 virtual void Execute() {
487 myResult = GeometryGUI::InitGeomGen();
490 bool GEOM_Swig::initGeomGen()
492 return ProcessEvent(new TInitGeomGenEvent());
497 void GEOM_Swig::eraseGO (const char* Entry, bool allWindows)
499 class TEvent: public SALOME_Event
502 bool myFromAllWindows;
504 TEvent(const char* theEntry, bool fromAllWindows):
505 myEntry(theEntry), myFromAllWindows(fromAllWindows)
507 virtual void Execute()
509 SUIT_Application* app = SUIT_Session::session()->activeApplication();
511 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
512 if (!ActiveStudy) return;
514 Handle (SALOME_InteractiveObject) aIO = new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
516 GEOM_Displayer(ActiveStudy).Erase(aIO, true);
517 /* if (myFromAllWindows) {
518 QPtrList<SUIT_ViewWindow> aWindows = app->desktop()->windows();
519 SUIT_ViewWindow* aWin = 0;
520 for (aWin = aWindows.first(); aWin; aWin = aWindows.next()) {
521 EraseObject(aWin, aIO);
524 SUIT_ViewWindow* aWin = app->desktop()->activeWindow();
526 EraseObject(aWin, aIO);
531 void EraseObject(SUIT_ViewWindow* theWin, Handle (SALOME_InteractiveObject) theIO)
533 if (theWin->getViewManager()->getType() == OCCViewer_Viewer::Type()){
534 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( theWin );
536 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
538 SOCC_Viewer* aViewer = dynamic_cast<SOCC_Viewer*>(vm->getOCCViewer());
540 SALOME_Prs* aPrs = aViewer->CreatePrs(myEntry.c_str());
542 SALOME_OCCPrs* aOccPrs = dynamic_cast<SALOME_OCCPrs*>(aPrs);
544 aViewer->Erase(aOccPrs);
551 } else if (theWin->getViewManager()->getType() == SVTK_Viewer::Type()){
552 SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>( theWin );
554 aViewWindow->Erase(theIO);
560 ProcessVoidEvent(new TEvent(Entry, allWindows));
565 void GEOM_Swig::setDeflection(const char* theEntry, float theDeflect)
567 class TEvent: public SALOME_Event {
571 TEvent(const char* theEntryArg, float theParam):
572 myEntry(theEntryArg), myParam(theParam)
574 virtual void Execute() {
575 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
578 Handle(SALOME_InteractiveObject) anIO =
579 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
581 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
582 vtkActorCollection* aActors = aViewWindow->getRenderer()->GetActors();
583 aActors->InitTraversal();
584 while (vtkActor* aAct = aActors->GetNextActor()) {
585 if (GEOM_Actor* aGeomActor = dynamic_cast<GEOM_Actor*>(aAct)) {
586 if (aGeomActor->hasIO()) {
587 Handle(SALOME_InteractiveObject) aNextIO = aGeomActor->getIO();
588 if (aNextIO->isSame(anIO)) {
589 aGeomActor->setDeflection(myParam);
590 aViewWindow->Repaint();
596 // aView->SetTransparency(anIO, myParam);
598 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
599 Handle(AIS_InteractiveContext) aContext = occViewer->getAISContext();
600 AIS_ListOfInteractive aAISList;
601 aContext->DisplayedObjects(aAISList);
602 AIS_ListIteratorOfListOfInteractive it(aAISList);
603 for (; it.More(); it.Next()) {
604 Handle(SALOME_InteractiveObject) aObj =
605 Handle(SALOME_InteractiveObject)::DownCast(it.Value()->GetOwner());
606 if ((!aObj.IsNull()) && aObj->hasEntry() && aObj->isSame(anIO)) {
607 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(it.Value());
608 if (!aShape.IsNull()) {
609 TopoDS_Shape aSh = aShape->Shape();
611 BRepTools::Clean(aSh);
613 aShape->SetOwnDeviationCoefficient( myParam );
614 aShape->SetOwnHLRDeviationAngle( 1.57 );
615 aContext->Redisplay(aShape);
624 ProcessVoidEvent(new TEvent (theEntry, theDeflect));