65067e0c4dc9e1f0292e20f03c0af3fb3c3edf01
[occt.git] / src / AIS2D / AIS2D_LocalContext.cxx
1 #include <AIS2D_LocalContext.ixx>
2 #include <AIS2D_LocalStatus.hxx>
3 #include <AIS2D_ProjShape.hxx>
4 #include <AIS2D_ListOfIO.hxx>
5 #include <AIS2D_DataMapIteratorOfDataMapOfLocStat.hxx>
6 #include <Prs2d_AspectLine.hxx>
7 #include <Prs2d_Drawer.hxx>
8 #include <Prs2d_Point.hxx>
9 #include <TColStd_ListIteratorOfListOfInteger.hxx>
10 #include <AIS2D_ListIteratorOfListOfIO.hxx>
11 #include <TColStd_Array1OfTransient.hxx>
12 #include <TColStd_HArray1OfTransient.hxx>
13 #include <V2d_Viewer.hxx>
14 #include <Graphic2d_TransientManager.hxx>
15 #include <Graphic2d_Line.hxx>
16 #include <Graphic2d_View.hxx>
17 #include <Graphic2d_DisplayList.hxx>
18 #include <AIS2D_HSequenceOfIO.hxx>
19
20 static Standard_Integer GetHiMod(const Handle(AIS2D_InteractiveObject)& anIO)
21 {
22   return anIO->HasHighlightMode() ? anIO->HighlightMode() 
23                                   : anIO->DefaultHighlightMode();
24 }
25
26
27 AIS2D_LocalContext::AIS2D_LocalContext()
28 {
29 }
30
31 AIS2D_LocalContext::AIS2D_LocalContext(
32                                const Handle(AIS2D_InteractiveContext)& aCtx,
33                                    const Standard_Integer /*Index*/,
34                                    const Standard_Boolean LoadDisplayed,
35                                    const Standard_Boolean AcceptStdModes,
36                                    const Standard_Boolean AcceptEraseOfTemp ):
37   myICTX( aCtx ),
38   myLastPicked( new AIS2D_InteractiveObject()),
39   myLastinMain( new AIS2D_InteractiveObject()),
40   myLastPickElInd( 0 ),
41   myLastSelElInd( 0 ),
42   myLoadDisplayed( LoadDisplayed ),
43   myAcceptStdMode( AcceptStdModes ),
44   myAcceptErase( AcceptEraseOfTemp ),
45   myAutoHighlight( Standard_True ),
46   myLastIndex( 0 ),
47   myLastGood( 0 ),
48   myCurDetected( 0 ),
49   myResetDetect( Standard_True )
50   
51 {
52   if ( myLoadDisplayed ) LoadContextObjects();
53 }
54
55
56 Standard_Boolean AIS2D_LocalContext::Display( 
57            const Handle( AIS2D_InteractiveObject)& anIObj,
58            const Standard_Integer WhichMode,
59            const Standard_Boolean /*AllowDecomp*/,
60            const Standard_Integer ActivationMode ) {
61
62   if ( myActiveObjects.IsBound(anIObj) ) {
63     const Handle( AIS2D_LocalStatus )& LStatus = myActiveObjects( anIObj );
64     
65     if ( LStatus->DisplayMode() == -1) {
66        if ( !anIObj->IsDisplayed() )
67                anIObj->Display();
68         if ( LStatus->IsTemporary() )
69            LStatus->SetDisplayMode( WhichMode );
70         } else if ( LStatus->DisplayMode() != WhichMode && LStatus -> IsTemporary() ) {
71           anIObj->Remove();
72           LStatus->SetDisplayMode( WhichMode );
73           if ( !anIObj->IsDisplayed() )
74                  anIObj->Display();
75         }
76     
77     if ( ActivationMode !=-1 ) {
78         if ( !LStatus->IsActivated( ActivationMode ) ) {
79                 LStatus->ClearSelectionModes();
80                 //mySelMgr->Load( anIObj, myMainVS );
81              LStatus->AddSelectionMode( ActivationMode );
82             //mySelMgr->Activate( anIObj, ActivationMode, myMainVS );
83                 }
84           }
85         } else {
86       Handle(AIS2D_LocalStatus) LStatus = new AIS2D_LocalStatus();
87     
88       /*if ( anIObj->AcceptDecomposition() && AllowDecomp )
89         LStatus->SetDecomposition(Standard_True);
90       else 
91         LStatus->SetDecomposition(Standard_False);
92       */
93       if( myICTX->DisplayStatus(anIObj) == AIS2D_DS_None ||
94           myICTX->DisplayStatus(anIObj) == AIS2D_DS_Temporary )
95         LStatus->SetTemporary(Standard_True);
96       else
97         LStatus->SetTemporary(Standard_False); 
98           AIS2D_TypeOfDetection HiMod ;  
99       if( !myICTX->IsDisplayed( anIObj, WhichMode) ) {
100           LStatus->SetDisplayMode(WhichMode);
101           if ( ActivationMode != -1 )
102                LStatus->AddSelectionMode(ActivationMode);
103           HiMod = anIObj->HasHighlightMode() ? 
104                           anIObj->HighlightMode() : anIObj->DefaultHighlightMode();
105           LStatus->SetHighlightMode( HiMod );
106           if( !anIObj->IsDisplayed()) {
107               DrawObject(anIObj,WhichMode);
108               anIObj->Display();         
109           }
110           /*if(ActivationMode!=-1) {
111                 mySelMgr->Load(anIObj,myMainVS);
112                 mySelMgr->Activate(anIObj,ActivationMode,myMainVS);
113                   } */
114           } else {
115         HiMod = anIObj->HasHighlightMode()? anIObj->HighlightMode(): anIObj->DefaultHighlightMode();
116         LStatus->SetHighlightMode( HiMod );
117           }
118       myActiveObjects.Bind(anIObj,LStatus);
119   }  
120   
121   return Standard_True;
122
123 }
124
125 Standard_Boolean AIS2D_LocalContext::Erase( const Handle(AIS2D_InteractiveObject)& anIObj) {
126
127   if ( !myActiveObjects.IsBound( anIObj ) ) return Standard_False;
128
129   const Handle(AIS2D_LocalStatus)& LStatus = myActiveObjects( anIObj );
130   
131   if ( LStatus->IsSubIntensityOn() ) {
132     LStatus->SubIntensityOff();
133     anIObj->Unhighlight();
134   }
135
136   Standard_Boolean stat( Standard_False );
137
138   if ( LStatus->DisplayMode() != -1 ) {
139    // if ( IsSelected( anIObj ) )
140    //   AddOrRemoveSelected( anIObj );
141     if ( anIObj->IsHighlighted() )
142       anIObj->Unhighlight();
143     anIObj->Remove();
144     LStatus->SetDisplayMode(-1);
145     stat = Standard_True;
146   }
147   if ( LStatus->IsTemporary() ) 
148     if ( anIObj->IsDisplayed() )
149       anIObj->Remove();
150
151    
152 /*  TColStd_ListIteratorOfListOfInteger It( * LStatus->SelectionModes() );
153   for ( ; It.More(); It.Next() )
154     mySelMgr->Deactivate( anIObj, It.Value(), myMainVS );
155  */
156    return stat;
157 }
158
159 Standard_Boolean AIS2D_LocalContext::Load( 
160                 const Handle(AIS2D_InteractiveObject)& anIObj,
161         const Standard_Boolean /*AllowDecomp*/,
162         const Standard_Integer ActivationMode ) {
163
164   if ( myActiveObjects.IsBound( anIObj ) ) return Standard_False;
165   Handle(AIS2D_LocalStatus) LStatus = new AIS2D_LocalStatus();
166   
167   /*if ( anIObj->AcceptDecomposition() && AllowDecomp )
168     LStatus->SetDecomposition( Standard_True );
169   else 
170     LStatus->SetDecomposition( Standard_False );
171   */
172   if ( !myICTX->IsDisplayed( anIObj ) )
173     LStatus->SetTemporary( Standard_True );
174   else
175     LStatus->SetTemporary( Standard_False );
176   LStatus->SetDisplayMode( -1 );
177   
178   //storing information....
179   if ( ActivationMode !=-1 )
180     LStatus->AddSelectionMode( ActivationMode );
181   AIS2D_TypeOfDetection HiMod = anIObj->HasHighlightMode() ? anIObj->HighlightMode() : anIObj->DefaultHighlightMode();
182   LStatus->SetHighlightMode( HiMod );
183   
184   /*mySelMgr->Load( anIObj, myMainVS );
185   if ( ActivationMode != -1 ) {
186     mySelMgr->Activate( anIObj, ActivationMode, myMainVS );
187   } */
188   myActiveObjects.Bind( anIObj, LStatus );
189   
190   return Standard_True;
191
192 }
193
194 Standard_Boolean AIS2D_LocalContext::Remove(const Handle(AIS2D_InteractiveObject)& /*aSelObj*/ ) {
195   
196   return Standard_False;
197 }
198
199 void AIS2D_LocalContext::LoadContextObjects() {
200
201   AIS2D_ListIteratorOfListOfIO It;
202   if ( myLoadDisplayed ) {
203     AIS2D_ListOfIO theLOI;
204     myICTX->DisplayedObjects( theLOI, Standard_True );
205     Handle(AIS2D_LocalStatus) LStatus;
206     for ( It.Initialize(theLOI); It.More(); It.Next() ) {
207       LStatus = new AIS2D_LocalStatus();
208 //      LStatus->SetDecomposition((It.Value()->AcceptDecomposition() && myAcceptStdMode));
209       LStatus->SetTemporary(Standard_False);
210       LStatus->SetHighlightMode( It.Value()->HasHighlightMode() ? It.Value()->HighlightMode(): It.Value()->DefaultHighlightMode() );
211       
212       myActiveObjects.Bind(It.Value(),LStatus);
213     }
214   }
215 }
216
217 void AIS2D_LocalContext::UnloadContextObjects()
218 {}
219
220
221 void AIS2D_LocalContext::Terminate() {
222
223   ClearDetected();
224   Clear();
225   myLastIndex = 0;
226   
227   if ( ! myICTX->mySeqOfSelIO->IsEmpty() ) 
228       for ( int i = 1; i <= myICTX->mySeqOfSelIO->Length(); ++i )
229           myICTX->mySeqOfSelIO->Value(i)->SetState(0);
230
231  /*       
232   AIS2D_Selection::Select();
233   AIS2D_Selection::Remove(mySelName.ToCString());*/
234   Handle(V2d_Viewer) Vwr = myICTX->CurrentViewer();
235   Handle(V2d_View) curV;
236   for ( Vwr->InitActiveViews(); Vwr->MoreActiveViews(); Vwr->NextActiveViews() ) {
237     curV = Vwr->ActiveView(); 
238   }
239
240 }
241
242 void AIS2D_LocalContext::Clear(const AIS2D_ClearMode aType) {
243   switch (aType){
244   case AIS2D_CM_All:
245     {
246       ClearObjects();
247       break;
248     }
249   case AIS2D_CM_Interactive:
250     ClearObjects();
251     break;
252   case AIS2D_CM_StandardModes:
253     {
254      while(!myListOfStdMode.IsEmpty())
255        //DeactivateStdMode(AIS2D_Shape::SelectionType(myListOfStdMode.Last()));
256       break;
257     }
258   case AIS2D_CM_TemporaryShapePrs:
259     ClearDetected();
260   }
261   //UpdateSort();
262 }
263
264 Standard_Boolean AIS2D_LocalContext::IsSelected( const Handle(AIS2D_InteractiveObject)& anIObj) const {
265   
266    if ( ! myICTX->mySeqOfSelIO->IsEmpty() ) 
267        for ( int i = 1; i <= myICTX->mySeqOfSelIO->Length(); ++i )
268            if ( myICTX->mySeqOfSelIO->Value(i) == anIObj )
269                return Standard_True;
270     return Standard_False;
271 }
272
273 Handle(AIS2D_InteractiveObject) AIS2D_LocalContext::SelectedIO() const {
274  return myLastPicked;
275 }
276
277
278 AIS2D_StatusOfDetection AIS2D_LocalContext::MoveTo(
279                                             const Standard_Integer XPix,
280                                             const Standard_Integer YPix,
281                                             const Handle(V2d_View)& aView ) {
282
283   if ( aView->Viewer() == myICTX->CurrentViewer() ) {
284     Standard_Integer Dprecision = myICTX->DetectPrecision();
285     Standard_Boolean UpdVwr = Standard_False;
286     Handle(V2d_Viewer) theViewer = aView->Viewer();
287  
288     AIS2D_StatusOfDetection theStat( AIS2D_SOD_Nothing );
289
290     Handle(Graphic2d_Primitive) thePrim;                                           
291     Handle(Graphic2d_TransientManager) theDrawer = 
292     Handle(Graphic2d_TransientManager)::DownCast( theViewer->View()->Drawer() );
293  
294     Handle(Graphic2d_DisplayList) thePickList = aView->Pick( XPix, YPix, Dprecision );
295  
296     if ( thePickList->Length() > 0 ) {
297    
298      if ( thePickList->Length() == 1 ) {
299       Handle(AIS2D_InteractiveObject) theIO = Handle(AIS2D_InteractiveObject)::DownCast(thePickList->Value(1));
300       if ( theIO == myLastPicked ) 
301        switch ( theIO->HighlightMode() ) {
302        default:
303        case AIS2D_TOD_OBJECT: 
304           return AIS2D_SOD_OnlyOneDetected;
305           break;
306        case AIS2D_TOD_PRIMITIVE:
307           if ( myLastPicked->Primitive(myLastPicked->PickedIndex()) == myLastPickPrim )
308             return AIS2D_SOD_OnlyOneDetected;
309           break;
310        case AIS2D_TOD_ELEMENT:
311           if ( ( myLastPicked->Primitive(myLastPicked->PickedIndex()) == myLastPickPrim ) && 
312              ( myLastPicked->Primitive(myLastPicked->PickedIndex())->PickedIndex() == myLastPickElInd ) )
313             return AIS2D_SOD_OnlyOneDetected;
314           break;
315        case AIS2D_TOD_VERTEX:
316           if ( ( myLastPicked->Primitive(myLastPicked->PickedIndex()) == myLastPickPrim ) && 
317              ( myLastPicked->Primitive(myLastPicked->PickedIndex())->PickedIndex() == myLastPickElInd ) )
318             return AIS2D_SOD_OnlyOneDetected;
319           break; 
320              
321        }
322    }
323    if ( !myLastPicked.IsNull() )
324     if ( !myLastPicked->State() ) {
325 #ifdef DEB
326         AIS2D_TypeOfDetection HiMod = myLastPicked->HasHighlightMode() ? 
327              myLastPicked->HighlightMode() : myLastPicked->DefaultHighlightMode();
328 #endif
329         myLastPicked->Unhighlight();
330             UpdVwr = Standard_True;
331     } // end if the last picked object isn't selected one
332  
333    if ( myResetDetect ) {
334
335      theDrawer->SetOverride(Standard_True);
336      theDrawer->SetOverrideColor( theViewer->InitializeColor(myICTX->HighlightColor()) );
337      theDrawer->BeginDraw( aView->Driver() );
338
339      for ( int i = 1 ; i <= thePickList->Length(); i++ ) {
340
341        myLastPicked = Handle(AIS2D_InteractiveObject)::DownCast( thePickList->Value(i) );
342        myLastinMain = myLastPicked;
343    
344      
345        if ( myLastPicked->HasHighlightMode() ) {
346
347         myLastPicked->Highlight(theViewer->InitializeColor(myICTX->HighlightColor()));
348         switch( myLastPicked->HighlightMode() ) {
349             default:
350         case AIS2D_TOD_OBJECT:
351          if ( myLastPicked->State() != 1 ) {
352            theDrawer->Draw( myLastPicked );  
353          } else {
354            theStat = AIS2D_SOD_Selected;
355          }
356          break;
357         case AIS2D_TOD_PRIMITIVE:
358          myLastPickPrim = myLastPicked->Primitive(myLastPicked->PickedIndex());
359          if ( !myLastPickPrim->IsHighlighted() ) {
360             theDrawer->Draw(myLastPickPrim);
361          } else {
362             theStat = AIS2D_SOD_Selected;
363          }
364              break;
365         case AIS2D_TOD_ELEMENT: {
366          Standard_Integer pInd = myLastPicked->PickedIndex();
367              myLastPickPrim = myLastPicked->Primitive(pInd);
368          if ( myLastPickPrim != myLastSelPrim ||
369             ( myLastPickPrim == myLastSelPrim && 
370               myLastPickPrim->PickedIndex() != myLastSelElInd ) ||
371             ( !myLastPickPrim->PickedIndex() && 
372               !myLastPickPrim->IsHighlighted() ) ) {
373             myLastPickElInd = myLastPickPrim->PickedIndex();
374             theDrawer->DrawElement(myLastPickPrim,myLastPickElInd);    
375          } else {
376             theStat = AIS2D_SOD_Selected;
377          }
378          break;
379         }
380         case AIS2D_TOD_VERTEX: {
381              myLastPickPrim = myLastPicked->Primitive(myLastPicked->PickedIndex());
382 #ifdef DEB
383          Standard_Integer pInd = myLastPicked->PickedIndex();
384 #endif
385          if ( myLastPickPrim != myLastSelPrim ||
386             ( myLastPickPrim == myLastSelPrim && 
387               myLastPickPrim->PickedIndex() != myLastSelElInd ) ||
388             ( !myLastPickPrim->PickedIndex() && 
389               !myLastPickPrim->IsHighlighted() ) ) {
390             myLastPickElInd = myLastPickPrim->PickedIndex();
391             theDrawer->DrawVertex(myLastPickPrim,-myLastPickElInd);    
392          } else {
393              theStat = AIS2D_SOD_Selected;
394          }
395              break;
396         }
397         } //end switch
398        } // end if lastPicked has highlight mode
399    
400      } // end for
401
402           theDrawer->EndDraw();
403           theDrawer->SetOverride(Standard_False);
404               myResetDetect= Standard_False;
405
406    } // end if myResetDetect is true
407
408  } else {
409
410     theStat = AIS2D_SOD_Nothing;
411     theDrawer->RestoreArea( aView->Driver() );
412     myResetDetect= Standard_True;
413     if ( !myLastPicked.IsNull() ) {
414      if ( myLastPicked->HighlightMode() == AIS2D_TOD_ELEMENT || 
415           myLastPicked->HighlightMode() == AIS2D_TOD_VERTEX ) {
416        myLastPicked->Highlight(theViewer->InitializeColor( myICTX->SelectionColor() ));
417
418        myLastPicked->Unhighlight();
419        UpdVwr = Standard_True;
420      } else if ( !myLastPicked->State() ) {
421        myLastPicked->Unhighlight();
422        UpdVwr = Standard_True;
423      }
424     }
425     
426     myLastinMain.Nullify();
427     myLastPickPrim.Nullify();
428     myLastPickElInd = 0;
429
430  }  // end if PickList isn't empty
431  
432  //if ( !myLastPicked.IsNull() )  myLastPicked->Unhighlight();
433     
434  if ( UpdVwr ) theViewer->Update();
435
436  myLastPicked.Nullify();
437  //myLastMoveView = aView;
438  return theStat;
439  
440  } else 
441    return AIS2D_SOD_Error;
442
443 }
444
445 AIS2D_StatusOfPick AIS2D_LocalContext::Select( const Standard_Boolean UpdateVwr ) {
446   
447   if ( myAutoHighlight )
448     UnhighlightPicked( Standard_False );
449   
450   Standard_Integer DI = DetectedIndex();
451 //  AIS2D_Selection::SetCurrentSelection( mySelName.ToCString() );
452 //  Standard_Integer NbSel = AIS2D_Selection::Extent();
453    Standard_Integer NbSel = myICTX->mySeqOfSelIO->Length();  
454   if ( DI <= 0 ) {
455     ClearSelected( UpdateVwr );
456     return NbSel == 0 ? AIS2D_SOP_NothingSelected : AIS2D_SOP_Removed;
457   }
458  
459   if ( myAutoHighlight )
460     HighlightPicked( UpdateVwr );
461   //return ( AIS2D_Selection::Extent() == 1) ? AIS2D_SOP_OneSelected : AIS2D_SOP_SeveralSelected;
462   return ( myICTX->mySeqOfSelIO->Length() == 1) ? AIS2D_SOP_OneSelected : AIS2D_SOP_SeveralSelected;
463 }
464
465 AIS2D_StatusOfPick AIS2D_LocalContext::ShiftSelect( const Standard_Boolean /*UpdateVwr*/ ) {
466   
467 /*  Standard_Integer I = DetectedIndex();
468   if ( I > 0 ) {
469     UnhighlightPicked(Standard_False);
470     
471     AIS2D_Selection::SetCurrentSelection(mySelName.ToCString());
472     const Handle(SelectMgr_EntityOwner)& EntOwr  = myMapOfOwner(I);
473     Standard_Integer mod = EntOwr->State() == 0 ? 1 : 0;
474     EntOwr->State(mod);
475     
476     AIS2D_Selection::Select(EntOwr);
477     
478     if ( myAutoHighlight )
479       HighlightPicked(UpdateVwr);  
480   }*/
481   return AIS2D_SOP_Error;
482 }
483
484 void AIS2D_LocalContext::SetDisplayPriority(
485                                         const Handle(AIS2D_InteractiveObject)& anIObj,
486                                         const Standard_Integer /*Prior*/ ) {
487   
488   if ( !myActiveObjects.IsBound(anIObj) ) return;
489   const Handle(AIS2D_LocalStatus)& LStatus = myActiveObjects(anIObj);
490   if ( LStatus->DisplayMode() == -1) return;
491   //myMainPM->SetDisplayPriority(anIObj, LStatus->DisplayMode(),Prior);
492   //if ( LStatus->IsSubIntensityOn())
493   //  myMainPM->SetDisplayPriority(anIObj, LStatus->HighlightMode(), Prior);
494   
495 }
496
497 Standard_Integer AIS2D_LocalContext::DisplayedObjects(TColStd_MapOfTransient& theMap) const {
498
499   Standard_Integer NbDisp(0);
500   for ( AIS2D_DataMapIteratorOfDataMapOfLocStat theIt(myActiveObjects); theIt.More(); theIt.Next() ) {
501     const Handle(AIS2D_InteractiveObject)& SObj = theIt.Key();
502     if ( !theMap.Contains(SObj) )
503       if ( theIt.Value()->DisplayMode() != -1 ) {
504          theMap.Add(SObj);
505              NbDisp++;
506       }
507   }
508   return NbDisp;
509 }
510
511 Standard_Boolean AIS2D_LocalContext::IsDisplayed(const Handle(AIS2D_InteractiveObject)& anIObj) const {
512
513   if ( !myActiveObjects.IsBound(anIObj) ) 
514       return Standard_False;
515   return ( myActiveObjects(anIObj)->DisplayMode() != -1 );
516 }
517
518 Standard_Boolean AIS2D_LocalContext::IsDisplayed(const Handle(AIS2D_InteractiveObject)& anIObj,
519                                                   const Standard_Integer aMode) const {
520
521   if (!myActiveObjects.IsBound(anIObj)) return Standard_False;
522   return (myActiveObjects(anIObj)->DisplayMode()==aMode);
523 }
524
525 void AIS2D_LocalContext::Unhighlight(const Handle(AIS2D_InteractiveObject)& /*anIObj*/)
526 {
527 }
528
529 Standard_Boolean AIS2D_LocalContext::IsHighlight(const Handle(AIS2D_InteractiveObject)& anIObj) const {
530
531   if ( !myActiveObjects.IsBound(anIObj) ) return Standard_False;
532   return myActiveObjects( anIObj )->IsSubIntensityOn();
533 }
534
535 Standard_Boolean AIS2D_LocalContext::IsHighlight(const Handle(AIS2D_InteractiveObject)& anIObj,
536                                                Standard_Boolean& WithColor,
537                                                Quantity_NameOfColor& HiCol) const 
538 {
539   if(!myActiveObjects.IsBound(anIObj)) return Standard_False;
540   if( myActiveObjects(anIObj)->IsSubIntensityOn()){
541     HiCol = myActiveObjects(anIObj)->HighlightColor();
542     if(HiCol==Quantity_NOC_WHITE)
543       WithColor = Standard_True;
544     else
545       WithColor = Standard_False;
546     return Standard_True;
547   }
548   return Standard_False;
549 }
550
551 void AIS2D_LocalContext::SubIntensityOn(const Handle(AIS2D_InteractiveObject)& anIObj) {
552   
553   if ( !myActiveObjects.IsBound(anIObj) ) return;
554   
555   const Handle(AIS2D_LocalStatus)& LStatus = myActiveObjects(anIObj);
556   if ( LStatus->IsTemporary() ) {
557           Quantity_NameOfColor aCol = myICTX->SubIntensityColor();
558       myICTX->HighlightWithColor(anIObj, aCol);
559   }
560   LStatus->SubIntensityOn();
561 }
562
563 void AIS2D_LocalContext::SubIntensityOff(const Handle(AIS2D_InteractiveObject)& anIObj) {
564   
565   if ( !myActiveObjects.IsBound(anIObj) ) return;
566   
567   const Handle(AIS2D_LocalStatus)& LStatus = myActiveObjects(anIObj);
568
569   if ( LStatus->IsTemporary()) 
570     myICTX->Unhighlight(anIObj);
571   LStatus->SubIntensityOff();
572 }
573
574 Standard_Boolean AIS2D_LocalContext::IsIn( const Handle(AIS2D_InteractiveObject)& anIObj) const {
575   return myActiveObjects.IsBound(anIObj);
576 }
577
578 Standard_Boolean AIS2D_LocalContext::UnhighLastDetect(const Handle(V2d_View)& /*aView*/) {  
579         
580 //  if ( !IsValidIndex(myLastIndex) ) return Standard_False;
581 /*  myMainPM->BeginDraw();
582   const Handle(SelectMgr_EntityOwner)& Ownr = myMapOfOwner(myLastIndex);
583   Standard_Integer HM(0);
584   if ( Ownr->HasSelectable() ) {
585     Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
586     HM = GetHiMod(* ( (Handle(AIS2D_InteractiveObject ) * ) & SO ) );
587   }
588   myMapOfOwner( myLastIndex )->Unhilight( myMainPM, HM );
589   myMainPM->EndDraw( aView );
590  */
591   myLastIndex =0;
592   return Standard_True;
593 }
594
595 void AIS2D_LocalContext::DrawObject( const Handle(AIS2D_InteractiveObject)& anIObj, const Standard_Integer /*aDispMode*/ ) {
596     
597  if ( anIObj->IsKind(STANDARD_TYPE(AIS2D_ProjShape) ) ) {
598
599        Handle(AIS2D_ProjShape) thePS = Handle(AIS2D_ProjShape)::DownCast(anIObj);
600        Handle(Graphic2d_Line) theLines = thePS->GetPrimitives();
601        Handle(Prs2d_AspectLine) theAspLine;
602
603        if ( !anIObj->HasAspect(theLines) ) {
604          Handle(Prs2d_AspectRoot) theAspRoot = anIObj->Attributes()->FindAspect(Prs2d_AN_LINE);
605          theAspLine = Handle(Prs2d_AspectLine)::DownCast(theAspRoot);
606          anIObj->SetAspect( theAspLine, theLines );
607                  
608        } else {
609          theAspLine = Handle(Prs2d_AspectLine)::DownCast( anIObj->GetAspect(theLines) );
610        }      
611      
612        Quantity_Color     aColor;
613        Aspect_TypeOfLine  aType;
614        Aspect_WidthOfLine aWidth;
615   
616        theAspLine->ValuesOfLine( aColor, aType, aWidth );
617   
618        Standard_Integer colorInd = theAspLine->ColorIndex();
619        Standard_Integer typeInd = theAspLine->TypeIndex();
620        Standard_Integer widthInd = theAspLine->WidthIndex();
621      
622        if ( !colorInd ) {
623          colorInd = myICTX->InitializeColor( aColor );
624          theLines->SetColorIndex( colorInd );
625        }
626
627        if ( !typeInd ) {
628          typeInd = myICTX->InitializeStyle( aType );
629          theLines->SetTypeIndex( typeInd );
630        }
631
632        if ( !widthInd ) {
633              widthInd = myICTX->InitializeWidth( aWidth );
634          theLines->SetWidthIndex( widthInd );
635        }
636
637        if ( thePS->IsHLMode() ) {
638          Handle(Graphic2d_Line) theHLines = thePS->GetHLPrimitives();
639          theHLines->SetColorIndex( colorInd );
640          theHLines->SetWidthIndex( widthInd );
641          Standard_Integer theDashInd = myICTX->InitializeStyle( Aspect_TOL_DASH );
642          theHLines->SetTypeIndex( theDashInd );          
643        }
644
645     }  else {
646
647       Handle(Graphic2d_Primitive) thePrim;
648
649       for ( Standard_Integer i = 1; i <= anIObj->Length(); i++ ) {
650        thePrim = anIObj->Primitive( i );
651        if ( thePrim->IsKind(STANDARD_TYPE(Graphic2d_Line) ) ) {
652
653          Handle(Graphic2d_Line) theLine = Handle(Graphic2d_Line)::DownCast(thePrim);
654          Handle(Prs2d_AspectLine) theAspLine;
655
656          if ( !anIObj->HasAspect(theLine) ) {
657            Handle(Prs2d_AspectRoot) theAspRoot = anIObj->Attributes()->FindAspect(Prs2d_AN_LINE);
658            theAspLine = Handle(Prs2d_AspectLine)::DownCast(theAspRoot);
659            anIObj->SetAspect( theAspLine, theLine );
660                  
661          } else {
662            theAspLine = Handle(Prs2d_AspectLine)::DownCast( anIObj->GetAspect(theLine) );
663          }      
664      
665          Quantity_Color     aColor;
666          Aspect_TypeOfLine  aType;
667          Aspect_WidthOfLine aWidth;
668   
669          Quantity_Color aIntColor;
670          Graphic2d_TypeOfPolygonFilling aTypeFill;
671          Standard_Integer aTile;
672          Standard_Boolean aDrawEdge;
673   
674          theAspLine->ValuesOfLine( aColor, aType, aWidth );
675          theAspLine->ValuesOfPoly( aIntColor, aTypeFill, aTile, aDrawEdge );
676      
677          Standard_Integer colorInd = theAspLine->ColorIndex();
678          Standard_Integer typeInd = theAspLine->TypeIndex();
679          Standard_Integer widthInd = theAspLine->WidthIndex();
680          Standard_Integer colorIntInd = theAspLine->InterColorIndex();
681
682          if ( !colorInd ) {
683            colorInd = myICTX->InitializeColor( aColor );
684            theLine->SetColorIndex( colorInd );
685          }
686
687          if ( !typeInd ) {
688            typeInd = myICTX->InitializeStyle( aType );
689            theLine->SetTypeIndex( typeInd );
690          }
691
692          if ( !widthInd ) {
693                widthInd = myICTX->InitializeWidth( aWidth );
694            theLine->SetWidthIndex( widthInd );
695          }
696
697          if ( !colorIntInd ) {
698            colorIntInd = myICTX->InitializeColor( aIntColor );
699            theLine->SetInteriorColorIndex( colorIntInd );
700          }
701    
702          theLine->SetTypeOfPolygonFilling( aTypeFill );
703          theLine->SetInteriorPattern( aTile );
704          theLine->SetDrawEdge( aDrawEdge );
705
706          if ( theLine->IsKind(STANDARD_TYPE(Prs2d_Point) ) ) {
707
708            Handle(Prs2d_Point) thePnt = Handle(Prs2d_Point)::DownCast(theLine);
709            thePnt->SetIndex( myICTX->InitializeMarker( thePnt->Marker() ) );
710          }    
711
712     } // end if the kind of primitive is Graphic2d_Line
713
714   } // end for exploration of the interactive object 
715  
716  }  // end if IO is AIS2D_ProjShape
717     
718 }
719
720 void AIS2D_LocalContext::ClearObjects() {
721
722   AIS2D_DataMapIteratorOfDataMapOfLocStat It( myActiveObjects );
723   for ( ; It.More(); It.Next() ) {
724     Handle(AIS2D_InteractiveObject) SObj = Handle(AIS2D_InteractiveObject)::DownCast( It.Key() );
725       
726       const Handle(AIS2D_LocalStatus) LStatus = It.Value();
727 //      TColStd_ListIteratorOfListOfInteger ItL;
728       
729       AIS2D_DisplayStatus theDS = myICTX->DisplayStatus( SObj );
730       
731       if ( theDS != AIS2D_DS_Displayed){
732         //if ( myMainPM->IsDisplayed( SObj, LStatus->DisplayMode() ) ) {
733        if ( myICTX->IsDisplayed(SObj) ) {
734              if ( LStatus->IsSubIntensityOn() && SObj->IsHighlighted() )
735                SObj->Unhighlight();
736              SObj->Remove();
737                 }
738         
739             if ( LStatus->IsTemporary() )
740              SObj->Remove();
741       } else {
742             if ( LStatus->IsSubIntensityOn() ) {
743               myICTX->SubIntensityOff(Handle(AIS2D_InteractiveObject)::DownCast(SObj) ); }
744           Standard_Integer DiMo = SObj->HasDisplayMode() ? SObj->DisplayMode() : myICTX->DisplayMode();
745           if ( LStatus->DisplayMode() !=-1 && LStatus->DisplayMode() != DiMo)
746                  SObj->Remove();
747           }
748       
749   }
750   ClearSelected();
751   myActiveObjects.Clear();
752   
753 }
754
755
756 void AIS2D_LocalContext::ClearDetected() {
757
758  /*for ( Standard_Integer i = 1; i <= myMapOfOwner.Extent(); i++ ) {
759     if ( !myMapOfOwner(i).IsNull() ) {
760       if ( myMapOfOwner(i)->IsHilighted(myMainPM) )
761         myMapOfOwner(i)->Unhilight(myMainPM);
762       else if ( myMapOfOwner(i)->IsHilighted( myICTX->CollectorPrsMgr() ) )
763                   myMapOfOwner(i)->Unhilight( myICTX->CollectorPrsMgr() );
764            else {
765                   const Handle(SelectMgr_SelectableObject)& SO = 
766                            myMapOfOwner.FindKey(i)->Selectable();
767                   if ( myActiveObjects.IsBound(SO) ) {
768                     const Handle(AIS2D_LocalStatus)& LStatus = myActiveObjects(SO);
769                 if ( LStatus->IsTemporary() && 
770                                          LStatus->DisplayMode() == -1 && 
771                          LStatus->SelectionModes()->IsEmpty() ) 
772            
773                                    myMapOfOwner(i)->Clear(myMainPM);
774                                 
775                           }
776                    }
777         }
778  } */
779 }
780
781 void AIS2D_LocalContext::ClearSelected(const Standard_Boolean UpdateVwr) {
782   
783   UnhighlightPicked(UpdateVwr);
784  // AIS2D_Selection::SetCurrentSelection(mySelName.ToCString());
785
786  // Handle(AIS2D_Selection) Sel = AIS2D_Selection::CurrentSelection();
787   /*Handle(Standard_Transient) Tr;
788   Handle(SelectMgr_EntityOwner) EO;
789   const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
790   for ( Standard_Integer i = Obj.Lower(); i <= Sel->NbStored(); i++ ) {
791     Tr = Obj(i);
792     if ( !Tr.IsNull() )
793       (*((Handle(SelectMgr_EntityOwner)*)&Tr))->State(0);
794   } */
795   //AIS2D_Selection::Select();
796   myLastIndex = 0;
797 }
798
799 void AIS2D_LocalContext::HighlightPicked(const Standard_Boolean /*UpdateVwr*/) {
800   
801   /*Standard_Boolean updMain(Standard_False),updColl(Standard_False);
802   Quantity_NameOfColor SelCol = myICTX->SelectionColor();
803   
804   Handle(AIS2D_Selection) Sel = AIS2D_Selection::Selection(mySelName.ToCString());
805   Handle(Standard_Transient) Tr;
806   Handle(SelectMgr_EntityOwner) Ownr;
807   Handle(PrsMgr_PresentationManager2d) PM = myMainPM;
808   
809   const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
810   for ( Standard_Integer i = Obj.Lower(); i <= Sel->NbStored(); i++ ) {
811     Tr = Obj(i);
812     if ( !Tr.IsNull() ) {
813       Ownr = * ( (Handle(SelectMgr_EntityOwner) * ) &Tr );
814       Handle(AIS2D_InteractiveObject) IO;
815       if ( Ownr->HasSelectable() ) 
816             if ( !Ownr->ComesFromDecomposition() ) {
817           Handle(SelectMgr_SelectableObject) SO  = Ownr->Selectable();
818               Handle(AIS2D_InteractiveObject) IO = 
819                            * ( (Handle(AIS2D_InteractiveObject) * ) &SO );
820               if ( myICTX->IsInCollector(IO) ) {
821                 PM = myICTX->CollectorPrsMgr();
822                 updColl = Standard_True;
823                   } else
824                 updMain = Standard_True;
825                 } else
826                updMain = Standard_True;
827           else
828         updMain = Standard_True;
829           // endif HasSelectable
830       Handle(SelectMgr_SelectableObject) SO = Ownr->Selectable();
831       Standard_Integer HM = GetHiMod( * ( (Handle(AIS2D_InteractiveObject ) * ) &SO ) );
832  //     Ownr->HilightWithColor( PM, myICTX->SelectionColor(), HM );
833           if ( Ownr->HasSelectable() ) {
834               Quantity_NameOfColor aCol = myICTX->SelectionColor();
835                   Standard_Integer indCol = myICTX->CurrentViewer()->InitializeColor( aCol );
836           PM->SetHighlightColor( indCol );
837                   PM->ColorHighlight( SO, indCol, HM );
838           }
839                   
840         } //endif 
841   }     //end for
842   if (UpdateVwr) {
843     if ( updMain ) myICTX->CurrentViewer()->Update();
844     if ( updColl ) myICTX->Collector()->Update();
845   } */
846 }
847
848 void AIS2D_LocalContext::UnhighlightPicked( const Standard_Boolean /*UpdateVwr*/ ) {
849
850   /*Standard_Boolean updMain(Standard_False), updColl(Standard_False);
851   Quantity_NameOfColor SelCol = myICTX->SelectionColor();
852   
853   Handle(AIS2D_Selection) Sel = AIS2D_Selection::Selection(mySelName.ToCString());
854   Handle(Standard_Transient) Tr;
855   Handle(SelectMgr_EntityOwner) Ownr;
856   Handle(PrsMgr_PresentationManager2d) PM = myMainPM;
857   
858   const TColStd_Array1OfTransient& Obj = Sel->Objects()->Array1();
859   for ( Standard_Integer i = Obj.Lower(); i <= Sel->NbStored(); i++ ) {
860     Tr = Obj(i);
861     if ( !Tr.IsNull() ) {
862       Ownr = * ( (Handle(SelectMgr_EntityOwner) * ) &Tr );
863       Standard_Integer HM(0);
864       if ( Ownr->HasSelectable() ) 
865                  if ( !Ownr->ComesFromDecomposition() ) {
866                Handle(SelectMgr_SelectableObject) SO  = Ownr->Selectable();
867            Handle(AIS2D_InteractiveObject) IO = 
868                            * ( (Handle(AIS2D_InteractiveObject) * ) &SO );
869                HM = GetHiMod(IO);
870                if ( myICTX->IsInCollector(IO) ) {
871                  PM = myICTX->CollectorPrsMgr();
872                  updColl = Standard_True;
873                    } else
874                  updMain = Standard_True;
875                  } else
876                updMain = Standard_True;
877          // endif HasSelectable
878       Ownr->Unhilight( PM, HM );
879     }  //end if
880   }     //end for
881   
882   if ( UpdateVwr ) {
883     if ( updMain ) myICTX->CurrentViewer()->Update();
884     if ( updColl ) myICTX->Collector()->Update();
885   } */
886 }
887
888 Standard_Boolean AIS2D_LocalContext::IsValidForSelection(const Handle(AIS2D_InteractiveObject)& /*anIObj*/) const {
889   
890   // return myFilter->IsOk(new SelectMgr_EntityOwner(anIObj));
891     return Standard_False;
892 }
893
894 void AIS2D_LocalContext::UpdateSelected(const Standard_Boolean UpdateVwr) {
895   
896   UnhighlightPicked(Standard_False);
897   HighlightPicked(UpdateVwr);
898 }
899
900 void AIS2D_LocalContext::SetSelected( const Handle(AIS2D_InteractiveObject)& /*anIObj*/,
901                                    const Standard_Boolean /*UpdateVwr*/ ) {
902
903   /*if ( !IsValidForSelection(anIObj) ) return;
904   UnhighlightPicked(Standard_False);
905   
906   Handle(AIS2D_Selection) sel = AIS2D_Selection::Selection(mySelName.ToCString());
907   Standard_Boolean found(Standard_False);
908   Handle(Standard_Transient) Tr;
909   Handle(SelectMgr_EntityOwner) EntOwr = FindSelectedOwnerFromIO(anIObj);
910   if ( EntOwr.IsNull() ) {
911      if ( anIObj->HasSelection(0) ) {
912       const Handle(SelectMgr_Selection)& SIObj = anIObj->Selection(0);
913       SIObj->Init();
914       if ( SIObj->More() ) {
915         Handle(SelectBasics_EntityOwner) BO = SIObj->Sensitive()->OwnerId();
916         EntOwr = *( (Handle(SelectMgr_EntityOwner) * ) & BO );
917       }
918     }
919     if ( EntOwr.IsNull() ) 
920       EntOwr = new SelectMgr_EntityOwner(anIObj);
921   }
922   
923   ClearSelected(Standard_False);
924   EntOwr->State(1);
925   AIS2D_Selection::Select(EntOwr);
926   HighlightPicked(UpdateVwr);
927   */
928 }
929
930 void AIS2D_LocalContext::AddOrRemoveSelected(
931                                         const Handle(AIS2D_InteractiveObject)& /*anIObj*/,
932                                         const Standard_Boolean /*UpdateVwr*/) {
933
934   /*if ( !IsValidForSelection(anIObj) ) return;
935   UnhighlightPicked(Standard_False);
936   Handle(SelectMgr_EntityOwner) EntOwr;
937
938   EntOwr = FindSelectedOwnerFromIO(anIObj);
939   if ( !EntOwr.IsNull() )
940     EntOwr->State(0);
941   else {
942     if ( anIObj->HasSelection(0) ) {
943       const Handle(SelectMgr_Selection)& SIObj = anIObj->Selection(0);
944       SIObj->Init();
945       if ( SIObj->More() ) {
946             Handle(SelectBasics_EntityOwner) BO = SIObj->Sensitive()->OwnerId();
947                 EntOwr = * ( ( Handle(SelectMgr_EntityOwner)* )& BO );          
948           }
949     }
950     if ( EntOwr.IsNull() ) 
951       EntOwr = new SelectMgr_EntityOwner(anIObj);
952     EntOwr->State(1);
953   }
954   
955   const Handle(AIS2D_Selection)& aSel = AIS2D_Selection::Selection(mySelName.ToCString() );
956   if ( !aSel.IsNull() )
957     aSel->Select(EntOwr);
958   HighlightPicked(UpdateVwr);
959   */
960 }
961
962 Standard_Integer AIS2D_LocalContext::DetectedIndex() {
963   return myLastIndex;
964 }
965
966 const Handle(AIS2D_LocalStatus)& AIS2D_LocalContext::Status(const Handle(AIS2D_InteractiveObject)& anObject) const {
967   return myActiveObjects(anObject);
968 }
969