Salome HOME
Fix for IPAL8993(3.0.0: It is impossible to export geometry object.).
[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 #include <SUIT_ViewModel.h>
44
45 #include <SalomeApp_Application.h>
46 #include <SalomeApp_Study.h>
47 #include <SalomeApp_SelectionMgr.h>
48
49 #include <SALOME_ListIteratorOfListIO.hxx>
50 #include <SALOME_Prs.h>
51
52 #include <qapplication.h>
53 #include <qmap.h>
54
55 #include "utilities.h"
56
57 using namespace std;
58
59 typedef QMap<QString, QString> FilterMap;
60
61 #include <SALOMEDS_SObject.hxx>
62
63
64
65
66 //=======================================================================
67 // function : getFileName
68 // purpose  : Selection of a file name for Import/Export. Returns also 
69 //            the selected file type code through <filter> argument.
70 //=======================================================================
71 static QString getFileName( QWidget*           parent, 
72                             const QString&     initial, 
73                             const FilterMap&   filterMap, 
74                             const QString&     caption,
75                             bool               open,
76                             QString&           format )
77 {
78   static QString lastUsedFilter;
79   QStringList filters;
80   for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it )
81     filters.push_back( it.key() );
82
83   SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );    
84   if ( !caption.isEmpty() )
85     fd->setCaption( caption );
86
87   if ( !initial.isEmpty() )
88     fd->setSelection( initial );
89
90   if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
91     fd->setSelectedFilter( lastUsedFilter );
92
93   fd->setFilters( filters );
94
95   fd->exec();
96   QString filename = fd->selectedFile();
97   format = filterMap[fd->selectedFilter()];
98   lastUsedFilter = fd->selectedFilter();
99   delete fd;
100   qApp->processEvents();
101   return filename;
102 }
103
104 //=======================================================================
105 // function : GEOMToolsGUI()
106 // purpose  : Constructor
107 //=======================================================================
108 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent ) 
109 : GEOMGUI( parent )
110 {
111 }
112
113
114 //=======================================================================
115 // function : ~GEOMToolsGUI()
116 // purpose  : Destructor
117 //=======================================================================
118 GEOMToolsGUI::~GEOMToolsGUI()
119 {
120 }
121
122
123 //=======================================================================
124 // function : OnGUIEvent()
125 // purpose  : 
126 //=======================================================================
127 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
128 {
129   getGeometryGUI()->EmitSignalDeactivateDialog();
130
131   switch (theCommandID)
132     {
133     case 31: // COPY
134       {
135         OnEditCopy();
136         break;
137       }
138     case 33: // DELETE
139       {
140         OnEditDelete();
141         break;
142       }
143     case 111: // IMPORT BREP
144     case 112: // IMPORT IGES
145     case 113: // IMPORT STEP
146       {
147         Import();
148         break;
149       }
150     case 121: // EXPORT BREP
151     case 122: // EXPORT IGES
152     case 123: // EXPORT STEP
153       {
154         Export();
155         break;
156       }
157     case 411: // SETTINGS - ADD IN STUDY
158       {
159         // SAN -- TO BE REMOVED !!!
160         break;
161       }
162     case 412: // SETTINGS - SHADING COLOR
163       {
164         OnSettingsColor();
165         break;
166       }
167     case 413: // SETTINGS - ISOS
168       {
169         OnSettingsIsos();
170         break;
171       }
172     case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
173       {
174         OnSettingsStep();
175         break;
176       }
177     case 804: // ADD IN STUDY - POPUP VIEWER
178       {
179         // SAN -- TO BE REMOVED !!!!
180         break;
181       }
182     case 901: // RENAME
183       {
184         OnRename();
185         break;
186       }
187     case 5103: // CHECK GEOMETRY
188       {
189         OnCheckGeometry();
190         break;
191       }
192     case 5104: // LOAD SCRIPT
193       {
194         OnLoadScript();
195         break;
196       }
197     case 8032: // COLOR - POPUP VIEWER
198       {
199         OnColor();
200         break;
201       }
202     case 8033: // TRANSPARENCY - POPUP VIEWER
203       {
204         OnTransparency();
205         break;
206       }
207     case 8034: // ISOS - POPUP VIEWER
208       {
209         OnNbIsos();
210         break;
211       }
212     case 9024 : // OPEN - OBJBROSER POPUP
213       {
214         OnOpen();
215         break;
216       }
217     default:
218       {
219         SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
220         break;
221       }
222     }
223   return true;
224 }
225
226
227
228 //===============================================================================
229 // function : OnEditDelete()
230 // purpose  :
231 //===============================================================================
232 void GEOMToolsGUI::OnEditDelete()
233 {
234   SALOME_ListIO selected;
235   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
236   if ( app ) {
237     SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
238     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
239     if ( aSelMgr && appStudy ) {
240       aSelMgr->selectedObjects( selected );
241       if ( !selected.IsEmpty() ) {
242         _PTR(Study) aStudy = appStudy->studyDS();
243
244         bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
245         if ( aLocked ) {
246           SUIT_MessageBox::warn1 ( app->desktop(),
247                                    QObject::tr("WRN_WARNING"), 
248                                    QObject::tr("WRN_STUDY_LOCKED"),
249                                    QObject::tr("BUT_OK") );
250           return;
251         }
252   
253         // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
254         // modifications of ASV 01.06.05
255         QString parentComp = getParentComponent( aStudy, selected );
256         const char* geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
257         QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR ) );
258
259         if ( parentComp != geomComp )  {
260           SUIT_MessageBox::warn1 ( app->desktop(),
261                                   QObject::tr("ERR_ERROR"), 
262                                   QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
263                                   QObject::tr("BUT_OK") );
264           return;
265         }
266         // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
267
268         if ( SUIT_MessageBox::warn2( app->desktop(), 
269                                      QObject::tr( "GEOM_WRN_WARNING" ),
270                                      QObject::tr( "GEOM_REALLY_DELETE" ),
271                                      QObject::tr( "GEOM_BUT_YES" ),
272                                      QObject::tr( "GEOM_BUG_NO" ), 1, 0, 0 ) != 1 )
273           return;
274
275         //      QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
276         //      op->start();
277         
278         // prepare list of SALOME_Views
279         QPtrList<SALOME_View> views;
280         SALOME_View* view;
281         // fill the list
282         ViewManagerList vmans = app->viewManagers();
283         SUIT_ViewManager* vman;
284         for ( vman = vmans.first(); vman; vman = vmans.next() ) {
285           SUIT_ViewModel* vmod = vman->getViewModel();
286           view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
287           if ( view )
288             views.append( view );
289         }
290         
291         _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
292         _PTR(GenericAttribute) anAttr;
293         GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
294
295         // MAIN LOOP OF SELECTED OBJECTS
296         for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
297
298           Handle(SALOME_InteractiveObject) io = It.Value();
299           if ( !io->hasEntry() )
300             continue;
301
302           _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
303
304           // disable removal of "Geometry" component object
305           if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
306             continue;
307
308           // iterate through all childres of obj, find IOR attributes on children and remove shapes that 
309           // correspond to these IORs
310           for ( _PTR(ChildIterator) it ( aStudy->NewChildIterator( obj ) ); it->More();it->Next() ) {
311             _PTR(SObject) child ( it->Value() );
312             if ( child->FindAttribute( anAttr, "AttributeIOR" ) ) {
313               _PTR(AttributeIOR) anIOR( anAttr );
314
315               // Delete child( s ) shape in Client :
316               const TCollection_AsciiString ASCior( (char*)anIOR->Value().c_str() ) ;
317               getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer( ASCior );
318
319               for ( view = views.first(); view; view = views.next() ) { 
320                 CORBA::Object_var corbaObj = (dynamic_cast<SALOMEDS_SObject*>(child.get()))->GetObject();
321                 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
322                 if ( !CORBA::is_nil( geomObj ) )
323                   disp->Erase( geomObj, true, view );
324               }
325             }
326           } // for ( childres of obj )
327
328           // Erase main graphical object
329           for ( view = views.first(); view; view = views.next() ) 
330             disp->Erase( io, true, view );
331
332           // Delete main shape in Client :
333           if ( obj->FindAttribute( anAttr, "AttributeIOR" ) ) {
334             _PTR(AttributeIOR) anIOR( anAttr );
335             const TCollection_AsciiString ASCIor( (char*)anIOR->Value().c_str() ) ;
336             getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer( ASCIor );
337           }
338
339           // Remove objects from Study
340           aStudyBuilder->RemoveObject( obj );
341
342           CORBA::Object_var corbaObj = (dynamic_cast<SALOMEDS_SObject*>(obj.get()))->GetObject();
343           GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
344           if ( !CORBA::is_nil( geomObj ) )
345             GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
346       
347           //deleted = true;
348         } // MAIN LOOP of selected
349         
350         selected.Clear();
351         aSelMgr->setSelectedObjects( selected );
352         getGeometryGUI()->updateObjBrowser();
353       } // if ( selected not empty )
354     } // if ( selMgr && appStudy )
355   } // if ( app )
356
357   //  if ( deleted )
358   //    op->finish();
359   //  else
360   //    op->abort();
361 }
362
363
364 //==============================================================================
365 // function : OnEditCopy()
366 // purpose  :
367 //==============================================================================
368 void GEOMToolsGUI::OnEditCopy()
369 {  
370 /* 
371  SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
372   GEOM::string_array_var listIOR = new GEOM::string_array;
373
374   const SALOME_ListIO& List = Sel->StoredIObjects();
375
376   myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
377
378   Sel->ClearIObjects();
379
380   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
381   int aStudyID = aStudy->StudyId();    
382
383   for (unsigned int ind = 0; ind < listIOR->length();ind++) {
384     GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
385     try {
386       GEOM::GEOM_IInsertOperations_var IOp =  myGeom->GetIInsertOperations(aStudyID);
387       GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit) ;
388       myGeomBase->Display(result);
389     }
390     catch  (const SALOME::SALOME_Exception& S_ex) {
391       QtCatchCorbaException(S_ex);
392     }
393   }
394   
395   QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
396 */
397 }
398
399
400 //=====================================================================================
401 // function : Import
402 // purpose  : BRep, Iges, Step
403 //=====================================================================================
404 bool GEOMToolsGUI::Import()
405 {
406   SUIT_Application* app = getGeometryGUI()->getApp();
407   if (! app) return false;
408  
409   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
410   if ( !stud ) {
411     cout << "FAILED to cast active study to SalomeApp_Study" << endl;
412     return false;
413   }
414   _PTR(Study) aStudy = stud->studyDS();
415
416   bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
417   if ( aLocked ) {
418     SUIT_MessageBox::warn1 ( app->desktop(),
419                             QObject::tr("WRN_WARNING"), 
420                             QObject::tr("WRN_STUDY_LOCKED"),
421                             QObject::tr("BUT_OK") );
422     return false;
423   }
424
425   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
426   if ( CORBA::is_nil( eng ) ) {
427     SUIT_MessageBox::error1( app->desktop(), 
428                             QObject::tr("WRN_WARNING"),
429                             QObject::tr( "GEOM Engine is not started" ), 
430                             QObject::tr("BUT_OK") );
431       return false;
432     }
433
434   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
435   if ( aInsOp->_is_nil() )
436     return false;
437   
438   GEOM::GEOM_Object_var anObj;
439
440   // Obtain a list of available import formats
441   FilterMap aMap;
442   GEOM::string_array_var aFormats, aPatterns;
443   aInsOp->ImportTranslators( aFormats, aPatterns );
444
445   for ( int i = 0, n = aFormats->length(); i < n; i++ ) 
446     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
447
448   QString fileType;
449
450   QString file = getFileName(app->desktop(), "", aMap, tr("GEOM_MEN_IMPORT"), true, fileType );
451   if( file.isEmpty() || fileType.isEmpty() ) 
452     return false;
453   
454   GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
455   try {
456     SUIT_OverrideCursor wc;
457
458     app->putInfo( tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
459
460     anOp->start();
461
462     anObj = aInsOp->Import( file.latin1(), fileType.latin1() );  
463
464     if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
465       anObj->SetName( GEOMBase::GetDefaultName( QObject::tr( "GEOM_IMPORT" ) ).latin1() );
466       QString aPublishObjName = GEOMBase::GetDefaultName( SUIT_Tools::file( file, /*withExten=*/true ));
467       GeometryGUI::GetGeomGen()->PublishInStudy(dynamic_cast<SALOMEDS_Study*>(aStudy.get())->GetStudy(), 
468                                                 SALOMEDS::SObject::_nil(), 
469                                                 anObj, 
470                                                 aPublishObjName );
471
472       GEOM_Displayer( stud ).Display( anObj.in() );
473
474       // update data model and object browser
475       getGeometryGUI()->updateObjBrowser( true );
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   SalomeApp_Application* app = getGeometryGUI()->getApp();
505   if (!app) return false;
506   
507   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
508   if ( !stud ) {
509     cout << "FAILED to cast active study to SalomeApp_Study" << endl;
510     return false;
511   }
512   _PTR(Study) aStudy = stud->studyDS();
513   
514   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
515   if ( CORBA::is_nil( eng ) ) {
516     SUIT_MessageBox::error1( app->desktop(), 
517                              QObject::tr("WRN_WARNING"),
518                              QObject::tr( "GEOM Engine is not started" ), 
519                              QObject::tr("BUT_OK") );
520     return false;
521   }
522   
523   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
524   if ( aInsOp->_is_nil() )
525     return false;
526
527   // Obtain a list of available export formats
528   FilterMap aMap;
529   GEOM::string_array_var aFormats, aPatterns;
530   aInsOp->ExportTranslators( aFormats, aPatterns );
531   for ( int i = 0, n = aFormats->length(); i < n; i++ ) 
532     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
533
534   // Get selected objects
535   SalomeApp_SelectionMgr* sm = app->selectionMgr();
536   if ( !sm )
537     return false;
538
539   SALOME_ListIO selectedObjects;
540   sm->selectedObjects( selectedObjects );
541
542   SALOME_ListIteratorOfListIO It( selectedObjects );
543   for(;It.More();It.Next()) {
544     Handle(SALOME_InteractiveObject) IObject = It.Value();
545     Standard_Boolean found;
546     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
547
548     if ( !found || anObj->_is_nil() )
549       continue;
550     
551     QString fileType;
552     QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap, 
553                                tr("GEOM_MEN_EXPORT"), false, fileType);
554     
555     // User has pressed "Cancel" --> stop the operation
556     if ( file.isEmpty() || fileType.isEmpty() )
557       return false;
558     
559     GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
560     try {
561       SUIT_OverrideCursor wc;
562       
563       app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
564       
565       anOp->start();
566
567
568       aInsOp->Export( anObj, file, fileType.latin1() );
569
570       if ( aInsOp->IsDone() )
571         anOp->commit();
572       else
573         {
574           anOp->abort();
575           wc.suspend();
576           SUIT_MessageBox::error1( app->desktop(), 
577                                    QObject::tr( "GEOM_ERROR" ),
578                                    QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ), 
579                                    QObject::tr("BUT_OK") );
580           return false;
581         }
582     }  
583     catch (const SALOME::SALOME_Exception& S_ex) {
584       //QtCatchCorbaException(S_ex);
585       anOp->abort();
586       return false;
587     }
588   }
589   
590   return true; 
591 }
592
593
594 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
595 {
596   QString parentComp;
597
598   for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
599
600     Handle(SALOME_InteractiveObject) io = it.Value();
601     if ( !io->hasEntry() ) 
602       continue;
603
604     QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
605
606     if ( parentComp.isNull() )
607       parentComp = compName;
608     else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
609       parentComp = QString::null;
610       break;
611     }
612   }
613
614   return parentComp;
615 }
616
617 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
618 {
619   if ( obj ) {
620     _PTR(SComponent) comp = obj->GetFatherComponent();
621     if ( comp ) {
622       _PTR(GenericAttribute) anAttr;
623       if ( comp->FindAttribute( anAttr, "AttributeName") ) {
624         _PTR(AttributeName) aName( anAttr );
625         return QString( aName->Value().c_str() );
626       }
627     }
628   }
629   return QString();
630 }
631
632 //=====================================================================================
633 // EXPORTED METHODS
634 //=====================================================================================
635 extern "C"
636 {
637   GEOMGUI* GetLibGUI( GeometryGUI* parent )
638   {
639     return new GEOMToolsGUI( parent );
640   }
641 }