1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File : StdMeshersGUI_PropagationHelperWdg.cxx
20 // Created : Thu Mar 19 18:46:24 2015
21 // Author : Edward AGAPOV (eap)
23 #include "StdMeshersGUI_PropagationHelperWdg.h"
25 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
26 #include "SMESH_PreviewActorsCollection.h"
27 #include "SMESHGUI_VTKUtils.h"
29 #include <GEOM_Actor.h>
31 #include <LightApp_SelectionMgr.h>
32 #include <SUIT_OverrideCursor.h>
33 #include <SVTK_ViewWindow.h>
34 #include <vtkRenderer.h>
37 #include <QGridLayout>
41 #include <QListWidget>
42 #include <QListWidgetItem>
43 #include <QPushButton>
45 #include <BRepTools_WireExplorer.hxx>
46 #include <BRep_Builder.hxx>
47 #include <NCollection_DataMap.hxx>
48 #include <TColStd_IndexedMapOfInteger.hxx>
49 #include <TopExp_Explorer.hxx>
51 #include <TopoDS_Compound.hxx>
52 #include <TopoDS_Iterator.hxx>
59 //================================================================================
63 //================================================================================
65 StdMeshersGUI_PropagationHelperWdg::
66 StdMeshersGUI_PropagationHelperWdg( StdMeshersGUI_SubShapeSelectorWdg* subSelectWdg,
68 QWidget( parent ), mySubSelectWdg( subSelectWdg ), myActor( 0 ), myModelActor( 0 )
70 QGroupBox* helperBox = new QGroupBox( tr("HELPER"), this );
71 QCheckBox* showGeomChkBox = new QCheckBox( tr("SHOW_GEOMETRY"), helperBox );
72 QGroupBox* chainBox = new QGroupBox( tr("PROPAGATION_CHAINS"), helperBox );
73 chainBox->setCheckable( true );
74 chainBox->setChecked( false );
75 myListWidget = new QListWidget( helperBox );
76 myListWidget->setSelectionMode( QAbstractItemView::SingleSelection );
77 myAddButton = new QPushButton( tr("ADD"), helperBox );
78 myReverseButton = new QPushButton( tr("REVERSE"), helperBox );
80 QGridLayout* chainsLayout = new QGridLayout( chainBox );
81 chainsLayout->setMargin( MARGIN );
82 chainsLayout->setSpacing( SPACING );
83 chainsLayout->addWidget(myListWidget, 0, 0, 3, 3);
84 chainsLayout->addWidget(myAddButton, 0, 3);
85 chainsLayout->addWidget(myReverseButton, 1, 3);
87 QVBoxLayout* helperLayout = new QVBoxLayout( helperBox );
88 helperLayout->setMargin( MARGIN );
89 helperLayout->setSpacing( SPACING );
90 helperLayout->addWidget( showGeomChkBox );
91 helperLayout->addWidget( chainBox );
93 QVBoxLayout* lay = new QVBoxLayout( this );
95 lay->setSpacing( SPACING );
96 lay->addWidget( helperBox );
98 connect( showGeomChkBox, SIGNAL( toggled(bool)), SLOT( onShowGeometry(bool)));
99 connect( chainBox, SIGNAL( toggled(bool)), SLOT( updateList(bool)));
100 connect( myListWidget, SIGNAL( itemSelectionChanged()), SLOT( onListSelectionChanged() ));
101 connect( myAddButton, SIGNAL( clicked(bool)), SLOT( onAdd() ));
102 connect( myReverseButton, SIGNAL( clicked(bool)), SLOT( onReverse() ));
104 onListSelectionChanged();
107 //================================================================================
111 //================================================================================
113 StdMeshersGUI_PropagationHelperWdg::~StdMeshersGUI_PropagationHelperWdg()
117 myActor->RemoveFromRender( myRenderer );
123 myModelActor->RemoveFromRender( myRenderer );
124 myModelActor->Delete();
129 //================================================================================
131 * \brief SLOT called when 'Show Geometry' is checked
133 //================================================================================
135 void StdMeshersGUI_PropagationHelperWdg::onShowGeometry(bool toShow)
137 if ( ! myModelActor )
139 TopoDS_Shape shape = mySubSelectWdg->GetGeomShape();
140 TopoDS_Shape mainShape = mySubSelectWdg->GetMainShape();
141 if ( shape.IsNull() && mainShape.IsNull() ) return;
142 if ( mainShape.IsNull() ) mainShape = shape;
144 SUIT_OverrideCursor wc;
146 TopoDS_Compound aCompound;
147 BRep_Builder aBuilder;
148 aBuilder.MakeCompound( aCompound );
150 SMESH_PreviewActorsCollection* previewActor = mySubSelectWdg->GetActorCollection();
151 if ( !previewActor ) return;
152 const QList<int>& egdeIDs = previewActor->GetIndices();
153 for ( QList<int>::const_iterator ieIt = egdeIDs.begin(); ieIt != egdeIDs.end(); ++ieIt )
155 TopoDS_Shape E = previewActor->GetShapeByIndex( *ieIt );
156 if ( !E.IsNull() && E.ShapeType() == TopAbs_EDGE )
157 aBuilder.Add( aCompound, E );
160 myModelActor = GEOM_Actor::New();
161 myModelActor->SetShape( aCompound, 0, 0 );
162 myModelActor->SetPickable( false );
163 myModelActor->SetIsolatedEdgeColor( 0.5, 0.5, 0.5 );
165 if (( myRenderer = mySubSelectWdg->GetRenderer() ))
166 myModelActor->AddToRender( myRenderer );
170 myModelActor->SetVisibility( toShow );
172 SMESH::RepaintCurrentView();
175 //================================================================================
177 * \brief Build propagation chains. Return false if no chains found
179 //================================================================================
181 bool StdMeshersGUI_PropagationHelperWdg::buildChains()
183 if ( !myChains.empty() ) return false;
185 if ( !mySubSelectWdg ) return false;
187 TopoDS_Shape shape = mySubSelectWdg->GetGeomShape();
188 TopoDS_Shape mainShape = mySubSelectWdg->GetMainShape();
189 if ( shape.IsNull() && mainShape.IsNull() ) return false;
191 if ( shape.IsNull() ) shape = mainShape;
192 if ( mainShape.IsNull() ) mainShape = shape;
194 SUIT_OverrideCursor wc;
196 // aPreviewActor holds a map od all sub-shapes of mainShape
197 SMESH_PreviewActorsCollection* previewActor = mySubSelectWdg->GetActorCollection();
198 if ( !previewActor ) return false;
199 const QList<int>& egdeIDs = previewActor->GetIndices();
201 // Make a 'map' of WIREs of EDGE with quadrilateral WIREs only
203 typedef int TGeomID; // index in the mainShape
204 typedef std::vector< TGeomID > TWire; // signed IDs of EDGEs, sign means orientation
205 typedef std::pair< int, TWire* > TEdgeInWire; // index in TWire + TWire*
206 typedef std::vector< TEdgeInWire > TWiresOfEdge;// WIREs including an EDGE
208 std::vector< TWire > quadWires;
209 quadWires.reserve( egdeIDs.count() );
210 NCollection_DataMap< TGeomID, TWiresOfEdge > wiresOfEdge( egdeIDs.count() );
212 TopExp_Explorer wire;
213 for ( TopExp_Explorer face( shape, TopAbs_FACE ); face.More(); face.Next() )
215 wire.Init( face.Current(), TopAbs_WIRE );
216 TopoDS_Shape W = wire.Current().Oriented( TopAbs_FORWARD );
219 if ( wire.More() ) continue;
223 for ( TopoDS_Iterator edge( W, false, false ); edge.More() && nbE < 5; ++nbE )
225 if ( nbE != 4 ) continue;
227 // fill a TWire and TWiresOfEdge
228 quadWires.push_back( TWire() );
229 TWire& wire = quadWires.back();
231 for ( BRepTools_WireExplorer edge( TopoDS::Wire( W )); edge.More(); edge.Next() )
233 const TopoDS_Shape& E = edge.Current();
234 int iE = previewActor->GetIndexByShape( E );
237 if ( !wiresOfEdge.IsBound( iE ))
238 wiresOfEdge.Bind( iE, TWiresOfEdge() );
239 wiresOfEdge( iE ).push_back( TEdgeInWire( wire.size(), & wire ));
241 wire.push_back( E.Orientation() == TopAbs_REVERSED ? -iE : iE );
244 if ( quadWires.empty() )
249 TColStd_IndexedMapOfInteger chain, chainedEdges;
251 // loop on all EDGEs in mainShape
252 for ( QList<TGeomID>::const_iterator ieIt = egdeIDs.begin(); ieIt != egdeIDs.end(); ++ieIt )
254 if ( chainedEdges.Contains( *ieIt ))
259 chainedEdges.Add( *ieIt );
260 for ( int iC = 1; iC <= chain.Extent(); ++iC ) // loop on EDGE's in chain
262 TGeomID iE = chain( iC ), iEAbs = Abs( iE );
263 if ( !wiresOfEdge.IsBound( iEAbs ))
265 const TWiresOfEdge& wires = wiresOfEdge( iEAbs );
266 for (size_t i = 0; i < wires.size(); ++i ) // loop on WIREs sharing iE
268 const TEdgeInWire& eInW = wires[i];
269 const TWire& W = *eInW.second;
270 if ( W.size() != 4 ) continue;
271 const int iInW = eInW.first;
272 const int iInWOpp = ( iInW + 2 ) % 4; // an opposite edge index
273 TGeomID iEOppAbs = Abs( W[ iInWOpp ] );
275 int prevNbChained = chainedEdges.Extent();
276 if ( prevNbChained < chainedEdges.Add( iEOppAbs ))
278 int dir = iE / iEAbs;
279 bool isSameDir = ( W[ iInW ] * W[ iInWOpp ] < 0 );
282 chain.Add( dir * iEOppAbs );
287 if ( chain.Extent() > 1 )
289 myChains.push_back( std::vector< TGeomID >() );
290 std::vector< TGeomID > & ch = myChains.back();
291 for ( int iC = 1; iC <= chain.Extent(); ++iC )
292 ch.push_back( chain( iC ) );
295 return !myChains.empty();
298 //================================================================================
300 * \brief Fills myListWidget
302 //================================================================================
304 void StdMeshersGUI_PropagationHelperWdg::updateList(bool enable)
308 myListWidget->clear();
312 QListWidgetItem* item;
313 if ( myChains.empty() || !enable )
315 item = new QListWidgetItem(tr("NO_CHAINS"), myListWidget );
316 item->setData( Qt::UserRole, -1 );
319 for ( size_t i = 0; i < myChains.size(); ++i )
321 QString text = tr( "CHAIN_NUM_NB_EDGES" ).arg( i+1 ).arg( myChains[i].size() );
322 item = new QListWidgetItem( text, myListWidget );
323 item->setData( Qt::UserRole, (int) i );
328 onListSelectionChanged();
332 //================================================================================
334 * \brief Returns ids of a selected chain
336 //================================================================================
338 std::vector< int > * StdMeshersGUI_PropagationHelperWdg::getSelectedChain()
340 std::vector< int > * chain = 0;
341 if ( QListWidgetItem * item = myListWidget->currentItem() )
343 int i = item->data( Qt::UserRole ).toInt();
344 if ( 0 <= i && i < myChains.size() )
345 chain = & myChains[i];
350 //================================================================================
352 * \brief SLOT called when a selected chain changes
354 //================================================================================
356 void StdMeshersGUI_PropagationHelperWdg::onListSelectionChanged()
358 if ( !mySubSelectWdg ) return;
359 SMESH_PreviewActorsCollection* previewActor = mySubSelectWdg->GetActorCollection();
360 if ( !previewActor ) return;
362 bool hasReversedEdges = false;
363 const std::vector< int > * chain = getSelectedChain();
366 SUIT_OverrideCursor wc;
368 TopoDS_Compound aCompound;
369 BRep_Builder aBuilder;
370 aBuilder.MakeCompound( aCompound );
372 for ( size_t i = 0; i < chain->size(); ++i )
374 int iE = Abs( (*chain)[ i ]);
375 TopoDS_Shape E = previewActor->GetShapeByIndex( iE );
376 if ( !E.IsNull() && E.ShapeType() == TopAbs_EDGE )
378 E.Orientation( (*chain)[ i ] < 0 ? TopAbs_REVERSED : TopAbs_FORWARD );
379 aBuilder.Add( aCompound, E );
380 if ( (*chain)[ i ] < 0 )
381 hasReversedEdges = true;
386 // SetShape() to an existing actor leads to a wrong FitAll
387 myActor->RemoveFromRender( myRenderer );
390 myActor = GEOM_Actor::New();
391 myActor->SetShape( aCompound, 0, true );
392 myActor->SetIsolatedEdgeColor( 1, 0, 1 );
393 myActor->SetWidth( 2 );
394 myActor->SetVectorMode( true );
395 myActor->SetPickable( false );
397 if (( myRenderer = mySubSelectWdg->GetRenderer() ))
398 myActor->AddToRender( myRenderer );
400 if ( LightApp_SelectionMgr* selMrg = SMESHGUI::selectionMgr())
402 selMrg->clearSelected();
403 mySubSelectWdg->ClearSelected(); /* call this because the above call does not
404 lead to currentSelectionChanged signal (bug?)*/
407 bool enableButtons = chain;
409 enableButtons = myListWidget->currentItem()->data( Qt::UserRole+1 ).isNull();
411 myAddButton->setEnabled( enableButtons && hasReversedEdges );
412 myReverseButton->setEnabled( enableButtons );
415 bool toShowChain = chain;
418 myActor->SetVisibility( toShowChain );
420 previewActor->SetShown( !toShowChain );
422 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow())
423 aViewWindow->Repaint();
426 //================================================================================
428 * \brief SLOT called when 'Add' button is clicked
430 //================================================================================
432 void StdMeshersGUI_PropagationHelperWdg::onAdd()
434 const std::vector< int > * chain = getSelectedChain();
435 if ( !chain || !mySubSelectWdg ) return;
437 // join current and new IDs
439 SMESH::long_array_var ids = mySubSelectWdg->GetListOfIDs();
441 std::set< int > idSet;
442 for ( int i = 0, nb = ids->length(); i < nb; ++i )
443 idSet.insert( idSet.end(), ids[i] );
445 for ( size_t i = 0; i < chain->size(); ++i )
446 if ( (*chain)[ i ] < 0 )
447 idSet.insert( -1 * (*chain)[ i ]);
449 if ( ids->length() != idSet.size() )
451 ids->length( idSet.size() );
452 std::set< int >::iterator id = idSet.begin();
453 for ( int i = 0, nb = ids->length(); i < nb; ++i, ++id )
456 mySubSelectWdg->SetListOfIDs( ids );
458 mySubSelectWdg->ClearSelected();
460 if ( QListWidgetItem * item = myListWidget->currentItem() )
463 item->setForeground( QBrush( QColor( 100, 100, 100 )));
464 item->setData( Qt::UserRole+1, 1 );
466 myAddButton->setEnabled( false );
467 myReverseButton->setEnabled( false );
470 //================================================================================
472 * \brief SLOT called when 'Reverse' button is clicked
474 //================================================================================
476 void StdMeshersGUI_PropagationHelperWdg::onReverse()
478 if ( std::vector< int > * chain = getSelectedChain())
480 for ( size_t i = 0; i < chain->size(); ++i )
483 onListSelectionChanged();