Salome HOME
Merge with OCC-V2_1_0_deb
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_1.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2004  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.salome-platform.org or email : webmaster.salome@opencascade.org 
21 //
22 //            
23 //
24 //  File   : GEOMToolsGUI_1.cxx
25 //  Author : Sergey ANIKIN
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GEOMToolsGUI.h"
30 #include "GeometryGUI.h"
31 #include "GEOM_Actor.h"
32 #include "GEOMBase.h"
33
34 #include "QAD_Desktop.h"
35 #include "QAD_Config.h"
36 #include "QAD_FileDlg.h"
37 #include "QAD_PyEditor.h"
38 #include "QAD_RightFrame.h"
39 #include "QAD_MessageBox.h"
40 #include "SALOMEGUI_NameDlg.h"
41
42 #include "OCCViewer_Viewer3d.h"
43 #include "OCCViewer_ViewFrame.h"
44 #include "VTKViewer_ViewFrame.h"
45 #include "VTKViewer_RenderWindowInteractor.h"
46
47 #include "SALOME_ListIO.hxx"
48 #include "SALOME_ListIteratorOfListIO.hxx"
49
50 #include <AIS_Drawer.hxx>
51 #include <AIS_ListOfInteractive.hxx>
52 #include <AIS_ListIteratorOfListOfInteractive.hxx>
53 #include <Prs3d_IsoAspect.hxx>
54
55 #include <vtkBMPReader.h>
56 #include <vtkTexture.h>
57 #include <vtkTextureMapToPlane.h>
58 #include <vtkTransformTextureCoords.h>
59 #include <vtkDataSetMapper.h>
60 #include <vtkRenderer.h>
61
62 #include <qfileinfo.h>
63 #include <qcolordialog.h>
64 #include <qspinbox.h>
65 #include <qapplication.h>
66
67 #include "GEOMToolsGUI_NbIsosDlg.h"        // Method ISOS adjustement
68 #include "GEOMToolsGUI_TransparencyDlg.h"  // Method TRANSPARENCY adjustement
69
70 #include "utilities.h"
71
72 using namespace std;
73
74 void GEOMToolsGUI::OnSettingsColor()
75 {
76   
77   QString aSCr = QAD_CONFIG->getSetting("Geometry:SettingsShadingColorRed");
78   QString aSCg = QAD_CONFIG->getSetting("Geometry:SettingsShadingColorGreen");
79   QString aSCb = QAD_CONFIG->getSetting("Geometry:SettingsShadingColorBlue");
80   QColor anInitColor;
81   if(!aSCr.isEmpty() && !aSCg.isEmpty() && !aSCb.isEmpty()) {     
82     anInitColor = QColor (aSCr.toInt(), aSCg.toInt(), aSCb.toInt());
83   } 
84   else {
85     Quantity_Color aDefColor = Quantity_Color();
86     anInitColor = 
87       QColor ((int)( aDefColor.Red() * 255.0 ), (int)( aDefColor.Green() * 255.0 ), (int)( aDefColor.Blue() * 255.0 ) );
88   }
89   
90   QColor aDialogColor = QColorDialog::getColor(anInitColor, QAD_Application::getDesktop());
91   if(aDialogColor.isValid()) {
92     QApplication::setOverrideCursor(Qt::waitCursor);
93     
94     if( QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_OCC &&
95         QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_VTK )
96       MESSAGE("Settings Color is not supported for current Viewer");
97     
98     QAD_CONFIG->addSetting("Geometry:SettingsShadingColorRed", aDialogColor.red());
99     QAD_CONFIG->addSetting("Geometry:SettingsShadingColorGreen", aDialogColor.green());
100     QAD_CONFIG->addSetting("Geometry:SettingsShadingColorBlue", aDialogColor.blue());   
101     QApplication::restoreOverrideCursor();
102   }
103 }
104
105 void GEOMToolsGUI::OnSettingsIsos()
106 {
107   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
108     return;
109   
110   OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
111   Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
112   
113   QString IsoU = QAD_CONFIG->getSetting("Geometry:SettingsIsoU");
114   QString IsoV = QAD_CONFIG->getSetting("Geometry:SettingsIsoV");
115   
116   if(!IsoU.isEmpty())
117     ic->DefaultDrawer()->UIsoAspect()->SetNumber(IsoU.toInt());
118   else
119     IsoU = "1";
120   if (!IsoV.isEmpty())
121     ic->DefaultDrawer()->VIsoAspect()->SetNumber(IsoV.toInt());
122   else
123     IsoV = "1";
124   
125   GEOMBase_NbIsosDlg* NbIsosDlg = new GEOMBase_NbIsosDlg(QAD_Application::getDesktop(), tr("GEOM_MEN_ISOS"), TRUE);     
126   int UIso = IsoU.toInt();
127   int VIso = IsoV.toInt();
128   
129   NbIsosDlg->SpinBoxU->setValue(UIso);
130   NbIsosDlg->SpinBoxV->setValue(VIso);
131   
132   if(NbIsosDlg->exec()) {
133     UIso = NbIsosDlg->SpinBoxU->text().toInt();
134     VIso = NbIsosDlg->SpinBoxV->text().toInt();
135     
136     ic->DefaultDrawer()->UIsoAspect()->SetNumber(UIso);
137     ic->DefaultDrawer()->VIsoAspect()->SetNumber(VIso);
138     QAD_CONFIG->addSetting("Geometry:SettingsIsoU", NbIsosDlg->SpinBoxU->text()); /* text format */
139     QAD_CONFIG->addSetting("Geometry:SettingsIsoV", NbIsosDlg->SpinBoxV->text()); /* text format */
140   }
141 }
142
143 void GEOMToolsGUI::OnSettingsStep()
144 {
145   QString step = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
146   if(step.isEmpty())
147     step = "100.0";
148   
149   Standard_Boolean res = false;
150   double dd = GEOMBase::Parameter(res, QString("%1").arg(step), tr("GEOM_MEN_STEP_LABEL"), tr("GEOM_STEP_TITLE"), 0.001, 10000.0, 3);
151   if(res) {
152     QAD_CONFIG->addSetting("Geometry:SettingsGeomStep", QString("%1").arg(dd));
153     
154     /* Emit signal to GeometryGUI_SpinBoxes */
155     GeometryGUI::GetGeomGUI()->EmitSignalDefaultStepValueChanged(dd);
156   }
157   else
158     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
159 }
160
161 void GEOMToolsGUI::OnRename()
162 {
163   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
164   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
165   
166   bool aLocked = aStudy->GetProperties()->IsLocked();
167   if ( aLocked ) {
168     QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
169                             QObject::tr("WRN_WARNING"), 
170                             QObject::tr("WRN_STUDY_LOCKED"),
171                             QObject::tr("BUT_OK") );
172     return;
173   }
174   
175   SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
176   for(; It.More(); It.Next()) {
177     Handle(SALOME_InteractiveObject) IObject = It.Value();
178     
179     SALOMEDS::SObject_var obj = aStudy->FindObjectID(IObject->getEntry());
180     SALOMEDS::GenericAttribute_var anAttr;
181     SALOMEDS::AttributeName_var aName;
182     if(!obj->_is_nil()) {
183       if(obj->FindAttribute(anAttr, "AttributeName")) {
184         aName = SALOMEDS::AttributeName::_narrow(anAttr);
185         
186         QString nm = QString(aName->Value());
187         nm = SALOMEGUI_NameDlg::getName(QAD_Application::getDesktop(), nm);
188         if(!nm.isEmpty()) {
189           QApplication::setOverrideCursor(Qt::waitCursor);
190           QAD_Application::getDesktop()->getActiveStudy()->renameIObject(IObject, nm);
191           QApplication::restoreOverrideCursor();
192         }
193       }
194     }
195   }
196 }
197
198 void GEOMToolsGUI::OnCheckGeometry()
199 {
200   QAD_PyEditor* PyEditor = QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getPyEditor();
201   PyEditor->setText("from GEOM_usinggeom import *\n");
202   PyEditor->handleReturn();
203 }
204
205 void GEOMToolsGUI::OnLoadScript()
206 {
207   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
208   bool aLocked = aStudy->GetProperties()->IsLocked();
209   if ( aLocked ) {
210     QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
211                             QObject::tr("WRN_WARNING"), 
212                             QObject::tr("WRN_STUDY_LOCKED"),
213                             QObject::tr("BUT_OK") );
214     return;
215   }
216   
217   QStringList filtersList;
218   filtersList.append(tr("GEOM_MEN_LOAD_SCRIPT"));
219   filtersList.append(tr("GEOM_MEN_ALL_FILES"));
220   
221   QString aFile = QAD_FileDlg::getFileName(QAD_Application::getDesktop(), "", filtersList, tr("GEOM_MEN_IMPORT"), true);
222   if(!aFile.isEmpty()) {
223     QFileInfo file = aFile;
224     QApplication::setOverrideCursor(Qt::waitCursor);
225     QAD_PyEditor* PyEditor = QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getPyEditor();
226     
227     QStringList aTextList = QStringList::split(".", file.fileName());
228     
229     PyEditor->setText("import geompy; geompy.addPath('" + file.dirPath() + "'); from " + aTextList.first() + " import *\n" );
230     PyEditor->handleReturn();
231   }
232   QApplication::restoreOverrideCursor();
233 }
234
235 void GEOMToolsGUI::OnColor()
236 {
237   SALOME_Selection* Sel = SALOME_Selection::Selection(
238     QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
239
240   if ( QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK )
241   {
242     VTKViewer_RenderWindowInteractor* myRenderInter =
243       ( (VTKViewer_ViewFrame*)QAD_Application::getDesktop()->
244          getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
245     
246     SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
247     Handle(SALOME_InteractiveObject) FirstIOS =  Sel->firstIObject();
248     if ( !FirstIOS.IsNull() )
249     {
250       QColor initcolor = myRenderInter->GetColor(FirstIOS);
251       QColor c = QColorDialog::getColor( initcolor, QAD_Application::getDesktop() );
252       
253       if ( c.isValid() )
254       {
255         QApplication::setOverrideCursor( Qt::waitCursor );
256         for ( ;It.More();It.Next() )
257         {
258           Handle(SALOME_InteractiveObject) IOS = It.Value();
259           myRenderInter->SetColor(IOS,c);
260         }
261       }
262       QApplication::restoreOverrideCursor();
263     }
264   }
265   else if( QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC )
266   {
267     Handle(AIS_InteractiveObject) anIO = GEOMBase::GetAIS( Sel->firstIObject() );
268     if ( !anIO.IsNull() )
269     {
270       Quantity_Color aColor;
271       anIO->Color( aColor );
272       
273       QColor qColor =  QColorDialog::getColor( QColor( (int)( aColor.Red() * 255.0 ),
274                                                        (int)( aColor.Green() * 255.0 ),
275                                                        (int)( aColor.Blue() * 255.0 ) ),
276                                                QAD_Application::getDesktop());
277       
278       if ( qColor.isValid() )
279       {
280         aColor = Quantity_Color( qColor.red() / 255.,
281                                  qColor.green() / 255.,
282                                  qColor.blue() / 255.,
283                                  Quantity_TOC_RGB );
284         
285         QApplication::setOverrideCursor( Qt::waitCursor );
286         
287         SALOME_ListIteratorOfListIO anIter ( Sel->StoredIObjects() );
288         for( ;anIter.More();anIter.Next() )
289         {
290           Handle(SALOME_InteractiveObject) IObject = anIter.Value();
291           Handle(AIS_InteractiveObject) anIO = GEOMBase::GetAIS( IObject, true );
292           if ( !anIO.IsNull() )
293           {
294             anIO->SetColor( aColor );
295
296             if ( anIO->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
297               Handle(GEOM_AISShape)::DownCast( anIO )->SetShadingColor( aColor );
298
299             anIO->Redisplay( Standard_True );
300           }
301         }
302       }
303     }
304   }
305   
306   QApplication::restoreOverrideCursor();
307 }
308
309 void GEOMToolsGUI::OnTransparency()
310 {
311   Handle(AIS_InteractiveContext) ic;
312   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
313   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
314     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
315     ic = v3d->getAISContext();
316   }
317   new GEOMBase_TransparencyDlg(QAD_Application::getDesktop(), "", Sel, ic);
318 }
319
320 void GEOMToolsGUI::OnNbIsos()
321 {
322   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
323     return;
324   
325   OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
326   Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
327   
328   ic->InitCurrent();
329   if(ic->MoreCurrent()) {
330     Handle(GEOM_AISShape) CurObject = Handle(GEOM_AISShape)::DownCast(ic->Current());
331     Handle(AIS_Drawer)    CurDrawer = CurObject->Attributes();
332     
333     int UIso = CurDrawer->UIsoAspect()->Number();
334     int VIso = CurDrawer->VIsoAspect()->Number();
335     
336     GEOMBase_NbIsosDlg * NbIsosDlg =
337       new GEOMBase_NbIsosDlg(QAD_Application::getDesktop(), tr("GEOM_MEN_ISOS"), TRUE);
338     
339     NbIsosDlg->SpinBoxU->setValue( UIso );
340     NbIsosDlg->SpinBoxV->setValue( VIso );
341     
342     if(NbIsosDlg->exec()) {
343       QApplication::setOverrideCursor(Qt::waitCursor);
344       for(; ic->MoreCurrent(); ic->NextCurrent()) {
345         CurObject = Handle(GEOM_AISShape)::DownCast(ic->Current());
346         Handle(AIS_Drawer) CurDrawer = CurObject->Attributes();
347         
348         int nbUIso = NbIsosDlg->SpinBoxU->text().toInt();
349         int nbVIso = NbIsosDlg->SpinBoxV->text().toInt();
350         
351         CurDrawer->SetUIsoAspect( new Prs3d_IsoAspect(Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5 , nbUIso) );
352         CurDrawer->SetVIsoAspect( new Prs3d_IsoAspect(Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5 , nbVIso) );
353         
354         ic->SetLocalAttributes(CurObject, CurDrawer);
355         ic->Redisplay(CurObject);
356       }
357     }
358   }
359   QApplication::restoreOverrideCursor();
360 }
361
362 void GEOMToolsGUI::OnOpen()
363 {
364   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
365   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
366
367   SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
368   Handle(SALOME_InteractiveObject) anIObject;
369   for(;It.More();It.Next()) {
370     anIObject = It.Value();
371     SALOMEDS::SObject_var obj = aStudy->FindObjectID(anIObject->getEntry());
372     SALOMEDS::AttributePersistentRef_var aPersist;
373     SALOMEDS::AttributeIOR_var anIOR;
374     if(!obj->_is_nil()) {
375       // this SObject may be GEOM module root SObject
376       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(obj);
377       SALOMEDS::GenericAttribute_var anAttr;
378       bool useSubItems = false;
379       while (anIter->More() && !useSubItems) {
380         SALOMEDS::SObject_var subobj = anIter->Value();
381         if (subobj->FindAttribute(anAttr, "AttributePersistentRef")) {
382           useSubItems = true;
383           obj = subobj;
384         }
385         else 
386           anIter->Next();
387       }
388       obj->FindAttribute(anAttr, "AttributePersistentRef");
389               
390       while(useSubItems?anIter->More():!anAttr->_is_nil()) { 
391         if(!obj->FindAttribute(anAttr, "AttributeIOR") &&
392            obj->FindAttribute(anAttr, "AttributePersistentRef")) {
393           SALOMEDS::SComponent_var FComp = obj->GetFatherComponent();
394           if (!CORBA::is_nil(FComp)) {
395             if (FComp->FindAttribute(anAttr, "AttributeName")) {
396               SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
397               QString compName = QAD_Application::getDesktop()->getComponentName(aName->Value());
398               //                    parent->loadComponentData(parent->getComponentName(aName->Value()));
399               Engines::Component_var comp ;
400               if ( compName.compare("SUPERV") == 0 ) {
401                 comp = QAD_Application::getDesktop()->getEngine( "SuperVisionContainer", compName) ;
402               }
403               else {
404                 comp = QAD_Application::getDesktop()->getEngine( "FactoryServer", compName);
405                 if ( comp->_is_nil() )
406                   comp = QAD_Application::getDesktop()->getEngine( "FactoryServerPy", compName);
407               }
408
409               if (!CORBA::is_nil(comp)) {
410                 SALOMEDS::Driver_var   driver = SALOMEDS::Driver::_narrow(comp);
411                 if (!CORBA::is_nil(driver)) {
412                   SALOMEDS::StudyBuilder_var  B = aStudy->NewBuilder();
413                   if (!CORBA::is_nil(B)) {
414                     B->LoadWith(FComp,driver);
415                   } else {
416                     return;
417                   }
418                 } 
419                 else {
420                   MESSAGE("loadComponentData(): Driver is null");
421                   return;
422                 }
423               }
424               else {
425                 MESSAGE("loadComponentData(): Engine is null");
426                 return;
427               }
428                 //              // load
429                 //              Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer","GEOM");
430                 //              if (!CORBA::is_nil(comp)) {
431                 //                SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(comp);
432                 //                SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
433                 //                SALOMEDS::SComponent_var SC = aStudy->FindComponent("GEOM");
434                 //                if (!CORBA::is_nil(SC))
435                 //                  aStudyBuilder->LoadWith(SC,driver);
436             } 
437           }
438           else {
439             MESSAGE("Component is null");
440           }
441         }
442         if(useSubItems) {
443           anIter->Next();
444           obj = anIter->Value();
445         } 
446         else 
447           anAttr = SALOMEDS::GenericAttribute::_nil();
448       }
449     }
450   }
451 }