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