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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
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>
31 // ====================================================================================
32 // This sample contains template for typical actions with OCAF visualization attributes
33 // ====================================================================================
38 // Starting with data framework
39 Handle(TDF_Data) DF = new TDF_Data();
40 TDF_Label aLabel = DF->Root();
42 //----------------------------------- TPrsStd_AISViewer ----------------------------------------
43 //==============================================================================================
45 // Setting the TPrsStd_AISViewer in the framework
47 Handle(V3d_Viewer) aViewer;
49 //... Initialization of aViewer
51 //Creating the new AIS_InteractiveContext
52 Handle(AIS_InteractiveContext) ctx = new AIS_InteractiveContext(aViewer);
54 //Creating the new TPrsStd_AISViewer attribute initialized with AIS_InteractiveContext
55 Handle(TPrsStd_AISViewer) aisviewer;
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);
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
66 //Getting AIS_InteractiveContext from TPrsStd_AISViewer may be done in two ways:
68 //1. If we have already gotten TPrsStd_AISViewer attribute (strored in a variable <aisviewer>)
69 Handle(AIS_InteractiveContext) context1 = aisviewer->GetInteractiveContext();
71 //2. Getting AIS_InteractiveContext directly
72 Handle(AIS_InteractiveContext) context2;
73 if( TPrsStd_AISViewer::Find(aLabel, context2) ) {
77 //----------------------------------- TPrsStd_Driver and TPrsStd_DriverTable -------------------
78 //==============================================================================================
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
83 // There is a map of drivers with Standard_GUID as a key.
85 // Adding driver to the map of drivers
87 Handle(TPrsStd_NamedShapeDriver) NSDriver = new TPrsStd_NamedShapeDriver();
89 Handle(TPrsStd_DriverTable) table = TPrsStd_DriverTable::Get();
91 Standard_GUID guid = TNaming_NamedShape::GetID();
93 table->AddDriver(guid, NSDriver);
95 // When the first time called TPrsStd_DriverTable loads standard drivers defined in TPrsStd package
97 // Getting driver from the map of drivers
99 Standard_GUID driverguid = TNaming_NamedShape::GetID();
101 Handle(TPrsStd_NamedShapeDriver) driver;
103 if( table->FindDriver(driverguid, driver) )
104 cout << "Driver was found " << endl;
106 cout << "Driver wasn't found" << endl;
108 // Driver can be used to build AIS_InteractiveObject for presenting the given label
110 Handle(TPrsStd_PlaneDriver) planedriver;
112 if( table->FindDriver(TDataXtd_Plane::GetID(), planedriver) ) {
114 TDF_Label planelabel;
116 // Finding planelabel ...
118 Handle(AIS_InteractiveObject) aisobject;
120 planedriver->Update(planelabel, aisobject);
122 if( !aisobject.IsNull() ) {
124 // Do something with aisobject ...
129 //----------------------------------- TPrsStd_AISPresentation ----------------------------------
130 //==============================================================================================
133 TDF_Label ShapeLabel;
135 // ... Setting TNaming_NamedShape to <ShapeLabel>
137 // Setting the new TPrsStd_AISPresentation to <ShapeLabel>
138 // It can be done in two different ways:
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
144 Presenation = TPrsStd_AISPresentation::Set( ShapeLabel, TNaming_NamedShape::GetID() );
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
149 Handle(TNaming_NamedShape) NS;
150 if( ShapeLabel.FindAttribute( TNaming_NamedShape::GetID(), NS) ) {
151 Presenation = TPrsStd_AISPresentation::Set( NS );
155 // Displaying (recomputation of presentation of attribute is done only if presentation is null)
157 Handle(TPrsStd_AISPresentation) PRS;
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
163 TPrsStd_AISViewer::Update(ShapeLabel); //Update presentation of the attribute in a viewer's window
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
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
179 Handle(TPrsStd_AISPresentation) P;
180 if( ShapeLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), P) ) {
182 // Updating and displaying presentation of the attribute to be displayed
184 P->Display(Standard_True);
185 TPrsStd_AISViewer::Update(ShapeLabel); //Update presenation of the attribute in a viewer's window
187 //Getting Standard_GUID of attribute with which TPrsStd_AISPresentation attribute is associeted
189 Standard_GUID guid = P->GetDriverGUID();
191 //Setting a color to the displayd attribute
193 P->SetColor(Quantity_NOC_RED);
194 TPrsStd_AISViewer::Update(ShapeLabel); //Update viewer to make changes visible to user
196 //Getting transparency the displayd attribute
198 Standard_Real transparency = P->Transparency();
200 //Getting AIS_InteractiveObject built and stored in the AIS_Presentation attribute
202 Handle(AIS_InteractiveObject) AISObject = P->GetAIS();
205 // ... Attribute is modified
208 //Updating presentation of the attribute in viewer
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