0024047: Exception in TPrsStd_AISPresentation during destruction of TDocStd_Document
[occt.git] / src / AIS / AIS_ExclusionFilter.cxx
1 // Created on: 1997-11-28
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22
23 #include <AIS_ExclusionFilter.ixx>
24 #include <TColStd_ListOfInteger.hxx>
25 #include <TColStd_ListIteratorOfListOfInteger.hxx>
26 #include <TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger.hxx>
27 #include <AIS_InteractiveObject.hxx>
28
29 //=======================================================================
30 //function : AIS_ExclusionFilter
31 //purpose  : Constructors
32 //=======================================================================
33
34 AIS_ExclusionFilter::AIS_ExclusionFilter(const Standard_Boolean ExclusionFlagOn):
35 myIsExclusionFlagOn(ExclusionFlagOn)
36 {
37 }
38
39 AIS_ExclusionFilter::AIS_ExclusionFilter(const AIS_KindOfInteractive TypeToExclude,
40                                          const Standard_Boolean ExclusionFlagOn):
41 myIsExclusionFlagOn(ExclusionFlagOn)
42 {
43   TColStd_ListOfInteger L;
44   myStoredTypes.Bind((Standard_Integer)TypeToExclude,L);
45 }
46
47 AIS_ExclusionFilter::AIS_ExclusionFilter(const AIS_KindOfInteractive TypeToExclude,
48                                          const Standard_Integer SignatureInType,
49                                          const Standard_Boolean ExclusionFlagOn):
50 myIsExclusionFlagOn(ExclusionFlagOn)
51 {
52   TColStd_ListOfInteger L;
53   L.Append(SignatureInType);
54   myStoredTypes.Bind((Standard_Integer)TypeToExclude,L);
55 }
56
57 //=======================================================================
58 //function : Add
59 //purpose  : 
60 //=======================================================================
61 Standard_Boolean AIS_ExclusionFilter::Add(const AIS_KindOfInteractive TypeToExclude) 
62 {
63   if(IsStored(TypeToExclude)) 
64     return Standard_False;
65   TColStd_ListOfInteger L;
66   myStoredTypes.Bind((Standard_Integer)TypeToExclude,L);
67   return Standard_True;
68 }
69
70 Standard_Boolean AIS_ExclusionFilter::Add(const AIS_KindOfInteractive TypeToExclude,
71                                           const Standard_Integer SignatureInType) 
72 {
73   if(!IsStored(TypeToExclude)){
74     TColStd_ListOfInteger L;
75     L.Append(SignatureInType);
76     myStoredTypes.Bind((Standard_Integer)TypeToExclude,L);
77     return Standard_True;
78   }
79
80   myStoredTypes((Standard_Integer)TypeToExclude).Append(SignatureInType);
81   return Standard_True;
82 }
83
84 //=======================================================================
85 //function : Remove
86 //purpose  : 
87 //=======================================================================
88
89 Standard_Boolean AIS_ExclusionFilter::Remove(const AIS_KindOfInteractive TypeToExclude) 
90 {
91   if(!IsStored(TypeToExclude)) return Standard_False;
92   myStoredTypes((Standard_Integer)TypeToExclude).Clear();
93   myStoredTypes.UnBind((Standard_Integer)TypeToExclude);
94   return Standard_True;
95 }
96
97 Standard_Boolean AIS_ExclusionFilter::Remove(const AIS_KindOfInteractive TypeToExclude,
98                                              const Standard_Integer SignatureInType) 
99 {
100   if(!IsStored(TypeToExclude)) return Standard_False;
101   TColStd_ListOfInteger& LL = myStoredTypes.ChangeFind((Standard_Integer)TypeToExclude);
102   for(TColStd_ListIteratorOfListOfInteger it(LL);it.More();it.Next()){
103     if(it.Value()==SignatureInType){
104       LL.Remove(it);
105       return Standard_True;
106     }
107   }
108   return Standard_False;
109 }
110
111
112 //=======================================================================
113 //function : Clear
114 //purpose  : 
115 //=======================================================================
116
117 void  AIS_ExclusionFilter::Clear()
118 {
119   TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger Mit(myStoredTypes);
120   for(;Mit.More();Mit.Next())
121     myStoredTypes.ChangeFind(Mit.Key()).Clear();
122   myStoredTypes.Clear();
123 }
124
125 //=======================================================================
126 //function : IsStored
127 //purpose  : 
128 //=======================================================================
129
130 Standard_Boolean AIS_ExclusionFilter::IsStored(const AIS_KindOfInteractive aType) const
131 {
132   return myStoredTypes.IsBound(Standard_Integer(aType));
133 }
134
135 //=======================================================================
136 //function : IsSignatureIn
137 //purpose  : 
138 //=======================================================================
139 Standard_Boolean AIS_ExclusionFilter::IsSignatureIn(const AIS_KindOfInteractive aType,
140                                                     const Standard_Integer SignatureInType) const
141 {
142   if(!myStoredTypes.IsBound(aType)) return Standard_False;
143   for(TColStd_ListIteratorOfListOfInteger Lit(myStoredTypes((Standard_Integer)aType));
144       Lit.More();
145       Lit.Next()){
146     if(Lit.Value()==SignatureInType)
147       return Standard_True;
148   }
149   return Standard_False;
150 }
151
152 //=======================================================================
153 //function : ListOfStoredTypes
154 //purpose  : 
155 //=======================================================================
156
157 void AIS_ExclusionFilter::ListOfStoredTypes(TColStd_ListOfInteger& TheList) const
158 {
159   TheList.Clear();
160   TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger MIT(myStoredTypes);
161   for(;MIT.More();MIT.Next())
162     TheList.Append(MIT.Key());
163 }
164
165 //=======================================================================
166 //function : ListOfSignature
167 //purpose  : 
168 //=======================================================================
169
170 void AIS_ExclusionFilter::ListOfSignature(const AIS_KindOfInteractive aType,TColStd_ListOfInteger& TheStoredList) const
171 {
172   TheStoredList.Clear();
173   if(IsStored(aType))
174     for(TColStd_ListIteratorOfListOfInteger it(myStoredTypes(aType));it.More();it.Next())
175       TheStoredList.Append(it.Value());
176 }
177
178 //=======================================================================
179 //function : IsOk
180 //purpose  : 
181 //=======================================================================
182
183 Standard_Boolean AIS_ExclusionFilter::IsOk(const Handle(SelectMgr_EntityOwner)& EO) const
184 {
185   if(myStoredTypes.IsEmpty())
186     return myIsExclusionFlagOn;
187
188   if(EO.IsNull()) 
189     return Standard_False;
190   Handle(AIS_InteractiveObject) IO = Handle(AIS_InteractiveObject)::DownCast(EO->Selectable());
191   if(IO.IsNull()) 
192     return Standard_False;
193
194   // type of AIS is not in the map...
195   if(!myStoredTypes.IsBound(IO->Type()))
196     return myIsExclusionFlagOn ;
197   // type of AIS is not in the map and there is no signature indicated
198   if(myStoredTypes(IO->Type()).IsEmpty())
199     return !myIsExclusionFlagOn ;
200   // one or several signatures are indicated...
201   if(IsSignatureIn(IO->Type(),IO->Signature()))
202     return !myIsExclusionFlagOn;
203   
204   return myIsExclusionFlagOn;
205 }
206
207
208
209
210
211
212