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>
34 #include <CurveCreator_Utils.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 #include <SUIT_MessageBox.h>
49 #include <SUIT_Desktop.h>
51 #include <TopoDS_Shape.hxx>
52 #include <TopoDS_Wire.hxx>
55 #include "HYDRO_trace.hxx"
57 //static int ZValueIncrement = 0;
58 static const double HYDROGUI_MAXIMAL_DEFLECTION = 1e-2;
60 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
61 : HYDROGUI_Operation( theModule ),
62 myIsEdit( theIsEdit ),
65 setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
68 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
74 * Redirect the delete action to input panel
76 void HYDROGUI_PolylineOp::deleteSelected()
78 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
79 aPanel->deleteSelected();
83 * Checks whether there are some to delete
85 bool HYDROGUI_PolylineOp::deleteEnabled()
87 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
88 return aPanel->deleteEnabled();
92 * Set Z layer for the operation preview.
93 \param theLayer a layer position
95 void HYDROGUI_PolylineOp::updatePreviewZLayer( int theLayer )
97 HYDROGUI_Operation::updatePreviewZLayer( theLayer );
99 int aZLayer = getPreviewZLayer();
102 if( getPreviewManager() )
104 if ( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
106 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
109 Handle(AIS_InteractiveObject) anObject = myCurve->getAISObject( true );
110 aCtx->SetZLayer( anObject, aZLayer );
117 void HYDROGUI_PolylineOp::startOperation()
119 DEBTRACE("startOperation");
122 if ( isApplyAndClose() )
123 myEditedObject = Handle(HYDROData_PolylineXY)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
124 if ( !myEditedObject.IsNull() && !myEditedObject->IsEditable() )
126 // Polyline is imported from GEOM module an is not recognized as
127 // polyline or spline and exist only as shape presentation
128 SUIT_MessageBox::critical( module()->getApp()->desktop(),
129 tr( "POLYLINE_IS_UNEDITABLE_TLT" ),
130 tr( "POLYLINE_IS_UNEDITABLE_MSG" ) );
139 myCurve = new CurveCreator_Curve( CurveCreator::Dim2d );
141 HYDROGUI_Operation::startOperation();
143 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
146 LightApp_Application* anApp = module()->getApp();
147 OCCViewer_ViewManager* aViewManager =
148 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
149 aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
150 setPreviewManager( aViewManager );
152 if ( isApplyAndClose() )
155 QString aPolylineName;
156 if( !myEditedObject.IsNull() )
158 NCollection_Sequence<TCollection_AsciiString> aSectNames;
159 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
160 NCollection_Sequence<bool> aSectClosures;
161 bool aIsInCustom = myEditedObject->GetIsInCustomFlag();
162 myEditedObject->SetIsInCustomFlag( true );
163 myEditedObject->GetSections( aSectNames, aSectTypes, aSectClosures );
164 myEditedObject->SetIsInCustomFlag( aIsInCustom );
166 if (!aSectNames.IsEmpty())
168 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
170 QString aSectName = HYDROGUI_Tool::ToQString( aSectNames.Value( i ) );
171 HYDROData_PolylineXY::SectionType aSectType = aSectTypes.Value( i );
172 bool aSectClosure = aSectClosures.Value( i );
174 CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
175 if( aSectType == HYDROData_PolylineXY::SECTION_SPLINE )
176 aCurveType = CurveCreator::Spline;
178 CurveCreator::Coordinates aCurveCoords;
180 HYDROData_PolylineXY::PointsList aSectPointsList =
181 myEditedObject->GetPoints( i - 1 );
182 for (int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k)
184 const HYDROData_PolylineXY::Point& aSectPoint =
185 aSectPointsList.Value( k );
186 aCurveCoords.push_back( aSectPoint.X() );
187 aCurveCoords.push_back( aSectPoint.Y() );
190 Quantity_Color aColor = CurveCreator_Utils::getRandColor();
191 QColor aQColor = CurveCreator_Utils::colorConv(aColor);
192 myEditedObject->GetSectionColor(i-1, aQColor);
194 myCurve->addSectionInternal( aSectName.toStdString(),
195 aCurveType, aSectClosure, aCurveCoords, CurveCreator_Utils::colorConv(aQColor) );
200 std::deque<CurveCreator::Coordinates> aPs;
201 std::deque<bool> isCloseds;
202 std::vector<TopoDS_Wire> aWires;
203 HYDROData_PolylineOperator::GetWires(myEditedObject, aWires);
204 const int aSCount = aWires.size();
205 bool isError = false;
206 for (int aSI = 0; aSI < aSCount; ++aSI)
208 HYDROData_TopoCurve aCurve, aCurve2;
209 std::list<gp_XYZ> aPs2;
211 if (!aCurve.Initialize(aWires[aSI]) ||
212 (aMaxPieceCount = aCurve.BSplinePiecewiseCurve(
213 HYDROGUI_MAXIMAL_DEFLECTION * 0.1, aCurve2)) == 0)
220 aMaxPieceCount *= 100;
222 while (aPieceCount < aMaxPieceCount &&
223 (aDefl = HYDROData_PolylineOperator::ReduceDeflection(
224 HYDROGUI_MAXIMAL_DEFLECTION, aCurve2, aPieceCount)) >
225 HYDROGUI_MAXIMAL_DEFLECTION);
226 if (aDefl < 0 || !aCurve2.ValuesInKnots(aPs2))
232 aPs.push_back(CurveCreator::Coordinates());
233 CurveCreator::Coordinates& aPs3 = aPs.back();
234 std::list<gp_XYZ>::const_iterator aLastPIt = aPs2.end();
235 std::list<gp_XYZ>::const_iterator aPIt = aPs2.begin();
236 for (; aPIt != aLastPIt; ++aPIt)
238 const gp_XYZ aP = *aPIt;
239 aPs3.push_back(aP.X());
240 aPs3.push_back(aP.Y());
242 isCloseds.push_back(aCurve.IsClosed());
247 const TCollection_AsciiString aNamePrefix = "Section_";
248 for (int aSI = 0; aSI < aSCount; ++aSI)
250 myCurve->addSectionInternal((aNamePrefix + (aSI + 1)).ToCString(),
251 CurveCreator::Spline, isCloseds[aSI], aPs[aSI], CurveCreator_Utils::getRandColor());
256 aPolylineName = myEditedObject->GetName();
260 aPolylineName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_NAME" ) );
263 aPanel->setPolylineName( aPolylineName );
264 aPanel->setCurve( myCurve );
269 void HYDROGUI_PolylineOp::abortOperation()
273 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
275 aPanel->setOCCViewer( 0 );
278 HYDROGUI_Operation::abortOperation();
281 void HYDROGUI_PolylineOp::commitOperation()
283 DEBTRACE("commitOperation");
284 if ( isApplyAndClose() )
288 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
290 aPanel->setOCCViewer( 0 );
295 HYDROGUI_Operation::commitOperation();
298 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
300 HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
301 connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
305 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
306 QString& theErrorMsg,
307 QStringList& theBrowseObjectsEntries )
309 DEBTRACE("processApply");
310 HYDROGUI_PolylineDlg* aPanel = ::qobject_cast<HYDROGUI_PolylineDlg*>( inputPanel() );
314 QString aPolylineName = aPanel->getPolylineName().simplified();
315 if ( aPolylineName.isEmpty() )
317 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
321 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aPolylineName ) )
323 // check that there are no other objects with the same name in the document
324 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aPolylineName );
325 if( !anObject.IsNull() )
327 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aPolylineName );
332 if ( myCurve->getNbSections() <= 0 )
334 theErrorMsg = tr( "EMPTY_POLYLINE_DATA" );
338 Handle(HYDROData_PolylineXY) aPolylineObj;
341 aPolylineObj = myEditedObject;
342 aPolylineObj->RemoveSections();
346 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
349 if( aPolylineObj.IsNull() )
352 aPolylineObj->SetName( aPolylineName );
354 for ( int i = 0 ; i < myCurve->getNbSections() ; i++ )
356 TCollection_AsciiString aSectName = HYDROGUI_Tool::ToAsciiString( myCurve->getSectionName( i ).c_str() );
357 CurveCreator::SectionType aCurveType = myCurve->getSectionType( i );
358 bool aSectClosure = myCurve->isClosed( i );
360 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
362 if ( aCurveType == CurveCreator::Spline )
363 aSectType = HYDROData_PolylineXY::SECTION_SPLINE;
365 aPolylineObj->AddSection( aSectName, aSectType, aSectClosure );
367 Quantity_Color aColor = myCurve->getColorSection(i);
368 aPolylineObj->SetSectionColor(i, CurveCreator_Utils::colorConv(aColor));
370 // Add the points from section
371 CurveCreator::Coordinates aCurveCoords = myCurve->getCoords( i );
373 if ( aCurveCoords.size() <= 2 )
375 theErrorMsg = tr( "NUMBER_OF_SECTION_POINTS_INCORRECT" );
379 for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
381 HYDROData_PolylineXY::Point aSectPoint;
383 aSectPoint.SetX( aCurveCoords.at( k ) );
385 aSectPoint.SetY( aCurveCoords.at( k ) );
387 aPolylineObj->AddPoint( i, aSectPoint );
393 // aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
396 // Update the wire of polyline
397 aPolylineObj->Update();
398 module()->setIsToUpdate( aPolylineObj );
400 // the viewer should be release from the widget before the module update it
401 // because it has an opened local context and updated presentation should not be displayed in it
403 aPanel->setOCCViewer( 0 );
405 theUpdateFlags = UF_Model;
407 // the polyline should be rebuild in all viewers, where it is displayed
408 theUpdateFlags |= UF_Viewer | UF_GV_Forced;
409 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced;
410 theUpdateFlags |= UF_VTKViewer;
412 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
413 if ( anActiveViewId == 0 )
415 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
420 module()->setObjectVisible( anActiveViewId, aPolylineObj, true );
421 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aPolylineObj );
422 theBrowseObjectsEntries.append( anEntry );
428 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
430 HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
435 CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
438 //QList<int> aSelSections = aPanel->getSelectedSections();
440 //if( aSelSections.contains(i) ){
442 //aDisplayer->highlight( myCurve->getAISObject(), aIsHl );
446 void HYDROGUI_PolylineOp::displayPreview()
448 if( getPreviewManager() )
450 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
452 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
455 CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx, getPreviewZLayer() );
456 myCurve->setDisplayer( aDisplayer );
457 aDisplayer->display( myCurve->getAISObject( true ), true );
463 void HYDROGUI_PolylineOp::erasePreview()
465 CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
466 if( getPreviewManager() && aDisplayer )
468 if( OCCViewer_Viewer* aViewer = getPreviewManager()->getOCCViewer() )
470 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
473 aDisplayer->eraseAll( true );
478 setPreviewManager( NULL );