7b48df16345b14f6423a5dcaae54eabe40ac6c7d
[occt.git] / src / SelectMgr / SelectMgr_SelectableObject.cxx
1 // Created on: 1995-02-20
2 // Created by: Mister rmi
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Standard_NotImplemented.hxx>
18
19 #include <SelectMgr_SelectableObject.ixx>
20 #include <Standard_NoSuchObject.hxx>
21 #include <SelectMgr_Selection.hxx>
22 #include <Select3D_SensitiveEntity.hxx>
23 #include <SelectBasics_EntityOwner.hxx>
24 #include <SelectMgr_EntityOwner.hxx>
25 #include <PrsMgr_PresentationManager3d.hxx>
26
27 #include <TopLoc_Location.hxx>
28 #include <gp_Pnt.hxx>
29
30 static Standard_Integer Search (const SelectMgr_SequenceOfSelection& seq,
31                                 const Handle (SelectMgr_Selection)& theSel)
32 {
33   Standard_Integer ifound=0;
34   for (Standard_Integer i=1;i<=seq.Length()&& ifound==0;i++)
35     {if(theSel == seq.Value(i)) ifound=i;}
36   return ifound;
37
38
39
40
41 //==================================================
42 // Function: 
43 // Purpose :
44 //==================================================
45
46 SelectMgr_SelectableObject::SelectMgr_SelectableObject( const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d):PrsMgr_PresentableObject(aTypeOfPresentation3d)
47 {
48   myAutoHilight = Standard_True;
49 }
50
51
52 //==================================================
53 // Function: 
54 // Purpose :
55 //==================================================
56
57 Standard_Boolean SelectMgr_SelectableObject
58 ::HasSelection(const Standard_Integer aMode) const
59 {
60   Standard_Boolean Found=Standard_False;
61   for (Standard_Integer I=1;I<= myselections.Length() && !Found;I++)
62     { if(((myselections.Value(I))->Mode())==aMode) 
63         return Standard_True;
64     }
65   return Standard_False;
66 }
67
68 //==================================================
69 // Function: UpdateSelection
70 // Purpose :
71 //==================================================
72 void SelectMgr_SelectableObject::UpdateSelection()
73 {
74   for (Standard_Integer I=1;I<=myselections.Length();I++)
75     {
76       UpdateSelection(myselections.ChangeValue(I)->Mode());
77     }
78 }
79
80 Standard_Integer  SelectMgr_SelectableObject::NbPossibleSelection() const
81 {return 0;}
82
83 //==================================================
84 // Function: UpdateSelection
85 // Purpose :
86 //==================================================
87 void SelectMgr_SelectableObject::UpdateSelection(const Standard_Integer aMode)
88 {
89   for (Standard_Integer i =1; i<= myselections.Length(); i++ ) {
90     if (myselections.Value(i)->Mode() == aMode) {
91       myselections(i)->Clear();
92       ComputeSelection(myselections(i),aMode);
93       myselections(i)->UpdateStatus(SelectMgr_TOU_Partial);
94       return;
95     }
96   }
97   Handle(SelectMgr_Selection) S = new SelectMgr_Selection(aMode);
98   ComputeSelection(S,aMode);
99   S->UpdateStatus(SelectMgr_TOU_Partial);
100   
101   myselections.Append(S);
102   
103 }
104
105 //==================================================
106 // Function: ClearSelections
107 // Purpose :
108 //==================================================
109 void SelectMgr_SelectableObject::ClearSelections(const Standard_Boolean update)
110 {
111   for (Standard_Integer i =1; i<= myselections.Length(); i++ ) {
112     myselections.Value(i)->Clear();
113     if(update)
114       myselections.Value(i)->UpdateStatus(SelectMgr_TOU_Full);
115   }
116 }
117
118
119 //==================================================
120 // Function: Selection
121 // Purpose :
122 //==================================================
123
124 const Handle(SelectMgr_Selection)& SelectMgr_SelectableObject
125 ::Selection(const Standard_Integer aMode) const
126 {
127   static Handle(SelectMgr_Selection) bidsel;
128   Standard_Boolean Found = Standard_False;
129   Standard_Integer Rank=0;
130   for (Standard_Integer i=1;i<=myselections.Length() && !Found;i++)
131     {
132       if((myselections.Value(i))->Mode()==aMode){ Found = Standard_True;
133                                                   Rank=i;}}
134   return myselections.Value(Rank);
135 }
136
137
138 //==================================================
139 // Function: AddSelection
140 // Purpose :
141 //==================================================
142
143 void SelectMgr_SelectableObject
144 ::AddSelection(const Handle(SelectMgr_Selection)& aSel,
145                const Standard_Integer aMode)
146 {
147   if(aSel->IsEmpty()){
148     ComputeSelection(aSel,aMode);
149     aSel->UpdateStatus(SelectMgr_TOU_Partial);
150   }
151   if(HasSelection(aMode))
152     {
153       const Handle(SelectMgr_Selection)& temp= Selection(aMode);
154       Standard_Integer I = Search(myselections,temp);
155       if(I!=0) myselections.Remove(I);
156     }
157   myselections.Append(aSel);
158   
159 }
160
161
162
163 //=======================================================================
164 //function : ReSetTransformation
165 //purpose  : 
166 //=======================================================================
167 void SelectMgr_SelectableObject::ResetTransformation() 
168 {
169   TopLoc_Location aLoc;
170
171   TopLoc_Location aSelfLocation (Transformation());
172
173   // les selections
174   Handle(Select3D_SensitiveEntity) SE;
175   for(Init();More();Next()){
176     const Handle(SelectMgr_Selection) & Sel =  CurrentSelection();
177     for(Sel->Init();Sel->More();Sel->Next()){
178       SE =  *((Handle(Select3D_SensitiveEntity)*) &(Sel->Sensitive()));
179       if(!SE.IsNull()){
180         if(SE->HasLocation()) {
181           if( SE->Location()==aSelfLocation){
182             SE->ResetLocation();
183             const Handle(SelectBasics_EntityOwner)& EO = SE->OwnerId();
184             (*((Handle(SelectMgr_EntityOwner)*)&EO))->ResetLocation();}
185           else{
186             const TopLoc_Location& iniloc =SE->Location();
187             SE->SetLocation(iniloc*aSelfLocation.Inverted());
188             const Handle(SelectBasics_EntityOwner)& EO = SE->OwnerId();
189             (*((Handle(SelectMgr_EntityOwner)*)&EO))->SetLocation(SE->Location());}
190         }
191       }
192     }
193     Sel->UpdateStatus(SelectMgr_TOU_None);
194   }
195
196   PrsMgr_PresentableObject::ResetTransformation();
197 }
198
199
200 //=======================================================================
201 //function : UpdateTransformation
202 //purpose  : 
203 //=======================================================================
204 void SelectMgr_SelectableObject::UpdateTransformation() 
205 {
206   
207   Handle(Select3D_SensitiveEntity) SE;
208   for(Init();More();Next()){
209     const Handle(SelectMgr_Selection) & Sel =  CurrentSelection();
210     Sel->UpdateStatus(SelectMgr_TOU_Partial);
211   }
212   PrsMgr_PresentableObject::UpdateTransformation();
213
214 }
215
216
217 //=======================================================================
218 //function : UpdateTransformation
219 //purpose  : 
220 //=======================================================================
221 void SelectMgr_SelectableObject::UpdateTransformations(const Handle(SelectMgr_Selection)& Sel)
222 {
223   TopLoc_Location aSelfLocation (Transformation());
224   Handle(Select3D_SensitiveEntity) SE;
225   if(aSelfLocation.IsIdentity()) return;
226   for(Sel->Init();Sel->More();Sel->Next()){
227     SE =  *((Handle(Select3D_SensitiveEntity)*) &(Sel->Sensitive()));
228     if(!SE.IsNull()){
229       SE->UpdateLocation(aSelfLocation);
230       const Handle(SelectBasics_EntityOwner)& aEOwner = SE->OwnerId();
231       Handle(SelectMgr_EntityOwner) aMgrEO =
232                               Handle(SelectMgr_EntityOwner)::DownCast (aEOwner);
233       if (!aMgrEO.IsNull())
234         aMgrEO->SetLocation (aSelfLocation);
235     }
236   }
237 }
238
239 //=======================================================================
240 //function : HilightSelected
241 //purpose  : 
242 //=======================================================================
243 void SelectMgr_SelectableObject::HilightSelected 
244   ( const Handle(PrsMgr_PresentationManager3d)&,
245     const SelectMgr_SequenceOfOwner&)
246 {
247   Standard_NotImplemented::Raise ("SelectMgr_SelectableObject::HilightSelected");
248 }
249
250 //=======================================================================
251 //function : ClearSelected
252 //purpose  : 
253 //=======================================================================
254 void SelectMgr_SelectableObject::ClearSelected ()
255 {
256   if( !mySelectionPrs.IsNull() )
257     mySelectionPrs->Clear();
258 }
259
260 //=======================================================================
261 //function : HilightOwnerWithColor
262 //purpose  : 
263 //=======================================================================
264 void SelectMgr_SelectableObject::HilightOwnerWithColor 
265   ( const Handle(PrsMgr_PresentationManager3d)&,
266     const Quantity_NameOfColor,
267     const Handle(SelectMgr_EntityOwner)&)
268 {
269   Standard_NotImplemented::Raise ("SelectMgr_SelectableObject::HilightOwnerWithColor");
270 }
271
272 //=======================================================================
273 //function : MaxFaceNodes
274 //purpose  : 
275 //=======================================================================
276 Standard_Boolean SelectMgr_SelectableObject::IsAutoHilight () const
277 {
278   return myAutoHilight;
279 }
280
281 //=======================================================================
282 //function : MaxFaceNodes
283 //purpose  : 
284 //=======================================================================
285 void SelectMgr_SelectableObject::SetAutoHilight ( const Standard_Boolean newAutoHilight )
286 {
287   myAutoHilight = newAutoHilight;
288 }
289
290 //=======================================================================
291 //function : GetHilightPresentation
292 //purpose  : 
293 //=======================================================================
294 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetHilightPresentation( const Handle(PrsMgr_PresentationManager3d)& TheMgr )
295 {
296   if( myHilightPrs.IsNull() && !TheMgr.IsNull() )
297     {
298       myHilightPrs = new Prs3d_Presentation( TheMgr->StructureManager() );
299       myHilightPrs->SetTransformPersistence( GetTransformPersistenceMode(), 
300                                          GetTransformPersistencePoint() );
301     }
302
303   return myHilightPrs;
304 }
305
306
307 //=======================================================================
308 //function : GetSelectPresentation
309 //purpose  : 
310 //=======================================================================
311 Handle(Prs3d_Presentation) SelectMgr_SelectableObject::GetSelectPresentation( const Handle(PrsMgr_PresentationManager3d)& TheMgr )
312 {
313   if( mySelectionPrs.IsNull() && !TheMgr.IsNull() ) {
314     mySelectionPrs = new Prs3d_Presentation( TheMgr->StructureManager() );
315     mySelectionPrs->SetTransformPersistence( GetTransformPersistenceMode(), 
316                                              GetTransformPersistencePoint() );
317   }
318   return mySelectionPrs;
319 }
320
321 //=======================================================================
322 //function : SetZLayer
323 //purpose  :
324 //=======================================================================
325 void SelectMgr_SelectableObject::SetZLayer 
326   (const Handle(PrsMgr_PresentationManager)& thePrsMgr,
327    const Standard_Integer theLayerId)
328 {
329   if (thePrsMgr.IsNull())
330     return;
331
332   // update own presentations
333   PrsMgr_PresentableObject::SetZLayer (thePrsMgr, theLayerId);
334
335   // update selection presentations
336   if (!mySelectionPrs.IsNull())
337     mySelectionPrs->SetZLayer (theLayerId);
338
339   if (!myHilightPrs.IsNull())
340     myHilightPrs->SetZLayer (theLayerId);
341
342   // update all entity owner presentations
343   for (Init (); More () ;Next ())
344   {
345     const Handle(SelectMgr_Selection)& aSel = CurrentSelection();
346     for (aSel->Init (); aSel->More (); aSel->Next ())
347     {
348       Handle(Select3D_SensitiveEntity) aEntity = 
349         Handle(Select3D_SensitiveEntity)::DownCast (aSel->Sensitive());
350       if (!aEntity.IsNull())
351       {
352         Handle(SelectMgr_EntityOwner) aOwner = 
353           Handle(SelectMgr_EntityOwner)::DownCast (aEntity->OwnerId());
354         if (!aOwner.IsNull())
355           aOwner->SetZLayer (thePrsMgr, theLayerId);
356       }
357     }
358   }
359 }