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
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_PolylineOp.h"
24 #include "HYDROGUI_PolylineDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "CurveCreator.hxx"
27 #include "CurveCreator_Curve.hxx"
28 #include "CurveCreator_CurveEditor.hxx"
29 #include "HYDROGUI_AISCurve.h"
31 #include <HYDROData_Document.h>
32 #include <HYDROData_Polyline.h>
33 #include <CurveCreator_Curve.hxx>
34 #include <CurveCreator_CurveEditor.hxx>
36 #include <LightApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <LightApp_UpdateFlags.h>
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
44 #include <OCCViewer_AISSelector.h>
46 #include <Precision.hxx>
48 static int ZValueIncrement = 0;
50 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
51 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myCurve(NULL),
52 myViewManager(NULL), myAISCurve(NULL)
54 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
57 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
62 void HYDROGUI_PolylineOp::startOperation()
70 HYDROGUI_Operation::startOperation();
72 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
75 myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
76 if( !myEditedObject.IsNull() )
78 int anIntDim = myEditedObject->GetDimension();
79 CurveCreator::Dimension aDim = CurveCreator::Dim3d;
81 aDim = CurveCreator::Dim2d;
82 myCurve = new CurveCreator_Curve(aDim);
83 QList<PolylineSection> aPolylineData = myEditedObject->GetPolylineData();
85 CurveCreator_CurveEditor* anEdit = new CurveCreator_CurveEditor(myCurve);
86 for( int i = 0 ; i < aPolylineData.size() ; i++ ){
87 std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
88 bool isClosed = aPolylineData[i].myIsClosed;
89 CurveCreator::Type aType = CurveCreator::Polyline;
90 if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
91 aType = CurveCreator::BSpline;
93 CurveCreator::Coordinates aCoords;
94 for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
95 aCoords.push_back(aPolylineData[i].myCoords[j]);
97 anEdit->addSection( aName, aType, isClosed, aCoords );
100 aPanel->setPolylineName( myEditedObject->GetName() );
104 myCurve = new CurveCreator_Curve(CurveCreator::Dim2d);
105 aPanel->setCurve(myCurve);
106 QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), "Polyline" );
107 aPanel->setPolylineName(aNewName);
109 aPanel->setCurve(myCurve);
112 myAISCurve->setCurve(myCurve);
117 void HYDROGUI_PolylineOp::abortOperation()
121 HYDROGUI_Operation::abortOperation();
124 void HYDROGUI_PolylineOp::commitOperation()
128 HYDROGUI_Operation::commitOperation();
131 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
133 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
134 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
138 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
139 QString& theErrorMsg )
141 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
143 int aStudyId = module()->getStudyId();
144 bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
145 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
146 if( aDocument.IsNull() )
149 Handle(HYDROData_Polyline) aPolylineObj;
151 aPolylineObj = myEditedObject;
154 aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
156 double aZValue = double( ++ZValueIncrement ) * 1e-2; // empiric value, to be revised
157 aPolylineObj->SetZValue( aZValue );
160 if( aPolylineObj.IsNull() )
163 QString aPolylineName = aPanel->getPolylineName();
164 aPolylineObj->SetName(aPolylineName);
166 if( myCurve->getDimension() == CurveCreator::Dim2d )
168 aPolylineObj->SetDimension(aDimInt);
169 QList<PolylineSection> aPolylineData;
170 for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
171 PolylineSection aSect;
172 aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
173 aSect.myIsClosed = myCurve->isClosed(i);
174 aSect.myType = PolylineSection::SECTION_POLYLINE;
175 if( myCurve->getType(i) == CurveCreator::BSpline ){
176 aSect.myType = PolylineSection::SECTION_SPLINE;
178 CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
179 for( int j = 0 ; j < aCoords.size() ; j++ ){
180 aSect.myCoords << aCoords.at(j);
182 aPolylineData << aSect;
184 aPolylineObj->SetPolylineData(aPolylineData);
186 theUpdateFlags = UF_Model;
187 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), aPolylineObj, true );
191 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
193 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
200 QList<int> aSelSections = aPanel->getSelectedSections();
201 for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
203 if( aSelSections.contains(i) ){
204 myAISCurve->highlightSection(i, aIsHl);
209 void HYDROGUI_PolylineOp::displayPreview()
211 LightApp_Application* anApp = module()->getApp();
214 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
217 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
219 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
222 myAISCurve = new HYDROGUI_AISCurve( myCurve, aCtx );
223 myAISCurve->Display();
229 void HYDROGUI_PolylineOp::erasePreview()
233 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
235 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
236 if( !aCtx.IsNull() && myAISCurve )