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>
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 aStudyBuilder->LoadWith(father, SalomeApp_Application::orb()->object_to_string(Geom));
149 father->ComponentIOR(aFatherIOR);
152 _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
157 _PTR(GenericAttribute) anAttr;
158 if (!obj->FindAttribute(anAttr, "AttributeIOR"))
160 _PTR(AttributeIOR) anIOR(anAttr);
161 string anIORValue = anIOR->Value();
163 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
164 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
165 if (!Shape.IsNull()) {
166 if (obj->FindAttribute(anAttr, "AttributeName")) {
167 _PTR(AttributeName) aName (anAttr);
168 string aNameValue = aName->Value();
170 /*SUIT_Operation* op = new SalomeApp_ImportOperation (app);
173 _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
174 aStudyBuilder->Addreference(newObj1, obj);
175 // commit transaction
177 Handle(GEOM_InteractiveObject) anIO =
178 new GEOM_InteractiveObject (const_cast<char*>(anIORValue.c_str()),
179 const_cast<char*>(aFatherIOR.c_str()),
181 const_cast<char*>( obj->GetID().c_str()));
183 GEOM_Displayer(ActiveStudy).Display(anIO, myUpdateViewer);
184 /*if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app)) {
185 SVTK_View* aView = aViewWindow->getView();
186 int aMode = aView->GetDisplayMode();
188 vtkActorCollection* theActors =
189 GEOM_AssemblyBuilder::BuildActors(Shape,0,aMode,true);
190 theActors->InitTraversal();
191 while (vtkActor* anActor = theActors->GetNextActor()) {
192 GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
193 GActor->setName(const_cast<char*>(aNameValue.c_str()));
195 aView->Display(GActor);
198 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app)) {
199 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
200 Handle(GEOM_AISShape) aSh =
201 new GEOM_AISShape (Shape,const_cast<char*>(aNameValue.c_str()));
202 aSh->setName(const_cast<char*>(aNameValue.c_str()));
205 ic->AddOrRemoveCurrentObject(aSh,true);
207 // update object browser
208 SalomeApp_Application* app = NULL; //dynamic_cast<SalomeApp_Application*>(app);
210 CAM_Module* module = app->module("Geometry");
211 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
213 appMod->updateObjBrowser(true);
220 // MESSAGE("createAndDisplayGO");
221 ProcessVoidEvent(new TEvent (Entry, isUpdated));
223 class TEventUpdateBrowser: public SALOME_Event
226 TEventUpdateBrowser() {}
227 virtual void Execute() {
228 SalomeApp_Application* app =
229 dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
231 CAM_Module* module = app->module("Geometry");
232 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>(module);
233 if (appMod) appMod->updateObjBrowser(true);
239 ProcessVoidEvent(new TEventUpdateBrowser ());
242 void GEOM_Swig::createAndDisplayFitAllGO (const char* Entry)
244 class TEventFitAll: public SALOME_Event
248 virtual void Execute() {
249 SUIT_Application* app = SUIT_Session::session()->activeApplication();
252 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app))
254 SVTK_View* aView = aViewWindow->getView();
255 aView->GetRenderer()->OnFitAll();
257 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app))
259 Handle(V3d_Viewer) aViewer3d = occViewer->getViewer3d();
260 aViewer3d->InitActiveViews();
262 if (aViewer3d->MoreActiveViews())
263 aViewer3d->ActiveView()->FitAll();
268 createAndDisplayGO(Entry);
269 ProcessVoidEvent(new TEventFitAll());
272 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
274 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
275 if (CORBA::is_nil(aGeomGen))
278 GEOM::GEOM_Object_var aMainShape = aGeomGen->GetIORFromString(IOR);
279 GEOM::GEOM_Object_var aSubShape = aGeomGen->GetIORFromString(SubIOR);
280 if (CORBA::is_nil(aMainShape) || CORBA::is_nil(aSubShape))
283 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
284 aGeomGen->GetIShapesOperations(aMainShape->GetStudyID());
285 if (CORBA::is_nil(anIShapesOperations))
288 return anIShapesOperations->GetTopologyIndex(aMainShape, aSubShape);
291 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
293 TCollection_AsciiString aTypeName ("Shape of unknown type");
295 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
296 if (!CORBA::is_nil(aGeomGen))
298 GEOM::GEOM_Object_var aShape = aGeomGen->GetIORFromString(IOR);
299 if (!CORBA::is_nil(aShape))
301 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
302 aGeomGen->GetIShapesOperations(aShape->GetStudyID());
303 if (!CORBA::is_nil(anIShapesOperations))
305 aTypeName = anIShapesOperations->GetShapeTypeString(aShape);
310 return CORBA::string_dup(aTypeName.ToCString());
314 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
316 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
317 if ( CORBA::is_nil( Geom ) )
320 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
321 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
323 if( shape.IsNull() ) {
327 switch (shape.ShapeType() )
329 case TopAbs_COMPOUND:
330 { return "ICON_OBJBROWSER_COMPOUND" ;}
331 case TopAbs_COMPSOLID:
332 { return "ICON_OBJBROWSER_COMPSOLID" ;}
334 { return "ICON_OBJBROWSER_SOLID" ;}
336 { return "ICON_OBJBROWSER_SHELL" ;}
338 { return "ICON_OBJBROWSER_FACE" ;}
340 { return "ICON_OBJBROWSER_WIRE" ;}
342 { return "ICON_OBJBROWSER_EDGE" ;}
344 { return "ICON_OBJBROWSER_VERTEX" ;}
350 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode, bool isUpdated)
352 class TEvent: public SALOME_Event {
357 TEvent(const char* theEntryArg, int theModeArg, bool theUpdated):
358 myEntry(theEntryArg), myMode(theModeArg), myUpdateViewer(theUpdated)
360 virtual void Execute() {
361 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
364 Handle(SALOME_InteractiveObject) anIO =
365 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
367 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
368 SVTK_View* aView = aViewWindow->getView();
369 aView->SetDisplayMode(anIO, myMode);
373 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
374 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
376 soccViewer->switchRepresentation(anIO, myMode, myUpdateViewer);
381 ProcessVoidEvent(new TEvent (theEntry, theMode, isUpdated));
384 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue, bool isUpdated)
386 class TEvent: public SALOME_Event {
393 TEvent(const char* theEntryArg, int theR, int theG, int theB, bool theUpdated):
394 myEntry(theEntryArg), myRed(theR), myGreen(theG), myBlue(theB), myUpdateViewer(theUpdated)
396 virtual void Execute() {
397 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
400 Handle(SALOME_InteractiveObject) anIO =
401 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
403 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)){
404 SVTK_View* aView = aViewWindow->getView();
405 QColor aColor (myRed, myGreen, myBlue);
406 aView->SetColor(anIO, aColor);
409 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
410 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
411 AIS_ListOfInteractive List;
412 ic->DisplayedObjects(List);
413 AIS_ListIteratorOfListOfInteractive ite (List);
414 for (; ite.More(); ite.Next()) {
415 Handle(SALOME_InteractiveObject) anObj =
416 Handle(SALOME_InteractiveObject)::DownCast(ite.Value()->GetOwner());
417 if (!anObj.IsNull() && anObj->hasEntry() && anObj->isSame(anIO)) {
418 Quantity_Color CSFColor =
419 Quantity_Color(myRed/255., myGreen/255., myBlue/255., Quantity_TOC_RGB);
420 ite.Value()->SetColor(CSFColor);
421 if (ite.Value()->IsKind(STANDARD_TYPE(GEOM_AISShape)))
422 Handle(GEOM_AISShape)::DownCast(ite.Value())->SetShadingColor(CSFColor);
423 ite.Value()->Redisplay(Standard_True);
432 ProcessVoidEvent(new TEvent(theEntry, red, green, blue, isUpdated));
435 void GEOM_Swig::setTransparency(const char* theEntry, float transp, bool isUpdated)
437 class TEvent: public SALOME_Event {
442 TEvent(const char* theEntryArg, float theParam, bool theUpdated):
443 myEntry(theEntryArg), myParam(theParam), myUpdateViewer(theUpdated)
445 virtual void Execute() {
446 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
449 Handle(SALOME_InteractiveObject) anIO =
450 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
452 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
453 SVTK_View* aView = aViewWindow->getView();
454 aView->SetTransparency(anIO, myParam);
457 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
458 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
460 soccViewer->setTransparency(anIO, myParam, myUpdateViewer);
465 ProcessVoidEvent(new TEvent (theEntry, transp, isUpdated));
469 class TInitGeomGenEvent: public SALOME_Event {
471 typedef bool TResult;
473 TInitGeomGenEvent() : myResult(false) {}
474 virtual void Execute() {
475 myResult = GeometryGUI::InitGeomGen();
478 bool GEOM_Swig::initGeomGen()
480 return ProcessEvent(new TInitGeomGenEvent());
485 void GEOM_Swig::eraseGO (const char* Entry, bool allWindows)
487 class TEvent: public SALOME_Event
490 bool myFromAllWindows;
492 TEvent(const char* theEntry, bool fromAllWindows):
493 myEntry(theEntry), myFromAllWindows(fromAllWindows)
495 virtual void Execute()
497 SUIT_Application* app = SUIT_Session::session()->activeApplication();
499 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
500 if (!ActiveStudy) return;
502 Handle (SALOME_InteractiveObject) aIO = new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
504 GEOM_Displayer(ActiveStudy).Erase(aIO, true);
505 /* if (myFromAllWindows) {
506 QPtrList<SUIT_ViewWindow> aWindows = app->desktop()->windows();
507 SUIT_ViewWindow* aWin = 0;
508 for (aWin = aWindows.first(); aWin; aWin = aWindows.next()) {
509 EraseObject(aWin, aIO);
512 SUIT_ViewWindow* aWin = app->desktop()->activeWindow();
514 EraseObject(aWin, aIO);
519 void EraseObject(SUIT_ViewWindow* theWin, Handle (SALOME_InteractiveObject) theIO)
521 if (theWin->getViewManager()->getType() == OCCViewer_Viewer::Type()){
522 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( theWin );
524 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
526 SOCC_Viewer* aViewer = dynamic_cast<SOCC_Viewer*>(vm->getOCCViewer());
528 SALOME_Prs* aPrs = aViewer->CreatePrs(myEntry.c_str());
530 SALOME_OCCPrs* aOccPrs = dynamic_cast<SALOME_OCCPrs*>(aPrs);
532 aViewer->Erase(aOccPrs);
539 } else if (theWin->getViewManager()->getType() == SVTK_Viewer::Type()){
540 SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>( theWin );
542 aViewWindow->Erase(theIO);
548 ProcessVoidEvent(new TEvent(Entry, allWindows));
553 void GEOM_Swig::setDeflection(const char* theEntry, float theDeflect)
555 class TEvent: public SALOME_Event {
559 TEvent(const char* theEntryArg, float theParam):
560 myEntry(theEntryArg), myParam(theParam)
562 virtual void Execute() {
563 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
566 Handle(SALOME_InteractiveObject) anIO =
567 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
569 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
570 vtkActorCollection* aActors = aViewWindow->getRenderer()->GetActors();
571 aActors->InitTraversal();
572 while (vtkActor* aAct = aActors->GetNextActor()) {
573 if (GEOM_Actor* aGeomActor = dynamic_cast<GEOM_Actor*>(aAct)) {
574 if (aGeomActor->hasIO()) {
575 Handle(SALOME_InteractiveObject) aNextIO = aGeomActor->getIO();
576 if (aNextIO->isSame(anIO)) {
577 aGeomActor->setDeflection(myParam);
578 aViewWindow->Repaint();
584 // aView->SetTransparency(anIO, myParam);
586 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
587 Handle(AIS_InteractiveContext) aContext = occViewer->getAISContext();
588 AIS_ListOfInteractive aAISList;
589 aContext->DisplayedObjects(aAISList);
590 AIS_ListIteratorOfListOfInteractive it(aAISList);
591 for (; it.More(); it.Next()) {
592 Handle(SALOME_InteractiveObject) aObj =
593 Handle(SALOME_InteractiveObject)::DownCast(it.Value()->GetOwner());
594 if ((!aObj.IsNull()) && aObj->hasEntry() && aObj->isSame(anIO)) {
595 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(it.Value());
596 if (!aShape.IsNull()) {
597 Handle(AIS_Drawer) aDrawer = aShape->Attributes();
598 if (aDrawer.IsNull())
599 aDrawer = new AIS_Drawer();
600 aDrawer->SetDeviationCoefficient(myParam);
601 aShape->SetAttributes(aDrawer);
602 aContext->Redisplay(aShape, true, true);
603 aContext->UpdateCurrentViewer();
612 ProcessVoidEvent(new TEvent (theEntry, theDeflect));