1 // Copyright (C) 2007-2012 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
20 // GEOM GEOMGUI : GUI for Geometry component
21 // File : MeasureGUI_AngleDlg.cxx
22 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
24 #include "MeasureGUI_AngleDlg.h"
25 #include "MeasureGUI_Widgets.h"
29 #include <GeometryGUI.h>
31 #include <Basics_OCCTVersion.hxx>
33 #include <SUIT_Session.h>
34 #include <SUIT_Desktop.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_ViewWindow.h>
38 #include <SUIT_ViewManager.h>
40 #include <SOCC_ViewModel.h>
41 #include <SalomeApp_Tools.h>
42 #include <SalomeApp_Application.h>
43 #include <LightApp_SelectionMgr.h>
46 #include <AIS_AngleDimension.hxx>
47 #include <AIS_ListIteratorOfListOfInteractive.hxx>
48 #include <BRep_Tool.hxx>
49 #include <BRepBuilderAPI_MakeEdge.hxx>
50 #include <BRepBuilderAPI_MakeVertex.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <Geom_Plane.hxx>
55 #include <gce_MakePln.hxx>
56 #include <Precision.hxx>
58 #include <AIS_Drawer.hxx>
59 #include <Prs3d_AngleAspect.hxx>
60 #include <Prs3d_LineAspect.hxx>
63 #include <qlineedit.h>
66 #include <qpushbutton.h>
67 #include <qradiobutton.h>
68 #include <qbuttongroup.h>
70 #include "GEOMImpl_Types.hxx"
72 #include "utilities.h"
74 #include <Standard_Failure.hxx>
75 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
77 //=================================================================================
78 // class : MeasureGUI_AngleDlg()
79 // purpose : Constructs a MeasureGUI_AngleDlg which is a child of 'parent', with the
80 // name 'name' and widget flags set to 'f'.
81 // The dialog will by default be modeless, unless you set 'modal' to
82 // TRUE to construct a modal dialog.
83 //=================================================================================
84 MeasureGUI_AngleDlg::MeasureGUI_AngleDlg (GeometryGUI* GUI, QWidget* parent)
85 : MeasureGUI_Skeleton(GUI, parent)
87 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
88 QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_ANGLE")));
89 QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
91 setWindowTitle(tr("GEOM_MEASURE_ANGLE_TITLE"));
93 /***************************************************************/
94 mainFrame()->GroupConstructors->setTitle(tr("GEOM_MEASURE_ANGLE_ANGLE"));
95 mainFrame()->RadioButton1->setIcon(image0);
97 myGrp = new MeasureGUI_2Sel1LineEdit(centralWidget());
98 myGrp->GroupBox1->setTitle(tr("GEOM_MEASURE_ANGLE_OBJ"));
99 myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
100 myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
101 myGrp->TextLabel3->setText(tr("GEOM_MEASURE_ANGLE_IS"));
102 myGrp->LineEdit3->setReadOnly(true);
103 myGrp->PushButton1->setIcon(image1);
104 myGrp->PushButton2->setIcon(image1);
105 myGrp->LineEdit1->setReadOnly(true);
106 myGrp->LineEdit2->setReadOnly(true);
108 QVBoxLayout* layout = new QVBoxLayout(centralWidget());
109 layout->setMargin(0); layout->setSpacing(6);
110 layout->addWidget(myGrp);
111 /***************************************************************/
113 // Help page reference
114 myHelpFileName = "using_measurement_tools_page.html#angle_anchor";
120 //=================================================================================
121 // function : ~MeasureGUI_AngleDlg()
122 // purpose : Destroys the object and frees any allocated resources
123 //=================================================================================
124 MeasureGUI_AngleDlg::~MeasureGUI_AngleDlg()
128 //=================================================================================
131 //=================================================================================
132 void MeasureGUI_AngleDlg::Init()
135 myGrp->LineEdit1->setText("");
136 myGrp->LineEdit2->setText("");
137 myObj = myObj2 = GEOM::GEOM_Object::_nil();
139 mySelBtn = myGrp->PushButton1;
140 mySelEdit = myGrp->LineEdit1;
141 mySelBtn2 = myGrp->PushButton2;
142 mySelEdit2 = myGrp->LineEdit2;
144 myEditCurrentArgument = mySelEdit;
146 // signals and slots connections
147 connect(mySelEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
148 connect(mySelBtn2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
150 globalSelection(GEOM_LINE);
151 MeasureGUI_Skeleton::Init();
154 //=================================================================================
155 // function : SelectionIntoArgument()
156 // purpose : Called when selection has changed
157 //=================================================================================
158 void MeasureGUI_AngleDlg::SelectionIntoArgument()
160 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
161 SALOME_ListIO aSelList;
162 aSelMgr->selectedObjects(aSelList);
164 GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
166 if (aSelList.Extent() > 0) {
167 aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
171 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
172 myGeomGUI->getApp()->selectionMgr()->clearSelected();
173 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
174 this, SLOT(SelectionIntoArgument()));
176 if (myEditCurrentArgument == mySelEdit) {
177 myObj = aSelectedObject;
178 if (!myObj->_is_nil() && myObj2->_is_nil())
179 myGrp->PushButton2->click();
182 myObj2 = aSelectedObject;
183 if (!myObj2->_is_nil() && myObj->_is_nil())
184 myGrp->PushButton1->click();
190 //=================================================================================
191 // function : processObject()
192 // purpose : Fill dialogs fields in accordance with myObj and myObj2
193 //=================================================================================
194 void MeasureGUI_AngleDlg::processObject()
196 myGrp->LineEdit1->setText(!myObj->_is_nil() ? GEOMBase::GetName(myObj ) : "");
197 myGrp->LineEdit2->setText(!myObj2->_is_nil() ? GEOMBase::GetName(myObj2) : "");
200 if (getParameters(anAngle)) {
201 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
202 int aPrecision = resMgr->integerValue( "Geometry", "angle_precision", 6 );
203 myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(anAngle, aPrecision));
207 myGrp->LineEdit3->setText("");
212 //=================================================================================
213 // function : getParameters()
214 // purpose : Get angle between objects
215 //=================================================================================
216 bool MeasureGUI_AngleDlg::getParameters (double& theAngle)
220 GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
222 theAngle = anOper->GetAngle(myObj, myObj2);
224 catch(const SALOME::SALOME_Exception& e) {
225 SalomeApp_Tools::QtCatchCorbaException(e);
229 bool isDone = anOper->IsDone();
231 CORBA::String_var aMsg = anOper->GetErrorCode();
232 SUIT_MessageBox::warning(this,
233 QObject::tr("WRN_WARNING"),
234 QObject::tr(aMsg.in()));
242 //=================================================================================
243 // function : SetEditCurrentArgument()
245 //=================================================================================
246 void MeasureGUI_AngleDlg::SetEditCurrentArgument()
248 QPushButton* send = (QPushButton*)sender();
250 if (send == myGrp->PushButton1) {
251 myEditCurrentArgument = myGrp->LineEdit1;
253 myGrp->PushButton2->setDown(false);
254 myGrp->LineEdit2->setEnabled(false);
257 myEditCurrentArgument = myGrp->LineEdit2;
259 myGrp->PushButton1->setDown(false);
260 myGrp->LineEdit1->setEnabled(false);
263 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
264 globalSelection(GEOM_LINE);
265 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
266 this, SLOT(SelectionIntoArgument()));
269 myEditCurrentArgument->setEnabled(true);
270 myEditCurrentArgument->setFocus();
271 // after setFocus(), because it will be setDown(false) when loses focus
274 // seems we need it only to avoid preview disappearing, caused by selection mode change
278 //=================================================================================
279 // function : LineEditReturnPressed()
281 //=================================================================================
282 void MeasureGUI_AngleDlg::LineEditReturnPressed()
284 QLineEdit* send = (QLineEdit*)sender();
286 if (send == mySelEdit)
287 myEditCurrentArgument = mySelEdit;
289 myEditCurrentArgument = mySelEdit2;
291 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
292 SALOME_ListIO aSelList;
293 aSelMgr->selectedObjects(aSelList);
295 if (GEOMBase::SelectionByNameInDialogs(this, mySelEdit->text(), aSelList))
296 mySelEdit->setText(mySelEdit->text());
299 //=================================================================================
300 // function : buildPrs()
302 //=================================================================================
303 SALOME_Prs* MeasureGUI_AngleDlg::buildPrs()
307 SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
309 if (myObj->_is_nil() || myObj2->_is_nil() || !getParameters(anAngle) ||
310 vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
313 if (anAngle > Precision::Angular()) {
315 #if OCC_VERSION_LARGE > 0x06010000
319 if (GEOMBase::GetShape(myObj , S1, TopAbs_EDGE) &&
320 GEOMBase::GetShape(myObj2, S2, TopAbs_EDGE)) {
321 TopoDS_Edge anEdge1 = TopoDS::Edge(S1);
322 TopoDS_Edge anEdge2 = TopoDS::Edge(S2);
324 // Build a plane for angle dimension presentation {P11, P12, P3}
325 TopoDS_Vertex V11, V12, V21, V22;
326 TopExp::Vertices(anEdge1, V11, V12);
327 TopExp::Vertices(anEdge2, V21, V22);
329 gp_Pnt aP11 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge1));
330 gp_Pnt aP12 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge1));
332 gp_Pnt aP21 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge2));
333 gp_Pnt aP22 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge2));
342 // *-----/----------*
346 gp_Pnt aP3 (aP22.XYZ() + aP11.XYZ() - aP21.XYZ());
348 gce_MakePln gce_MP(aP11, aP12, aP3);
349 Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
351 // Build the angle dimension presentation
353 aLabel.sprintf("%.1f", anAngle);
355 Handle(AIS_AngleDimension) anIO = new AIS_AngleDimension
356 (anEdge1, anEdge2, aPlane, anAngle * M_PI / 180.,
357 TCollection_ExtendedString((Standard_CString)aLabel.toLatin1().data()));
358 Handle(Geom_Line) geom_lin1,geom_lin2;
359 gp_Pnt ptat11,ptat12,ptat21,ptat22;
360 Standard_Boolean isInfinite1,isInfinite2;
361 Handle(Geom_Curve) extCurv;
362 Standard_Integer extShape;
363 if (AIS::ComputeGeometry(anEdge1,
376 Standard_Real arrSize1 = anIO->ArrowSize();
377 Standard_Real arrSize2 = anIO->ArrowSize();
378 if (!isInfinite1) arrSize1 = ptat11.Distance(ptat12)/10.;
379 if (!isInfinite2) arrSize2 = ptat21.Distance(ptat22)/10.;
380 Standard_Real arrowSize = Max(arrSize1,arrSize2);
381 anIO->SetArrowSize(arrowSize);
384 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
385 int w = resMgr->integerValue("Geometry", "measures_line_width", 1);
386 Handle(Prs3d_AngleAspect) asp = new Prs3d_AngleAspect();
387 asp->LineAspect()->SetWidth(w);
388 anIO->Attributes()->SetAngleAspect(asp);
391 dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
394 aPrs->AddObject(anIO);
399 catch(Standard_Failure) {
406 //=================================================================================
407 // function : isValid()
409 //=================================================================================
410 bool MeasureGUI_AngleDlg::isValid (QString& msg)
412 return MeasureGUI_Skeleton::isValid(msg) && !myObj2->_is_nil();