1 // Copyright (C) 2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "DXFPlugin_GUI.h"
24 #include <SUIT_Desktop.h>
25 #include <SUIT_FileDlg.h>
26 #include <SUIT_MessageBox.h>
27 #include <SUIT_OverrideCursor.h>
28 #include <SUIT_Tools.h>
29 #include <LightApp_SelectionMgr.h>
30 #include <SalomeApp_Application.h>
31 #include <SalomeApp_Study.h>
32 #include <SALOME_ListIO.hxx>
35 #include "GeometryGUI.h"
36 #include "GEOM_Operation.h"
38 #include "GEOM_Displayer.h"
40 #include <SALOMEconfig.h>
41 #include CORBA_SERVER_HEADER(DXFPlugin)
43 //=======================================================================
44 // function : DXFPlugin_GUI()
45 // purpose : Constructor
46 //=======================================================================
47 DXFPlugin_GUI::DXFPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
51 //=======================================================================
52 // function : ~DXFPlugin_GUI
53 // purpose : Destructor
54 //=======================================================================
55 DXFPlugin_GUI::~DXFPlugin_GUI()
59 //=======================================================================
60 // function : OnGUIEvent()
62 //=======================================================================
63 bool DXFPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
66 switch ( theCommandID ) {
74 return OnGUIEvent( cmd, parent );
77 //=======================================================================
78 // function : OnGUIEvent()
80 //=======================================================================
81 bool DXFPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
85 if ( theCommandID == "Export_DXF" )
87 result = exportDXF( parent );
89 else if ( theCommandID == "Import_DXF" )
91 result = importDXF( parent );
95 getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
100 //=======================================================================
101 // function : importDXF
103 //=======================================================================
104 bool DXFPlugin_GUI::importDXF( SUIT_Desktop* parent )
106 SalomeApp_Application* app = getGeometryGUI()->getApp();
107 if ( !app ) return false;
108 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
109 if ( !study ) return false;
111 SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
112 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "DXFPluginEngine" );
113 DXFPlugin::IDXFOperations_var dxfOp = DXFPlugin::IDXFOperations::_narrow( op );
114 if ( CORBA::is_nil( dxfOp ) ) return false;
116 QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
118 tr( "IMPORT_TITLE" ),
120 if ( fileNames.count() > 0 )
122 QStringList entryList;
125 foreach( QString fileName, fileNames )
127 SUIT_OverrideCursor wc;
128 GEOM_Operation transaction( app, dxfOp.in() );
132 app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( fileName ) );
134 GEOM::ListOfGO_var result = dxfOp->ImportDXF( fileName.toUtf8().constData() );
135 if ( result->length() > 0 && dxfOp->IsDone() )
137 GEOM::GEOM_Object_var main = result[0];
138 QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
139 SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
140 SALOMEDS::SObject::_nil(),
142 publishName.toUtf8().constData() );
144 entryList.append( so->GetID() );
145 transaction.commit();
146 GEOM_Displayer( study ).Display( main.in() );
151 errors.append( QString( "%1 : %2" ).arg( fileName ).arg( dxfOp->GetErrorCode() ) );
154 catch( const SALOME::SALOME_Exception& e )
159 getGeometryGUI()->updateObjBrowser( true );
160 app->browseObjects( entryList );
162 if ( errors.count() > 0 )
164 SUIT_MessageBox::critical( parent,
166 tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
169 return fileNames.count() > 0;
172 //=======================================================================
173 // function : exportDXF
175 //=======================================================================
176 bool DXFPlugin_GUI::exportDXF( SUIT_Desktop* parent )
178 SalomeApp_Application* app = getGeometryGUI()->getApp();
179 if ( !app ) return false;
180 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
181 if ( !study ) return false;
183 SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
184 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "DXFPluginEngine" );
185 DXFPlugin::IDXFOperations_var dxfOp = DXFPlugin::IDXFOperations::_narrow( op );
186 if ( CORBA::is_nil( dxfOp ) ) return false;
188 LightApp_SelectionMgr* sm = app->selectionMgr();
189 if ( !sm ) return false;
191 SALOME_ListIO selectedObjects;
192 sm->selectedObjects( selectedObjects );
195 SALOME_ListIteratorOfListIO it( selectedObjects );
196 for ( ; it.More(); it.Next() )
198 Handle(SALOME_InteractiveObject) io = it.Value();
199 GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
201 if ( CORBA::is_nil( obj ) ) continue;
203 QString fileName = app->getFileName( false,
204 QString( io->getName() ),
206 tr( "EXPORT_TITLE" ),
209 if ( fileName.isEmpty() )
212 SUIT_OverrideCursor wc;
214 GEOM_Operation transaction( app, dxfOp.in() );
218 app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
221 dxfOp->ExportDXF( obj, fileName.toUtf8().constData() );
223 if ( dxfOp->IsDone() )
225 transaction.commit();
230 SUIT_MessageBox::critical( parent,
232 tr( "GEOM_PRP_ABORT" ) + "\n" + tr( dxfOp->GetErrorCode() ) );
236 catch ( const SALOME::SALOME_Exception& e )
246 SUIT_MessageBox::warning( parent,
248 tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
253 //=====================================================================================
255 //=====================================================================================
259 __declspec( dllexport )
261 GEOMGUI* GetLibGUI( GeometryGUI* parent )
263 return new DXFPlugin_GUI( parent );