1 // OCCDemo_Presentation.cpp: implementation of the OCCDemo_Presentation class.
2 // This is a base class for all presentations
3 //////////////////////////////////////////////////////////////////////
6 #include "OCCDemo_Presentation.h"
7 #include "Viewer3dView.h"
8 #include "ISession_Curve.h"
10 #include <AIS_InteractiveObject.hxx>
11 #include <Geom_Surface.hxx>
12 #include <Geom_Curve.hxx>
13 #include <Geom2d_Curve.hxx>
14 #include <Quantity_Color.hxx>
15 #include <AIS_Shape.hxx>
16 #include <BRepBuilderAPI_MakeFace.hxx>
17 #include <Precision.hxx>
18 #include <Geom_Line.hxx>
19 #include <Geom_TrimmedCurve.hxx>
20 #include <Prs3d_Drawer.hxx>
21 #include <Prs3d_ArrowAspect.hxx>
22 #include <AIS_Point.hxx>
23 #include <Geom_CartesianPoint.hxx>
24 #include <Geom2d_OffsetCurve.hxx>
25 #include <GeomAPI.hxx>
27 #include <Geom_OffsetCurve.hxx>
29 #define MAX_PARAM 1000 // if a surface parameter is infinite, it is assingned
30 // this value in order to display the "infinit" object in the viewer.
33 Standard_Boolean OCCDemo_Presentation::WaitForInput (unsigned long aMilliSeconds)
35 //::WaitForSingleObject(::CreateEvent (NULL, FALSE, FALSE, NULL), aMilliSeconds);
36 if (::MsgWaitForMultipleObjects(0, NULL, FALSE, aMilliSeconds,
37 QS_KEY | QS_MOUSEBUTTON) != WAIT_TIMEOUT)
40 if (::PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
42 if (msg.message == WM_KEYUP)
44 ::PeekMessage (&msg, NULL, 0, 0, PM_REMOVE);
45 return WaitForInput (aMilliSeconds);
51 return Standard_False;
54 //================================================================
55 // Function : fixParam
56 // Purpose : assings a finite value to theParam if it intinite
57 // (equal to +- Precision::Infinite())
58 //================================================================
59 static Standard_Boolean fixParam(Standard_Real& theParam)
61 Standard_Boolean aResult = Standard_False;
62 if (Precision::IsNegativeInfinite(theParam))
64 theParam = -MAX_PARAM;
65 aResult = Standard_True;
67 if (Precision::IsPositiveInfinite(theParam))
70 aResult = Standard_True;
75 //================================================================
76 // Function : DrawSurface
77 // Purpose : displays a given geometric surface in 3d viewer
78 // (creates a finite face and displays it)
79 //================================================================
80 Handle(AIS_InteractiveObject) OCCDemo_Presentation::drawSurface
81 (const Handle(Geom_Surface)& theSurface,
82 const Quantity_Color& theColor,
83 const Standard_Boolean toDisplay)
85 Standard_Real u1, u2, v1, v2;
86 theSurface->Bounds(u1,u2,v1,v2);
92 Handle(AIS_Shape) aGraphicSurface =
93 new AIS_Shape(BRepBuilderAPI_MakeFace (theSurface, u1, u2, v1, v2, Precision::Confusion()));
95 getAISContext()->SetMaterial(aGraphicSurface, Graphic3d_NOM_PLASTIC, toDisplay);
96 getAISContext()->SetColor(aGraphicSurface, theColor, toDisplay);
99 getAISContext()->Display (aGraphicSurface, Standard_False);
103 getAISContext()->Display (aGraphicSurface, Standard_True);
106 return aGraphicSurface;
109 //================================================================
110 // Function : DrawCurve
111 // Purpose : displays a given curve 3d
112 //================================================================
113 Handle(AIS_InteractiveObject) OCCDemo_Presentation::drawCurve
114 (const Handle(Geom_Curve)& theCurve,
115 const Quantity_Color& theColor,
116 const Standard_Boolean toDisplay)
118 Handle(ISession_Curve) aGraphicCurve = new ISession_Curve (theCurve);
120 getAISContext()->SetColor (aGraphicCurve, theColor, toDisplay);
121 aGraphicCurve->Attributes()->Link()->SetLineArrowDraw(Standard_False);
124 getAISContext()->Display (aGraphicCurve, Standard_False);
128 getAISContext()->Display (aGraphicCurve, Standard_True);
131 return aGraphicCurve;
134 //================================================================
135 // Function : DrawCurve
136 // Purpose : displays a given curve 2d
137 //================================================================
138 Handle(AIS_InteractiveObject) OCCDemo_Presentation::drawCurve
139 (const Handle(Geom2d_Curve)& theCurve,
140 const Quantity_Color& theColor,
141 const Standard_Boolean toDisplay,
142 const gp_Ax2& aPosition)
144 // create 3D curve in plane
145 Handle(Geom_Curve) aCurve3d;
146 if (theCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
148 Handle(Geom2d_OffsetCurve) aOffCurve =
149 Handle(Geom2d_OffsetCurve)::DownCast(theCurve);
150 Handle(Geom_Curve) aBasCurve3d =
151 GeomAPI::To3d (aOffCurve->BasisCurve(), gp_Pln(aPosition));
152 Standard_Real aDist = aOffCurve->Offset();
153 aCurve3d = new Geom_OffsetCurve (aBasCurve3d, aDist, aPosition.Direction());
157 aCurve3d = GeomAPI::To3d (theCurve, gp_Pln(aPosition));
159 return drawCurve (aCurve3d, theColor, toDisplay);
162 //================================================================
163 // Function : drawPoint
164 // Purpose : displays a given point
165 //================================================================
166 Handle(AIS_Point) OCCDemo_Presentation::drawPoint
168 const Quantity_Color& theColor,
169 const Standard_Boolean toDisplay)
171 Handle(AIS_Point) aGraphicPoint = new AIS_Point (new Geom_CartesianPoint(aPnt));
173 getAISContext()->SetColor (aGraphicPoint, theColor, toDisplay);
175 getAISContext()->Display (aGraphicPoint, Standard_True);
176 //COCCDemoDoc::Fit();
179 return aGraphicPoint;
182 //================================================================
183 // Function : drawVector
184 // Purpose : displays a given vector in 3d viewer
185 // (segment of line starting at thePnt with the arrow at the end,
186 // the length of segment is the length of the vector)
187 //================================================================
188 Handle(AIS_InteractiveObject) OCCDemo_Presentation::drawVector
189 (const gp_Pnt& thePnt,
190 const gp_Vec& theVec,
191 const Quantity_Color& theColor,
192 const Standard_Boolean toDisplay)
194 Standard_Real aLength = theVec.Magnitude();
195 if (aLength < Precision::Confusion())
196 return Handle(AIS_InteractiveObject)();
198 Handle(Geom_Curve) aCurve = new Geom_Line (thePnt, theVec);
199 aCurve = new Geom_TrimmedCurve (aCurve, 0, aLength);
201 Handle(ISession_Curve) aGraphicCurve = new ISession_Curve (aCurve);
203 getAISContext()->SetColor (aGraphicCurve, theColor, toDisplay);
204 Handle(Prs3d_Drawer) aDrawer = aGraphicCurve->Attributes()->Link();
205 aDrawer->SetLineArrowDraw(Standard_True);
206 aDrawer->ArrowAspect()->SetLength(aLength/10);
209 getAISContext()->Display (aGraphicCurve, Standard_False);
213 getAISContext()->Display (aGraphicCurve, Standard_True);
216 return aGraphicCurve;
220 Handle(AIS_Shape) OCCDemo_Presentation::drawShape
221 (const TopoDS_Shape& theShape,const Quantity_Color& theColor,
222 const Standard_Boolean toDisplay)
224 Handle(AIS_Shape) aGraphicShape = new AIS_Shape(theShape);
226 getAISContext()->SetMaterial(aGraphicShape, Graphic3d_NOM_PLASTIC, toDisplay);
227 getAISContext()->SetColor (aGraphicShape, theColor, toDisplay);
230 getAISContext()->Display (aGraphicShape, Standard_False);
234 getAISContext()->Display (aGraphicShape, Standard_True);
237 return aGraphicShape;
240 Handle(AIS_Shape) OCCDemo_Presentation::drawShape
241 (const TopoDS_Shape& theShape,
242 const Graphic3d_NameOfMaterial theMaterial,
243 const Standard_Boolean toDisplay)
245 Handle(AIS_Shape) aGraphicShape = new AIS_Shape(theShape);
247 getAISContext()->SetMaterial(aGraphicShape, theMaterial, toDisplay);
250 getAISContext()->Display (aGraphicShape, Standard_False);
254 getAISContext()->Display (aGraphicShape, Standard_True);
257 return aGraphicShape;
260 void OCCDemo_Presentation::GetViewAt (V3d_Coordinate& theX, V3d_Coordinate& theY, V3d_Coordinate& theZ)
262 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
263 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
264 CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
265 pView->GetViewAt (theX, theY, theZ);
268 void OCCDemo_Presentation::SetViewAt (const V3d_Coordinate theX, const V3d_Coordinate theY, const V3d_Coordinate theZ)
270 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
271 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
272 CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
273 pView->SetViewAt (theX, theY, theZ);
276 void OCCDemo_Presentation::GetViewEye(V3d_Coordinate& X, V3d_Coordinate& Y, V3d_Coordinate& Z)
278 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
279 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
280 CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
281 pView->GetViewEye(X,Y,Z);
284 void OCCDemo_Presentation::SetViewEye(V3d_Coordinate X, V3d_Coordinate Y, V3d_Coordinate Z)
286 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
287 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
288 CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
289 pView->SetViewEye(X,Y,Z);
292 Quantity_Factor OCCDemo_Presentation::GetViewScale()
294 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
295 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
296 CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
297 return pView->GetViewScale();
300 void OCCDemo_Presentation::SetViewScale(Quantity_Factor Coef)
302 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
303 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
304 CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
305 pView->SetViewScale(Coef);
308 void OCCDemo_Presentation::ResetView()
310 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
311 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
312 CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
316 Handle(AIS_InteractiveContext) OCCDemo_Presentation::getAISContext() const
318 return myDoc->GetAISContext();
321 Handle(V3d_Viewer) OCCDemo_Presentation::getViewer() const
323 return myDoc->GetViewer();
326 Standard_CString OCCDemo_Presentation::GetDataDir()
328 return myDoc->GetDataDir();