Salome HOME
Copyright update 2021
[modules/geom.git] / src / AdvancedGUI / AdvancedGUI_SmoothingSurfaceDlg.cxx
1 // Copyright (C) 2013-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 #include "AdvancedGUI_SmoothingSurfaceDlg.h"
21
22 #include <SALOMEconfig.h>
23 #include CORBA_SERVER_HEADER(AdvancedGEOM)
24
25 #include <DlgRef.h>
26 #include <GeometryGUI.h>
27 #include <GEOMBase.h>
28
29 #include <SUIT_Session.h>
30 #include <SUIT_ResourceMgr.h>
31 #include <SalomeApp_Application.h>
32 #include <LightApp_SelectionMgr.h>
33
34 // OCCT Includes
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Iterator.hxx>
38 #include <TopExp.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40
41 #include <GEOMImpl_Types.hxx>
42
43 //=================================================================================
44 // Constructor
45 //=================================================================================
46 AdvancedGUI_SmoothingSurfaceDlg::AdvancedGUI_SmoothingSurfaceDlg (GeometryGUI* theGeometryGUI, QWidget* parent)
47   : GEOMBase_Skeleton(theGeometryGUI, parent, false),
48     myNbMaxSpin(0),
49     myDegMaxSpin(0),
50     myDMaxSpin(0)
51 {
52   QPixmap imageOp  (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SMOOTHINGSURFACE_LPOINTS")));
53   QPixmap imageSel (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
54   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
55
56   setWindowTitle(tr("GEOM_SMOOTHINGSURFACE_TITLE"));
57
58   /***************************************************************/
59   mainFrame()->GroupConstructors->setTitle(tr("GEOM_SMOOTHINGSURFACE"));
60   mainFrame()->RadioButton1->setIcon(imageOp);
61   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
62   mainFrame()->RadioButton2->close();
63   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
64   mainFrame()->RadioButton3->close();
65
66   GroupPoints = new DlgRef_1Sel( centralWidget() );
67
68   GroupPoints->GroupBox1->setTitle( tr( "GEOM_SMOOTHINGSURFACE_ARG" ) );
69   GroupPoints->TextLabel1->setText( tr( "GEOM_SMOOTHINGSURFACE_ARG_POINTS" ) );
70   GroupPoints->PushButton1->setIcon( image1 );
71   GroupPoints->LineEdit1->setReadOnly( true );
72
73   QLabel *aNbMax  = new QLabel(tr("GEOM_SMOOTHINGSURFACE_ARG_NB_MAX"));
74   QLabel *aDegMax = new QLabel(tr("GEOM_SMOOTHINGSURFACE_ARG_DEG_MAX"));
75   QLabel *aDMax   = new QLabel(tr("GEOM_SMOOTHINGSURFACE_ARG_D_MAX"));
76
77   myNbMaxSpin  = new SalomeApp_IntSpinBox(0, 1000, 1, 0, true, true);
78   myDegMaxSpin = new SalomeApp_IntSpinBox(0, 1000, 1, 0, true, true);
79   myDMaxSpin   = new SalomeApp_DoubleSpinBox;
80   GroupPoints->gridLayout1->addWidget(aNbMax,       1, 0);
81   GroupPoints->gridLayout1->addWidget(aDegMax,      2, 0);
82   GroupPoints->gridLayout1->addWidget(aDMax,        3, 0);
83   GroupPoints->gridLayout1->addWidget(myNbMaxSpin,  1, 1, 1, 2);
84   GroupPoints->gridLayout1->addWidget(myDegMaxSpin, 2, 1, 1, 2);
85   GroupPoints->gridLayout1->addWidget(myDMaxSpin,   3, 1, 1, 2);
86
87   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
88   layout->setMargin(0); layout->setSpacing(6);
89   layout->addWidget(GroupPoints);
90   /***************************************************************/
91
92   setHelpFileName("create_smoothingsurface_page.html");
93
94   Init();
95 }
96
97 //=================================================================================
98 // Destructor
99 //=================================================================================
100 AdvancedGUI_SmoothingSurfaceDlg::~AdvancedGUI_SmoothingSurfaceDlg()
101 {
102   // no need to delete child widgets, Qt does it all for us
103 }
104
105 //=================================================================================
106 // function : Init()
107 // purpose  :
108 //=================================================================================
109 void AdvancedGUI_SmoothingSurfaceDlg::Init()
110 {
111   // Get setting of step value from file configuration
112   //SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
113
114   initSpinBox(myDMaxSpin, 0., COORD_MAX, 0.00001, "parametric_precision" );
115
116   myNbMaxSpin->setValue(2);
117   myDegMaxSpin->setValue(8);
118   myDMaxSpin->setValue(0.);
119
120   showOnlyPreviewControl();
121
122   globalSelection();
123   localSelection( TopAbs_VERTEX );
124   //@@ initialize dialog box widgets here @@//
125
126   // Signal/slot connections
127   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
128   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
129   connect(myGeomGUI,     SIGNAL(SignalDefaultStepValueChanged(double)),
130           this,          SLOT(SetDoubleSpinBoxStep(double)));
131   connect( myGeomGUI->getApp()->selectionMgr(),
132            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
133   connect(myNbMaxSpin, SIGNAL(valueChanged(const QString&)),
134           this, SLOT(processPreview()) );
135   connect(myDegMaxSpin, SIGNAL(valueChanged(const QString&)),
136           this, SLOT(processPreview()) );
137   connect(myDMaxSpin, SIGNAL(valueChanged(const QString&)),
138           this, SLOT(processPreview()) );
139  
140   initName(tr("GEOM_SMOOTHINGSURFACE"));
141   //displayPreview();
142   SelectionIntoArgument();
143 }
144
145 //=================================================================================
146 // function : ClickOnOk()
147 // purpose  :
148 //=================================================================================
149 void AdvancedGUI_SmoothingSurfaceDlg::ClickOnOk()
150 {
151   if (ClickOnApply())
152     ClickOnCancel();
153 }
154
155 //=================================================================================
156 // function : ClickOnApply()
157 // purpose  :
158 //=================================================================================
159 bool AdvancedGUI_SmoothingSurfaceDlg::ClickOnApply()
160 {
161   if (!onAccept())
162     return false;
163
164   initName();
165   globalSelection();
166   localSelection( TopAbs_VERTEX );
167   return true;
168 }
169
170 //=================================================================================
171 // function : ActivateThisDialog()
172 // purpose  :
173 //=================================================================================
174 void AdvancedGUI_SmoothingSurfaceDlg::ActivateThisDialog()
175 {
176   GEOMBase_Skeleton::ActivateThisDialog();
177   globalSelection();
178   localSelection( TopAbs_VERTEX );
179   //displayPreview();
180 }
181
182 //=================================================================================
183 // function : enterEvent [REDEFINED]
184 // purpose  :
185 //=================================================================================
186 void AdvancedGUI_SmoothingSurfaceDlg::enterEvent (QEvent*)
187 {
188   if (!mainFrame()->GroupConstructors->isEnabled())
189     ActivateThisDialog();
190 }
191
192 //=================================================================================
193 // function : createOperation
194 // purpose  :
195 //=================================================================================
196 GEOM::GEOM_IOperations_ptr AdvancedGUI_SmoothingSurfaceDlg::createOperation()
197 {
198   return getGeomEngine()->GetPluginOperations("AdvancedEngine");
199 }
200
201 //=================================================================================
202 // function : isValid
203 // purpose  :
204 //=================================================================================
205 bool AdvancedGUI_SmoothingSurfaceDlg::isValid (QString& msg)
206 {
207   if (myPoints.empty()) {
208     msg += tr("GEOM_SMOOTHINGSURFACE_NO_POINTS");
209     return false;
210   }
211
212   bool ok = myNbMaxSpin->isValid (msg, !IsPreview()) &&
213             myDegMaxSpin->isValid(msg, !IsPreview()) &&
214             myDMaxSpin->isValid  (msg, !IsPreview());
215
216   return ok;
217 }
218
219 //=================================================================================
220 // function : execute
221 // purpose  :
222 //=================================================================================
223 bool AdvancedGUI_SmoothingSurfaceDlg::execute (ObjectList& objects)
224 {
225   bool res = false;
226
227   GEOM::GEOM_Object_var anObj;
228
229   GEOM::IAdvancedOperations_var anOper = GEOM::IAdvancedOperations::_narrow(getOperation());
230
231   //@@ retrieve input values from the widgets here @@//
232   GEOM::ListOfGO_var points = new GEOM::ListOfGO();
233   points->length( myPoints.count() );
234   for ( int i = 0; i < myPoints.count(); i++ )
235     points[i] = myPoints[i].copy();
236
237   const int    aNbMax  = myNbMaxSpin->value();
238   const int    aDegMax = myDegMaxSpin->value();
239   const double aDMax   = myDMaxSpin->value();
240
241   // call engine function
242   anObj = anOper->MakeSmoothingSurface(points, aNbMax, aDegMax, aDMax);
243   res = !anObj->_is_nil();
244   if (res && !IsPreview())
245   {
246     QStringList aParameters;
247     //@@ put stringified input parameters to the string list here to store in the data model for notebook @@//
248     if ( aParameters.count() > 0 ) anObj->SetParameters(aParameters.join(":").toUtf8().constData());
249   }
250   
251   if (res)
252     objects.push_back(anObj._retn());
253
254   return res;
255 }
256
257 //=================================================================================
258 // function : addSubshapesToStudy
259 // purpose  : virtual method to add new SubObjects if local selection
260 //=================================================================================
261 void AdvancedGUI_SmoothingSurfaceDlg::addSubshapesToStudy()
262 {
263   for ( int i = 0; i < myPoints.count(); i++ )
264     GEOMBase::PublishSubObject( myPoints[i].get() );
265 }
266
267
268 //=================================================================================
269 // function : getSourceObjects
270 // purpose  : virtual method to get source objects
271 //=================================================================================
272 QList<GEOM::GeomObjPtr> AdvancedGUI_SmoothingSurfaceDlg::getSourceObjects()
273 {
274   return myPoints;
275 }
276
277 //=================================================================================
278 // function : getNbPoints()
279 // purpose  : Returns the number of points in myPoints list.
280 //=================================================================================
281 int AdvancedGUI_SmoothingSurfaceDlg::getNbPoints() const
282 {
283   TopTools_IndexedMapOfShape aMap;
284
285   foreach (GEOM::GeomObjPtr anObj, myPoints) {
286     TopoDS_Shape aShape;
287
288     if(anObj && GEOMBase::GetShape(anObj.get(), aShape) && !aShape.IsNull()) {
289       if (aShape.ShapeType() == TopAbs_VERTEX) {
290         aMap.Add(aShape);
291       } else {
292         // Compound.
293         TopExp::MapShapes(aShape, TopAbs_VERTEX, aMap);
294       }
295     }
296   }
297
298   const int aNbPoints = aMap.Extent();
299
300   return aNbPoints;
301 }
302
303 //=================================================================================
304 // function : SelectionIntoArgument()
305 // purpose  : Called when selection as changed or other case
306 //=================================================================================
307 void AdvancedGUI_SmoothingSurfaceDlg::SelectionIntoArgument()
308 {
309   QList<TopAbs_ShapeEnum> aTypes;
310
311   aTypes << TopAbs_VERTEX << TopAbs_COMPOUND;
312
313   QList<GEOM::GeomObjPtr> points = getSelected( aTypes, -1 );
314
315   // Check the selected compounds if they consist of points only.
316   foreach (GEOM::GeomObjPtr anObj, points) {
317     TopoDS_Shape aShape;
318
319     if(anObj && GEOMBase::GetShape(anObj.get(), aShape) && !aShape.IsNull()) {
320       if (aShape.ShapeType() == TopAbs_COMPOUND) {
321         // Check if the compound contains vertices only.
322         TopoDS_Iterator anIter(aShape);
323         Standard_Boolean isValid = Standard_False;
324
325         for (; anIter.More(); anIter.Next()) {
326           const TopoDS_Shape &aSubShape = anIter.Value();
327
328           if (aSubShape.ShapeType() == TopAbs_VERTEX) {
329             isValid = Standard_True;
330           } else {
331             isValid = Standard_False;
332             break;
333           }
334         }
335
336         if (!isValid) {
337           points.clear();
338           break;
339         }
340       }
341     } else {
342       // NEVERREACHED
343       points.clear();
344       break;
345     }
346   }
347
348   GEOMBase::Synchronize( myPoints, points );
349   if ( !myPoints.isEmpty()  )
350     GroupPoints->LineEdit1->setText( QString::number( getNbPoints() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
351   else
352     GroupPoints->LineEdit1->setText( "" );
353   processPreview();
354 }
355
356 //=================================================================================
357 // function : SetEditCurrentArgument()
358 // purpose  :
359 //=================================================================================
360 void AdvancedGUI_SmoothingSurfaceDlg::SetEditCurrentArgument()
361 {
362   if ( sender() == GroupPoints->PushButton1 )
363     myEditCurrentArgument = GroupPoints->LineEdit1;
364   myEditCurrentArgument->setFocus();
365   globalSelection();
366   localSelection( TopAbs_VERTEX );
367   SelectionIntoArgument();
368 }