Salome HOME
Fix problem with 'Glue all coincident edges' option
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CheckSelfIntersectionsDlg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : MeasureGUI_CheckSelfIntersectionsDlg.cxx
24 // Author : Vladimir KLYACHIN, Open CASCADE S.A.S. (vladimir.klyachin@opencascade.com)
25
26 #include "MeasureGUI_CheckSelfIntersectionsDlg.h"
27 #include "MeasureGUI_Widgets.h"
28
29 #include <SUIT_Session.h>
30 #include <SUIT_ResourceMgr.h>
31 #include <LightApp_SelectionMgr.h>
32 #include <SalomeApp_Tools.h>
33 #include <SalomeApp_Application.h>
34
35 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <TopExp.hxx>
37 #include <TColStd_MapOfInteger.hxx>
38
39 #include <DlgRef.h>
40 #include <GeometryGUI.h>
41 #include <GEOMBase.h>
42 #include <GEOMImpl_Types.hxx>
43
44 #define TEXTEDIT_FONT_FAMILY "Courier"
45 #define TEXTEDIT_FONT_SIZE 11
46
47 //=================================================================================
48 // class    : MeasureGUI_CheckSelfIntersectionsDlg()
49 // purpose  : Constructs a MeasureGUI_CheckSelfIntersectionsDlg which is a child of 'parent', with the
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            true to construct a modal dialog.
53 //=================================================================================
54 MeasureGUI_CheckSelfIntersectionsDlg::MeasureGUI_CheckSelfIntersectionsDlg (GeometryGUI* GUI, QWidget* parent)
55   : MeasureGUI_Skeleton(GUI, parent, false)
56 {
57   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
58   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_CHECK_SELF_INTERSECTIONS")));
59   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
60
61   setWindowTitle(tr("GEOM_CHECK_SELF_INTERSECTIONS"));
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle(tr("GEOM_CHECK_SELF_INTERSECTIONS"));
65   mainFrame()->RadioButton1->setIcon(image0);
66
67   myGrp = new MeasureGUI_1Sel1TextView2ListBox (centralWidget());
68   myGrp->GroupBox1->setTitle(tr("GEOM_CHECK_INFOS"));
69   myGrp->TextLabel1->setText(tr("GEOM_OBJECT"));
70
71   myGrp->TextView1->setReadOnly(true);
72   QFont aFont (TEXTEDIT_FONT_FAMILY, TEXTEDIT_FONT_SIZE);
73   aFont.setStyleHint(QFont::TypeWriter, QFont::PreferAntialias);
74   myGrp->TextView1->setFont(aFont);
75
76   myGrp->PushButton1->setIcon(image1);
77   myGrp->LineEdit1->setReadOnly(true);
78
79   myGrp->TextLabel2->setText(tr("GEOM_CHECK_BLOCKS_COMPOUND_ERRORS"));
80   myGrp->TextLabel3->setText(tr("GEOM_CHECK_BLOCKS_COMPOUND_SUBSHAPES"));
81
82   myGrp->ListBox2->setSelectionMode(QAbstractItemView::ExtendedSelection);
83
84   QVBoxLayout* layout = new QVBoxLayout (centralWidget());
85   layout->setMargin(0); layout->setSpacing(6);
86   layout->addWidget(myGrp);
87
88   /***************************************************************/
89
90   myHelpFileName = "using_measurement_tools_page.html#check_self_intersections_anchor";
91
92   /* Initialisation */
93   Init();
94 }
95
96 //=================================================================================
97 // function : ~MeasureGUI_CheckSelfIntersectionsDlg()
98 // purpose  : Destroys the object and frees any allocated resources
99 //=================================================================================
100 MeasureGUI_CheckSelfIntersectionsDlg::~MeasureGUI_CheckSelfIntersectionsDlg()
101 {
102 }
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void MeasureGUI_CheckSelfIntersectionsDlg::Init()
109 {
110   mySelBtn = myGrp->PushButton1;
111   mySelEdit = myGrp->LineEdit1;
112   MeasureGUI_Skeleton::Init();
113
114   connect(myGrp->ListBox1, SIGNAL(itemSelectionChanged()), SLOT(onErrorsListSelectionChanged()));
115   connect(myGrp->ListBox2, SIGNAL(itemSelectionChanged()), SLOT(onSubShapesListSelectionChanged()));
116 }
117
118 //=================================================================================
119 // function : processObject
120 // purpose  :
121 //=================================================================================
122 void MeasureGUI_CheckSelfIntersectionsDlg::processObject()
123 {
124   myGrp->ListBox1->clear();
125   myGrp->ListBox2->clear();
126   erasePreview();
127
128   if (myObj->_is_nil())
129     return;
130
131   QString aMsg ("");
132   GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow(getOperation());
133   bool isGood = false, isFailed = false;
134   int nbPairs = 0;
135   try {
136     isGood = anOper->CheckSelfIntersections(myObj, myInters);
137     nbPairs = myInters->length()/2;
138     if (nbPairs*2 != myInters->length()) {
139       isFailed = true;
140     }
141   }
142   catch (const SALOME::SALOME_Exception& e) {
143     SalomeApp_Tools::QtCatchCorbaException(e);
144     isFailed = true;
145   }
146   if (isFailed) {
147     aMsg += tr("GEOM_CHECK_SELF_INTERSECTIONS_FAILED");
148     myGrp->TextView1->setText(aMsg);
149     return;
150   }
151
152   // Status
153   if (isGood) {
154     aMsg += tr("GEOM_NO_SELF_INTERSECTIONS");
155   }
156   else {
157     aMsg += tr("GEOM_SELF_INTERSECTIONS_FOUND");
158   }
159   myGrp->TextView1->setText(aMsg);
160
161   // Pairs
162   QStringList aErrList;
163   QString aErrStr ("");
164   for (int i = 1; i <= nbPairs; i++) {
165     aErrStr = "Intersection # ";
166     aErrStr += QString::number(i);
167     aErrList.append(aErrStr);
168   }
169
170   myGrp->ListBox1->addItems(aErrList);
171 }
172
173 //=================================================================================
174 // function : onErrorsListSelectionChanged
175 // purpose  :
176 //=================================================================================
177 void MeasureGUI_CheckSelfIntersectionsDlg::onErrorsListSelectionChanged()
178 {
179   erasePreview();
180   int aCurItem = myGrp->ListBox1->currentRow();
181   if (aCurItem < 0)
182     return;
183
184   //int nbPairs = myInters->length()/2;
185
186   QStringList aSubShapeList;
187   TopoDS_Shape aSelShape;
188   if (!myObj->_is_nil() && GEOMBase::GetShape(myObj, aSelShape)) {
189     TopTools_IndexedMapOfShape anIndices;
190     TopExp::MapShapes(aSelShape, anIndices);
191
192     TopoDS_Shape aSubShape = anIndices.FindKey(myInters[aCurItem*2]);
193     QString aType = GEOMBase::GetShapeTypeString(aSubShape);
194     if (!aType.isEmpty())
195       aSubShapeList.append(QString("%1_%2").arg(aType).arg(myInters[aCurItem*2]));
196     aSubShape = anIndices.FindKey(myInters[aCurItem*2 + 1]);
197     aType = GEOMBase::GetShapeTypeString(aSubShape);
198     if (!aType.isEmpty())
199       aSubShapeList.append(QString("%1_%2").arg(aType).arg(myInters[aCurItem*2 + 1]));
200   }
201   myGrp->ListBox2->clear();
202   myGrp->ListBox2->addItems(aSubShapeList);
203 }
204
205 //=================================================================================
206 // function : onSubShapesListSelectionChanged
207 // purpose  :
208 //=================================================================================
209 void MeasureGUI_CheckSelfIntersectionsDlg::onSubShapesListSelectionChanged()
210 {
211   erasePreview();
212
213   // Current pair
214   int aErrCurItem = myGrp->ListBox1->currentRow();
215   if (aErrCurItem < 0)
216     return;
217
218   // Selected IDs
219   QList<int> aIds;
220   for (int i = 0, n = myGrp->ListBox2->count(); i < n; i++) {
221     if (myGrp->ListBox2->item(i)->isSelected())
222       aIds.append(i);
223   }
224   if (aIds.count() < 1)
225     return;
226
227   TopoDS_Shape aSelShape;
228   TopoDS_Shape aSubShape;
229   TopTools_IndexedMapOfShape anIndices;
230   if (!myObj->_is_nil() && GEOMBase::GetShape(myObj, aSelShape)) {
231     SALOME_Prs* aPrs = 0;
232     TopExp::MapShapes(aSelShape, anIndices);
233     QList<int>::iterator it;
234     for (it = aIds.begin(); it != aIds.end(); ++it) {
235       aSubShape = anIndices.FindKey(myInters[aErrCurItem*2 + (*it)]);
236       try {
237         getDisplayer()->SetColor(Quantity_NOC_RED);
238         getDisplayer()->SetWidth(3);
239         getDisplayer()->SetToActivate(false);
240         aPrs = !aSubShape.IsNull() ? getDisplayer()->BuildPrs(aSubShape) : 0;
241         if (aPrs)
242           displayPreview(aPrs, true);
243       }
244       catch (const SALOME::SALOME_Exception& e) {
245         SalomeApp_Tools::QtCatchCorbaException(e);
246       }
247     }
248   }
249 }