Salome HOME
00b4a577b473839fd04cfe5ef4a170111701988d
[modules/geom.git] / src / STLPlugin / STLPlugin_ExportDlg.cxx
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "STLPlugin_ExportDlg.h"
22
23 // GUI includes
24 #include <SUIT_Session.h>
25 #include <SUIT_ResourceMgr.h>
26 #include <SUIT_ViewManager.h>
27
28 #include <SalomeApp_Application.h>
29 #include <SalomeApp_DoubleSpinBox.h>
30 #include <SalomeApp_Study.h>
31
32 // GEOM includes
33 #include "GEOMBase.h"
34 #include "GEOM_Constants.h"
35
36 // OCC includes
37 #include <BRepBndLib.hxx>
38
39 // QT includes
40 #include <QApplication>
41 #include <QLabel>
42 #include <QLayout>
43 #include <QComboBox>
44 #include <QCheckBox>
45
46 //=================================================================================
47 // Constructor
48 //=================================================================================
49 STLPlugin_ExportDlg::STLPlugin_ExportDlg( const Handle(SALOME_InteractiveObject)& io, QWidget* parent )
50 : SUIT_FileDlg( parent, false, true, true )
51 {
52   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
53
54   QLabel* deflectionLabel = new QLabel( tr( "DEFLECTION" ), this );
55
56   QWidget* options = new QWidget( this );
57   QHBoxLayout* optionsLayout = new QHBoxLayout( options );
58   optionsLayout->setMargin( 0 );
59   optionsLayout->setSpacing( 5 );
60
61   myDeflectionSB = new SalomeApp_DoubleSpinBox( options );
62   int aPrecision = resMgr->integerValue( "Geometry", "parametric_precision", 6 );
63   myDeflectionSB->setAcceptNames( false );
64   myDeflectionSB->setPrecision( aPrecision );
65   myDeflectionSB->setDecimals( aPrecision );
66   myDeflectionSB->setRange( GEOM::minDeflection(), 1.0 );
67   myDeflectionSB->setSingleStep( 1.0e-04 );
68
69   myModeCB = new QCheckBox( tr( "RELATIVE" ), options );
70
71   myFormatCB = new QCheckBox( tr( "ASCII" ) );
72
73   optionsLayout->addWidget( myDeflectionSB );
74   optionsLayout->addWidget( myModeCB );
75   optionsLayout->addWidget( myFormatCB );
76
77   layout()->addWidget( deflectionLabel );
78   layout()->addWidget( options );
79
80   myShapeSize = getShapeSize( io );
81
82   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
83   SalomeApp_Study* study = dynamic_cast< SalomeApp_Study* >( app->activeStudy() );
84   int mgrId = app->activeViewManager()->getGlobalId();
85   QVariant v = study->getObjectProperty( mgrId, io->getEntry(), GEOM::propertyName( GEOM::Deflection ), QVariant() );
86   double deflection =  v.isValid() ? v.toDouble() : SUIT_Session::session()->resourceMgr()->doubleValue( "Geometry", "deflection_coef", 0.001 );
87
88   myDeflectionSB->setValue( deflection );
89   myModeCB->setChecked( true );
90   myFormatCB->setChecked( true );
91
92   connect( myModeCB, SIGNAL( clicked ( bool ) ), this, SLOT( modeChanged() ) );
93 }
94
95 //=================================================================================
96 // Destructor
97 //=================================================================================
98 STLPlugin_ExportDlg::~STLPlugin_ExportDlg()
99 {
100 }
101
102 //=================================================================================
103 // modeChanged
104 //=================================================================================
105 void STLPlugin_ExportDlg::modeChanged()
106 {
107   if ( myModeCB->isChecked() ) {
108     double deflection = myDeflectionSB->value() / myShapeSize;
109     deflection = ( deflection > 1.0 ) ? 1.0 : deflection;
110     myDeflectionSB->setRange( GEOM::minDeflection(), 1.0 );
111     myDeflectionSB->setValue( deflection );
112   }
113   else {
114     double deflection = myDeflectionSB->value() * myShapeSize;
115     myDeflectionSB->setRange( GEOM::minDeflection(), 10000.0 );
116     myDeflectionSB->setValue( deflection );
117   }
118 }
119
120 //=================================================================================
121 // getShapeSize
122 //=================================================================================
123 double STLPlugin_ExportDlg::getShapeSize( const Handle(SALOME_InteractiveObject)& io )
124 {
125   TopoDS_Shape shape;
126   GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
127   GEOMBase::GetShape( obj, shape, TopAbs_SHAPE );
128   Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
129   Bnd_Box bnd;
130   BRepBndLib::Add( shape, bnd );
131   bnd.Get( xmin, ymin, zmin, xmax, ymax, zmax );
132   double xd = fabs( xmax-xmin );
133   double yd = fabs( ymax-ymin );
134   double zd = fabs( zmax-zmin ); 
135   return std::max( std::max ( xd, yd ), zd );
136 }
137
138 //=================================================================================
139 // isAscii
140 //=================================================================================
141 bool STLPlugin_ExportDlg::isAscii() const
142 {
143   return myFormatCB->isChecked();
144 }
145
146 //=================================================================================
147 // getDeflection
148 //=================================================================================
149 double STLPlugin_ExportDlg::getDeflection() const
150 {
151   return myDeflectionSB->value();
152 }
153  
154 //=================================================================================
155 // isDeflectionRelative
156 //=================================================================================
157 bool STLPlugin_ExportDlg::isDeflectionRelative() const
158 {
159   return myModeCB->isChecked();
160 }
161
162 //=================================================================================
163 // getFileName
164 //=================================================================================
165 QString STLPlugin_ExportDlg::getFileName( const Handle(SALOME_InteractiveObject)& io,
166                                           const QString& filters, const QString& caption,
167                                           QWidget* parent, bool& isAscii,
168                                           double& deflection, bool& isRelative )
169 {
170   QStringList fls = filters.split( ";;", QString::SkipEmptyParts );
171
172   QString tmpfilename = io->getName();
173   tmpfilename = tmpfilename.simplified();
174   tmpfilename = tmpfilename.replace( QRegExp( "\\*" ), "" ).replace( QRegExp( "\\?" ), "" );
175
176   STLPlugin_ExportDlg fd( io, parent );
177   fd.setFileMode( AnyFile );
178   fd.setNameFilters( fls );
179   fd.setWindowTitle( caption );
180   if ( !tmpfilename.isEmpty() )
181     fd.processPath( tmpfilename );
182
183   QString filename;
184
185   if ( fd.exec() == QDialog::Accepted ) {
186     filename = fd.selectedFile();
187     isAscii = fd.isAscii();
188     deflection = fd.getDeflection();
189     isRelative = fd.isDeflectionRelative();
190   }
191
192   QApplication::processEvents();
193
194   return filename;
195 }
196