1 // Copyright (C) 2007-2015 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 <AIS_Drawer.hxx>
51 #include <Prs3d_LineAspect.hxx>
52 #include <BRepBuilderAPI_MakeEdge.hxx>
53 #include <BRepBuilderAPI_MakeVertex.hxx>
54 #include <gce_MakePln.hxx>
55 #include <Precision.hxx>
57 //=================================================================================
58 // class : MeasureGUI_DistanceDlg()
59 // purpose : Constructs a MeasureGUI_DistanceDlg which is a child of 'parent', with the
60 // name 'name' and widget flags set to 'f'.
61 // The dialog will by default be modeless, unless you set 'modal' to
62 // true to construct a modal dialog.
63 //=================================================================================
64 MeasureGUI_DistanceDlg::MeasureGUI_DistanceDlg (GeometryGUI* GUI, QWidget* parent)
65 : GEOMBase_Skeleton(GUI, parent)
67 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
68 QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_MINDIST")));
69 QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
71 setWindowTitle(tr("GEOM_MINDIST_TITLE"));
73 /***************************************************************/
74 mainFrame()->GroupConstructors->setTitle(tr("GEOM_DISTANCE"));
75 mainFrame()->RadioButton1->setIcon(image0);
76 mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
77 mainFrame()->RadioButton2->close();
78 mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
79 mainFrame()->RadioButton3->close();
81 mainFrame()->GroupBoxName->hide();
83 myGrp = new MeasureGUI_DistanceGroup (centralWidget());
84 myGrp->GroupBox1->setTitle(tr("GEOM_MINDIST_OBJ"));
87 myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
88 myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
89 myGrp->PushButton1->setIcon(image1);
90 myGrp->PushButton2->setIcon(image1);
91 myGrp->LineEdit1->setReadOnly(true);
92 myGrp->LineEdit2->setReadOnly(true);
95 myGrp->TextLabel7->setText(tr("GEOM_SOLUTION"));
97 // Distance, dx, dy and dz
98 myGrp->TextLabel3->setText(tr("GEOM_LENGTH"));
99 myGrp->TextLabel4->setText(tr("GEOM_DX"));
100 myGrp->TextLabel5->setText(tr("GEOM_DY"));
101 myGrp->TextLabel6->setText(tr("GEOM_DZ"));
102 myGrp->LineEdit3->setReadOnly(true);
103 myGrp->LineEdit4->setReadOnly(true);
104 myGrp->LineEdit5->setReadOnly(true);
105 myGrp->LineEdit6->setReadOnly(true);
106 /***************************************************************/
108 myHelpFileName = "min_distance_page.html";
114 //=================================================================================
115 // function : ~MeasureGUI_DistanceDlg()
116 // purpose : Destroys the object and frees any allocated resources
117 //=================================================================================
118 MeasureGUI_DistanceDlg::~MeasureGUI_DistanceDlg()
122 //=================================================================================
125 //=================================================================================
126 void MeasureGUI_DistanceDlg::Init()
128 myEditCurrentArgument = myGrp->LineEdit1;
129 myDbls = new GEOM::ListOfDouble();
131 myGrp->LineEdit1->setEnabled(true);
132 myGrp->LineEdit2->setEnabled(false);
137 // signals and slots connections
138 connect(buttonOk(), SIGNAL(clicked()), this, SLOT(ClickOnOk()));
139 connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
141 connect(myGrp->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
142 connect(myGrp->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
144 connect(myGrp->ComboBox1, SIGNAL(currentIndexChanged(int)), this, SLOT(SolutionSelected(int)));
146 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
147 this, SLOT(SelectionIntoArgument()));
150 SelectionIntoArgument();
153 //=================================================================================
154 // function : ClickOnOk()
156 //=================================================================================
157 void MeasureGUI_DistanceDlg::ClickOnOk()
163 //=================================================================================
164 // function : ClickOnApply()
166 //=================================================================================
167 bool MeasureGUI_DistanceDlg::ClickOnApply()
177 //=================================================================================
178 // function : ActivateThisDialog()
180 //=================================================================================
181 void MeasureGUI_DistanceDlg::ActivateThisDialog()
183 GEOMBase_Skeleton::ActivateThisDialog();
185 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
186 this, SLOT(SelectionIntoArgument()));
192 //=================================================================================
193 // function : enterEvent()
195 //=================================================================================
196 void MeasureGUI_DistanceDlg::enterEvent(QEvent*)
198 if (!mainFrame()->GroupConstructors->isEnabled())
199 ActivateThisDialog();
202 //=================================================================================
203 // function : SolutionSelected()
204 // purpose : Called when ComboBox selection has changed
205 //=================================================================================
206 void MeasureGUI_DistanceDlg::SolutionSelected (int i)
208 if (i < 0 || myDbls->length() < (i+1)*6) {
209 myGrp->LineEdit3->setText("");
210 myGrp->LineEdit4->setText("");
211 myGrp->LineEdit5->setText("");
212 myGrp->LineEdit6->setText("");
217 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
218 int aPrecision = resMgr->integerValue( "Geometry", "length_precision", 6 );
220 gp_Pnt p1 (myDbls[i*6 + 0], myDbls[i*6 + 1], myDbls[i*6 + 2]);
221 gp_Pnt p2 (myDbls[i*6 + 3], myDbls[i*6 + 4], myDbls[i*6 + 5]);
223 double aDist = p1.Distance(p2);
224 myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(aDist, aPrecision));
226 gp_XYZ aVec = p2.XYZ() - p1.XYZ();
227 myGrp->LineEdit4->setText(DlgRef::PrintDoubleValue(aVec.X(), aPrecision));
228 myGrp->LineEdit5->setText(DlgRef::PrintDoubleValue(aVec.Y(), aPrecision));
229 myGrp->LineEdit6->setText(DlgRef::PrintDoubleValue(aVec.Z(), aPrecision));
234 //=================================================================================
235 // function : SelectionIntoArgument()
236 // purpose : Called when selection has changed
237 //=================================================================================
238 void MeasureGUI_DistanceDlg::SelectionIntoArgument()
240 QList<TopAbs_ShapeEnum> aTypes;
241 aTypes << TopAbs_VERTEX << TopAbs_EDGE << TopAbs_WIRE << TopAbs_FACE << TopAbs_SHELL << TopAbs_SOLID << TopAbs_COMPSOLID << TopAbs_COMPOUND << TopAbs_SHAPE;
242 GEOM::GeomObjPtr aSelectedObject = getSelected( aTypes );
245 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
246 myGeomGUI->getApp()->selectionMgr()->clearSelected();
247 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
248 this, SLOT(SelectionIntoArgument()));
250 if (myEditCurrentArgument == myGrp->LineEdit1) {
251 myObj1 = aSelectedObject;
252 if (myObj1 && !myObj2)
253 myGrp->PushButton2->click();
256 myObj2 = aSelectedObject;
257 if (myObj2 && !myObj1)
258 myGrp->PushButton1->click();
264 //=================================================================================
265 // function : SetEditCurrentArgument()
267 //=================================================================================
268 void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
270 QPushButton* send = (QPushButton*)sender();
272 if (send == myGrp->PushButton1) {
273 myEditCurrentArgument = myGrp->LineEdit1;
275 myGrp->PushButton2->setDown(false);
276 myGrp->LineEdit2->setEnabled(false);
279 myEditCurrentArgument = myGrp->LineEdit2;
281 myGrp->PushButton1->setDown(false);
282 myGrp->LineEdit1->setEnabled(false);
286 myEditCurrentArgument->setEnabled(true);
287 myEditCurrentArgument->setFocus();
288 // after setFocus(), because it will be setDown(false) when loses focus
291 // seems we need it only to avoid preview disappearing, caused by selection mode change
295 //=================================================================================
296 // function : processObject()
297 // purpose : Fill dialogs fields in accordance with myObj1 and myObj2
298 //=================================================================================
299 void MeasureGUI_DistanceDlg::processObject()
301 myGrp->LineEdit1->setText(myObj1 ? GEOMBase::GetName(myObj1.get()) : "");
302 myGrp->LineEdit2->setText(myObj2 ? GEOMBase::GetName(myObj2.get()) : "");
304 myGrp->ComboBox1->clear();
311 if (!isValid(msg)) return;
313 GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow(getOperation());
315 nbSols = anOper->ClosestPoints(myObj1.get(), myObj2.get(), myDbls);
317 catch (const SALOME::SALOME_Exception& e) {
318 SalomeApp_Tools::QtCatchCorbaException(e);
322 if (!anOper->IsDone())
323 myGrp->ComboBox1->addItem(tr(anOper->GetErrorCode()));
324 else if (nbSols <= 0)
325 myGrp->ComboBox1->addItem(tr("GEOM_MINDIST_NO_SOL"));
327 for (int i = 0; i < nbSols; i++) {
328 myGrp->ComboBox1->addItem(tr("GEOM_SOLUTION_I").arg(i + 1));
330 myGrp->ComboBox1->setCurrentIndex(0);
334 //=================================================================================
335 // function : buildPrs()
337 //=================================================================================
338 SALOME_Prs* MeasureGUI_DistanceDlg::buildPrs()
340 SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
342 int currSol = myGrp->ComboBox1->currentIndex();
344 if (!myObj1 || !myObj2 ||
345 currSol == -1 || (currSol+1)*6 > myDbls->length() ||
346 vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
349 gp_Pnt aPnt1 (myDbls[currSol*6 + 0], myDbls[currSol*6 + 1], myDbls[currSol*6 + 2]);
350 gp_Pnt aPnt2 (myDbls[currSol*6 + 3], myDbls[currSol*6 + 4], myDbls[currSol*6 + 5]);
352 double aDist = aPnt1.Distance(aPnt2);
356 if (aDist <= 1.e-9) {
357 BRepBuilderAPI_MakeVertex aMaker (aPnt1);
358 return getDisplayer()->BuildPrs(aMaker.Vertex());
361 BRepBuilderAPI_MakeEdge MakeEdge (aPnt1, aPnt2);
362 TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex(aPnt1);
363 TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex(aPnt2);
366 //aLabel.sprintf("%.1f", aDist);
368 gp_Pnt aPnt3 ((aPnt1.X() + aPnt2.X()) / 2,
369 (aPnt1.Y() + aPnt2.Y()) / 2,
370 (aPnt1.Z() + aPnt2.Z()) / 2);
372 gp_Vec va (aPnt3, aPnt1);
373 gp_Vec vb (aPnt3, aPnt2);
375 if (va.IsParallel(vb, Precision::Angular())) {
376 aPnt3.SetY((aPnt1.Y() + aPnt2.Y()) / 2 + 100);
377 aPnt3.SetZ((aPnt1.Z() + aPnt2.Z()) / 2);
380 gce_MakePln gce_MP (aPnt1, aPnt2, aPnt3);
381 Handle(Geom_Plane) P = new Geom_Plane (gce_MP.Value());
383 Handle(AIS_LengthDimension) anIO = new AIS_LengthDimension( aVert1, aVert2, P->Pln() );
385 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
386 int w = resMgr->integerValue( "Geometry", "measures_line_width", 1 );
388 Handle(Prs3d_DimensionAspect) aDimensionStyle = new Prs3d_DimensionAspect;
390 aDimensionStyle->ArrowAspect()->SetLength( aDist / 20.0 );
391 aDimensionStyle->LineAspect()->SetWidth( w );
392 aDimensionStyle->SetTextHorizontalPosition( Prs3d_DTHP_Center );
393 aDimensionStyle->SetTextVerticalPosition( Prs3d_DTVP_Center );
394 aDimensionStyle->MakeText3d( Standard_False );
395 aDimensionStyle->MakeArrows3d( Standard_True );
397 anIO->SetFlyout( 0.0 );
398 anIO->SetDimensionAspect( aDimensionStyle );
400 SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
403 aPrs->AddObject(anIO);
408 catch (Standard_Failure) {
413 //=================================================================================
414 // function : createOperation
416 //=================================================================================
417 GEOM::GEOM_IOperations_ptr MeasureGUI_DistanceDlg::createOperation()
419 return getGeomEngine()->GetIMeasureOperations(getStudyId());
422 //=================================================================================
423 // function : isValid()
425 //=================================================================================
426 bool MeasureGUI_DistanceDlg::isValid (QString& msg)
428 return myObj1 && myObj2;
431 //=================================================================================
432 // function : execute
434 //=================================================================================
435 bool MeasureGUI_DistanceDlg::execute (ObjectList& objects)
437 GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow(getOperation());
438 GEOM::GEOM_IBasicOperations_var aBasicOper = getGeomEngine()->GetIBasicOperations(getStudyId());
440 GEOM::ListOfDouble_var aDbls;
441 int nbSols = anOper->ClosestPoints(myObj1.get(), myObj2.get(), aDbls);
443 if (anOper->IsDone()) {
444 bool doPublishAll = true;
446 QMessageBox::StandardButton anAnswer =
447 SUIT_MessageBox::question(this, tr("GEOM_MINDIST_PUBLISH_TITLE"),
448 tr("GEOM_MINDIST_PUBLISH_TEXT"),
449 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
451 if (anAnswer == QMessageBox::No)
452 doPublishAll = false;
453 else if (anAnswer != QMessageBox::Yes)
457 for (int i = 0; i < nbSols; i++) {
458 GEOM::GEOM_Object_var anObj1 =
459 aBasicOper->MakePointXYZ(aDbls[i*6 + 0], aDbls[i*6 + 1], aDbls[i*6 + 2]);
460 GEOM::GEOM_Object_var anObj2 =
461 aBasicOper->MakePointXYZ(aDbls[i*6 + 3], aDbls[i*6 + 4], aDbls[i*6 + 5]);
463 if (!anObj1->_is_nil() && !anObj2->_is_nil()) {
464 objects.push_back(anObj1._retn());
465 objects.push_back(anObj2._retn());
470 int i = myGrp->ComboBox1->currentIndex();
471 GEOM::GEOM_Object_var anObj1 =
472 aBasicOper->MakePointXYZ(aDbls[i*6 + 0], aDbls[i*6 + 1], aDbls[i*6 + 2]);
473 GEOM::GEOM_Object_var anObj2 =
474 aBasicOper->MakePointXYZ(aDbls[i*6 + 3], aDbls[i*6 + 4], aDbls[i*6 + 5]);
476 if (!anObj1->_is_nil() && !anObj2->_is_nil()) {
477 objects.push_back(anObj1._retn());
478 objects.push_back(anObj2._retn());
486 //=================================================================================
487 // function : activateSelection()
489 //=================================================================================
490 void MeasureGUI_DistanceDlg::activateSelection()
492 globalSelection( GEOM_ALLSHAPES );
493 std::list<int> needTypes;
494 needTypes.push_back( TopAbs_SHAPE ), needTypes.push_back( TopAbs_VERTEX ), needTypes.push_back( TopAbs_EDGE );
495 needTypes.push_back( TopAbs_WIRE ), needTypes.push_back( TopAbs_FACE ), needTypes.push_back( TopAbs_SHELL );
496 needTypes.push_back( TopAbs_SOLID ), needTypes.push_back( TopAbs_COMPSOLID ), needTypes.push_back( TopAbs_COMPOUND );
497 localSelection(GEOM::GEOM_Object::_nil(), needTypes );
500 //=================================================================================
501 // function : redisplayPreview()
503 //=================================================================================
504 void MeasureGUI_DistanceDlg::redisplayPreview()
507 if (!isValid(aMess)) {
515 SUIT_OverrideCursor();
517 getDisplayer()->SetColor(Quantity_NOC_VIOLET);
518 getDisplayer()->SetToActivate(false);
520 if (SALOME_Prs* aPrs = buildPrs())
521 displayPreview(aPrs);
523 catch (const SALOME::SALOME_Exception& e) {
524 SalomeApp_Tools::QtCatchCorbaException(e);
528 //================================================================
529 // Function : getNewObjectName
530 // Purpose : Redefine this method to return proper name for a new object
531 //================================================================
532 QString MeasureGUI_DistanceDlg::getNewObjectName (int currObj) const
534 QString aName = tr("GEOM_MINDIST_NAME") + QString("_%1_").arg((currObj+1)/2);
535 aName += GEOMBase::GetName(currObj%2 ? myObj1.get() : myObj2.get());
540 //=================================================================================
541 // function : addSubshapeToStudy
542 // purpose : virtual method to add new SubObjects if local selection
543 //=================================================================================
544 void MeasureGUI_DistanceDlg::addSubshapesToStudy()
546 GEOMBase::PublishSubObject( myObj1.get() );
547 GEOMBase::PublishSubObject( myObj2.get() );
550 //=================================================================================
551 // function : MeasureGUI_DistanceGroup
553 //=================================================================================
554 MeasureGUI_DistanceGroup::MeasureGUI_DistanceGroup (QWidget *parent)
556 gridLayout = new QGridLayout (parent);
557 gridLayout->setSpacing(6);
558 gridLayout->setContentsMargins(11, 11, 11, 11);
559 gridLayout->setHorizontalSpacing(0);
560 gridLayout->setVerticalSpacing(0);
561 gridLayout->setContentsMargins(0, 0, 0, 0);
563 GroupBox1 = new QGroupBox (parent);
565 gridLayout1 = new QGridLayout (GroupBox1);
566 gridLayout1->setSpacing(6);
567 gridLayout1->setContentsMargins(11, 11, 11, 11);
568 gridLayout1->setHorizontalSpacing(6);
569 gridLayout1->setVerticalSpacing(6);
570 gridLayout1->setContentsMargins(9, 9, 9, 9);
573 TextLabel1 = new QLabel(GroupBox1);
574 TextLabel2 = new QLabel(GroupBox1);
576 PushButton1 = new QPushButton (GroupBox1);
577 PushButton2 = new QPushButton (GroupBox1);
579 LineEdit1 = new QLineEdit(GroupBox1);
580 LineEdit2 = new QLineEdit(GroupBox1);
582 gridLayout1->addWidget(TextLabel1, 0, 0, 1, 1);
583 gridLayout1->addWidget(TextLabel2, 1, 0, 1, 1);
584 gridLayout1->addWidget(PushButton1, 0, 1, 1, 1);
585 gridLayout1->addWidget(PushButton2, 1, 1, 1, 1);
586 gridLayout1->addWidget(LineEdit1, 0, 2, 1, 1);
587 gridLayout1->addWidget(LineEdit2, 1, 2, 1, 1);
590 TextLabel7 = new QLabel (GroupBox1);
592 ComboBox1 = new QComboBox (GroupBox1);
594 gridLayout1->addWidget(TextLabel7, 2, 0, 1, 1);
595 gridLayout1->addWidget(ComboBox1, 2, 1, 1, 2);
598 TextLabel3 = new QLabel (GroupBox1);
599 TextLabel4 = new QLabel (GroupBox1);
600 TextLabel5 = new QLabel (GroupBox1);
601 TextLabel6 = new QLabel (GroupBox1);
603 LineEdit3 = new QLineEdit(GroupBox1);
604 LineEdit4 = new QLineEdit(GroupBox1);
605 LineEdit5 = new QLineEdit(GroupBox1);
606 LineEdit6 = new QLineEdit(GroupBox1);
608 gridLayout1->addWidget(TextLabel3, 3, 0, 1, 1);
609 gridLayout1->addWidget(TextLabel4, 4, 0, 1, 1);
610 gridLayout1->addWidget(TextLabel5, 5, 0, 1, 1);
611 gridLayout1->addWidget(TextLabel6, 6, 0, 1, 1);
613 gridLayout1->addWidget(LineEdit3, 3, 1, 1, 2);
614 gridLayout1->addWidget(LineEdit4, 4, 1, 1, 2);
615 gridLayout1->addWidget(LineEdit5, 5, 1, 1, 2);
616 gridLayout1->addWidget(LineEdit6, 6, 1, 1, 2);
618 gridLayout->addWidget(GroupBox1, 0, 0, 1, 1);
621 //=================================================================================
622 // function : ~MeasureGUI_DistanceGroup()
623 // purpose : Destroys the object and frees any allocated resources
624 //=================================================================================
625 MeasureGUI_DistanceGroup::~MeasureGUI_DistanceGroup()
627 // no need to delete child widgets, Qt does it all for us