0025552: Visualization - provide the way to hide the object in specified view of...
[occt.git] / src / Graphic3d / Graphic3d_StructureManager.cxx
1 // Created by: NW,JPB,CAL
2 // Copyright (c) 1991-1999 Matra Datavision
3 // Copyright (c) 1999-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 //-Version      
17
18 //-Design       Declaration of variables specific to managers
19
20 //-Warning      Manager manages a set of structures
21
22 //-References   
23
24 //-Language     C++ 2.0
25
26 //-Declarations
27
28 // for the class
29 #include <Graphic3d_StructureManager.ixx>
30 #include <Graphic3d_StructureManager.pxx>
31 static Standard_Boolean Initialisation = Standard_True;
32 static int StructureManager_ArrayId[StructureManager_MAX];
33 static Standard_Integer StructureManager_CurrentId = 0;
34
35 #include <Graphic3d_Structure.pxx>
36 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
37
38 Graphic3d_StructureManager::Graphic3d_StructureManager (const Handle(Graphic3d_GraphicDriver)& theDriver)
39 {
40
41 Standard_Real Coef;
42 Standard_Integer i;
43 Standard_Boolean NotFound       = Standard_True;
44 Standard_Integer Limit  = Graphic3d_StructureManager::Limit ();
45
46         /* Initialize PHIGS and start up */
47         if (Initialisation) {
48
49                 Initialisation = Standard_False;
50                 /* table to manage IDs of StructureManager */
51                 for (i=0; i<Limit; i++) StructureManager_ArrayId[i]    = 0;
52
53                 StructureManager_CurrentId      = 0;
54                 StructureManager_ArrayId[0]     = 1;
55
56         }
57         else {
58                 for (i=0; i<Limit && NotFound; i++)
59                         if (StructureManager_ArrayId[i] == 0) {
60                                 NotFound        = Standard_False;
61                                 StructureManager_CurrentId      = i;
62                                 StructureManager_ArrayId[i]     = 1;
63                         }
64
65                 if (NotFound)
66                 {
67                   Standard_SStream anErrorDescription;
68                   anErrorDescription<<"You are trying to create too many ViewManagers at the same time!\n"<<
69                     "The number of simultaneously created ViewManagers can't exceed "<<Limit<<".\n";
70                   Graphic3d_InitialisationError::Raise(anErrorDescription);
71                 }
72         }
73
74         Coef            = (Structure_IDMIN+Structure_IDMAX)/Limit;
75         Aspect_GenId theGenId(
76           Standard_Integer (Structure_IDMIN+Coef*(StructureManager_CurrentId)),
77           Standard_Integer (Structure_IDMIN+Coef*(StructureManager_CurrentId+1)-1));
78         MyStructGenId   = theGenId;
79
80         MyId                    = StructureManager_CurrentId;
81
82         MyAspectLine3d          = new Graphic3d_AspectLine3d ();
83         MyAspectText3d          = new Graphic3d_AspectText3d ();
84         MyAspectMarker3d        = new Graphic3d_AspectMarker3d ();
85         MyAspectFillArea3d      = new Graphic3d_AspectFillArea3d ();
86
87         MyUpdateMode            = Aspect_TOU_WAIT;
88         MyGraphicDriver         = theDriver;
89
90 }
91
92 //-Destructors
93
94 void Graphic3d_StructureManager::Destroy () {
95
96         MyDisplayedStructure.Clear ();
97         MyHighlightedStructure.Clear ();
98         StructureManager_ArrayId[MyId]  = 0;
99
100 }
101
102 //-Methods, in order
103
104 void Graphic3d_StructureManager::SetUpdateMode (const Aspect_TypeOfUpdate AType) {
105
106         MyUpdateMode    = AType;
107
108 }
109
110 Aspect_TypeOfUpdate Graphic3d_StructureManager::UpdateMode () const {
111
112         return (MyUpdateMode);
113
114 }
115
116 void Graphic3d_StructureManager::SetPrimitivesAspect (const Handle(Graphic3d_AspectLine3d)& CTX) {
117
118         MyAspectLine3d          = CTX;
119
120 }
121
122 void Graphic3d_StructureManager::SetPrimitivesAspect (const Handle(Graphic3d_AspectFillArea3d)& CTX) {
123
124         MyAspectFillArea3d      = CTX;
125
126 }
127
128 void Graphic3d_StructureManager::SetPrimitivesAspect (const Handle(Graphic3d_AspectText3d)& CTX) {
129
130         MyAspectText3d          = CTX;
131
132 }
133
134 void Graphic3d_StructureManager::SetPrimitivesAspect (const Handle(Graphic3d_AspectMarker3d)& CTX) {
135
136         MyAspectMarker3d        = CTX;
137
138 }
139
140 void Graphic3d_StructureManager::PrimitivesAspect (Handle(Graphic3d_AspectLine3d)& CTXL, Handle(Graphic3d_AspectText3d)& CTXT, Handle(Graphic3d_AspectMarker3d)& CTXM, Handle(Graphic3d_AspectFillArea3d)& CTXF) const {
141
142         CTXL    = MyAspectLine3d;
143         CTXT    = MyAspectText3d;
144         CTXM    = MyAspectMarker3d;
145         CTXF    = MyAspectFillArea3d;
146
147 }
148
149 Handle(Graphic3d_AspectLine3d) Graphic3d_StructureManager::Line3dAspect () const {
150
151         return (MyAspectLine3d);
152
153 }
154
155 Handle(Graphic3d_AspectText3d) Graphic3d_StructureManager::Text3dAspect () const {
156
157         return (MyAspectText3d);
158
159 }
160
161 Handle(Graphic3d_AspectMarker3d) Graphic3d_StructureManager::Marker3dAspect () const {
162
163         return (MyAspectMarker3d);
164
165 }
166
167 Handle(Graphic3d_AspectFillArea3d) Graphic3d_StructureManager::FillArea3dAspect () const {
168
169         return (MyAspectFillArea3d);
170
171 }
172
173 void Graphic3d_StructureManager::Remove (const Standard_Integer AnId) {
174
175         MyStructGenId.Free (AnId);
176
177 }
178
179 void Graphic3d_StructureManager::DisplayedStructures (Graphic3d_MapOfStructure& SG) const {
180
181   SG.Assign(MyDisplayedStructure);
182
183   //JMBStandard_Integer Length  = MyDisplayedStructure.Length ();
184
185   //JMBfor (Standard_Integer i=1; i<=Length; i++)
186   //JMB SG.Add (MyDisplayedStructure.Value (i));
187
188 }
189
190 Standard_Integer Graphic3d_StructureManager::NumberOfDisplayedStructures () const {
191
192 Standard_Integer Length = MyDisplayedStructure.Extent ();
193
194         return (Length);
195
196 }
197
198 //Handle(Graphic3d_Structure) Graphic3d_StructureManager::DisplayedStructure (const Standard_Integer AnIndex) const {
199
200 //return (MyDisplayedStructure.Value (AnIndex));
201
202 //}
203
204 void Graphic3d_StructureManager::HighlightedStructures (Graphic3d_MapOfStructure& SG) const {
205
206   SG.Assign(MyHighlightedStructure);
207
208 }
209
210 Standard_Integer Graphic3d_StructureManager::NewIdentification () {
211
212 Standard_Integer Id     = MyStructGenId.Next ();
213
214         return Id;
215
216 }
217
218 Handle(Graphic3d_Structure) Graphic3d_StructureManager::Identification (const Standard_Integer AId) const {
219
220 //  Standard_Integer ind=0;
221   Standard_Boolean notfound     = Standard_True;
222
223   Handle(Graphic3d_Structure) StructNull;
224  
225   Graphic3d_MapIteratorOfMapOfStructure it( MyDisplayedStructure);
226   
227   Handle(Graphic3d_Structure) SGfound;
228
229   for (; it.More() && notfound; it.Next()) {
230     Handle(Graphic3d_Structure) SG = it.Key();
231     if ( SG->Identification () == AId) {
232       notfound  = Standard_False;
233       SGfound = SG;
234     }
235   }
236
237
238   if (notfound)
239     return (StructNull);
240   else
241     return (SGfound);
242
243 }
244
245 Standard_Integer Graphic3d_StructureManager::Identification () const {
246  
247         return (MyId);
248  
249 }
250
251 Standard_Integer Graphic3d_StructureManager::Limit () {
252
253         return (StructureManager_MAX);
254
255 }
256
257 Standard_Integer Graphic3d_StructureManager::CurrentId () {
258
259         return (StructureManager_CurrentId);
260
261 }
262
263 const Handle(Graphic3d_GraphicDriver)& Graphic3d_StructureManager::GraphicDriver () const {
264
265         return (MyGraphicDriver);
266
267 }
268
269 void Graphic3d_StructureManager::RecomputeStructures()
270 {
271   // Go through all unique structures including child (connected) ones and ensure that they are computed.
272   Graphic3d_MapOfStructure aStructNetwork;
273
274   for (Graphic3d_MapIteratorOfMapOfStructure anIter(MyDisplayedStructure); anIter.More(); anIter.Next())
275   {
276     Handle(Graphic3d_Structure) aStructure = anIter.Key();
277     anIter.Key()->Network (anIter.Key(), Graphic3d_TOC_DESCENDANT, aStructNetwork);
278   }
279
280   RecomputeStructures (aStructNetwork);
281 }
282
283 void Graphic3d_StructureManager::RecomputeStructures (const Graphic3d_MapOfStructure& theStructures)
284 {
285   for (Graphic3d_MapIteratorOfMapOfStructure anIter (theStructures); anIter.More(); anIter.Next())
286   {
287     Handle(Graphic3d_Structure) aStruct = anIter.Key();
288     aStruct->Clear();
289     aStruct->Compute();
290   }
291 }
292
293 Handle(Graphic3d_ViewAffinity) Graphic3d_StructureManager::RegisterObject (const Handle(Standard_Transient)& theObject)
294 {
295   Handle(Graphic3d_ViewAffinity) aResult;
296   if (myRegisteredObjects.Find (theObject.operator->(), aResult))
297   {
298     return aResult;
299   }
300
301   aResult = new Graphic3d_ViewAffinity();
302   myRegisteredObjects.Bind (theObject.operator->(), aResult);
303   return aResult;
304 }
305
306 void Graphic3d_StructureManager::UnregisterObject (const Handle(Standard_Transient)& theObject)
307 {
308   myRegisteredObjects.UnBind (theObject.operator->());
309 }
310
311 Handle(Graphic3d_ViewAffinity) Graphic3d_StructureManager::ObjectAffinity (const Handle(Standard_Transient)& theObject) const
312 {
313   Handle(Graphic3d_ViewAffinity) aResult;
314   myRegisteredObjects.Find (theObject.operator->(), aResult);
315   return aResult;
316 }