1 // Copyright (C) 2007-2008 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
22 // GEOM GEOMGUI : GUI for Geometry component
23 // File : GeometryGUI_Swig.cxx
24 // Author : Nicolas REJNERI, Paul RASCLE
28 #include "GeometryGUI_Swig.hxx"
29 #include "SALOMEGUI_Swig.hxx"
31 #include "GeometryGUI.h"
33 #include "SUIT_Session.h"
34 #include "SalomeApp_Application.h"
35 #include "SalomeApp_Study.h"
36 #include "SalomeApp_ImportOperation.h"
38 #include "OB_Browser.h"
40 #include "OCCViewer_ViewWindow.h"
41 #include "OCCViewer_ViewManager.h"
42 #include "SOCC_ViewModel.h"
44 #include "SVTK_ViewModel.h"
45 #include "SVTK_ViewWindow.h"
46 #include "SVTK_View.h"
47 #include "SVTK_Renderer.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"
54 #include "GEOM_Displayer.h"
56 #include "SALOME_Event.hxx"
58 #include "utilities.h"
60 #include "SALOMEDSClient.hxx"
64 #include <TopoDS_Shape.hxx>
65 #include <AIS_ListOfInteractive.hxx>
66 #include <AIS_ListIteratorOfListOfInteractive.hxx>
67 #include <AIS_Drawer.hxx>
68 #include <BRepTools.hxx>
71 #include <SALOMEconfig.h>
72 #include CORBA_SERVER_HEADER(GEOM_Gen)
74 #include <vtkRenderer.h>
79 static GEOM_Client ShapeReader;
81 inline OCCViewer_Viewer* GetOCCViewer(SUIT_Application* theApp){
82 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
83 if(window && window->getViewManager()->getType() == OCCViewer_Viewer::Type()){
84 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( window );
86 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
88 return vm->getOCCViewer();
95 inline SVTK_ViewWindow* GetSVTKViewWindow(SUIT_Application* theApp){
96 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
97 if(window && window->getViewManager()->getType() == SVTK_Viewer::Type())
98 return dynamic_cast<SVTK_ViewWindow*>( window );
103 GEOM_Swig::GEOM_Swig()
105 // MESSAGE("Constructeur");
108 GEOM_Swig::~GEOM_Swig()
110 // MESSAGE("Destructeur");
113 void GEOM_Swig::createAndDisplayGO (const char* Entry, bool isUpdated)
115 class TEvent: public SALOME_Event
120 TEvent(const char* theEntry, bool toUpdateViewer):
122 myUpdateViewer(toUpdateViewer)
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 CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string(Geom);
149 aStudyBuilder->LoadWith(father, objStr.in());
150 father->ComponentIOR(aFatherIOR);
153 _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
158 _PTR(GenericAttribute) anAttr;
159 if (!obj->FindAttribute(anAttr, "AttributeIOR"))
161 _PTR(AttributeIOR) anIOR(anAttr);
162 string anIORValue = anIOR->Value();
164 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
165 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
166 if (!Shape.IsNull()) {
167 if (obj->FindAttribute(anAttr, "AttributeName")) {
168 _PTR(AttributeName) aName (anAttr);
169 string aNameValue = aName->Value();
171 /*SUIT_Operation* op = new SalomeApp_ImportOperation (app);
174 _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
175 aStudyBuilder->Addreference(newObj1, obj);
176 // commit transaction
178 Handle(GEOM_InteractiveObject) anIO =
179 new GEOM_InteractiveObject (const_cast<char*>(anIORValue.c_str()),
180 const_cast<char*>(aFatherIOR.c_str()),
182 const_cast<char*>( obj->GetID().c_str()));
184 GEOM_Displayer(ActiveStudy).Display(anIO, myUpdateViewer);
185 /*if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app)) {
186 SVTK_View* aView = aViewWindow->getView();
187 int aMode = aView->GetDisplayMode();
189 vtkActorCollection* theActors =
190 GEOM_AssemblyBuilder::BuildActors(Shape,0,aMode,true);
191 theActors->InitTraversal();
192 while (vtkActor* anActor = theActors->GetNextActor()) {
193 GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
194 GActor->setName(const_cast<char*>(aNameValue.c_str()));
196 aView->Display(GActor);
199 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app)) {
200 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
201 Handle(GEOM_AISShape) aSh =
202 new GEOM_AISShape (Shape,const_cast<char*>(aNameValue.c_str()));
203 aSh->setName(const_cast<char*>(aNameValue.c_str()));
206 ic->AddOrRemoveCurrentObject(aSh,true);
208 // update object browser
209 SalomeApp_Application* app = NULL; //dynamic_cast<SalomeApp_Application*>(app);
211 CAM_Module* module = app->module("Geometry");
212 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
214 appMod->updateObjBrowser(true);
221 // MESSAGE("createAndDisplayGO");
222 ProcessVoidEvent(new TEvent (Entry, isUpdated));
224 class TEventUpdateBrowser: public SALOME_Event
227 TEventUpdateBrowser() {}
228 virtual void Execute() {
229 SalomeApp_Application* app =
230 dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
232 CAM_Module* module = app->module("Geometry");
233 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
234 if (appMod) appMod->updateObjBrowser(true);
240 ProcessVoidEvent(new TEventUpdateBrowser ());
243 void GEOM_Swig::createAndDisplayFitAllGO (const char* Entry)
245 class TEventFitAll: public SALOME_Event
249 virtual void Execute() {
250 SUIT_Application* app = SUIT_Session::session()->activeApplication();
253 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app))
255 SVTK_View* aView = aViewWindow->getView();
256 aView->GetRenderer()->OnFitAll();
258 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app))
260 Handle(V3d_Viewer) aViewer3d = occViewer->getViewer3d();
261 aViewer3d->InitActiveViews();
263 if (aViewer3d->MoreActiveViews())
264 aViewer3d->ActiveView()->FitAll();
269 createAndDisplayGO(Entry);
270 ProcessVoidEvent(new TEventFitAll());
273 void GEOM_Swig::UpdateViewer()
275 class TEventUpdateViewer: public SALOME_Event
278 TEventUpdateViewer() {}
279 virtual void Execute() {
280 SUIT_Application* app = SUIT_Session::session()->activeApplication();
282 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
283 if (!ActiveStudy) return;
285 GEOM_Displayer(ActiveStudy).UpdateViewer();
289 ProcessVoidEvent(new TEventUpdateViewer());
292 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
294 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
295 if (CORBA::is_nil(aGeomGen))
298 GEOM::GEOM_Object_var aMainShape = aGeomGen->GetIORFromString(IOR);
299 GEOM::GEOM_Object_var aSubShape = aGeomGen->GetIORFromString(SubIOR);
300 if (CORBA::is_nil(aMainShape) || CORBA::is_nil(aSubShape))
303 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
304 aGeomGen->GetIShapesOperations(aMainShape->GetStudyID());
305 if (CORBA::is_nil(anIShapesOperations))
308 return anIShapesOperations->GetTopologyIndex(aMainShape, aSubShape);
311 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
313 TCollection_AsciiString aTypeName ("Shape of unknown type");
315 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
316 if (!CORBA::is_nil(aGeomGen))
318 GEOM::GEOM_Object_var aShape = aGeomGen->GetIORFromString(IOR);
319 if (!CORBA::is_nil(aShape))
321 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
322 aGeomGen->GetIShapesOperations(aShape->GetStudyID());
323 if (!CORBA::is_nil(anIShapesOperations))
325 aTypeName = anIShapesOperations->GetShapeTypeString(aShape);
330 return CORBA::string_dup(aTypeName.ToCString());
334 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
336 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
337 if ( CORBA::is_nil( Geom ) )
340 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
341 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
343 if( shape.IsNull() ) {
347 switch (shape.ShapeType() )
349 case TopAbs_COMPOUND:
350 { return "ICON_OBJBROWSER_COMPOUND" ;}
351 case TopAbs_COMPSOLID:
352 { return "ICON_OBJBROWSER_COMPSOLID" ;}
354 { return "ICON_OBJBROWSER_SOLID" ;}
356 { return "ICON_OBJBROWSER_SHELL" ;}
358 { return "ICON_OBJBROWSER_FACE" ;}
360 { return "ICON_OBJBROWSER_WIRE" ;}
362 { return "ICON_OBJBROWSER_EDGE" ;}
364 { return "ICON_OBJBROWSER_VERTEX" ;}
370 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode, bool isUpdated)
372 class TEvent: public SALOME_Event {
377 TEvent(const char* theEntryArg, int theModeArg, bool theUpdated):
378 myEntry(theEntryArg), myMode(theModeArg), myUpdateViewer(theUpdated)
380 virtual void Execute() {
381 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
384 Handle(SALOME_InteractiveObject) anIO =
385 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
387 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
388 SVTK_View* aView = aViewWindow->getView();
389 aView->SetDisplayMode(anIO, myMode);
393 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
394 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
396 soccViewer->switchRepresentation(anIO, myMode, myUpdateViewer);
401 ProcessVoidEvent(new TEvent (theEntry, theMode, isUpdated));
404 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue, bool isUpdated)
406 class TEvent: public SALOME_Event {
413 TEvent(const char* theEntryArg, int theR, int theG, int theB, bool theUpdated):
414 myEntry(theEntryArg), myRed(theR), myGreen(theG), myBlue(theB), myUpdateViewer(theUpdated)
416 virtual void Execute() {
417 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
420 Handle(SALOME_InteractiveObject) anIO =
421 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
423 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)){
424 SVTK_View* aView = aViewWindow->getView();
425 QColor aColor (myRed, myGreen, myBlue);
426 aView->SetColor(anIO, aColor);
429 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
430 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
431 AIS_ListOfInteractive List;
432 ic->DisplayedObjects(List);
433 AIS_ListIteratorOfListOfInteractive ite (List);
434 for (; ite.More(); ite.Next()) {
435 Handle(SALOME_InteractiveObject) anObj =
436 Handle(SALOME_InteractiveObject)::DownCast(ite.Value()->GetOwner());
437 if (!anObj.IsNull() && anObj->hasEntry() && anObj->isSame(anIO)) {
438 Quantity_Color CSFColor =
439 Quantity_Color(myRed/255., myGreen/255., myBlue/255., Quantity_TOC_RGB);
440 ite.Value()->SetColor(CSFColor);
441 if (ite.Value()->IsKind(STANDARD_TYPE(GEOM_AISShape)))
442 Handle(GEOM_AISShape)::DownCast(ite.Value())->SetShadingColor(CSFColor);
443 ic->Redisplay(ite.Value(), true, true);
452 ProcessVoidEvent(new TEvent(theEntry, red, green, blue, isUpdated));
455 void GEOM_Swig::setTransparency(const char* theEntry, float transp, bool isUpdated)
457 class TEvent: public SALOME_Event {
462 TEvent(const char* theEntryArg, float theParam, bool theUpdated):
463 myEntry(theEntryArg), myParam(theParam), myUpdateViewer(theUpdated)
465 virtual void Execute() {
466 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
469 Handle(SALOME_InteractiveObject) anIO =
470 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
472 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
473 SVTK_View* aView = aViewWindow->getView();
474 aView->SetTransparency(anIO, myParam);
477 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
478 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
480 soccViewer->setTransparency(anIO, myParam, myUpdateViewer);
485 ProcessVoidEvent(new TEvent (theEntry, transp, isUpdated));
489 class TInitGeomGenEvent: public SALOME_Event {
491 typedef bool TResult;
493 TInitGeomGenEvent() : myResult(false) {}
494 virtual void Execute() {
495 myResult = GeometryGUI::InitGeomGen();
498 bool GEOM_Swig::initGeomGen()
500 return ProcessEvent(new TInitGeomGenEvent());
505 void GEOM_Swig::eraseGO (const char* Entry, bool allWindows)
507 class TEvent: public SALOME_Event
510 bool myFromAllWindows;
512 TEvent(const char* theEntry, bool fromAllWindows):
513 myEntry(theEntry), myFromAllWindows(fromAllWindows)
515 virtual void Execute()
517 SUIT_Application* app = SUIT_Session::session()->activeApplication();
519 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
520 if (!ActiveStudy) return;
522 Handle (SALOME_InteractiveObject) aIO = new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
524 GEOM_Displayer(ActiveStudy).Erase(aIO, true);
525 /* if (myFromAllWindows) {
526 QPtrList<SUIT_ViewWindow> aWindows = app->desktop()->windows();
527 SUIT_ViewWindow* aWin = 0;
528 for (aWin = aWindows.first(); aWin; aWin = aWindows.next()) {
529 EraseObject(aWin, aIO);
532 SUIT_ViewWindow* aWin = app->desktop()->activeWindow();
534 EraseObject(aWin, aIO);
539 void EraseObject(SUIT_ViewWindow* theWin, Handle (SALOME_InteractiveObject) theIO)
541 if (theWin->getViewManager()->getType() == OCCViewer_Viewer::Type()){
542 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( theWin );
544 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
546 SOCC_Viewer* aViewer = dynamic_cast<SOCC_Viewer*>(vm->getOCCViewer());
548 SALOME_Prs* aPrs = aViewer->CreatePrs(myEntry.c_str());
550 SALOME_OCCPrs* aOccPrs = dynamic_cast<SALOME_OCCPrs*>(aPrs);
552 aViewer->Erase(aOccPrs);
559 } else if (theWin->getViewManager()->getType() == SVTK_Viewer::Type()){
560 SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>( theWin );
562 aViewWindow->Erase(theIO);
568 ProcessVoidEvent(new TEvent(Entry, allWindows));
573 void GEOM_Swig::setDeflection(const char* theEntry, float theDeflect)
575 class TEvent: public SALOME_Event {
579 TEvent(const char* theEntryArg, float theParam):
580 myEntry(theEntryArg), myParam(theParam)
582 virtual void Execute() {
583 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
586 Handle(SALOME_InteractiveObject) anIO =
587 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
589 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
590 vtkActorCollection* aActors = aViewWindow->getRenderer()->GetActors();
591 aActors->InitTraversal();
592 while (vtkActor* aAct = aActors->GetNextActor()) {
593 if (GEOM_Actor* aGeomActor = dynamic_cast<GEOM_Actor*>(aAct)) {
594 if (aGeomActor->hasIO()) {
595 Handle(SALOME_InteractiveObject) aNextIO = aGeomActor->getIO();
596 if (aNextIO->isSame(anIO)) {
597 aGeomActor->setDeflection(myParam);
598 aViewWindow->Repaint();
604 // aView->SetTransparency(anIO, myParam);
606 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
607 Handle(AIS_InteractiveContext) aContext = occViewer->getAISContext();
608 AIS_ListOfInteractive aAISList;
609 aContext->DisplayedObjects(aAISList);
610 AIS_ListIteratorOfListOfInteractive it(aAISList);
611 for (; it.More(); it.Next()) {
612 Handle(SALOME_InteractiveObject) aObj =
613 Handle(SALOME_InteractiveObject)::DownCast(it.Value()->GetOwner());
614 if ((!aObj.IsNull()) && aObj->hasEntry() && aObj->isSame(anIO)) {
615 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(it.Value());
616 if (!aShape.IsNull()) {
617 TopoDS_Shape aSh = aShape->Shape();
619 BRepTools::Clean(aSh);
621 aShape->SetOwnDeviationCoefficient( myParam );
622 aShape->SetOwnHLRDeviationAngle( 1.57 );
623 aContext->Redisplay(aShape);
632 ProcessVoidEvent(new TEvent (theEntry, theDeflect));