7fd59977 |
1 | // File: ISession2D_InteractiveContext.cxx |
2 | // Created: Thu Jul 10 18:53:50 1997 |
3 | // Author: David Enjolras |
4 | // <den@zodiax.paris3.matra-dtv.fr> |
5 | |
6 | |
7 | #include "stdafx.h" |
8 | #include <OCC_MainFrame.h>// use for Message Bar |
9 | |
10 | #include "ISession2D_InteractiveContext.h" |
11 | #include <SelectMgr_EntityOwner.hxx> |
12 | #include <PrsMgr_PresentationManager2d.hxx> |
13 | #include <SelectMgr_SelectionManager.hxx> |
14 | #include <StdSelect_TextProjector2d.hxx> |
15 | |
16 | IMPLEMENT_STANDARD_HANDLE(ISession2D_InteractiveContext,MMgt_TShared) |
17 | IMPLEMENT_STANDARD_RTTIEXT(ISession2D_InteractiveContext,MMgt_TShared) |
18 | |
19 | #include "ISession2D_ObjectOwner.h" |
20 | #include "TColStd_MapIteratorOfMapOfTransient.hxx" |
21 | |
22 | ISession2D_InteractiveContext::ISession2D_InteractiveContext() |
23 | { |
24 | } |
25 | |
26 | ISession2D_InteractiveContext::ISession2D_InteractiveContext(const Handle(V2d_Viewer)& aViewer) |
27 | { |
28 | Initialize(aViewer); |
29 | } |
30 | |
31 | void ISession2D_InteractiveContext::Initialize(const Handle(V2d_Viewer)& aViewer) |
32 | { |
33 | ASSERT(!aViewer.IsNull()); |
34 | myViewer = aViewer; |
35 | myPrsmgr = new PrsMgr_PresentationManager2d(myViewer->View()); |
36 | mySelectionManager = new SelectMgr_SelectionManager(); |
37 | mySelector = new StdSelect_ViewerSelector2d(); |
38 | |
39 | mySelector->Set(3); |
40 | // set Sensitivity very very important for SensitiveCurve !! |
41 | mySelector->SetSensitivity (0.5); // en mm |
42 | mySelectionManager->Add(mySelector); |
43 | myHilightColorIndex = 5; // for dynamic highlight |
44 | IsAreasDisplayed = Standard_False; |
45 | |
46 | myPrsmgr->SetHighlightColor(8); // For Static highlight |
47 | } |
48 | |
49 | void ISession2D_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& anObject, |
50 | const Standard_Boolean Redraw ) |
51 | { |
52 | ASSERT(!anObject.IsNull()); |
53 | myObjects.Add(anObject); |
54 | |
55 | Standard_Integer DispMode ,HiMode,SelMode; |
56 | GetDefModes(anObject,DispMode ,HiMode,SelMode); |
57 | |
58 | myPrsmgr->Display(anObject,DispMode); |
59 | |
60 | mySelectionManager->Load(anObject,mySelector); |
61 | mySelectionManager->Activate(anObject,SelMode,mySelector); |
62 | |
63 | if (Redraw) myViewer->Update(); |
64 | } |
65 | |
66 | void ISession2D_InteractiveContext::Display (const Handle(AIS_InteractiveObject)& anObject, |
67 | const Standard_Integer aDisplayMode, |
68 | const Standard_Integer aSelectionMode, |
69 | const Standard_Boolean Redraw ) |
70 | { |
71 | ASSERT(!anObject.IsNull()); |
72 | myObjects.Add(anObject); |
73 | ASSERT(anObject->AcceptDisplayMode(aDisplayMode)); |
74 | anObject->SetDisplayMode(aDisplayMode); |
75 | |
76 | anObject->SetSelectionMode(aSelectionMode); |
77 | |
78 | myObjects.Add(anObject); |
79 | myPrsmgr->Display(anObject,aDisplayMode); |
80 | |
81 | mySelectionManager->Load(anObject,mySelector); |
82 | mySelectionManager->Activate(anObject,aSelectionMode,mySelector); |
83 | if (Redraw) myViewer->Update(); |
84 | } |
85 | |
86 | void ISession2D_InteractiveContext::Erase (const Handle(AIS_InteractiveObject)& anObject, |
87 | const Standard_Boolean Redraw ) |
88 | { |
89 | ASSERT(!myPrsmgr.IsNull()); |
90 | ASSERT(!anObject.IsNull()); |
91 | ASSERT(myObjects.Contains(anObject)); |
92 | Standard_Integer DispMode ,HiMode,SelMode; |
93 | GetDefModes(anObject,DispMode ,HiMode,SelMode); |
94 | myPrsmgr->Erase(anObject,DispMode); |
95 | mySelectionManager->Deactivate(anObject,mySelector); |
96 | if (Redraw) myViewer->Update(); |
97 | } |
98 | |
99 | void ISession2D_InteractiveContext::DisplayAll(const Standard_Boolean Redraw ) |
100 | { |
101 | ASSERT(!myPrsmgr.IsNull()); |
102 | |
103 | TColStd_MapIteratorOfMapOfTransient It(myObjects); |
104 | for(;It.More();It.Next()){ |
105 | Handle(AIS_InteractiveObject) anObject = Handle(AIS_InteractiveObject)::DownCast(It.Key()); |
106 | Standard_Integer DispMode ,HiMode,SelMode; |
107 | GetDefModes(anObject,DispMode ,HiMode,SelMode); |
108 | myPrsmgr->Display(anObject,DispMode); |
109 | mySelectionManager->Load(anObject,mySelector); |
110 | mySelectionManager->Activate(anObject,HiMode,mySelector); |
111 | } |
112 | if (Redraw) myViewer->Update(); |
113 | } |
114 | |
115 | void ISession2D_InteractiveContext::EraseAll (const Standard_Boolean Redraw ) |
116 | { |
117 | ASSERT(!myPrsmgr.IsNull()); |
118 | TColStd_MapIteratorOfMapOfTransient It(myObjects); |
119 | for(;It.More();It.Next()){ |
120 | Handle(AIS_InteractiveObject) anObject = Handle(AIS_InteractiveObject)::DownCast(It.Key()); |
121 | Standard_Integer DispMode ,HiMode,SelMode; |
122 | GetDefModes(anObject,DispMode ,HiMode,SelMode); |
123 | myPrsmgr->Erase(anObject,DispMode); |
124 | mySelectionManager->Deactivate(anObject,mySelector); |
125 | } |
126 | if (Redraw) myViewer->Update(); |
127 | } |
128 | |
129 | Standard_Boolean ISession2D_InteractiveContext::IsDisplayed(const Handle(AIS_InteractiveObject)& anObject, |
130 | const Standard_Integer aMode) |
131 | { |
132 | ASSERT(!myPrsmgr.IsNull()); |
133 | ASSERT(!anObject.IsNull()); |
134 | return myPrsmgr->IsDisplayed(anObject,aMode); |
135 | } |
136 | |
137 | void ISession2D_InteractiveContext::Redisplay(const Handle(AIS_InteractiveObject)& anObject, |
138 | const Standard_Boolean Redraw, |
139 | const Standard_Boolean allmodes) |
140 | { |
141 | ASSERT(!myPrsmgr.IsNull()); |
142 | ASSERT(!anObject.IsNull()); |
143 | ASSERT(myObjects.Contains(anObject)); |
144 | |
145 | Standard_Integer DispMode ,HiMode,SelMode; |
146 | GetDefModes(anObject,DispMode ,HiMode,SelMode); |
147 | myPrsmgr->Update(anObject,DispMode); |
148 | |
149 | // WARNING : IMPLEMENTATION IS NOT FINISH !!!!!!!! |
150 | // NEED TO UPDATE THE OTHER MODES, AND SELECTION !!!!! |
151 | |
152 | // mySelectionManager->Load(anObject,mySelector); |
153 | // mySelectionManager->Activate(anObject,SelMode,mySelector); |
154 | |
155 | if (Redraw) myViewer->Update(); |
156 | |
157 | } |
158 | |
159 | void ISession2D_InteractiveContext::Clear (const Handle(AIS_InteractiveObject)& anObject, |
160 | const Standard_Boolean Redraw) |
161 | { |
162 | ASSERT(!myPrsmgr.IsNull()); |
163 | ASSERT(!anObject.IsNull()); |
164 | ASSERT(myObjects.Contains(anObject)); |
165 | |
166 | Standard_Failure::Raise("NotYetImplemented"); |
167 | } |
168 | |
169 | void ISession2D_InteractiveContext::Remove (const Handle(AIS_InteractiveObject)& anObject, |
170 | const Standard_Boolean Redraw) |
171 | { |
172 | ASSERT(!myPrsmgr.IsNull()); |
173 | ASSERT(!anObject.IsNull()); |
174 | ASSERT(myObjects.Contains(anObject)); |
175 | Erase(anObject,Redraw); |
176 | myObjects.Remove(anObject); |
177 | } |
178 | |
179 | void ISession2D_InteractiveContext::Highlight (const Handle(AIS_InteractiveObject)& anObject, |
180 | const Standard_Boolean Redraw) |
181 | { |
182 | ASSERT(!myPrsmgr.IsNull()); |
183 | ASSERT(!anObject.IsNull()); |
184 | ASSERT(myObjects.Contains(anObject)); |
185 | Standard_Integer DispMode ,HiMode,SelMode; |
186 | GetDefModes(anObject,DispMode ,HiMode,SelMode); |
187 | myPrsmgr->Highlight(anObject,HiMode); |
188 | if(Redraw) myViewer->Update(); |
189 | } |
190 | |
191 | void ISession2D_InteractiveContext::Unhighlight (const Handle(AIS_InteractiveObject)& anObject, |
192 | const Standard_Boolean Redraw) |
193 | { |
194 | ASSERT(!myPrsmgr.IsNull()); |
195 | ASSERT(!anObject.IsNull()); |
196 | ASSERT(myObjects.Contains(anObject)); |
197 | Standard_Integer DispMode ,HiMode,SelMode; |
198 | GetDefModes(anObject,DispMode ,HiMode,SelMode); |
199 | myPrsmgr->Unhighlight(anObject,HiMode); |
200 | if(Redraw) myViewer->Update(); |
201 | } |
202 | |
203 | Standard_Boolean ISession2D_InteractiveContext::IsHilighted(const Handle(AIS_InteractiveObject)& anObject) |
204 | { |
205 | ASSERT(!myPrsmgr.IsNull()); |
206 | ASSERT(!anObject.IsNull()); |
207 | ASSERT(myObjects.Contains(anObject)); |
208 | Standard_Integer DispMode ,HiMode,SelMode; |
209 | GetDefModes(anObject,DispMode ,HiMode,SelMode); |
210 | return myPrsmgr->IsHighlighted(anObject,HiMode); |
211 | } |
212 | |
213 | Standard_Boolean ISession2D_InteractiveContext::NewProjector(const Handle(V2d_View)& aView, |
214 | Handle(Select2D_Projector)& NewProjector) // out parameter. |
215 | { |
216 | // to be changed : here we build a new projector each time. |
217 | // or we have to define a new one only if we change : |
218 | // the View |
219 | // the view zoom factor |
220 | // the view panning |
221 | // the fonts changes. |
222 | NewProjector = new StdSelect_TextProjector2d(aView); |
223 | return Standard_True; |
224 | } |
225 | |
226 | // Manage Detected : update The Display |
227 | /*private*/ void ISession2D_InteractiveContext::ManageDetected(TColStd_MapOfTransient& aNewMapOfDetectedOwner) |
228 | { |
229 | Standard_Boolean NeedRedraw = Standard_False; |
230 | //====================================================================== |
231 | // Treatement on the old Selection : |
232 | // for each owner : |
233 | // if the object is decomposed : Unhighlight the Owner, |
234 | // else Unhighlight the Object,except if selected |
235 | //====================================================================== |
236 | |
237 | TColStd_MapIteratorOfMapOfTransient anIterator(myMapOfDetectedOwner); |
238 | for (;anIterator.More(); |
239 | anIterator.Next()) |
240 | { |
241 | // for all owner : |
242 | Handle(ISession2D_ObjectOwner) TheOwner = |
243 | Handle(ISession2D_ObjectOwner)::DownCast(anIterator.Key()); |
244 | |
245 | Handle(AIS_InteractiveObject) TheObject = |
246 | Handle(AIS_InteractiveObject)::DownCast(TheOwner->Selectable()); |
247 | |
248 | // if the owner is not still detected |
249 | if (!aNewMapOfDetectedOwner.Contains(TheOwner)) |
250 | { |
251 | NeedRedraw = Standard_True; |
252 | // if the corresponding object is a Shape and is not Decomposed |
253 | Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(TheObject); |
254 | Standard_Boolean TreatementOnCompleteObject = Standard_True; |
255 | if (!anAISShape.IsNull()) |
256 | if (anAISShape->AcceptShapeDecomposition() && anAISShape->HasSelectionMode()) |
257 | TreatementOnCompleteObject = Standard_False; |
258 | if(TreatementOnCompleteObject) |
259 | { |
260 | // if the corresponding object is not Selected |
261 | if (!myMapOfSelectedOwner.Contains(TheOwner)) |
262 | { |
263 | Standard_Integer DispMode ,HiMode,SelMode; |
264 | GetDefModes(TheObject,DispMode ,HiMode,SelMode); |
265 | if ( myPrsmgr->IsHighlighted(TheObject)) |
266 | myPrsmgr->Unhighlight(TheObject,HiMode); |
267 | } |
268 | } |
269 | else |
270 | { |
271 | // UnHighlight the old detected Owner, |
272 | TheOwner->Unhilight(myPrsmgr); |
273 | } |
274 | } |
275 | } |
276 | |
277 | //====================================================================== |
278 | // Treatement on the New Selection |
279 | //====================================================================== |
280 | |
281 | anIterator.Initialize(aNewMapOfDetectedOwner); |
282 | if (!anIterator.More()) ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage("Nothing Detected"); |
283 | for (;anIterator.More(); |
284 | anIterator.Next()) |
285 | { |
286 | // for all owner : |
287 | Handle(ISession2D_ObjectOwner) TheOwner = |
288 | Handle(ISession2D_ObjectOwner)::DownCast(anIterator.Key()); |
289 | // update the message bar |
290 | ((OCC_MainFrame*)AfxGetMainWnd())->SetStatusMessage(TheOwner->DumpMessage().ToCString()); |
291 | Handle(AIS_InteractiveObject) TheObject = |
292 | Handle(AIS_InteractiveObject)::DownCast(TheOwner->Selectable()); |
293 | |
294 | // if the owner is not alwraidy detected |
295 | if (!myMapOfDetectedOwner.Contains(TheOwner)) |
296 | { |
297 | NeedRedraw = Standard_True; |
298 | |
299 | // if the corresponding object is a Shape and is not Decomposed |
300 | Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(TheObject); |
301 | Standard_Boolean TreatementOnCompleteObject = Standard_True; |
302 | if (!anAISShape.IsNull()) |
303 | if (anAISShape->AcceptShapeDecomposition() && anAISShape->HasSelectionMode()) |
304 | TreatementOnCompleteObject = Standard_False; |
305 | if(TreatementOnCompleteObject) |
306 | { |
307 | // if the corresponding object is not Selected |
308 | if (!myMapOfSelectedOwner.Contains(TheOwner)) |
309 | { |
310 | Standard_Integer DispMode ,HiMode,SelMode; |
311 | GetDefModes(TheObject,DispMode ,HiMode,SelMode); |
312 | if (!myPrsmgr->IsHighlighted(TheObject)) |
313 | myPrsmgr->ColorHighlight(TheObject,myHilightColorIndex,HiMode); |
314 | } |
315 | } |
316 | else |
317 | { |
318 | // The Object Is Decomposed --> Specific Treatement on the owner |
319 | TheOwner->Hilight(myPrsmgr); |
320 | } |
321 | } |
322 | } |
323 | //====================================================================== |
324 | // set the new detected map |
325 | //====================================================================== |
326 | |
327 | // set The new detected Owner |
328 | myMapOfDetectedOwner = aNewMapOfDetectedOwner; |
329 | |
330 | if (NeedRedraw) myViewer->Update(); |
331 | } |
332 | |
333 | void ISession2D_InteractiveContext::Move(const Standard_Integer x1, |
334 | const Standard_Integer y1, |
335 | const Handle(V2d_View)& aView) |
336 | { |
337 | Handle(Select2D_Projector) aProjector; |
338 | if (NewProjector(aView,aProjector)) // if we need a new projector |
339 | mySelector->Set(aProjector); |
340 | |
341 | mySelector->Pick(x1,y1,aView); |
342 | TColStd_MapOfTransient aNewMapOfDetectedOwner; |
343 | mySelector->Init(); |
344 | if(mySelector->More()) |
345 | { |
346 | Handle(SelectMgr_EntityOwner) aOwner = mySelector->OnePicked(); |
347 | ASSERT(aOwner->IsKind(STANDARD_TYPE(ISession2D_ObjectOwner))); |
348 | Handle(ISession2D_ObjectOwner) aNewDetectedObjectOwner = |
349 | Handle(ISession2D_ObjectOwner)::DownCast(aOwner); |
350 | aNewMapOfDetectedOwner.Add(aNewDetectedObjectOwner); |
351 | } |
352 | |
353 | ManageDetected(aNewMapOfDetectedOwner); |
354 | |
355 | } |
356 | |
357 | void ISession2D_InteractiveContext::Move(const Standard_Integer x1, |
358 | const Standard_Integer y1, |
359 | const Standard_Integer x2, |
360 | const Standard_Integer y2, |
361 | const Handle(V2d_View)& aView) |
362 | { |
363 | Handle(Select2D_Projector) aProjector; |
364 | if (NewProjector(aView,aProjector)) // if we need a new projector |
365 | mySelector->Set(aProjector); |
366 | |
367 | mySelector->Pick(x1,y1,x2,y2,aView); |
368 | TColStd_MapOfTransient aNewMapOfDetectedOwner; |
369 | for(mySelector->Init();mySelector->More();mySelector->Next()) |
370 | { |
371 | Handle(SelectMgr_EntityOwner) aOwner = mySelector->Picked(); |
372 | ASSERT(aOwner->IsKind(STANDARD_TYPE(ISession2D_ObjectOwner))); |
373 | Handle(ISession2D_ObjectOwner) aNewDetectedObjectOwner = |
374 | Handle(ISession2D_ObjectOwner)::DownCast(aOwner); |
375 | aNewMapOfDetectedOwner.Add(aNewDetectedObjectOwner); |
376 | } |
377 | ManageDetected(aNewMapOfDetectedOwner); |
378 | } |
379 | |
380 | void ISession2D_InteractiveContext::Pick(const Standard_Boolean MultiSelection)// = Standard_False |
381 | { |
382 | // We have a Map of all the seletected owners : myMapOfSelectedOwner; |
383 | // if we are not in MultiSelection. |
384 | // UnHighLight and keep out the map all the last selected objects |
385 | |
386 | if (!MultiSelection) |
387 | { |
388 | TColStd_MapIteratorOfMapOfTransient anIterator(myMapOfSelectedOwner); |
389 | for (;anIterator.More(); |
390 | anIterator.Next()) |
391 | { |
392 | Handle(ISession2D_ObjectOwner) TheOwner = |
393 | Handle(ISession2D_ObjectOwner)::DownCast(anIterator.Key()); |
394 | Handle(AIS_InteractiveObject) TheObject = |
395 | Handle(AIS_InteractiveObject)::DownCast(TheOwner->Selectable()); |
396 | |
397 | // UnHighLight The Last Selected ObjectObject. |
398 | Unhighlight(TheObject, // Object |
399 | Standard_False); // Redraw |
400 | } |
401 | myMapOfSelectedOwner.Clear(); |
402 | } |
403 | |
404 | TColStd_MapIteratorOfMapOfTransient anIterator(myMapOfDetectedOwner); |
405 | for (;anIterator.More(); |
406 | anIterator.Next()) |
407 | { |
408 | // for all owner : |
409 | Handle(ISession2D_ObjectOwner) TheOwner = |
410 | Handle(ISession2D_ObjectOwner)::DownCast(anIterator.Key()); |
411 | myMapOfSelectedOwner.Add(TheOwner); |
412 | |
413 | Handle(AIS_InteractiveObject) TheObject = |
414 | Handle(AIS_InteractiveObject)::DownCast(TheOwner->Selectable()); |
415 | Highlight(TheObject, // Object |
416 | Standard_False); // Redraw |
417 | } |
418 | myViewer->Update(); |
419 | } |
420 | |
421 | //======================================================================= |
422 | //function : GetDefModes |
423 | //purpose : |
424 | //======================================================================= |
425 | |
426 | void ISession2D_InteractiveContext::GetDefModes(const Handle(AIS_InteractiveObject)& anObject, |
427 | Standard_Integer& DispMode, |
428 | Standard_Integer& HiMode, |
429 | Standard_Integer& SelMode) |
430 | { |
431 | DispMode = anObject->HasDisplayMode() ? anObject->DisplayMode() : |
432 | anObject->DefaultDisplayMode(); |
433 | |
434 | HiMode = anObject->HasHilightMode()? anObject->HilightMode():DispMode; |
435 | |
436 | SelMode = anObject->HasSelectionMode()? anObject->SelectionMode() : |
437 | 0; |
438 | } |
439 | |
440 | void ISession2D_InteractiveContext::DisplayAreas() |
441 | { |
442 | if (IsAreasDisplayed) return; |
443 | myViewer->InitActiveViews(); |
444 | while(myViewer->MoreActiveViews()) |
445 | { |
446 | Handle(V2d_View) aView = myViewer->ActiveView(); |
447 | mySelector->DisplayAreas(aView); |
448 | myViewer->NextActiveViews(); |
449 | } |
450 | myViewer->Update(); |
451 | IsAreasDisplayed= Standard_True; |
452 | } |
453 | void ISession2D_InteractiveContext::ClearAreas() |
454 | { |
455 | if (!IsAreasDisplayed) return; |
456 | mySelector->ClearAreas(); |
457 | myViewer->Update(); |
458 | IsAreasDisplayed= Standard_False; |
459 | } |
460 | |
461 | |