Salome HOME
594d197d69e696321639aa2a3d49aacc16a0337e
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : GEOMBase_Tools.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GEOMToolsGUI.h"
30 #include "GeometryGUI.h"
31 #include "GEOM_Actor.h"
32 #include "GEOMBase.h"
33 #include "GEOM_Operation.h"
34 #include "GEOM_Displayer.h"
35
36 #include <SUIT_Session.h>
37 #include <SUIT_Application.h>
38 #include <SUIT_OverrideCursor.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Tools.h>
41 #include <SUIT_FileDlg.h>
42 #include <SUIT_Desktop.h>
43
44 #include <SalomeApp_Application.h>
45 #include <SalomeApp_Study.h>
46
47 //#include "OCCViewer_Viewer3d.h"
48 //#include "VTKViewer_ViewWindow.h"
49 //#include "VTKViewer_RenderWindowInteractor.h"
50
51 #include "SALOME_ListIteratorOfListIO.hxx"
52 //#include "SALOMEGUI_ImportOperation.h"
53 //#include "SALOMEGUI_QtCatchCorbaException.hxx"
54
55 #include <qapplication.h>
56 #include <qmap.h>
57
58 #include "utilities.h"
59
60 using namespace std;
61
62 typedef QMap<QString, QString> FilterMap;
63
64 #include "SALOMEDSClient.hxx"
65 #include "SALOMEDS_SObject.hxx"
66 #include "SALOMEDS_Study.hxx"
67
68
69
70
71 //=======================================================================
72 // function : getFileName
73 // purpose  : Selection of a file name for Import/Export. Returns also 
74 //            the selected file type code through <filter> argument.
75 //=======================================================================
76 static QString getFileName( QWidget*           parent, 
77                             const QString&     initial, 
78                             const FilterMap&   filterMap, 
79                             const QString&     caption,
80                             bool               open,
81                             QString&           format )
82 {
83   static QString lastUsedFilter;
84   QStringList filters;
85   for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it )
86     filters.push_back( it.key() );
87
88   SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );    
89   if ( !caption.isEmpty() )
90     fd->setCaption( caption );
91
92   if ( !initial.isEmpty() )
93     fd->setSelection( initial );
94
95   if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
96     fd->setSelectedFilter( lastUsedFilter );
97
98   fd->setFilters( filters );
99
100   fd->exec();
101   QString filename = fd->selectedFile();
102   format = filterMap[fd->selectedFilter()];
103   lastUsedFilter = fd->selectedFilter();
104   delete fd;
105   qApp->processEvents();
106   return filename;
107 }
108
109 //=======================================================================
110 // function : GEOMToolsGUI()
111 // purpose  : Constructor
112 //=======================================================================
113 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent ) 
114 : GEOMGUI( parent )
115 {
116 }
117
118
119 //=======================================================================
120 // function : ~GEOMToolsGUI()
121 // purpose  : Destructor
122 //=======================================================================
123 GEOMToolsGUI::~GEOMToolsGUI()
124 {
125 }
126
127
128 //=======================================================================
129 // function : OnGUIEvent()
130 // purpose  : 
131 //=======================================================================
132 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
133 {
134   getGeometryGUI()->EmitSignalDeactivateDialog();
135
136   switch (theCommandID)
137     {
138     case 31: // COPY
139       {
140         OnEditCopy();
141         break;
142       }
143     case 33: // DELETE
144       {
145         OnEditDelete();
146         break;
147       }
148     case 111: // IMPORT BREP
149     case 112: // IMPORT IGES
150     case 113: // IMPORT STEP
151       {
152         Import();
153         break;
154       }
155     case 121: // EXPORT BREP
156     case 122: // EXPORT IGES
157     case 123: // EXPORT STEP
158       {
159         Export();
160         break;
161       }
162     case 411: // SETTINGS - ADD IN STUDY
163       {
164         // SAN -- TO BE REMOVED !!!
165         break;
166       }
167     case 412: // SETTINGS - SHADING COLOR
168       {
169         OnSettingsColor();
170         break;
171       }
172     case 413: // SETTINGS - ISOS
173       {
174         OnSettingsIsos();
175         break;
176       }
177     case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
178       {
179         OnSettingsStep();
180         break;
181       }
182     case 804: // ADD IN STUDY - POPUP VIEWER
183       {
184         // SAN -- TO BE REMOVED !!!!
185         break;
186       }
187     case 901: // RENAME
188       {
189         OnRename();
190         break;
191       }
192     case 5103: // CHECK GEOMETRY
193       {
194         OnCheckGeometry();
195         break;
196       }
197     case 5104: // LOAD SCRIPT
198       {
199         OnLoadScript();
200         break;
201       }
202     case 8032: // COLOR - POPUP VIEWER
203       {
204         OnColor();
205         break;
206       }
207     case 8033: // TRANSPARENCY - POPUP VIEWER
208       {
209         OnTransparency();
210         break;
211       }
212     case 8034: // ISOS - POPUP VIEWER
213       {
214         OnNbIsos();
215         break;
216       }
217     case 9024 : // OPEN - OBJBROSER POPUP
218       {
219         OnOpen();
220         break;
221       }
222     default:
223       {
224         SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
225         break;
226       }
227     }
228   return true;
229 }
230
231
232 //===============================================================================
233 // function : OnEditDelete()
234 // purpose  :
235 //===============================================================================
236 void GEOMToolsGUI::OnEditDelete()
237 {
238 /*
239    SALOME_Selection* Sel = SALOME_Selection::Selection(
240     QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
241     
242   if ( Sel->IObjectCount() == 0 )
243     return;
244   
245   _PTR(Study) aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
246   
247   bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
248   if ( aLocked ) {
249     QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
250                             QObject::tr("WRN_WARNING"), 
251                             QObject::tr("WRN_STUDY_LOCKED"),
252                             QObject::tr("BUT_OK") );
253     return;
254   }
255
256   // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
257   QString aParentComponent = ((SALOMEGUI_Desktop*)QAD_Application::getDesktop())->getComponentFromSelection();
258   if ( aParentComponent != QAD_Application::getDesktop()->getActiveComponent() )  {
259     QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
260                             QObject::tr("ERR_ERROR"), 
261                             QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg(QAD_Application::getDesktop()->getComponentUserName( "GEOM" )),
262                             QObject::tr("BUT_OK") );
263     return;
264   }
265   // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
266
267   if ( QAD_MessageBox::warn2( QAD_Application::getDesktop(),
268                               tr( "GEOM_WRN_WARNING" ),
269                               tr( "GEOM_REALLY_DELETE" ),
270                               tr( "GEOM_BUT_YES" ),
271                               tr( "GEOM_BUT_NO" ), 1, 0, 0 ) != 1 )
272     return;
273
274   int nbSf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
275
276   Standard_Boolean found;
277   _PTR(GenericAttribute) anAttr;
278
279   SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
280   
281   QAD_Operation* op = new SALOMEGUI_ImportOperation( QAD_Application::getDesktop()->getActiveStudy() );
282
283   op->start();
284   
285   Standard_Boolean    deleted = false;
286   
287   for ( ;It.More();It.Next() )
288   {
289     Handle( SALOME_InteractiveObject ) IObject = It.Value();
290     
291     if ( !IObject->hasEntry() )
292       continue;
293
294     _PTR(SObject) SO ( aStudy->FindObjectID( IObject->getEntry() ) );
295     _PTR(AttributeIOR) anIOR;
296
297     // Erase child graphical objects
298
299     _PTR(ChildIterator) it ( aStudy->NewChildIterator( SO ) );
300     for ( ; it->More();it->Next() )
301     {
302       _PTR(SObject) CSO ( it->Value() );
303
304       if ( CSO->FindAttribute( anAttr, "AttributeIOR" ) )
305       {
306         anIOR =  anAttr;
307
308         // Delete child( s ) shape in Client :
309
310         const TCollection_AsciiString ASCior( (char*)anIOR->Value().c_str() ) ;
311         GeometryGUI::GetGeomGUI()->GetShapeReader().RemoveShapeFromBuffer( ASCior );
312
313         for ( int i = 0; i < nbSf; i++ )
314         {
315           GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( dynamic_cast<SALOMEDS_SObject*>(CSO.get())->GetObject() );
316           if ( !aGeomObj->_is_nil() )
317             GEOM_Displayer().Erase( aGeomObj, true );
318         }
319       }
320     }
321
322     // Erase main graphical object
323
324     for ( int i = 0; i < nbSf; i++ )
325     {
326       QAD_StudyFrame* sf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame( i );
327       GEOM_Displayer().Erase( IObject, true );
328     }
329
330     // Delete main shape in Client :
331
332     if ( SO->FindAttribute( anAttr, "AttributeIOR" ) )
333     {
334       anIOR = anAttr;
335       const TCollection_AsciiString ASCIor( (char*)anIOR->Value().c_str() ) ;
336       GeometryGUI::GetGeomGUI()->GetShapeReader().RemoveShapeFromBuffer( ASCIor );
337     }
338
339     // Erase objects in Study
340
341     _PTR(SObject) obj ( aStudy->FindObjectID( IObject->getEntry() ) );
342     if ( obj )
343     {
344       _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
345       aStudyBuilder->RemoveObject( obj );
346       
347       GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow(dynamic_cast<SALOMEDS_SObject*>(obj.get())->GetObject());
348       if ( !aGeomObj->_is_nil() )
349         GeometryGUI::GetGeomGUI()->GetGeomGen()->RemoveObject( aGeomObj );
350       
351       deleted = true;
352     }
353   }   
354
355   if ( deleted )
356     op->finish();
357   else
358     op->abort();
359
360   // Clear any previous selection
361   Sel->ClearIObjects();
362   QAD_Application::getDesktop()->getActiveStudy()->updateObjBrowser();
363   */
364 }
365
366
367 //==============================================================================
368 // function : OnEditCopy()
369 // purpose  :
370 //==============================================================================
371 void GEOMToolsGUI::OnEditCopy()
372 {  
373 /* 
374  SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
375   GEOM::string_array_var listIOR = new GEOM::string_array;
376
377   const SALOME_ListIO& List = Sel->StoredIObjects();
378
379   myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
380
381   Sel->ClearIObjects();
382
383   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
384   int aStudyID = aStudy->StudyId();    
385
386   for (unsigned int ind = 0; ind < listIOR->length();ind++) {
387     GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
388     try {
389       GEOM::GEOM_IInsertOperations_var IOp =  myGeom->GetIInsertOperations(aStudyID);
390       GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit) ;
391       myGeomBase->Display(result);
392     }
393     catch  (const SALOME::SALOME_Exception& S_ex) {
394       QtCatchCorbaException(S_ex);
395     }
396   }
397   
398   QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
399 */
400 }
401
402
403 //=====================================================================================
404 // function : Import
405 // purpose  : BRep, Iges, Step
406 //=====================================================================================
407 bool GEOMToolsGUI::Import()
408 {
409   SUIT_Application* app = getGeometryGUI()->getApp();
410   if (! app) return false;
411  
412   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
413   if ( !stud ) {
414     cout << "FAILED to cast active study to SalomeApp_Study" << endl;
415     return false;
416   }
417   _PTR(Study) aStudy = stud->studyDS();
418
419   bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
420   if ( aLocked ) {
421     SUIT_MessageBox::warn1 ( app->desktop(),
422                             QObject::tr("WRN_WARNING"), 
423                             QObject::tr("WRN_STUDY_LOCKED"),
424                             QObject::tr("BUT_OK") );
425     return false;
426   }
427
428   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
429   if ( CORBA::is_nil( eng ) ) {
430     SUIT_MessageBox::error1( app->desktop(), 
431                             QObject::tr("WRN_WARNING"),
432                             QObject::tr( "GEOM Engine is not started" ), 
433                             QObject::tr("BUT_OK") );
434       return false;
435     }
436
437   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
438   if ( aInsOp->_is_nil() )
439     return false;
440   
441   GEOM::GEOM_Object_var anObj;
442
443   // Obtain a list of available import formats
444   FilterMap aMap;
445   GEOM::string_array_var aFormats, aPatterns;
446   aInsOp->ImportTranslators( aFormats, aPatterns );
447
448   for ( int i = 0, n = aFormats->length(); i < n; i++ ) 
449     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
450
451   QString fileType;
452
453   QString file = getFileName(app->desktop(), "", aMap, tr("GEOM_MEN_IMPORT"), true, fileType );
454   if( file.isEmpty() || fileType.isEmpty() ) 
455     return false;
456   
457   GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
458   try {
459     SUIT_OverrideCursor wc;
460
461     app->putInfo( tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
462
463     anOp->start();
464
465     anObj = aInsOp->Import( file.latin1(), fileType.latin1() );  
466
467     if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
468       anObj->SetName( GEOMBase::GetDefaultName( QObject::tr( "GEOM_IMPORT" ) ).latin1() );
469       QString aPublishObjName = GEOMBase::GetDefaultName( SUIT_Tools::file( file, /*withExten=*/true ));
470       GeometryGUI::GetGeomGen()->PublishInStudy(dynamic_cast<SALOMEDS_Study*>(aStudy.get())->GetStudy(), 
471                                                 SALOMEDS::SObject::_nil(), 
472                                                 anObj, 
473                                                 aPublishObjName );
474
475       GEOM_Displayer( stud ).Display( anObj.in() );
476
477       anOp->commit();      
478     }
479     else {
480       anOp->abort();
481       wc.suspend();
482       SUIT_MessageBox::error1( app->desktop(), 
483                               QObject::tr( "GEOM_ERROR" ),
484                               QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ), 
485                               QObject::tr("BUT_OK") );
486     }
487   }
488   catch( const SALOME::SALOME_Exception& S_ex ) {
489     //QtCatchCorbaException(S_ex);
490     anOp->abort();
491     return false;
492   }
493
494   return true;
495 }
496
497
498 //=====================================================================================
499 // function : Export
500 // purpose  : BRep, Iges, Step
501 //=====================================================================================
502 bool GEOMToolsGUI::Export()
503 {
504 /*
505   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
506   GEOM::GEOM_IInsertOperations_var aInsOp = GeometryGUI::GetGeomGUI()->GetGeomGen()->GetIInsertOperations( aStudy->StudyId() );
507   if ( aInsOp->_is_nil() )
508     return false;
509
510   // Obtain a list of available export formats
511   FilterMap aMap;
512   GEOM::string_array_var aFormats, aPatterns;
513   aInsOp->ExportTranslators( aFormats, aPatterns );
514   for ( int i = 0, n = aFormats->length(); i < n; i++ ) 
515     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
516   
517   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
518   SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
519
520   for(;It.More();It.Next()) {
521     Handle(SALOME_InteractiveObject) IObject = It.Value();
522     Standard_Boolean found;
523     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
524
525     if ( !found || anObj->_is_nil() )
526       continue;
527
528     QString fileType;
529     QString file = getFileName(QAD_Application::getDesktop(), 
530                                QString( IObject->getName() ),
531                                aMap,
532                                tr("GEOM_MEN_EXPORT"),
533                                false,
534                                fileType);
535
536     // User has pressed "Cancel" --> stop the operation
537     if ( file.isEmpty() || fileType.isEmpty() )
538       return false;
539
540     //      Standard_Boolean result = BRepTools::Write(Shape->Shape(), strdup(file.latin1()) );
541     try {
542       QAD_WaitCursor wc;
543       aInsOp->Export( anObj, file, fileType.latin1() );
544       if ( !aInsOp->IsDone() ) {
545         wc.stop();
546         QAD_MessageBox::error1( QAD_Application::getDesktop(), 
547                                QObject::tr( "GEOM_ERROR" ),
548                                QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ), 
549                                QObject::tr("BUT_OK") );
550         return false;
551       }
552     }  
553     catch (const SALOME::SALOME_Exception& S_ex) {
554       QtCatchCorbaException(S_ex);
555     }
556   }
557 */
558   return true; 
559 }
560
561
562 //=====================================================================================
563 // EXPORTED METHODS
564 //=====================================================================================
565 extern "C"
566 {
567   GEOMGUI* GetLibGUI( GeometryGUI* parent )
568   {
569     return new GEOMToolsGUI( parent );
570   }
571 }