Back to index

salome-geom  6.5.0
MeasureGUI_AngleDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // GEOM GEOMGUI : GUI for Geometry component
00021 // File   : MeasureGUI_AngleDlg.cxx
00022 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
00023 
00024 #include "MeasureGUI_AngleDlg.h"
00025 #include "MeasureGUI_Widgets.h"
00026 
00027 #include <DlgRef.h>
00028 #include <GEOMBase.h>
00029 #include <GeometryGUI.h>
00030 
00031 #include <Basics_OCCTVersion.hxx>
00032 
00033 #include <SUIT_Session.h>
00034 #include <SUIT_Desktop.h>
00035 #include <SUIT_ResourceMgr.h>
00036 #include <SUIT_MessageBox.h>
00037 #include <SUIT_ViewWindow.h>
00038 #include <SUIT_ViewManager.h>
00039 #include <SOCC_Prs.h>
00040 #include <SOCC_ViewModel.h>
00041 #include <SalomeApp_Tools.h>
00042 #include <SalomeApp_Application.h>
00043 #include <LightApp_SelectionMgr.h>
00044 
00045 // OCCT Includes
00046 #include <AIS_AngleDimension.hxx>
00047 #include <AIS_ListIteratorOfListOfInteractive.hxx>
00048 #include <BRep_Tool.hxx>
00049 #include <BRepBuilderAPI_MakeEdge.hxx>
00050 #include <BRepBuilderAPI_MakeVertex.hxx>
00051 #include <TopExp.hxx>
00052 #include <TopoDS.hxx>
00053 #include <TopoDS_Edge.hxx>
00054 #include <Geom_Plane.hxx>
00055 #include <gce_MakePln.hxx>
00056 #include <Precision.hxx>
00057 #include <AIS.hxx>
00058 #include <AIS_Drawer.hxx>
00059 #include <Prs3d_AngleAspect.hxx>
00060 #include <Prs3d_LineAspect.hxx>
00061 
00062 // QT Includes
00063 #include <qlineedit.h>
00064 #include <qlabel.h>
00065 #include <qlayout.h>
00066 #include <qpushbutton.h>
00067 #include <qradiobutton.h>
00068 #include <qbuttongroup.h>
00069 
00070 #include "GEOMImpl_Types.hxx"
00071 
00072 #include "utilities.h"
00073 
00074 #include <Standard_Failure.hxx>
00075 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
00076 
00077 //=================================================================================
00078 // class    : MeasureGUI_AngleDlg()
00079 // purpose  : Constructs a MeasureGUI_AngleDlg which is a child of 'parent', with the
00080 //            name 'name' and widget flags set to 'f'.
00081 //            The dialog will by default be modeless, unless you set 'modal' to
00082 //            TRUE to construct a modal dialog.
00083 //=================================================================================
00084 MeasureGUI_AngleDlg::MeasureGUI_AngleDlg (GeometryGUI* GUI, QWidget* parent)
00085   : MeasureGUI_Skeleton(GUI, parent)
00086 {
00087   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
00088   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_ANGLE")));
00089   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
00090 
00091   setWindowTitle(tr("GEOM_MEASURE_ANGLE_TITLE"));
00092 
00093   /***************************************************************/
00094   mainFrame()->GroupConstructors->setTitle(tr("GEOM_MEASURE_ANGLE_ANGLE"));
00095   mainFrame()->RadioButton1->setIcon(image0);
00096 
00097   myGrp = new MeasureGUI_2Sel1LineEdit(centralWidget());
00098   myGrp->GroupBox1->setTitle(tr("GEOM_MEASURE_ANGLE_OBJ"));
00099   myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
00100   myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
00101   myGrp->TextLabel3->setText(tr("GEOM_MEASURE_ANGLE_IS"));
00102   myGrp->LineEdit3->setReadOnly(true);
00103   myGrp->PushButton1->setIcon(image1);
00104   myGrp->PushButton2->setIcon(image1);
00105   myGrp->LineEdit1->setReadOnly(true);
00106   myGrp->LineEdit2->setReadOnly(true);
00107 
00108   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
00109   layout->setMargin(0); layout->setSpacing(6);
00110   layout->addWidget(myGrp);
00111   /***************************************************************/
00112 
00113   // Help page reference
00114   myHelpFileName = "using_measurement_tools_page.html#angle_anchor";
00115 
00116   // Initialisation
00117   Init();
00118 }
00119 
00120 //=================================================================================
00121 // function : ~MeasureGUI_AngleDlg()
00122 // purpose  : Destroys the object and frees any allocated resources
00123 //=================================================================================
00124 MeasureGUI_AngleDlg::~MeasureGUI_AngleDlg()
00125 {
00126 }
00127 
00128 //=================================================================================
00129 // function : Init()
00130 // purpose  :
00131 //=================================================================================
00132 void MeasureGUI_AngleDlg::Init()
00133 {
00134   // init variables
00135   myGrp->LineEdit1->setText("");
00136   myGrp->LineEdit2->setText("");
00137   myObj = myObj2 = GEOM::GEOM_Object::_nil();
00138 
00139   mySelBtn   = myGrp->PushButton1;
00140   mySelEdit  = myGrp->LineEdit1;
00141   mySelBtn2  = myGrp->PushButton2;
00142   mySelEdit2 = myGrp->LineEdit2;
00143 
00144   myEditCurrentArgument = mySelEdit;
00145 
00146   // signals and slots connections
00147   connect(mySelEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
00148   connect(mySelBtn2,  SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
00149 
00150   globalSelection(GEOM_LINE);
00151   MeasureGUI_Skeleton::Init();
00152 }
00153 
00154 //=================================================================================
00155 // function : SelectionIntoArgument()
00156 // purpose  : Called when selection has changed
00157 //=================================================================================
00158 void MeasureGUI_AngleDlg::SelectionIntoArgument()
00159 {
00160   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
00161   SALOME_ListIO aSelList;
00162   aSelMgr->selectedObjects(aSelList);
00163 
00164   GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
00165 
00166   if (aSelList.Extent() > 0) {
00167     aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
00168   }
00169 
00170   // clear selection
00171   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00172   myGeomGUI->getApp()->selectionMgr()->clearSelected();
00173   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
00174           this, SLOT(SelectionIntoArgument()));
00175 
00176   if (myEditCurrentArgument == mySelEdit) {
00177     myObj = aSelectedObject;
00178     if (!myObj->_is_nil() && myObj2->_is_nil())
00179       myGrp->PushButton2->click();
00180   }
00181   else {
00182     myObj2 = aSelectedObject;
00183     if (!myObj2->_is_nil() && myObj->_is_nil())
00184       myGrp->PushButton1->click();
00185   }
00186 
00187   processObject();
00188 }
00189 
00190 //=================================================================================
00191 // function : processObject()
00192 // purpose  : Fill dialogs fields in accordance with myObj and myObj2
00193 //=================================================================================
00194 void MeasureGUI_AngleDlg::processObject()
00195 {
00196   myGrp->LineEdit1->setText(!myObj->_is_nil()  ? GEOMBase::GetName(myObj ) : "");
00197   myGrp->LineEdit2->setText(!myObj2->_is_nil() ? GEOMBase::GetName(myObj2) : "");
00198 
00199   double anAngle = 0.;
00200   if (getParameters(anAngle)) {
00201     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
00202     int aPrecision = resMgr->integerValue( "Geometry", "angle_precision", 6 );
00203     myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(anAngle, aPrecision));
00204     redisplayPreview();
00205   }
00206   else {
00207     myGrp->LineEdit3->setText("");
00208     erasePreview();
00209   }
00210 }
00211 
00212 //=================================================================================
00213 // function : getParameters()
00214 // purpose  : Get angle between objects
00215 //=================================================================================
00216 bool MeasureGUI_AngleDlg::getParameters (double& theAngle)
00217 {
00218   QString msg;
00219   if (isValid(msg)) {
00220     GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() );
00221     try {
00222       theAngle = anOper->GetAngle(myObj, myObj2);
00223     }
00224     catch(const SALOME::SALOME_Exception& e) {
00225       SalomeApp_Tools::QtCatchCorbaException(e);
00226       return false;
00227     }
00228 
00229     bool isDone = anOper->IsDone();
00230     if (!isDone) {
00231       CORBA::String_var aMsg = anOper->GetErrorCode();
00232       SUIT_MessageBox::warning(this,
00233                                QObject::tr("WRN_WARNING"),
00234                                QObject::tr(aMsg.in()));
00235     }
00236     return isDone;
00237   }
00238 
00239   return false;
00240 }
00241 
00242 //=================================================================================
00243 // function : SetEditCurrentArgument()
00244 // purpose  :
00245 //=================================================================================
00246 void MeasureGUI_AngleDlg::SetEditCurrentArgument()
00247 {
00248   QPushButton* send = (QPushButton*)sender();
00249 
00250   if (send == myGrp->PushButton1) {
00251     myEditCurrentArgument = myGrp->LineEdit1;
00252 
00253     myGrp->PushButton2->setDown(false);
00254     myGrp->LineEdit2->setEnabled(false);
00255   }
00256   else {
00257     myEditCurrentArgument = myGrp->LineEdit2;
00258 
00259     myGrp->PushButton1->setDown(false);
00260     myGrp->LineEdit1->setEnabled(false);
00261   }
00262 
00263   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
00264   globalSelection(GEOM_LINE);
00265   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
00266           this, SLOT(SelectionIntoArgument()));
00267 
00268   // enable line edit
00269   myEditCurrentArgument->setEnabled(true);
00270   myEditCurrentArgument->setFocus();
00271   // after setFocus(), because it will be setDown(false) when loses focus
00272   send->setDown(true);
00273 
00274   // seems we need it only to avoid preview disappearing, caused by selection mode change
00275   redisplayPreview();
00276 }
00277 
00278 //=================================================================================
00279 // function : LineEditReturnPressed()
00280 // purpose  :
00281 //=================================================================================
00282 void MeasureGUI_AngleDlg::LineEditReturnPressed()
00283 {
00284   QLineEdit* send = (QLineEdit*)sender();
00285 
00286   if (send == mySelEdit)
00287     myEditCurrentArgument = mySelEdit;
00288   else
00289     myEditCurrentArgument = mySelEdit2;
00290 
00291   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
00292   SALOME_ListIO aSelList;
00293   aSelMgr->selectedObjects(aSelList);
00294 
00295   if (GEOMBase::SelectionByNameInDialogs(this, mySelEdit->text(), aSelList))
00296     mySelEdit->setText(mySelEdit->text());
00297 }
00298 
00299 //=================================================================================
00300 // function : buildPrs()
00301 // purpose  :
00302 //=================================================================================
00303 SALOME_Prs* MeasureGUI_AngleDlg::buildPrs()
00304 {
00305   double anAngle = 0.;
00306 
00307   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
00308 
00309   if (myObj->_is_nil() || myObj2->_is_nil() || !getParameters(anAngle) ||
00310        vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
00311     return 0;
00312 
00313   if (anAngle > Precision::Angular()) {
00314     try {
00315 #if OCC_VERSION_LARGE > 0x06010000
00316       OCC_CATCH_SIGNALS;
00317 #endif
00318       TopoDS_Shape S1, S2;
00319       if (GEOMBase::GetShape(myObj , S1, TopAbs_EDGE) &&
00320            GEOMBase::GetShape(myObj2, S2, TopAbs_EDGE)) {
00321         TopoDS_Edge anEdge1 = TopoDS::Edge(S1);
00322         TopoDS_Edge anEdge2 = TopoDS::Edge(S2);
00323 
00324         // Build a plane for angle dimension presentation {P11, P12, P3}
00325         TopoDS_Vertex V11, V12, V21, V22;
00326         TopExp::Vertices(anEdge1, V11, V12);
00327         TopExp::Vertices(anEdge2, V21, V22);
00328 
00329         gp_Pnt aP11 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge1));
00330         gp_Pnt aP12 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge1));
00331 
00332         gp_Pnt aP21 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge2));
00333         gp_Pnt aP22 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge2));
00334 
00335         //        *P3
00336         //         \
00337         //          \
00338         //           *P22
00339         //          /
00340         //         /
00341         // P11    /         P12
00342         // *-----/----------*
00343         //  \   /
00344         //   \ /
00345         //    *P21
00346         gp_Pnt aP3 (aP22.XYZ() + aP11.XYZ() - aP21.XYZ());
00347 
00348         gce_MakePln gce_MP(aP11, aP12, aP3);
00349         Handle(Geom_Plane) aPlane = new Geom_Plane(gce_MP.Value());
00350 
00351         // Build the angle dimension presentation
00352         QString aLabel;
00353         aLabel.sprintf("%.1f", anAngle);
00354 
00355         Handle(AIS_AngleDimension) anIO = new AIS_AngleDimension
00356           (anEdge1, anEdge2, aPlane, anAngle * M_PI / 180.,
00357            TCollection_ExtendedString((Standard_CString)aLabel.toLatin1().data()));
00358         Handle(Geom_Line) geom_lin1,geom_lin2;
00359         gp_Pnt ptat11,ptat12,ptat21,ptat22;
00360         Standard_Boolean isInfinite1,isInfinite2;
00361         Handle(Geom_Curve) extCurv;
00362         Standard_Integer extShape;
00363         if (AIS::ComputeGeometry(anEdge1,
00364                                   anEdge2,
00365                                   extShape,
00366                                   geom_lin1,
00367                                   geom_lin2,
00368                                   ptat11,
00369                                   ptat12,
00370                                   ptat21,
00371                                   ptat22,
00372                                   extCurv,
00373                                   isInfinite1,
00374                                   isInfinite2,
00375                                   aPlane)) {
00376           Standard_Real arrSize1 = anIO->ArrowSize();
00377           Standard_Real arrSize2 = anIO->ArrowSize();
00378           if (!isInfinite1) arrSize1 = ptat11.Distance(ptat12)/10.;
00379           if (!isInfinite2) arrSize2 = ptat21.Distance(ptat22)/10.;
00380           Standard_Real arrowSize = Max(arrSize1,arrSize2);
00381           anIO->SetArrowSize(arrowSize);
00382         }
00383 
00384        SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
00385        int w = resMgr->integerValue("Geometry", "measures_line_width", 1);
00386   Handle(Prs3d_AngleAspect) asp = new Prs3d_AngleAspect();
00387   asp->LineAspect()->SetWidth(w);
00388        anIO->Attributes()->SetAngleAspect(asp);
00389 
00390         SOCC_Prs* aPrs =
00391           dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
00392 
00393         if (aPrs)
00394           aPrs->AddObject(anIO);
00395 
00396         return aPrs;
00397       }
00398     }
00399     catch(Standard_Failure) {
00400     }
00401   }
00402 
00403   return 0;
00404 }
00405 
00406 //=================================================================================
00407 // function : isValid()
00408 // purpose  :
00409 //=================================================================================
00410 bool MeasureGUI_AngleDlg::isValid (QString& msg)
00411 {
00412   return MeasureGUI_Skeleton::isValid(msg) && !myObj2->_is_nil();
00413 }