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