File: View_Displayer.cxx

package info (click to toggle)
opencascade 7.9.2%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 301,924 kB
  • sloc: cpp: 1,523,264; tcl: 10,159; cs: 5,173; java: 1,554; sh: 1,342; ansic: 827; xml: 699; makefile: 30; javascript: 22
file content (436 lines) | stat: -rw-r--r-- 14,806 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Created on: 2017-06-16
// Created by: Natalia ERMOLAEVA
// Copyright (c) 2017 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.

#include <inspector/View_Displayer.hxx>

#include <AIS_InteractiveContext.hxx>
#include <AIS_InteractiveObject.hxx>
#include <AIS_ListIteratorOfListOfInteractive.hxx>
#include <AIS_Shape.hxx>
#include <AIS_Trihedron.hxx>
#include <AIS_ViewCube.hxx>
#include <Geom_Axis2Placement.hxx>
#include <Prs3d_PointAspect.hxx>
#include <V3d_View.hxx>
#include <V3d_Viewer.hxx>

#include <inspector/View_DisplayPreview.hxx>
#include <inspector/View_Viewer.hxx>

// =======================================================================
// function : Constructor
// purpose :
// =======================================================================
View_Displayer::View_Displayer()
    : myIsKeepPresentations(false),
      myFitAllActive(false),
      myDisplayMode(0)
{
  myDisplayPreview = new View_DisplayPreview();
}

// =======================================================================
// function : SetContext
// purpose :
// =======================================================================
void View_Displayer::SetContext(const Handle(AIS_InteractiveContext)& theContext)
{
  NCollection_DataMap<View_PresentationType, NCollection_Shared<AIS_ListOfInteractive>> aDisplayed =
    myDisplayed;
  EraseAllPresentations(true);
  myContext = theContext;

  for (NCollection_DataMap<View_PresentationType,
                           NCollection_Shared<AIS_ListOfInteractive>>::Iterator
         aDisplayedIt(aDisplayed);
       aDisplayedIt.More();
       aDisplayedIt.Next())
  {
    View_PresentationType aType = aDisplayedIt.Key();
    for (AIS_ListIteratorOfListOfInteractive aPresentationsIt(aDisplayedIt.Value());
         aPresentationsIt.More();
         aPresentationsIt.Next())
      DisplayPresentation(aPresentationsIt.Value(), aType, false);
  }
  myDisplayPreview->SetContext(theContext);
  UpdateViewer();
}

// =======================================================================
// function : SetDisplayMode
// purpose :
// =======================================================================
void View_Displayer::SetDisplayMode(const int                   theDisplayMode,
                                    const View_PresentationType theType,
                                    const bool                  theToUpdateViewer)
{
  myDisplayMode = theDisplayMode;
  if (GetContext().IsNull())
    return;

  NCollection_Shared<AIS_ListOfInteractive> aDisplayed;
  DisplayedPresentations(aDisplayed, theType);

  for (AIS_ListIteratorOfListOfInteractive aDisplayedIt(aDisplayed); aDisplayedIt.More();
       aDisplayedIt.Next())
    GetContext()->SetDisplayMode(aDisplayedIt.Value(), theDisplayMode, Standard_False);

  if (theToUpdateViewer)
    UpdateViewer();
}

// =======================================================================
// function : DisplayPresentation
// purpose :
// =======================================================================
void View_Displayer::DisplayPresentation(const Handle(Standard_Transient)& thePresentation,
                                         const View_PresentationType       theType,
                                         const bool                        theToUpdateViewer)
{
  if (GetContext().IsNull())
    return;

  NCollection_Shared<AIS_ListOfInteractive> aDisplayed;
  DisplayedPresentations(aDisplayed, theType);
  if (!myIsKeepPresentations)
    ErasePresentations(theType, false);

  Handle(AIS_InteractiveObject) aPresentation =
    Handle(AIS_InteractiveObject)::DownCast(thePresentation);
  if (!aPresentation.IsNull() && aPresentation->GetContext().IsNull())
  {
    // one presentation can not be shown in several contexts
    if (theType == View_PresentationType_Additional)
    {
      Quantity_Color aColor;
      if (myColorAttributes.Find(View_PresentationType_Additional, aColor))
        aPresentation->SetColor(aColor);
    }
    GetContext()->Display(aPresentation, false);
    if (myDisplayMode != -1)
      GetContext()->SetDisplayMode(aPresentation, myDisplayMode, false);
    aDisplayed.Append(aPresentation);
  }

  if (myFitAllActive)
    fitAllView();

  myDisplayed.Bind(theType, aDisplayed);

  if (theToUpdateViewer)
    UpdateViewer();
}

