1 // Copyright (C) 2014-2016 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"
40 #include "STEPPlugin_ExportDlg.h"
41 #include "STEPPlugin_ImportDlg.h"
43 #include <SALOMEconfig.h>
44 #include CORBA_SERVER_HEADER(STEPPlugin)
46 typedef GEOM::GenericObjPtr<GEOM::ISTEPOperations> STEPOpPtr;
48 //=======================================================================
49 // function : STEPPlugin_GUI()
50 // purpose : Constructor
51 //=======================================================================
52 STEPPlugin_GUI::STEPPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
56 //=======================================================================
57 // function : ~STEPPlugin_GUI
58 // purpose : Destructor
59 //=======================================================================
60 STEPPlugin_GUI::~STEPPlugin_GUI()
64 //=======================================================================
65 // function : OnGUIEvent()
67 //=======================================================================
68 bool STEPPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
71 switch ( theCommandID ) {
73 cmd = "Export_STEP"; break;
75 cmd = "Import_STEP"; break;
79 return OnGUIEvent( cmd, parent );
82 //=======================================================================
83 // function : OnGUIEvent()
85 //=======================================================================
86 bool STEPPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
90 if ( theCommandID == "Export_STEP" )
92 result = exportSTEP( parent );
94 else if ( theCommandID == "Import_STEP" )
96 result = importSTEP( parent );
100 getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
106 //=======================================================================
107 // function : importSTEP
109 //=======================================================================
110 bool STEPPlugin_GUI::importSTEP( SUIT_Desktop* parent )
112 SalomeApp_Application* app = getGeometryGUI()->getApp();
113 if ( !app ) return false;
114 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
115 if ( !study ) return false;
117 SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
118 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STEPPluginEngine" );
119 STEPOpPtr stepOp = GEOM::ISTEPOperations::_narrow( op );
120 if ( stepOp.isNull() ) return false;
122 bool isCreateAssemblies = true;
123 QStringList fileNames = STEPPlugin_ImportDlg::getOpenFileNames
124 (SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
125 tr("STEP_FILES"), tr("IMPORT_TITLE"), parent, isCreateAssemblies);
127 if ( fileNames.count() > 0 )
129 QStringList entryList;
131 SUIT_MessageBox::StandardButton stepAnswer = SUIT_MessageBox::NoButton;
133 for ( int i = 0; i < fileNames.count(); i++ )
135 QString fileName = fileNames.at( i );
136 SUIT_OverrideCursor wc;
137 GEOM_Operation transaction( app, stepOp.get() );
138 bool ignoreUnits = false;
142 app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( fileName, true ) ) );
145 CORBA::String_var units = stepOp->ReadValue( fileName.toUtf8().constData(), "LEN_UNITS" );
146 QString unitsStr( units.in() );
147 bool unitsOK = unitsStr.isEmpty() || unitsStr == "M" || unitsStr.toLower() == "metre";
151 if( stepAnswer == SUIT_MessageBox::NoToAll )
155 else if( stepAnswer != SUIT_MessageBox::YesToAll )
157 SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No | SUIT_MessageBox::Cancel;
158 if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
159 stepAnswer = SUIT_MessageBox::question( parent,
161 tr( "SCALE_DIMENSIONS" ).arg( unitsStr ),
163 SUIT_MessageBox::No );
164 switch ( stepAnswer )
166 case SUIT_MessageBox::Cancel:
167 return true; // cancel (break) import operation
168 case SUIT_MessageBox::Yes:
169 case SUIT_MessageBox::YesToAll:
170 break; // scaling is confirmed
171 case SUIT_MessageBox::No:
172 case SUIT_MessageBox::NoAll:
173 ignoreUnits = true; // scaling is rejected
180 GEOM::ListOfGO_var result = stepOp->ImportSTEP
181 (fileName.toUtf8().constData(), ignoreUnits, isCreateAssemblies);
182 if ( result->length() > 0 && stepOp->IsDone() )
184 GEOM::GEOM_Object_var main = result[0];
185 QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
186 SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
187 SALOMEDS::SObject::_nil(),
189 publishName.toUtf8().constData() );
191 entryList.append( so->GetID() );
192 for ( int i = 1, n = result->length(); i < n; i++ ) {
193 GEOM::GEOM_Object_ptr group = result[i];
194 CORBA::String_var grpName = group->GetName();
195 GeometryGUI::GetGeomGen()->AddInStudy( dsStudy, group, grpName.in(), main );
197 transaction.commit();
198 GEOM_Displayer( study ).Display( main.in() );
204 errors.append( QString( "%1 : %2" ).arg( fileName ).arg( stepOp->GetErrorCode() ) );
207 catch( const SALOME::SALOME_Exception& e )
213 getGeometryGUI()->updateObjBrowser( true );
214 app->browseObjects( entryList );
216 if ( errors.count() > 0 )
218 SUIT_MessageBox::critical( parent,
220 tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
223 return fileNames.count() > 0;
226 //=======================================================================
227 // function : exportSTEP
229 //=======================================================================
230 bool STEPPlugin_GUI::exportSTEP( SUIT_Desktop* parent )
232 SalomeApp_Application* app = getGeometryGUI()->getApp();
233 if ( !app ) return false;
234 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
235 if ( !study ) return false;
237 SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
238 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STEPPluginEngine" );
239 STEPOpPtr stepOp = GEOM::ISTEPOperations::_narrow( op );
240 if ( stepOp.isNull() ) return false;
242 LightApp_SelectionMgr* sm = app->selectionMgr();
243 if ( !sm ) return false;
245 SALOME_ListIO selectedObjects;
246 sm->selectedObjects( selectedObjects );
249 SALOME_ListIteratorOfListIO it( selectedObjects );
250 for ( ; it.More(); it.Next() )
252 Handle(SALOME_InteractiveObject) io = it.Value();
253 GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
255 if ( CORBA::is_nil( obj ) ) continue;
257 GEOM::length_unit anUnit;
258 QString fileName = STEPPlugin_ExportDlg::getFileName
259 (QString( io->getName() ),
261 tr( "EXPORT_TITLE" ),
264 if ( fileName.isEmpty() )
267 SUIT_OverrideCursor wc;
269 GEOM_Operation transaction( app, stepOp.get() );
273 app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
276 stepOp->ExportSTEP( obj, fileName.toUtf8().constData(), anUnit);
278 if ( stepOp->IsDone() )
280 transaction.commit();
285 SUIT_MessageBox::critical( parent,
287 tr( "GEOM_PRP_ABORT" ) + "\n" + tr( stepOp->GetErrorCode() ) );
291 catch ( const SALOME::SALOME_Exception& e )
301 SUIT_MessageBox::warning( parent,
303 tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
308 //=====================================================================================
310 //=====================================================================================
314 __declspec( dllexport )
316 GEOMGUI* GetLibGUI( GeometryGUI* parent )
318 return new STEPPlugin_GUI( parent );