3a01f700a6265e72b43478b7707d00ab8074d892
[occt.git] / src / MeshVS / MeshVS_MeshOwner.cxx
1 // Created on: 2007-01-25
2 // Created by: Sergey KOCHETKOV
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <MeshVS_DataSource.hxx>
18 #include <MeshVS_Mesh.hxx>
19 #include <MeshVS_MeshOwner.hxx>
20 #include <PrsMgr_PresentationManager.hxx>
21 #include <PrsMgr_PresentationManager3d.hxx>
22 #include <SelectBasics_EntityOwner.hxx>
23 #include <SelectMgr_SelectableObject.hxx>
24 #include <Standard_Type.hxx>
25 #include <TColStd_HPackedMapOfInteger.hxx>
26 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
27
28 IMPLEMENT_STANDARD_RTTIEXT(MeshVS_MeshOwner,SelectMgr_EntityOwner)
29
30 #ifndef MeshVS_PRSBUILDERHXX
31 #include <MeshVS_PrsBuilder.hxx>
32 #endif
33
34
35 //================================================================
36 // Function : Constructor MeshVS_MeshOwner
37 // Purpose  :
38 //================================================================
39 MeshVS_MeshOwner::MeshVS_MeshOwner (const SelectMgr_SOPtr&           theSelObj,
40                                     const Handle(MeshVS_DataSource)& theDS,
41                                     const Standard_Integer           thePriority)
42 : SelectMgr_EntityOwner ( theSelObj, thePriority )
43 {
44   myLastID = -1;
45   if( !theDS.IsNull() )
46     myDataSource = theDS;
47   SelectBasics_EntityOwner::Set ( thePriority );
48 }
49
50 //================================================================
51 // Function : GetDataSource
52 // Purpose  :
53 //================================================================
54 const Handle(MeshVS_DataSource)& MeshVS_MeshOwner::GetDataSource () const
55 {
56   return myDataSource;
57 }
58
59 //================================================================
60 // Function : GetSelectedNodes
61 // Purpose  :
62 //================================================================
63 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetSelectedNodes () const
64 {
65   return mySelectedNodes;
66 }
67
68 //================================================================
69 // Function : GetSelectedElements
70 // Purpose  :
71 //================================================================
72 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetSelectedElements () const
73 {
74   return mySelectedElems;
75 }
76
77 //================================================================
78 // Function : AddSelectedEntities
79 // Purpose  :
80 //================================================================
81 void MeshVS_MeshOwner::AddSelectedEntities (const Handle(TColStd_HPackedMapOfInteger)& Nodes,
82                                             const Handle(TColStd_HPackedMapOfInteger)& Elems)
83 {
84   if( mySelectedNodes.IsNull() )
85     mySelectedNodes = Nodes;
86   else if( !Nodes.IsNull() )
87     mySelectedNodes->ChangeMap().Unite( Nodes->Map() );
88   if( mySelectedElems.IsNull() )
89     mySelectedElems = Elems;
90   else if( !Elems.IsNull() )
91     mySelectedElems->ChangeMap().Unite( Elems->Map() );
92 }
93
94 //================================================================
95 // Function : ClearSelectedEntities
96 // Purpose  :
97 //================================================================
98 void MeshVS_MeshOwner::ClearSelectedEntities ()
99 {
100   mySelectedNodes.Nullify();
101   mySelectedElems.Nullify();
102 }
103
104 //================================================================
105 // Function : GetDetectedNodes
106 // Purpose  :
107 //================================================================
108 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetDetectedNodes () const
109 {
110   return myDetectedNodes;
111 }
112
113 //================================================================
114 // Function : GetDetectedElements
115 // Purpose  :
116 //================================================================
117 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetDetectedElements () const
118 {
119   return myDetectedElems;
120 }
121
122 //================================================================
123 // Function : SetDetectedEntities
124 // Purpose  :
125 //================================================================
126 void MeshVS_MeshOwner::SetDetectedEntities (const Handle(TColStd_HPackedMapOfInteger)& Nodes,
127                                             const Handle(TColStd_HPackedMapOfInteger)& Elems)
128 {
129   myDetectedNodes = Nodes;
130   myDetectedElems = Elems;
131   if (IsSelected()) SetSelected (Standard_False);
132 }
133
134 //================================================================
135 // Function : HilightWithColor
136 // Purpose  :
137 //================================================================
138 void MeshVS_MeshOwner::HilightWithColor (const Handle(PrsMgr_PresentationManager3d)& thePM,
139                                          const Quantity_NameOfColor theColor,
140                                          const Standard_Integer /*theMode*/)
141 {
142   Handle( SelectMgr_SelectableObject ) aSelObj;
143   if ( HasSelectable() )
144     aSelObj = Selectable();
145
146   if ( thePM->IsImmediateModeOn() && aSelObj->IsKind( STANDARD_TYPE( MeshVS_Mesh ) ) )
147   {
148     // Update last detected entity ID
149     Handle(TColStd_HPackedMapOfInteger) aNodes = GetDetectedNodes();
150     Handle(TColStd_HPackedMapOfInteger) aElems = GetDetectedElements(); 
151     if( !aNodes.IsNull() && aNodes->Map().Extent() == 1 )
152     {
153       TColStd_MapIteratorOfPackedMapOfInteger anIt( aNodes->Map() );
154       if( myLastID != anIt.Key() )
155       {
156         myLastID = anIt.Key();
157       }
158     }
159     else if( !aElems.IsNull() && aElems->Map().Extent() == 1 )
160     {
161       TColStd_MapIteratorOfPackedMapOfInteger anIt( aElems->Map() );
162       if( myLastID != anIt.Key() )
163       {
164         myLastID = anIt.Key();
165       }
166     }
167
168     // hilight detected entities
169     Handle( MeshVS_Mesh ) aMesh = Handle( MeshVS_Mesh )::DownCast ( aSelObj );
170     aMesh->HilightOwnerWithColor ( thePM, theColor, this );
171   }
172 }
173
174 void MeshVS_MeshOwner::Unhilight(const Handle(PrsMgr_PresentationManager)& thePM,
175                                  const Standard_Integer theMode)
176 {
177   SelectMgr_EntityOwner::Unhilight( thePM, theMode );
178
179   Handle(TColStd_HPackedMapOfInteger) aNodes = GetDetectedNodes();
180   Handle(TColStd_HPackedMapOfInteger) aElems = GetDetectedElements();  
181   if( ( !aNodes.IsNull() && !aNodes->Map().Contains( myLastID ) ) ||
182       ( !aElems.IsNull() && !aElems->Map().Contains( myLastID ) ) )
183     return;
184   // Reset last detected ID
185   myLastID = -1;
186 }
187
188 Standard_Boolean MeshVS_MeshOwner::IsForcedHilight () const
189 {
190   Standard_Boolean aHilight = Standard_True;
191   Standard_Integer aKey = -1;
192   if( myLastID > 0 )
193   {
194     // Check the detected entity and 
195     // allow to hilight it if it differs from the last detected entity <myLastID>
196     Handle(TColStd_HPackedMapOfInteger) aNodes = GetDetectedNodes();
197     if( !aNodes.IsNull() && aNodes->Map().Extent() == 1 )
198     {
199       TColStd_MapIteratorOfPackedMapOfInteger anIt( aNodes->Map() );
200       aKey = anIt.Key();
201       if( myLastID == aKey )
202       {  
203          aHilight = Standard_False;
204       }
205     }  
206     Handle(TColStd_HPackedMapOfInteger) aElems = GetDetectedElements();
207     if( !aElems.IsNull() && aElems->Map().Extent() == 1 )
208     {
209       TColStd_MapIteratorOfPackedMapOfInteger anIt( aElems->Map() );
210       aKey = anIt.Key();
211       if( myLastID == aKey )
212       {
213           aHilight = Standard_False;
214       }
215     }
216   } 
217   return aHilight;
218 }