Salome HOME
8ceaea0538622c579f6f268ee1af2021e4667daf
[modules/geom.git] / src / MeasureGUI / MeasureGUI_InertiaDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : MeasureGUI_InertiaDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27
28 #include "MeasureGUI_InertiaDlg.h"
29
30 #include <BRepGProp.hxx>
31 #include <GProp_GProps.hxx>
32 #include <GProp_PrincipalProps.hxx>
33
34 #include "utilities.h"
35
36 using namespace std;
37
38 //=================================================================================
39 // class    : MeasureGUI_InertiaDlg()
40 // purpose  : Constructs a MeasureGUI_InertiaDlg which is a child of 'parent', with the 
41 //            name 'name' and widget flags set to 'f'.
42 //            The dialog will by default be modeless, unless you set 'modal' to
43 //            TRUE to construct a modal dialog.
44 //=================================================================================
45 MeasureGUI_InertiaDlg::MeasureGUI_InertiaDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
46   :MeasureGUI_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
47 {
48   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_INERTIA")));
49   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
50     
51   setCaption(tr("GEOM_INERTIA_TITLE"));
52
53   /***************************************************************/
54   GroupConstructors->setTitle(tr("GEOM_INERTIA_CONSTR"));
55   RadioButton1->setPixmap(image0);
56
57   GroupC1 = new MeasureGUI_1Sel12LineEdit_QTD(this, "GroupC1");
58   GroupC1->GroupBox1->setTitle(tr("GEOM_MATRIX"));
59   GroupC1->TextLabel1->setText(tr("GEOM_OBJECT"));
60   GroupC1->TextLabel2->setText(tr("GEOM_INERTIA_I").arg("1"));
61   GroupC1->TextLabel3->setText(tr("GEOM_INERTIA_I").arg("2"));
62   GroupC1->TextLabel4->setText(tr("GEOM_INERTIA_I").arg("3"));
63   GroupC1->TextLabel5->setText(tr("GEOM_INERTIA_IXYZ"));
64   GroupC1->LineEdit11->setReadOnly(TRUE);
65   GroupC1->LineEdit12->setReadOnly(TRUE);
66   GroupC1->LineEdit13->setReadOnly(TRUE);
67   GroupC1->LineEdit21->setReadOnly(TRUE);
68   GroupC1->LineEdit22->setReadOnly(TRUE);
69   GroupC1->LineEdit23->setReadOnly(TRUE);
70   GroupC1->LineEdit31->setReadOnly(TRUE);
71   GroupC1->LineEdit32->setReadOnly(TRUE);
72   GroupC1->LineEdit33->setReadOnly(TRUE);
73   GroupC1->LineEdit41->setReadOnly(TRUE);
74   GroupC1->LineEdit42->setReadOnly(TRUE);
75   GroupC1->LineEdit43->setReadOnly(TRUE);
76   GroupC1->PushButton1->setPixmap(image1);
77
78   Layout1->addWidget(GroupC1, 1, 0);
79   /***************************************************************/
80
81   /* Initialisation */
82   Init();
83 }
84
85
86 //=================================================================================
87 // function : ~MeasureGUI_InertiaDlg()
88 // purpose  : Destroys the object and frees any allocated resources
89 //=================================================================================
90 MeasureGUI_InertiaDlg::~MeasureGUI_InertiaDlg()
91 {
92   // no need to delete child widgets, Qt does it all for us
93 }
94
95
96 //=================================================================================
97 // function : Init()
98 // purpose  :
99 //=================================================================================
100 void MeasureGUI_InertiaDlg::Init()
101 {
102   /* init variables */
103   myEditCurrentArgument = GroupC1->LineEdit1;
104
105    /* signals and slots connections */
106   connect(GroupC1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
107   connect(GroupC1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
108
109   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
110
111   /* displays Dialog */
112   GroupC1->show();
113   this->show();
114
115   return;
116 }
117
118
119 //=================================================================================
120 // function : SelectionIntoArgument()
121 // purpose  : Called when selection as changed or other case
122 //=================================================================================
123 void MeasureGUI_InertiaDlg::SelectionIntoArgument()
124 {
125   myEditCurrentArgument->setText("");
126   QString aString = "";
127
128   GroupC1->LineEdit11->setText("");
129   GroupC1->LineEdit12->setText("");
130   GroupC1->LineEdit13->setText("");
131   GroupC1->LineEdit21->setText("");
132   GroupC1->LineEdit22->setText("");
133   GroupC1->LineEdit23->setText("");
134   GroupC1->LineEdit31->setText("");
135   GroupC1->LineEdit32->setText("");
136   GroupC1->LineEdit33->setText("");
137   GroupC1->LineEdit41->setText("");
138   GroupC1->LineEdit42->setText("");
139   GroupC1->LineEdit43->setText("");
140
141   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
142   if(nbSel != 1)
143     return;
144
145   /*  nbSel == 1  */
146   TopoDS_Shape S;
147   if(!myGeomBase->GetTopoFromSelection(mySelection, S) || S.IsNull())
148     return;
149
150   GroupC1->LineEdit1->setText(aString);
151
152   this->CalculateAndDisplayInertia(S);
153   return;
154 }
155
156
157 //=================================================================================
158 // function : SetEditCurrentArgument()
159 // purpose  :
160 //=================================================================================
161 void MeasureGUI_InertiaDlg::SetEditCurrentArgument()
162 {
163   QPushButton* send = (QPushButton*)sender();
164
165   if(send == GroupC1->PushButton1) {
166     GroupC1->LineEdit1->setFocus();
167     myEditCurrentArgument = GroupC1->LineEdit1;
168   }
169
170   this->SelectionIntoArgument();
171   return;
172 }
173
174
175 //=================================================================================
176 // function : LineEditReturnPressed()
177 // purpose  :
178 //=================================================================================
179 void MeasureGUI_InertiaDlg::LineEditReturnPressed()
180 {
181   QLineEdit* send = (QLineEdit*)sender();
182   if(send == GroupC1->LineEdit1)
183     myEditCurrentArgument = GroupC1->LineEdit1;
184   else
185     return;
186
187   MeasureGUI_Skeleton::LineEditReturnPressed();
188   return;
189 }
190
191
192 //=================================================================================
193 // function : ActivateThisDialog()
194 // purpose  :
195 //=================================================================================
196 void MeasureGUI_InertiaDlg::ActivateThisDialog()
197 {
198   MeasureGUI_Skeleton::ActivateThisDialog();
199   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
200   return;
201 }
202
203
204 //=================================================================================
205 // function : enterEvent()
206 // purpose  :
207 //=================================================================================
208 void MeasureGUI_InertiaDlg::enterEvent(QEvent* e)
209 {
210   if(GroupConstructors->isEnabled())
211     return;
212   this->ActivateThisDialog();
213   return;
214 }
215
216
217 //=================================================================================
218 // function : CalculateAndDisplayInertia()
219 // purpose  :
220 //=================================================================================
221 void MeasureGUI_InertiaDlg::CalculateAndDisplayInertia(const TopoDS_Shape& S)
222 {
223   GroupC1->LineEdit11->setText("");
224   GroupC1->LineEdit12->setText("");
225   GroupC1->LineEdit13->setText("");
226   GroupC1->LineEdit21->setText("");
227   GroupC1->LineEdit22->setText("");
228   GroupC1->LineEdit23->setText("");
229   GroupC1->LineEdit31->setText("");
230   GroupC1->LineEdit32->setText("");
231   GroupC1->LineEdit33->setText("");
232   GroupC1->LineEdit41->setText("");
233   GroupC1->LineEdit42->setText("");
234   GroupC1->LineEdit43->setText("");
235
236   if(S.IsNull()) 
237     return;
238
239   QString resString;
240   GProp_GProps System;
241
242   try {
243     if(S.ShapeType() == TopAbs_VERTEX || S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE)
244       BRepGProp::LinearProperties(S, System);
245     else if(S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL)
246       BRepGProp::SurfaceProperties(S, System);
247     else
248       BRepGProp::VolumeProperties(S, System);
249
250     gp_Mat I = System.MatrixOfInertia() ;
251     GProp_PrincipalProps Pr = System.PrincipalProperties();
252     Standard_Real Ix,Iy,Iz;
253     Pr.Moments(Ix,Iy,Iz);
254     
255     /* matrix 3x3 */
256     resString = tr("%1").arg(I(1,1), 12, 'f', 6);
257     GroupC1->LineEdit11->setText(resString);
258     resString = tr("%1").arg(I(1,2), 12, 'f', 6);
259     GroupC1->LineEdit12->setText(resString);
260     resString = tr("%1").arg(I(1,3), 12, 'f', 6);
261     GroupC1->LineEdit13->setText(resString);
262
263     resString = tr("%1").arg(I(2,1), 12, 'f', 6);
264     GroupC1->LineEdit21->setText(resString);
265     resString = tr("%1").arg(I(2,2), 12, 'f', 6);
266     GroupC1->LineEdit22->setText(resString);
267     resString = tr("%1").arg(I(2,3), 12, 'f', 6);
268     GroupC1->LineEdit23->setText(resString);
269
270     resString = tr("%1").arg(I(3,1), 12, 'f', 6);
271     GroupC1->LineEdit31->setText(resString);
272     resString = tr("%1").arg(I(3,2), 12, 'f', 6);
273     GroupC1->LineEdit32->setText(resString);
274     resString = tr("%1").arg(I(3,3), 12, 'f', 6);
275     GroupC1->LineEdit33->setText(resString);
276
277     /* moments */
278     resString = tr("%1").arg(Ix, 12, 'f', 6);
279     GroupC1->LineEdit41->setText(resString);
280     resString = tr("%1").arg(Ix, 12, 'f', 6);
281     GroupC1->LineEdit42->setText(resString);
282     resString = tr("%1").arg(Iz, 12, 'f', 6);
283     GroupC1->LineEdit43->setText(resString);
284   }
285   catch(Standard_Failure) {
286     MESSAGE("Catch intercepted in CalculateAndDisplayInertia()");
287   }
288   return;
289 }