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
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.
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.
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.
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>
38 // ====================================================================================
39 // This sample contains template for typical actions with OCAF visualization attributes
40 // ====================================================================================
45 // Starting with data framework
46 Handle(TDF_Data) DF = new TDF_Data();
47 TDF_Label aLabel = DF->Root();
49 //----------------------------------- TPrsStd_AISViewer ----------------------------------------
50 //==============================================================================================
52 // Setting the TPrsStd_AISViewer in the framework
54 Handle(V3d_Viewer) aViewer;
56 //... Initialization of aViewer
58 //Creating the new AIS_InteractiveContext
59 Handle(AIS_InteractiveContext) ctx = new AIS_InteractiveContext(aViewer);
61 //Creating the new TPrsStd_AISViewer attribute initialized with AIS_InteractiveContext
62 Handle(TPrsStd_AISViewer) aisviewer;
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);
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
73 //Getting AIS_InteractiveContext from TPrsStd_AISViewer may be done in two ways:
75 //1. If we have already gotten TPrsStd_AISViewer attribute (strored in a variable <aisviewer>)
76 Handle(AIS_InteractiveContext) context1 = aisviewer->GetInteractiveContext();
78 //2. Getting AIS_InteractiveContext directly
79 Handle(AIS_InteractiveContext) context2;
80 if( TPrsStd_AISViewer::Find(aLabel, context2) ) {
84 //----------------------------------- TPrsStd_Driver and TPrsStd_DriverTable -------------------
85 //==============================================================================================
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
90 // There is a map of drivers with Standard_GUID as a key.
92 // Adding driver to the map of drivers
94 Handle(TPrsStd_NamedShapeDriver) NSDriver = new TPrsStd_NamedShapeDriver();
96 Handle(TPrsStd_DriverTable) table = TPrsStd_DriverTable::Get();
98 Standard_GUID guid = TNaming_NamedShape::GetID();
100 table->AddDriver(guid, NSDriver);
102 // When the first time called TPrsStd_DriverTable loads standard drivers defined in TPrsStd package
104 // Getting driver from the map of drivers
106 Standard_GUID driverguid = TNaming_NamedShape::GetID();
108 Handle(TPrsStd_NamedShapeDriver) driver;
110 if( table->FindDriver(driverguid, driver) )
111 cout << "Driver was found " << endl;
113 cout << "Driver wasn't found" << endl;
115 // Driver can be used to build AIS_InteractiveObject for presenting the given label
117 Handle(TPrsStd_PlaneDriver) planedriver;
119 if( table->FindDriver(TDataXtd_Plane::GetID(), planedriver) ) {
121 TDF_Label planelabel;
123 // Finding planelabel ...
125 Handle(AIS_InteractiveObject) aisobject;
127 planedriver->Update(planelabel, aisobject);
129 if( !aisobject.IsNull() ) {
131 // Do something with aisobject ...
136 //----------------------------------- TPrsStd_AISPresentation ----------------------------------
137 //==============================================================================================
140 TDF_Label ShapeLabel;
142 // ... Setting TNaming_NamedShape to <ShapeLabel>
144 // Setting the new TPrsStd_AISPresentation to <ShapeLabel>
145 // It can be done in two different ways:
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
151 Presenation = TPrsStd_AISPresentation::Set( ShapeLabel, TNaming_NamedShape::GetID() );
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
156 Handle(TNaming_NamedShape) NS;
157 if( ShapeLabel.FindAttribute( TNaming_NamedShape::GetID(), NS) ) {
158 Presenation = TPrsStd_AISPresentation::Set( NS );
162 // Displaying (recomputation of presentation of attribute is done only if presentation is null)
164 Handle(TPrsStd_AISPresentation) PRS;
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
170 TPrsStd_AISViewer::Update(ShapeLabel); //Update presentation of the attribute in a viewer's window
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
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
186 Handle(TPrsStd_AISPresentation) P;
187 if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), P) ) {
189 // Updating and displaying presentation of the attribute to be displayed
191 P->Display(Standard_True);
192 TPrsStd_AISViewer::Update(ShapeLabel); //Update presenation of the attribute in a viewer's window
194 //Getting Standard_GUID of attribute with which TPrsStd_AISPresentation attribute is associeted
196 Standard_GUID guid = P->GetDriverGUID();
198 //Setting a color to the displayd attribute
200 P->SetColor(Quantity_NOC_RED);
201 TPrsStd_AISViewer::Update(ShapeLabel); //Update viewer to make changes visible to user
203 //Getting transparency the displayd attribute
205 Standard_Real transparency = P->Transparency();
207 //Getting AIS_InteractiveObject built and stored in the AIS_Presentation attribute
209 Handle(AIS_InteractiveObject) AISObject = P->GetAIS();
212 // ... Attribute is modified
215 //Updating presentation of the attribute in viewer
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