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