1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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 "utilities.h"
33 #include <OCCViewer_ViewManager.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <SUIT_ResourceMgr.h>
37 #include <SUIT_Session.h>
38 #include "utilities.h"
40 #include <BRep_Tool.hxx>
41 #include <Geom_Surface.hxx>
42 #include <GeomLib_IsPlanarSurface.hxx>
46 #include <QVBoxLayout>
50 //=================================================================================
51 // function : Constructor
53 //=================================================================================
54 EntityGUI_PolylineDlg::EntityGUI_PolylineDlg
55 (GeometryGUI* theGeometryGUI, QWidget* parent, bool modal, Qt::WindowFlags fl)
56 : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl ),
64 myPolylineSelButton (0),
66 myEditCurrentArgument (0)
68 QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
69 QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
71 setWindowTitle(tr("POLYLINE_DLG_TITLE"));
73 /***************************************************************/
74 mainFrame()->GroupConstructors->setTitle(tr("POLYLINE_TITLE"));
75 mainFrame()->RadioButton1->setIcon(image0);
76 mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
77 mainFrame()->RadioButton2->close();
78 mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
79 mainFrame()->RadioButton3->close();
81 QGroupBox *aGroupBox1 = new QGroupBox(tr("GEOM_CS"), this);
82 QGridLayout *aPlaneLayout = new QGridLayout(aGroupBox1);
84 aPlaneLayout->setSpacing(6);
85 aPlaneLayout->setMargin(11);
87 myPlnComboBox = new QComboBox(aGroupBox1);
88 aPlaneLayout->addWidget(myPlnComboBox, 0, 0, 1, 3);
90 myPlnButton = new QPushButton (aGroupBox1);
91 myPlnButton->setText( tr( "GEOM_SKETCHER_RESTORE" ) );
92 aPlaneLayout->addWidget(myPlnButton, 0, 3);
95 QLabel *aPlaneLbl = new QLabel(tr("GEOM_PLANE"), aGroupBox1);
97 myPlnSelButton = new QPushButton (aGroupBox1);
98 myPlnSelButton->setIcon(image1);
99 myWPlaneLineEdit = new QLineEdit (aGroupBox1);
100 myWPlaneLineEdit->setReadOnly(true);
103 QLabel *aPolylineLbl = new QLabel(tr("POLYLINE_IMPORT"), aGroupBox1);
105 myPolylineSelButton = new QPushButton (aGroupBox1);
106 myPolylineSelButton->setIcon(image1);
107 myPolylineEdit = new QLineEdit (aGroupBox1);
108 myPolylineEdit->setReadOnly(true);
111 aPlaneLayout->addWidget(aPlaneLbl, 1, 0);
112 aPlaneLayout->addWidget(myPlnSelButton, 1, 1);
113 aPlaneLayout->addWidget(myWPlaneLineEdit, 1, 2, 1, 2);
115 aPlaneLayout->addWidget(aPolylineLbl, 2, 0);
116 aPlaneLayout->addWidget(myPolylineSelButton, 2, 1);
117 aPlaneLayout->addWidget(myPolylineEdit, 2, 2, 1, 2);
119 aPlaneLayout->setColumnStretch(2, 1);
121 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
122 myEditorWidget = new CurveCreator_Widget (centralWidget(), myCurve);
123 myAddElementBox = new QGroupBox (tr("POLYLINE_ADD_SECTION"), centralWidget());
125 QBoxLayout* anAddElementLayout = new QVBoxLayout( myAddElementBox );
127 anAddElementLayout->setMargin( 0 );
128 anAddElementLayout->setSpacing( 6 );
130 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
132 layout->setMargin( 0 );
133 layout->setSpacing( 6 );
134 layout->addWidget( aGroupBox1 );
135 layout->addWidget( myEditorWidget );
136 layout->addWidget( myAddElementBox );
138 /***************************************************************/
140 setHelpFileName( "create_polyline_page.html" );
142 /* Initialisations */
146 //=================================================================================
147 // function : Destructor
149 //=================================================================================
150 EntityGUI_PolylineDlg::~EntityGUI_PolylineDlg()
155 //=================================================================================
158 //=================================================================================
159 void EntityGUI_PolylineDlg::Init()
161 initName(tr("POLYLINE_NAME"));
163 SalomeApp_Application *anApp = myGeomGUI->getApp();
164 OCCViewer_ViewManager *aViewManager = dynamic_cast<OCCViewer_ViewManager*>
165 (anApp->getViewManager(OCCViewer_Viewer::Type(), true));
166 LightApp_SelectionMgr *aSelMgr = myGeomGUI->getApp()->selectionMgr();
168 myEditorWidget->setOCCViewer(aViewManager ? aViewManager->getOCCViewer() : 0);
170 // Init the list of local coordinate system
171 gp_Pnt aPnt(0., 0., 0.);
172 gp_Dir aDirN(0., 0., 1.);
173 gp_Dir aDirX(1., 0., 0.);
174 gp_Ax3 aLCS(aPnt, aDirN, aDirX);
177 myPlnComboBox->addItem(tr("GEOM_GCS"), false);
178 myWPlaneList.push_back(GEOM::GeomObjPtr());
179 myLCSList.push_back(aLCS);
181 connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
182 connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
185 connect(myPlnSelButton, SIGNAL(clicked()),
186 this, SLOT(SetEditCurrentArgument()));
188 connect(myPolylineSelButton, SIGNAL(clicked()),
189 this, SLOT(SetEditCurrentArgument()));
190 connect(aSelMgr, SIGNAL(currentSelectionChanged()),
191 this, SLOT(SelectionIntoArgument()));
192 connect(myEditorWidget, SIGNAL(subOperationStarted(QWidget*, bool)),
193 this, SLOT(processStartedSubOperation(QWidget*, bool)));
194 connect(myEditorWidget, SIGNAL(subOperationFinished(QWidget*)),
195 this, SLOT(processFinishedSubOperation(QWidget*)));
196 connect(myEditorWidget, SIGNAL(curveModified()),
197 this, SLOT(onUpdatePreview()));
199 connect(myPlnComboBox, SIGNAL(activated(int)),
200 this, SLOT(ActivateLocalCS()));
201 connect(myPlnButton, SIGNAL(clicked()),
202 this, SLOT(ActivateLocalCS()));
204 connect(buttonOk(), SIGNAL(clicked()), this, SLOT(ClickOnOk()));
205 connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
207 myAddElementBox->hide();
208 myPolylineSelButton->click();
209 SelectionIntoArgument();
212 //=================================================================================
215 //=================================================================================
216 void EntityGUI_PolylineDlg::Clear()
220 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
221 myEditorWidget->setCurve(myCurve);
224 //=================================================================================
225 // function : GetCurveParams
227 //=================================================================================
228 void EntityGUI_PolylineDlg::GetCurveParams(GEOM::ListOfListOfDouble &theCoords,
229 GEOM::string_array &theNames,
230 GEOM::short_array &theTypes,
231 GEOM::ListOfBool &theCloseds)
233 const int aNbSec = myCurve->getNbSections();
237 theCoords.length(aNbSec);
238 theNames.length(aNbSec);
239 theTypes.length(aNbSec);
240 theCloseds.length(aNbSec);
242 for (i = 0; i < aNbSec; ++i) {
244 CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
245 const int aNbPoints = aCoords.size();
247 theCoords[i].length(aNbPoints);
249 for (j = 0; j < aNbPoints; ++j) {
250 theCoords[i][j] = aCoords[j];
254 const CurveCreator::SectionType aType = myCurve->getSectionType(i);
257 case CurveCreator::Spline:
258 theTypes[i] = GEOM::Interpolation;
260 case CurveCreator::Polyline:
262 theTypes[i] = GEOM::Polyline;
266 // Set section names and closed flags.
267 theNames[i] = CORBA::string_dup(myCurve->getSectionName(i).c_str());
268 theCloseds[i] = myCurve->isClosed(i);
272 //=================================================================================
273 // function : createOperation
275 //=================================================================================
276 GEOM::GEOM_IOperations_ptr EntityGUI_PolylineDlg::createOperation()
278 return getGeomEngine()->GetICurvesOperations( getStudyId() );
281 //=================================================================================
282 // function : isValid
284 //=================================================================================
285 bool EntityGUI_PolylineDlg::isValid( QString& msg )
290 //=================================================================================
291 // function : execute
293 //=================================================================================
294 bool EntityGUI_PolylineDlg::execute( ObjectList& objects )
296 GEOM::GEOM_ICurvesOperations_var anOper =
297 GEOM::GEOM_ICurvesOperations::_narrow(getOperation());
299 // Get the polyline creation parameters.
300 GEOM::ListOfListOfDouble aCoords;
301 GEOM::string_array aNames;
302 GEOM::short_array aTypes;
303 GEOM::ListOfBool aCloseds;
305 GetCurveParams(aCoords, aNames, aTypes, aCloseds);
307 // Get Working Plane.
308 int ind = myPlnComboBox->currentIndex();
311 bool isPlane = myPlnComboBox->itemData(ind).toBool();
312 GEOM::GEOM_Object_var anObj;
316 anObj = anOper->MakePolyline2DOnPlane
317 (aCoords, aNames, aTypes, aCloseds, myWPlaneList.at(ind).get());
319 gp_Ax3 anAxis = myLCSList.at(ind);
320 GEOM::ListOfDouble aPlane;
323 aPlane[0] = anAxis.Location().X();
324 aPlane[1] = anAxis.Location().Y();
325 aPlane[2] = anAxis.Location().Z();
326 aPlane[3] = anAxis.Direction().X();
327 aPlane[4] = anAxis.Direction().Y();
328 aPlane[5] = anAxis.Direction().Z();
329 aPlane[6] = anAxis.XDirection().X();
330 aPlane[7] = anAxis.XDirection().Y();
331 aPlane[8] = anAxis.XDirection().Z();
333 anObj = anOper->MakePolyline2D
334 (aCoords, aNames, aTypes, aCloseds, aPlane);
337 if (!anObj->_is_nil()) {
338 objects.push_back(anObj._retn());
345 //=================================================================================
346 // function : ClickOnOk()
348 //=================================================================================
349 void EntityGUI_PolylineDlg::ClickOnOk()
351 setIsApplyAndClose( true );
357 //=================================================================================
358 // function : ClickOnApply()
360 //=================================================================================
361 bool EntityGUI_PolylineDlg::ClickOnApply()
371 //=================================================================================
372 // function : ClickOnCancel()
374 //=================================================================================
375 void EntityGUI_PolylineDlg::ClickOnCancel()
377 myEditorWidget->SetViewer2DMode(false);
378 GEOMBase_Skeleton::ClickOnCancel();
381 //=================================================================================
382 // function : processStartedSubOperation
384 //=================================================================================
385 void EntityGUI_PolylineDlg::processStartedSubOperation( QWidget* theWidget, bool theIsEdit )
387 myEditorWidget->setEnabled( false );
389 myAddElementBox->setTitle( theIsEdit ? tr( "POLYLINE_EDIT_SECTION" ) : tr( "POLYLINE_ADD_SECTION" ) );
390 QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
391 anAddElementLayout->addWidget( theWidget );
394 myAddElementBox->show();
398 //=================================================================================
399 // function : processFinishedSubOperation
401 //=================================================================================
402 void EntityGUI_PolylineDlg::processFinishedSubOperation( QWidget* theWidget )
404 myEditorWidget->setEnabled( true );
406 QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
407 anAddElementLayout->removeWidget( theWidget );
410 myAddElementBox->hide();
413 //=================================================================================
414 // function : deleteSelected
415 // purpose : Redirect the delete action to editor widget
416 //=================================================================================
417 void EntityGUI_PolylineDlg::deleteSelected()
419 myEditorWidget->removeSelected();
422 //=================================================================================
423 // function : deleteEnabled
424 // purpose : Checks whether there are some to delete
425 //=================================================================================
426 bool EntityGUI_PolylineDlg::deleteEnabled()
428 return myEditorWidget->removeEnabled();
431 //=================================================================================
432 // function : SelectionIntoArgument
433 // purpose : Called when selection is changed
434 //=================================================================================
435 void EntityGUI_PolylineDlg::SelectionIntoArgument()
437 bool isModified = false;
438 GEOM::GeomObjPtr aSelectedObject = getSelected(TopAbs_SHAPE);
441 if (aSelectedObject && GEOMBase::GetShape(aSelectedObject.get(), aShape) &&
443 QString aName = GEOMBase::GetName(aSelectedObject.get());
445 if (myEditCurrentArgument == myPolylineEdit) {
447 CurveCreator_Curve *aNewCurve =
448 new CurveCreator_Curve(CurveCreator::Dim2d);
451 if (CurveCreator_Utils::constructCurve(aShape, aNewCurve, aLocalCS)) {
452 // Change the current curve be the new one.
453 myEditorWidget->setCurve(aNewCurve);
457 myPolylineEdit->setText(aName);
459 AddLocalCS(aSelectedObject.get(), false, aLocalCS);
460 myWPlaneLineEdit->clear();
461 myPlnSelButton->setDown(false);
463 myPolylineSelButton->setDown(true);
465 // Does nothing, just clears selection.
469 } else if (myEditCurrentArgument == myWPlaneLineEdit) {
470 // Import planar face.
471 if (aShape.ShapeType() == TopAbs_FACE) {
472 // Check if the face is planar
473 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
474 GeomLib_IsPlanarSurface aPlanarCheck(aSurf, Precision::Confusion());
476 if (aPlanarCheck.IsPlanar()) {
477 myWPlaneLineEdit->setText(aName);
478 myPolylineEdit->clear();
479 AddLocalCS(aSelectedObject.get(), true,
480 WPlaneToLCS(aSelectedObject.get()));
482 myPlnSelButton->setDown(true);
483 myPolylineSelButton->setDown(false);
488 myEditCurrentArgument->setText(tr("GEOM_SKETCHER_WPLANE"));
495 // Does nothing, just clears selection.
496 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
497 myGeomGUI->getApp()->selectionMgr()->clearSelected();
498 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
499 this, SLOT(SelectionIntoArgument()));
503 //=================================================================================
504 // function : SetEditCurrentArgument()
506 //=================================================================================
507 void EntityGUI_PolylineDlg::SetEditCurrentArgument()
509 if (sender() == myPlnSelButton) {
511 myEditCurrentArgument = myWPlaneLineEdit;
512 myEditCurrentArgument->setFocus();
513 myPlnSelButton->setDown(true);
514 myPolylineSelButton->setDown(false);
516 } else if (sender() == myPolylineSelButton) {
517 myEditCurrentArgument = myPolylineEdit;
518 myEditCurrentArgument->setFocus();
520 myPlnSelButton->setDown(false);
522 myPolylineSelButton->setDown(true);
526 //=================================================================================
527 // function : ActivateThisDialog
529 //=================================================================================
530 void EntityGUI_PolylineDlg::ActivateThisDialog()
532 GEOMBase_Skeleton::ActivateThisDialog();
534 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
535 this, SLOT(SelectionIntoArgument()));
538 //=================================================================================
539 // function : enterEvent()
541 //=================================================================================
542 void EntityGUI_PolylineDlg::enterEvent (QEvent*)
544 if (!mainFrame()->GroupConstructors->isEnabled())
545 ActivateThisDialog();
548 //=================================================================================
549 // function : onUpdatePreview
551 //=================================================================================
552 void EntityGUI_PolylineDlg::onUpdatePreview()
554 displayPreview(true);
557 //=================================================================================
558 // function : ActivateLocalCS
559 // purpose : Activate & Fit Working plane
560 //=================================================================================
561 void EntityGUI_PolylineDlg::ActivateLocalCS()
563 const int ind = myPlnComboBox->currentIndex();
568 myWPlaneLineEdit->clear();
570 myPolylineEdit->clear();
571 } else if (ind > 0) { // Skip 0 as it is default
572 // Update text on line edits.
573 QString aName = GEOMBase::GetName(GetActiveWPlane().get());
574 bool isPlane = myPlnComboBox->itemData(ind).toBool();
578 myWPlaneLineEdit->setText(aName);
580 myPolylineEdit->clear();
582 myPolylineEdit->setText(aName);
584 myWPlaneLineEdit->clear();
589 gp_Ax3 anAxis = GetActiveLocalCS();
591 myGeomGUI->SetWorkingPlane(anAxis);
592 myGeomGUI->ActiveWorkingPlane();
595 //=================================================================================
596 // function : GetActiveLocalCS
597 // purpose : Get Working plane
598 //=================================================================================
599 gp_Ax3 EntityGUI_PolylineDlg::GetActiveLocalCS()
601 const int ind = myPlnComboBox->currentIndex();
603 return ind >= 0 ? myLCSList.at(ind) : myGeomGUI->GetWorkingPlane();
606 //=================================================================================
607 // function : GetActiveWPlane
608 // purpose : Get Working plane
609 //=================================================================================
610 GEOM::GeomObjPtr EntityGUI_PolylineDlg::GetActiveWPlane()
612 const int ind = myPlnComboBox->currentIndex();
614 return ind >= 0 ? myWPlaneList.at(ind) : GEOM::GeomObjPtr();
617 //=================================================================================
618 // function : AddLocalCS()
619 // purpose : Add All Coordinates systems in study
620 //=================================================================================
621 void EntityGUI_PolylineDlg::AddLocalCS(GEOM::GeomObjPtr theSelectedObject,
623 const gp_Ax3 &theLCS)
625 QString aName = GEOMBase::GetName(theSelectedObject.get());
627 int index = myPlnComboBox->findText(aName, Qt::MatchExactly);
629 if (index == -1) { // If the working plane hasn't been added yet
630 myWPlaneList.push_back(theSelectedObject);
631 myLCSList.push_back(theLCS);
632 myPlnComboBox->addItem(aName, QVariant(IsPlane));
633 index = myPlnComboBox->count();
634 myPlnComboBox->setCurrentIndex(index - 1);
636 myPlnComboBox->setCurrentIndex(index);
641 //=================================================================================
642 // function : WPlaneToLCS ( aWPlane )
644 //=================================================================================
645 gp_Ax3 EntityGUI_PolylineDlg::WPlaneToLCS(GEOM::GeomObjPtr theGeomObj)
647 TopoDS_Shape aShape =
648 GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), theGeomObj.get());
651 if (theGeomObj || aShape.IsNull()) {
652 MESSAGE("CORBA::is_nil(theGeomObj) || aShape.IsNull()")
655 aLCS.Transform(aShape.Location().Transformation());
657 if (aShape.ShapeType() == TopAbs_FACE) {
658 GEOM::GEOM_IMeasureOperations_ptr aMeasureOp =
659 myGeomGUI->GetGeomGen()->GetIMeasureOperations(getStudyId());
660 double Ox, Oy, Oz, Zx, Zy, Zz, Xx, Xy, Xz;
662 aMeasureOp->GetPosition(theGeomObj.get(), Ox, Oy, Oz, Zx, Zy, Zz, Xx, Xy, Xz);
664 if (aMeasureOp->IsDone()) {
665 gp_Pnt aPnt (Ox, Oy, Oz);
666 gp_Dir aDirN (Zx, Zy, Zz);
667 gp_Dir aDirX (Xx, Xy, Xz);
668 aLCS = gp_Ax3(aPnt, aDirN, aDirX);
675 //=================================================================================
676 // function : getSourceObjects
677 // purpose : virtual method to get source objects
678 //=================================================================================
679 QList<GEOM::GeomObjPtr> EntityGUI_PolylineDlg::getSourceObjects()