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>
20 #include <gp_Pnt2d.hxx>
21 #include <ProjLib.hxx>
23 #include <Precision.hxx>
27 #include <WNT_Window.hxx>
28 #include <Graphic3d_WNTGraphicDevice.hxx>
30 #include <Xw_Window.hxx>
31 #include <Graphic3d_GraphicDevice.hxx>
35 /*----------------------------------------------------------------------*/
37 Handle(V3d_DirectionalLight) aLight;
38 Handle(AIS_Shape) aShape;
43 //===============================================================
44 // Function name: CreateViewer
45 //===============================================================
46 Handle(V3d_Viewer) SampleAISDisplayModePackage::CreateViewer (const Standard_ExtString aName)
49 static Handle(Graphic3d_WNTGraphicDevice) defaultDevice;
51 if (defaultDevice.IsNull())
52 defaultDevice = new Graphic3d_WNTGraphicDevice();
53 return new V3d_Viewer(defaultDevice, aName);
55 static Handle(Graphic3d_GraphicDevice) defaultDevice;
57 if (defaultDevice.IsNull())
58 defaultDevice = new Graphic3d_GraphicDevice("");
59 return new V3d_Viewer(defaultDevice, aName);
63 //===============================================================
64 // Function name: SetWindow
65 //===============================================================
66 void SampleAISDisplayModePackage::SetWindow (const Handle(V3d_View)& aView,
67 const Standard_Integer hiwin,
68 const Standard_Integer lowin)
71 Handle(Graphic3d_WNTGraphicDevice) d =
72 Handle(Graphic3d_WNTGraphicDevice)::DownCast(aView->Viewer()->Device());
73 Handle(WNT_Window) w = new WNT_Window(d,hiwin,lowin);
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);
83 /*----------------------------------------------------------------------*/
85 gp_Pnt ConvertClickToPoint(Standard_Real x, Standard_Real y, Handle(V3d_View) aView)
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);
93 gp_Vec EyeVector(EyePoint,AtPoint);
94 gp_Dir EyeDir(EyeVector);
96 gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir);
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);
102 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(),
103 ConvertedPointOnPlane.Y(),
109 //======================================================================
111 //= Display objects =
113 //======================================================================
115 //===============================================================
116 // Function name: DisplayBox
117 //===============================================================
118 void SampleAISDisplayModePackage::DisplayBox(const Handle(AIS_InteractiveContext)& aContext,
119 TCollection_AsciiString& Message)
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);
127 BRepPrimAPI_MakeBox B(gp_Pnt(-400.,-400.,-100.),200.,150.,100.); \n\
128 Handle(AIS_Shape) aBox = new AIS_Shape(B.Shape()); \n\
129 myAISContext->Display(aBox); \n\
133 //===============================================================
134 // Function name: DisplaySphere
135 //===============================================================
136 void SampleAISDisplayModePackage::DisplaySphere(const Handle(AIS_InteractiveContext)& aContext,
137 TCollection_AsciiString& Message)
139 BRepPrimAPI_MakeSphere S(gp_Pnt(200.,300.,200.), 100.);
140 Handle(AIS_Shape) anAISShape = new AIS_Shape(S.Shape());
141 aContext->Display(anAISShape);
145 BRepPrimAPI_MakeSphere S(gp_Pnt(200.,300.,200.), 100.); \n\
146 Handle(AIS_Shape) anAISShape = new AIS_Shape(S.Shape()); \n\
147 myAISContext->Display(anAISShape); \n\
151 //===============================================================
152 // Function name: DisplayCylinder
153 //===============================================================
154 void SampleAISDisplayModePackage::DisplayCylinder(const Handle(AIS_InteractiveContext)& aContext,
155 TCollection_AsciiString& Message)
157 Handle(User_Cylinder) aCyl = new User_Cylinder(100.,200.);
158 aContext->SetDisplayMode(aCyl,1);
159 aContext->Display(aCyl);
163 Handle(User_Cylinder) aCyl = new User_Cylinder(100.,200.); \n\
164 myAISContext->SetDisplayMode(aCyl,1); \n\
165 myAISContext->Display(aCyl); \n\
167 NOTE: a User_Cylinder is an object defined by the user. \n\
168 The User_Cylinder class inherits the AIS_InteractiveObject \n\
169 CASCADE class, its usage is the same as an AIS_InteractiveObject. \n\
173 //===============================================================
174 // Function name: EraseAll
175 //===============================================================
176 void SampleAISDisplayModePackage::EraseAll(const Handle(AIS_InteractiveContext)& aContext,
177 TCollection_AsciiString& Message)
179 aContext->EraseAll(Standard_False);
183 myAISContext->EraseAll(Standard_False); \n\
187 //======================================================================
189 //= Context properties =
191 //======================================================================
193 //===============================================================
194 // Function name: InitContext
195 //===============================================================
196 void SampleAISDisplayModePackage::InitContext(const Handle(AIS_InteractiveContext)& aContext)
198 aContext->DefaultDrawer()->ShadingAspect()->SetColor(Quantity_NOC_CHARTREUSE1);
199 aContext->DefaultDrawer()->ShadingAspect()->SetMaterial(Graphic3d_NOM_SILVER);
202 //===============================================================
203 // Function name: GetIsosNumber
204 //===============================================================
205 void SampleAISDisplayModePackage::GetIsosNumber(const Handle(AIS_InteractiveContext)& aContext,
206 Standard_Integer& u,Standard_Integer& v)
208 u = aContext->DefaultDrawer()->UIsoAspect()->Number();
209 v = aContext->DefaultDrawer()->VIsoAspect()->Number();
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)
220 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(u);
221 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(v);
225 myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber(u); \n\
226 myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber(v); \n\
230 //===============================================================
231 // Function name: SetDisplayMode
232 //===============================================================
233 void SampleAISDisplayModePackage::SetDisplayMode(const Handle(AIS_InteractiveContext)& aContext,
234 const AIS_DisplayMode aMode,
235 TCollection_AsciiString& Message)
237 aContext->SetDisplayMode(aMode);
241 myAISContext->SetDisplayMode(aMode); \n\
245 //======================================================================
247 //= Object properties =
249 //======================================================================
251 //===============================================================
252 // Function name: SetObjectDisplayMode
253 //===============================================================
254 void SampleAISDisplayModePackage::SetObjectDisplayMode(const Handle(AIS_InteractiveContext)& aContext,
255 const AIS_DisplayMode aMode,
256 TCollection_AsciiString& Message)
258 for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
259 aContext->SetDisplayMode(aContext->Current(), aMode);
263 for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); \n\
264 myAISContext->NextCurrent()) \n\
265 myAISContext->SetDisplayMode(myAISContext->Current(), aMode); \n\
269 //===============================================================
270 // Function name: SetObjectMaterial
271 //===============================================================
272 void SampleAISDisplayModePackage::SetObjectMaterial(const Handle(AIS_InteractiveContext)& aContext,
273 const Graphic3d_NameOfMaterial aName,
274 TCollection_AsciiString& Message)
276 for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
277 aContext->SetMaterial(aContext->Current(), aName);
281 for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); \n\
282 myAISContext->NextCurrent()) \n\
283 myAISContext->SetMaterial(myAISContext->Current(), aName); \n\
287 //===============================================================
288 // Function name: GetObjectColor
289 //===============================================================
290 Quantity_Color SampleAISDisplayModePackage::GetObjectColor(const Handle(AIS_InteractiveContext)& aContext)
292 Handle(AIS_InteractiveObject) Current;
293 Quantity_Color aColor;
295 aContext->InitCurrent();
296 if (aContext->MoreCurrent()) {
297 Current = aContext->Current();
298 if (Current->HasColor())
299 aColor = Current->Color();
305 //===============================================================
306 // Function name: SetObjectColor
307 //===============================================================
308 void SampleAISDisplayModePackage::SetObjectColor(const Handle(AIS_InteractiveContext)& aContext,
309 const Quantity_Color& aColor,
310 TCollection_AsciiString& Message)
312 for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
313 aContext->SetColor(aContext->Current(), aColor.Name());
317 for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); \n\
318 myAISContext->NextCurrent()) \n\
319 myAISContext->SetColor(myAISContext->Current(), aColor.Name()); \n\
323 //===============================================================
324 // Function name: GetObjectTransparency
325 //===============================================================
326 Standard_Real SampleAISDisplayModePackage::GetObjectTransparency(const Handle(AIS_InteractiveContext)& aContext)
328 Standard_Real aValue = 0.0;
330 aContext->InitCurrent();
331 if (aContext->MoreCurrent())
332 aValue = aContext->Current()->Transparency();
337 //===============================================================
338 // Function name: SetObjectTransparency
339 //===============================================================
340 void SampleAISDisplayModePackage::SetObjectTransparency(const Handle(AIS_InteractiveContext)& aContext,
341 const Standard_Real aValue,
342 TCollection_AsciiString& Message)
344 for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
345 aContext->SetTransparency(aContext->Current(), aValue);
349 for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); \n\
350 myAISContext->NextCurrent()) \n\
351 myAISContext->SetTransparency(myAISContext->Current(), aValue); \n\
357 //======================================================================
361 //======================================================================
363 //===============================================================
364 // Function name: CreateLight
365 //===============================================================
366 void SampleAISDisplayModePackage::CreateLight(const Handle(AIS_InteractiveContext)& aContext)
368 aContext->OpenLocalContext();
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)
379 p1 = ConvertClickToPoint(X,Y,aView);
381 // Create a directional light
382 aLight = new V3d_DirectionalLight(aView->Viewer(), p1.X(),p1.Y(),p1.Z(),0.,0.,1.);
384 p2 = gp_Pnt(p1.X(),p1.Y(),p1.Z()+1.);
386 BRepBuilderAPI_MakeEdge E(p1, p2);
387 aShape = new AIS_Shape(E.Edge());
389 aShape->SetColor(Quantity_NOC_YELLOW);
390 aContext->Display(aShape);
392 // Activate the light in the view
393 aView->SetLightOn(aLight);
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)
404 p2 = ConvertClickToPoint(X,Y,aView);
406 if (p1.Distance(p2)>Precision::Confusion()) {
407 BRepBuilderAPI_MakeEdge E(p1, p2);
408 aShape->Set(E.Edge());
409 aContext->Redisplay(aShape);
411 //Update the light dynamically
412 aLight->SetDirection(p2.X()-p1.X(),p2.Y()-p1.Y(),p2.Z()-p1.Z());
413 aView->UpdateLights();
417 //===============================================================
418 // Function name: SetSecondPointOfLight
419 //===============================================================
420 void SampleAISDisplayModePackage::SetSecondPointOfLight(const Handle(AIS_InteractiveContext)& aContext)
422 aContext->CloseLocalContext();