1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_PolylineOp.h"
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_DataObject.h"
23 #include "HYDROGUI_PolylineDlg.h"
24 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_UpdateFlags.h"
27 #include <HYDROData_Document.h>
28 #include <HYDROData_PolylineOperator.h>
29 #include <HYDROData_TopoCurve.h>
31 #include <CurveCreator_Curve.hxx>
32 #include <CurveCreator_Displayer.hxx>
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <LightApp_UpdateFlags.h>
38 #include <OCCViewer_ViewManager.h>
39 #include <OCCViewer_ViewModel.h>
40 #include <OCCViewer_ViewWindow.h>
42 #include <OCCViewer_AISSelector.h>
44 #include <Precision.hxx>
46 #include <SUIT_MessageBox.h>
47 #include <SUIT_Desktop.h>
49 #include <TopoDS_Shape.hxx>
50 #include <TopoDS_Wire.hxx>
52 //static int ZValueIncrement = 0;
54 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
55 : HYDROGUI_Operation( theModule ),
56 myIsEdit( theIsEdit ),
59 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
62 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
68 * Redirect the delete action to input panel
70 void HYDROGUI_PolylineOp::deleteSelected()
72 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
73 aPanel->deleteSelected();
77 * Checks whether there are some to delete
79 bool HYDROGUI_PolylineOp::deleteEnabled()
81 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
82 return aPanel->deleteEnabled();
86 * Set Z layer for the operation preview.
87 \param theLayer a layer position
89 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
91 HYDROGUI_Operation::updatePreviewZLayer( theLayer );
93 int aZLayer = getPreviewZLayer();
96 if( getPreviewManager() )
98 if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
100 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
103 Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
104 aCtx->SetZLayer( anObject, aZLayer );
111 void HYDROGUI_PolylineOp::startOperation()
115 if ( isApplyAndClose() )
116 myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
117 if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
119 // Polyline is imported from GEOM module an is not recognized as
120 // polyline or spline and exist only as shape presentation
121 SUIT_MessageBox::critical( module()->getApp()->desktop(),
122 tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
123 tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
132 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
134 HYDROGUI_Operation::startOperation();
136 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
139 LightApp_Application* anApp = module()->getApp();
140 OCCViewer_ViewManager* aViewManager =
141 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
142 aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
143 setPreviewManager( aViewManager );
145 if ( isApplyAndClose() )
148 QString aPolylineName;
149 if( !myEditedObject.IsNull() )
151 NCollection_Sequence<TCollection_AsciiString> aSectNames;
152 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
153 NCollection_Sequence<bool> aSectClosures;
154 myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
156 if (!aSectNames.IsEmpty())
158 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
160 QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
161 HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
162 bool aSectClosure = aSectClosures.Value( i );
164 CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
165 if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
166 aCurveType = CurveCreator::Spline;
168 CurveCreator::Coordinates aCurveCoords;
170 HYDROData_PolylineXY::PointsList aSectPointsList =
171 myEditedObject->GetPoints( i - 1 );
172 for (int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k)
174 const HYDROData_PolylineXY::Point& aSectPoint =
175 aSectPointsList.Value( k );
176 aCurveCoords.push_back( aSectPoint.X() );
177 aCurveCoords.push_back( aSectPoint.Y() );
180 myCurve->addSectionInternal( aSectName.toStdString(),
181 aCurveType, aSectClosure, aCurveCoords );
186 std::deque<CurveCreator::Coordinates> aPs;
187 std::deque<bool> isCloseds;
188 std::vector<TopoDS_Wire> aWires;
189 HYDROData_PolylineOperator::GetWires(myEditedObject, aWires);
190 const int aSCount = aWires.size();
191 bool isError = false;
192 for (int aSI = 0; aSI < aSCount; ++aSI)
194 HYDROData_TopoCurve aCurve, aCurve2;
195 std::deque<gp_XYZ> aPs2;
196 if (!aCurve.Initialize(aWires[aSI]) ||
197 !aCurve.BSplinePiecewiseCurve(Precision::Confusion(), aCurve2) ||
198 !aCurve2.ValuesInKnots(aPs2))
204 aPs.push_back(CurveCreator::Coordinates());
205 CurveCreator::Coordinates& aPs3 = aPs.back();
206 const Standard_Integer aPCount = aPs2.size();
207 for (Standard_Integer aPI = 0; aPI < aPCount; ++aPI)
209 const gp_XYZ aP = aPs2[aPI];
210 aPs3.push_back(aP.X());
211 aPs3.push_back(aP.Y());
213 isCloseds.push_back(aCurve.IsClosed());
218 const TCollection_AsciiString aNamePrefix = "Section_";
219 for (int aSI = 0; aSI < aSCount; ++aSI)
221 myCurve->addSectionInternal((aNamePrefix + (aSI + 1)).ToCString(),
222 CurveCreator::Spline, isCloseds[aSI], aPs[aSI]);
227 aPolylineName = myEditedObject->GetName();
231 aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
234 aPanel->setPolylineName( aPolylineName );
235 aPanel->setCurve( myCurve );
240 void HYDROGUI_PolylineOp::abortOperation()
244 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
246 aPanel->setOCCViewer( 0 );
249 HYDROGUI_Operation::abortOperation();
252 void HYDROGUI_PolylineOp::commitOperation()
254 if ( isApplyAndClose() )
258 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
260 aPanel->setOCCViewer( 0 );
265 HYDROGUI_Operation::commitOperation();
268 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
270 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
271 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
275 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
276 QString& theErrorMsg,
277 QStringList& theBrowseObjectsEntries )
279 HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
283 QString aPolylineName = aPanel->getPolylineName().simplified();
284 if ( aPolylineName.isEmpty() )
286 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
290 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
292 // check that there are no other objects with the same name in the document
293 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
294 if( !anObject.IsNull() )
296 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
301 if ( myCurve->getNbSections() <= 0 )
303 theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
307 Handle(HYDROData_PolylineXY) aPolylineObj;
310 aPolylineObj = myEditedObject;
311 aPolylineObj->RemoveSections();
315 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
318 if( aPolylineObj.IsNull() )
321 aPolylineObj->SetName( aPolylineName );
323 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
325 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
326 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
327 bool aSectClosure = myCurve->isClosed( i );
329 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
331 if ( aCurveType == CurveCreator::Spline )
332 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
334 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
336 // Add the points fro section
337 CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
339 if ( aCurveCoords.size() <= 2 )
341 theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
345 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
347 HYDROData_PolylineXY::Point aSectPoint;
349 aSectPoint.SetX( aCurveCoords.at( k ) );
351 aSectPoint.SetY( aCurveCoords.at( k ) );
353 aPolylineObj->AddPoint( i, aSectPoint );
359 aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
362 // Update the wire of polyline
363 aPolylineObj->Update();
364 module()->setIsToUpdate( aPolylineObj );
366 // the viewer should be release from the widget before the module update it
367 // because it has an opened local context and updated presentation should not be displayed in it
369 aPanel->setOCCViewer( 0 );
371 theUpdateFlags = UF_Model;
373 // the polyline should be rebuild in all viewers, where it is displayed
374 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
375 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
376 theUpdateFlags |= UF_VTKViewer;
378 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
379 if ( anActiveViewId == 0 )
381 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
386 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
387 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
388 theBrowseObjectsEntries.append( anEntry );
394 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
396 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
401 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
404 //QList<int> aSelSections = aPanel->getSelectedSections();
406 //if( aSelSections.contains(i) ){
408 //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
412 void HYDROGUI_PolylineOp::displayPreview()
414 if( getPreviewManager() )
416 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
418 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
421 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
422 myCurve->setDisplayer( aDisplayer );
423 aDisplayer->display( myCurve->getAISObject( true ), true );
429 void HYDROGUI_PolylineOp::erasePreview()
431 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
432 if( getPreviewManager() && aDisplayer )
434 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
436 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
439 aDisplayer->eraseAll( true );
444 setPreviewManager( NULL );