// =======================================================================
// function : RedisplayPresentation
// purpose :
// =======================================================================
void View_Displayer::RedisplayPresentation(const Handle(Standard_Transient)& thePresentation,
                                           const bool                        theToUpdateViewer)
{
  Handle(AIS_InteractiveObject) aPresentation =
    Handle(AIS_InteractiveObject)::DownCast(thePresentation);
  if (aPresentation.IsNull() || aPresentation->GetContext().IsNull())
    return;

  GetContext()->Redisplay(aPresentation, false);

  if (myFitAllActive)
    fitAllView();

  if (theToUpdateViewer)
    UpdateViewer();
}

// =======================================================================
// function : EraseAllPresentations
// purpose :
// =======================================================================
void View_Displayer::EraseAllPresentations(const bool theToUpdateViewer)
{
  for (NCollection_DataMap<View_PresentationType,
                           NCollection_Shared<AIS_ListOfInteractive>>::Iterator
         aDisplayedIt(myDisplayed);
       aDisplayedIt.More();
       aDisplayedIt.Next())
    ErasePresentations(aDisplayedIt.Key(), false);

  if (theToUpdateViewer)
    UpdateViewer();
}

// =======================================================================
// function : ErasePresentations
// purpose :
// =======================================================================
void View_Displayer::ErasePresentations(const View_PresentationType theType,
                                        const bool                  theToUpdateViewer)
{
  if (GetContext().IsNull())
    return;

  NCollection_Shared<AIS_ListOfInteractive> aDisplayed;
  DisplayedPresentations(aDisplayed, theType);

  for (AIS_ListIteratorOfListOfInteractive aDisplayedIt(aDisplayed); aDisplayedIt.More();
       aDisplayedIt.Next())
  {
    if (aDisplayedIt.Value()->IsKind(STANDARD_TYPE(AIS_Trihedron)))
      continue;

    GetContext()->Remove(aDisplayedIt.Value(), Standard_False);
  }

  aDisplayed.Clear();
  myDisplayed.Bind(theType, aDisplayed);

  if (theToUpdateViewer)
    UpdateViewer();
}

// =======================================================================
// function : ErasePresentation
// purpose :
// =======================================================================
void View_Displayer::ErasePresentation(const Handle(Standard_Transient)& thePresentation,
                                       const View_PresentationType       theType,
                                       const bool                        theToUpdateViewer)
{
  if (GetContext().IsNull())
    return;

  Handle(AIS_InteractiveObject) aPresentation =
    Handle(AIS_InteractiveObject)::DownCast(thePresentation);
  if (aPresentation.IsNull())
    return;

  GetContext()->Remove(aPresentation, Standard_False);

  NCollection_Shared<AIS_ListOfInteractive> aDisplayed;
  DisplayedPresentations(aDisplayed, theType);
  aDisplayed.Remove(aPresentation);
  myDisplayed.Bind(theType, aDisplayed);

  if (myFitAllActive)
    fitAllView();

  if (theToUpdateViewer)
    UpdateViewer();
}

// =======================================================================
// function : DisplayDefaultTrihedron
// purpose :
// =======================================================================
void View_Displayer::DisplayDefaultTrihedron(const Standard_Boolean toDisplay,
                                             const bool             theToUpdateViewer)
{
  const Handle(AIS_Trihedron)& aTrihedron = defaultTrihedron(toDisplay);
  if (aTrihedron.IsNull())
    return;

  if (toDisplay)
    GetContext()->Display(aTrihedron, theToUpdateViewer);
  else
    GetContext()->Erase(aTrihedron, theToUpdateViewer);
}

// =======================================================================
// function : DisplayViewCube
// purpose :
// =======================================================================
void View_Displayer::DisplayViewCube(const Standard_Boolean toDisplay, const bool theToUpdateViewer)
{
  if (myViewCube.IsNull() && toDisplay)
  {
    myViewCube = new AIS_ViewCube();
    myViewCube->SetSize(35.0);
    myViewCube->SetBoxColor(Quantity_NOC_GRAY50);
  }

  if (myViewCube.IsNull())
    return;

  if (toDisplay)
    GetContext()->Display(myViewCube, theToUpdateViewer);
  else
    GetContext()->Erase(myViewCube, theToUpdateViewer);
}

