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