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;
53 static const double HYDROGUI_MAXIMAL_DEFLECTION = 1e-2;
55 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
56 : HYDROGUI_Operation( theModule ),
57 myIsEdit( theIsEdit ),
60 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
63 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
69 * Redirect the delete action to input panel
71 void HYDROGUI_PolylineOp::deleteSelected()
73 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
74 aPanel->deleteSelected();
78 * Checks whether there are some to delete
80 bool HYDROGUI_PolylineOp::deleteEnabled()
82 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
83 return aPanel->deleteEnabled();
87 * Set Z layer for the operation preview.
88 \param theLayer a layer position
90 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
92 HYDROGUI_Operation::updatePreviewZLayer( theLayer );
94 int aZLayer = getPreviewZLayer();
97 if( getPreviewManager() )
99 if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
101 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
104 Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
105 aCtx->SetZLayer( anObject, aZLayer );
112 void HYDROGUI_PolylineOp::startOperation()
116 if ( isApplyAndClose() )
117 myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
118 if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
120 // Polyline is imported from GEOM module an is not recognized as
121 // polyline or spline and exist only as shape presentation
122 SUIT_MessageBox::critical( module()->getApp()->desktop(),
123 tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
124 tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
133 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
135 HYDROGUI_Operation::startOperation();
137 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
140 LightApp_Application* anApp = module()->getApp();
141 OCCViewer_ViewManager* aViewManager =
142 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
143 aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
144 setPreviewManager( aViewManager );
146 if ( isApplyAndClose() )
149 QString aPolylineName;
150 if( !myEditedObject.IsNull() )
152 NCollection_Sequence<TCollection_AsciiString> aSectNames;
153 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
154 NCollection_Sequence<bool> aSectClosures;
155 myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
157 if (!aSectNames.IsEmpty())
159 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
161 QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
162 HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
163 bool aSectClosure = aSectClosures.Value( i );
165 CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
166 if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
167 aCurveType = CurveCreator::Spline;
169 CurveCreator::Coordinates aCurveCoords;
171 HYDROData_PolylineXY::PointsList aSectPointsList =
172 myEditedObject->GetPoints( i - 1 );
173 for (int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k)
175 const HYDROData_PolylineXY::Point& aSectPoint =
176 aSectPointsList.Value( k );
177 aCurveCoords.push_back( aSectPoint.X() );
178 aCurveCoords.push_back( aSectPoint.Y() );
181 myCurve->addSectionInternal( aSectName.toStdString(),
182 aCurveType, aSectClosure, aCurveCoords );
187 std::deque<CurveCreator::Coordinates> aPs;
188 std::deque<bool> isCloseds;
189 std::vector<TopoDS_Wire> aWires;
190 HYDROData_PolylineOperator::GetWires(myEditedObject, aWires);
191 const int aSCount = aWires.size();
192 bool isError = false;
193 for (int aSI = 0; aSI < aSCount; ++aSI)
195 HYDROData_TopoCurve aCurve, aCurve2;
196 std::list<gp_XYZ> aPs2;
198 if (!aCurve.Initialize(aWires[aSI]) ||
199 (aMaxPieceCount = aCurve.BSplinePiecewiseCurve(
200 HYDROGUI_MAXIMAL_DEFLECTION * 0.1, aCurve2)) == 0)
207 aMaxPieceCount *= 100;
209 while (aPieceCount < aMaxPieceCount &&
210 (aDefl = HYDROData_PolylineOperator::ReduceDeflection(
211 HYDROGUI_MAXIMAL_DEFLECTION, aCurve2, aPieceCount)) >
212 HYDROGUI_MAXIMAL_DEFLECTION);
213 if (aDefl < 0 || !aCurve2.ValuesInKnots(aPs2))
219 aPs.push_back(CurveCreator::Coordinates());
220 CurveCreator::Coordinates& aPs3 = aPs.back();
221 std::list<gp_XYZ>::const_iterator aLastPIt = aPs2.end();
222 std::list<gp_XYZ>::const_iterator aPIt = aPs2.begin();
223 for (; aPIt != aLastPIt; ++aPIt)
225 const gp_XYZ aP = *aPIt;
226 aPs3.push_back(aP.X());
227 aPs3.push_back(aP.Y());
229 isCloseds.push_back(aCurve.IsClosed());
234 const TCollection_AsciiString aNamePrefix = "Section_";
235 for (int aSI = 0; aSI < aSCount; ++aSI)
237 myCurve->addSectionInternal((aNamePrefix + (aSI + 1)).ToCString(),
238 CurveCreator::Spline, isCloseds[aSI], aPs[aSI]);
243 aPolylineName = myEditedObject->GetName();
247 aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
250 aPanel->setPolylineName( aPolylineName );
251 aPanel->setCurve( myCurve );
256 void HYDROGUI_PolylineOp::abortOperation()
260 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
262 aPanel->setOCCViewer( 0 );
265 HYDROGUI_Operation::abortOperation();
268 void HYDROGUI_PolylineOp::commitOperation()
270 if ( isApplyAndClose() )
274 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
276 aPanel->setOCCViewer( 0 );
281 HYDROGUI_Operation::commitOperation();
284 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
286 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
287 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
291 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
292 QString& theErrorMsg,
293 QStringList& theBrowseObjectsEntries )
295 HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
299 QString aPolylineName = aPanel->getPolylineName().simplified();
300 if ( aPolylineName.isEmpty() )
302 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
306 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
308 // check that there are no other objects with the same name in the document
309 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
310 if( !anObject.IsNull() )
312 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
317 if ( myCurve->getNbSections() <= 0 )
319 theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
323 Handle(HYDROData_PolylineXY) aPolylineObj;
326 aPolylineObj = myEditedObject;
327 aPolylineObj->RemoveSections();
331 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
334 if( aPolylineObj.IsNull() )
337 aPolylineObj->SetName( aPolylineName );
339 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
341 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
342 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
343 bool aSectClosure = myCurve->isClosed( i );
345 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
347 if ( aCurveType == CurveCreator::Spline )
348 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
350 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
352 // Add the points fro section
353 CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
355 if ( aCurveCoords.size() <= 2 )
357 theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
361 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
363 HYDROData_PolylineXY::Point aSectPoint;
365 aSectPoint.SetX( aCurveCoords.at( k ) );
367 aSectPoint.SetY( aCurveCoords.at( k ) );
369 aPolylineObj->AddPoint( i, aSectPoint );
375 aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
378 // Update the wire of polyline
379 aPolylineObj->Update();
380 module()->setIsToUpdate( aPolylineObj );
382 // the viewer should be release from the widget before the module update it
383 // because it has an opened local context and updated presentation should not be displayed in it
385 aPanel->setOCCViewer( 0 );
387 theUpdateFlags = UF_Model;
389 // the polyline should be rebuild in all viewers, where it is displayed
390 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
391 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
392 theUpdateFlags |= UF_VTKViewer;
394 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
395 if ( anActiveViewId == 0 )
397 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
402 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
403 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
404 theBrowseObjectsEntries.append( anEntry );
410 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
412 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
417 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
420 //QList<int> aSelSections = aPanel->getSelectedSections();
422 //if( aSelSections.contains(i) ){
424 //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
428 void HYDROGUI_PolylineOp::displayPreview()
430 if( getPreviewManager() )
432 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
434 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
437 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
438 myCurve->setDisplayer( aDisplayer );
439 aDisplayer->display( myCurve->getAISObject( true ), true );
445 void HYDROGUI_PolylineOp::erasePreview()
447 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
448 if( getPreviewManager() && aDisplayer )
450 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
452 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
455 aDisplayer->eraseAll( true );
460 setPreviewManager( NULL );