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