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>
43 #include <SUIT_ViewModel.h>
45 #include <SalomeApp_Application.h>
46 #include <SalomeApp_Study.h>
47 #include <SalomeApp_SelectionMgr.h>
49 #include <SALOME_ListIteratorOfListIO.hxx>
50 #include <SALOME_Prs.h>
52 #include <qapplication.h>
55 #include "utilities.h"
59 typedef QMap<QString, QString> FilterMap;
61 #include <SALOMEDS_SObject.hxx>
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,
78 static QString lastUsedFilter;
80 for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it )
81 filters.push_back( it.key() );
83 SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
84 if ( !caption.isEmpty() )
85 fd->setCaption( caption );
87 if ( !initial.isEmpty() )
88 fd->setSelection( initial );
90 if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
91 fd->setSelectedFilter( lastUsedFilter );
93 fd->setFilters( filters );
96 QString filename = fd->selectedFile();
97 format = filterMap[fd->selectedFilter()];
98 lastUsedFilter = fd->selectedFilter();
100 qApp->processEvents();
104 //=======================================================================
105 // function : GEOMToolsGUI()
106 // purpose : Constructor
107 //=======================================================================
108 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
114 //=======================================================================
115 // function : ~GEOMToolsGUI()
116 // purpose : Destructor
117 //=======================================================================
118 GEOMToolsGUI::~GEOMToolsGUI()
123 //=======================================================================
124 // function : OnGUIEvent()
126 //=======================================================================
127 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
129 getGeometryGUI()->EmitSignalDeactivateDialog();
131 switch (theCommandID)
143 case 111: // IMPORT BREP
144 case 112: // IMPORT IGES
145 case 113: // IMPORT STEP
150 case 121: // EXPORT BREP
151 case 122: // EXPORT IGES
152 case 123: // EXPORT STEP
157 case 411: // SETTINGS - ADD IN STUDY
159 // SAN -- TO BE REMOVED !!!
162 case 412: // SETTINGS - SHADING COLOR
167 case 413: // SETTINGS - ISOS
172 case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
177 case 804: // ADD IN STUDY - POPUP VIEWER
179 // SAN -- TO BE REMOVED !!!!
187 case 5103: // CHECK GEOMETRY
192 case 8032: // COLOR - POPUP VIEWER
197 case 8033: // TRANSPARENCY - POPUP VIEWER
202 case 8034: // ISOS - POPUP VIEWER
207 case 9024 : // OPEN - OBJBROSER POPUP
214 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
223 //===============================================================================
224 // function : OnEditDelete()
226 //===============================================================================
227 void GEOMToolsGUI::OnEditDelete()
229 SALOME_ListIO selected;
230 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
232 SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
233 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
234 if ( aSelMgr && appStudy ) {
235 aSelMgr->selectedObjects( selected );
236 if ( !selected.IsEmpty() ) {
237 _PTR(Study) aStudy = appStudy->studyDS();
239 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
241 SUIT_MessageBox::warn1 ( app->desktop(),
242 QObject::tr("WRN_WARNING"),
243 QObject::tr("WRN_STUDY_LOCKED"),
244 QObject::tr("BUT_OK") );
248 // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
249 // modifications of ASV 01.06.05
250 QString parentComp = getParentComponent( aStudy, selected );
251 const char* geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
252 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR ) );
254 if ( parentComp != geomComp ) {
255 SUIT_MessageBox::warn1 ( app->desktop(),
256 QObject::tr("ERR_ERROR"),
257 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
258 QObject::tr("BUT_OK") );
261 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
263 if ( SUIT_MessageBox::warn2( app->desktop(),
264 QObject::tr( "GEOM_WRN_WARNING" ),
265 QObject::tr( "GEOM_REALLY_DELETE" ),
266 QObject::tr( "GEOM_BUT_YES" ),
267 QObject::tr( "GEOM_BUT_NO" ), 1, 0, 0 ) != 1 )
270 // QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
273 // prepare list of SALOME_Views
274 QPtrList<SALOME_View> views;
277 ViewManagerList vmans = app->viewManagers();
278 SUIT_ViewManager* vman;
279 for ( vman = vmans.first(); vman; vman = vmans.next() ) {
280 SUIT_ViewModel* vmod = vman->getViewModel();
281 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
283 views.append( view );
286 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
287 _PTR(GenericAttribute) anAttr;
288 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
290 // MAIN LOOP OF SELECTED OBJECTS
291 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
293 Handle(SALOME_InteractiveObject) io = It.Value();
294 if ( !io->hasEntry() )
297 _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
299 // disable removal of "Geometry" component object
300 if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
303 // iterate through all childres of obj, find IOR attributes on children and remove shapes that
304 // correspond to these IORs
305 for ( _PTR(ChildIterator) it ( aStudy->NewChildIterator( obj ) ); it->More();it->Next() ) {
306 _PTR(SObject) child ( it->Value() );
307 if ( child->FindAttribute( anAttr, "AttributeIOR" ) ) {
308 _PTR(AttributeIOR) anIOR( anAttr );
310 // Delete child( s ) shape in Client :
311 const TCollection_AsciiString ASCior( (char*)anIOR->Value().c_str() ) ;
312 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer( ASCior );
314 for ( view = views.first(); view; view = views.next() ) {
315 CORBA::Object_var corbaObj = (dynamic_cast<SALOMEDS_SObject*>(child.get()))->GetObject();
316 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
317 if ( !CORBA::is_nil( geomObj ) )
318 disp->Erase( geomObj, true, view );
321 } // for ( childres of obj )
323 // Erase main graphical object
324 for ( view = views.first(); view; view = views.next() )
325 disp->Erase( io, true, view );
327 // Delete main shape in Client :
328 if ( obj->FindAttribute( anAttr, "AttributeIOR" ) ) {
329 _PTR(AttributeIOR) anIOR( anAttr );
330 const TCollection_AsciiString ASCIor( (char*)anIOR->Value().c_str() ) ;
331 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer( ASCIor );
334 // Remove objects from Study
335 aStudyBuilder->RemoveObject( obj );
337 CORBA::Object_var corbaObj = (dynamic_cast<SALOMEDS_SObject*>(obj.get()))->GetObject();
338 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
339 if ( !CORBA::is_nil( geomObj ) )
340 GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
343 } // MAIN LOOP of selected
346 aSelMgr->setSelectedObjects( selected );
347 getGeometryGUI()->updateObjBrowser();
348 } // if ( selected not empty )
349 } // if ( selMgr && appStudy )
351 app->updateActions(); //SRN: BugID IPAL9377, case 1 for GEOM module
363 //==============================================================================
364 // function : OnEditCopy()
366 //==============================================================================
367 void GEOMToolsGUI::OnEditCopy()
370 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
371 GEOM::string_array_var listIOR = new GEOM::string_array;
373 const SALOME_ListIO& List = Sel->StoredIObjects();
375 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
377 Sel->ClearIObjects();
379 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
380 int aStudyID = aStudy->StudyId();
382 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
383 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
385 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
386 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit) ;
387 myGeomBase->Display(result);
389 catch (const SALOME::SALOME_Exception& S_ex) {
390 QtCatchCorbaException(S_ex);
394 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
399 //=====================================================================================
401 // purpose : BRep, Iges, Step
402 //=====================================================================================
403 bool GEOMToolsGUI::Import()
405 SUIT_Application* app = getGeometryGUI()->getApp();
406 if (! app) return false;
408 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
410 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
413 _PTR(Study) aStudy = stud->studyDS();
415 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
417 SUIT_MessageBox::warn1 ( app->desktop(),
418 QObject::tr("WRN_WARNING"),
419 QObject::tr("WRN_STUDY_LOCKED"),
420 QObject::tr("BUT_OK") );
424 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
425 if ( CORBA::is_nil( eng ) ) {
426 SUIT_MessageBox::error1( app->desktop(),
427 QObject::tr("WRN_WARNING"),
428 QObject::tr( "GEOM Engine is not started" ),
429 QObject::tr("BUT_OK") );
433 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
434 if ( aInsOp->_is_nil() )
437 GEOM::GEOM_Object_var anObj;
439 // Obtain a list of available import formats
441 GEOM::string_array_var aFormats, aPatterns;
442 aInsOp->ImportTranslators( aFormats, aPatterns );
444 for ( int i = 0, n = aFormats->length(); i < n; i++ )
445 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
449 QString file = getFileName(app->desktop(), "", aMap, tr("GEOM_MEN_IMPORT"), true, fileType );
450 if( file.isEmpty() || fileType.isEmpty() )
453 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
455 SUIT_OverrideCursor wc;
457 app->putInfo( tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
461 anObj = aInsOp->Import( file.latin1(), fileType.latin1() );
463 if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
464 anObj->SetName( GEOMBase::GetDefaultName( QObject::tr( "GEOM_IMPORT" ) ).latin1() );
465 QString aPublishObjName = GEOMBase::GetDefaultName( SUIT_Tools::file( file, /*withExten=*/true ));
466 GeometryGUI::GetGeomGen()->PublishInStudy(dynamic_cast<SALOMEDS_Study*>(aStudy.get())->GetStudy(),
467 SALOMEDS::SObject::_nil(),
471 GEOM_Displayer( stud ).Display( anObj.in() );
473 // update data model and object browser
474 getGeometryGUI()->updateObjBrowser( true );
481 SUIT_MessageBox::error1( app->desktop(),
482 QObject::tr( "GEOM_ERROR" ),
483 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
484 QObject::tr("BUT_OK") );
487 catch( const SALOME::SALOME_Exception& S_ex ) {
488 //QtCatchCorbaException(S_ex);
497 //=====================================================================================
499 // purpose : BRep, Iges, Step
500 //=====================================================================================
501 bool GEOMToolsGUI::Export()
503 SalomeApp_Application* app = getGeometryGUI()->getApp();
504 if (!app) return false;
506 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
508 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
511 _PTR(Study) aStudy = stud->studyDS();
513 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
514 if ( CORBA::is_nil( eng ) ) {
515 SUIT_MessageBox::error1( app->desktop(),
516 QObject::tr("WRN_WARNING"),
517 QObject::tr( "GEOM Engine is not started" ),
518 QObject::tr("BUT_OK") );
522 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
523 if ( aInsOp->_is_nil() )
526 // Obtain a list of available export formats
528 GEOM::string_array_var aFormats, aPatterns;
529 aInsOp->ExportTranslators( aFormats, aPatterns );
530 for ( int i = 0, n = aFormats->length(); i < n; i++ )
531 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
533 // Get selected objects
534 SalomeApp_SelectionMgr* sm = app->selectionMgr();
538 SALOME_ListIO selectedObjects;
539 sm->selectedObjects( selectedObjects );
541 SALOME_ListIteratorOfListIO It( selectedObjects );
542 for(;It.More();It.Next()) {
543 Handle(SALOME_InteractiveObject) IObject = It.Value();
544 Standard_Boolean found;
545 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
547 if ( !found || anObj->_is_nil() )
551 QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap,
552 tr("GEOM_MEN_EXPORT"), false, fileType);
554 // User has pressed "Cancel" --> stop the operation
555 if ( file.isEmpty() || fileType.isEmpty() )
558 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
560 SUIT_OverrideCursor wc;
562 app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
567 aInsOp->Export( anObj, file, fileType.latin1() );
569 if ( aInsOp->IsDone() )
575 SUIT_MessageBox::error1( app->desktop(),
576 QObject::tr( "GEOM_ERROR" ),
577 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
578 QObject::tr("BUT_OK") );
582 catch (const SALOME::SALOME_Exception& S_ex) {
583 //QtCatchCorbaException(S_ex);
593 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
597 for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
599 Handle(SALOME_InteractiveObject) io = it.Value();
600 if ( !io->hasEntry() )
603 QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
605 if ( parentComp.isNull() )
606 parentComp = compName;
607 else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
608 parentComp = QString::null;
616 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
619 _PTR(SComponent) comp = obj->GetFatherComponent();
621 _PTR(GenericAttribute) anAttr;
622 if ( comp->FindAttribute( anAttr, "AttributeName") ) {
623 _PTR(AttributeName) aName( anAttr );
624 return QString( aName->Value().c_str() );
631 //=====================================================================================
633 //=====================================================================================
636 GEOMGUI* GetLibGUI( GeometryGUI* parent )
638 return new GEOMToolsGUI( parent );