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 "HYDROGUI_UpdateFlags.h"
28 #include "HYDROData_Document.h"
29 #include "HYDROData_Polyline.h"
30 #include "CurveCreator_Curve.hxx"
31 #include "CurveCreator_Displayer.h"
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <LightApp_UpdateFlags.h>
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
39 #include <OCCViewer_ViewWindow.h>
41 #include <OCCViewer_AISSelector.h>
43 #include <Precision.hxx>
45 //static int ZValueIncrement = 0;
47 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
48 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myCurve(NULL),
49 myViewManager(NULL), myCurveDisplayer(NULL)
51 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
54 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
59 void HYDROGUI_PolylineOp::startOperation()
67 HYDROGUI_Operation::startOperation();
69 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
72 LightApp_Application* anApp = module()->getApp();
74 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
75 aPanel->setOCCViewer( myViewManager ? myViewManager->getOCCViewer() : 0 );
78 myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
79 if( !myEditedObject.IsNull() )
81 int anIntDim = myEditedObject->GetDimension();
82 CurveCreator::Dimension aDim = CurveCreator::Dim3d;
84 aDim = CurveCreator::Dim2d;
85 myCurve = new CurveCreator_Curve(aDim);
86 QList<PolylineSection> aPolylineData = myEditedObject->GetPolylineData();
88 for( int i = 0 ; i < aPolylineData.size() ; i++ ){
89 std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
90 bool isClosed = aPolylineData[i].myIsClosed;
91 CurveCreator::SectionType aType = CurveCreator::Polyline;
92 if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
93 aType = CurveCreator::Spline;
95 CurveCreator::Coordinates aCoords;
96 for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
97 aCoords.push_back(aPolylineData[i].myCoords[j]);
99 myCurve->addSection( aName, aType, isClosed, aCoords );
101 aPanel->setPolylineName( myEditedObject->GetName() );
105 myCurve = new CurveCreator_Curve(CurveCreator::Dim2d);
106 //aPanel->setCurve(myCurve);
107 QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
108 aPanel->setPolylineName(aNewName);
110 aPanel->setCurve(myCurve);
115 void HYDROGUI_PolylineOp::abortOperation()
117 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
119 aPanel->setOCCViewer( 0 );
122 HYDROGUI_Operation::abortOperation();
125 void HYDROGUI_PolylineOp::commitOperation()
127 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
129 aPanel->setOCCViewer( 0 );
132 HYDROGUI_Operation::commitOperation();
135 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
137 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
138 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
142 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
143 QString& theErrorMsg )
145 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
147 int aStudyId = module()->getStudyId();
148 bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
149 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
150 if( aDocument.IsNull() )
153 Handle(HYDROData_Polyline) aPolylineObj;
155 aPolylineObj = myEditedObject;
158 aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
160 //double aZValue = double( ++ZValueIncrement ) * 1e-2; // empiric value, to be revised
161 //aPolylineObj->SetZValue( aZValue );
164 if( aPolylineObj.IsNull() )
167 QString aPolylineName = aPanel->getPolylineName();
168 aPolylineObj->SetName(aPolylineName);
170 if( myCurve->getDimension() == CurveCreator::Dim2d )
172 aPolylineObj->SetDimension(aDimInt);
173 QList<PolylineSection> aPolylineData;
174 for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
175 PolylineSection aSect;
176 aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
177 aSect.myIsClosed = myCurve->isClosed(i);
178 aSect.myType = PolylineSection::SECTION_POLYLINE;
179 if( myCurve->getSectionType(i) == CurveCreator::Spline ){
180 aSect.myType = PolylineSection::SECTION_SPLINE;
182 CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
183 for( int j = 0 ; j < aCoords.size() ; j++ ){
184 aSect.myCoords << aCoords.at(j);
186 aPolylineData << aSect;
188 aPolylineObj->SetPolylineData(aPolylineData);
190 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
191 module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), aPolylineObj, true );
195 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
197 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
202 if( !myCurveDisplayer )
204 QList<int> aSelSections = aPanel->getSelectedSections();
205 for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
207 if( aSelSections.contains(i) ){
208 myCurveDisplayer->highlight( myCurve->constructSection(i), aIsHl );
213 void HYDROGUI_PolylineOp::displayPreview()
217 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
219 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
222 myCurveDisplayer = new CurveCreator_Displayer( aCtx );
223 myCurve->setDisplayer( myCurveDisplayer );
224 myCurveDisplayer->display( myCurve->constructWire() );
230 void HYDROGUI_PolylineOp::erasePreview()
234 if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
236 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
237 if( !aCtx.IsNull() && myCurveDisplayer )
239 myCurveDisplayer->erase();
240 delete myCurveDisplayer;
241 myCurveDisplayer = 0;