Salome HOME
85b578957910b45a3ea8ad76dc06c4e39380bb59
[modules/geom.git] / src / BuildGUI / BuildGUI_SolidDlg.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : BuildGUI_SolidDlg.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
26
27 #include "BuildGUI_SolidDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <GEOMImpl_Types.hxx>
34
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_Session.h>
38 #include <SalomeApp_Application.h>
39 #include <LightApp_SelectionMgr.h>
40
41 #include <TColStd_MapOfInteger.hxx>
42
43 //=================================================================================
44 // class    : BuildGUI_SolidDlg()
45 // purpose  : Constructs a BuildGUI_SolidDlg which is a child of 'parent', with the
46 //            name 'name' and widget flags set to 'f'.
47 //            The dialog will by default be modeless, unless you set 'modal' to
48 //            TRUE to construct a modal dialog.
49 //=================================================================================
50 BuildGUI_SolidDlg::BuildGUI_SolidDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
51   : GEOMBase_Skeleton( theGeometryGUI, parent )
52 {
53   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
54   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BUILD_SOLID" ) ) );
55   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SOLID_FROM_FACES" ) ) );
56
57   setWindowTitle( tr( "GEOM_SOLID_TITLE" ) );
58
59   /***************************************************************/
60   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_SOLID" ) );
61   mainFrame()->RadioButton1->setIcon( image1 );
62   mainFrame()->RadioButton2->setIcon( image2 );
63   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
64   mainFrame()->RadioButton3->close();
65
66   GroupSolid = new DlgRef_1Sel1Check( centralWidget() );
67   GroupSolid->GroupBox1->setTitle( tr( "GEOM_SOLID_SHELLS" ) );
68   GroupSolid->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
69   GroupSolid->CheckButton1->setText( tr( "GEOM_CREATE_SINGLE_SOLID" ) );
70   GroupSolid->PushButton1->setIcon( image0 );
71   GroupSolid->LineEdit1->setReadOnly( true );
72
73   GroupFaces = new DlgRef_1Sel1Check( centralWidget() );
74   GroupFaces->GroupBox1->setTitle( tr( "GEOM_SOLID_FACES" ) );
75   GroupFaces->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
76   GroupFaces->CheckButton1->setText( tr( "GEOM_SOLID_FROM_FACE_OPT" ) );
77   GroupFaces->PushButton1->setIcon( image0 );
78   GroupFaces->LineEdit1->setReadOnly( true );
79
80   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
81   layout->setMargin( 0 ); layout->setSpacing( 6 );
82   layout->addWidget( GroupSolid );
83   layout->addWidget( GroupFaces );
84   /***************************************************************/
85
86   setHelpFileName("create_solid_page.html");
87
88   /* Initialisations */
89   Init();
90 }
91
92
93 //=================================================================================
94 // function : ~BuildGUI_SolidDlg()
95 // purpose  : Destroys the object and frees any allocated resources
96 //=================================================================================
97 BuildGUI_SolidDlg::~BuildGUI_SolidDlg()
98 {
99   // no need to delete child widgets, Qt does it all for us
100 }
101
102
103 //=================================================================================
104 // function : Init()
105 // purpose  :
106 //=================================================================================
107 void BuildGUI_SolidDlg::Init()
108 {
109   /* init variables */
110   myEditCurrentArgument = GroupSolid->LineEdit1;
111   GroupSolid->LineEdit1->setReadOnly( true );
112   GroupSolid->CheckButton1->setChecked( true );
113
114   myShells.clear();
115
116   //globalSelection( GEOM_SHELL );
117   TColStd_MapOfInteger aMap;
118   aMap.Add( GEOM_SHELL );
119   aMap.Add( GEOM_COMPOUNDFILTER );
120   QList<int> aSubShapes;
121   aSubShapes.append( GEOM_SHELL );
122   globalSelection( aMap, aSubShapes );
123
124   /* signals and slots connections */
125   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
126   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
127
128   connect(this,      SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
129
130   connect( GroupSolid->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
131   connect( GroupFaces->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
132   connect( GroupSolid->CheckButton1, SIGNAL( toggled( bool ) ), this, SLOT( EnableNameField( bool ) ) );
133
134   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
135            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
136
137   initName( tr( "GEOM_SOLID" ) );
138   SelectionIntoArgument();
139
140   ConstructorsClicked(0);
141 }
142
143
144 //=================================================================================
145 // function : ClickOnOk()
146 // purpose  :
147 //=================================================================================
148 void BuildGUI_SolidDlg::ClickOnOk()
149 {
150   setIsApplyAndClose( true );
151   if ( ClickOnApply() )
152     ClickOnCancel();
153 }
154
155 //=================================================================================
156 // function : ClickOnApply()
157 // purpose  :
158 //=================================================================================
159 bool BuildGUI_SolidDlg::ClickOnApply()
160 {
161   if ( !onAccept() )
162     return false;
163
164   initName();
165
166   myEditCurrentArgument->setText("");
167   ConstructorsClicked(getConstructorId());
168
169   return true;
170 }
171
172 //=================================================================================
173 // function : ConstructorsClicked()
174 // purpose  : Radio button management
175 //=================================================================================
176 void BuildGUI_SolidDlg::ConstructorsClicked(int constructorId)
177 {
178   switch (constructorId) {
179   case 0:
180     {
181       globalSelection();
182       GroupFaces->hide();
183       GroupSolid->show();
184       myEditCurrentArgument = GroupSolid->LineEdit1;
185       GroupSolid->LineEdit1->setText("");
186       break;
187     }
188   case 1:
189     {
190       globalSelection();
191       GroupSolid->hide();
192       GroupFaces->show();
193       myEditCurrentArgument = GroupFaces->LineEdit1;
194       GroupFaces->LineEdit1->setText("");
195       break;
196     }
197   }
198
199   qApp->processEvents();
200   updateGeometry();
201   resize(minimumSizeHint());
202   SelectionIntoArgument();
203 }
204
205 //=================================================================================
206 // function : SelectionIntoArgument()
207 // purpose  : Called when selection as changed or other case
208 //=================================================================================
209 void BuildGUI_SolidDlg::SelectionIntoArgument()
210 {
211   myEditCurrentArgument->setText( "" );
212
213   //myShells = getSelected( TopAbs_SHELL, -1 );
214   QList<TopAbs_ShapeEnum> types;
215   if (myEditCurrentArgument == GroupSolid->LineEdit1)
216   types << TopAbs_SHELL << TopAbs_COMPOUND;
217   else if (myEditCurrentArgument == GroupFaces->LineEdit1)
218   types << TopAbs_FACE << TopAbs_SHELL << TopAbs_COMPOUND;
219   myShells = getSelected( types, -1 );
220
221   if ( !myShells.isEmpty() ) {
222     QString aName = myShells.count() > 1 ? QString( "%1_objects").arg( myShells.count() ) : GEOMBase::GetName( myShells[0].get() );
223     myEditCurrentArgument->setText( aName );
224   }
225 }
226
227 //=================================================================================
228 // function : SetEditCurrentArgument()
229 // purpose  :
230 //=================================================================================
231 void BuildGUI_SolidDlg::SetEditCurrentArgument()
232 {
233   QPushButton* send = (QPushButton*)sender();
234
235   TColStd_MapOfInteger aMap;
236   QList<int> aSubShapes;
237   aMap.Add( GEOM_COMPOUNDFILTER );
238
239   if (send == GroupSolid->PushButton1) {
240     aMap.Add( GEOM_SHELL );
241         aSubShapes.append( GEOM_SHELL );
242         globalSelection( aMap, aSubShapes );
243         myEditCurrentArgument = GroupSolid->LineEdit1;
244   }
245   else if (send == GroupFaces->PushButton1) {
246     aMap.Add( GEOM_SHELL );
247     aMap.Add( GEOM_FACE );
248     aSubShapes.append( GEOM_SHELL );
249     aSubShapes.append( GEOM_FACE );
250     globalSelection( aMap, aSubShapes );
251     myEditCurrentArgument = GroupFaces->LineEdit1;
252   }
253
254   myEditCurrentArgument->setFocus();
255   SelectionIntoArgument();
256 }
257
258
259 //=================================================================================
260 // function : ActivateThisDialog()
261 // purpose  :
262 //=================================================================================
263 void BuildGUI_SolidDlg::ActivateThisDialog()
264 {
265   GEOMBase_Skeleton::ActivateThisDialog();
266   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
267            this, SLOT(SelectionIntoArgument()));
268
269   ConstructorsClicked(getConstructorId());
270 }
271
272
273 //=================================================================================
274 // function : enterEvent()
275 // purpose  :
276 //=================================================================================
277 void BuildGUI_SolidDlg::enterEvent( QEvent* )
278 {
279   if ( !mainFrame()->GroupConstructors->isEnabled() )
280     ActivateThisDialog();
281 }
282
283
284 //=================================================================================
285 // function :  EnableNameField()
286 // purpose  :
287 //=================================================================================
288 void  BuildGUI_SolidDlg::EnableNameField( bool toEnable )
289 {
290   mainFrame()->GroupBoxName->setEnabled( toEnable );
291 }
292
293 //=================================================================================
294 // function : createOperation
295 // purpose  :
296 //=================================================================================
297 GEOM::GEOM_IOperations_ptr BuildGUI_SolidDlg::createOperation()
298 {
299   return getGeomEngine()->GetIShapesOperations();
300 }
301
302 //=================================================================================
303 // function : isValid
304 // purpose  :
305 //=================================================================================
306 bool BuildGUI_SolidDlg::isValid (QString& msg)
307 {
308   bool ok = !myShells.isEmpty();
309
310   GEOM::MeasureOpPtr anOp;
311   anOp.take(myGeomGUI->GetGeomGen()->GetIMeasureOperations());
312   if (getConstructorId() == 0 && (!GroupSolid->CheckButton1->isChecked() || myShells.count() == 1)) {
313         for (int i = 0, n = myShells.count(); i < n && ok; i++) {
314           CORBA::String_var aRes = anOp->IsGoodForSolid(myShells[i].get());
315           if (strlen(aRes.in())) {
316                 msg = QObject::tr(aRes.in()).arg(GEOMBase::GetName(myShells[i].get()));
317                 ok = false;
318           }
319         }
320   }
321   return ok;
322 }
323
324 /*
325 //=================================================================================
326 // function : isClosed
327 // purpose  : Check the object 'i' in myShells list is closed or unclosed
328 //=================================================================================
329 bool BuildGUI_SolidDlg::isClosed( GEOM::GEOM_Object_ptr shell )
330 {
331   bool ok = false;
332
333   GEOM::GEOM_IKindOfShape::shape_kind aKind;
334   GEOM::ListOfLong_var anInts;
335   GEOM::ListOfDouble_var aDbls;
336
337   if ( !CORBA::is_nil( shell ) ) {
338     GEOM::MeasureOpPtr anOp;
339     anOp.take( myGeomGUI->GetGeomGen()->GetIMeasureOperations() );
340
341     // Detect kind of shape and parameters
342     aKind = anOp->KindOfShape(shell, anInts, aDbls);
343
344     if ( anOp->IsDone() ) {
345       if ( anInts[0] == 1 )
346         ok = true;
347       else if ( anInts[0] == 2 )
348         ok = false;
349     }
350     else {
351       MESSAGE ("KindOfShape Operation is NOT DONE!!!");
352     }
353   }
354   else {
355     MESSAGE ("Shape is NULL!!!");
356   }
357
358   return ok;
359 }
360 */
361
362 //=================================================================================
363 // function : execute
364 // purpose  :
365 //=================================================================================
366 bool BuildGUI_SolidDlg::execute( ObjectList& objects )
367 {
368   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
369   GEOM::GEOM_Object_var anObj;
370
371   switch (getConstructorId()) {
372     case 0:
373     {
374       if ( GroupSolid->CheckButton1->isChecked() ) {
375         GEOM::ListOfGO_var objlist = new GEOM::ListOfGO();
376         objlist->length( myShells.count() );
377         for ( int i = 0; i < myShells.count(); i++ )
378           objlist[i] = myShells[i].copy();
379
380         anObj = anOper->MakeSolidShells( objlist.in() );
381         if ( !anObj->_is_nil() ) objects.push_back( anObj._retn() );
382       }
383       else {
384         for ( int i = 0, n = myShells.count(); i< n; i++ ) {
385           anObj = anOper->MakeSolidShell( myShells[ i ].get() );
386           if ( !anObj->_is_nil() ) objects.push_back( anObj._retn() );
387         }
388       }
389       break;
390     }
391     case 1:
392     {
393       GEOM::ListOfGO_var objlist = new GEOM::ListOfGO();
394       objlist->length( myShells.count() );
395       for ( int i = 0; i < myShells.count(); i++ )
396         objlist[i] = myShells[i].copy();
397
398       anObj = anOper->MakeSolidFromConnectedFaces( objlist.in(), GroupFaces->CheckButton1->isChecked() );
399       if ( !anObj->_is_nil() ) objects.push_back( anObj._retn() );
400
401       break;
402     }
403   }
404
405   return true;
406 }
407
408 //=================================================================================
409 // function : getSourceObjects
410 // purpose  : virtual method to get source objects
411 //=================================================================================
412 QList<GEOM::GeomObjPtr> BuildGUI_SolidDlg::getSourceObjects()
413 {
414   return myShells;
415 }