// =======================================================================
// function : SetVisible
// purpose :
// =======================================================================
void View_Displayer::SetVisible(const TopoDS_Shape&         theShape,
                                const bool                  theState,
                                const View_PresentationType theType)
{
  if (theShape.IsNull())
    return;

  if (theState)
    DisplayPresentation(CreatePresentation(theShape), View_PresentationType_Main, Standard_False);
  else
  {
    Handle(AIS_InteractiveObject) aPresentation = FindPresentation(theShape, theType);
    if (!aPresentation.IsNull())
      ErasePresentation(aPresentation, theType, Standard_False);
  }

  UpdateViewer();
}

// =======================================================================
// function : IsVisible
// purpose :
// =======================================================================
bool View_Displayer::IsVisible(const TopoDS_Shape&         theShape,
                               const View_PresentationType theType) const
{
  Handle(AIS_InteractiveObject) aPresentation = FindPresentation(theShape, theType);
  return !aPresentation.IsNull();
}

// =======================================================================
// function : UpdatePreview
// purpose :
// =======================================================================
void View_Displayer::UpdatePreview(
  const View_DisplayActionType                        theType,
  const NCollection_List<Handle(Standard_Transient)>& thePresentations)
{
  myDisplayPreview->UpdatePreview(theType, thePresentations);
  if (myFitAllActive)
    fitAllView();
}

// =======================================================================
// function : UpdateViewer
// purpose :
// =======================================================================
void View_Displayer::UpdateViewer()
{
  if (GetContext().IsNull())
    return;

  GetContext()->UpdateCurrentViewer();
}

// =======================================================================
// function : SetAttributeColor
// purpose :
// =======================================================================
void View_Displayer::SetAttributeColor(const Quantity_Color&       theColor,
                                       const View_PresentationType theType)
{
  myColorAttributes.Bind(theType, theColor);
}

// =======================================================================
// function : DisplayedPresentations
// purpose :
// =======================================================================
void View_Displayer::DisplayedPresentations(
  NCollection_Shared<AIS_ListOfInteractive>& thePresentations,
  const View_PresentationType                theType) const
{
  myDisplayed.Find(theType, thePresentations);
}

// =======================================================================
// function : getView
// purpose :
// =======================================================================
Handle(V3d_View) View_Displayer::GetView() const
{
  Handle(V3d_View) aView;
  if (GetContext().IsNull())
    return aView;

  const Handle(V3d_Viewer)& aViewer = GetContext()->CurrentViewer();
  if (!aViewer.IsNull())
  {
    if (!aViewer->ActiveViews().IsEmpty())
    {
      aView = aViewer->ActiveViews().First();
    }
  }
  return aView;
}

// =======================================================================
// function : FindPresentation
// purpose :
// =======================================================================
Handle(AIS_InteractiveObject) View_Displayer::FindPresentation(
  const TopoDS_Shape&         theShape,
  const View_PresentationType theType) const
{
  if (theShape.IsNull())
    return Handle(AIS_InteractiveObject)();

  NCollection_Shared<AIS_ListOfInteractive> aDisplayed;
  DisplayedPresentations(aDisplayed, theType);

  for (AIS_ListIteratorOfListOfInteractive aDisplayedIt(aDisplayed); aDisplayedIt.More();
       aDisplayedIt.Next())
  {
    Handle(AIS_Shape) aPresentation = Handle(AIS_Shape)::DownCast(aDisplayedIt.Value());
    if (aPresentation->Shape().IsEqual(theShape))
      return aPresentation;
  }

  return Handle(AIS_InteractiveObject)();
}

// =======================================================================
// function : CreatePresentation
// purpose :
// =======================================================================
Handle(Standard_Transient) View_Displayer::CreatePresentation(const TopoDS_Shape& theShape)
{
  Handle(AIS_Shape) aPresentation = new AIS_Shape(theShape);
  aPresentation->Attributes()->SetAutoTriangulation(Standard_False);

  return aPresentation;
}

// =======================================================================
// function : fitAllView
// purpose :
// =======================================================================
void View_Displayer::fitAllView()
{
  Handle(V3d_View) aView = GetView();
  if (!aView.IsNull())
  {
    aView->FitAll();
    aView->Redraw();
  }
}

// =======================================================================
// function : defaultTrihedron
// purpose :
// =======================================================================
const Handle(AIS_Trihedron)& View_Displayer::defaultTrihedron(const bool toCreate)
{
  if (myDefaultTrihedron.IsNull() && toCreate)
  {
    myDefaultTrihedron = new AIS_Trihedron(new Geom_Axis2Placement(gp::XOY()));
    myDefaultTrihedron->SetSize(1);
  }
  return myDefaultTrihedron;
}