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"
40 #include "STEPPlugin_ExportDlg.h"
42 #include <SALOMEconfig.h>
43 #include CORBA_SERVER_HEADER(STEPPlugin)
45 typedef GEOM::GenericObjPtr<GEOM::ISTEPOperations> STEPOpPtr;
47 //=======================================================================
48 // function : STEPPlugin_GUI()
49 // purpose : Constructor
50 //=======================================================================
51 STEPPlugin_GUI::STEPPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
55 //=======================================================================
56 // function : ~STEPPlugin_GUI
57 // purpose : Destructor
58 //=======================================================================
59 STEPPlugin_GUI::~STEPPlugin_GUI()
63 //=======================================================================
64 // function : OnGUIEvent()
66 //=======================================================================
67 bool STEPPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
70 switch ( theCommandID ) {
72 cmd = "Export_STEP"; break;
74 cmd = "Import_STEP"; break;
78 return OnGUIEvent( cmd, parent );
81 //=======================================================================
82 // function : OnGUIEvent()
84 //=======================================================================
85 bool STEPPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
89 if ( theCommandID == "Export_STEP" )
91 result = exportSTEP( parent );
93 else if ( theCommandID == "Import_STEP" )
95 result = importSTEP( parent );
99 getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
105 //=======================================================================
106 // function : importSTEP
108 //=======================================================================
109 bool STEPPlugin_GUI::importSTEP( SUIT_Desktop* parent )
111 SalomeApp_Application* app = getGeometryGUI()->getApp();
112 if ( !app ) return false;
113 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
114 if ( !study ) return false;
116 SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
117 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STEPPluginEngine" );
118 STEPOpPtr stepOp = GEOM::ISTEPOperations::_narrow( op );
119 if ( stepOp.isNull() ) return false;
121 QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
123 tr( "IMPORT_TITLE" ),
125 if ( fileNames.count() > 0 )
127 QStringList entryList;
129 SUIT_MessageBox::StandardButton stepAnswer = SUIT_MessageBox::NoButton;
131 for ( int i = 0; i < fileNames.count(); i++ )
133 QString fileName = fileNames.at( i );
134 SUIT_OverrideCursor wc;
135 GEOM_Operation transaction( app, stepOp.get() );
136 bool ignoreUnits = false;
140 app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( fileName, true ) ) );
143 CORBA::String_var units = stepOp->ReadValue( fileName.toUtf8().constData(), "LEN_UNITS" );
144 QString unitsStr( units.in() );
145 bool unitsOK = unitsStr.isEmpty() || unitsStr == "M" || unitsStr.toLower() == "metre";
149 if( stepAnswer == SUIT_MessageBox::NoToAll )
153 else if( stepAnswer != SUIT_MessageBox::YesToAll )
155 SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No | SUIT_MessageBox::Cancel;
156 if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
157 stepAnswer = SUIT_MessageBox::question( parent,
159 tr( "SCALE_DIMENSIONS" ).arg( unitsStr ),
161 SUIT_MessageBox::No );
162 switch ( stepAnswer )
164 case SUIT_MessageBox::Cancel:
165 return true; // cancel (break) import operation
166 case SUIT_MessageBox::Yes:
167 case SUIT_MessageBox::YesToAll:
168 break; // scaling is confirmed
169 case SUIT_MessageBox::No:
170 case SUIT_MessageBox::NoAll:
171 ignoreUnits = true; // scaling is rejected
178 GEOM::ListOfGO_var result = stepOp->ImportSTEP( fileName.toUtf8().constData(), ignoreUnits );
179 if ( result->length() > 0 && stepOp->IsDone() )
181 GEOM::GEOM_Object_var main = result[0];
182 QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
183 SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
184 SALOMEDS::SObject::_nil(),
186 publishName.toUtf8().constData() );
188 entryList.append( so->GetID() );
189 for ( int i = 1, n = result->length(); i < n; i++ ) {
190 GEOM::GEOM_Object_ptr group = result[i];
191 CORBA::String_var grpName = group->GetName();
192 GeometryGUI::GetGeomGen()->AddInStudy( dsStudy, group, grpName.in(), main );
194 transaction.commit();
195 GEOM_Displayer( study ).Display( main.in() );
201 errors.append( QString( "%1 : %2" ).arg( fileName ).arg( stepOp->GetErrorCode() ) );
204 catch( const SALOME::SALOME_Exception& e )
210 getGeometryGUI()->updateObjBrowser( true );
211 app->browseObjects( entryList );
213 if ( errors.count() > 0 )
215 SUIT_MessageBox::critical( parent,
217 tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
220 return fileNames.count() > 0;
223 //=======================================================================
224 // function : exportSTEP
226 //=======================================================================
227 bool STEPPlugin_GUI::exportSTEP( SUIT_Desktop* parent )
229 SalomeApp_Application* app = getGeometryGUI()->getApp();
230 if ( !app ) return false;
231 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
232 if ( !study ) return false;
234 SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
235 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STEPPluginEngine" );
236 STEPOpPtr stepOp = GEOM::ISTEPOperations::_narrow( op );
237 if ( stepOp.isNull() ) return false;
239 LightApp_SelectionMgr* sm = app->selectionMgr();
240 if ( !sm ) return false;
242 SALOME_ListIO selectedObjects;
243 sm->selectedObjects( selectedObjects );
246 SALOME_ListIteratorOfListIO it( selectedObjects );
247 for ( ; it.More(); it.Next() )
249 Handle(SALOME_InteractiveObject) io = it.Value();
250 GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
252 if ( CORBA::is_nil( obj ) ) continue;
254 GEOM::length_unit anUnit;
255 QString fileName = STEPPlugin_ExportDlg::getFileName
256 (QString( io->getName() ),
258 tr( "EXPORT_TITLE" ),
261 if ( fileName.isEmpty() )
264 SUIT_OverrideCursor wc;
266 GEOM_Operation transaction( app, stepOp.get() );
270 app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
273 stepOp->ExportSTEP( obj, fileName.toUtf8().constData(), anUnit);
275 if ( stepOp->IsDone() )
277 transaction.commit();
282 SUIT_MessageBox::critical( parent,
284 tr( "GEOM_PRP_ABORT" ) + "\n" + tr( stepOp->GetErrorCode() ) );
288 catch ( const SALOME::SALOME_Exception& e )
298 SUIT_MessageBox::warning( parent,
300 tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
305 //=====================================================================================
307 //=====================================================================================
311 __declspec( dllexport )
313 GEOMGUI* GetLibGUI( GeometryGUI* parent )
315 return new STEPPlugin_GUI( parent );