CR24212: Moving OCAF samples from 'DRAW' component to OCCT samples.
[occt.git] / samples / ocafsamples / TPrsStd_Sample.cxx
1 // Created on: 1999-12-27
2 // Created by: Sergey RUIN
3 // Copyright (c) 1999-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
24 #include <TDF_Data.hxx>
25 #include <TDF_Label.hxx>
26 #include <TNaming_NamedShape.hxx>
27 #include <TPrsStd_AISPresentation.hxx>
28 #include <TPrsStd_AISViewer.hxx>
29 #include <AIS_InteractiveContext.hxx>
30 #include <AIS_InteractiveObject.hxx>
31 #include <V3d_Viewer.hxx>
32 #include <Quantity_NameOfColor.hxx>
33 #include <TPrsStd_DriverTable.hxx>
34 #include <TPrsStd_NamedShapeDriver.hxx>
35 #include <TPrsStd_PlaneDriver.hxx>
36 #include <TDataXtd_Plane.hxx>
37
38 // ====================================================================================
39 // This sample contains template for typical actions with OCAF visualization attributes
40 // ====================================================================================
41
42 #ifdef DEB
43 static void Sample()
44 {
45   // Starting with data framework 
46   Handle(TDF_Data) DF = new TDF_Data();
47   TDF_Label aLabel = DF->Root();
48
49   //----------------------------------- TPrsStd_AISViewer ----------------------------------------
50   //==============================================================================================
51
52   // Setting the TPrsStd_AISViewer in the framework
53
54   Handle(V3d_Viewer) aViewer;
55
56   //... Initialization of aViewer
57
58   //Creating the new AIS_InteractiveContext 
59   Handle(AIS_InteractiveContext) ctx = new AIS_InteractiveContext(aViewer);
60
61   //Creating the new TPrsStd_AISViewer attribute initialized with AIS_InteractiveContext
62   Handle(TPrsStd_AISViewer) aisviewer;
63
64   if( !TPrsStd_AISViewer::Has(aLabel) ) { //Check if there has already been set TPrsStd_AISViewer in the framework
65     aisviewer = TPrsStd_AISViewer::New(aLabel, ctx);
66   }
67
68   //Finding TPrsStd_AISViewer attribute in the DataFramework 
69   if( TPrsStd_AISViewer::Find(aLabel, aisviewer) ) {
70     aisviewer->Update(); //Update the viewer associated with this attribute
71   }
72
73   //Getting AIS_InteractiveContext from TPrsStd_AISViewer may be done in two ways: 
74
75   //1. If we have already gotten TPrsStd_AISViewer attribute (strored in a variable <aisviewer>)
76   Handle(AIS_InteractiveContext) context1 = aisviewer->GetInteractiveContext();
77  
78   //2. Getting AIS_InteractiveContext directly
79   Handle(AIS_InteractiveContext) context2;
80   if( TPrsStd_AISViewer::Find(aLabel, context2) ) {
81     //do something...
82   }
83
84   //----------------------------------- TPrsStd_Driver and TPrsStd_DriverTable -------------------
85   //==============================================================================================
86
87   // All work for building AIS_InteractiveObject to be presented by TPrsStd_AISPresentation is done
88   // by drivers which are descendants of deferred class TPrsStd_Driver
89
90   // There is a map of drivers with Standard_GUID as a key. 
91
92   // Adding driver to the map of drivers
93
94   Handle(TPrsStd_NamedShapeDriver) NSDriver = new TPrsStd_NamedShapeDriver();
95  
96   Handle(TPrsStd_DriverTable) table  = TPrsStd_DriverTable::Get();
97  
98   Standard_GUID guid = TNaming_NamedShape::GetID();
99
100   table->AddDriver(guid, NSDriver);  
101
102   // When the first time called TPrsStd_DriverTable loads standard drivers defined in TPrsStd package
103
104   // Getting driver from the map of drivers
105
106   Standard_GUID driverguid = TNaming_NamedShape::GetID();
107   
108   Handle(TPrsStd_NamedShapeDriver) driver;
109
110   if( table->FindDriver(driverguid,  driver) ) 
111     cout << "Driver was found " << endl;
112   else 
113     cout << "Driver wasn't found" << endl;
114
115   // Driver can be used to build AIS_InteractiveObject for presenting the given label
116
117   Handle(TPrsStd_PlaneDriver) planedriver;
118
119   if( table->FindDriver(TDataXtd_Plane::GetID(),  planedriver) ) {
120     
121     TDF_Label planelabel;
122
123     // Finding planelabel ... 
124
125     Handle(AIS_InteractiveObject) aisobject;
126
127     planedriver->Update(planelabel, aisobject);
128
129     if( !aisobject.IsNull() ) {
130
131       // Do something with aisobject ...
132
133     }
134   } 
135
136   //----------------------------------- TPrsStd_AISPresentation ----------------------------------
137   //==============================================================================================
138
139
140   TDF_Label ShapeLabel;
141   
142   // ... Setting TNaming_NamedShape to <ShapeLabel>
143
144   // Setting the new  TPrsStd_AISPresentation to <ShapeLabel>
145   // It can be done in two different ways:
146
147   Handle(TPrsStd_AISPresentation) Presenation;
148   //  1. By giving to TPrsStd_AISPresentation attribute Standard_GUID of an attribute  to be displayed:
149   //  This GUID will be used to find driver for building AIS_InteractiveObject in the map of drivers
150         
151   Presenation = TPrsStd_AISPresentation::Set( ShapeLabel, TNaming_NamedShape::GetID() );
152  
153   //  2. Or by giving the attribute itself to TPrsStd_AISPresentation attribute:
154   //  An ID of attribute  will be used to find driver for building AIS_InteractiveObject in the map of drivers
155
156   Handle(TNaming_NamedShape) NS;
157   if( ShapeLabel.FindAttribute( TNaming_NamedShape::GetID(), NS) ) {
158     Presenation = TPrsStd_AISPresentation::Set( NS );
159   }
160
161
162   // Displaying (recomputation of presentation of attribute is done only if presentation is null)
163
164   Handle(TPrsStd_AISPresentation) PRS;
165   
166   if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), PRS) ) PRS->Display();
167   //After call of the method PRS->Display() the presentation of the attribute is marked as displayed in 
168   //AIS_InteractiveContext  but not in viewer, in order to draw the object in viewer last has to be updated
169
170   TPrsStd_AISViewer::Update(ShapeLabel);  //Update presentation of the attribute in a viewer's window
171
172   // Erasing
173   
174   if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), PRS) ) PRS->Erase();
175   // The method Erase() marks presentation of attribute as erased in AIS_InteractiveContext; 
176   // in order to make changes visible in a viewer's window viewer has to be updated  
177   TPrsStd_AISViewer::Update(ShapeLabel);  //Update viewer to erase presenation of the attribute in a viewer's window
178                                           //Presentation of the attribute is erased from viewer but
179                                           // stays in AIS_InteractiveContext
180  
181   if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), PRS) ) PRS->Erase(Standard_True); 
182   TPrsStd_AISViewer::Update(ShapeLabel);
183                                         //Presentation of the attribute is erased
184                                         //from viewer and removed from AIS_InteractiveContext
185
186   Handle(TPrsStd_AISPresentation) P;
187   if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), P) ) {
188
189     // Updating and displaying presentation of the attribute to be displayed 
190
191     P->Display(Standard_True); 
192     TPrsStd_AISViewer::Update(ShapeLabel);  //Update presenation of the attribute in a viewer's window
193
194     //Getting Standard_GUID of attribute with which TPrsStd_AISPresentation attribute is associeted
195
196     Standard_GUID guid = P->GetDriverGUID();
197
198     //Setting a color to the displayd attribute
199
200     P->SetColor(Quantity_NOC_RED);
201     TPrsStd_AISViewer::Update(ShapeLabel);  //Update viewer to make changes visible to user
202
203     //Getting transparency the displayd attribute
204
205     Standard_Real transparency = P->Transparency();
206
207     //Getting AIS_InteractiveObject built and stored in the AIS_Presentation attribute
208
209     Handle(AIS_InteractiveObject) AISObject = P->GetAIS();
210   }
211
212   // ... Attribute is modified  
213
214
215   //Updating presentation of the attribute in viewer
216
217   if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), PRS) )
218   PRS->Update(); //Updates presentation of attribute in AIS_InteractiveContext
219   TPrsStd_AISViewer::Update(ShapeLabel); //Updates presentation in viewer
220
221   return; 
222 }
223
224 #endif