1 // Copyright (C) 2014-2019 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 "STLPlugin_GUI.h"
22 #include "STLPlugin_ExportDlg.h"
25 #include <SUIT_Desktop.h>
26 #include <SUIT_FileDlg.h>
27 #include <SUIT_MessageBox.h>
28 #include <SUIT_OverrideCursor.h>
29 #include <SUIT_Tools.h>
30 #include <LightApp_SelectionMgr.h>
31 #include <SalomeApp_Application.h>
32 #include <SalomeApp_Study.h>
33 #include <SALOME_ListIO.hxx>
36 #include "GeometryGUI.h"
37 #include "GEOM_Operation.h"
39 #include "GEOM_Displayer.h"
40 #include "GEOM_GenericObjPtr.h"
42 #include <SALOMEconfig.h>
43 #include CORBA_SERVER_HEADER(STLPlugin)
45 typedef GEOM::GenericObjPtr<GEOM::ISTLOperations> STLOpPtr;
47 //=======================================================================
48 // function : STLPlugin_GUI()
49 // purpose : Constructor
50 //=======================================================================
51 STLPlugin_GUI::STLPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
55 //=======================================================================
56 // function : ~STLPlugin_GUI
57 // purpose : Destructor
58 //=======================================================================
59 STLPlugin_GUI::~STLPlugin_GUI()
63 //=======================================================================
64 // function : OnGUIEvent()
66 //=======================================================================
67 bool STLPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
70 switch ( theCommandID ) {
72 cmd = "Export_STL"; break;
74 cmd = "Import_STL"; break;
78 return OnGUIEvent( cmd, parent );
81 //=======================================================================
82 // function : OnGUIEvent()
84 //=======================================================================
85 bool STLPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
89 if( theCommandID == "Export_STL" ) {
90 result = exportSTL( parent );
92 else if (theCommandID == "Import_STL") {
93 result = importSTL( parent );
96 getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
99 return true; // ?????????
102 //=======================================================================
103 // function : importSTL
105 //=======================================================================
106 bool STLPlugin_GUI::importSTL( SUIT_Desktop* parent )
108 SalomeApp_Application* app = getGeometryGUI()->getApp();
109 if ( !app ) return false;
111 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( "STLPluginEngine" );
112 STLOpPtr stlOp = GEOM::ISTLOperations::_narrow( op );
113 if ( stlOp.isNull() ) return false;
115 QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
117 tr( "IMPORT_TITLE" ),
119 if ( fileNames.count() > 0 )
121 QStringList entryList;
124 foreach( QString fileName, fileNames )
126 SUIT_OverrideCursor wc;
127 GEOM_Operation transaction( app, stlOp.get() );
131 app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( fileName ) );
133 GEOM::ListOfGO_var result = stlOp->ImportSTL( fileName.toUtf8().constData() );
134 if ( result->length() > 0 && stlOp->IsDone() )
136 GEOM::GEOM_Object_var main = result[0];
137 QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
138 SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( SALOMEDS::SObject::_nil(),
140 publishName.toUtf8().constData() );
142 entryList.append( so->GetID() );
143 transaction.commit();
144 GEOM_Displayer().Display( main.in() );
150 errors.append( QString( "%1 : %2" ).arg( fileName ).arg( stlOp->GetErrorCode() ) );
153 catch( const SALOME::SALOME_Exception& e )
158 getGeometryGUI()->updateObjBrowser();
159 app->browseObjects( entryList );
161 if ( errors.count() > 0 )
163 SUIT_MessageBox::critical( parent,
165 tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
168 return fileNames.count() > 0;
171 //=======================================================================
172 // function : exportSTL
174 //=======================================================================
175 bool STLPlugin_GUI::exportSTL( SUIT_Desktop* parent )
177 SalomeApp_Application* app = getGeometryGUI()->getApp();
178 if ( !app ) return false;
180 GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( "STLPluginEngine" );
181 STLOpPtr stlOp = GEOM::ISTLOperations::_narrow( op );
182 if ( stlOp.isNull() ) return false;
184 LightApp_SelectionMgr* sm = app->selectionMgr();
185 if ( !sm ) return false;
187 SALOME_ListIO selectedObjects;
188 sm->selectedObjects( selectedObjects );
191 SALOME_ListIteratorOfListIO it( selectedObjects );
192 for ( ; it.More(); it.Next() )
194 Handle(SALOME_InteractiveObject) io = it.Value();
195 GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
197 if ( CORBA::is_nil( obj ) ) continue;
199 bool isASCII = false;
200 double deflection = 0.;
201 bool isRelative = false;
202 QString fileName = STLPlugin_ExportDlg::getFileName( io,
204 tr( "EXPORT_TITLE" ),
210 if ( fileName.isEmpty() )
213 SUIT_OverrideCursor wc;
215 GEOM_Operation transaction( app, stlOp.get() );
219 app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
222 stlOp->ExportSTL( obj, fileName.toUtf8().constData(), isASCII, deflection, isRelative );
224 if ( stlOp->IsDone() )
226 transaction.commit();
231 SUIT_MessageBox::critical( parent,
233 tr( "GEOM_PRP_ABORT" ) + "\n" + tr( stlOp->GetErrorCode() ) );
237 catch ( const SALOME::SALOME_Exception& e )
247 SUIT_MessageBox::warning( parent,
249 tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
254 //=====================================================================================
256 //=====================================================================================
260 __declspec( dllexport )
262 GEOMGUI* GetLibGUI( GeometryGUI* parent )
264 return new STLPlugin_GUI( parent );