1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // GEOM GEOMGUI : GUI for Geometry component
20 // File : MeasureGUI_AngleDlg.cxx
21 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
23 #include "MeasureGUI_AngleDlg.h"
24 #include "MeasureGUI_Widgets.h"
28 #include <GeometryGUI.h>
30 #include <Basics_OCCTVersion.hxx>
32 #include <SUIT_Session.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_ViewWindow.h>
37 #include <SUIT_ViewManager.h>
39 #include <SOCC_ViewModel.h>
40 #include <SalomeApp_Tools.h>
41 #include <SalomeApp_Application.h>
42 #include <LightApp_SelectionMgr.h>
45 #include <AIS_AngleDimension.hxx>
46 #include <AIS_ListIteratorOfListOfInteractive.hxx>
47 #include <BRep_Tool.hxx>
48 #include <BRepBuilderAPI_MakeEdge.hxx>
49 #include <BRepBuilderAPI_MakeVertex.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <Geom_Plane.hxx>
54 #include <gce_MakePln.hxx>
55 #include <Precision.hxx>
59 #include <qlineedit.h>
62 #include <qpushbutton.h>
63 #include <qradiobutton.h>
64 #include <qbuttongroup.h>
66 #include "GEOMImpl_Types.hxx"
68 #include "utilities.h"
70 #include <Standard_Failure.hxx>
71 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
73 //=================================================================================
74 // class : MeasureGUI_AngleDlg()
75 // purpose : Constructs a MeasureGUI_AngleDlg which is a child of 'parent', with the
76 // name 'name' and widget flags set to 'f'.
77 // The dialog will by default be modeless, unless you set 'modal' to
78 // TRUE to construct a modal dialog.
79 //=================================================================================
80 MeasureGUI_AngleDlg::MeasureGUI_AngleDlg (GeometryGUI* GUI, QWidget* parent)
81 : MeasureGUI_Skeleton(GUI, parent)
83 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
84 QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_ANGLE")));
85 QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
87 setWindowTitle(tr("GEOM_MEASURE_ANGLE_TITLE"));
89 /***************************************************************/
90 mainFrame()->GroupConstructors->setTitle(tr("GEOM_MEASURE_ANGLE_ANGLE"));
91 mainFrame()->RadioButton1->setIcon(image0);
93 myGrp = new MeasureGUI_2Sel1LineEdit(centralWidget());
94 myGrp->GroupBox1->setTitle(tr("GEOM_MEASURE_ANGLE_OBJ"));
95 myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
96 myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
97 myGrp->TextLabel3->setText(tr("GEOM_MEASURE_ANGLE_IS"));
98 myGrp->LineEdit3->setReadOnly(true);
99 myGrp->PushButton1->setIcon(image1);
100 myGrp->PushButton2->setIcon(image1);
101 myGrp->LineEdit1->setReadOnly(true);
102 myGrp->LineEdit2->setReadOnly(true);
104 QVBoxLayout* layout = new QVBoxLayout(centralWidget());
105 layout->setMargin(0); layout->setSpacing(6);
106 layout->addWidget(myGrp);
107 /***************************************************************/
109 // Help page reference
110 myHelpFileName = "using_measurement_tools_page.html#angle_anchor";
116 //=================================================================================
117 // function : ~MeasureGUI_AngleDlg()
118 // purpose : Destroys the object and frees any allocated resources
119 //=================================================================================
120 MeasureGUI_AngleDlg::~MeasureGUI_AngleDlg()
124 //=================================================================================
127 //=================================================================================
128 void MeasureGUI_AngleDlg::Init()
131 myGrp->LineEdit1->setText("");
132 myGrp->LineEdit2->setText("");
133 myObj = myObj2 = GEOM::GEOM_Object::_nil();
135 mySelBtn = myGrp->PushButton1;
136 mySelEdit = myGrp->LineEdit1;
137 mySelBtn2 = myGrp->PushButton2;
138 mySelEdit2 = myGrp->LineEdit2;
140 myEditCurrentArgument = mySelEdit;
142 // signals and slots connections
143 connect(mySelEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
144 connect(mySelBtn2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
146 globalSelection(GEOM_LINE);
147 MeasureGUI_Skeleton::Init();
150 //=================================================================================
151 // function : SelectionIntoArgument()
152 // purpose : Called when selection has changed
153 //=================================================================================
154 void MeasureGUI_AngleDlg::SelectionIntoArgument()
156 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
157 SALOME_ListIO aSelList;
158 aSelMgr->selectedObjects(aSelList);
160 GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
162 if (aSelList.Extent() > 0) {
163 aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
167 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
168 myGeomGUI->getApp()->selectionMgr()->clearSelected();
169 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
170 this, SLOT(SelectionIntoArgument()));
172 if (myEditCurrentArgument == mySelEdit) {
173 myObj = aSelectedObject;
174 if (!myObj->_is_nil() && myObj2->_is_nil())
175 myGrp->PushButton2->click();
178 myObj2 = aSelectedObject;
179 if (!myObj2->_is_nil() && myObj->_is_nil())
180 myGrp->PushButton1->click();
186 //=================================================================================
187 // function : processObject()
188 // purpose : Fill dialogs fields in accordance with myObj and myObj2
189 //=================================================================================
190 void MeasureGUI_AngleDlg::processObject()
192 myGrp->LineEdit1->setText(!myObj->_is_nil() ? GEOMBase::GetName(myObj ) : "");
193 myGrp->LineEdit2->setText(!myObj2->_is_nil() ? GEOMBase::GetName(myObj2) : "");
196 if (getParameters(anAngle)) {
197 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
198 int aPrecision = resMgr->integerValue( "Geometry", "angle_precision", 6 );
199 myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(anAngle, aPrecision));
203 myGrp->LineEdit3->setText("");
208 //=================================================================================
209 // function : getParameters()
210 // purpose : Get angle between objects
211 //=================================================================================
212 bool MeasureGUI_AngleDlg::getParameters (double& theAngle)
216 GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
218 theAngle = anOper->GetAngle(myObj, myObj2);
220 catch(const SALOME::SALOME_Exception& e) {
221 SalomeApp_Tools::QtCatchCorbaException(e);
225 bool isDone = anOper->IsDone();
227 CORBA::String_var aMsg = anOper->GetErrorCode();
228 SUIT_MessageBox::warning(this,
229 QObject::tr("WRN_WARNING"),
230 QObject::tr(aMsg.in()));
238 //=================================================================================
239 // function : SetEditCurrentArgument()
241 //=================================================================================
242 void MeasureGUI_AngleDlg::SetEditCurrentArgument()
244 QPushButton* send = (QPushButton*)sender();
246 if (send == myGrp->PushButton1) {
247 myEditCurrentArgument = myGrp->LineEdit1;
249 myGrp->PushButton2->setDown(false);
250 myGrp->LineEdit2->setEnabled(false);
253 myEditCurrentArgument = myGrp->LineEdit2;
255 myGrp->PushButton1->setDown(false);
256 myGrp->LineEdit1->setEnabled(false);
259 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
260 globalSelection(GEOM_LINE);
261 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
262 this, SLOT(SelectionIntoArgument()));
265 myEditCurrentArgument->setEnabled(true);
266 myEditCurrentArgument->setFocus();
267 // after setFocus(), because it will be setDown(false) when loses focus
270 // seems we need it only to avoid preview disappearing, caused by selection mode change
274 //=================================================================================
275 // function : LineEditReturnPressed()
277 //=================================================================================
278 void MeasureGUI_AngleDlg::LineEditReturnPressed()
280 QLineEdit* send = (QLineEdit*)sender();
282 if (send == mySelEdit)
283 myEditCurrentArgument = mySelEdit;
285 myEditCurrentArgument = mySelEdit2;
287 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
288 SALOME_ListIO aSelList;
289 aSelMgr->selectedObjects(aSelList);
291 if (GEOMBase::SelectionByNameInDialogs(this, mySelEdit->text(), aSelList))
292 mySelEdit->setText(mySelEdit->text());
295 //=================================================================================
296 // function : buildPrs()
298 //=================================================================================
299 SALOME_Prs* MeasureGUI_AngleDlg::buildPrs()
303 SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
305 if (myObj->_is_nil() || myObj2->_is_nil() || !getParameters(anAngle) ||
306 vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
309 if (anAngle > Precision::Angular()) {
311 #if OCC_VERSION_LARGE > 0x06010000
315 if (GEOMBase::GetShape(myObj , S1, TopAbs_EDGE) &&
316 GEOMBase::GetShape(myObj2, S2, TopAbs_EDGE)) {
317 TopoDS_Edge anEdge1 = TopoDS::Edge(S1);
318 TopoDS_Edge anEdge2 = TopoDS::Edge(S2);
320 // Build a plane for angle dimension presentation {P11, P12, P3}
321 TopoDS_Vertex V11, V12, V21, V22;
322 TopExp::Vertices(anEdge1, V11, V12);
323 TopExp::Vertices(anEdge2, V21, V22);
325 gp_Pnt aP11 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge1));
326 gp_Pnt aP12 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge1));
328 gp_Pnt aP21 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge2));
329 gp_Pnt aP22 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge2));
338 // *-----/----------*
342 gp_Pnt aP3 (aP22.XYZ() + aP11.XYZ() - aP21.XYZ());
344 gce_MakePln gce_MP(aP11, aP12, aP3);
345 Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
347 // Build the angle dimension presentation
349 aLabel.sprintf("%.1f", anAngle);
351 Handle(AIS_AngleDimension) anIO = new AIS_AngleDimension
352 (anEdge1, anEdge2, aPlane, anAngle * M_PI / 180.,
353 TCollection_ExtendedString((Standard_CString)aLabel.toLatin1().data()));
354 Handle(Geom_Line) geom_lin1,geom_lin2;
355 gp_Pnt ptat11,ptat12,ptat21,ptat22;
356 Standard_Boolean isInfinite1,isInfinite2;
357 Handle(Geom_Curve) extCurv;
358 Standard_Integer extShape;
359 if (AIS::ComputeGeometry(anEdge1,
372 Standard_Real arrSize1 = anIO->ArrowSize();
373 Standard_Real arrSize2 = anIO->ArrowSize();
374 if (!isInfinite1) arrSize1 = ptat11.Distance(ptat12)/10.;
375 if (!isInfinite2) arrSize2 = ptat21.Distance(ptat22)/10.;
376 Standard_Real arrowSize = Max(arrSize1,arrSize2);
377 anIO->SetArrowSize(arrowSize);
381 dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
384 aPrs->AddObject(anIO);
389 catch(Standard_Failure) {
396 //=================================================================================
397 // function : isValid()
399 //=================================================================================
400 bool MeasureGUI_AngleDlg::isValid (QString& msg)
402 return MeasureGUI_Skeleton::isValid(msg) && !myObj2->_is_nil();