Salome HOME
Implementation of DXFPLUGIN as a GEOM plugin (added files)
[plugins/dxfplugin.git] / src / DXFPlugin_ImportDlg.cxx
1 // Copyright (C) 2014  CEA/DEN, EDF R&D, 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_ImportDlg.h"
22 #include "DXFPlugin_Operations_i.hh"
23
24 // GUI includes
25 #include <SUIT_Session.h>
26 #include <SUIT_MessageBox.h>
27 #include <SUIT_Desktop.h>
28 #include <SUIT_Tools.h>
29 #include <SalomeApp_Application.h>
30
31 // GEOM includes
32 #include <GEOMBase.h>
33 #include <GEOM_Operation.h>
34
35 //=================================================================================
36 // Constructor
37 //=================================================================================
38 DXFPlugin_ImportDlg::DXFPlugin_ImportDlg( QWidget* parent )
39 : SUIT_FileDlg( parent, true, true, true ),
40   GEOMBase_Helper( dynamic_cast<SUIT_Desktop*>( parent ) )
41 {
42   setWindowTitle( tr( "DXFPLUGIN_IMPORT_TITLE" ) );
43   setAcceptMode( AcceptOpen );
44   setNameFilter( tr( "DXFPLUGIN_DXF_FILES" ) );
45   setFileMode( QFileDialog::ExistingFiles );
46 }
47
48 //=================================================================================
49 // Destructor
50 //=================================================================================
51 DXFPlugin_ImportDlg::~DXFPlugin_ImportDlg()
52 {
53   // no need to delete child widgets, Qt does it all for us
54 }
55
56 //=================================================================================
57 // function : createOperation
58 // purpose  :
59 //=================================================================================
60 GEOM::GEOM_IOperations_ptr DXFPlugin_ImportDlg::createOperation()
61 {
62   return getGeomEngine()->GetPluginOperations(getStudyId(), "DXFPluginEngine");
63 }
64
65 //=================================================================================
66 // function : isValid
67 // purpose  :
68 //=================================================================================
69 bool DXFPlugin_ImportDlg::isValid (QString& msg)
70 {
71   bool ok = true;
72   //@@ add custom validation actions here @@//
73   return ok;
74 }
75
76 //=================================================================================
77 // function : execute
78 // purpose  :
79 //=================================================================================
80 bool DXFPlugin_ImportDlg::execute( ObjectList& objects )
81 {
82   bool res = false;
83   GEOM::GEOM_Object_var anObj;
84   DXFPlugin::GEOM_IDXFPluginOperations_var anOper = DXFPlugin::GEOM_IDXFPluginOperations::_narrow( getOperation() );
85
86   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
87
88   QStringList fileNames = selectedFiles();
89   if ( fileNames.count() == 0 )
90     return false; // nothing selected, return
91
92   for( int i = 0; i < fileNames.size(); i++ ) {
93     QString aFileName = fileNames.at( i );
94     if ( aFileName.isEmpty() )
95       continue;
96     GEOM_Operation* anOp = new GEOM_Operation( app, anOper.in() );
97     try {
98       app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( aFileName.toUtf8().constData(), /*withExten=*/true ) ) );
99       anOp->start();
100       GEOM::ListOfGO_var anObj = anOper->ImportDXF( aFileName.toUtf8().constData() );
101       if( anObj->length() > 0 && anOper->IsDone() ) {
102         GEOM::GEOM_Object_ptr anObjPtr = anObj[0]._retn();
103         objects.push_back( anObjPtr );
104         QString aPublishObjName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileNames.at( i ),
105                                                             /*withExten=*/true ) );
106         anObjPtr->SetName( aPublishObjName.toStdString().c_str() );
107         anOp->commit();
108       }
109       else {
110         anOp->abort();
111         SUIT_MessageBox::critical( app->desktop(),
112                                    QObject::tr("GEOM_ERROR"),
113                                    QObject::tr("GEOM_PRP_ABORT") + "\n" + QObject::tr( anOper->GetErrorCode() ) );
114       }
115     }
116     catch( const SALOME::SALOME_Exception& S_ex ) {
117       anOp->abort();
118     }
119   }
120   return true;
121 }
122
123 //=================================================================================
124 // function : accept
125 // purpose  :
126 //=================================================================================
127 void DXFPlugin_ImportDlg::accept()
128 {
129   SUIT_FileDlg::accept();
130   onAccept();
131 }
132
133 //=================================================================================
134 // function : getObjectName
135 // purpose  :
136 //=================================================================================
137 QString DXFPlugin_ImportDlg::getObjectName( GEOM::GEOM_Object_ptr object ) const
138 {
139   if( object->_is_nil() )
140     return QString::null;
141   return object->GetName();
142 }