Salome HOME
Clean-up deprecated OCCT-related code
[plugins/dxfplugin.git] / src / DXFPlugin_GUI.cxx
1 // Copyright (C) 2014-2016  OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "DXFPlugin_GUI.h"
22
23 // GUI includes
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>
33
34 // GEOM includes
35 #include "GeometryGUI.h"
36 #include "GEOM_Operation.h"
37 #include "GEOMBase.h"
38 #include "GEOM_Displayer.h"
39
40 #include <SALOMEconfig.h>
41 #include CORBA_SERVER_HEADER(DXFPlugin)
42
43 //=======================================================================
44 // function : DXFPlugin_GUI()
45 // purpose  : Constructor
46 //=======================================================================
47 DXFPlugin_GUI::DXFPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
48 {
49 }
50
51 //=======================================================================
52 // function : ~DXFPlugin_GUI
53 // purpose  : Destructor
54 //=======================================================================
55 DXFPlugin_GUI::~DXFPlugin_GUI()
56 {
57 }
58
59 //=======================================================================
60 // function : OnGUIEvent()
61 // purpose  : 
62 //=======================================================================
63 bool DXFPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
64 {
65   QString cmd;
66   switch ( theCommandID ) {
67   case 1:
68     cmd = "Export_DXF";
69   case 2:
70     cmd = "Import_DXF";
71   default:
72     break;
73   }
74   return OnGUIEvent( cmd, parent );
75 }
76
77 //=======================================================================
78 // function : OnGUIEvent()
79 // purpose  :
80 //=======================================================================
81 bool DXFPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
82 {
83   bool result = false;
84
85   if ( theCommandID == "Export_DXF" )
86   {
87     result = exportDXF( parent );
88   }
89   else if ( theCommandID == "Import_DXF" )
90   {
91     result = importDXF( parent );
92   }
93   else
94   {
95     getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
96   }
97   return result;
98 }
99
100 //=======================================================================
101 // function : importDXF
102 // purpose  :
103 //=======================================================================
104 bool DXFPlugin_GUI::importDXF( SUIT_Desktop* parent )
105 {
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;
110
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;
115
116   QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
117                                                  tr( "DXF_FILES" ),
118                                                  tr( "IMPORT_TITLE" ),
119                                                  parent );
120   if ( fileNames.count() > 0 )
121   {
122     QStringList entryList;
123     QStringList errors;
124
125     foreach( QString fileName, fileNames )
126     {
127       SUIT_OverrideCursor wc;
128       GEOM_Operation transaction( app, dxfOp.in() );
129
130       try
131       {
132         app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( fileName ) );
133         transaction.start();
134         GEOM::ListOfGO_var result = dxfOp->ImportDXF( fileName.toUtf8().constData() );
135         if ( result->length() > 0 && dxfOp->IsDone() )
136         {
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(),
141                                                   main.in(),
142                                                   publishName.toUtf8().constData() );
143
144           entryList.append( so->GetID() );
145           transaction.commit();
146           GEOM_Displayer( study ).Display( main.in() );
147         }
148         else
149         {
150           transaction.abort();
151           errors.append( QString( "%1 : %2" ).arg( fileName ).arg( dxfOp->GetErrorCode() ) );
152         }
153       }
154       catch( const SALOME::SALOME_Exception& e )
155       {
156         transaction.abort();
157       }
158     }
159     getGeometryGUI()->updateObjBrowser( true );
160     app->browseObjects( entryList );
161
162     if ( errors.count() > 0 )
163     {
164       SUIT_MessageBox::critical( parent,
165                                  tr( "GEOM_ERROR" ),
166                                  tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
167     }
168   }
169   return fileNames.count() > 0;
170 }
171
172 //=======================================================================
173 // function : exportDXF
174 // purpose  :
175 //=======================================================================
176 bool DXFPlugin_GUI::exportDXF( SUIT_Desktop* parent )
177 {
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;
182
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;
187
188   LightApp_SelectionMgr* sm = app->selectionMgr();
189   if ( !sm ) return false;
190
191   SALOME_ListIO selectedObjects;
192   sm->selectedObjects( selectedObjects );
193   bool ok = false;
194
195   SALOME_ListIteratorOfListIO it( selectedObjects );
196   for ( ; it.More(); it.Next() )
197   {
198     Handle(SALOME_InteractiveObject) io = it.Value();
199     GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
200
201     if ( CORBA::is_nil( obj ) ) continue;
202
203     QString fileName = app->getFileName( false,
204                                          QString( io->getName() ),
205                                          tr( "DXF_FILES" ),
206                                          tr( "EXPORT_TITLE" ),
207                                          parent );
208
209     if ( fileName.isEmpty() )
210       return false;
211
212     SUIT_OverrideCursor wc;
213
214     GEOM_Operation transaction( app, dxfOp.in() );
215
216     try
217     {
218       app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
219       transaction.start();
220
221       dxfOp->ExportDXF( obj, fileName.toUtf8().constData() );
222
223       if ( dxfOp->IsDone() )
224       {
225         transaction.commit();
226       }
227       else
228       {
229         transaction.abort();
230         SUIT_MessageBox::critical( parent,
231                                    tr( "GEOM_ERROR" ),
232                                    tr( "GEOM_PRP_ABORT" ) + "\n" + tr( dxfOp->GetErrorCode() ) );
233         return false;
234       }
235     }
236     catch ( const SALOME::SALOME_Exception& e )
237     {
238       transaction.abort();
239       return false;
240     }
241     ok = true;
242   }
243
244   if ( !ok )
245   {
246     SUIT_MessageBox::warning( parent,
247                               tr( "WRN_WARNING" ),
248                               tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
249   }
250   return ok;
251 }
252
253 //=====================================================================================
254 // EXPORTED METHODS
255 //=====================================================================================
256 extern "C"
257 {
258 #ifdef WIN32
259   __declspec( dllexport )
260 #endif
261   GEOMGUI* GetLibGUI( GeometryGUI* parent )
262   {
263     return new DXFPlugin_GUI( parent );
264   }
265 }