1 // Copyright (C) 2007-2010 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 <SUIT_Session.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_MessageBox.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_ViewManager.h>
38 #include <SOCC_ViewModel.h>
39 #include <SalomeApp_Tools.h>
40 #include <SalomeApp_Application.h>
41 #include <LightApp_SelectionMgr.h>
44 #include <AIS_AngleDimension.hxx>
45 #include <AIS_ListIteratorOfListOfInteractive.hxx>
46 #include <BRep_Tool.hxx>
47 #include <BRepBuilderAPI_MakeEdge.hxx>
48 #include <BRepBuilderAPI_MakeVertex.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <Geom_Plane.hxx>
53 #include <gce_MakePln.hxx>
54 #include <Precision.hxx>
58 #include <qlineedit.h>
61 #include <qpushbutton.h>
62 #include <qradiobutton.h>
63 #include <qbuttongroup.h>
65 #include "GEOMImpl_Types.hxx"
67 #include "utilities.h"
69 #include <Standard_Failure.hxx>
70 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
72 //=================================================================================
73 // class : MeasureGUI_AngleDlg()
74 // purpose : Constructs a MeasureGUI_AngleDlg which is a child of 'parent', with the
75 // name 'name' and widget flags set to 'f'.
76 // The dialog will by default be modeless, unless you set 'modal' to
77 // TRUE to construct a modal dialog.
78 //=================================================================================
79 MeasureGUI_AngleDlg::MeasureGUI_AngleDlg (GeometryGUI* GUI, QWidget* parent)
80 : MeasureGUI_Skeleton(GUI, parent)
82 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
83 QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_ANGLE")));
84 QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
86 setWindowTitle(tr("GEOM_MEASURE_ANGLE_TITLE"));
88 /***************************************************************/
89 mainFrame()->GroupConstructors->setTitle(tr("GEOM_MEASURE_ANGLE_ANGLE"));
90 mainFrame()->RadioButton1->setIcon(image0);
92 myGrp = new MeasureGUI_2Sel1LineEdit(centralWidget());
93 myGrp->GroupBox1->setTitle(tr("GEOM_MEASURE_ANGLE_OBJ"));
94 myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
95 myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
96 myGrp->TextLabel3->setText(tr("GEOM_MEASURE_ANGLE_IS"));
97 myGrp->LineEdit3->setReadOnly(true);
98 myGrp->PushButton1->setIcon(image1);
99 myGrp->PushButton2->setIcon(image1);
100 myGrp->LineEdit1->setReadOnly(true);
101 myGrp->LineEdit2->setReadOnly(true);
103 QVBoxLayout* layout = new QVBoxLayout(centralWidget());
104 layout->setMargin(0); layout->setSpacing(6);
105 layout->addWidget(myGrp);
106 /***************************************************************/
108 // Help page reference
109 myHelpFileName = "using_measurement_tools_page.html#angle_anchor";
115 //=================================================================================
116 // function : ~MeasureGUI_AngleDlg()
117 // purpose : Destroys the object and frees any allocated resources
118 //=================================================================================
119 MeasureGUI_AngleDlg::~MeasureGUI_AngleDlg()
123 //=================================================================================
126 //=================================================================================
127 void MeasureGUI_AngleDlg::Init()
130 myGrp->LineEdit1->setText("");
131 myGrp->LineEdit2->setText("");
132 myObj = myObj2 = GEOM::GEOM_Object::_nil();
134 mySelBtn = myGrp->PushButton1;
135 mySelEdit = myGrp->LineEdit1;
136 mySelBtn2 = myGrp->PushButton2;
137 mySelEdit2 = myGrp->LineEdit2;
139 myEditCurrentArgument = mySelEdit;
141 // signals and slots connections
142 connect(mySelEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
143 connect(mySelBtn2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
145 globalSelection(GEOM_LINE);
146 MeasureGUI_Skeleton::Init();
149 //=================================================================================
150 // function : SelectionIntoArgument()
151 // purpose : Called when selection has changed
152 //=================================================================================
153 void MeasureGUI_AngleDlg::SelectionIntoArgument()
155 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
156 SALOME_ListIO aSelList;
157 aSelMgr->selectedObjects(aSelList);
159 GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
161 if (aSelList.Extent() > 0) {
162 aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
166 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
167 myGeomGUI->getApp()->selectionMgr()->clearSelected();
168 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
169 this, SLOT(SelectionIntoArgument()));
171 if (myEditCurrentArgument == mySelEdit) {
172 myObj = aSelectedObject;
173 if (!myObj->_is_nil() && myObj2->_is_nil())
174 myGrp->PushButton2->click();
177 myObj2 = aSelectedObject;
178 if (!myObj2->_is_nil() && myObj->_is_nil())
179 myGrp->PushButton1->click();
185 //=================================================================================
186 // function : processObject()
187 // purpose : Fill dialogs fields in accordance with myObj and myObj2
188 //=================================================================================
189 void MeasureGUI_AngleDlg::processObject()
191 myGrp->LineEdit1->setText(!myObj->_is_nil() ? GEOMBase::GetName(myObj ) : "");
192 myGrp->LineEdit2->setText(!myObj2->_is_nil() ? GEOMBase::GetName(myObj2) : "");
195 if (getParameters(anAngle)) {
196 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
197 int aPrecision = resMgr->integerValue( "Geometry", "angle_precision", 6 );
198 myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(anAngle, aPrecision));
202 myGrp->LineEdit3->setText("");
207 //=================================================================================
208 // function : getParameters()
209 // purpose : Get angle between objects
210 //=================================================================================
211 bool MeasureGUI_AngleDlg::getParameters (double& theAngle)
215 GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
217 theAngle = anOper->GetAngle(myObj, myObj2);
219 catch(const SALOME::SALOME_Exception& e) {
220 SalomeApp_Tools::QtCatchCorbaException(e);
224 bool isDone = anOper->IsDone();
226 CORBA::String_var aMsg = anOper->GetErrorCode();
227 SUIT_MessageBox::warning(this,
228 QObject::tr("WRN_WARNING"),
229 QObject::tr(aMsg.in()));
237 //=================================================================================
238 // function : SetEditCurrentArgument()
240 //=================================================================================
241 void MeasureGUI_AngleDlg::SetEditCurrentArgument()
243 QPushButton* send = (QPushButton*)sender();
245 if (send == myGrp->PushButton1) {
246 myEditCurrentArgument = myGrp->LineEdit1;
248 myGrp->PushButton2->setDown(false);
249 myGrp->LineEdit2->setEnabled(false);
252 myEditCurrentArgument = myGrp->LineEdit2;
254 myGrp->PushButton1->setDown(false);
255 myGrp->LineEdit1->setEnabled(false);
258 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
259 globalSelection(GEOM_LINE);
260 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
261 this, SLOT(SelectionIntoArgument()));
264 myEditCurrentArgument->setEnabled(true);
265 myEditCurrentArgument->setFocus();
266 // after setFocus(), because it will be setDown(false) when loses focus
269 // seems we need it only to avoid preview disappearing, caused by selection mode change
273 //=================================================================================
274 // function : LineEditReturnPressed()
276 //=================================================================================
277 void MeasureGUI_AngleDlg::LineEditReturnPressed()
279 QLineEdit* send = (QLineEdit*)sender();
281 if (send == mySelEdit)
282 myEditCurrentArgument = mySelEdit;
284 myEditCurrentArgument = mySelEdit2;
286 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
287 SALOME_ListIO aSelList;
288 aSelMgr->selectedObjects(aSelList);
290 if (GEOMBase::SelectionByNameInDialogs(this, mySelEdit->text(), aSelList))
291 mySelEdit->setText(mySelEdit->text());
294 //=================================================================================
295 // function : buildPrs()
297 //=================================================================================
298 SALOME_Prs* MeasureGUI_AngleDlg::buildPrs()
302 SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
304 if (myObj->_is_nil() || myObj2->_is_nil() || !getParameters(anAngle) ||
305 vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
308 if (anAngle > Precision::Angular()) {
310 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
314 if (GEOMBase::GetShape(myObj , S1, TopAbs_EDGE) &&
315 GEOMBase::GetShape(myObj2, S2, TopAbs_EDGE)) {
316 TopoDS_Edge anEdge1 = TopoDS::Edge(S1);
317 TopoDS_Edge anEdge2 = TopoDS::Edge(S2);
319 // Build a plane for angle dimension presentation {P11, P12, P3}
320 TopoDS_Vertex V11, V12, V21, V22;
321 TopExp::Vertices(anEdge1, V11, V12);
322 TopExp::Vertices(anEdge2, V21, V22);
324 gp_Pnt aP11 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge1));
325 gp_Pnt aP12 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge1));
327 gp_Pnt aP21 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge2));
328 gp_Pnt aP22 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge2));
337 // *-----/----------*
341 gp_Pnt aP3 (aP22.XYZ() + aP11.XYZ() - aP21.XYZ());
343 gce_MakePln gce_MP(aP11, aP12, aP3);
344 Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
346 // Build the angle dimension presentation
348 aLabel.sprintf("%.1f", anAngle);
350 Handle(AIS_AngleDimension) anIO = new AIS_AngleDimension
351 (anEdge1, anEdge2, aPlane, anAngle * PI180,
352 TCollection_ExtendedString((Standard_CString)aLabel.toLatin1().data()));
353 Handle(Geom_Line) geom_lin1,geom_lin2;
354 gp_Pnt ptat11,ptat12,ptat21,ptat22;
355 Standard_Boolean isInfinite1,isInfinite2;
356 Handle(Geom_Curve) extCurv;
357 Standard_Integer extShape;
358 if (AIS::ComputeGeometry(anEdge1,
371 Standard_Real arrSize1 = anIO->ArrowSize();
372 Standard_Real arrSize2 = anIO->ArrowSize();
373 if (!isInfinite1) arrSize1 = ptat11.Distance(ptat12)/10.;
374 if (!isInfinite2) arrSize2 = ptat21.Distance(ptat22)/10.;
375 Standard_Real arrowSize = Max(arrSize1,arrSize2);
376 anIO->SetArrowSize(arrowSize);
380 dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
383 aPrs->AddObject(anIO);
388 catch(Standard_Failure) {
395 //=================================================================================
396 // function : isValid()
398 //=================================================================================
399 bool MeasureGUI_AngleDlg::isValid (QString& msg)
401 return MeasureGUI_Skeleton::isValid(msg) && !myObj2->_is_nil();