1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : GEOMBase_Tools.cxx
25 // Author : Damien COQUERET
29 #include "GEOMToolsGUI.h"
30 #include "GeometryGUI.h"
31 #include "GEOM_Actor.h"
33 #include "GEOM_Operation.h"
34 #include "GEOM_Displayer.h"
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>
44 #include <SalomeApp_Application.h>
45 #include <SalomeApp_Study.h>
47 //#include "OCCViewer_Viewer3d.h"
48 //#include "VTKViewer_ViewWindow.h"
49 //#include "VTKViewer_RenderWindowInteractor.h"
51 #include "SALOME_ListIteratorOfListIO.hxx"
52 //#include "SALOMEGUI_ImportOperation.h"
53 //#include "SALOMEGUI_QtCatchCorbaException.hxx"
55 #include <qapplication.h>
58 #include "utilities.h"
62 typedef QMap<QString, QString> FilterMap;
64 #include "SALOMEDSClient.hxx"
65 #include "SALOMEDS_SObject.hxx"
66 #include "SALOMEDS_Study.hxx"
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,
83 static QString lastUsedFilter;
85 for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it )
86 filters.push_back( it.key() );
88 SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
89 if ( !caption.isEmpty() )
90 fd->setCaption( caption );
92 if ( !initial.isEmpty() )
93 fd->setSelection( initial );
95 if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
96 fd->setSelectedFilter( lastUsedFilter );
98 fd->setFilters( filters );
101 QString filename = fd->selectedFile();
102 format = filterMap[fd->selectedFilter()];
103 lastUsedFilter = fd->selectedFilter();
105 qApp->processEvents();
109 //=======================================================================
110 // function : GEOMToolsGUI()
111 // purpose : Constructor
112 //=======================================================================
113 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
119 //=======================================================================
120 // function : ~GEOMToolsGUI()
121 // purpose : Destructor
122 //=======================================================================
123 GEOMToolsGUI::~GEOMToolsGUI()
128 //=======================================================================
129 // function : OnGUIEvent()
131 //=======================================================================
132 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
134 getGeometryGUI()->EmitSignalDeactivateDialog();
136 switch (theCommandID)
148 case 111: // IMPORT BREP
149 case 112: // IMPORT IGES
150 case 113: // IMPORT STEP
155 case 121: // EXPORT BREP
156 case 122: // EXPORT IGES
157 case 123: // EXPORT STEP
162 case 411: // SETTINGS - ADD IN STUDY
164 // SAN -- TO BE REMOVED !!!
167 case 412: // SETTINGS - SHADING COLOR
172 case 413: // SETTINGS - ISOS
177 case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
182 case 804: // ADD IN STUDY - POPUP VIEWER
184 // SAN -- TO BE REMOVED !!!!
192 case 5103: // CHECK GEOMETRY
197 case 5104: // LOAD SCRIPT
202 case 8032: // COLOR - POPUP VIEWER
207 case 8033: // TRANSPARENCY - POPUP VIEWER
212 case 8034: // ISOS - POPUP VIEWER
217 case 9024 : // OPEN - OBJBROSER POPUP
224 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
232 //===============================================================================
233 // function : OnEditDelete()
235 //===============================================================================
236 void GEOMToolsGUI::OnEditDelete()
239 SALOME_Selection* Sel = SALOME_Selection::Selection(
240 QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
242 if ( Sel->IObjectCount() == 0 )
245 _PTR(Study) aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
247 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
249 QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
250 QObject::tr("WRN_WARNING"),
251 QObject::tr("WRN_STUDY_LOCKED"),
252 QObject::tr("BUT_OK") );
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") );
265 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
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 )
274 int nbSf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
276 Standard_Boolean found;
277 _PTR(GenericAttribute) anAttr;
279 SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
281 QAD_Operation* op = new SALOMEGUI_ImportOperation( QAD_Application::getDesktop()->getActiveStudy() );
285 Standard_Boolean deleted = false;
287 for ( ;It.More();It.Next() )
289 Handle( SALOME_InteractiveObject ) IObject = It.Value();
291 if ( !IObject->hasEntry() )
294 _PTR(SObject) SO ( aStudy->FindObjectID( IObject->getEntry() ) );
295 _PTR(AttributeIOR) anIOR;
297 // Erase child graphical objects
299 _PTR(ChildIterator) it ( aStudy->NewChildIterator( SO ) );
300 for ( ; it->More();it->Next() )
302 _PTR(SObject) CSO ( it->Value() );
304 if ( CSO->FindAttribute( anAttr, "AttributeIOR" ) )
308 // Delete child( s ) shape in Client :
310 const TCollection_AsciiString ASCior( (char*)anIOR->Value().c_str() ) ;
311 GeometryGUI::GetGeomGUI()->GetShapeReader().RemoveShapeFromBuffer( ASCior );
313 for ( int i = 0; i < nbSf; i++ )
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 );
322 // Erase main graphical object
324 for ( int i = 0; i < nbSf; i++ )
326 QAD_StudyFrame* sf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame( i );
327 GEOM_Displayer().Erase( IObject, true );
330 // Delete main shape in Client :
332 if ( SO->FindAttribute( anAttr, "AttributeIOR" ) )
335 const TCollection_AsciiString ASCIor( (char*)anIOR->Value().c_str() ) ;
336 GeometryGUI::GetGeomGUI()->GetShapeReader().RemoveShapeFromBuffer( ASCIor );
339 // Erase objects in Study
341 _PTR(SObject) obj ( aStudy->FindObjectID( IObject->getEntry() ) );
344 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
345 aStudyBuilder->RemoveObject( obj );
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 );
360 // Clear any previous selection
361 Sel->ClearIObjects();
362 QAD_Application::getDesktop()->getActiveStudy()->updateObjBrowser();
367 //==============================================================================
368 // function : OnEditCopy()
370 //==============================================================================
371 void GEOMToolsGUI::OnEditCopy()
374 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
375 GEOM::string_array_var listIOR = new GEOM::string_array;
377 const SALOME_ListIO& List = Sel->StoredIObjects();
379 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
381 Sel->ClearIObjects();
383 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
384 int aStudyID = aStudy->StudyId();
386 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
387 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
389 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
390 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit) ;
391 myGeomBase->Display(result);
393 catch (const SALOME::SALOME_Exception& S_ex) {
394 QtCatchCorbaException(S_ex);
398 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
403 //=====================================================================================
405 // purpose : BRep, Iges, Step
406 //=====================================================================================
407 bool GEOMToolsGUI::Import()
409 SUIT_Application* app = getGeometryGUI()->getApp();
410 if (! app) return false;
412 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
414 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
417 _PTR(Study) aStudy = stud->studyDS();
419 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
421 SUIT_MessageBox::warn1 ( app->desktop(),
422 QObject::tr("WRN_WARNING"),
423 QObject::tr("WRN_STUDY_LOCKED"),
424 QObject::tr("BUT_OK") );
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") );
437 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
438 if ( aInsOp->_is_nil() )
441 GEOM::GEOM_Object_var anObj;
443 // Obtain a list of available import formats
445 GEOM::string_array_var aFormats, aPatterns;
446 aInsOp->ImportTranslators( aFormats, aPatterns );
448 for ( int i = 0, n = aFormats->length(); i < n; i++ )
449 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
453 QString file = getFileName(app->desktop(), "", aMap, tr("GEOM_MEN_IMPORT"), true, fileType );
454 if( file.isEmpty() || fileType.isEmpty() )
457 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
459 SUIT_OverrideCursor wc;
461 app->putInfo( tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
465 anObj = aInsOp->Import( file.latin1(), fileType.latin1() );
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(),
475 GEOM_Displayer( stud ).Display( anObj.in() );
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") );
488 catch( const SALOME::SALOME_Exception& S_ex ) {
489 //QtCatchCorbaException(S_ex);
498 //=====================================================================================
500 // purpose : BRep, Iges, Step
501 //=====================================================================================
502 bool GEOMToolsGUI::Export()
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() )
510 // Obtain a list of available export formats
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] );
517 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
518 SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
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);
525 if ( !found || anObj->_is_nil() )
529 QString file = getFileName(QAD_Application::getDesktop(),
530 QString( IObject->getName() ),
532 tr("GEOM_MEN_EXPORT"),
536 // User has pressed "Cancel" --> stop the operation
537 if ( file.isEmpty() || fileType.isEmpty() )
540 // Standard_Boolean result = BRepTools::Write(Shape->Shape(), strdup(file.latin1()) );
543 aInsOp->Export( anObj, file, fileType.latin1() );
544 if ( !aInsOp->IsDone() ) {
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") );
553 catch (const SALOME::SALOME_Exception& S_ex) {
554 QtCatchCorbaException(S_ex);
562 //=====================================================================================
564 //=====================================================================================
567 GEOMGUI* GetLibGUI( GeometryGUI* parent )
569 return new GEOMToolsGUI( parent );