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_Tool2.h"
26 #include "HYDROGUI_UpdateFlags.h"
28 #include <HYDROData_Document.h>
29 #include <HYDROData_PolylineOperator.h>
30 #include <HYDROData_TopoCurve.h>
32 #include <CurveCreator_Curve.hxx>
33 #include <CurveCreator_Displayer.hxx>
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_UpdateFlags.h>
39 #include <OCCViewer_ViewManager.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <OCCViewer_ViewWindow.h>
43 #include <OCCViewer_AISSelector.h>
45 #include <Precision.hxx>
47 #include <SUIT_MessageBox.h>
48 #include <SUIT_Desktop.h>
50 #include <TopoDS_Shape.hxx>
51 #include <TopoDS_Wire.hxx>
53 //static int ZValueIncrement = 0;
54 static const double HYDROGUI_MAXIMAL_DEFLECTION = 1e-2;
56 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
57 : HYDROGUI_Operation( theModule ),
58 myIsEdit( theIsEdit ),
61 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
64 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
70 * Redirect the delete action to input panel
72 void HYDROGUI_PolylineOp::deleteSelected()
74 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
75 aPanel->deleteSelected();
79 * Checks whether there are some to delete
81 bool HYDROGUI_PolylineOp::deleteEnabled()
83 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
84 return aPanel->deleteEnabled();
88 * Set Z layer for the operation preview.
89 \param theLayer a layer position
91 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
93 HYDROGUI_Operation::updatePreviewZLayer( theLayer );
95 int aZLayer = getPreviewZLayer();
98 if( getPreviewManager() )
100 if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
102 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
105 Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
106 aCtx->SetZLayer( anObject, aZLayer );
113 void HYDROGUI_PolylineOp::startOperation()
117 if ( isApplyAndClose() )
118 myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
119 if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
121 // Polyline is imported from GEOM module an is not recognized as
122 // polyline or spline and exist only as shape presentation
123 SUIT_MessageBox::critical( module()->getApp()->desktop(),
124 tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
125 tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
134 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
136 HYDROGUI_Operation::startOperation();
138 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
141 LightApp_Application* anApp = module()->getApp();
142 OCCViewer_ViewManager* aViewManager =
143 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
144 aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
145 setPreviewManager( aViewManager );
147 if ( isApplyAndClose() )
150 QString aPolylineName;
151 if( !myEditedObject.IsNull() )
153 NCollection_Sequence<TCollection_AsciiString> aSectNames;
154 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
155 NCollection_Sequence<bool> aSectClosures;
156 myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
158 if (!aSectNames.IsEmpty())
160 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
162 QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
163 HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
164 bool aSectClosure = aSectClosures.Value( i );
166 CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
167 if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
168 aCurveType = CurveCreator::Spline;
170 CurveCreator::Coordinates aCurveCoords;
172 HYDROData_PolylineXY::PointsList aSectPointsList =
173 myEditedObject->GetPoints( i - 1 );
174 for (int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k)
176 const HYDROData_PolylineXY::Point& aSectPoint =
177 aSectPointsList.Value( k );
178 aCurveCoords.push_back( aSectPoint.X() );
179 aCurveCoords.push_back( aSectPoint.Y() );
182 myCurve->addSectionInternal( aSectName.toStdString(),
183 aCurveType, aSectClosure, aCurveCoords );
188 std::deque<CurveCreator::Coordinates> aPs;
189 std::deque<bool> isCloseds;
190 std::vector<TopoDS_Wire> aWires;
191 HYDROData_PolylineOperator::GetWires(myEditedObject, aWires);
192 const int aSCount = aWires.size();
193 bool isError = false;
194 for (int aSI = 0; aSI < aSCount; ++aSI)
196 HYDROData_TopoCurve aCurve, aCurve2;
197 std::list<gp_XYZ> aPs2;
199 if (!aCurve.Initialize(aWires[aSI]) ||
200 (aMaxPieceCount = aCurve.BSplinePiecewiseCurve(
201 HYDROGUI_MAXIMAL_DEFLECTION * 0.1, aCurve2)) == 0)
208 aMaxPieceCount *= 100;
210 while (aPieceCount < aMaxPieceCount &&
211 (aDefl = HYDROData_PolylineOperator::ReduceDeflection(
212 HYDROGUI_MAXIMAL_DEFLECTION, aCurve2, aPieceCount)) >
213 HYDROGUI_MAXIMAL_DEFLECTION);
214 if (aDefl < 0 || !aCurve2.ValuesInKnots(aPs2))
220 aPs.push_back(CurveCreator::Coordinates());
221 CurveCreator::Coordinates& aPs3 = aPs.back();
222 std::list<gp_XYZ>::const_iterator aLastPIt = aPs2.end();
223 std::list<gp_XYZ>::const_iterator aPIt = aPs2.begin();
224 for (; aPIt != aLastPIt; ++aPIt)
226 const gp_XYZ aP = *aPIt;
227 aPs3.push_back(aP.X());
228 aPs3.push_back(aP.Y());
230 isCloseds.push_back(aCurve.IsClosed());
235 const TCollection_AsciiString aNamePrefix = "Section_";
236 for (int aSI = 0; aSI < aSCount; ++aSI)
238 myCurve->addSectionInternal((aNamePrefix + (aSI + 1)).ToCString(),
239 CurveCreator::Spline, isCloseds[aSI], aPs[aSI]);
244 aPolylineName = myEditedObject->GetName();
248 aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
251 aPanel->setPolylineName( aPolylineName );
252 aPanel->setCurve( myCurve );
257 void HYDROGUI_PolylineOp::abortOperation()
261 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
263 aPanel->setOCCViewer( 0 );
266 HYDROGUI_Operation::abortOperation();
269 void HYDROGUI_PolylineOp::commitOperation()
271 if ( isApplyAndClose() )
275 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
277 aPanel->setOCCViewer( 0 );
282 HYDROGUI_Operation::commitOperation();
285 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
287 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
288 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
292 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
293 QString& theErrorMsg,
294 QStringList& theBrowseObjectsEntries )
296 HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
300 QString aPolylineName = aPanel->getPolylineName().simplified();
301 if ( aPolylineName.isEmpty() )
303 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
307 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
309 // check that there are no other objects with the same name in the document
310 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
311 if( !anObject.IsNull() )
313 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
318 if ( myCurve->getNbSections() <= 0 )
320 theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
324 Handle(HYDROData_PolylineXY) aPolylineObj;
327 aPolylineObj = myEditedObject;
328 aPolylineObj->RemoveSections();
332 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
335 if( aPolylineObj.IsNull() )
338 aPolylineObj->SetName( aPolylineName );
340 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
342 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
343 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
344 bool aSectClosure = myCurve->isClosed( i );
346 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
348 if ( aCurveType == CurveCreator::Spline )
349 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
351 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
353 // Add the points fro section
354 CurveCreator::Coordinates aCurveCoords = myCurve->getPoints( i );
356 if ( aCurveCoords.size() <= 2 )
358 theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
362 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
364 HYDROData_PolylineXY::Point aSectPoint;
366 aSectPoint.SetX( aCurveCoords.at( k ) );
368 aSectPoint.SetY( aCurveCoords.at( k ) );
370 aPolylineObj->AddPoint( i, aSectPoint );
376 aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
379 // Update the wire of polyline
380 aPolylineObj->Update();
381 module()->setIsToUpdate( aPolylineObj );
383 // the viewer should be release from the widget before the module update it
384 // because it has an opened local context and updated presentation should not be displayed in it
386 aPanel->setOCCViewer( 0 );
388 theUpdateFlags = UF_Model;
390 // the polyline should be rebuild in all viewers, where it is displayed
391 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
392 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
393 theUpdateFlags |= UF_VTKViewer;
395 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
396 if ( anActiveViewId == 0 )
398 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
403 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
404 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
405 theBrowseObjectsEntries.append( anEntry );
411 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
413 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
418 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
421 //QList<int> aSelSections = aPanel->getSelectedSections();
423 //if( aSelSections.contains(i) ){
425 //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
429 void HYDROGUI_PolylineOp::displayPreview()
431 if( getPreviewManager() )
433 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
435 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
438 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
439 myCurve->setDisplayer( aDisplayer );
440 aDisplayer->display( myCurve->getAISObject( true ), true );
446 void HYDROGUI_PolylineOp::erasePreview()
448 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
449 if( getPreviewManager() && aDisplayer )
451 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
453 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
456 aDisplayer->eraseAll( true );
461 setPreviewManager( NULL );