1 // Copyright (C) 2014-2015 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 "STEPPlugin_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"
39 #include "GEOM_GenericObjPtr.h"
41 #include <SALOMEconfig.h>
42 #include CORBA_SERVER_HEADER(STEPPlugin)
44 typedef GEOM::GenericObjPtr<GEOM::ISTEPOperations> STEPOpPtr;
46 //=======================================================================
47 // function : STEPPlugin_GUI()
48 // purpose : Constructor
49 //=======================================================================
50 STEPPlugin_GUI::STEPPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
54 //=======================================================================
55 // function : ~STEPPlugin_GUI
56 // purpose : Destructor
57 //=======================================================================
58 STEPPlugin_GUI::~STEPPlugin_GUI()
62 //=======================================================================
63 // function : OnGUIEvent()
65 //=======================================================================
66 bool STEPPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
69 switch ( theCommandID ) {
71 cmd = "Export_STEP"; break;
73 cmd = "Import_STEP"; break;
77 return OnGUIEvent( cmd, parent );
80 //=======================================================================
81 // function : OnGUIEvent()
83 //=======================================================================
84 bool STEPPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
88 if ( theCommandID == "Export_STEP" )
90 result = exportSTEP( parent );
92 else if ( theCommandID == "Import_STEP" )
94 result = importSTEP( parent );
98 getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
104 //=======================================================================
105 // function : importSTEP
107 //=======================================================================
108 bool STEPPlugin_GUI::importSTEP( SUIT_Desktop* parent )
110 SalomeApp_Application* app = getGeometryGUI()->getApp();
111 if ( !app ) return false;
112 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
113 if ( !study ) return false;
115 SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
116 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STEPPluginEngine" );
117 STEPOpPtr stepOp = GEOM::ISTEPOperations::_narrow( op );
118 if ( stepOp.isNull() ) return false;
120 QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
122 tr( "IMPORT_TITLE" ),
124 if ( fileNames.count() > 0 )
126 QStringList entryList;
128 SUIT_MessageBox::StandardButton stepAnswer = SUIT_MessageBox::NoButton;
130 for ( int i = 0; i < fileNames.count(); i++ )
132 QString fileName = fileNames.at( i );
133 SUIT_OverrideCursor wc;
134 GEOM_Operation transaction( app, stepOp.get() );
135 bool ignoreUnits = false;
139 app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( fileName, true ) ) );
142 CORBA::String_var units = stepOp->ReadValue( fileName.toUtf8().constData(), "LEN_UNITS" );
143 QString unitsStr( units.in() );
144 bool unitsOK = unitsStr.isEmpty() || unitsStr == "M" || unitsStr.toLower() == "metre";
148 if( stepAnswer == SUIT_MessageBox::NoToAll )
152 else if( stepAnswer != SUIT_MessageBox::YesToAll )
154 SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No | SUIT_MessageBox::Cancel;
155 if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
156 stepAnswer = SUIT_MessageBox::question( parent,
158 tr( "SCALE_DIMENSIONS" ).arg( unitsStr ),
160 SUIT_MessageBox::No );
161 switch ( stepAnswer )
163 case SUIT_MessageBox::Cancel:
164 return true; // cancel (break) import operation
165 case SUIT_MessageBox::Yes:
166 case SUIT_MessageBox::YesToAll:
167 break; // scaling is confirmed
168 case SUIT_MessageBox::No:
169 case SUIT_MessageBox::NoAll:
170 ignoreUnits = true; // scaling is rejected
177 GEOM::ListOfGO_var result = stepOp->ImportSTEP( fileName.toUtf8().constData(), ignoreUnits );
178 if ( result->length() > 0 && stepOp->IsDone() )
180 GEOM::GEOM_Object_var main = result[0];
181 QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
182 SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
183 SALOMEDS::SObject::_nil(),
185 publishName.toUtf8().constData() );
187 entryList.append( so->GetID() );
188 for ( int i = 1, n = result->length(); i < n; i++ ) {
189 GEOM::GEOM_Object_ptr group = result[i];
190 CORBA::String_var grpName = group->GetName();
191 GeometryGUI::GetGeomGen()->AddInStudy( dsStudy, group, grpName.in(), main );
193 transaction.commit();
194 GEOM_Displayer( study ).Display( main.in() );
200 errors.append( QString( "%1 : %2" ).arg( fileName ).arg( stepOp->GetErrorCode() ) );
203 catch( const SALOME::SALOME_Exception& e )
209 getGeometryGUI()->updateObjBrowser( true );
210 app->browseObjects( entryList );
212 if ( errors.count() > 0 )
214 SUIT_MessageBox::critical( parent,
216 tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
219 return fileNames.count() > 0;
222 //=======================================================================
223 // function : exportSTEP
225 //=======================================================================
226 bool STEPPlugin_GUI::exportSTEP( SUIT_Desktop* parent )
228 SalomeApp_Application* app = getGeometryGUI()->getApp();
229 if ( !app ) return false;
230 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
231 if ( !study ) return false;
233 SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
234 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STEPPluginEngine" );
235 STEPOpPtr stepOp = GEOM::ISTEPOperations::_narrow( op );
236 if ( stepOp.isNull() ) return false;
238 LightApp_SelectionMgr* sm = app->selectionMgr();
239 if ( !sm ) return false;
241 SALOME_ListIO selectedObjects;
242 sm->selectedObjects( selectedObjects );
245 SALOME_ListIteratorOfListIO it( selectedObjects );
246 for ( ; it.More(); it.Next() )
248 Handle(SALOME_InteractiveObject) io = it.Value();
249 GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
251 if ( CORBA::is_nil( obj ) ) continue;
253 QString fileName = app->getFileName( false,
254 QString( io->getName() ),
256 tr( "EXPORT_TITLE" ),
259 if ( fileName.isEmpty() )
262 SUIT_OverrideCursor wc;
264 GEOM_Operation transaction( app, stepOp.get() );
268 app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
271 stepOp->ExportSTEP( obj, fileName.toUtf8().constData() );
273 if ( stepOp->IsDone() )
275 transaction.commit();
280 SUIT_MessageBox::critical( parent,
282 tr( "GEOM_PRP_ABORT" ) + "\n" + tr( stepOp->GetErrorCode() ) );
286 catch ( const SALOME::SALOME_Exception& e )
296 SUIT_MessageBox::warning( parent,
298 tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
303 //=====================================================================================
305 //=====================================================================================
309 __declspec( dllexport )
311 GEOMGUI* GetLibGUI( GeometryGUI* parent )
313 return new STEPPlugin_GUI( parent );