0026886: Visualization, TKV3d - eliminate global variables
[occt.git] / src / AIS / AIS_LocalContext_1.cxx
CommitLineData
b311480e 1// Created on: 1996-10-30
2// Created by: Robert COUBLANC
3// Copyright (c) 1996-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
b311480e 16
7fd59977 17// Modified by rob Thu Apr 02 1998
18// - use of optimisation in SelectMgr_ViewerSelector
19// -> Best management in detected entities...
20
42cf5bc1 21#include <AIS_InteractiveContext.hxx>
22#include <AIS_InteractiveObject.hxx>
23#include <AIS_LocalContext.hxx>
7fd59977 24#include <AIS_LocalStatus.hxx>
42cf5bc1 25#include <AIS_Selection.hxx>
26#include <AIS_Shape.hxx>
27#include <Aspect_Grid.hxx>
28#include <Geom_Transformation.hxx>
b8ddfc2f 29#include <Graphic3d_ArrayOfTriangles.hxx>
7fd59977 30#include <Graphic3d_Group.hxx>
42cf5bc1 31#include <NCollection_Map.hxx>
32#include <OSD_Environment.hxx>
33#include <Prs3d_Drawer.hxx>
34#include <Prs3d_Presentation.hxx>
35#include <Prs3d_ShadingAspect.hxx>
7fd59977 36#include <Select3D_SensitiveTriangulation.hxx>
37#include <SelectBasics_SensitiveEntity.hxx>
42cf5bc1 38#include <SelectMgr_EntityOwner.hxx>
39#include <SelectMgr_Filter.hxx>
40#include <SelectMgr_OrFilter.hxx>
41#include <SelectMgr_SelectableObject.hxx>
7fd59977 42#include <SelectMgr_Selection.hxx>
42cf5bc1 43#include <SelectMgr_SelectionManager.hxx>
e3a6386d 44#include <SelectMgr_SequenceOfOwner.hxx>
42cf5bc1 45#include <Standard_Transient.hxx>
46#include <StdSelect_BRepOwner.hxx>
47#include <StdSelect_ViewerSelector3d.hxx>
48#include <TCollection_AsciiString.hxx>
49#include <TColStd_ListIteratorOfListOfInteger.hxx>
50#include <TColStd_ListOfInteger.hxx>
51#include <TColStd_MapIteratorOfMapOfTransient.hxx>
52#include <TColStd_MapOfTransient.hxx>
42cf5bc1 53#include <V3d_View.hxx>
7fd59977 54
016e5959 55namespace
56{
57 TopoDS_Shape AIS_myDummyShape;
58}
59
7fd59977 60static Standard_Integer GetHiMod(const Handle(AIS_InteractiveObject)& IO)
61{
62 return IO->HasHilightMode() ? IO->HilightMode():0;
63}
64
65//==================================================
679ecdee 66// Function: MoveTo
7fd59977 67// Purpose :
68//==================================================
679ecdee 69AIS_StatusOfDetection AIS_LocalContext::MoveTo (const Standard_Integer theXpix,
70 const Standard_Integer theYpix,
71 const Handle(V3d_View)& theView,
72 const Standard_Boolean theToRedrawImmediate)
7fd59977 73{
679ecdee 74 // check that ViewerSelector gives
75 if (theView->Viewer() != myCTX->CurrentViewer())
76 {
77 return AIS_SOD_Error;
78 }
79
80 myAISCurDetected = 0;
81 myAISDetectedSeq.Clear();
82
83 myCurDetected = 0;
84 myDetectedSeq.Clear();
a272ed94 85 myFilters->SetDisabledObjects (theView->View()->HiddenObjects());
679ecdee 86 myMainVS->Pick (theXpix, theYpix, theView);
87
88 const Standard_Integer aDetectedNb = myMainVS->NbPicked();
89 for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
90 {
91 Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (aDetIter);
92 if (anOwner.IsNull()
93 || !myFilters->IsOk (anOwner))
94 {
95 continue;
7fd59977 96 }
679ecdee 97
57ad5cbd 98 myDetectedSeq.Append (aDetIter); // normally they are already arranged in correct order...
679ecdee 99 Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
57ad5cbd 100 if (!anObj.IsNull())
679ecdee 101 {
102 myAISDetectedSeq.Append (anObj);
7fd59977 103 }
679ecdee 104 }
7fd59977 105
679ecdee 106 // result of courses..
107 if (aDetectedNb == 0 || myDetectedSeq.IsEmpty())
108 {
b586500b 109 if (mylastindex != 0 && mylastindex <= myMapOfOwner->Extent())
679ecdee 110 {
63d907e7 111 myMainPM->ClearImmediateDraw();
b586500b 112 Unhilight (myMapOfOwner->FindKey (mylastindex), theView);
679ecdee 113 if (theToRedrawImmediate)
114 {
115 theView->RedrawImmediate();
7fd59977 116 }
7fd59977 117 }
679ecdee 118
119 mylastindex = 0;
120 return aDetectedNb == 0
121 ? AIS_SOD_Nothing
122 : AIS_SOD_AllBad;
123 }
124
125 // all owners detected by the selector are passed to the
126 // filters and correct ones are preserved...
127 myCurDetected = 1;
128 Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (myDetectedSeq (myCurDetected));
129 manageDetected (anOwner, theView, theToRedrawImmediate);
130 if (myDetectedSeq.Length() == 1)
131 {
132 return aDetectedNb == 1
133 ? AIS_SOD_OnlyOneDetected
134 : AIS_SOD_OnlyOneGood;
135 }
136 else
137 {
138 return AIS_SOD_SeveralGood;
7fd59977 139 }
7fd59977 140}
141
016e5959 142//=======================================================================
143//function : AddSelect
144//purpose :
145//=======================================================================
146AIS_StatusOfPick AIS_LocalContext::AddSelect (const Handle(Standard_Transient)& theObject)
147{
148 mySelection->AddSelect (theObject);
149
150 Standard_Integer aSelNum = mySelection->Extent();
151 return (aSelNum == 1) ? AIS_SOP_OneSelected
152 : (aSelNum > 1) ? AIS_SOP_SeveralSelected
153 : AIS_SOP_Error;
154}
155
c398b00e 156//=======================================================================
157//function : Select
158//purpose :
159//=======================================================================
160AIS_StatusOfPick AIS_LocalContext::Select (const Standard_Boolean toUpdateViewer)
7fd59977 161{
c398b00e 162 if (myAutoHilight)
163 {
164 UnhilightPicked (Standard_False);
165 }
7fd59977 166
c398b00e 167 Standard_Integer aDetIndex = DetectedIndex();
168 if (aDetIndex <= 0)
169 {
170 ClearSelected (toUpdateViewer);
016e5959 171 return (mySelection->Extent() == 0) ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
7fd59977 172 }
173
b586500b 174 const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner->FindKey (aDetIndex);
7fd59977 175
c398b00e 176 ClearSelected (Standard_False);
177
178 if (!anOwner->IsSelected()) // anOwner is not selected
179 {
180 anOwner->SetSelected (Standard_True);
016e5959 181 mySelection->Select (anOwner);
7fd59977 182 }
c398b00e 183
679ecdee 184 if (myAutoHilight)
185 {
7fd59977 186 const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
679ecdee 187 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
188 {
c398b00e 189 Unhilight (anOwner, aViewer->ActiveView());
7fd59977 190 }
191
679ecdee 192 // advanced selection highlighting mechanism
c398b00e 193 if (!anOwner->IsAutoHilight() && anOwner->HasSelectable())
679ecdee 194 {
c398b00e 195 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
679ecdee 196 UpdateSelected (anIO, Standard_False);
197 }
198
c398b00e 199 if (toUpdateViewer)
679ecdee 200 {
201 myCTX->CurrentViewer()->Update();
202 }
7fd59977 203 }
c398b00e 204
016e5959 205 return (mySelection->Extent() == 1) ? AIS_SOP_OneSelected : AIS_SOP_SeveralSelected;
7fd59977 206}
c398b00e 207
208//=======================================================================
209//function : Select
210//purpose :
211//=======================================================================
212AIS_StatusOfPick AIS_LocalContext::Select (const Standard_Integer theXPMin,
213 const Standard_Integer theYPMin,
214 const Standard_Integer theXPMax,
215 const Standard_Integer theYPMax,
216 const Handle(V3d_View)& theView,
217 const Standard_Boolean toUpdateViewer)
7fd59977 218{
c398b00e 219 if (theView->Viewer() == myCTX->CurrentViewer())
220 {
221 myMainVS->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
222 if (myAutoHilight)
223 {
224 UnhilightPicked (Standard_False);
225 }
226
016e5959 227 Standard_Integer aSelNum = mySelection->Extent();
c398b00e 228
7fd59977 229 myMainVS->Init();
c398b00e 230 if (!myMainVS->More())
231 {
232 ClearSelected (toUpdateViewer);
233 mylastindex = 0;
234 return aSelNum == 0 ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
7fd59977 235 }
236
c398b00e 237 ClearSelected (Standard_False);
238
239 for (myMainVS->Init(); myMainVS->More(); myMainVS->Next())
240 {
241 const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked();
242 if (myFilters->IsOk (anOwner))
243 {
244 // it can be helpful to classify this owner immediately...
245 if (!anOwner->IsSelected())
246 {
247 anOwner->SetSelected (Standard_True);
016e5959 248 mySelection->Select (anOwner);
c398b00e 249 }
7fd59977 250 }
251 }
c398b00e 252
253 if (myAutoHilight)
254 {
255 HilightPicked (toUpdateViewer);
256 }
7fd59977 257 }
7fd59977 258
016e5959 259 Standard_Integer aSelNum = mySelection->Extent();
7fd59977 260
c398b00e 261 return (aSelNum == 1) ? AIS_SOP_OneSelected
262 : (aSelNum > 1) ? AIS_SOP_SeveralSelected
263 : AIS_SOP_Error;
264}
7fd59977 265
266//==================================================
c398b00e 267// Function: Select
268// Purpose : Selection by polyline
7fd59977 269//==================================================
c398b00e 270AIS_StatusOfPick AIS_LocalContext::Select (const TColgp_Array1OfPnt2d& thePolyline,
271 const Handle(V3d_View)& theView,
272 const Standard_Boolean toUpdateViewer)
7fd59977 273{
c398b00e 274 if (theView->Viewer() == myCTX->CurrentViewer())
275 {
276 myMainVS->Pick (thePolyline, theView);
7fd59977 277
016e5959 278 Standard_Integer aLastSelNum = mySelection->Extent();
c398b00e 279 myMainVS->Init();
280 if (!myMainVS->More())
281 {
282 // Nothing is selected clear selection.
283 ClearSelected (toUpdateViewer);
284 mylastindex = 0;
285
286 // Return state to know if something was unselected
287 return aLastSelNum == 0 ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
288 }
289
290 if (myAutoHilight)
291 {
292 UnhilightPicked (Standard_False);
293 }
294
295 // Clear previous selection without update to process this selection
296 ClearSelected (Standard_False);
297
298 for (myMainVS->Init(); myMainVS->More(); myMainVS->Next())
299 {
300 const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked();
301 if (myFilters->IsOk (anOwner))
302 {
303 // it can be helpful to classify this owner immediately...
304 if (!anOwner->IsSelected())
305 {
016e5959 306 mySelection->AddSelect (anOwner);
c398b00e 307 anOwner->SetSelected (Standard_True);
308 }
309 }
310 }
311
312 if (myAutoHilight)
313 {
314 HilightPicked (toUpdateViewer);
315 }
316 }
317
016e5959 318 Standard_Integer aSelNum = mySelection->Extent();
c398b00e 319 return (aSelNum == 1) ? AIS_SOP_OneSelected
320 : (aSelNum > 1) ? AIS_SOP_SeveralSelected
321 : AIS_SOP_Error;
322}
323
324//=======================================================================
325//function : ShiftSelect
326//purpose :
327//=======================================================================
328AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const Standard_Boolean toUpdateViewer)
329{
330 Standard_Integer aDetIndex = DetectedIndex();
c398b00e 331
332 if(aDetIndex > 0)
333 {
016e5959 334 Standard_Integer aSelNum = mySelection->Extent();
b586500b 335 const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner->FindKey (aDetIndex);
c398b00e 336 Standard_Boolean toSelect = anOwner->IsSelected() ? Standard_False : Standard_True;
016e5959 337 mySelection->Select (anOwner);
c398b00e 338 anOwner->SetSelected (toSelect);
7fd59977 339
679ecdee 340 if(myAutoHilight)
341 {
63d907e7 342 myMainPM->ClearImmediateDraw();
7fd59977 343 const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
679ecdee 344 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
345 {
c398b00e 346 Unhilight (anOwner, aViewer->ActiveView());
679ecdee 347 }
348
349 // advanced selection highlighting mechanism
c398b00e 350 if (!anOwner->IsAutoHilight() && anOwner->HasSelectable())
679ecdee 351 {
c398b00e 352 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
679ecdee 353 UpdateSelected (anIO, Standard_False);
7fd59977 354 }
355
c398b00e 356 if (toUpdateViewer)
679ecdee 357 {
7fd59977 358 myCTX->CurrentViewer()->Update();
679ecdee 359 }
7fd59977 360 }
c398b00e 361
016e5959 362 Standard_Integer NS = mySelection->Extent();
7fd59977 363 if( NS == 1 ) return AIS_SOP_OneSelected;
364 else if( NS > 1 ) return AIS_SOP_SeveralSelected;
c398b00e 365 return aSelNum == 0 ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
7fd59977 366 }
367 return AIS_SOP_Error;
368}
c398b00e 369
370//=======================================================================
371//function : ShiftSelect
372//purpose :
373//=======================================================================
374AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const Standard_Integer theXPMin,
375 const Standard_Integer theYPMin,
376 const Standard_Integer theXPMax,
377 const Standard_Integer theYPMax,
378 const Handle(V3d_View)& theView,
379 const Standard_Boolean toUpdateViewer)
7fd59977 380{
679ecdee 381 myMainPM->ClearImmediateDraw();
382
c398b00e 383 if (theView->Viewer() == myCTX->CurrentViewer())
384 {
385 myMainVS->Pick (theXPMin, theYPMin, theXPMax, theYPMax, theView);
386
016e5959 387 Standard_Integer aLastSelNum = mySelection->Extent();
c398b00e 388
7fd59977 389 myMainVS->Init();
c398b00e 390 if (!myMainVS->More())
391 {
392 // Nothing is selected clear selection, but don't clear the selection
393 // as it is shift selection and previous selection matters.
394 // Return state to know if something was unselected
395 return aLastSelNum == 0 ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
396 }
7fd59977 397
c398b00e 398 if (myAutoHilight)
399 {
400 UnhilightPicked (Standard_False);
401 }
7fd59977 402
c398b00e 403 for (myMainVS->Init(); myMainVS->More(); myMainVS->Next())
404 {
405 const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked();
406 if(myFilters->IsOk (anOwner))
407 {
408 Standard_Boolean toSelect = anOwner->IsSelected() ? Standard_False : Standard_True;
016e5959 409 mySelection->Select (anOwner);
c398b00e 410 anOwner->SetSelected (toSelect);
7fd59977 411 }
412 }
c398b00e 413
414 if (myAutoHilight)
415 {
416 HilightPicked (toUpdateViewer);
417 }
7fd59977 418 }
c398b00e 419
016e5959 420 Standard_Integer aSelNum = mySelection->Extent();
c398b00e 421
422 return (aSelNum == 1) ? AIS_SOP_OneSelected
423 : (aSelNum > 1) ? AIS_SOP_SeveralSelected
424 : AIS_SOP_Error;
7fd59977 425}
426
427//==================================================
428// Function: Select
429// Purpose : Selection by polyline
430//==================================================
c398b00e 431AIS_StatusOfPick AIS_LocalContext::ShiftSelect (const TColgp_Array1OfPnt2d& thePolyline,
432 const Handle(V3d_View)& theView,
433 const Standard_Boolean toUpdateViewer)
7fd59977 434{
c398b00e 435 if (theView->Viewer() == myCTX->CurrentViewer())
436 {
437 myMainVS->Pick (thePolyline, theView);
438
016e5959 439 Standard_Integer aLastSelNum = mySelection->Extent();
7fd59977 440 myMainVS->Init();
c398b00e 441 if(!myMainVS->More())
442 {
443 // Nothing is selected clear selection, but don't clear the selection
444 // as it is shift selection and previous selection matters.
445 // Return state to know if something was unselected
446 return aLastSelNum == 0 ? AIS_SOP_NothingSelected : AIS_SOP_Removed;
7fd59977 447 }
448
c398b00e 449 if (myAutoHilight)
450 {
451 UnhilightPicked (Standard_False);
7fd59977 452 }
453
c398b00e 454 for (myMainVS->Init(); myMainVS->More(); myMainVS->Next())
455 {
456 const Handle(SelectMgr_EntityOwner)& anOwner = myMainVS->Picked();
457 if (myFilters->IsOk (anOwner))
458 {
459 Standard_Boolean toSelect = anOwner->IsSelected() ? Standard_False : Standard_True;
016e5959 460 mySelection->Select (anOwner);
c398b00e 461 anOwner->SetSelected (toSelect);
462 }
463 }
464 if (myAutoHilight)
465 {
466 HilightPicked (toUpdateViewer);
467 }
7fd59977 468 }
7fd59977 469
016e5959 470 Standard_Integer aSelNum = mySelection->Extent();
c398b00e 471
472 return (aSelNum == 1) ? AIS_SOP_OneSelected
473 : (aSelNum > 1) ? AIS_SOP_SeveralSelected
474 : AIS_SOP_Error;
7fd59977 475}
476
477//==================================================
679ecdee 478// Function: Hilight
7fd59977 479// Purpose :
480//==================================================
679ecdee 481void AIS_LocalContext::Hilight (const Handle(SelectMgr_EntityOwner)& theOwner,
482 const Handle(V3d_View)& theView)
7fd59977 483{
679ecdee 484 if (theView.IsNull())
485 {
486 return;
487 }
7fd59977 488
679ecdee 489 const Standard_Integer aHilightMode = GetHiMod (Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable()));
490 myMainPM->BeginImmediateDraw();
491 theOwner->HilightWithColor (myMainPM, myCTX->HilightColor(), aHilightMode);
c3282ec1 492 myMainPM->EndImmediateDraw (theView->Viewer());
7fd59977 493}
494
495//==================================================
679ecdee 496// Function: Unhilight
7fd59977 497// Purpose :
498//==================================================
679ecdee 499void AIS_LocalContext::Unhilight (const Handle(SelectMgr_EntityOwner)& theOwner,
500 const Handle(V3d_View)& theView)
7fd59977 501{
679ecdee 502 if (theView.IsNull())
503 {
504 return;
505 }
7fd59977 506
679ecdee 507 const Standard_Integer aHilightMode = GetHiMod (Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable()));
508 if (IsSelected (theOwner))
7fd59977 509 {
679ecdee 510 if (theOwner->IsAutoHilight())
511 {
512 theOwner->HilightWithColor (myMainPM, myCTX->SelectionColor(), aHilightMode);
513 }
514 }
515 else
516 {
517 theOwner->Unhilight (myMainPM, aHilightMode);
7fd59977 518 }
7fd59977 519}
520
521//=======================================================================
522//function : HilightPicked
523//purpose :
524//=======================================================================
525void AIS_LocalContext::HilightPicked(const Standard_Boolean updateviewer)
526{
016e5959 527 if( mySelection.IsNull() ) return;
528
e3a6386d 529 typedef NCollection_DataMap <Handle(SelectMgr_SelectableObject), NCollection_Handle<SelectMgr_SequenceOfOwner> > SelectMgr_DataMapOfObjectOwners;
7fd59977 530 SelectMgr_DataMapOfObjectOwners aMap;
e3a6386d 531
532 Handle (PrsMgr_PresentationManager3d) PM = myMainPM;
7fd59977 533
81bba717 534 // to avoid problems when there is a loop searching for selected objects...
016e5959 535 const AIS_NListTransient& Obj = mySelection->Objects();
7fd59977 536 AIS_NListTransient::Iterator anIter( Obj );
537 for(; anIter.More(); anIter.Next())
538 {
539 const Handle(Standard_Transient)& Tr = anIter.Value();
7fd59977 540 if(!Tr.IsNull()){
541 const Handle(SelectMgr_EntityOwner)& Ownr =
542 *((const Handle(SelectMgr_EntityOwner)*) &Tr);
543 Handle(AIS_InteractiveObject) IO;
544 if(Ownr->HasSelectable()){
545 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(Ownr);
546 if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
547 Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
c5f3a425 548 IO = Handle(AIS_InteractiveObject)::DownCast (SO);
7fd59977 549 }
7fd59977 550 }
7fd59977 551 Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
c5f3a425 552 Standard_Integer HM = GetHiMod(Handle(AIS_InteractiveObject)::DownCast (SO));
7fd59977 553 if ( Ownr->IsAutoHilight() )
554 Ownr->HilightWithColor(PM,myCTX->SelectionColor(),HM);
555 else if ( aMap.IsBound (SO) )
e3a6386d 556 aMap(SO)->Append ( Ownr );
7fd59977 557 else {
e3a6386d 558 NCollection_Handle<SelectMgr_SequenceOfOwner> aSeq = new SelectMgr_SequenceOfOwner;
559 aSeq->Append ( Ownr );
7fd59977 560 aMap.Bind ( SO, aSeq );
561 }
562 }
563 }
564
e3a6386d 565 for ( SelectMgr_DataMapOfObjectOwners::Iterator aMapIter(aMap);
7fd59977 566 aMapIter.More(); aMapIter.Next() )
e3a6386d 567 {
568 aMapIter.Key()->HilightSelected (myMainPM, *aMapIter.Value());
569 }
7fd59977 570
679ecdee 571 if (updateviewer)
572 {
573 myCTX->CurrentViewer()->Update();
7fd59977 574 }
575}
576
577//==================================================
578// Function:
579// Purpose :
580//==================================================
679ecdee 581void AIS_LocalContext::UnhilightPicked (const Standard_Boolean updateviewer)
7fd59977 582{
679ecdee 583 myMainPM->ClearImmediateDraw();
584
016e5959 585 if( mySelection.IsNull() ) return;
7fd59977 586 Handle (PrsMgr_PresentationManager3d) PM = myMainPM;
e3a6386d 587 NCollection_Map<Handle(SelectMgr_SelectableObject)> anObjMap;
7fd59977 588
016e5959 589 const AIS_NListTransient& Obj = mySelection->Objects();
7fd59977 590 AIS_NListTransient::Iterator anIter( Obj );
591 for(; anIter.More(); anIter.Next()){
592 const Handle(Standard_Transient)& Tr = anIter.Value();
7fd59977 593 if(!Tr.IsNull()){
594 const Handle(SelectMgr_EntityOwner)& Ownr =
595 *((const Handle(SelectMgr_EntityOwner)*) &Tr);
596 Standard_Integer HM(0);
597 if(Ownr->HasSelectable()){
7fd59977 598 Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
c5f3a425 599 Handle(AIS_InteractiveObject) IO = Handle(AIS_InteractiveObject)::DownCast (SO);
e3a6386d 600 anObjMap.Add (IO);
7fd59977 601
e3a6386d 602 HM = GetHiMod(IO);
7fd59977 603 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(Ownr);
604 if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
7fd59977 605 }
7fd59977 606 }
607 Ownr->Unhilight(PM,HM);
608 }
609 }
610
e3a6386d 611 for (NCollection_Map<Handle(SelectMgr_SelectableObject)>::Iterator anIter1 ( anObjMap );
7fd59977 612 anIter1.More(); anIter1.Next() )
e3a6386d 613 {
7fd59977 614 if ( !anIter1.Key()->IsAutoHilight() )
615 anIter1.Key()->ClearSelected();
e3a6386d 616 }
7fd59977 617
bcf50875 618 if(updateviewer)
7fd59977 619 myCTX->CurrentViewer()->Update();
7fd59977 620}
621
622//=======================================================================
623//function : IsSelected
624//purpose :
625//=======================================================================
626Standard_Boolean AIS_LocalContext::IsSelected(const Handle(AIS_InteractiveObject)& anIObj) const
627{
628 return (!FindSelectedOwnerFromIO(anIObj).IsNull());
629}
630
631//=======================================================================
632//function : IsSelected
633//purpose :
634//=======================================================================
635
c398b00e 636Standard_Boolean AIS_LocalContext::IsSelected (const Handle(SelectMgr_EntityOwner)& theOwner) const
7fd59977 637{
c398b00e 638 return !theOwner.IsNull() && theOwner->IsSelected();
7fd59977 639}
640
641//==================================================
642// Function:
643// Purpose :
644//==================================================
645void AIS_LocalContext::
646InitSelected()
647{
016e5959 648 mySelection->Init();
7fd59977 649}
650
651//==================================================
652// Function:
653// Purpose :
654//==================================================
655Standard_Boolean AIS_LocalContext::
656MoreSelected() const
657{
016e5959 658 return mySelection->More();
7fd59977 659}
660
661//==================================================
662// Function:
663// Purpose :
664//==================================================
665void AIS_LocalContext::
666NextSelected()
667{
016e5959 668 mySelection->Next();
7fd59977 669}
670
671//==================================================
672// Function:
673// Purpose :
674//==================================================
675Standard_Boolean AIS_LocalContext::
676HasShape() const
677{
016e5959 678 Handle(Standard_Transient) Tr = mySelection->Value();
7fd59977 679 if( Tr.IsNull() ) return Standard_False;
c5f3a425 680 Handle(SelectMgr_EntityOwner) EO = Handle(SelectMgr_EntityOwner)::DownCast (Tr);
7fd59977 681 Handle(StdSelect_BRepOwner) BRO = Handle(StdSelect_BRepOwner)::DownCast(EO);
682 if(BRO.IsNull()) return Standard_False;
683 Standard_Boolean hasshape = BRO->HasShape();
684 Standard_Boolean comes = BRO->ComesFromDecomposition();
685 return (hasshape&&comes);
686}
687
51023771 688//================================================================
689// Function : HasSelectedShape
690// Purpose : Checks if there is a selected shape regardless of its decomposition status
691//================================================================
692Standard_Boolean AIS_LocalContext::HasSelectedShape() const
693{
016e5959 694 if (mySelection->Extent() == 0)
51023771 695 return Standard_False;
696
016e5959 697 Handle(Standard_Transient) aCurSelection = mySelection->Value();
51023771 698 if (aCurSelection.IsNull())
699 return Standard_False;
700
701 Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (aCurSelection);
702 Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast (anOwner);
703 if (aBrepOwner.IsNull())
704 {
705 return Standard_False;
706 }
707 return aBrepOwner->HasShape();
708}
709
7fd59977 710//==================================================
711// Function:
712// Purpose :
713//==================================================
0717ddc1 714TopoDS_Shape AIS_LocalContext::SelectedShape() const
7fd59977 715{
016e5959 716 Handle(Standard_Transient) aTr = mySelection->Value();
c5f3a425 717 Handle(SelectMgr_EntityOwner) anEO = Handle(SelectMgr_EntityOwner)::DownCast (aTr);
d8110103 718 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(anEO);
719 if( aBRO.IsNull() )
7fd59977 720 {
d8110103 721 return TopoDS_Shape();
7fd59977 722 }
51023771 723
d8110103 724 return aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
7fd59977 725}
726
727//==================================================
728// Function:
729// Purpose :
730//==================================================
731Handle(AIS_InteractiveObject) AIS_LocalContext::
732SelectedInteractive() const
733{
734 Handle(AIS_InteractiveObject) IO;
016e5959 735 Handle(Standard_Transient) Tr = mySelection->Value();
7fd59977 736 if( !Tr.IsNull() ) {
c5f3a425 737 Handle(SelectMgr_EntityOwner) EO = Handle(SelectMgr_EntityOwner)::DownCast (Tr);
7fd59977 738 Handle(SelectMgr_SelectableObject) SO;
739 if(EO->HasSelectable()){
740 SO = EO->Selectable();
c5f3a425 741 IO = Handle(AIS_InteractiveObject)::DownCast (SO);
7fd59977 742 }
743 }
744 return IO;
745}
746//==================================================
747// Function:
748// Purpose :
749//==================================================
750Handle(SelectMgr_EntityOwner) AIS_LocalContext::
751SelectedOwner() const
752{
753 Handle(SelectMgr_EntityOwner) EO;
016e5959 754 Handle(Standard_Transient) Tr = mySelection->Value();
7fd59977 755 if( !Tr.IsNull() )
c5f3a425 756 EO = Handle(SelectMgr_EntityOwner)::DownCast (Tr);
7fd59977 757 return EO;
758}
759
760//==================================================
761// Function:
762// Purpose :
763//==================================================
764Standard_Boolean AIS_LocalContext::
765HasApplicative() const
766{
767 Handle(AIS_InteractiveObject) IO = SelectedInteractive();
768 if( IO.IsNull() ) return Standard_False;
769 return IO->HasOwner();
770}
771
772//==================================================
773// Function:
774// Purpose :
775//==================================================
776const Handle(Standard_Transient)& AIS_LocalContext::
777SelectedApplicative() const
778{
779 return SelectedInteractive()->GetOwner();
780}
781
782
783
784//=======================================================================
785//function : UpdateSelection
81bba717 786//purpose : should disappear...
7fd59977 787//=======================================================================
788void AIS_LocalContext::UpdateSelected(const Standard_Boolean updateviewer)
789{
790 UnhilightPicked(Standard_False);
791 HilightPicked(updateviewer);
792}
793
794//================================================================
795// Function : UpdateSelected
796// Purpose : Part of advanced selection mechanism.
797// Highlightes or clears selection presentation for the given IO
798//================================================================
799void AIS_LocalContext::UpdateSelected(const Handle(AIS_InteractiveObject)& anobj,
800 const Standard_Boolean updateviewer)
801{
802 if (anobj.IsNull() || anobj->IsAutoHilight())
803 return;
804
7fd59977 805 SelectMgr_SequenceOfOwner aSeq;
016e5959 806 for (mySelection->Init(); mySelection->More(); mySelection->Next() ){
807 Handle(SelectMgr_EntityOwner) aOwner = Handle(SelectMgr_EntityOwner)::DownCast(mySelection->Value());
7fd59977 808
809 if ( !aOwner.IsNull() && aOwner->HasSelectable() && aOwner->Selectable() == anobj )
810 aSeq.Append( aOwner );
811 }
812
813 if ( aSeq.Length() )
814 anobj->HilightSelected( myMainPM, aSeq );
815 else
816 anobj->ClearSelected();
817
818 if(updateviewer){
819 myCTX->CurrentViewer()->Update();
820 }
821}
822
823//==================================================
824// Function: ClearSelected
825// Purpose :
826//==================================================
c398b00e 827void AIS_LocalContext::ClearSelected (const Standard_Boolean updateviewer)
7fd59977 828{
829 UnhilightPicked(updateviewer);
7fd59977 830
016e5959 831 const AIS_NListTransient& Obj = mySelection->Objects();
7fd59977 832 AIS_NListTransient::Iterator anIter( Obj );
833 for(; anIter.More(); anIter.Next()){
834 const Handle(Standard_Transient)& Tr = anIter.Value();
c398b00e 835 if(!Tr.IsNull())
836 {
837 (*((const Handle(SelectMgr_EntityOwner)*)&Tr))->SetSelected (Standard_False);
7fd59977 838 }
839 }
016e5959 840 mySelection->Select();
7fd59977 841 mylastindex = 0;
842}
843
fb4b684a 844//==================================================
bc677575 845// Function: ClearOutdatedSelection
fb4b684a 846// Purpose :
847//==================================================
bc677575 848void AIS_LocalContext::ClearOutdatedSelection (const Handle(AIS_InteractiveObject)& theIO,
849 const Standard_Boolean toClearDeactivated)
fb4b684a 850{
bc677575 851 // 1. Collect selectable entities
852 SelectMgr_IndexedMapOfOwner aValidOwners;
853
854 const TColStd_ListOfInteger& aModes = SelectionModes (theIO);
855
856 TColStd_ListIteratorOfListOfInteger aModeIter (aModes);
857 for (; aModeIter.More(); aModeIter.Next())
858 {
859 int aMode = aModeIter.Value();
860 if (!theIO->HasSelection(aMode))
861 {
862 continue;
863 }
864
f751596e 865 if (toClearDeactivated && !mySM->IsActivated(theIO, aMode, myMainVS))
bc677575 866 {
867 continue;
868 }
869
870 Handle(SelectMgr_Selection) aSelection = theIO->Selection(aMode);
871 for (aSelection->Init(); aSelection->More(); aSelection->Next())
872 {
f751596e 873 Handle(SelectBasics_SensitiveEntity) anEntity = aSelection->Sensitive()->BaseSensitive();
bc677575 874 if (anEntity.IsNull())
875 {
876 continue;
877 }
878
879 Handle(SelectMgr_EntityOwner) anOwner =
880 Handle(SelectMgr_EntityOwner)::DownCast (anEntity->OwnerId());
881
882 if (anOwner.IsNull())
883 {
884 continue;
885 }
886
887 aValidOwners.Add(anOwner);
888 }
889 }
890
badc9305 891 // 2. Refresh context's detection and selection and keep only active owners.
fb4b684a 892 // Keep last detected object for lastindex initialization.
badc9305 893 Handle(SelectMgr_EntityOwner) aLastPicked;
894 if (IsValidIndex (mylastindex))
895 {
896 aLastPicked = myMapOfOwner->FindKey (mylastindex);
897 }
fb4b684a 898
bc677575 899 // Remove entity owners from detected sequences
900 for (Standard_Integer anIdx = 1; anIdx <= myDetectedSeq.Length(); ++anIdx)
fb4b684a 901 {
bc677575 902 Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (myDetectedSeq (anIdx));
903 if (anOwner.IsNull() || anOwner->Selectable() != theIO || aValidOwners.Contains (anOwner))
fb4b684a 904 {
bc677575 905 continue;
906 }
907
908 myDetectedSeq.Remove (anIdx--);
909
910 if (anIdx < myCurDetected)
911 {
912 myCurDetected--;
fb4b684a 913 }
914 }
bc677575 915 myCurDetected = Max (myCurDetected, 1);
fb4b684a 916
bc677575 917 Standard_Boolean isAISRemainsDetected = Standard_False;
fb4b684a 918
c070aa39 919 // 3. AIS_Selection : remove entity owners from AIS_Selection
bc677575 920 const Handle(V3d_Viewer)& aViewer = myCTX->CurrentViewer();
016e5959 921 AIS_NListTransient::Iterator anIter (mySelection->Objects());
fb4b684a 922 AIS_NListTransient aRemoveEntites;
923 for (; anIter.More(); anIter.Next())
924 {
925 Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anIter.Value());
926 if (anOwner.IsNull() || anOwner->Selectable() != theIO)
927 {
928 continue;
929 }
930
bc677575 931 if (aValidOwners.Contains (anOwner))
932 {
933 isAISRemainsDetected = Standard_True;
934 }
c070aa39 935 else
fb4b684a 936 {
c070aa39 937 aRemoveEntites.Append (anOwner);
938 anOwner->SetSelected (Standard_False);
939 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
940 {
941 Unhilight (anOwner, aViewer->ActiveView());
942 }
fb4b684a 943 }
944 }
945 AIS_NListTransient::Iterator anIterRemove (aRemoveEntites);
946 for (; anIterRemove.More(); anIterRemove.Next())
947 {
016e5959 948 mySelection->Select (anIterRemove.Value());
fb4b684a 949 }
950
c070aa39 951 // 4. AIS_LocalContext - myMapOfOwner : remove entity owners from myMapOfOwner
fb4b684a 952 SelectMgr_IndexedMapOfOwner anOwnersToKeep;
b586500b 953 for (Standard_Integer anIdx = 1; anIdx <= myMapOfOwner->Extent(); anIdx++)
fb4b684a 954 {
b586500b 955 Handle(SelectMgr_EntityOwner) anOwner = myMapOfOwner->FindKey (anIdx);
fb4b684a 956 if (anOwner.IsNull())
957 {
958 continue;
959 }
960
bc677575 961 if (anOwner->Selectable() != theIO || aValidOwners.Contains (anOwner))
fb4b684a 962 {
963 anOwnersToKeep.Add (anOwner);
964 }
965 else
966 {
967 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
968 {
969 Unhilight (anOwner, aViewer->ActiveView());
970 }
971 }
972 }
b586500b 973 myMapOfOwner->Clear();
974 myMapOfOwner->Assign (anOwnersToKeep);
badc9305 975
976 if (myDetectedSeq.IsEmpty() && !aLastPicked.IsNull())
63d907e7 977 {
978 myMainPM->ClearImmediateDraw();
badc9305 979 mylastindex = 0;
980 }
981 else if (!aLastPicked.IsNull())
982 {
983 // For a case when the last detected owner was unhilighted and removed as outdated we
984 // need to check if there were other detected owners with less priority. If yes then
985 // one from the remaining should be treated.
986 Standard_Integer anIndex = 1, aDetectedSeqLength = myDetectedSeq.Length();
987 for (; anIndex <= aDetectedSeqLength; anIndex++)
988 {
989 if (aLastPicked == myMainVS->Picked (myDetectedSeq.Value(anIndex)))
990 {
991 break; // detected owner was not removed
992 }
993 }
994 if (anIndex <= aDetectedSeqLength)
995 {
996 // Last detected owner was not removed, update mylastindex variable
997 mylastindex = myMapOfOwner->FindIndex (aLastPicked);
998 }
999 else
1000 {
1001 // Last detected owner was removed. First object from sequence become detected.
1002 // Pass any active view because in current implementation the highlighting is
1003 // synchronized in all view.
1004 aViewer->InitActiveViews();
1005 manageDetected (myMainVS->Picked (myDetectedSeq.First()),
1006 aViewer->ActiveView(),
1007 Standard_False);
1008 }
63d907e7 1009 }
fb4b684a 1010
badc9305 1011 // Renew iterator of ::DetectedCurrentObject()
bc677575 1012 if (!isAISRemainsDetected)
fb4b684a 1013 {
bc677575 1014 // Remove the interactive object from detected sequences
1015 for (Standard_Integer anIdx = 1; anIdx <= myAISDetectedSeq.Length(); ++anIdx)
1016 {
1017 Handle(AIS_InteractiveObject) aDetectedIO = myAISDetectedSeq.Value (anIdx);
1018 if (aDetectedIO.IsNull() || aDetectedIO != theIO)
1019 {
1020 continue;
1021 }
1022
1023 myAISDetectedSeq.Remove (anIdx--);
1024
1025 if (anIdx < myAISCurDetected)
1026 {
1027 myAISCurDetected--;
1028 }
1029 }
1030 myAISCurDetected = Max (myAISCurDetected, 1);
fb4b684a 1031 }
1032}
7fd59977 1033
1034//=======================================================================
1035//function : SetSelected
1036//purpose :
1037//=======================================================================
1038void AIS_LocalContext::SetSelected(const Handle(AIS_InteractiveObject)& anIObj,
1039 const Standard_Boolean updateviewer)
1040{
1041 if(!IsValidForSelection(anIObj)) return;
1042 UnhilightPicked(Standard_False);
1043
81bba717 1044 //1st case, owner already <anIObj> as owner
1045 // and not separated is found...
7fd59977 1046
7fd59977 1047 Handle(Standard_Transient) Tr;
1048 Handle(SelectMgr_EntityOwner) EO = FindSelectedOwnerFromIO(anIObj);
1049 if(EO.IsNull()){
81bba717 1050 //check if in selection number 0 there is an owner that can be triturated...
7fd59977 1051 if(anIObj->HasSelection(0)){
1052 const Handle(SelectMgr_Selection)& SIOBJ = anIObj->Selection(0);
1053 SIOBJ->Init();
1054 if(SIOBJ->More()){
f751596e 1055 Handle(SelectBasics_EntityOwner) BO = SIOBJ->Sensitive()->BaseSensitive()->OwnerId();
c5f3a425 1056 EO = Handle(SelectMgr_EntityOwner)::DownCast (BO);
7fd59977 1057 }
1058 }
1059 if(EO.IsNull())
543a9964 1060 EO = new SelectMgr_EntityOwner((const Handle(SelectMgr_SelectableObject)&)anIObj);
7fd59977 1061 }
1062
1063 ClearSelected(Standard_False);
c398b00e 1064
016e5959 1065 mySelection->Select(EO);
c398b00e 1066 EO->SetSelected (Standard_True);
1067
7fd59977 1068 HilightPicked(updateviewer);
1069}
1070
1071//=======================================================================
1072//function : AddOrRemoveSelected
1073//purpose :
1074//=======================================================================
1075
1076void AIS_LocalContext::AddOrRemoveSelected(const Handle(AIS_InteractiveObject)& anIObj,
1077 const Standard_Boolean updateviewer)
1078{
1079 if(!IsValidForSelection(anIObj)) return;
1080 UnhilightPicked(Standard_False);
81bba717 1081 // first check if it is selected...
7fd59977 1082 Handle(SelectMgr_EntityOwner) EO;
1083
1084 EO = FindSelectedOwnerFromIO(anIObj);
c398b00e 1085
1086 if (EO.IsNull())
1087 {
1088 if(anIObj->HasSelection(0))
1089 {
7fd59977 1090 const Handle(SelectMgr_Selection)& SIOBJ = anIObj->Selection(0);
1091 SIOBJ->Init();
f751596e 1092 if(SIOBJ->More()){
1093 Handle(SelectBasics_EntityOwner) BO = SIOBJ->Sensitive()->BaseSensitive()->OwnerId();
c5f3a425 1094 EO = Handle(SelectMgr_EntityOwner)::DownCast (BO);
7fd59977 1095 }
1096 }
c398b00e 1097 if(EO.IsNull())
1098 {
543a9964 1099 EO = new SelectMgr_EntityOwner((const Handle(SelectMgr_SelectableObject)&)anIObj);
c398b00e 1100 }
7fd59977 1101 }
016e5959 1102
1103 if (!mySelection.IsNull())
c398b00e 1104 {
016e5959 1105 AIS_SelectStatus aStatus = mySelection->Select(EO);
c398b00e 1106 EO->SetSelected (aStatus == AIS_SS_Added);
7fd59977 1107 }
c398b00e 1108
7fd59977 1109 HilightPicked(updateviewer);
1110}
1111
1112//=======================================================================
1113//function : AddOrRemoveSelected
81bba717 1114//purpose : To check...
7fd59977 1115//=======================================================================
1116void AIS_LocalContext::AddOrRemoveSelected(const TopoDS_Shape& Sh,
1117 const Standard_Boolean updateviewer)
1118{
c398b00e 1119 UnhilightPicked (Standard_False);
7fd59977 1120 Handle(SelectMgr_EntityOwner) EO = FindSelectedOwnerFromShape(Sh);
c398b00e 1121 if (!EO.IsNull())
1122 {
016e5959 1123 mySelection->Select(EO);
c398b00e 1124 EO->SetSelected (Standard_True);
7fd59977 1125 }
c398b00e 1126 HilightPicked (updateviewer);
7fd59977 1127}
1128
c398b00e 1129void AIS_LocalContext::AddOrRemoveSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
1130 const Standard_Boolean toUpdateViewer)
1131{
7fd59977 1132 if(myAutoHilight)
c398b00e 1133 {
1134 UnhilightPicked (Standard_False);
1135 }
7fd59977 1136
c398b00e 1137 Standard_Boolean toSelect = theOwner->IsSelected() ? Standard_False : Standard_True;
1138
016e5959 1139 mySelection->Select(theOwner);
c398b00e 1140 theOwner->SetSelected (toSelect);
7fd59977 1141
7fd59977 1142 if(myAutoHilight)
c398b00e 1143 {
1144 HilightPicked (toUpdateViewer);
1145 }
7fd59977 1146}
1147
1148//==================================================
679ecdee 1149// Function: manageDetected
7fd59977 1150// Purpose :
1151//==================================================
679ecdee 1152void AIS_LocalContext::manageDetected (const Handle(SelectMgr_EntityOwner)& thePickOwner,
1153 const Handle(V3d_View)& theView,
1154 const Standard_Boolean theToRedrawImmediate)
7fd59977 1155{
679ecdee 1156 if (thePickOwner.IsNull())
1157 {
63d907e7 1158 myMainPM->ClearImmediateDraw();
679ecdee 1159 if (theToRedrawImmediate)
1160 {
1161 theView->RedrawImmediate();
1162 }
1163 return;
1164 }
7fd59977 1165
679ecdee 1166 if (!myFilters->IsOk (thePickOwner))
1167 {
1168 if (mylastindex != 0)
1169 {
1170 mylastgood = mylastindex;
1171 }
1172 if (theToRedrawImmediate)
1173 {
1174 theView->RedrawImmediate();
1175 }
1176 return;
1177 }
7fd59977 1178
679ecdee 1179 //=======================================================================================================
1180 // 2 cases : a- object is in the map of picks:
1181 // 1. this is the same index as the last detected: -> Do nothing
1182 // 2. otherwise :
1183 // - if lastindex = 0 (no object was detected at the last step)
1184 // the object presentation is highlighted and lastindex = index(objet)
1185 // - othrwise :
1186 // the presentation of the object corresponding to lastindex is "unhighlighted"
1187 // it is removed if the object is not visualized but only active
1188 // then the presentation of the detected object is highlighted and lastindex = index(objet)
1189 // b- the object is not in the map of picked objects
1190 // - if lastindex != 0 (object detected at the last step) it is unhighlighted ...
1191 // if the object was decomposed, presentation is created for the detected shape and the couple
1192 // (Proprietaire,Prs)is added in the map.
1193 // otherwise the couple(proprietaire, NullPrs) is placed in the map and the interactive object
1194 // itself is highlighted.
1195 //
1196 //=======================================================================================================
1197
b586500b 1198 const Standard_Integer aNewIndex = myMapOfOwner->Contains (thePickOwner)
1199 ? myMapOfOwner->FindIndex (thePickOwner)
1200 : myMapOfOwner->Add (thePickOwner);
679ecdee 1201
1202 // For the advanced mesh selection mode the owner indices comparison
1203 // is not effective because in that case only one owner manage the
1204 // selection in current selection mode. It is necessary to check the current detected
1205 // entity and hilight it only if the detected entity is not the same as
1206 // previous detected (IsForcedHilight call)
1207 if (aNewIndex != mylastindex
1208 || thePickOwner->IsForcedHilight())
1209 {
63d907e7 1210 myMainPM->ClearImmediateDraw();
679ecdee 1211 if (mylastindex != 0
b586500b 1212 && mylastindex <= myMapOfOwner->Extent())
679ecdee 1213 {
b586500b 1214 const Handle(SelectMgr_EntityOwner)& aLastOwner = myMapOfOwner->FindKey (mylastindex);
679ecdee 1215 Unhilight (aLastOwner, theView);
1216 }
7fd59977 1217
679ecdee 1218 if (myAutoHilight)
1219 {
c398b00e 1220 if (!thePickOwner->IsSelected() || myCTX->ToHilightSelected())
679ecdee 1221 {
1222 Hilight (thePickOwner, theView);
7fd59977 1223 }
679ecdee 1224 if (theToRedrawImmediate)
1225 {
1226 theView->RedrawImmediate();
7fd59977 1227 }
7fd59977 1228 }
679ecdee 1229
1230 mylastindex = aNewIndex;
7fd59977 1231 }
1232
63d907e7 1233 if (mylastindex != 0)
679ecdee 1234 {
1235 mylastgood = mylastindex;
1236 }
7fd59977 1237}
1238
7fd59977 1239//=======================================================================
1240//function : HasDetectedShape
1241//purpose :
1242//=======================================================================
1243
1244Standard_Boolean AIS_LocalContext::HasDetectedShape() const
1245{
1246 if(mylastindex==0) return Standard_False;
1247 return IsShape(mylastindex);
1248}
1249
1250//=======================================================================
1251//function : DetectedShape
1252//purpose :
1253//=======================================================================
1254
1255const TopoDS_Shape&
1256AIS_LocalContext::DetectedShape() const
1257{
7fd59977 1258 if(mylastindex != 0)
1259 {
b586500b 1260 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(myMapOfOwner->FindKey (mylastindex));
016e5959 1261 if(BROwnr.IsNull()) return AIS_myDummyShape;
7fd59977 1262 return BROwnr->Shape();
1263 }
016e5959 1264 return AIS_myDummyShape;
7fd59977 1265}
1266
1267//=======================================================================
1268//function : DetectedInteractive
1269//purpose :
1270//=======================================================================
1271
1272Handle(AIS_InteractiveObject)
1273AIS_LocalContext::DetectedInteractive() const
1274{
1275 Handle(AIS_InteractiveObject) Iobj;
1276 if(IsValidIndex(mylastindex)){
b586500b 1277 Handle(SelectMgr_SelectableObject) SO = myMapOfOwner->FindKey(mylastindex)->Selectable();
c5f3a425 1278 Iobj = Handle(AIS_InteractiveObject)::DownCast (SO);
7fd59977 1279 }
1280 return Iobj;
1281}
1282//=======================================================================
1283//function : DetectedInteractive
1284//purpose :
1285//=======================================================================
1286Handle(SelectMgr_EntityOwner) AIS_LocalContext::DetectedOwner() const
1287{
1288 Handle(SelectMgr_EntityOwner) bid;
1289 if(!IsValidIndex(mylastindex)) return bid;
b586500b 1290 return myMapOfOwner->FindKey(mylastindex);
7fd59977 1291}
1292
1293
1294//=======================================================================
1295//function : ComesFromDecomposition
1296//purpose :
1297//=======================================================================
1298
1299Standard_Boolean AIS_LocalContext::ComesFromDecomposition(const Standard_Integer PickedIndex) const
1300{
b586500b 1301 const Handle(SelectMgr_EntityOwner)& OWN = myMapOfOwner->FindKey(PickedIndex);
7fd59977 1302 Handle(SelectMgr_SelectableObject) aSel = OWN->Selectable();
81bba717 1303 if (myActiveObjects.IsBound (aSel)) { // debug of jmi
7fd59977 1304 const Handle(AIS_LocalStatus)& Stat = myActiveObjects(aSel);
1305 return Stat->Decomposed();
1306 }
1307 return Standard_False;
1308}
1309
7fd59977 1310//=======================================================================
1311//function : DisplaySensitive
1312//purpose :
1313//=======================================================================
1314
1315void AIS_LocalContext::DisplaySensitive(const Handle(V3d_View)& aviou)
1316{
1317 myMainVS->DisplaySensitive(aviou);
1318}
1319
1320//=======================================================================
1321//function : ClearSensitive
1322//purpose :
1323//=======================================================================
1324
1325void AIS_LocalContext::ClearSensitive(const Handle(V3d_View)& aviou)
1326{
1327 myMainVS->ClearSensitive(aviou);
1328}
1329
1330
1331//=======================================================================
1332//function : IsShape
1333//purpose :
1334//=======================================================================
1335Standard_Boolean AIS_LocalContext::IsShape(const Standard_Integer Index) const
1336{
5b111128 1337 Handle(SelectMgr_EntityOwner) aEO (myMapOfOwner->FindKey(Index));
1338 if (aEO.IsNull() || ! aEO->IsKind(STANDARD_TYPE(StdSelect_BRepOwner)))
7fd59977 1339 return Standard_False;
1340 return
1341 ComesFromDecomposition(Index);
1342}
1343
1344Standard_Boolean AIS_LocalContext::IsValidForSelection(const Handle(AIS_InteractiveObject)& anIObj) const
1345{
4796758e 1346 const Handle(SelectMgr_SelectableObject)& aSelObj = anIObj; // to avoid ambiguity
7fd59977 1347 // Shape was not transfered from AIS_Shape to EntityOwner
1348 Handle(AIS_Shape) shape = Handle(AIS_Shape)::DownCast(anIObj);
1349 if( !shape.IsNull() )
4796758e 1350 return myFilters->IsOk(new StdSelect_BRepOwner(shape->Shape(), aSelObj));
1351 return myFilters->IsOk(new SelectMgr_EntityOwner(aSelObj));
7fd59977 1352}
1353
1354
1355//=======================================================================
1356//function : HilightNextDetected
679ecdee 1357//purpose :
7fd59977 1358//=======================================================================
679ecdee 1359Standard_Integer AIS_LocalContext::HilightNextDetected (const Handle(V3d_View)& theView,
1360 const Standard_Boolean theToRedrawImmediate)
7fd59977 1361{
81bba717 1362 // go to the next owner
679ecdee 1363 if (myDetectedSeq.IsEmpty())
1364 {
1365 return 0;
1366 }
7fd59977 1367
679ecdee 1368 const Standard_Integer aLen = myDetectedSeq.Length();
1369 if (++myCurDetected > aLen)
1370 {
7fd59977 1371 myCurDetected = 1;
7fd59977 1372 }
bc677575 1373 Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (myDetectedSeq (myCurDetected));
679ecdee 1374 if (anOwner.IsNull())
1375 {
1376 return 0;
1377 }
1378 manageDetected (anOwner, theView, theToRedrawImmediate);
7fd59977 1379 return myCurDetected;
1380}
1381
1382//=======================================================================
1383//function : HilightPreviousDetected
679ecdee 1384//purpose :
7fd59977 1385//=======================================================================
679ecdee 1386Standard_Integer AIS_LocalContext::HilightPreviousDetected (const Handle(V3d_View)& theView,
1387 const Standard_Boolean theToRedrawImmediate)
7fd59977 1388{
679ecdee 1389 if (myDetectedSeq.IsEmpty())
1390 {
1391 return 0;
1392 }
7fd59977 1393
4ca4bbe8 1394 const Standard_Integer aLen = myDetectedSeq.Length();
679ecdee 1395 if (--myCurDetected < 1)
1396 {
4ca4bbe8 1397 myCurDetected = aLen;
679ecdee 1398 }
bc677575 1399 Handle(SelectMgr_EntityOwner) anOwner = myMainVS->Picked (myDetectedSeq (myCurDetected));
679ecdee 1400 if (anOwner.IsNull())
1401 {
1402 return 0;
7fd59977 1403 }
1404
679ecdee 1405 manageDetected (anOwner, theView, theToRedrawImmediate);
7fd59977 1406 return myCurDetected;
1407}
1408
1409//=======================================================================
1410//function : UnhilightLastDetected
679ecdee 1411//purpose :
7fd59977 1412//=======================================================================
679ecdee 1413Standard_Boolean AIS_LocalContext::UnhilightLastDetected (const Handle(V3d_View)& theView)
7fd59977 1414{
679ecdee 1415 if (!IsValidIndex (mylastindex))
1416 {
1417 return Standard_False;
7fd59977 1418 }
7fd59977 1419
679ecdee 1420 myMainPM->BeginImmediateDraw();
b586500b 1421 const Handle(SelectMgr_EntityOwner)& anOwner = myMapOfOwner->FindKey (mylastindex);
679ecdee 1422 const Standard_Integer aHilightMode = anOwner->HasSelectable()
1423 ? GetHiMod (Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable()))
1424 : 0;
7fd59977 1425
b586500b 1426 myMapOfOwner->FindKey (mylastindex)->Unhilight (myMainPM, aHilightMode);
c3282ec1 1427 myMainPM->EndImmediateDraw (theView->Viewer());
679ecdee 1428 mylastindex = 0;
1429 return Standard_True;
7fd59977 1430}
1431
1432//=======================================================================
1433//function : FindSelectedOwnerFromIO
81bba717 1434//purpose : it is checked if one of the selected owners really presents IObj
7fd59977 1435//=======================================================================
1436Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromIO
1437 (const Handle(AIS_InteractiveObject)& anIObj) const
1438{
1439 Handle(SelectMgr_EntityOwner) EO,bid;
1440 if (anIObj.IsNull()) return EO;
1441
016e5959 1442 if(mySelection.IsNull()) {
0797d9d3 1443#ifdef OCCT_DEBUG
7fd59977 1444 cout<<"\t\tAIS_LocalCOntext::FindSelectedOwnerFromShape : Selection "
1445 <<mySelName<<" Nulle "<<endl;
1446#endif
1447 return EO;
1448 }
1449 Standard_Boolean found(Standard_False);
016e5959 1450 const AIS_NListTransient& Obj = mySelection->Objects();
7fd59977 1451 AIS_NListTransient::Iterator anIter( Obj );
1452 for(; anIter.More(); anIter.Next()){
1453 const Handle(Standard_Transient)& Tr = anIter.Value();
7fd59977 1454 if(!Tr.IsNull()){
c5f3a425 1455 EO = Handle(SelectMgr_EntityOwner)::DownCast (Tr);
7fd59977 1456 if(EO->HasSelectable()){
1457 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(EO);
1458 if(BROwnr.IsNull() || !BROwnr->ComesFromDecomposition()){
1459 if (anIObj == EO->Selectable()){
1460 found =Standard_True;
1461 break;
1462 }
1463 }
1464 }
1465 }
1466 }
1467 if(found) return EO;
1468 return bid;
1469}
1470
1471//=======================================================================
1472//function : FindSelectedOwnerFromShape
81bba717 1473//purpose : it is checked if one of the selected owners really presents IObj
7fd59977 1474//=======================================================================
1475Handle(SelectMgr_EntityOwner) AIS_LocalContext::FindSelectedOwnerFromShape(const TopoDS_Shape& sh) const
1476{
7fd59977 1477 Handle(SelectMgr_EntityOwner) EO, bid;
7fd59977 1478 if (sh.IsNull()) return EO;
1479
016e5959 1480 if(mySelection.IsNull()) {
0797d9d3 1481#ifdef OCCT_DEBUG
7fd59977 1482 cout<<"\t\tAIS_LocalCOntext::FindSelectedOwnerFromShape : Selection "<<mySelName<<" Nulle "<<endl;
1483#endif
1484 return EO;
1485 }
1486
1487 Standard_Boolean found(Standard_False);
1488
7fd59977 1489 if (!found) {
751955d4 1490 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1491 myMainVS->ActiveOwners (anActiveOwners);
1492 for (NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners); anOwnersIt.More(); anOwnersIt.Next())
1493 {
f751596e 1494 EO = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
7fd59977 1495 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(EO);
1496 if (!BROwnr.IsNull() && BROwnr->HasShape() && BROwnr->Shape() == sh) {
1497 found = Standard_True;
1498 break;
1499 }
1500 }
1501 }
7fd59977 1502
1503 if(found) return EO;
1504 return bid;
1505}
1506
7fd59977 1507//=======================================================================
1508//function : AIS_LocalContext::InitDetected
1509//purpose :
1510//=======================================================================
7fd59977 1511void AIS_LocalContext::InitDetected()
1512{
1513 myAISCurDetected = myAISDetectedSeq.Length()? 1 : 0;
1514}
1515
1516//=======================================================================
1517//function : AIS_LocalContext::MoreDetected
1518//purpose :
1519//=======================================================================
7fd59977 1520Standard_Boolean AIS_LocalContext::MoreDetected() const
1521{
1522 return (myAISCurDetected > 0 && myAISCurDetected <= myAISDetectedSeq.Length());
1523}
1524
7fd59977 1525//=======================================================================
1526//function : AIS_LocalContext::NextDetected
1527//purpose :
1528//=======================================================================
7fd59977 1529void AIS_LocalContext::NextDetected()
1530{
57ad5cbd 1531 myAISCurDetected++;
7fd59977 1532}
1533
1534//=======================================================================
1535//function : DetectedCurrentShape
1536//purpose :
1537//=======================================================================
7fd59977 1538const TopoDS_Shape& AIS_LocalContext::DetectedCurrentShape() const
1539{
57ad5cbd 1540 Handle(AIS_Shape) aCurrentShape = Handle(AIS_Shape)::DownCast (DetectedCurrentObject());
1541
1542 if (aCurrentShape.IsNull())
1543 {
016e5959 1544 return AIS_myDummyShape;
57ad5cbd 1545 }
1546
1547 return aCurrentShape->Shape();
1548}
7fd59977 1549//=======================================================================
1550//function : DetectedCurrentObject
1551//purpose :
1552//=======================================================================
7fd59977 1553Handle(AIS_InteractiveObject) AIS_LocalContext::DetectedCurrentObject() const
1554{
57ad5cbd 1555 return MoreDetected() ? myAISDetectedSeq(myAISCurDetected) : NULL;
7fd59977 1556}
325e442b 1557
1558//=======================================================================
1559//function : RestoreActivatedModes
1560//purpose :
1561//=======================================================================
1562void AIS_LocalContext::RestoreActivatedModes() const
1563{
1564 for (AIS_DataMapOfSelStat::Iterator anIter (myActiveObjects); anIter.More(); anIter.Next())
1565 {
1566 const TColStd_ListOfInteger& anActivatedModes = anIter.Value()->SelectionModes();
1567 for (TColStd_ListIteratorOfListOfInteger aModesIter (anActivatedModes); aModesIter.More(); aModesIter.Next())
1568 {
1569 mySM->Activate (anIter.Key(), aModesIter.Value(), myMainVS);
1570 }
1571 }
1572}