summaryrefslogtreecommitdiff
path: root/src/DPrsStd/DPrsStd_Sample.cxx
blob: 6c4d9567958d1350c2355b91b7aab304f6e969d0 (plain)
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
// File:	DPrsStd_Sample.cxx
// Created:	Mon Dec 27 16:10:35 1999
// Author:	Sergey RUIN
//		<srn@popox.nnov.matra-dtv.fr>



#include <TDF_Data.hxx>
#include <TDF_Label.hxx>
#include <TNaming_NamedShape.hxx>
#include <TPrsStd_AISPresentation.hxx>
#include <TPrsStd_AISViewer.hxx>
#include <AIS_InteractiveContext.hxx>
#include <AIS_InteractiveObject.hxx>
#include <V3d_Viewer.hxx>
#include <Quantity_NameOfColor.hxx>
#include <TPrsStd_DriverTable.hxx>
#include <TPrsStd_NamedShapeDriver.hxx>
#include <TPrsStd_PlaneDriver.hxx>
#include <TDataXtd_Plane.hxx>

#ifdef DEB
static void Sample()
{
  // Starting with data framework 
  Handle(TDF_Data) DF = new TDF_Data();
  TDF_Label aLabel = DF->Root();

  //----------------------------------- TPrsStd_AISViewer ----------------------------------------
  //==============================================================================================

  // Setting the TPrsStd_AISViewer in the framework

  Handle(V3d_Viewer) aViewer;

  //... Initialization of aViewer

  //Creating the new AIS_InteractiveContext 
  Handle(AIS_InteractiveContext) ctx = new AIS_InteractiveContext(aViewer);

  //Creating the new TPrsStd_AISViewer attribute initialized with AIS_InteractiveContext
  Handle(TPrsStd_AISViewer) aisviewer;

  if( !TPrsStd_AISViewer::Has(aLabel) ) { //Check if there has already been set TPrsStd_AISViewer in the framework
    aisviewer = TPrsStd_AISViewer::New(aLabel, ctx);
  }

  //Finding TPrsStd_AISViewer attribute in the DataFramework 
  if( TPrsStd_AISViewer::Find(aLabel, aisviewer) ) {
    aisviewer->Update(); //Update the viewer associated with this attribute
  }

  //Getting AIS_InteractiveContext from TPrsStd_AISViewer may be done in two ways: 

  //1. If we have already gotten TPrsStd_AISViewer attribute (strored in a variable <aisviewer>)
  Handle(AIS_InteractiveContext) context1 = aisviewer->GetInteractiveContext();
 
  //2. Getting AIS_InteractiveContext directly
  Handle(AIS_InteractiveContext) context2;
  if( TPrsStd_AISViewer::Find(aLabel, context2) ) {
    //do something...
  }

  //----------------------------------- TPrsStd_Driver and TPrsStd_DriverTable -------------------
  //==============================================================================================

  // All work for building AIS_InteractiveObject to be presented by TPrsStd_AISPresentation is done
  // by drivers which are descendants of deferred class TPrsStd_Driver

  // There is a map of drivers with Standard_GUID as a key. 

  // Adding driver to the map of drivers

  Handle(TPrsStd_NamedShapeDriver) NSDriver = new TPrsStd_NamedShapeDriver();
 
  Handle(TPrsStd_DriverTable) table  = TPrsStd_DriverTable::Get();
 
  Standard_GUID guid = TNaming_NamedShape::GetID();

  table->AddDriver(guid, NSDriver);  

  // When the first time called TPrsStd_DriverTable loads standard drivers defined in TPrsStd package

  // Getting driver from the map of drivers

  Standard_GUID driverguid = TNaming_NamedShape::GetID();
  
  Handle(TPrsStd_NamedShapeDriver) driver;

  if( table->FindDriver(driverguid,  driver) ) 
    cout << "Driver was found " << endl;
  else 
    cout << "Driver wasn't found" << endl;

  // Driver can be used to build AIS_InteractiveObject for presenting the given label

  Handle(TPrsStd_PlaneDriver) planedriver;

  if( table->FindDriver(TDataXtd_Plane::GetID(),  planedriver) ) {
    
    TDF_Label planelabel;

    // Finding planelabel ... 

    Handle(AIS_InteractiveObject) aisobject;

    planedriver->Update(planelabel, aisobject);

    if( !aisobject.IsNull() ) {

      // Do something with aisobject ...

    }
  } 

  //----------------------------------- TPrsStd_AISPresentation ----------------------------------
  //==============================================================================================


  TDF_Label ShapeLabel;
  
  // ... Setting TNaming_NamedShape to <ShapeLabel>

  // Setting the new  TPrsStd_AISPresentation to <ShapeLabel>
  // It can be done in two different ways:

  Handle(TPrsStd_AISPresentation) Presenation;
  //  1. By giving to TPrsStd_AISPresentation attribute Standard_GUID of an attribute  to be displayed:
  //  This GUID will be used to find driver for building AIS_InteractiveObject in the map of drivers
        
  Presenation = TPrsStd_AISPresentation::Set( ShapeLabel, TNaming_NamedShape::GetID() );
 
  //  2. Or by giving the attribute itself to TPrsStd_AISPresentation attribute:
  //  An ID of attribute  will be used to find driver for building AIS_InteractiveObject in the map of drivers

  Handle(TNaming_NamedShape) NS;
  if( ShapeLabel.FindAttribute( TNaming_NamedShape::GetID(), NS) ) {
    Presenation = TPrsStd_AISPresentation::Set( NS );
  }


  // Displaying (recomputation of presentation of attribute is done only if presentation is null)

  Handle(TPrsStd_AISPresentation) PRS;
  
  if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), PRS) ) PRS->Display();
  //After call of the method PRS->Display() the presentation of the attribute is marked as displayed in 
  //AIS_InteractiveContext  but not in viewer, in order to draw the object in viewer last has to be updated

  TPrsStd_AISViewer::Update(ShapeLabel);  //Update presentation of the attribute in a viewer's window

  // Erasing
  
  if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), PRS) ) PRS->Erase();
  // The method Erase() marks presentation of attribute as erased in AIS_InteractiveContext; 
  // in order to make changes visible in a viewer's window viewer has to be updated  
  TPrsStd_AISViewer::Update(ShapeLabel);  //Update viewer to erase presenation of the attribute in a viewer's window
                                          //Presentation of the attribute is erased from viewer but
                                          // stays in AIS_InteractiveContext
 
  if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), PRS) ) PRS->Erase(Standard_True); 
  TPrsStd_AISViewer::Update(ShapeLabel);
                                        //Presentation of the attribute is erased
                                        //from viewer and removed from AIS_InteractiveContext

  //Checking if attribute is displayed in viewer 

  if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), PRS) ) 
  Standard_Boolean isDisplayed = PRS->IsDisplayed();

  
  Handle(TPrsStd_AISPresentation) P;
  if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), P) ) {

    // Updating and displaying presentation of the attribute to be displayed 

    P->Display(Standard_True); 
    TPrsStd_AISViewer::Update(ShapeLabel);  //Update presenation of the attribute in a viewer's window

    //Setting a color to the displayd attribute

    P->SetColor(Quantity_NOC_RED);
    TPrsStd_AISViewer::Update(ShapeLabel);  //Update viewer to make changes visible to user

    //Getting transparency the displayd attribute

    Standard_Real transparency = P->Transparency();

    //Getting AIS_InteractiveObject built and stored in the AIS_Presentation attribute

    Handle(AIS_InteractiveObject) AISObject = P->GetAIS();
  }

  // ... Attribute is modified  


  //Updating presentation of the attribute in viewer

  if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), PRS) )
  PRS->Update(); //Updates presentation of attribute in AIS_InteractiveContext
  TPrsStd_AISViewer::Update(ShapeLabel); //Updates presentation in viewer

  return; 
}

#endif