3c6393e59464ec46a17ff2b99f5e8c5dd49c8704
[occt.git] / src / StdSelect / StdSelect_BRepHilighter.cxx
1 // Created on: 1995-03-22
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22
23
24 #include <StdSelect_BRepHilighter.ixx>
25 #include <StdSelect_BRepOwner.hxx>
26 #include <StdSelect_Prs.hxx>
27 #include <Graphic3d_StructureManager.hxx>
28 #include <Graphic3d_Structure.hxx>
29 #include <Aspect_TypeOfMarker.hxx>
30 #include <Prs3d_PointAspect.hxx>
31 #include <V3d_Viewer.hxx>
32 #include <V3d_View.hxx>
33 #include <Prs3d_Presentation.hxx>
34 #include <StdPrs_WFShape.hxx>
35 #include <OSD_Chronometer.hxx>
36 #include <Visual3d_TransientManager.hxx>
37 static TColStd_MapIteratorOfMapOfInteger it;
38 static Standard_Boolean ImmediateIsEnable;
39 static  Standard_Boolean ImmediateIsEnableIsDef= Standard_False;
40 //==================================================
41 // Function: 
42 // Purpose :
43 //==================================================
44
45 StdSelect_BRepHilighter::
46 StdSelect_BRepHilighter():mylastindex(0)
47 ,myManager(new Visual3d_TransientManager){}
48
49 //==================================================
50 // Function: 
51 // Purpose :
52 //==================================================
53
54 StdSelect_BRepHilighter::
55 StdSelect_BRepHilighter(const Handle(StdSelect_ViewerSelector3d)& aSelector,
56                         const Handle(V3d_Viewer)& aViewer,
57                         const Quantity_NameOfColor aColor,
58                         const StdSelect_TypeOfResult aTOR):
59 myselector(aSelector),
60 myviewer(aViewer),
61 mycolor(aColor),
62 mydrwr(new Prs3d_Drawer()),
63 mytype(aTOR),
64 mylastindex(0),
65 myManager(new Visual3d_TransientManager)
66
67 {
68   Handle(Prs3d_PointAspect) PA = new Prs3d_PointAspect(Aspect_TOM_O,Quantity_NOC_INDIANRED3,3.);
69   mydrwr->SetPointAspect(PA);
70 }
71 //==================================================
72 // Function: 
73 // Purpose :
74 //==================================================
75
76 void StdSelect_BRepHilighter::
77 Set(const Handle(StdSelect_ViewerSelector3d)& aSelector)
78 {myselector=aSelector;}
79 //==================================================
80 // Function: 
81 // Purpose :
82 //==================================================
83
84 void StdSelect_BRepHilighter::
85 Set(const StdSelect_TypeOfResult aTOR)
86 {mytype =aTOR;}
87
88 //==================================================
89 // Function: Set
90 // Purpose :
91 //==================================================
92
93 void StdSelect_BRepHilighter::
94 Set(const Handle(V3d_Viewer)& aViewer)
95 {myviewer=aViewer;}
96 //==================================================
97 // Function: 
98 // Purpose :
99 //==================================================
100
101 void StdSelect_BRepHilighter::
102 Set(const Quantity_NameOfColor aColor)
103 {mycolor=aColor;} 
104 //==================================================
105 // Function: 
106 // Purpose :
107 //==================================================
108 void StdSelect_BRepHilighter::
109 Process()
110 {
111   Update();
112   myviewer->Update();
113 }
114 //==================================================
115 // Function: 
116 // Purpose :
117 //==================================================
118 void StdSelect_BRepHilighter::
119 Process(const Handle(V3d_View)& aViou,const Standard_Boolean DoubleBuffer)
120 {
121   if(aViou->Viewer() == myviewer){
122     Update(aViou,DoubleBuffer);
123 //    aViou->Update();
124   }
125 }
126
127 //==================================================
128 // Function: 
129 // Purpose :
130 //==================================================
131
132 void StdSelect_BRepHilighter::
133 Update()
134 {
135   myselector->Init();
136   
137   switch(mytype){
138   case StdSelect_TOR_MULTIPLE:{
139     TColStd_MapIteratorOfMapOfInteger it;
140     if(!myselector->More())
141       {
142         for(it.Initialize(myold);it.More();it.Next())
143           {myhimap.FindFromIndex(it.Key())->UnHighlight();
144            myhimap.FindFromIndex(it.Key())->SetVisible(Standard_False);
145          }
146         myold.Clear();
147         mynew.Clear();
148       }
149     else{
150
151       for (;myselector->More();myselector->Next())
152         {
153           const Handle(SelectBasics_EntityOwner)& OO = myselector->Picked();
154           if (!myhimap.Contains(OO))
155             {
156               if(!Handle(StdSelect_BRepOwner)::DownCast(OO).IsNull()){
157                 Handle(StdSelect_Prs) NiouPrs = new StdSelect_Prs
158                   (myviewer->Viewer());
159                 
160                 StdPrs_WFShape::Add(NiouPrs,
161                                     Handle(StdSelect_BRepOwner)::DownCast(OO)->Shape(),
162                                     mydrwr);
163                 NiouPrs->Display();
164                 myhimap.Add(OO,NiouPrs);
165               mynew.Add(myhimap.FindIndex(OO));
166               if(myold.Contains(myhimap.FindIndex(OO))) 
167                 myold.Remove(myhimap.FindIndex(OO));
168               }
169             }
170         }
171       for(it.Initialize(myold);it.More();it.Next()){
172         myhimap.FindFromIndex(it.Key())->UnHighlight();
173         myhimap.FindFromIndex(it.Key())->SetVisible(Standard_False);
174       }
175       
176       myold = mynew;
177       mynew.Clear();
178       for(it.Initialize(myold);it.More();it.Next()){
179         myhimap.FindFromIndex(it.Key())->SetVisible(Standard_True);
180         myhimap.FindFromIndex(it.Key())->SetDisplayPriority(10);
181         myhimap.FindFromIndex(it.Key())->Color(mycolor);
182       }
183     }
184     break;
185   }
186     
187   case StdSelect_TOR_SIMPLE:
188     {
189       if(myselector->More()){
190         const Handle(SelectBasics_EntityOwner)& OO = myselector->OnePicked();    
191         if(!Handle(StdSelect_BRepOwner)::DownCast(OO).IsNull()){
192           if (!myhimap.Contains(OO)){    
193             Handle(StdSelect_Prs) NiouPrs = new StdSelect_Prs
194               (myviewer->Viewer());
195             StdPrs_WFShape::Add(NiouPrs,
196                                 Handle(StdSelect_BRepOwner)::DownCast(OO)->Shape(),
197                                 mydrwr);
198             NiouPrs->Display();
199             myhimap.Add(OO,NiouPrs);
200           } 
201         }
202         if(mylastindex!=myhimap.FindIndex(OO))
203           {
204             if(mylastindex!=0) 
205               {
206                 myhimap.FindFromIndex(mylastindex)->UnHighlight();
207                 myhimap.FindFromIndex(mylastindex)->SetVisible(Standard_False);
208               }
209             mylastindex = myhimap.FindIndex(OO);
210             myhimap.FindFromIndex(mylastindex)->SetVisible(Standard_True);
211             myhimap.FindFromIndex(mylastindex)->SetDisplayPriority(10);
212             myhimap.FindFromIndex(mylastindex)->Color(mycolor);
213           }
214       }
215       else {   
216         if(mylastindex!=0) 
217               {
218                 myhimap.FindFromIndex(mylastindex)->UnHighlight();
219                 myhimap.FindFromIndex(mylastindex)->SetVisible(Standard_False);
220                 mylastindex=0;
221               }
222         for (it.Initialize(myold);it.More();it.Next()){
223           myhimap.FindFromIndex(it.Key())->UnHighlight();
224           myhimap.FindFromIndex(it.Key())->SetVisible(Standard_False);}
225         myold.Clear();
226       }
227     }
228   }
229 }
230
231 void StdSelect_BRepHilighter::
232 Update(const Handle(V3d_View)& aView,const Standard_Boolean DoubleBuffer)
233 {
234   if(!ImmediateIsEnableIsDef) {
235     ImmediateIsEnable = aView->TransientManagerBeginDraw();
236     ImmediateIsEnableIsDef = Standard_True;
237     Visual3d_TransientManager::EndDraw();
238   }
239   if(!ImmediateIsEnable) {
240     Update();
241   }
242   else {
243     myselector->Init();
244     mynew.Clear();
245   
246     switch(mytype){
247     case StdSelect_TOR_MULTIPLE:{
248       
249       for (;myselector->More();myselector->Next()) {
250         const Handle(SelectBasics_EntityOwner)& OO = myselector->Picked();
251         if(!Handle(StdSelect_BRepOwner)::DownCast(OO).IsNull()){
252           if (!myhimap.Contains(OO)) {
253             Handle(StdSelect_Prs) NiouPrs = new StdSelect_Prs(myviewer->Viewer());
254             
255             StdPrs_WFShape::Add(NiouPrs,Handle(StdSelect_BRepOwner)::DownCast(OO)->Shape(),mydrwr);
256             NiouPrs->Color(mycolor);
257             myhimap.Add(OO,NiouPrs);
258           }
259           mynew.Add(myhimap.FindIndex(OO));
260         }
261       }
262       break;
263     }
264       
265     case StdSelect_TOR_SIMPLE: {
266       
267       if(myselector->More()){
268         const Handle(SelectBasics_EntityOwner)& OO = myselector->OnePicked();    
269         if(!Handle(StdSelect_BRepOwner)::DownCast(OO).IsNull()){
270           if (!myhimap.Contains(OO)){    
271             Handle(StdSelect_Prs) NiouPrs = new StdSelect_Prs(myviewer->Viewer());
272             StdPrs_WFShape::Add(NiouPrs,Handle(StdSelect_BRepOwner)::DownCast(OO)->Shape(),mydrwr);
273             NiouPrs->Color(mycolor);
274             myhimap.Add(OO,NiouPrs);
275           }
276           mynew.Add(myhimap.FindIndex(OO));
277         }
278       }
279     }
280       break;
281     } 
282     Standard_Boolean First = Standard_True; // to avoid begindraw if there is nothing (expensive)
283     
284     for(it.Initialize(mynew);it.More();it.Next()){
285       
286       if(First) {
287         aView->TransientManagerBeginDraw(DoubleBuffer);
288         First = Standard_False;
289       }
290       
291       Visual3d_TransientManager::DrawStructure(myhimap.FindFromIndex(it.Key()));
292     }
293     
294     if(!First) 
295       Visual3d_TransientManager::EndDraw();
296   }
297 }
298
299 void StdSelect_BRepHilighter::Clear()
300 {
301   if(!myhimap.IsEmpty()){
302     for (Standard_Integer i=1;i<=myhimap.Extent();i++){
303       myhimap.FindFromIndex(i)->UnHighlight();
304       myhimap.FindFromIndex(i)->Erase();
305       myhimap.FindFromIndex(i)->Clear();
306     }
307     myold.Clear();
308     mynew.Clear();
309     mylastindex=0;   
310   }
311   myhimap.Clear();
312 }
313
314 const Handle(Prs3d_Drawer)& StdSelect_BRepHilighter::Drawer() const 
315 {
316   return mydrwr;
317 }
318