1 // Copyright (C) 2014-2022 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;
115 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( "STEPPluginEngine" );
116 STEPOpPtr stepOp = GEOM::ISTEPOperations::_narrow( op );
117 if ( stepOp.isNull() ) return false;
119 bool isCreateAssemblies = true;
120 QStringList fileNames = STEPPlugin_ImportDlg::getOpenFileNames
121 (SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
122 tr("STEP_FILES"), tr("IMPORT_TITLE"), parent, isCreateAssemblies);
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
178 (fileName.toUtf8().constData(), ignoreUnits, isCreateAssemblies);
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( 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( group, grpName.in(), main );
193 transaction.commit();
194 GEOM_Displayer().Display( main.in() );
200 errors.append( QString( "%1 : %2" ).arg( fileName ).arg( stepOp->GetErrorCode() ) );
203 catch( const SALOME::SALOME_Exception& e )
209 getGeometryGUI()->updateObjBrowser();
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 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( "STEPPluginEngine" );
234 STEPOpPtr stepOp = GEOM::ISTEPOperations::_narrow( op );
235 if ( stepOp.isNull() ) return false;
237 LightApp_SelectionMgr* sm = app->selectionMgr();
238 if ( !sm ) return false;
240 SALOME_ListIO selectedObjects;
241 sm->selectedObjects( selectedObjects );
244 SALOME_ListIteratorOfListIO it( selectedObjects );
245 for ( ; it.More(); it.Next() )
247 Handle(SALOME_InteractiveObject) io = it.Value();
248 GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
250 if ( CORBA::is_nil( obj ) ) continue;
252 GEOM::length_unit anUnit;
253 QString fileName = STEPPlugin_ExportDlg::getFileName
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(), anUnit);
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 );