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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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"
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>
69 #include <SALOMEconfig.h>
70 #include CORBA_SERVER_HEADER(GEOM_Gen)
75 static GEOM_Client ShapeReader;
77 inline OCCViewer_Viewer* GetOCCViewer(SUIT_Application* theApp){
78 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
79 if(window && window->getViewManager()->getType() == OCCViewer_Viewer::Type()){
80 OCCViewer_ViewWindow* vw = dynamic_cast<OCCViewer_ViewWindow*>( window );
82 OCCViewer_ViewManager* vm = dynamic_cast<OCCViewer_ViewManager*>( vw->getViewManager() );
84 return vm->getOCCViewer();
91 inline SVTK_ViewWindow* GetSVTKViewWindow(SUIT_Application* theApp){
92 SUIT_ViewWindow* window = theApp->desktop()->activeWindow();
93 if(window && window->getViewManager()->getType() == SVTK_Viewer::Type())
94 return dynamic_cast<SVTK_ViewWindow*>( window );
99 GEOM_Swig::GEOM_Swig()
101 // MESSAGE("Constructeur");
104 GEOM_Swig::~GEOM_Swig()
106 // MESSAGE("Destructeur");
109 void GEOM_Swig::createAndDisplayGO (const char* Entry)
111 class TEvent: public SALOME_Event
115 TEvent(const char* theEntry):
118 virtual void Execute()
120 SUIT_Application* app = SUIT_Session::session()->activeApplication();
123 SalomeApp_Study* ActiveStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
124 if (!ActiveStudy) return;
126 _PTR(Study) aStudy(ActiveStudy->studyDS());
127 _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
129 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
130 if (CORBA::is_nil(Geom)) {
131 GeometryGUI::InitGeomGen();
132 Geom = GeometryGUI::GetGeomGen();
134 if (CORBA::is_nil(Geom))
138 _PTR(SComponent) father = aStudy->FindComponent("GEOM");
141 if (!father->ComponentIOR(aFatherIOR)) {
142 aStudyBuilder->LoadWith(father, SalomeApp_Application::orb()->object_to_string(Geom));
143 father->ComponentIOR(aFatherIOR);
146 _PTR(SObject) obj = aStudy->FindObjectID(myEntry);
151 _PTR(GenericAttribute) anAttr;
152 if (!obj->FindAttribute(anAttr, "AttributeIOR"))
154 _PTR(AttributeIOR) anIOR(anAttr);
155 string anIORValue = anIOR->Value();
157 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue.c_str());
158 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
159 if (!Shape.IsNull()) {
160 if (obj->FindAttribute(anAttr, "AttributeName")) {
161 _PTR(AttributeName) aName (anAttr);
162 string aNameValue = aName->Value();
164 /*SUIT_Operation* op = new SalomeApp_ImportOperation (app);
167 _PTR(SObject) newObj1 = aStudyBuilder->NewObject(father);
168 aStudyBuilder->Addreference(newObj1, obj);
169 // commit transaction
171 Handle(GEOM_InteractiveObject) anIO =
172 new GEOM_InteractiveObject (const_cast<char*>(anIORValue.c_str()),
173 const_cast<char*>(aFatherIOR.c_str()),
175 const_cast<char*>( obj->GetID().c_str()));
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));
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);
231 ProcessVoidEvent(new TEventUpdateBrowser ());
234 void GEOM_Swig::createAndDisplayFitAllGO (const char* Entry)
236 class TEventFitAll: public SALOME_Event
240 virtual void Execute() {
241 SUIT_Application* app = SUIT_Session::session()->activeApplication();
244 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(app))
246 SVTK_View* aView = aViewWindow->getView();
247 aView->GetRenderer()->OnFitAll();
249 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(app))
251 Handle(V3d_Viewer) aViewer3d = occViewer->getViewer3d();
252 aViewer3d->InitActiveViews();
254 if (aViewer3d->MoreActiveViews())
255 aViewer3d->ActiveView()->FitAll();
260 createAndDisplayGO(Entry);
261 ProcessVoidEvent(new TEventFitAll());
264 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
266 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
267 if (CORBA::is_nil(aGeomGen))
270 GEOM::GEOM_Object_var aMainShape = aGeomGen->GetIORFromString(IOR);
271 GEOM::GEOM_Object_var aSubShape = aGeomGen->GetIORFromString(SubIOR);
272 if (CORBA::is_nil(aMainShape) || CORBA::is_nil(aSubShape))
275 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
276 aGeomGen->GetIShapesOperations(aMainShape->GetStudyID());
277 if (CORBA::is_nil(anIShapesOperations))
280 return anIShapesOperations->GetTopologyIndex(aMainShape, aSubShape);
283 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
285 TCollection_AsciiString aTypeName ("Shape of unknown type");
287 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
288 if (!CORBA::is_nil(aGeomGen))
290 GEOM::GEOM_Object_var aShape = aGeomGen->GetIORFromString(IOR);
291 if (!CORBA::is_nil(aShape))
293 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
294 aGeomGen->GetIShapesOperations(aShape->GetStudyID());
295 if (!CORBA::is_nil(anIShapesOperations))
297 aTypeName = anIShapesOperations->GetShapeTypeString(aShape);
302 return CORBA::string_dup(aTypeName.ToCString());
306 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
308 GEOM::GEOM_Gen_var Geom = GeometryGUI::GetGeomGen();
309 if ( CORBA::is_nil( Geom ) )
312 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
313 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
315 if( shape.IsNull() ) {
319 switch (shape.ShapeType() )
321 case TopAbs_COMPOUND:
322 { return "ICON_OBJBROWSER_COMPOUND" ;}
323 case TopAbs_COMPSOLID:
324 { return "ICON_OBJBROWSER_COMPSOLID" ;}
326 { return "ICON_OBJBROWSER_SOLID" ;}
328 { return "ICON_OBJBROWSER_SHELL" ;}
330 { return "ICON_OBJBROWSER_FACE" ;}
332 { return "ICON_OBJBROWSER_WIRE" ;}
334 { return "ICON_OBJBROWSER_EDGE" ;}
336 { return "ICON_OBJBROWSER_VERTEX" ;}
342 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
344 class TEvent: public SALOME_Event {
348 TEvent(const char* theEntryArg, int theModeArg):
349 myEntry(theEntryArg), myMode(theModeArg)
351 virtual void Execute() {
352 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
355 Handle(SALOME_InteractiveObject) anIO =
356 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
358 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
359 SVTK_View* aView = aViewWindow->getView();
360 aView->SetDisplayMode(anIO, myMode);
363 else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
364 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
366 soccViewer->switchRepresentation(anIO, myMode);
371 ProcessVoidEvent(new TEvent (theEntry, theMode));
374 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
376 class TEvent: public SALOME_Event {
382 TEvent(const char* theEntryArg, int theR, int theG, int theB):
383 myEntry(theEntryArg), myRed(theR), myGreen(theG), myBlue(theB)
385 virtual void Execute() {
386 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
389 Handle(SALOME_InteractiveObject) anIO =
390 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
392 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)){
393 SVTK_View* aView = aViewWindow->getView();
394 QColor aColor (myRed, myGreen, myBlue);
395 aView->SetColor(anIO, aColor);
397 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
398 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
399 AIS_ListOfInteractive List;
400 ic->DisplayedObjects(List);
401 AIS_ListIteratorOfListOfInteractive ite (List);
402 for (; ite.More(); ite.Next()) {
403 Handle(SALOME_InteractiveObject) anObj =
404 Handle(SALOME_InteractiveObject)::DownCast(ite.Value()->GetOwner());
405 if (!anObj.IsNull() && anObj->hasEntry() && anObj->isSame(anIO)) {
406 Quantity_Color CSFColor =
407 Quantity_Color(myRed/255., myGreen/255., myBlue/255., Quantity_TOC_RGB);
408 ite.Value()->SetColor(CSFColor);
409 if (ite.Value()->IsKind(STANDARD_TYPE(GEOM_AISShape)))
410 Handle(GEOM_AISShape)::DownCast(ite.Value())->SetShadingColor(CSFColor);
411 ite.Value()->Redisplay(Standard_True);
419 ProcessVoidEvent(new TEvent(theEntry, red, green, blue));
422 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
424 class TEvent: public SALOME_Event {
428 TEvent(const char* theEntryArg, float theParam):
429 myEntry(theEntryArg), myParam(theParam)
431 virtual void Execute() {
432 SUIT_Application* anApp = SUIT_Session::session()->activeApplication();
435 Handle(SALOME_InteractiveObject) anIO =
436 new SALOME_InteractiveObject(myEntry.c_str(), "GEOM", "");
438 if (SVTK_ViewWindow* aViewWindow = GetSVTKViewWindow(anApp)) {
439 SVTK_View* aView = aViewWindow->getView();
440 aView->SetTransparency(anIO, myParam);
442 } else if (OCCViewer_Viewer* occViewer = GetOCCViewer(anApp)) {
443 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
445 soccViewer->setTransparency(anIO, myParam);
450 ProcessVoidEvent(new TEvent (theEntry, transp));
454 class TInitGeomGenEvent: public SALOME_Event {
456 typedef bool TResult;
458 TInitGeomGenEvent() : myResult(false) {}
459 virtual void Execute() {
460 myResult = GeometryGUI::InitGeomGen();
463 bool GEOM_Swig::initGeomGen()
465 return ProcessEvent(new TInitGeomGenEvent());