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"
40 #include <SALOMEconfig.h>
41 #include CORBA_SERVER_HEADER(STEPPlugin)
43 //=======================================================================
44 // function : STEPPlugin_GUI()
45 // purpose : Constructor
46 //=======================================================================
47 STEPPlugin_GUI::STEPPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
51 //=======================================================================
52 // function : ~STEPPlugin_GUI
53 // purpose : Destructor
54 //=======================================================================
55 STEPPlugin_GUI::~STEPPlugin_GUI()
59 //=======================================================================
60 // function : OnGUIEvent()
62 //=======================================================================
63 bool STEPPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
66 switch ( theCommandID ) {
68 cmd = "Export_STEP"; break;
70 cmd = "Import_STEP"; break;
74 return OnGUIEvent( cmd, parent );
77 //=======================================================================
78 // function : OnGUIEvent()
80 //=======================================================================
81 bool STEPPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
85 if ( theCommandID == "Export_STEP" )
87 result = exportSTEP( parent );
89 else if ( theCommandID == "Import_STEP" )
91 result = importSTEP( parent );
95 getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
101 //=======================================================================
102 // function : importSTEP
104 //=======================================================================
105 bool STEPPlugin_GUI::importSTEP( SUIT_Desktop* parent )
107 SalomeApp_Application* app = getGeometryGUI()->getApp();
108 if ( !app ) return false;
109 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
110 if ( !study ) return false;
112 SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
113 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STEPPluginEngine" );
114 GEOM::ISTEPOperations_var stepOp = GEOM::ISTEPOperations::_narrow( op );
115 if ( CORBA::is_nil( stepOp ) ) return false;
117 QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
119 tr( "IMPORT_TITLE" ),
121 if ( fileNames.count() > 0 )
123 QStringList entryList;
125 SUIT_MessageBox::StandardButton stepAnswer = SUIT_MessageBox::NoButton;
127 for ( int i = 0; i < fileNames.count(); i++ )
129 QString fileName = fileNames.at( i );
130 SUIT_OverrideCursor wc;
131 GEOM_Operation transaction( app, stepOp.in() );
132 bool ignoreUnits = false;
136 app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( fileName, true ) ) );
139 CORBA::String_var units = stepOp->ReadValue( fileName.toUtf8().constData(), "LEN_UNITS" );
140 QString unitsStr( units.in() );
141 bool unitsOK = unitsStr.isEmpty() || unitsStr == "M" || unitsStr.toLower() == "metre";
145 if( stepAnswer == SUIT_MessageBox::NoToAll )
149 else if( stepAnswer != SUIT_MessageBox::YesToAll )
151 SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No | SUIT_MessageBox::Cancel;
152 if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
153 stepAnswer = SUIT_MessageBox::question( parent,
155 tr( "SCALE_DIMENSIONS" ).arg( unitsStr ),
157 SUIT_MessageBox::No );
158 switch ( stepAnswer )
160 case SUIT_MessageBox::Cancel:
161 return true; // cancel (break) import operation
162 case SUIT_MessageBox::Yes:
163 case SUIT_MessageBox::YesToAll:
164 break; // scaling is confirmed
165 case SUIT_MessageBox::No:
166 case SUIT_MessageBox::NoAll:
167 ignoreUnits = true; // scaling is rejected
174 GEOM::ListOfGO_var result = stepOp->ImportSTEP( fileName.toUtf8().constData(), ignoreUnits );
175 if ( result->length() > 0 && stepOp->IsDone() )
177 GEOM::GEOM_Object_var main = result[0];
178 QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
179 SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
180 SALOMEDS::SObject::_nil(),
182 publishName.toUtf8().constData() );
184 entryList.append( so->GetID() );
185 for ( int i = 1, n = result->length(); i < n; i++ ) {
186 GEOM::GEOM_Object_ptr group = result[i];
187 CORBA::String_var grpName = group->GetName();
188 GeometryGUI::GetGeomGen()->AddInStudy( dsStudy, group, grpName.in(), main );
190 transaction.commit();
191 GEOM_Displayer( study ).Display( main.in() );
196 errors.append( QString( "%1 : %2" ).arg( fileName ).arg( stepOp->GetErrorCode() ) );
199 catch( const SALOME::SALOME_Exception& e )
205 getGeometryGUI()->updateObjBrowser( true );
206 app->browseObjects( entryList );
208 if ( errors.count() > 0 )
210 SUIT_MessageBox::critical( parent,
212 tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
215 return fileNames.count() > 0;
218 //=======================================================================
219 // function : exportSTEP
221 //=======================================================================
222 bool STEPPlugin_GUI::exportSTEP( SUIT_Desktop* parent )
224 SalomeApp_Application* app = getGeometryGUI()->getApp();
225 if ( !app ) return false;
226 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
227 if ( !study ) return false;
229 SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
230 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STEPPluginEngine" );
231 GEOM::ISTEPOperations_var stepOp = GEOM::ISTEPOperations::_narrow( op );
232 if ( CORBA::is_nil( stepOp ) ) return false;
234 LightApp_SelectionMgr* sm = app->selectionMgr();
235 if ( !sm ) return false;
237 SALOME_ListIO selectedObjects;
238 sm->selectedObjects( selectedObjects );
241 SALOME_ListIteratorOfListIO it( selectedObjects );
242 for ( ; it.More(); it.Next() )
244 Handle(SALOME_InteractiveObject) io = it.Value();
245 GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
247 if ( CORBA::is_nil( obj ) ) continue;
249 QString fileName = app->getFileName( false,
250 QString( io->getName() ),
252 tr( "EXPORT_TITLE" ),
255 if ( fileName.isEmpty() )
258 SUIT_OverrideCursor wc;
260 GEOM_Operation transaction( app, stepOp.in() );
264 app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
267 stepOp->ExportSTEP( obj, fileName.toUtf8().constData() );
269 if ( stepOp->IsDone() )
271 transaction.commit();
276 SUIT_MessageBox::critical( parent,
278 tr( "GEOM_PRP_ABORT" ) + "\n" + tr( stepOp->GetErrorCode() ) );
282 catch ( const SALOME::SALOME_Exception& e )
292 SUIT_MessageBox::warning( parent,
294 tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
299 //=====================================================================================
301 //=====================================================================================
305 __declspec( dllexport )
307 GEOMGUI* GetLibGUI( GeometryGUI* parent )
309 return new STEPPlugin_GUI( parent );