1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : MeasureGUI_DistanceDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
27 #include "MeasureGUI_DistanceDlg.h"
28 #include "MeasureGUI_Widgets.h"
32 #include <GeometryGUI.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_Desktop.h>
36 #include <SUIT_ResourceMgr.h>
37 #include <SUIT_ViewWindow.h>
38 #include <SUIT_ViewManager.h>
39 #include <SUIT_OverrideCursor.h>
40 #include <SUIT_MessageBox.h>
42 #include <SOCC_ViewModel.h>
43 #include <SalomeApp_Tools.h>
44 #include <SalomeApp_Application.h>
45 #include <LightApp_SelectionMgr.h>
48 #include <Geom_Plane.hxx>
49 #include <AIS_LengthDimension.hxx>
50 #include <Prs3d_LineAspect.hxx>
51 #include <BRepBuilderAPI_MakeEdge.hxx>
52 #include <BRepBuilderAPI_MakeVertex.hxx>
53 #include <gce_MakePln.hxx>
54 #include <Precision.hxx>
56 //=================================================================================
57 // class : MeasureGUI_DistanceDlg()
58 // purpose : Constructs a MeasureGUI_DistanceDlg which is a child of 'parent', with the
59 // name 'name' and widget flags set to 'f'.
60 // The dialog will by default be modeless, unless you set 'modal' to
61 // true to construct a modal dialog.
62 //=================================================================================
63 MeasureGUI_DistanceDlg::MeasureGUI_DistanceDlg (GeometryGUI* GUI, QWidget* parent)
64 : GEOMBase_Skeleton(GUI, parent)
66 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
67 QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_MINDIST")));
68 QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
70 setWindowTitle(tr("GEOM_MINDIST_TITLE"));
72 /***************************************************************/
73 mainFrame()->GroupConstructors->setTitle(tr("GEOM_DISTANCE"));
74 mainFrame()->RadioButton1->setIcon(image0);
75 mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
76 mainFrame()->RadioButton2->close();
77 mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
78 mainFrame()->RadioButton3->close();
80 mainFrame()->GroupBoxName->hide();
82 myGrp = new MeasureGUI_DistanceGroup (centralWidget());
83 myGrp->GroupBox1->setTitle(tr("GEOM_MINDIST_OBJ"));
86 myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
87 myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
88 myGrp->PushButton1->setIcon(image1);
89 myGrp->PushButton2->setIcon(image1);
90 myGrp->LineEdit1->setReadOnly(true);
91 myGrp->LineEdit2->setReadOnly(true);
94 myGrp->TextLabel7->setText(tr("GEOM_SOLUTION"));
96 // Distance, dx, dy and dz
97 myGrp->TextLabel3->setText(tr("GEOM_LENGTH"));
98 myGrp->TextLabel4->setText(tr("GEOM_DX"));
99 myGrp->TextLabel5->setText(tr("GEOM_DY"));
100 myGrp->TextLabel6->setText(tr("GEOM_DZ"));
101 myGrp->LineEdit3->setReadOnly(true);
102 myGrp->LineEdit4->setReadOnly(true);
103 myGrp->LineEdit5->setReadOnly(true);
104 myGrp->LineEdit6->setReadOnly(true);
105 /***************************************************************/
107 myHelpFileName = "min_distance_page.html";
113 //=================================================================================
114 // function : ~MeasureGUI_DistanceDlg()
115 // purpose : Destroys the object and frees any allocated resources
116 //=================================================================================
117 MeasureGUI_DistanceDlg::~MeasureGUI_DistanceDlg()
121 //=================================================================================
124 //=================================================================================
125 void MeasureGUI_DistanceDlg::Init()
127 myEditCurrentArgument = myGrp->LineEdit1;
128 myDbls = new GEOM::ListOfDouble();
130 myGrp->LineEdit1->setEnabled(true);
131 myGrp->LineEdit2->setEnabled(false);
136 // signals and slots connections
137 connect(buttonOk(), SIGNAL(clicked()), this, SLOT(ClickOnOk()));
138 connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
140 connect(myGrp->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
141 connect(myGrp->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
143 connect(myGrp->ComboBox1, SIGNAL(currentIndexChanged(int)), this, SLOT(SolutionSelected(int)));
145 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
146 this, SLOT(SelectionIntoArgument()));
149 SelectionIntoArgument();
152 //=================================================================================
153 // function : ClickOnOk()
155 //=================================================================================
156 void MeasureGUI_DistanceDlg::ClickOnOk()
162 //=================================================================================
163 // function : ClickOnApply()
165 //=================================================================================
166 bool MeasureGUI_DistanceDlg::ClickOnApply()
176 //=================================================================================
177 // function : ActivateThisDialog()
179 //=================================================================================
180 void MeasureGUI_DistanceDlg::ActivateThisDialog()
182 GEOMBase_Skeleton::ActivateThisDialog();
184 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
185 this, SLOT(SelectionIntoArgument()));
191 //=================================================================================
192 // function : enterEvent()
194 //=================================================================================
195 void MeasureGUI_DistanceDlg::enterEvent(QEvent*)
197 if (!mainFrame()->GroupConstructors->isEnabled())
198 ActivateThisDialog();
201 //=================================================================================
202 // function : SolutionSelected()
203 // purpose : Called when ComboBox selection has changed
204 //=================================================================================
205 void MeasureGUI_DistanceDlg::SolutionSelected (int i)
207 if (i < 0 || myDbls->length() < (i+1)*6) {
208 myGrp->LineEdit3->setText("");
209 myGrp->LineEdit4->setText("");
210 myGrp->LineEdit5->setText("");
211 myGrp->LineEdit6->setText("");
216 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
217 int aPrecision = resMgr->integerValue( "Geometry", "length_precision", 6 );
219 gp_Pnt p1 (myDbls[i*6 + 0], myDbls[i*6 + 1], myDbls[i*6 + 2]);
220 gp_Pnt p2 (myDbls[i*6 + 3], myDbls[i*6 + 4], myDbls[i*6 + 5]);
222 double aDist = p1.Distance(p2);
223 myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(aDist, aPrecision));
225 gp_XYZ aVec = p2.XYZ() - p1.XYZ();
226 myGrp->LineEdit4->setText(DlgRef::PrintDoubleValue(aVec.X(), aPrecision));
227 myGrp->LineEdit5->setText(DlgRef::PrintDoubleValue(aVec.Y(), aPrecision));
228 myGrp->LineEdit6->setText(DlgRef::PrintDoubleValue(aVec.Z(), aPrecision));
233 //=================================================================================
234 // function : SelectionIntoArgument()
235 // purpose : Called when selection has changed
236 //=================================================================================
237 void MeasureGUI_DistanceDlg::SelectionIntoArgument()
239 QList<TopAbs_ShapeEnum> aTypes;
240 aTypes << TopAbs_VERTEX << TopAbs_EDGE << TopAbs_WIRE << TopAbs_FACE << TopAbs_SHELL << TopAbs_SOLID << TopAbs_COMPSOLID << TopAbs_COMPOUND << TopAbs_SHAPE;
241 GEOM::GeomObjPtr aSelectedObject = getSelected( aTypes );
244 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
245 myGeomGUI->getApp()->selectionMgr()->clearSelected();
246 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
247 this, SLOT(SelectionIntoArgument()));
249 if (myEditCurrentArgument == myGrp->LineEdit1) {
250 myObj1 = aSelectedObject;
251 if (myObj1 && !myObj2)
252 myGrp->PushButton2->click();
255 myObj2 = aSelectedObject;
256 if (myObj2 && !myObj1)
257 myGrp->PushButton1->click();
263 //=================================================================================
264 // function : SetEditCurrentArgument()
266 //=================================================================================
267 void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
269 QPushButton* send = (QPushButton*)sender();
271 if (send == myGrp->PushButton1) {
272 myEditCurrentArgument = myGrp->LineEdit1;
274 myGrp->PushButton2->setDown(false);
275 myGrp->LineEdit2->setEnabled(false);
278 myEditCurrentArgument = myGrp->LineEdit2;
280 myGrp->PushButton1->setDown(false);
281 myGrp->LineEdit1->setEnabled(false);
285 myEditCurrentArgument->setEnabled(true);
286 myEditCurrentArgument->setFocus();
287 // after setFocus(), because it will be setDown(false) when loses focus
290 // seems we need it only to avoid preview disappearing, caused by selection mode change
294 //=================================================================================
295 // function : processObject()
296 // purpose : Fill dialogs fields in accordance with myObj1 and myObj2
297 //=================================================================================
298 void MeasureGUI_DistanceDlg::processObject()
300 myGrp->LineEdit1->setText(myObj1 ? GEOMBase::GetName(myObj1.get()) : "");
301 myGrp->LineEdit2->setText(myObj2 ? GEOMBase::GetName(myObj2.get()) : "");
303 myGrp->ComboBox1->clear();
310 if (!isValid(msg)) return;
312 GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow(getOperation());
314 nbSols = anOper->ClosestPoints(myObj1.get(), myObj2.get(), myDbls);
316 catch (const SALOME::SALOME_Exception& e) {
317 SalomeApp_Tools::QtCatchCorbaException(e);
321 if (!anOper->IsDone())
322 myGrp->ComboBox1->addItem(tr(anOper->GetErrorCode()));
323 else if (nbSols <= 0)
324 myGrp->ComboBox1->addItem(tr("GEOM_MINDIST_NO_SOL"));
326 for (int i = 0; i < nbSols; i++) {
327 myGrp->ComboBox1->addItem(tr("GEOM_SOLUTION_I").arg(i + 1));
329 myGrp->ComboBox1->setCurrentIndex(0);
333 //=================================================================================
334 // function : buildPrs()
336 //=================================================================================
337 SALOME_Prs* MeasureGUI_DistanceDlg::buildPrs()
339 SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
341 int currSol = myGrp->ComboBox1->currentIndex();
343 if (!myObj1 || !myObj2 ||
344 currSol == -1 || (currSol+1)*6 > myDbls->length() ||
345 vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
348 gp_Pnt aPnt1 (myDbls[currSol*6 + 0], myDbls[currSol*6 + 1], myDbls[currSol*6 + 2]);
349 gp_Pnt aPnt2 (myDbls[currSol*6 + 3], myDbls[currSol*6 + 4], myDbls[currSol*6 + 5]);
351 double aDist = aPnt1.Distance(aPnt2);
355 if (aDist <= 1.e-9) {
356 BRepBuilderAPI_MakeVertex aMaker (aPnt1);
357 return getDisplayer()->BuildPrs(aMaker.Vertex());
360 BRepBuilderAPI_MakeEdge MakeEdge (aPnt1, aPnt2);
361 TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex(aPnt1);
362 TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex(aPnt2);
365 //aLabel.sprintf("%.1f", aDist);
367 gp_Pnt aPnt3 ((aPnt1.X() + aPnt2.X()) / 2,
368 (aPnt1.Y() + aPnt2.Y()) / 2,
369 (aPnt1.Z() + aPnt2.Z()) / 2);
371 gp_Vec va (aPnt3, aPnt1);
372 gp_Vec vb (aPnt3, aPnt2);
374 if (va.IsParallel(vb, Precision::Angular())) {
375 aPnt3.SetY((aPnt1.Y() + aPnt2.Y()) / 2 + 100);
376 aPnt3.SetZ((aPnt1.Z() + aPnt2.Z()) / 2);
379 gce_MakePln gce_MP (aPnt1, aPnt2, aPnt3);
380 Handle(Geom_Plane) P = new Geom_Plane (gce_MP.Value());
382 Handle(AIS_LengthDimension) anIO = new AIS_LengthDimension( aVert1, aVert2, P->Pln() );
384 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
385 int w = resMgr->integerValue( "Geometry", "measures_line_width", 1 );
387 Handle(Prs3d_DimensionAspect) aDimensionStyle = new Prs3d_DimensionAspect;
389 aDimensionStyle->ArrowAspect()->SetLength( aDist / 20.0 );
390 aDimensionStyle->LineAspect()->SetWidth( w );
391 aDimensionStyle->SetTextHorizontalPosition( Prs3d_DTHP_Center );
392 aDimensionStyle->SetTextVerticalPosition( Prs3d_DTVP_Center );
393 aDimensionStyle->MakeText3d( Standard_False );
394 aDimensionStyle->MakeArrows3d( Standard_True );
396 anIO->SetFlyout( 0.0 );
397 anIO->SetDimensionAspect( aDimensionStyle );
399 SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
402 aPrs->AddObject(anIO);
407 catch (Standard_Failure) {
412 //=================================================================================
413 // function : createOperation
415 //=================================================================================
416 GEOM::GEOM_IOperations_ptr MeasureGUI_DistanceDlg::createOperation()
418 return getGeomEngine()->GetIMeasureOperations(getStudyId());
421 //=================================================================================
422 // function : isValid()
424 //=================================================================================
425 bool MeasureGUI_DistanceDlg::isValid (QString& msg)
427 return myObj1 && myObj2;
430 //=================================================================================
431 // function : execute
433 //=================================================================================
434 bool MeasureGUI_DistanceDlg::execute (ObjectList& objects)
436 GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow(getOperation());
437 GEOM::GEOM_IBasicOperations_var aBasicOper = getGeomEngine()->GetIBasicOperations(getStudyId());
439 GEOM::ListOfDouble_var aDbls;
440 int nbSols = anOper->ClosestPoints(myObj1.get(), myObj2.get(), aDbls);
442 if (anOper->IsDone()) {
443 bool doPublishAll = true;
445 QMessageBox::StandardButton anAnswer =
446 SUIT_MessageBox::question(this, tr("GEOM_MINDIST_PUBLISH_TITLE"),
447 tr("GEOM_MINDIST_PUBLISH_TEXT"),
448 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
450 if (anAnswer == QMessageBox::No)
451 doPublishAll = false;
452 else if (anAnswer != QMessageBox::Yes)
456 for (int i = 0; i < nbSols; i++) {
457 GEOM::GEOM_Object_var anObj1 =
458 aBasicOper->MakePointXYZ(aDbls[i*6 + 0], aDbls[i*6 + 1], aDbls[i*6 + 2]);
459 GEOM::GEOM_Object_var anObj2 =
460 aBasicOper->MakePointXYZ(aDbls[i*6 + 3], aDbls[i*6 + 4], aDbls[i*6 + 5]);
462 if (!anObj1->_is_nil() && !anObj2->_is_nil()) {
463 objects.push_back(anObj1._retn());
464 objects.push_back(anObj2._retn());
469 int i = myGrp->ComboBox1->currentIndex();
470 GEOM::GEOM_Object_var anObj1 =
471 aBasicOper->MakePointXYZ(aDbls[i*6 + 0], aDbls[i*6 + 1], aDbls[i*6 + 2]);
472 GEOM::GEOM_Object_var anObj2 =
473 aBasicOper->MakePointXYZ(aDbls[i*6 + 3], aDbls[i*6 + 4], aDbls[i*6 + 5]);
475 if (!anObj1->_is_nil() && !anObj2->_is_nil()) {
476 objects.push_back(anObj1._retn());
477 objects.push_back(anObj2._retn());
485 //=================================================================================
486 // function : activateSelection()
488 //=================================================================================
489 void MeasureGUI_DistanceDlg::activateSelection()
491 globalSelection( GEOM_ALLSHAPES );
492 localSelection( TopAbs_SHAPE );
495 //=================================================================================
496 // function : redisplayPreview()
498 //=================================================================================
499 void MeasureGUI_DistanceDlg::redisplayPreview()
502 if (!isValid(aMess)) {
510 SUIT_OverrideCursor wc;
512 getDisplayer()->SetColor(Quantity_NOC_VIOLET);
513 getDisplayer()->SetToActivate(false);
515 if (SALOME_Prs* aPrs = buildPrs())
516 displayPreview(aPrs);
518 catch (const SALOME::SALOME_Exception& e) {
519 SalomeApp_Tools::QtCatchCorbaException(e);
523 //================================================================
524 // Function : getNewObjectName
525 // Purpose : Redefine this method to return proper name for a new object
526 //================================================================
527 QString MeasureGUI_DistanceDlg::getNewObjectName (int currObj) const
529 QString aName = tr("GEOM_MINDIST_NAME") + QString("_%1_").arg((currObj+1)/2);
530 aName += GEOMBase::GetName(currObj%2 ? myObj1.get() : myObj2.get());
535 //=================================================================================
536 // function : addSubshapeToStudy
537 // purpose : virtual method to add new SubObjects if local selection
538 //=================================================================================
539 void MeasureGUI_DistanceDlg::addSubshapesToStudy()
541 GEOMBase::PublishSubObject( myObj1.get() );
542 GEOMBase::PublishSubObject( myObj2.get() );
545 //=================================================================================
546 // function : getSourceObjects
547 // purpose : virtual method to get source objects
548 //=================================================================================
549 QList<GEOM::GeomObjPtr> MeasureGUI_DistanceDlg::getSourceObjects()
551 QList<GEOM::GeomObjPtr> res;
552 res << myObj1 << myObj2;
556 //=================================================================================
557 // function : MeasureGUI_DistanceGroup
559 //=================================================================================
560 MeasureGUI_DistanceGroup::MeasureGUI_DistanceGroup (QWidget *parent)
562 gridLayout = new QGridLayout (parent);
563 gridLayout->setSpacing(6);
564 gridLayout->setContentsMargins(11, 11, 11, 11);
565 gridLayout->setHorizontalSpacing(0);
566 gridLayout->setVerticalSpacing(0);
567 gridLayout->setContentsMargins(0, 0, 0, 0);
569 GroupBox1 = new QGroupBox (parent);
571 gridLayout1 = new QGridLayout (GroupBox1);
572 gridLayout1->setSpacing(6);
573 gridLayout1->setContentsMargins(11, 11, 11, 11);
574 gridLayout1->setHorizontalSpacing(6);
575 gridLayout1->setVerticalSpacing(6);
576 gridLayout1->setContentsMargins(9, 9, 9, 9);
579 TextLabel1 = new QLabel(GroupBox1);
580 TextLabel2 = new QLabel(GroupBox1);
582 PushButton1 = new QPushButton (GroupBox1);
583 PushButton2 = new QPushButton (GroupBox1);
585 LineEdit1 = new QLineEdit(GroupBox1);
586 LineEdit2 = new QLineEdit(GroupBox1);
588 gridLayout1->addWidget(TextLabel1, 0, 0, 1, 1);
589 gridLayout1->addWidget(TextLabel2, 1, 0, 1, 1);
590 gridLayout1->addWidget(PushButton1, 0, 1, 1, 1);
591 gridLayout1->addWidget(PushButton2, 1, 1, 1, 1);
592 gridLayout1->addWidget(LineEdit1, 0, 2, 1, 1);
593 gridLayout1->addWidget(LineEdit2, 1, 2, 1, 1);
596 TextLabel7 = new QLabel (GroupBox1);
598 ComboBox1 = new QComboBox (GroupBox1);
600 gridLayout1->addWidget(TextLabel7, 2, 0, 1, 1);
601 gridLayout1->addWidget(ComboBox1, 2, 1, 1, 2);
604 TextLabel3 = new QLabel (GroupBox1);
605 TextLabel4 = new QLabel (GroupBox1);
606 TextLabel5 = new QLabel (GroupBox1);
607 TextLabel6 = new QLabel (GroupBox1);
609 LineEdit3 = new QLineEdit(GroupBox1);
610 LineEdit4 = new QLineEdit(GroupBox1);
611 LineEdit5 = new QLineEdit(GroupBox1);
612 LineEdit6 = new QLineEdit(GroupBox1);
614 gridLayout1->addWidget(TextLabel3, 3, 0, 1, 1);
615 gridLayout1->addWidget(TextLabel4, 4, 0, 1, 1);
616 gridLayout1->addWidget(TextLabel5, 5, 0, 1, 1);
617 gridLayout1->addWidget(TextLabel6, 6, 0, 1, 1);
619 gridLayout1->addWidget(LineEdit3, 3, 1, 1, 2);
620 gridLayout1->addWidget(LineEdit4, 4, 1, 1, 2);
621 gridLayout1->addWidget(LineEdit5, 5, 1, 1, 2);
622 gridLayout1->addWidget(LineEdit6, 6, 1, 1, 2);
624 gridLayout->addWidget(GroupBox1, 0, 0, 1, 1);
627 //=================================================================================
628 // function : ~MeasureGUI_DistanceGroup()
629 // purpose : Destroys the object and frees any allocated resources
630 //=================================================================================
631 MeasureGUI_DistanceGroup::~MeasureGUI_DistanceGroup()
633 // no need to delete child widgets, Qt does it all for us