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