1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : GeometryGUI_Swig.cxx
25 // Author : Nicolas REJNERI, Paul RASCLE
27 #include "GeometryGUI_Swig.hxx"
29 #include "GeometryGUI.h"
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_Study.h"
36 #include "OB_Browser.h"
38 #include "OCCViewer_ViewWindow.h"
39 #include "OCCViewer_ViewManager.h"
40 #include "SOCC_ViewModel.h"
43 #include "SVTK_ViewModel.h"
44 #include "SVTK_ViewWindow.h"
45 #include "SVTK_View.h"
46 #include "SVTK_Renderer.h"
49 #include "GEOM_Actor.h"
50 #include "GEOM_Client.hxx"
51 #include "GEOM_AISShape.hxx"
52 #include "GEOM_InteractiveObject.hxx"
53 #include "GEOM_Displayer.h"
55 #include "SALOME_Event.h"
59 #include <TopoDS_Shape.hxx>
60 #include <AIS_ListOfInteractive.hxx>
61 #include <AIS_ListIteratorOfListOfInteractive.hxx>
62 #include <AIS_Drawer.hxx>
63 #include <BRepTools.hxx>
66 #include <SALOMEconfig.h>
67 #include CORBA_SERVER_HEADER(GEOM_Gen)
69 #include <vtkRenderer.h>
71 static SHAPE_READER(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))
135 std::string aFatherIOR;
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 std::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 std::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::setVectorsMode(const char* theEntry, bool isOn, bool isUpdated)
398 class TEvent: public SALOME_Event {
403 TEvent(const char* theEntryArg, bool theOn, bool theUpdated):
404 myEntry(theEntryArg), myOn(theOn), myUpdateViewer(theUpdated)
406 virtual void Execute() {
407 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
410 Handle(SALOME_InteractiveObject) anIO =
411 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
413 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
414 SVTK_View* aView = aViewWindow->getView();
415 SVTK_Viewer* stvkViewer = dynamic_cast<SVTK_Viewer*>(aViewWindow->getViewManager()->getViewModel());
416 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( stvkViewer->CreatePrs( myEntry.c_str() ) );
417 vtkActorCollection* anActors = vtkPrs->GetObjects();
418 anActors->InitTraversal();
419 while (vtkActor* anAct = anActors->GetNextActor()) {
420 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(anAct);
421 aGeomActor->SetVectorMode(!aGeomActor->GetVectorMode());
426 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
427 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
428 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
430 SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( soccViewer->CreatePrs( myEntry.c_str() ) );
431 if ( occPrs && !occPrs->IsNull() ) {
432 AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
433 AIS_ListIteratorOfListOfInteractive interIter( shapes );
434 for ( ; interIter.More(); interIter.Next() ) {
435 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast( interIter.Value() );
436 aSh->SetDisplayVectors(myOn);
437 ic->RecomputePrsOnly(interIter.Value());
445 ProcessVoidEvent(new TEvent (theEntry, isOn, isUpdated));
448 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue, bool isUpdated)
450 class TEvent: public SALOME_Event {
457 TEvent(const char* theEntryArg, int theR, int theG, int theB, bool theUpdated):
458 myEntry(theEntryArg), myRed(theR), myGreen(theG), myBlue(theB), myUpdateViewer(theUpdated)
460 virtual void Execute() {
461 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
464 Handle(SALOME_InteractiveObject) anIO =
465 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
467 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)){
468 SVTK_View* aView = aViewWindow->getView();
469 QColor aColor (myRed, myGreen, myBlue);
470 aView->SetColor(anIO, aColor);
473 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
474 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
475 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
478 SALOME_Prs* prs= soccViewer->CreatePrs( myEntry.c_str() );
479 const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
480 if ( !anOCCPrs || anOCCPrs->IsNull() )
483 // get objects to be displayed
484 AIS_ListOfInteractive anAISObjects;
485 anOCCPrs->GetObjects( anAISObjects );
486 AIS_ListIteratorOfListOfInteractive ite( anAISObjects );
487 Quantity_Color CSFColor = Quantity_Color(myRed/255., myGreen/255., myBlue/255., Quantity_TOC_RGB);
488 for ( ; ite.More(); ite.Next() )
490 if(!ic->IsDisplayed(ite.Value()))continue; //only displayed ais
491 ite.Value()->SetColor(CSFColor);
492 if (ite.Value()->IsKind(STANDARD_TYPE(GEOM_AISShape))) Handle(GEOM_AISShape)::DownCast(ite.Value())->SetShadingColor(CSFColor);
493 ite.Value()->Redisplay(Standard_True); // as in OnColor
495 if (myUpdateViewer) occViewer->update();
500 ProcessVoidEvent(new TEvent(theEntry, red, green, blue, isUpdated));
503 void GEOM_Swig::setTransparency(const char* theEntry, float transp, bool isUpdated)
505 class TEvent: public SALOME_Event {
510 TEvent(const char* theEntryArg, float theParam, bool theUpdated):
511 myEntry(theEntryArg), myParam(theParam), myUpdateViewer(theUpdated)
513 virtual void Execute() {
514 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
517 Handle(SALOME_InteractiveObject) anIO =
518 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
520 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
521 SVTK_View* aView = aViewWindow->getView();
522 aView->SetTransparency(anIO, myParam);
525 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
526 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
528 soccViewer->setTransparency(anIO, myParam, myUpdateViewer);
533 ProcessVoidEvent(new TEvent (theEntry, transp, isUpdated));
537 class TInitGeomGenEvent: public SALOME_Event {
539 typedef bool TResult;
541 TInitGeomGenEvent() : myResult(false) {}
542 virtual void Execute() {
543 myResult = GeometryGUI::InitGeomGen();
546 bool GEOM_Swig::initGeomGen()
548 return ProcessEvent(new TInitGeomGenEvent());
553 void GEOM_Swig::eraseGO (const char* Entry, bool allWindows)
555 class TEvent: public SALOME_Event
558 bool myFromAllWindows;
560 TEvent(const char* theEntry, bool fromAllWindows):
561 myEntry(theEntry), myFromAllWindows(fromAllWindows)
563 virtual void Execute()
565 SUIT_Application* app = SUIT_Session::session()->activeApplication();
567 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
568 if (!ActiveStudy) return;
570 Handle (SALOME_InteractiveObject) aIO = new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
572 GEOM_Displayer(ActiveStudy).Erase(aIO, true);
573 /* if (myFromAllWindows) {
574 QPtrList<SUIT_ViewWindow> aWindows = app->desktop()->windows();
575 SUIT_ViewWindow* aWin = 0;
576 for (aWin = aWindows.first(); aWin; aWin = aWindows.next()) {
577 EraseObject(aWin, aIO);
580 SUIT_ViewWindow* aWin = app->desktop()->activeWindow();
582 EraseObject(aWin, aIO);
587 void EraseObject(SUIT_ViewWindow* theWin, Handle (SALOME_InteractiveObject) theIO)
589 if (theWin->getViewManager()->getType() == OCCViewer_Viewer::Type()){
590 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( theWin );
592 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
594 SOCC_Viewer* aViewer = dynamic_cast<SOCC_Viewer*>(vm->getOCCViewer());
596 SALOME_Prs* aPrs = aViewer->CreatePrs(myEntry.c_str());
598 SALOME_OCCPrs* aOccPrs = dynamic_cast<SALOME_OCCPrs*>(aPrs);
600 aViewer->Erase(aOccPrs);
607 } else if (theWin->getViewManager()->getType() == SVTK_Viewer::Type()){
608 SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>( theWin );
610 aViewWindow->Erase(theIO);
616 ProcessVoidEvent(new TEvent(Entry, allWindows));
621 void GEOM_Swig::setDeflection(const char* theEntry, float theDeflect)
623 class TEvent: public SALOME_Event {
627 TEvent(const char* theEntryArg, float theParam):
628 myEntry(theEntryArg), myParam(theParam)
630 virtual void Execute() {
631 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
634 Handle(SALOME_InteractiveObject) anIO =
635 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
637 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
638 vtkActorCollection* aActors = aViewWindow->getRenderer()->GetActors();
639 aActors->InitTraversal();
640 while (vtkActor* aAct = aActors->GetNextActor()) {
641 if (GEOM_Actor* aGeomActor = dynamic_cast<GEOM_Actor*>(aAct)) {
642 if (aGeomActor->hasIO()) {
643 Handle(SALOME_InteractiveObject) aNextIO = aGeomActor->getIO();
644 if (aNextIO->isSame(anIO)) {
645 aGeomActor->setDeflection(myParam);
646 aViewWindow->Repaint();
652 // aView->SetTransparency(anIO, myParam);
654 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
655 Handle(AIS_InteractiveContext) aContext = occViewer->getAISContext();
656 AIS_ListOfInteractive aAISList;
657 aContext->DisplayedObjects(aAISList);
658 AIS_ListIteratorOfListOfInteractive it(aAISList);
659 for (; it.More(); it.Next()) {
660 Handle(SALOME_InteractiveObject) aObj =
661 Handle(SALOME_InteractiveObject)::DownCast(it.Value()->GetOwner());
662 if ((!aObj.IsNull()) && aObj->hasEntry() && aObj->isSame(anIO)) {
663 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(it.Value());
664 if (!aShape.IsNull()) {
665 TopoDS_Shape aSh = aShape->Shape();
667 BRepTools::Clean(aSh);
669 aShape->SetOwnDeviationCoefficient( myParam );
670 aShape->SetOwnHLRDeviationAngle( 1.57 );
671 aContext->Redisplay(aShape);
680 ProcessVoidEvent(new TEvent (theEntry, theDeflect));