Salome HOME
Implementation of ACISPLUGIN as a GEOM plugin (added files)
[plugins/acisplugin.git] / src / ACISPlugin_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 "ACISPlugin_ImportDlg.h"
22 #include "ACISPlugin_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 #include <SalomeApp_Study.h>
31
32 // GEOM includes
33 #include <GeometryGUI.h>
34 #include <GEOMBase.h>
35 #include <GEOM_Operation.h>
36
37 //=================================================================================
38 // Constructor
39 //=================================================================================
40 ACISPlugin_ImportDlg::ACISPlugin_ImportDlg( QWidget* parent )
41 : SUIT_FileDlg( parent, true, true, true ),
42   GEOMBase_Helper( dynamic_cast<SUIT_Desktop*>( parent ) )
43 {
44   setWindowTitle( tr( "ACISPLUGIN_IMPORT_TITLE" ) );
45   setAcceptMode( AcceptOpen );
46   setNameFilter( tr( "ACISPLUGIN_ACIS_FILES" ) );
47   setFileMode( QFileDialog::ExistingFiles );
48 }
49
50 //=================================================================================
51 // Destructor
52 //=================================================================================
53 ACISPlugin_ImportDlg::~ACISPlugin_ImportDlg()
54 {
55   // no need to delete child widgets, Qt does it all for us
56 }
57
58 //=================================================================================
59 // function : createOperation
60 // purpose  :
61 //=================================================================================
62 GEOM::GEOM_IOperations_ptr ACISPlugin_ImportDlg::createOperation()
63 {
64   return getGeomEngine()->GetPluginOperations(getStudyId(), "ACISPluginEngine");
65 }
66
67 //=================================================================================
68 // function : isValid
69 // purpose  :
70 //=================================================================================
71 bool ACISPlugin_ImportDlg::isValid (QString& msg)
72 {
73   bool ok = true;
74   //@@ add custom validation actions here @@//
75   return ok;
76 }
77
78 //=================================================================================
79 // function : execute
80 // purpose  :
81 //=================================================================================
82 bool ACISPlugin_ImportDlg::execute( ObjectList& objects )
83 {
84   bool res = false;
85   GEOM::GEOM_Object_var anObj;
86   ACISPlugin::GEOM_IACISPluginOperations_var anOper = ACISPlugin::GEOM_IACISPluginOperations::_narrow( getOperation() );
87
88   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
89   _PTR(Study) aStudy = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() )->studyDS();
90   SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy );
91
92   QStringList fileNames = selectedFiles();
93   if ( fileNames.count() == 0 )
94     return false; // nothing selected, return
95
96   SUIT_MessageBox::StandardButton acisAnswer = SUIT_MessageBox::NoButton;
97
98   for( int i = 0; i < fileNames.size(); i++ ) {
99     QString aFileName = fileNames.at( i );
100     if ( aFileName.isEmpty() )
101       continue;
102     GEOM_Operation* anOp = new GEOM_Operation( app, anOper.in() );
103     try {
104       app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( aFileName.toUtf8().constData(), /*withExten=*/true ) ) );
105       anOp->start();
106
107       if ( acisAnswer != SUIT_MessageBox::YesToAll && acisAnswer != SUIT_MessageBox::NoToAll ) {
108         SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No;
109         if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
110         acisAnswer = SUIT_MessageBox::question( app->desktop(),
111                                                 "Question",//tr("WRN_WARNING"),
112                                                 tr("GEOM_PUBLISH_NAMED_SHAPES"),
113                                                 btns | SUIT_MessageBox::Cancel,
114                                                 SUIT_MessageBox::No );
115         if ( acisAnswer == SUIT_MessageBox::Cancel ) {
116           anOper->SetErrorCode(OK);
117           return true; // cancel (break) import operation
118         }
119       }
120
121       GEOM::ListOfGO_var anObj = anOper->ImportACIS( aFileName.toUtf8().constData() );
122
123       if ( anObj->length() > 0 && anOper->IsDone() ) {
124         GEOM::GEOM_Object_ptr anObjPtr = anObj[0]._retn();
125         objects.push_back( anObjPtr );
126         QString aPublishObjName =
127           GEOMBase::GetDefaultName( SUIT_Tools::file( aFileName, /*withExten=*/true ) );
128         anObjPtr->SetName( aPublishObjName.toStdString().c_str() );
129
130         SALOMEDS::SObject_var aSO =
131           GeometryGUI::GetGeomGen()->PublishInStudy( aDSStudy,
132                                                      SALOMEDS::SObject::_nil(),
133                                                      anObjPtr,
134                                                      aPublishObjName.toLatin1().constData() );
135         if ( acisAnswer == SUIT_MessageBox::Yes || acisAnswer == SUIT_MessageBox::YesToAll )
136           GeometryGUI::GetGeomGen()->PublishNamedShapesInStudy( aDSStudy, anObjPtr );
137
138         anOp->commit();
139       }
140       else {
141         anOp->abort();
142         SUIT_MessageBox::critical( app->desktop(),
143                                    QObject::tr("GEOM_ERROR"),
144                                    QObject::tr("GEOM_PRP_ABORT") + "\n" + QObject::tr( anOper->GetErrorCode() ) );
145       }
146     }
147     catch( const SALOME::SALOME_Exception& S_ex ) {
148       anOp->abort();
149     }
150   }
151   return true;
152 }
153
154 //=================================================================================
155 // function : accept
156 // purpose  :
157 //=================================================================================
158 void ACISPlugin_ImportDlg::accept()
159 {
160   SUIT_FileDlg::accept();
161   onAccept( false );
162 }
163
164 //=================================================================================
165 // function : getObjectName
166 // purpose  :
167 //=================================================================================
168 QString ACISPlugin_ImportDlg::getObjectName( GEOM::GEOM_Object_ptr object ) const
169 {
170   if( object->_is_nil() )
171     return QString::null;
172   return object->GetName();
173 }