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 <OCC_3dView.h>
8 #include "TriangulationDoc.h"
9 #include "ISession_Curve.h"
11 #include <AIS_InteractiveObject.hxx>
12 #include <Geom_Surface.hxx>
13 #include <Geom_Curve.hxx>
14 #include <Geom2d_Curve.hxx>
15 #include <Quantity_Color.hxx>
16 #include <AIS_Shape.hxx>
17 #include <BRepBuilderAPI_MakeFace.hxx>
18 #include <Precision.hxx>
19 #include <Geom_Line.hxx>
20 #include <Geom_TrimmedCurve.hxx>
21 #include <Prs3d_Drawer.hxx>
22 #include <Prs3d_ArrowAspect.hxx>
23 #include <AIS_Point.hxx>
24 #include <Geom_CartesianPoint.hxx>
25 #include <Geom2d_OffsetCurve.hxx>
26 #include <GeomAPI.hxx>
28 #include <Geom_OffsetCurve.hxx>
30 #define MAX_PARAM 1000 // if a surface parameter is infinite, it is assingned
31 // this value in order to display the "infinit" object in the viewer.
34 Standard_Boolean OCCDemo_Presentation::WaitForInput (unsigned long aMilliSeconds)
36 //::WaitForSingleObject(::CreateEvent (NULL, FALSE, FALSE, NULL), aMilliSeconds);
37 if (::MsgWaitForMultipleObjects(0, NULL, FALSE, aMilliSeconds,
38 QS_KEY | QS_MOUSEBUTTON) != WAIT_TIMEOUT)
41 if (::PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
43 if (msg.message == WM_KEYUP)
45 ::PeekMessage (&msg, NULL, 0, 0, PM_REMOVE);
46 return WaitForInput (aMilliSeconds);
52 return Standard_False;
55 //================================================================
56 // Function : fixParam
57 // Purpose : assings a finite value to theParam if it intinite
58 // (equal to +- Precision::Infinite())
59 //================================================================
60 static Standard_Boolean fixParam(Standard_Real& theParam)
62 Standard_Boolean aResult = Standard_False;
63 if (Precision::IsNegativeInfinite(theParam))
65 theParam = -MAX_PARAM;
66 aResult = Standard_True;
68 if (Precision::IsPositiveInfinite(theParam))
71 aResult = Standard_True;
76 //================================================================
77 // Function : DrawSurface
78 // Purpose : displays a given geometric surface in 3d viewer
79 // (creates a finite face and displays it)
80 //================================================================
81 Handle(AIS_InteractiveObject) OCCDemo_Presentation::drawSurface
82 (const Handle(Geom_Surface)& theSurface,
83 const Quantity_Color& theColor,
84 const Standard_Boolean toDisplay)
86 Standard_Real u1, u2, v1, v2;
87 theSurface->Bounds(u1,u2,v1,v2);
93 Handle(AIS_Shape) aGraphicSurface =
94 new AIS_Shape(BRepBuilderAPI_MakeFace (theSurface, u1, u2, v1, v2, Precision::Confusion()));
96 getAISContext()->SetMaterial(aGraphicSurface, Graphic3d_NOM_PLASTIC, toDisplay);
97 getAISContext()->SetColor(aGraphicSurface, theColor, toDisplay);
100 getAISContext()->Display (aGraphicSurface, Standard_False);
101 CTriangulationDoc::Fit();
104 getAISContext()->Display (aGraphicSurface, Standard_True);
107 return aGraphicSurface;
110 //================================================================
111 // Function : DrawCurve
112 // Purpose : displays a given curve 3d
113 //================================================================
114 Handle(AIS_InteractiveObject) OCCDemo_Presentation::drawCurve
115 (const Handle(Geom_Curve)& theCurve,
116 const Quantity_Color& theColor,
117 const Standard_Boolean toDisplay)
119 Handle(ISession_Curve) aGraphicCurve = new ISession_Curve (theCurve);
121 getAISContext()->SetColor (aGraphicCurve, theColor, toDisplay);
122 aGraphicCurve->Attributes()->Link()->SetLineArrowDraw(Standard_False);
125 getAISContext()->Display (aGraphicCurve, Standard_False);
126 CTriangulationDoc::Fit();
129 getAISContext()->Display (aGraphicCurve, Standard_True);
132 return aGraphicCurve;
135 //================================================================
136 // Function : DrawCurve
137 // Purpose : displays a given curve 2d
138 //================================================================
139 Handle(AIS_InteractiveObject) OCCDemo_Presentation::drawCurve
140 (const Handle(Geom2d_Curve)& theCurve,
141 const Quantity_Color& theColor,
142 const Standard_Boolean toDisplay,
143 const gp_Ax2& aPosition)
145 // create 3D curve in plane
146 Handle(Geom_Curve) aCurve3d;
147 if (theCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve)))
149 Handle(Geom2d_OffsetCurve) aOffCurve =
150 Handle(Geom2d_OffsetCurve)::DownCast(theCurve);
151 Handle(Geom_Curve) aBasCurve3d =
152 GeomAPI::To3d (aOffCurve->BasisCurve(), gp_Pln(aPosition));
153 Standard_Real aDist = aOffCurve->Offset();
154 aCurve3d = new Geom_OffsetCurve (aBasCurve3d, aDist, aPosition.Direction());
158 aCurve3d = GeomAPI::To3d (theCurve, gp_Pln(aPosition));
160 return drawCurve (aCurve3d, theColor, toDisplay);
163 //================================================================
164 // Function : drawPoint
165 // Purpose : displays a given point
166 //================================================================
167 Handle(AIS_Point) OCCDemo_Presentation::drawPoint
169 const Quantity_Color& theColor,
170 const Standard_Boolean toDisplay)
172 Handle(AIS_Point) aGraphicPoint = new AIS_Point (new Geom_CartesianPoint(aPnt));
174 getAISContext()->SetColor (aGraphicPoint, theColor, toDisplay);
176 getAISContext()->Display (aGraphicPoint, Standard_True);
177 //COCCDemoDoc::Fit();
180 return aGraphicPoint;
183 //================================================================
184 // Function : drawVector
185 // Purpose : displays a given vector in 3d viewer
186 // (segment of line starting at thePnt with the arrow at the end,
187 // the length of segment is the length of the vector)
188 //================================================================
189 Handle(AIS_InteractiveObject) OCCDemo_Presentation::drawVector
190 (const gp_Pnt& thePnt,
191 const gp_Vec& theVec,
192 const Quantity_Color& theColor,
193 const Standard_Boolean toDisplay)
195 Standard_Real aLength = theVec.Magnitude();
196 if (aLength < Precision::Confusion())
197 return Handle(AIS_InteractiveObject)();
199 Handle(Geom_Curve) aCurve = new Geom_Line (thePnt, theVec);
200 aCurve = new Geom_TrimmedCurve (aCurve, 0, aLength);
202 Handle(ISession_Curve) aGraphicCurve = new ISession_Curve (aCurve);
204 getAISContext()->SetColor (aGraphicCurve, theColor, toDisplay);
205 Handle(Prs3d_Drawer) aDrawer = aGraphicCurve->Attributes()->Link();
206 aDrawer->SetLineArrowDraw(Standard_True);
207 aDrawer->ArrowAspect()->SetLength(aLength/10);
210 getAISContext()->Display (aGraphicCurve, Standard_False);
211 CTriangulationDoc::Fit();
214 getAISContext()->Display (aGraphicCurve, Standard_True);
217 return aGraphicCurve;
221 Handle(AIS_Shape) OCCDemo_Presentation::drawShape
222 (const TopoDS_Shape& theShape,const Quantity_Color& theColor,
223 const Standard_Boolean toDisplay)
225 Handle(AIS_Shape) aGraphicShape = new AIS_Shape(theShape);
227 getAISContext()->SetMaterial(aGraphicShape, Graphic3d_NOM_PLASTIC, toDisplay);
228 getAISContext()->SetColor (aGraphicShape, theColor, toDisplay);
231 getAISContext()->Display (aGraphicShape, Standard_False);
232 CTriangulationDoc::Fit();
235 getAISContext()->Display (aGraphicShape, Standard_True);
238 return aGraphicShape;
241 Handle(AIS_Shape) OCCDemo_Presentation::drawShape
242 (const TopoDS_Shape& theShape,
243 const Graphic3d_NameOfMaterial theMaterial,
244 const Standard_Boolean toDisplay)
246 Handle(AIS_Shape) aGraphicShape = new AIS_Shape(theShape);
248 getAISContext()->SetMaterial(aGraphicShape, theMaterial, toDisplay);
251 getAISContext()->Display (aGraphicShape, Standard_False);
252 CTriangulationDoc::Fit();
255 getAISContext()->Display (aGraphicShape, Standard_True);
258 return aGraphicShape;
261 void OCCDemo_Presentation::GetViewCenter(Standard_Real& Xc, Standard_Real& Yc)
263 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
264 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
265 OCC_3dView *pView = (OCC_3dView*)pChild->GetActiveView();
266 pView->GetViewCenter(Xc,Yc);
269 void OCCDemo_Presentation::SetViewCenter(Standard_Real Xc, Standard_Real Yc)
271 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
272 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
273 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
274 pView->SetViewCenter(Xc,Yc);
277 void OCCDemo_Presentation::GetViewEye(Standard_Real& X, Standard_Real& Y, Standard_Real& Z)
279 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
280 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
281 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
282 pView->GetViewEye(X,Y,Z);
285 void OCCDemo_Presentation::SetViewEye(Standard_Real X, Standard_Real Y, Standard_Real Z)
287 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
288 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
289 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
290 pView->SetViewEye(X,Y,Z);
293 Standard_Real OCCDemo_Presentation::GetViewScale()
295 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
296 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
297 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
298 return pView->GetViewScale();
301 void OCCDemo_Presentation::SetViewScale(Standard_Real Coef)
303 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
304 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
305 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
306 pView->SetViewScale(Coef);
309 void OCCDemo_Presentation::ResetView()
311 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
312 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
313 COCCDemoView *pView = (COCCDemoView *) pChild->GetActiveView();
317 Handle(AIS_InteractiveContext) OCCDemo_Presentation::getAISContext() const
319 return myDoc->GetAISContext();
322 Handle(V3d_Viewer) OCCDemo_Presentation::getViewer() const
324 return myDoc->GetViewer();
328 Standard_CString OCCDemo_Presentation::GetDataDir()
330 return myDoc->GetDataDir();