0026377: Passing Handle objects as arguments to functions as non-const reference...
[occt.git] / src / StdSelect / StdSelect_BRepOwner.cxx
1 // Created on: 1995-03-08
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
18 #include <Graphic3d_StructureManager.hxx>
19 #include <Prs3d_Drawer.hxx>
20 #include <PrsMgr_PresentationManager.hxx>
21 #include <SelectBasics_EntityOwner.hxx>
22 #include <SelectMgr_SelectableObject.hxx>
23 #include <Standard_Type.hxx>
24 #include <StdSelect_BRepOwner.hxx>
25 #include <StdSelect_Shape.hxx>
26 #include <TopLoc_Location.hxx>
27 #include <TopoDS_Shape.hxx>
28
29 IMPLEMENT_STANDARD_RTTIEXT(StdSelect_BRepOwner,SelectMgr_EntityOwner)
30
31 //==================================================
32 // Function: 
33 // Purpose :
34 //==================================================
35 StdSelect_BRepOwner::StdSelect_BRepOwner(const Standard_Integer aPriority):
36 SelectMgr_EntityOwner(aPriority),
37 myFromDecomposition(Standard_False),
38 myCurMode(0)
39 {
40 }
41
42 StdSelect_BRepOwner::StdSelect_BRepOwner(const TopoDS_Shape& aShape,
43                                          const Standard_Integer aPriority,
44                                          const Standard_Boolean ComesFromDecomposition):
45 SelectMgr_EntityOwner(aPriority),
46 myFromDecomposition(ComesFromDecomposition),
47 myShape(aShape),
48 myCurMode(0)
49 {
50 }
51
52 StdSelect_BRepOwner::StdSelect_BRepOwner(const TopoDS_Shape& aShape,
53                                          const Handle (SelectMgr_SelectableObject)& theOrigin,
54                                          const Standard_Integer aPriority,
55                                          const Standard_Boolean ComesFromDecomposition):
56 SelectMgr_EntityOwner(theOrigin,aPriority),
57 myFromDecomposition(ComesFromDecomposition),
58 myShape(aShape),
59 myCurMode(0)
60 {
61 }
62
63 //=======================================================================
64 //function : IsHilighted
65 //purpose  : 
66 //=======================================================================
67 Standard_Boolean StdSelect_BRepOwner::
68 IsHilighted(const Handle(PrsMgr_PresentationManager)& PM,
69             const Standard_Integer aMode) const 
70 {
71   Standard_Integer M = (aMode < 0) ? myCurMode : aMode;
72   if(myPrsSh.IsNull())
73     return PM->IsHighlighted(Selectable(),M);
74   return PM->IsHighlighted(myPrsSh,M);
75 }
76
77 //=======================================================================
78 //function : Hilight/Unhilight Methods...
79 //purpose  : 
80 //=======================================================================
81 void StdSelect_BRepOwner::Hilight(const Handle(PrsMgr_PresentationManager)& PM,
82                                   const Standard_Integer aMode)
83 {
84   
85   Standard_Integer M = (aMode < 0) ? myCurMode : aMode;
86   Handle(SelectMgr_SelectableObject) aSel = Selectable();
87   if (myFromDecomposition)
88   {
89     // do the update flag check
90     if (!myPrsSh.IsNull())
91     {
92       TColStd_ListOfInteger aModesList;
93       myPrsSh->ToBeUpdated (aModesList);
94       if (!aModesList.IsEmpty())
95         myPrsSh.Nullify();
96     }
97
98     Handle(Prs3d_Drawer) aDrawer;
99     if (!aSel.IsNull())
100     {
101       aDrawer = aSel->HilightAttributes();
102     }
103     else
104     {
105       aDrawer = new Prs3d_Drawer();
106       SelectMgr_SelectableObject::InitDefaultHilightAttributes (aDrawer);
107     }
108
109     // generate new presentable shape
110     if (myPrsSh.IsNull())
111     {
112       myPrsSh = new StdSelect_Shape (myShape, aDrawer);
113     }
114     if (!aSel.IsNull())
115     {
116       myPrsSh->SetZLayer (aSel->ZLayer());
117     }
118
119     // highlight and set layer
120     PM->Highlight (myPrsSh, M);
121   }  
122   else
123   {
124     if(myPrsSh.IsNull())
125       PM->Highlight(aSel,M);
126     else
127       PM->Highlight(myPrsSh,M);
128   }
129 }
130
131 void StdSelect_BRepOwner::Hilight()
132 {}
133
134 void StdSelect_BRepOwner::HilightWithColor(const Handle(PrsMgr_PresentationManager3d)& PM,
135                                            const Quantity_NameOfColor aCol,
136                                            const Standard_Integer aMode)
137 {
138   Standard_Integer M = (aMode < 0) ? myCurMode : aMode;
139   Graphic3d_ZLayerId aHiLayer = this == Selectable()->GlobalSelOwner().get() ?
140                                 Graphic3d_ZLayerId_Top : Graphic3d_ZLayerId_Topmost;
141   Handle(SelectMgr_SelectableObject) aSel = Selectable();
142   if (myFromDecomposition)
143   {
144     // do the update flag check
145     if (!myPrsSh.IsNull())
146     {
147       TColStd_ListOfInteger aModesList;
148       myPrsSh->ToBeUpdated (aModesList);
149       if (!aModesList.IsEmpty())
150         myPrsSh.Nullify();
151     }
152
153     Handle(Prs3d_Drawer) aDrawer;
154     if (!aSel.IsNull())
155     {
156       aDrawer = aSel->HilightAttributes();
157     }
158     else
159     {
160       aDrawer = new Prs3d_Drawer();
161       SelectMgr_SelectableObject::InitDefaultHilightAttributes (aDrawer);
162     }
163
164     // generate new presentable shape
165     if(myPrsSh.IsNull())
166     {
167       if(HasLocation())
168       {
169         TopLoc_Location lbid = Location() * myShape.Location();
170         TopoDS_Shape ShBis = myShape.Located(lbid);
171         myPrsSh = new StdSelect_Shape(ShBis, aDrawer);
172       }
173       else
174         myPrsSh = new StdSelect_Shape(myShape, aDrawer);
175     }
176     if (!aSel.IsNull())
177     {
178       myPrsSh->SetZLayer (aSel->ZLayer());
179     }
180
181     // highlight with color and set layer
182     PM->Color (myPrsSh, aCol, M, aSel, aHiLayer);
183   }
184   else
185   {
186     if (!myPrsSh.IsNull())
187     {
188       PM->Color (myPrsSh, aCol, M, aSel, aHiLayer);
189     }
190     else
191     {
192       PM->Color (aSel, aCol, M, NULL, aHiLayer);
193     }
194   }
195 }
196
197 void StdSelect_BRepOwner::Unhilight(const Handle(PrsMgr_PresentationManager)& PM,
198                                     const Standard_Integer aMode)
199 {
200   Standard_Integer M = (aMode < 0) ? myCurMode : aMode;
201   if(myPrsSh.IsNull() || !myFromDecomposition)
202     PM->Unhighlight(Selectable(),M);
203   else
204     PM->Unhighlight(myPrsSh,M);
205 }
206
207 void StdSelect_BRepOwner::Clear(const Handle(PrsMgr_PresentationManager)& PM,
208                                 const Standard_Integer aMode)
209 {
210   Standard_Integer M = (aMode < 0) ? myCurMode : aMode;
211   if (!myPrsSh.IsNull())
212     PM->Clear(myPrsSh,M);
213   myPrsSh.Nullify();
214 }
215
216 void StdSelect_BRepOwner::SetLocation(const TopLoc_Location& aLoc)
217 {
218   SelectMgr_EntityOwner::SetLocation(aLoc);
219   // we must not nullify the myPrsSh here, because unhilight method
220   // will be working with wrong entity in this case, the best is to
221   // set the update flag and then recompute myPrsSh on hilighting
222   if (!myPrsSh.IsNull())
223     myPrsSh->SetToUpdate();
224 }
225
226 void StdSelect_BRepOwner::ResetLocation()
227 {
228   SelectMgr_EntityOwner::ResetLocation();
229   // we must not nullify the myPrsSh here, because unhilight method
230   // will be working with wrong entity in this case, the best is to
231   // set the update flag and then recompute myPrsSh on hilighting
232   if (!myPrsSh.IsNull())
233     myPrsSh->SetToUpdate();
234 }
235
236 //=======================================================================
237 //function : SetZLayer
238 //purpose  :
239 //=======================================================================
240 void StdSelect_BRepOwner::SetZLayer (const Graphic3d_ZLayerId theLayerId)
241 {
242   if (!myPrsSh.IsNull())
243   {
244     myPrsSh->SetZLayer (theLayerId);
245   }
246 }
247
248 //=======================================================================
249 //function : UpdateHighlightTrsf
250 //purpose  :
251 //=======================================================================
252 void StdSelect_BRepOwner::UpdateHighlightTrsf (const Handle(V3d_Viewer)& theViewer,
253                                                const Handle(PrsMgr_PresentationManager3d)& theManager,
254                                                const Standard_Integer theDispMode)
255 {
256   if (myPrsSh.IsNull() && Selectable().IsNull())
257     return;
258
259   theManager->UpdateHighlightTrsf (theViewer, Selectable(), theDispMode, myPrsSh);
260 }