Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / java / src / SampleAISDisplayModePackage / SampleAISDisplayModePackage.cxx
CommitLineData
7fd59977 1#include <SampleAISDisplayModePackage.hxx>
2#include <TCollection_AsciiString.hxx>
3#include <AIS_InteractiveContext.hxx>
4#include <AIS_Shape.hxx>
5#include <User_Cylinder.hxx>
6#include <BRepPrimAPI_MakeBox.hxx>
7#include <BRepPrimAPI_MakeSphere.hxx>
8#include <BRepBuilderAPI_MakeEdge.hxx>
9#include <Quantity_Color.hxx>
10#include <V3d_Viewer.hxx>
11#include <V3d_View.hxx>
12#include <V3d_DirectionalLight.hxx>
13#include <Prs3d_Drawer.hxx>
14#include <Prs3d_ShadingAspect.hxx>
15#include <Prs3d_IsoAspect.hxx>
16#include <gp_Pnt.hxx>
17#include <gp_Dir.hxx>
18#include <gp_Vec.hxx>
19#include <gp_Pln.hxx>
20#include <gp_Pnt2d.hxx>
21#include <ProjLib.hxx>
22#include <ElSLib.hxx>
23#include <Precision.hxx>
24
25
26#ifdef WNT
27#include <WNT_Window.hxx>
28#include <Graphic3d_WNTGraphicDevice.hxx>
29#else
30#include <Xw_Window.hxx>
31#include <Graphic3d_GraphicDevice.hxx>
32#endif
33
34
35/*----------------------------------------------------------------------*/
36
37Handle(V3d_DirectionalLight) aLight;
38Handle(AIS_Shape) aShape;
39gp_Pnt p1, p2;
40
41
42
43//===============================================================
44// Function name: CreateViewer
45//===============================================================
46 Handle(V3d_Viewer) SampleAISDisplayModePackage::CreateViewer (const Standard_ExtString aName)
47{
48#ifdef WNT
49static Handle(Graphic3d_WNTGraphicDevice) defaultDevice;
50
51 if (defaultDevice.IsNull())
52 defaultDevice = new Graphic3d_WNTGraphicDevice();
53 return new V3d_Viewer(defaultDevice, aName);
54#else
55static Handle(Graphic3d_GraphicDevice) defaultDevice;
56
57 if (defaultDevice.IsNull())
58 defaultDevice = new Graphic3d_GraphicDevice("");
59 return new V3d_Viewer(defaultDevice, aName);
60#endif //WNT
61}
62
63//===============================================================
64// Function name: SetWindow
65//===============================================================
66void SampleAISDisplayModePackage::SetWindow (const Handle(V3d_View)& aView,
67 const Standard_Integer hiwin,
68 const Standard_Integer lowin)
69{
70#ifdef WNT
71 Handle(Graphic3d_WNTGraphicDevice) d =
72 Handle(Graphic3d_WNTGraphicDevice)::DownCast(aView->Viewer()->Device());
73 Handle(WNT_Window) w = new WNT_Window(d,hiwin,lowin);
74#else
75 Handle(Graphic3d_GraphicDevice) d =
76 Handle(Graphic3d_GraphicDevice)::DownCast(aView->Viewer()->Device());
77 Handle(Xw_Window) w = new Xw_Window(d,hiwin,lowin,Xw_WQ_3DQUALITY);
78#endif
79 aView->SetWindow(w);
80}
81
82
83/*----------------------------------------------------------------------*/
84
85gp_Pnt ConvertClickToPoint(Standard_Real x, Standard_Real y, Handle(V3d_View) aView)
86{
87 V3d_Coordinate XEye,YEye,ZEye,XAt,YAt,ZAt;
88 aView->Eye(XEye,YEye,ZEye);
89 aView->At(XAt,YAt,ZAt);
90 gp_Pnt EyePoint(XEye,YEye,ZEye);
91 gp_Pnt AtPoint(XAt,YAt,ZAt);
92
93 gp_Vec EyeVector(EyePoint,AtPoint);
94 gp_Dir EyeDir(EyeVector);
95
96 gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir);
97 V3d_Coordinate X,Y,Z;
98 aView->Convert(Standard_Integer(x),Standard_Integer(y),X,Y,Z);
99 gp_Pnt ConvertedPoint(X,Y,Z);
100 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView,ConvertedPoint);
101
102 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(),
103 ConvertedPointOnPlane.Y(),
104 PlaneOfTheView);
105 return ResultPoint;
106}
107
108
109//======================================================================
110//= =
111//= Display objects =
112//= =
113//======================================================================
114
115//===============================================================
116// Function name: DisplayBox
117//===============================================================
118 void SampleAISDisplayModePackage::DisplayBox(const Handle(AIS_InteractiveContext)& aContext,
119 TCollection_AsciiString& Message)
120{
121 BRepPrimAPI_MakeBox B(gp_Pnt(-400.,-400.,-100.),200.,150.,100.);
122 Handle(AIS_Shape) aBox = new AIS_Shape(B.Shape());
123 aContext->Display(aBox);
124
125 Message = "\
126 \n\
127BRepPrimAPI_MakeBox B(gp_Pnt(-400.,-400.,-100.),200.,150.,100.); \n\
128Handle(AIS_Shape) aBox = new AIS_Shape(B.Shape()); \n\
129myAISContext->Display(aBox); \n\
130 \n";
131}
132
133//===============================================================
134// Function name: DisplaySphere
135//===============================================================
136 void SampleAISDisplayModePackage::DisplaySphere(const Handle(AIS_InteractiveContext)& aContext,
137 TCollection_AsciiString& Message)
138{
139 BRepPrimAPI_MakeSphere S(gp_Pnt(200.,300.,200.), 100.);
140 Handle(AIS_Shape) anAISShape = new AIS_Shape(S.Shape());
141 aContext->Display(anAISShape);
142
143 Message = "\
144 \n\
145BRepPrimAPI_MakeSphere S(gp_Pnt(200.,300.,200.), 100.); \n\
146Handle(AIS_Shape) anAISShape = new AIS_Shape(S.Shape()); \n\
147myAISContext->Display(anAISShape); \n\
148 \n";
149}
150
151//===============================================================
152// Function name: DisplayCylinder
153//===============================================================
154 void SampleAISDisplayModePackage::DisplayCylinder(const Handle(AIS_InteractiveContext)& aContext,
155 TCollection_AsciiString& Message)
156{
157 Handle(User_Cylinder) aCyl = new User_Cylinder(100.,200.);
158 aContext->SetDisplayMode(aCyl,1);
159 aContext->Display(aCyl);
160
161 Message = "\
162 \n\
163Handle(User_Cylinder) aCyl = new User_Cylinder(100.,200.); \n\
164myAISContext->SetDisplayMode(aCyl,1); \n\
165myAISContext->Display(aCyl); \n\
166 \n\
167NOTE: a User_Cylinder is an object defined by the user. \n\
168The User_Cylinder class inherits the AIS_InteractiveObject \n\
169CASCADE class, its usage is the same as an AIS_InteractiveObject. \n\
170 \n";
171}
172
173//===============================================================
174// Function name: EraseAll
175//===============================================================
176 void SampleAISDisplayModePackage::EraseAll(const Handle(AIS_InteractiveContext)& aContext,
177 TCollection_AsciiString& Message)
178{
179 aContext->EraseAll(Standard_False);
180
181 Message = "\
182 \n\
183myAISContext->EraseAll(Standard_False); \n\
184 \n";
185}
186
187//======================================================================
188//= =
189//= Context properties =
190//= =
191//======================================================================
192
193//===============================================================
194// Function name: InitContext
195//===============================================================
196 void SampleAISDisplayModePackage::InitContext(const Handle(AIS_InteractiveContext)& aContext)
197{
198 aContext->DefaultDrawer()->ShadingAspect()->SetColor(Quantity_NOC_CHARTREUSE1);
199 aContext->DefaultDrawer()->ShadingAspect()->SetMaterial(Graphic3d_NOM_SILVER);
200}
201
202//===============================================================
203// Function name: GetIsosNumber
204//===============================================================
205 void SampleAISDisplayModePackage::GetIsosNumber(const Handle(AIS_InteractiveContext)& aContext,
206 Standard_Integer& u,Standard_Integer& v)
207{
208 u = aContext->DefaultDrawer()->UIsoAspect()->Number();
209 v = aContext->DefaultDrawer()->VIsoAspect()->Number();
210}
211
212//===============================================================
213// Function name: SetIsosNumber
214//===============================================================
215 void SampleAISDisplayModePackage::SetIsosNumber(const Handle(AIS_InteractiveContext)& aContext,
216 const Standard_Integer u,
217 const Standard_Integer v,
218 TCollection_AsciiString& Message)
219{
220 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(u);
221 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(v);
222
223 Message = "\
224 \n\
225myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber(u); \n\
226myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber(v); \n\
227 \n";
228}
229
230//===============================================================
231// Function name: SetDisplayMode
232//===============================================================
233 void SampleAISDisplayModePackage::SetDisplayMode(const Handle(AIS_InteractiveContext)& aContext,
234 const AIS_DisplayMode aMode,
235 TCollection_AsciiString& Message)
236{
237 aContext->SetDisplayMode(aMode);
238
239 Message = "\
240 \n\
241myAISContext->SetDisplayMode(aMode); \n\
242 \n";
243}
244
245//======================================================================
246//= =
247//= Object properties =
248//= =
249//======================================================================
250
251//===============================================================
252// Function name: SetObjectDisplayMode
253//===============================================================
254 void SampleAISDisplayModePackage::SetObjectDisplayMode(const Handle(AIS_InteractiveContext)& aContext,
255 const AIS_DisplayMode aMode,
256 TCollection_AsciiString& Message)
257{
258 for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
259 aContext->SetDisplayMode(aContext->Current(), aMode);
260
261 Message = "\
262 \n\
263for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); \n\
264 myAISContext->NextCurrent()) \n\
265 myAISContext->SetDisplayMode(myAISContext->Current(), aMode); \n\
266 \n";
267}
268
269//===============================================================
270// Function name: SetObjectMaterial
271//===============================================================
272 void SampleAISDisplayModePackage::SetObjectMaterial(const Handle(AIS_InteractiveContext)& aContext,
273 const Graphic3d_NameOfMaterial aName,
274 TCollection_AsciiString& Message)
275{
276 for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
277 aContext->SetMaterial(aContext->Current(), aName);
278
279 Message = "\
280 \n\
281for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); \n\
282 myAISContext->NextCurrent()) \n\
283 myAISContext->SetMaterial(myAISContext->Current(), aName); \n\
284 \n";
285}
286
287//===============================================================
288// Function name: GetObjectColor
289//===============================================================
290 Quantity_Color SampleAISDisplayModePackage::GetObjectColor(const Handle(AIS_InteractiveContext)& aContext)
291{
292 Handle(AIS_InteractiveObject) Current;
293 Quantity_Color aColor;
294
295 aContext->InitCurrent();
296 if (aContext->MoreCurrent()) {
297 Current = aContext->Current();
298 if (Current->HasColor())
299 aColor = Current->Color();
300 }
301
302 return aColor;
303}
304
305//===============================================================
306// Function name: SetObjectColor
307//===============================================================
308 void SampleAISDisplayModePackage::SetObjectColor(const Handle(AIS_InteractiveContext)& aContext,
309 const Quantity_Color& aColor,
310 TCollection_AsciiString& Message)
311{
312 for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
313 aContext->SetColor(aContext->Current(), aColor.Name());
314
315 Message = "\
316 \n\
317for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); \n\
318 myAISContext->NextCurrent()) \n\
319 myAISContext->SetColor(myAISContext->Current(), aColor.Name()); \n\
320 \n";
321}
322
323//===============================================================
324// Function name: GetObjectTransparency
325//===============================================================
326 Standard_Real SampleAISDisplayModePackage::GetObjectTransparency(const Handle(AIS_InteractiveContext)& aContext)
327{
328 Standard_Real aValue = 0.0;
329
330 aContext->InitCurrent();
331 if (aContext->MoreCurrent())
332 aValue = aContext->Current()->Transparency();
333
334 return aValue;
335}
336
337//===============================================================
338// Function name: SetObjectTransparency
339//===============================================================
340 void SampleAISDisplayModePackage::SetObjectTransparency(const Handle(AIS_InteractiveContext)& aContext,
341 const Standard_Real aValue,
342 TCollection_AsciiString& Message)
343{
344 for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
345 aContext->SetTransparency(aContext->Current(), aValue);
346
347 Message = "\
348 \n\
349for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); \n\
350 myAISContext->NextCurrent()) \n\
351 myAISContext->SetTransparency(myAISContext->Current(), aValue); \n\
352 \n";
353}
354
355
356
357//======================================================================
358//= =
359//= Light =
360//= =
361//======================================================================
362
363//===============================================================
364// Function name: CreateLight
365//===============================================================
366 void SampleAISDisplayModePackage::CreateLight(const Handle(AIS_InteractiveContext)& aContext)
367{
368 aContext->OpenLocalContext();
369}
370
371//===============================================================
372// Function name: SetFirstPointOfLight
373//===============================================================
374 void SampleAISDisplayModePackage::SetFirstPointOfLight(const Handle(AIS_InteractiveContext)& aContext,
375 const Handle(V3d_View)& aView,
376 const Standard_Integer X,
377 const Standard_Integer Y)
378{
379 p1 = ConvertClickToPoint(X,Y,aView);
380
381 // Create a directional light
382 aLight = new V3d_DirectionalLight(aView->Viewer(), p1.X(),p1.Y(),p1.Z(),0.,0.,1.);
383
384 p2 = gp_Pnt(p1.X(),p1.Y(),p1.Z()+1.);
385
386 BRepBuilderAPI_MakeEdge E(p1, p2);
387 aShape = new AIS_Shape(E.Edge());
388
389 aShape->SetColor(Quantity_NOC_YELLOW);
390 aContext->Display(aShape);
391
392 // Activate the light in the view
393 aView->SetLightOn(aLight);
394}
395
396//===============================================================
397// Function name: MoveSecondPointOfLight
398//===============================================================
399 void SampleAISDisplayModePackage::MoveSecondPointOfLight(const Handle(AIS_InteractiveContext)& aContext,
400 const Handle(V3d_View)& aView,
401 const Standard_Integer X,
402 const Standard_Integer Y)
403{
404 p2 = ConvertClickToPoint(X,Y,aView);
405
406 if (p1.Distance(p2)>Precision::Confusion()) {
407 BRepBuilderAPI_MakeEdge E(p1, p2);
408 aShape->Set(E.Edge());
409 aContext->Redisplay(aShape);
410
411 //Update the light dynamically
412 aLight->SetDirection(p2.X()-p1.X(),p2.Y()-p1.Y(),p2.Z()-p1.Z());
413 aView->UpdateLights();
414 }
415}
416
417//===============================================================
418// Function name: SetSecondPointOfLight
419//===============================================================
420 void SampleAISDisplayModePackage::SetSecondPointOfLight(const Handle(AIS_InteractiveContext)& aContext)
421{
422 aContext->CloseLocalContext();
423}
424