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.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : GEOMBase_Tools.cxx
25 // Author : Damien COQUERET
29 #include "GEOMToolsGUI.h"
31 #include "GeometryGUI.h"
32 #include "GEOM_Actor.h"
34 #include "GEOM_Operation.h"
35 #include "GEOM_Displayer.h"
37 #include <SUIT_Session.h>
38 #include <SUIT_Application.h>
39 #include <SUIT_OverrideCursor.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_Tools.h>
42 #include <SUIT_FileDlg.h>
43 #include <SUIT_Desktop.h>
44 #include <SUIT_ViewModel.h>
46 #include <SalomeApp_Application.h>
47 #include <SalomeApp_Study.h>
48 #include <LightApp_SelectionMgr.h>
50 #include <SALOME_ListIteratorOfListIO.hxx>
51 #include <SALOME_Prs.h>
53 #include "utilities.h"
56 #include <qapplication.h>
60 #include <TCollection_AsciiString.hxx>
64 typedef QMap<QString, QString> FilterMap;
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 =
231 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
233 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
234 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
235 if ( aSelMgr && appStudy ) {
236 aSelMgr->selectedObjects( selected, QString::null, false );
237 if ( !selected.IsEmpty() ) {
238 _PTR(Study) aStudy = appStudy->studyDS();
240 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
242 SUIT_MessageBox::warn1 ( app->desktop(),
243 QObject::tr("WRN_WARNING"),
244 QObject::tr("WRN_STUDY_LOCKED"),
245 QObject::tr("BUT_OK") );
249 // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
250 // modifications of ASV 01.06.05
251 QString parentComp = getParentComponent( aStudy, selected );
252 const char* geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
253 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR ) );
255 if ( parentComp != geomComp ) {
256 SUIT_MessageBox::warn1 ( app->desktop(),
257 QObject::tr("ERR_ERROR"),
258 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
259 QObject::tr("BUT_OK") );
262 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
264 if ( SUIT_MessageBox::warn2( app->desktop(),
265 QObject::tr( "GEOM_WRN_WARNING" ),
266 QObject::tr( "GEOM_REALLY_DELETE" ),
267 QObject::tr( "GEOM_BUT_YES" ),
268 QObject::tr( "GEOM_BUT_NO" ), 1, 0, 0 ) != 1 )
271 // QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
274 // prepare list of SALOME_Views
275 QPtrList<SALOME_View> views;
278 ViewManagerList vmans = app->viewManagers();
279 SUIT_ViewManager* vman;
280 for ( vman = vmans.first(); vman; vman = vmans.next() ) {
281 SUIT_ViewModel* vmod = vman->getViewModel();
282 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
284 views.append( view );
287 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
288 _PTR(GenericAttribute) anAttr;
289 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
291 // MAIN LOOP OF SELECTED OBJECTS
292 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
294 Handle(SALOME_InteractiveObject) io = It.Value();
295 if ( !io->hasEntry() )
298 _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
300 // disable removal of "Geometry" component object
301 if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
304 RemoveObjectWithChildren(obj, aStudy, views, disp);
306 // Remove objects from Study
307 aStudyBuilder->RemoveObject( obj );
310 } // MAIN LOOP of selected
313 aSelMgr->setSelectedObjects( selected );
314 getGeometryGUI()->updateObjBrowser();
315 } // if ( selected not empty )
316 } // if ( selMgr && appStudy )
318 app->updateActions(); //SRN: To update a Save button in the toolbar
330 //==============================================================================
331 // function : OnEditCopy()
333 //==============================================================================
334 void GEOMToolsGUI::OnEditCopy()
337 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
338 GEOM::string_array_var listIOR = new GEOM::string_array;
340 const SALOME_ListIO& List = Sel->StoredIObjects();
342 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
344 Sel->ClearIObjects();
346 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
347 int aStudyID = aStudy->StudyId();
349 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
350 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
352 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
353 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
354 myGeomBase->Display(result);
356 catch (const SALOME::SALOME_Exception& S_ex) {
357 QtCatchCorbaException(S_ex);
361 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
366 //=====================================================================================
368 // purpose : BRep, Iges, Step
369 //=====================================================================================
370 bool GEOMToolsGUI::Import()
372 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
373 //SUIT_Application* app = getGeometryGUI()->getApp();
374 if (! app) return false;
376 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
378 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
381 _PTR(Study) aStudy = stud->studyDS();
383 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
385 SUIT_MessageBox::warn1 ( app->desktop(),
386 QObject::tr("WRN_WARNING"),
387 QObject::tr("WRN_STUDY_LOCKED"),
388 QObject::tr("BUT_OK") );
392 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
393 if ( CORBA::is_nil( eng ) ) {
394 SUIT_MessageBox::error1( app->desktop(),
395 QObject::tr("WRN_WARNING"),
396 QObject::tr( "GEOM Engine is not started" ),
397 QObject::tr("BUT_OK") );
401 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
402 if ( aInsOp->_is_nil() )
405 GEOM::GEOM_Object_var anObj;
407 // Obtain a list of available import formats
409 GEOM::string_array_var aFormats, aPatterns;
410 aInsOp->ImportTranslators( aFormats, aPatterns );
412 for ( int i = 0, n = aFormats->length(); i < n; i++ )
413 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
417 QString fileName = getFileName(app->desktop(), "", aMap,
418 tr("GEOM_MEN_IMPORT"), true, fileType);
420 if (fileType.isEmpty() )
422 // Trying to detect file type
423 QFileInfo aFileInfo( fileName );
424 QString aPossibleType = (aFileInfo.extension(false)).upper() ;
426 if ( (aMap.values()).contains(aPossibleType) )
427 fileType = aPossibleType;
430 if (fileName.isEmpty() || fileType.isEmpty())
433 GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
435 SUIT_OverrideCursor wc;
437 app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
441 CORBA::String_var fileN = fileName.latin1();
442 CORBA::String_var fileT = fileType.latin1();
443 anObj = aInsOp->Import(fileN, fileT);
445 if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
446 anObj->SetName(GEOMBase::GetDefaultName(QObject::tr("GEOM_IMPORT")).latin1());
447 QString aPublishObjName =
448 GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
450 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
451 GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
452 SALOMEDS::SObject::_nil(),
456 GEOM_Displayer( stud ).Display( anObj.in() );
458 // update data model and object browser
459 getGeometryGUI()->updateObjBrowser( true );
466 SUIT_MessageBox::error1( app->desktop(),
467 QObject::tr( "GEOM_ERROR" ),
468 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
469 QObject::tr("BUT_OK") );
472 catch( const SALOME::SALOME_Exception& S_ex ) {
473 //QtCatchCorbaException(S_ex);
478 app->updateActions(); //SRN: To update a Save button in the toolbar
484 //=====================================================================================
486 // purpose : BRep, Iges, Step
487 //=====================================================================================
488 bool GEOMToolsGUI::Export()
490 SalomeApp_Application* app = getGeometryGUI()->getApp();
491 if (!app) return false;
493 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
495 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
498 _PTR(Study) aStudy = stud->studyDS();
500 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
501 if ( CORBA::is_nil( eng ) ) {
502 SUIT_MessageBox::error1( app->desktop(),
503 QObject::tr("WRN_WARNING"),
504 QObject::tr( "GEOM Engine is not started" ),
505 QObject::tr("BUT_OK") );
509 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
510 if ( aInsOp->_is_nil() )
513 // Obtain a list of available export formats
515 GEOM::string_array_var aFormats, aPatterns;
516 aInsOp->ExportTranslators( aFormats, aPatterns );
517 for ( int i = 0, n = aFormats->length(); i < n; i++ )
518 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
520 // Get selected objects
521 LightApp_SelectionMgr* sm = app->selectionMgr();
525 SALOME_ListIO selectedObjects;
526 sm->selectedObjects( selectedObjects );
528 SALOME_ListIteratorOfListIO It( selectedObjects );
529 for(;It.More();It.Next()) {
530 Handle(SALOME_InteractiveObject) IObject = It.Value();
531 Standard_Boolean found;
532 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
534 if ( !found || anObj->_is_nil() )
538 QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap,
539 tr("GEOM_MEN_EXPORT"), false, fileType);
541 // User has pressed "Cancel" --> stop the operation
542 if ( file.isEmpty() || fileType.isEmpty() )
545 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
547 SUIT_OverrideCursor wc;
549 app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
554 aInsOp->Export( anObj, file, fileType.latin1() );
556 if ( aInsOp->IsDone() )
562 SUIT_MessageBox::error1( app->desktop(),
563 QObject::tr( "GEOM_ERROR" ),
564 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
565 QObject::tr("BUT_OK") );
569 catch (const SALOME::SALOME_Exception& S_ex) {
570 //QtCatchCorbaException(S_ex);
580 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
584 for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
586 Handle(SALOME_InteractiveObject) io = it.Value();
587 if ( !io->hasEntry() )
590 QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
592 if ( parentComp.isNull() )
593 parentComp = compName;
594 else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
595 parentComp = QString::null;
603 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
606 _PTR(SComponent) comp = obj->GetFatherComponent();
608 _PTR(GenericAttribute) anAttr;
609 if ( comp->FindAttribute( anAttr, "AttributeName") ) {
610 _PTR(AttributeName) aName( anAttr );
611 return QString( aName->Value().c_str() );
618 //=====================================================================================
619 // function : RemoveObjectWithChildren
620 // purpose : to be used by OnEditDelete() method
621 //=====================================================================================
622 void GEOMToolsGUI::RemoveObjectWithChildren(_PTR(SObject) obj,
624 QPtrList<SALOME_View> views,
625 GEOM_Displayer* disp)
627 // iterate through all children of obj
628 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
629 _PTR(SObject) child (it->Value());
630 RemoveObjectWithChildren(child, aStudy, views, disp);
633 // erase object and remove it from engine
634 _PTR(GenericAttribute) anAttr;
635 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
636 _PTR(AttributeIOR) anIOR (anAttr);
638 // Delete shape in Client
639 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
640 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
642 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
643 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
644 if (!CORBA::is_nil(geomObj)) {
645 // Erase graphical object
646 SALOME_View* view = views.first();
647 for (; view; view = views.next()) {
648 disp->Erase(geomObj, true, view);
651 // Remove object from Engine
652 GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
657 //=====================================================================================
659 //=====================================================================================
663 __declspec( dllexport )
665 GEOMGUI* GetLibGUI( GeometryGUI* parent )
667 return new GEOMToolsGUI( parent );