1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "EntityGUI_PolylineDlg.h"
24 #include <CurveCreator_Curve.hxx>
25 #include <CurveCreator_Utils.hxx>
26 #include <CurveCreator_Widget.h>
28 #include <GeometryGUI.h>
31 #include <OCCViewer_ViewManager.h>
32 #include <LightApp_SelectionMgr.h>
33 #include <SalomeApp_Application.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Session.h>
37 #include <BRep_Tool.hxx>
38 #include <Geom_Surface.hxx>
39 #include <GeomLib_IsPlanarSurface.hxx>
43 #include <QVBoxLayout>
47 //=================================================================================
48 // function : Constructor
50 //=================================================================================
51 EntityGUI_PolylineDlg::EntityGUI_PolylineDlg
52 (GeometryGUI* theGeometryGUI, QWidget* parent, bool modal, Qt::WindowFlags fl)
53 : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl ),
61 myPolylineSelButton (0),
63 myEditCurrentArgument (0)
65 QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
66 QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
68 setWindowTitle(tr("POLYLINE_DLG_TITLE"));
70 /***************************************************************/
71 mainFrame()->GroupConstructors->setTitle(tr("POLYLINE_TITLE"));
72 mainFrame()->RadioButton1->setIcon(image0);
73 mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
74 mainFrame()->RadioButton2->close();
75 mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
76 mainFrame()->RadioButton3->close();
78 QGroupBox *aGroupBox1 = new QGroupBox(tr("GEOM_CS"), this);
79 QGridLayout *aPlaneLayout = new QGridLayout(aGroupBox1);
81 aPlaneLayout->setSpacing(6);
82 aPlaneLayout->setMargin(11);
84 myPlnComboBox = new QComboBox(aGroupBox1);
85 aPlaneLayout->addWidget(myPlnComboBox, 0, 0, 1, 3);
87 myPlnButton = new QPushButton (aGroupBox1);
88 myPlnButton->setText( tr( "GEOM_SKETCHER_RESTORE" ) );
89 aPlaneLayout->addWidget(myPlnButton, 0, 3);
92 QLabel *aPlaneLbl = new QLabel(tr("GEOM_PLANE"), aGroupBox1);
94 myPlnSelButton = new QPushButton (aGroupBox1);
95 myPlnSelButton->setIcon(image1);
96 myWPlaneLineEdit = new QLineEdit (aGroupBox1);
97 myWPlaneLineEdit->setReadOnly(true);
100 QLabel *aPolylineLbl = new QLabel(tr("POLYLINE_IMPORT"), aGroupBox1);
102 myPolylineSelButton = new QPushButton (aGroupBox1);
103 myPolylineSelButton->setIcon(image1);
104 myPolylineEdit = new QLineEdit (aGroupBox1);
105 myPolylineEdit->setReadOnly(true);
108 aPlaneLayout->addWidget(aPlaneLbl, 1, 0);
109 aPlaneLayout->addWidget(myPlnSelButton, 1, 1);
110 aPlaneLayout->addWidget(myWPlaneLineEdit, 1, 2, 1, 2);
112 aPlaneLayout->addWidget(aPolylineLbl, 2, 0);
113 aPlaneLayout->addWidget(myPolylineSelButton, 2, 1);
114 aPlaneLayout->addWidget(myPolylineEdit, 2, 2, 1, 2);
116 aPlaneLayout->setColumnStretch(2, 1);
118 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
119 myEditorWidget = new CurveCreator_Widget (centralWidget(), myCurve);
120 myAddElementBox = new QGroupBox (tr("POLYLINE_ADD_SECTION"), centralWidget());
122 QBoxLayout* anAddElementLayout = new QVBoxLayout( myAddElementBox );
124 anAddElementLayout->setMargin( 0 );
125 anAddElementLayout->setSpacing( 6 );
127 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
129 layout->setMargin( 0 );
130 layout->setSpacing( 6 );
131 layout->addWidget( aGroupBox1 );
132 layout->addWidget( myEditorWidget );
133 layout->addWidget( myAddElementBox );
135 /***************************************************************/
137 setHelpFileName( "create_polyline_page.html" );
139 /* Initialisations */
143 //=================================================================================
144 // function : Destructor
146 //=================================================================================
147 EntityGUI_PolylineDlg::~EntityGUI_PolylineDlg()
152 //=================================================================================
155 //=================================================================================
156 void EntityGUI_PolylineDlg::Init()
158 initName(tr("POLYLINE_NAME"));
160 SalomeApp_Application *anApp = myGeomGUI->getApp();
161 OCCViewer_ViewManager *aViewManager = dynamic_cast<OCCViewer_ViewManager*>
162 (anApp->getViewManager(OCCViewer_Viewer::Type(), true));
163 LightApp_SelectionMgr *aSelMgr = myGeomGUI->getApp()->selectionMgr();
165 myEditorWidget->setOCCViewer(aViewManager ? aViewManager->getOCCViewer() : 0);
167 // Init the list of local coordinate system
168 gp_Pnt aPnt(0., 0., 0.);
169 gp_Dir aDirN(0., 0., 1.);
170 gp_Dir aDirX(1., 0., 0.);
171 gp_Ax3 aLCS(aPnt, aDirN, aDirX);
174 myPlnComboBox->addItem(tr("GEOM_GCS"), false);
175 myWPlaneList.push_back(GEOM::GeomObjPtr());
176 myLCSList.push_back(aLCS);
178 connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
179 connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
182 connect(myPlnSelButton, SIGNAL(clicked()),
183 this, SLOT(SetEditCurrentArgument()));
185 connect(myPolylineSelButton, SIGNAL(clicked()),
186 this, SLOT(SetEditCurrentArgument()));
187 connect(aSelMgr, SIGNAL(currentSelectionChanged()),
188 this, SLOT(SelectionIntoArgument()));
189 connect(myEditorWidget, SIGNAL(subOperationStarted(QWidget*, bool)),
190 this, SLOT(processStartedSubOperation(QWidget*, bool)));
191 connect(myEditorWidget, SIGNAL(subOperationFinished(QWidget*)),
192 this, SLOT(processFinishedSubOperation(QWidget*)));
193 connect(myEditorWidget, SIGNAL(curveModified()),
194 this, SLOT(onUpdatePreview()));
196 connect(myPlnComboBox, SIGNAL(activated(int)),
197 this, SLOT(ActivateLocalCS()));
198 connect(myPlnButton, SIGNAL(clicked()),
199 this, SLOT(ActivateLocalCS()));
201 connect(buttonOk(), SIGNAL(clicked()), this, SLOT(ClickOnOk()));
202 connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
204 myAddElementBox->hide();
205 myPolylineSelButton->click();
206 SelectionIntoArgument();
209 //=================================================================================
212 //=================================================================================
213 void EntityGUI_PolylineDlg::Clear()
217 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
218 myEditorWidget->setCurve(myCurve);
221 //=================================================================================
222 // function : GetCurveParams
224 //=================================================================================
225 void EntityGUI_PolylineDlg::GetCurveParams(GEOM::ListOfListOfDouble &theCoords,
226 GEOM::string_array &theNames,
227 GEOM::short_array &theTypes,
228 GEOM::ListOfBool &theCloseds)
230 const int aNbSec = myCurve->getNbSections();
234 theCoords.length(aNbSec);
235 theNames.length(aNbSec);
236 theTypes.length(aNbSec);
237 theCloseds.length(aNbSec);
239 for (i = 0; i < aNbSec; ++i) {
241 CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
242 const int aNbPoints = aCoords.size();
244 theCoords[i].length(aNbPoints);
246 for (j = 0; j < aNbPoints; ++j) {
247 theCoords[i][j] = aCoords[j];
251 const CurveCreator::SectionType aType = myCurve->getSectionType(i);
254 case CurveCreator::Spline:
255 theTypes[i] = GEOM::Interpolation;
257 case CurveCreator::Polyline:
259 theTypes[i] = GEOM::Polyline;
263 // Set section names and closed flags.
264 theNames[i] = CORBA::string_dup(myCurve->getSectionName(i).c_str());
265 theCloseds[i] = myCurve->isClosed(i);
269 //=================================================================================
270 // function : createOperation
272 //=================================================================================
273 GEOM::GEOM_IOperations_ptr EntityGUI_PolylineDlg::createOperation()
275 return getGeomEngine()->GetICurvesOperations( getStudyId() );
278 //=================================================================================
279 // function : isValid
281 //=================================================================================
282 bool EntityGUI_PolylineDlg::isValid( QString& msg )
287 //=================================================================================
288 // function : execute
290 //=================================================================================
291 bool EntityGUI_PolylineDlg::execute( ObjectList& objects )
293 GEOM::GEOM_ICurvesOperations_var anOper =
294 GEOM::GEOM_ICurvesOperations::_narrow(getOperation());
296 // Get the polyline creation parameters.
297 GEOM::ListOfListOfDouble aCoords;
298 GEOM::string_array aNames;
299 GEOM::short_array aTypes;
300 GEOM::ListOfBool aCloseds;
302 GetCurveParams(aCoords, aNames, aTypes, aCloseds);
304 // Get Working Plane.
305 int ind = myPlnComboBox->currentIndex();
308 bool isPlane = myPlnComboBox->itemData(ind).toBool();
309 GEOM::GEOM_Object_var anObj;
313 anObj = anOper->MakePolyline2DOnPlane
314 (aCoords, aNames, aTypes, aCloseds, myWPlaneList.at(ind).get());
316 gp_Ax3 anAxis = myLCSList.at(ind);
317 GEOM::ListOfDouble aPlane;
320 aPlane[0] = anAxis.Location().X();
321 aPlane[1] = anAxis.Location().Y();
322 aPlane[2] = anAxis.Location().Z();
323 aPlane[3] = anAxis.Direction().X();
324 aPlane[4] = anAxis.Direction().Y();
325 aPlane[5] = anAxis.Direction().Z();
326 aPlane[6] = anAxis.XDirection().X();
327 aPlane[7] = anAxis.XDirection().Y();
328 aPlane[8] = anAxis.XDirection().Z();
330 anObj = anOper->MakePolyline2D
331 (aCoords, aNames, aTypes, aCloseds, aPlane);
334 if (!anObj->_is_nil()) {
335 objects.push_back(anObj._retn());
342 //=================================================================================
343 // function : ClickOnOk()
345 //=================================================================================
346 void EntityGUI_PolylineDlg::ClickOnOk()
348 setIsApplyAndClose( true );
354 //=================================================================================
355 // function : ClickOnApply()
357 //=================================================================================
358 bool EntityGUI_PolylineDlg::ClickOnApply()
368 //=================================================================================
369 // function : ClickOnCancel()
371 //=================================================================================
372 void EntityGUI_PolylineDlg::ClickOnCancel()
374 myEditorWidget->SetViewer2DMode(false);
375 GEOMBase_Skeleton::ClickOnCancel();
378 //=================================================================================
379 // function : processStartedSubOperation
381 //=================================================================================
382 void EntityGUI_PolylineDlg::processStartedSubOperation( QWidget* theWidget, bool theIsEdit )
384 myEditorWidget->setEnabled( false );
386 myAddElementBox->setTitle( theIsEdit ? tr( "POLYLINE_EDIT_SECTION" ) : tr( "POLYLINE_ADD_SECTION" ) );
387 QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
388 anAddElementLayout->addWidget( theWidget );
391 myAddElementBox->show();
395 //=================================================================================
396 // function : processFinishedSubOperation
398 //=================================================================================
399 void EntityGUI_PolylineDlg::processFinishedSubOperation( QWidget* theWidget )
401 myEditorWidget->setEnabled( true );
403 QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
404 anAddElementLayout->removeWidget( theWidget );
407 myAddElementBox->hide();
410 //=================================================================================
411 // function : deleteSelected
412 // purpose : Redirect the delete action to editor widget
413 //=================================================================================
414 void EntityGUI_PolylineDlg::deleteSelected()
416 myEditorWidget->removeSelected();
419 //=================================================================================
420 // function : deleteEnabled
421 // purpose : Checks whether there are some to delete
422 //=================================================================================
423 bool EntityGUI_PolylineDlg::deleteEnabled()
425 return myEditorWidget->removeEnabled();
428 //=================================================================================
429 // function : SelectionIntoArgument
430 // purpose : Called when selection is changed
431 //=================================================================================
432 void EntityGUI_PolylineDlg::SelectionIntoArgument()
434 bool isModified = false;
435 GEOM::GeomObjPtr aSelectedObject = getSelected(TopAbs_SHAPE);
438 if (aSelectedObject && GEOMBase::GetShape(aSelectedObject.get(), aShape) &&
440 QString aName = GEOMBase::GetName(aSelectedObject.get());
442 if (myEditCurrentArgument == myPolylineEdit) {
444 CurveCreator_Curve *aNewCurve =
445 new CurveCreator_Curve(CurveCreator::Dim2d);
448 if (CurveCreator_Utils::constructCurve(aShape, aNewCurve, aLocalCS)) {
449 // Change the current curve be the new one.
450 myEditorWidget->setCurve(aNewCurve);
454 myPolylineEdit->setText(aName);
456 AddLocalCS(aSelectedObject.get(), false, aLocalCS);
457 myWPlaneLineEdit->clear();
458 myPlnSelButton->setDown(false);
460 myPolylineSelButton->setDown(true);
462 // Does nothing, just clears selection.
466 } else if (myEditCurrentArgument == myWPlaneLineEdit) {
467 // Import planar face.
468 if (aShape.ShapeType() == TopAbs_FACE) {
469 // Check if the face is planar
470 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
471 GeomLib_IsPlanarSurface aPlanarCheck(aSurf, Precision::Confusion());
473 if (aPlanarCheck.IsPlanar()) {
474 myWPlaneLineEdit->setText(aName);
475 myPolylineEdit->clear();
476 AddLocalCS(aSelectedObject.get(), true,
477 WPlaneToLCS(aSelectedObject.get()));
479 myPlnSelButton->setDown(true);
480 myPolylineSelButton->setDown(false);
485 myEditCurrentArgument->setText(tr("GEOM_SKETCHER_WPLANE"));
492 // Does nothing, just clears selection.
493 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
494 myGeomGUI->getApp()->selectionMgr()->clearSelected();
495 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
496 this, SLOT(SelectionIntoArgument()));
500 //=================================================================================
501 // function : SetEditCurrentArgument()
503 //=================================================================================
504 void EntityGUI_PolylineDlg::SetEditCurrentArgument()
506 if (sender() == myPlnSelButton) {
508 myEditCurrentArgument = myWPlaneLineEdit;
509 myEditCurrentArgument->setFocus();
510 myPlnSelButton->setDown(true);
511 myPolylineSelButton->setDown(false);
513 } else if (sender() == myPolylineSelButton) {
514 myEditCurrentArgument = myPolylineEdit;
515 myEditCurrentArgument->setFocus();
517 myPlnSelButton->setDown(false);
519 myPolylineSelButton->setDown(true);
523 //=================================================================================
524 // function : ActivateThisDialog
526 //=================================================================================
527 void EntityGUI_PolylineDlg::ActivateThisDialog()
529 GEOMBase_Skeleton::ActivateThisDialog();
531 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
532 this, SLOT(SelectionIntoArgument()));
535 //=================================================================================
536 // function : enterEvent()
538 //=================================================================================
539 void EntityGUI_PolylineDlg::enterEvent (QEvent*)
541 if (!mainFrame()->GroupConstructors->isEnabled())
542 ActivateThisDialog();
545 //=================================================================================
546 // function : onUpdatePreview
548 //=================================================================================
549 void EntityGUI_PolylineDlg::onUpdatePreview()
551 displayPreview(true);
554 //=================================================================================
555 // function : ActivateLocalCS
556 // purpose : Activate & Fit Working plane
557 //=================================================================================
558 void EntityGUI_PolylineDlg::ActivateLocalCS()
560 const int ind = myPlnComboBox->currentIndex();
565 myWPlaneLineEdit->clear();
567 myPolylineEdit->clear();
568 } else if (ind > 0) { // Skip 0 as it is default
569 // Update text on line edits.
570 QString aName = GEOMBase::GetName(GetActiveWPlane().get());
571 bool isPlane = myPlnComboBox->itemData(ind).toBool();
575 myWPlaneLineEdit->setText(aName);
577 myPolylineEdit->clear();
579 myPolylineEdit->setText(aName);
581 myWPlaneLineEdit->clear();
586 gp_Ax3 anAxis = GetActiveLocalCS();
588 myGeomGUI->SetWorkingPlane(anAxis);
589 myGeomGUI->ActiveWorkingPlane();
592 //=================================================================================
593 // function : GetActiveLocalCS
594 // purpose : Get Working plane
595 //=================================================================================
596 gp_Ax3 EntityGUI_PolylineDlg::GetActiveLocalCS()
598 const int ind = myPlnComboBox->currentIndex();
600 return ind >= 0 ? myLCSList.at(ind) : myGeomGUI->GetWorkingPlane();
603 //=================================================================================
604 // function : GetActiveWPlane
605 // purpose : Get Working plane
606 //=================================================================================
607 GEOM::GeomObjPtr EntityGUI_PolylineDlg::GetActiveWPlane()
609 const int ind = myPlnComboBox->currentIndex();
611 return ind >= 0 ? myWPlaneList.at(ind) : GEOM::GeomObjPtr();
614 //=================================================================================
615 // function : AddLocalCS()
616 // purpose : Add All Coordinates systems in study
617 //=================================================================================
618 void EntityGUI_PolylineDlg::AddLocalCS(GEOM::GeomObjPtr theSelectedObject,
620 const gp_Ax3 &theLCS)
622 QString aName = GEOMBase::GetName(theSelectedObject.get());
624 int index = myPlnComboBox->findText(aName, Qt::MatchExactly);
626 if (index == -1) { // If the working plane hasn't been added yet
627 myWPlaneList.push_back(theSelectedObject);
628 myLCSList.push_back(theLCS);
629 myPlnComboBox->addItem(aName, QVariant(IsPlane));
630 index = myPlnComboBox->count();
631 myPlnComboBox->setCurrentIndex(index - 1);
633 myPlnComboBox->setCurrentIndex(index);
638 //=================================================================================
639 // function : WPlaneToLCS ( aWPlane )
641 //=================================================================================
642 gp_Ax3 EntityGUI_PolylineDlg::WPlaneToLCS(GEOM::GeomObjPtr theGeomObj)
644 TopoDS_Shape aShape =
645 GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), theGeomObj.get());
648 if (theGeomObj || aShape.IsNull()) {
649 MESSAGE("CORBA::is_nil(theGeomObj) || aShape.IsNull()")
652 aLCS.Transform(aShape.Location().Transformation());
654 if (aShape.ShapeType() == TopAbs_FACE) {
655 GEOM::GEOM_IMeasureOperations_ptr aMeasureOp =
656 myGeomGUI->GetGeomGen()->GetIMeasureOperations(getStudyId());
657 double Ox, Oy, Oz, Zx, Zy, Zz, Xx, Xy, Xz;
659 aMeasureOp->GetPosition(theGeomObj.get(), Ox, Oy, Oz, Zx, Zy, Zz, Xx, Xy, Xz);
661 if (aMeasureOp->IsDone()) {
662 gp_Pnt aPnt (Ox, Oy, Oz);
663 gp_Dir aDirN (Zx, Zy, Zz);
664 gp_Dir aDirX (Xx, Xy, Xz);
665 aLCS = gp_Ax3(aPnt, aDirN, aDirX);