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
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"
48 #include "SVTK_Renderer.h"
50 #include "GEOM_Actor.h"
51 #include "GEOM_Client.hxx"
52 #include "GEOM_AISShape.hxx"
53 #include "GEOM_AssemblyBuilder.h"
54 #include "GEOM_InteractiveObject.hxx"
55 #include "GEOM_Displayer.h"
57 #include "SALOME_Event.hxx"
59 #include "utilities.h"
61 #include "SALOMEDSClient.hxx"
65 #include <TopoDS_Shape.hxx>
66 #include <AIS_ListOfInteractive.hxx>
67 #include <AIS_ListIteratorOfListOfInteractive.hxx>
68 #include <AIS_Drawer.hxx>
69 #include <BRepTools.hxx>
72 #include <SALOMEconfig.h>
73 #include CORBA_SERVER_HEADER(GEOM_Gen)
75 #include <vtkRenderer.h>
80 static GEOM_Client ShapeReader;
82 inline OCCViewer_Viewer* GetOCCViewer(SUIT_Application* theApp){
83 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
84 if(window && window->getViewManager()->getType() == OCCViewer_Viewer::Type()){
85 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( window );
87 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
89 return vm->getOCCViewer();
96 inline SVTK_ViewWindow* GetSVTKViewWindow(SUIT_Application* theApp){
97 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
98 if(window && window->getViewManager()->getType() == SVTK_Viewer::Type())
99 return dynamic_cast<SVTK_ViewWindow*>( window );
104 GEOM_Swig::GEOM_Swig()
106 // MESSAGE("Constructeur");
109 GEOM_Swig::~GEOM_Swig()
111 // MESSAGE("Destructeur");
114 void GEOM_Swig::createAndDisplayGO (const char* Entry, bool isUpdated)
116 class TEvent: public SALOME_Event
121 TEvent(const char* theEntry, bool toUpdateViewer):
123 myUpdateViewer(toUpdateViewer)
125 virtual void Execute()
127 SUIT_Application* app = SUIT_Session::session()->activeApplication();
130 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
131 if (!ActiveStudy) return;
133 _PTR(Study) aStudy(ActiveStudy->studyDS());
134 _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
136 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
137 if (CORBA::is_nil(Geom)) {
138 GeometryGUI::InitGeomGen();
139 Geom = GeometryGUI::GetGeomGen();
141 if (CORBA::is_nil(Geom))
145 _PTR(SComponent) father = aStudy->FindComponent("GEOM");
148 if (!father->ComponentIOR(aFatherIOR)) {
149 CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string(Geom);
150 aStudyBuilder->LoadWith(father, objStr.in());
151 father->ComponentIOR(aFatherIOR);
154 _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
159 _PTR(GenericAttribute) anAttr;
160 if (!obj->FindAttribute(anAttr, "AttributeIOR"))
162 _PTR(AttributeIOR) anIOR(anAttr);
163 string anIORValue = anIOR->Value();
165 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
166 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
167 if (!Shape.IsNull()) {
168 if (obj->FindAttribute(anAttr, "AttributeName")) {
169 _PTR(AttributeName) aName (anAttr);
170 string aNameValue = aName->Value();
172 /*SUIT_Operation* op = new SalomeApp_ImportOperation (app);
175 _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
176 aStudyBuilder->Addreference(newObj1, obj);
177 // commit transaction
179 Handle(GEOM_InteractiveObject) anIO =
180 new GEOM_InteractiveObject (const_cast<char*>(anIORValue.c_str()),
181 const_cast<char*>(aFatherIOR.c_str()),
183 const_cast<char*>( obj->GetID().c_str()));
185 GEOM_Displayer(ActiveStudy).Display(anIO, myUpdateViewer);
186 /*if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app)) {
187 SVTK_View* aView = aViewWindow->getView();
188 int aMode = aView->GetDisplayMode();
190 vtkActorCollection* theActors =
191 GEOM_AssemblyBuilder::BuildActors(Shape,0,aMode,true);
192 theActors->InitTraversal();
193 while (vtkActor* anActor = theActors->GetNextActor()) {
194 GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
195 GActor->setName(const_cast<char*>(aNameValue.c_str()));
197 aView->Display(GActor);
200 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app)) {
201 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
202 Handle(GEOM_AISShape) aSh =
203 new GEOM_AISShape (Shape,const_cast<char*>(aNameValue.c_str()));
204 aSh->setName(const_cast<char*>(aNameValue.c_str()));
207 ic->AddOrRemoveCurrentObject(aSh,true);
209 // update object browser
210 SalomeApp_Application* app = NULL; //dynamic_cast<SalomeApp_Application*>(app);
212 CAM_Module* module = app->module("Geometry");
213 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
215 appMod->updateObjBrowser(true);
222 // MESSAGE("createAndDisplayGO");
223 ProcessVoidEvent(new TEvent (Entry, isUpdated));
225 class TEventUpdateBrowser: public SALOME_Event
228 TEventUpdateBrowser() {}
229 virtual void Execute() {
230 SalomeApp_Application* app =
231 dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
233 CAM_Module* module = app->module("Geometry");
234 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
235 if (appMod) appMod->updateObjBrowser(true);
241 ProcessVoidEvent(new TEventUpdateBrowser ());
244 void GEOM_Swig::createAndDisplayFitAllGO (const char* Entry)
246 class TEventFitAll: public SALOME_Event
250 virtual void Execute() {
251 SUIT_Application* app = SUIT_Session::session()->activeApplication();
254 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app))
256 SVTK_View* aView = aViewWindow->getView();
257 aView->GetRenderer()->OnFitAll();
259 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app))
261 Handle(V3d_Viewer) aViewer3d = occViewer->getViewer3d();
262 aViewer3d->InitActiveViews();
264 if (aViewer3d->MoreActiveViews())
265 aViewer3d->ActiveView()->FitAll();
270 createAndDisplayGO(Entry);
271 ProcessVoidEvent(new TEventFitAll());
274 void GEOM_Swig::UpdateViewer()
276 class TEventUpdateViewer: public SALOME_Event
279 TEventUpdateViewer() {}
280 virtual void Execute() {
281 SUIT_Application* app = SUIT_Session::session()->activeApplication();
283 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
284 if (!ActiveStudy) return;
286 GEOM_Displayer(ActiveStudy).UpdateViewer();
290 ProcessVoidEvent(new TEventUpdateViewer());
293 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
295 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
296 if (CORBA::is_nil(aGeomGen))
299 GEOM::GEOM_Object_var aMainShape = aGeomGen->GetIORFromString(IOR);
300 GEOM::GEOM_Object_var aSubShape = aGeomGen->GetIORFromString(SubIOR);
301 if (CORBA::is_nil(aMainShape) || CORBA::is_nil(aSubShape))
304 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
305 aGeomGen->GetIShapesOperations(aMainShape->GetStudyID());
306 if (CORBA::is_nil(anIShapesOperations))
309 return anIShapesOperations->GetTopologyIndex(aMainShape, aSubShape);
312 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
314 TCollection_AsciiString aTypeName ("Shape of unknown type");
316 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
317 if (!CORBA::is_nil(aGeomGen))
319 GEOM::GEOM_Object_var aShape = aGeomGen->GetIORFromString(IOR);
320 if (!CORBA::is_nil(aShape))
322 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
323 aGeomGen->GetIShapesOperations(aShape->GetStudyID());
324 if (!CORBA::is_nil(anIShapesOperations))
326 aTypeName = anIShapesOperations->GetShapeTypeString(aShape);
331 return CORBA::string_dup(aTypeName.ToCString());
335 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
337 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
338 if ( CORBA::is_nil( Geom ) )
341 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
342 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
344 if( shape.IsNull() ) {
348 switch (shape.ShapeType() )
350 case TopAbs_COMPOUND:
351 { return "ICON_OBJBROWSER_COMPOUND" ;}
352 case TopAbs_COMPSOLID:
353 { return "ICON_OBJBROWSER_COMPSOLID" ;}
355 { return "ICON_OBJBROWSER_SOLID" ;}
357 { return "ICON_OBJBROWSER_SHELL" ;}
359 { return "ICON_OBJBROWSER_FACE" ;}
361 { return "ICON_OBJBROWSER_WIRE" ;}
363 { return "ICON_OBJBROWSER_EDGE" ;}
365 { return "ICON_OBJBROWSER_VERTEX" ;}
371 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode, bool isUpdated)
373 class TEvent: public SALOME_Event {
378 TEvent(const char* theEntryArg, int theModeArg, bool theUpdated):
379 myEntry(theEntryArg), myMode(theModeArg), myUpdateViewer(theUpdated)
381 virtual void Execute() {
382 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
385 Handle(SALOME_InteractiveObject) anIO =
386 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
388 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
389 SVTK_View* aView = aViewWindow->getView();
390 aView->SetDisplayMode(anIO, myMode);
394 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
395 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
397 soccViewer->switchRepresentation(anIO, myMode, myUpdateViewer);
402 ProcessVoidEvent(new TEvent (theEntry, theMode, isUpdated));
405 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue, bool isUpdated)
407 class TEvent: public SALOME_Event {
414 TEvent(const char* theEntryArg, int theR, int theG, int theB, bool theUpdated):
415 myEntry(theEntryArg), myRed(theR), myGreen(theG), myBlue(theB), myUpdateViewer(theUpdated)
417 virtual void Execute() {
418 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
421 Handle(SALOME_InteractiveObject) anIO =
422 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
424 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)){
425 SVTK_View* aView = aViewWindow->getView();
426 QColor aColor (myRed, myGreen, myBlue);
427 aView->SetColor(anIO, aColor);
430 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
431 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
432 AIS_ListOfInteractive List;
433 ic->DisplayedObjects(List);
434 AIS_ListIteratorOfListOfInteractive ite (List);
435 for (; ite.More(); ite.Next()) {
436 Handle(SALOME_InteractiveObject) anObj =
437 Handle(SALOME_InteractiveObject)::DownCast(ite.Value()->GetOwner());
438 if (!anObj.IsNull() && anObj->hasEntry() && anObj->isSame(anIO)) {
439 Quantity_Color CSFColor =
440 Quantity_Color(myRed/255., myGreen/255., myBlue/255., Quantity_TOC_RGB);
441 ite.Value()->SetColor(CSFColor);
442 if (ite.Value()->IsKind(STANDARD_TYPE(GEOM_AISShape)))
443 Handle(GEOM_AISShape)::DownCast(ite.Value())->SetShadingColor(CSFColor);
444 ic->Redisplay(ite.Value(), true, true);
453 ProcessVoidEvent(new TEvent(theEntry, red, green, blue, isUpdated));
456 void GEOM_Swig::setTransparency(const char* theEntry, float transp, bool isUpdated)
458 class TEvent: public SALOME_Event {
463 TEvent(const char* theEntryArg, float theParam, bool theUpdated):
464 myEntry(theEntryArg), myParam(theParam), myUpdateViewer(theUpdated)
466 virtual void Execute() {
467 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
470 Handle(SALOME_InteractiveObject) anIO =
471 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
473 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
474 SVTK_View* aView = aViewWindow->getView();
475 aView->SetTransparency(anIO, myParam);
478 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
479 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
481 soccViewer->setTransparency(anIO, myParam, myUpdateViewer);
486 ProcessVoidEvent(new TEvent (theEntry, transp, isUpdated));
490 class TInitGeomGenEvent: public SALOME_Event {
492 typedef bool TResult;
494 TInitGeomGenEvent() : myResult(false) {}
495 virtual void Execute() {
496 myResult = GeometryGUI::InitGeomGen();
499 bool GEOM_Swig::initGeomGen()
501 return ProcessEvent(new TInitGeomGenEvent());
506 void GEOM_Swig::eraseGO (const char* Entry, bool allWindows)
508 class TEvent: public SALOME_Event
511 bool myFromAllWindows;
513 TEvent(const char* theEntry, bool fromAllWindows):
514 myEntry(theEntry), myFromAllWindows(fromAllWindows)
516 virtual void Execute()
518 SUIT_Application* app = SUIT_Session::session()->activeApplication();
520 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
521 if (!ActiveStudy) return;
523 Handle (SALOME_InteractiveObject) aIO = new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
525 GEOM_Displayer(ActiveStudy).Erase(aIO, true);
526 /* if (myFromAllWindows) {
527 QPtrList<SUIT_ViewWindow> aWindows = app->desktop()->windows();
528 SUIT_ViewWindow* aWin = 0;
529 for (aWin = aWindows.first(); aWin; aWin = aWindows.next()) {
530 EraseObject(aWin, aIO);
533 SUIT_ViewWindow* aWin = app->desktop()->activeWindow();
535 EraseObject(aWin, aIO);
540 void EraseObject(SUIT_ViewWindow* theWin, Handle (SALOME_InteractiveObject) theIO)
542 if (theWin->getViewManager()->getType() == OCCViewer_Viewer::Type()){
543 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( theWin );
545 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
547 SOCC_Viewer* aViewer = dynamic_cast<SOCC_Viewer*>(vm->getOCCViewer());
549 SALOME_Prs* aPrs = aViewer->CreatePrs(myEntry.c_str());
551 SALOME_OCCPrs* aOccPrs = dynamic_cast<SALOME_OCCPrs*>(aPrs);
553 aViewer->Erase(aOccPrs);
560 } else if (theWin->getViewManager()->getType() == SVTK_Viewer::Type()){
561 SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>( theWin );
563 aViewWindow->Erase(theIO);
569 ProcessVoidEvent(new TEvent(Entry, allWindows));
574 void GEOM_Swig::setDeflection(const char* theEntry, float theDeflect)
576 class TEvent: public SALOME_Event {
580 TEvent(const char* theEntryArg, float theParam):
581 myEntry(theEntryArg), myParam(theParam)
583 virtual void Execute() {
584 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
587 Handle(SALOME_InteractiveObject) anIO =
588 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
590 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
591 vtkActorCollection* aActors = aViewWindow->getRenderer()->GetActors();
592 aActors->InitTraversal();
593 while (vtkActor* aAct = aActors->GetNextActor()) {
594 if (GEOM_Actor* aGeomActor = dynamic_cast<GEOM_Actor*>(aAct)) {
595 if (aGeomActor->hasIO()) {
596 Handle(SALOME_InteractiveObject) aNextIO = aGeomActor->getIO();
597 if (aNextIO->isSame(anIO)) {
598 aGeomActor->setDeflection(myParam);
599 aViewWindow->Repaint();
605 // aView->SetTransparency(anIO, myParam);
607 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
608 Handle(AIS_InteractiveContext) aContext = occViewer->getAISContext();
609 AIS_ListOfInteractive aAISList;
610 aContext->DisplayedObjects(aAISList);
611 AIS_ListIteratorOfListOfInteractive it(aAISList);
612 for (; it.More(); it.Next()) {
613 Handle(SALOME_InteractiveObject) aObj =
614 Handle(SALOME_InteractiveObject)::DownCast(it.Value()->GetOwner());
615 if ((!aObj.IsNull()) && aObj->hasEntry() && aObj->isSame(anIO)) {
616 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(it.Value());
617 if (!aShape.IsNull()) {
618 TopoDS_Shape aSh = aShape->Shape();
620 BRepTools::Clean(aSh);
622 aShape->SetOwnDeviationCoefficient( myParam );
623 aShape->SetOwnHLRDeviationAngle( 1.57 );
624 aContext->Redisplay(aShape);
633 ProcessVoidEvent(new TEvent (theEntry, theDeflect));