5381c7baf541eb9d593d84ea66460d559dbe720d
[occt.git] / samples / mfc / standard / 09_Animation / src / AnimationDoc.cpp
1 // AnimationDocApp.cpp : implementation of the CAnimationDoc class
2 //
3
4 #include "stdafx.h"
5
6 #include "AnimationApp.h"
7 #include "AnimationDoc.h"
8 #include "AnimationView3D.h"
9 #include "AISDialogs.h"
10 #include "ShadingDialog.h"
11 #include "ThreadDialog.h"
12 #include "Fonc.hxx"
13
14 #define DEFAULT_COLOR Quantity_NOC_CYAN1
15 #define DEFAULT_MATERIAL Graphic3d_NOM_PLASTER
16 #define DEFAULT_DEVIATIONCOEFFICIENT 0.001
17 #define DEFAULT_HILIGHTCOLOR Quantity_NOC_YELLOW
18
19
20 #ifdef _DEBUG
21 //#define new DEBUG_NEW  // by cascade
22 #undef THIS_FILE
23 static char THIS_FILE[] = __FILE__;
24 #endif
25
26 /////////////////////////////////////////////////////////////////////////////
27 // CAnimationDoc
28
29 IMPLEMENT_DYNCREATE(CAnimationDoc, CDocument)
30
31 BEGIN_MESSAGE_MAP(CAnimationDoc, CDocument)
32         //{{AFX_MSG_MAP(CAnimationDoc)
33         ON_COMMAND(ID_SHADING, OnShading)
34         ON_COMMAND(ID_Thread, OnThread)
35         ON_COMMAND(ID_FILE_LOADGRID, OnFileLoadgrid)
36         ON_UPDATE_COMMAND_UI(ID_WALK_WALKTHRU, OnUpdateWalkWalkthru)
37         //}}AFX_MSG_MAP
38
39 END_MESSAGE_MAP()
40
41 /////////////////////////////////////////////////////////////////////////////
42 // CAnimationDoc construction/destruction
43
44 CAnimationDoc::CAnimationDoc()
45 {
46         // TODO: add one-time construction code here
47
48         static Standard_Integer StaticCount=1;
49         StaticCount++;
50         myCount = StaticCount;
51
52         Handle(Graphic3d_GraphicDriver) aGraphicDriver = 
53                 ((CAnimationApp*)AfxGetApp())->GetGraphicDriver();
54
55         myViewer = new V3d_Viewer(aGraphicDriver);
56         myViewer->SetDefaultLights();
57         myViewer->SetLightOn();
58
59         myAISContext =new AIS_InteractiveContext(myViewer);
60
61         myDeviation = 0.0008;
62         thread = 4;
63         myAngle = 0;
64
65         BRep_Builder B;
66         TopoDS_Shape CrankArm;
67         TopoDS_Shape CylinderHead;
68         TopoDS_Shape Propeller;
69         TopoDS_Shape Piston;
70         TopoDS_Shape EngineBlock;
71         
72         /*wchar_t AbloluteExecutableFileName[200];
73         HMODULE hModule = GetModuleHandleW (NULL);
74         GetModuleFileNameW (hModule, AbloluteExecutableFileName, 200);
75   CString aDataDirPath (AbloluteExecutableFileName);
76   int index = aDataDirPath.ReverseFind ('\\');
77   aDataDirPath.Delete (index + 1, aDataDirPath.GetLength() - index - 1);*/
78
79   CString SHAREPATHValue;
80   SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
81   CString aDataDirPath = (SHAREPATHValue + "\\occ");
82
83   std::filebuf aFileBuf;
84   std::istream aStream (&aFileBuf);
85   CString aPathCrankArm = aDataDirPath + "\\CrankArm.rle";
86   if (aFileBuf.open (aPathCrankArm, ios::in))
87   {
88     BRepTools::Read (CrankArm, aStream, B);
89     aFileBuf.close();
90   }
91
92   CString aPathCylinderHead = aDataDirPath + "\\CylinderHead.rle";
93   if (aFileBuf.open (aPathCylinderHead, ios::in))
94   {
95     BRepTools::Read (CylinderHead, aStream, B);
96     aFileBuf.close();
97   }
98
99   CString aPathPropeller = aDataDirPath + "\\Propeller.rle";
100   if (aFileBuf.open (aPathPropeller, ios::in))
101   {
102     BRepTools::Read (Propeller, aStream, B);
103     aFileBuf.close();
104   }
105
106   CString aPathPiston = aDataDirPath + "\\Piston.rle";
107   if (aFileBuf.open (aPathPiston, ios::in))
108   {
109     BRepTools::Read (Piston, aStream, B);
110     aFileBuf.close();
111   }
112
113   CString aPathEngineBlock = aDataDirPath + "\\EngineBlock.rle";
114   if (aFileBuf.open (aPathEngineBlock, ios::in))
115   {
116     BRepTools::Read (EngineBlock, aStream, B);
117     aFileBuf.close();
118   }
119
120         if (CrankArm.IsNull() || CylinderHead.IsNull() || 
121         Propeller.IsNull() || Piston.IsNull() || EngineBlock.IsNull())
122         {
123                 int rep = MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Shape(s) not found.\nCheck the Data directory path!", L"Error", MB_OK | MB_ICONERROR);
124                 if (rep == IDOK)
125                         exit(0);
126         }
127         myAISContext->SetDeviationCoefficient(myDeviation);
128
129         myAisCylinderHead = new AIS_Shape (CylinderHead);
130         myAISContext->SetColor    (myAisCylinderHead, Quantity_NOC_WHITE);
131         myAISContext->SetMaterial (myAisCylinderHead, Graphic3d_NOM_PLASTIC);
132         myAisEngineBlock  = new AIS_Shape (EngineBlock);
133         myAISContext->SetColor(myAisEngineBlock,   Quantity_NOC_WHITE);
134         myAISContext->SetMaterial(myAisEngineBlock,Graphic3d_NOM_PLASTIC);
135
136         myAISContext->Display(myAisCylinderHead ,1,-1,Standard_False,Standard_False);
137         myAISContext->Display(myAisEngineBlock  ,1,-1,Standard_False,Standard_False);
138
139         myAisCrankArm     = new AIS_Shape (CrankArm);
140         myAISContext->SetColor   (myAisCrankArm, Quantity_NOC_HOTPINK);
141         myAISContext->SetMaterial(myAisCrankArm, Graphic3d_NOM_PLASTIC);
142         myAisPiston       = new AIS_Shape (Piston);
143         myAISContext->SetColor   (myAisPiston  , Quantity_NOC_WHITE);
144         myAISContext->SetMaterial(myAisPiston  , Graphic3d_NOM_PLASTIC);
145         myAisPropeller    = new AIS_Shape (Propeller);
146         myAISContext->SetColor   (myAisPropeller, Quantity_NOC_RED);
147         myAISContext->SetMaterial(myAisPropeller, Graphic3d_NOM_PLASTIC);
148
149         myAISContext->Display(myAisCrankArm     ,1,-1,Standard_False,Standard_False);
150         myAISContext->Display(myAisPropeller    ,1,-1,Standard_False,Standard_False);
151         myAISContext->Display(myAisPiston       ,1,-1,Standard_True,Standard_False);
152
153         m_Xmin = -300. ;
154         m_Ymin = -300. ;
155         m_Zmin = -300. ;
156         m_Xmax = +300. ;
157         m_Ymax = +300. ;
158         m_Zmax = +300. ;
159
160         m_bIsGridLoaded = FALSE;
161 }
162
163 CAnimationDoc::~CAnimationDoc()
164 {
165 }
166
167 /////////////////////////////////////////////////////////////////////////////
168 // CAnimationDoc diagnostics
169
170 #ifdef _DEBUG
171 void CAnimationDoc::AssertValid() const
172 {
173         CDocument::AssertValid();
174 }
175
176 void CAnimationDoc::Dump(CDumpContext& dc) const
177 {
178         CDocument::Dump(dc);
179 }
180 #endif //_DEBUG
181
182 /////////////////////////////////////////////////////////////////////////////
183 // CAnimationDoc commands
184 //-----------------------------------------------------------------------------------------
185 //
186 //-----------------------------------------------------------------------------------------
187 void CAnimationDoc::DragEvent(const Standard_Integer  x        ,
188                                                   const Standard_Integer  y        ,
189                                                   const Standard_Integer  TheState ,
190                                   const Handle(V3d_View)& aView    )
191 {
192
193     // TheState == -1  button down
194         // TheState ==  0  move
195         // TheState ==  1  button up
196
197     static Standard_Integer theButtonDownX=0;
198     static Standard_Integer theButtonDownY=0;
199
200         if (TheState == -1)
201     {
202       theButtonDownX=x;
203       theButtonDownY=y;
204     }
205
206         if (TheState == 1)
207           myAISContext->Select(theButtonDownX,theButtonDownY,x,y,aView);  
208 }
209
210 //-----------------------------------------------------------------------------------------
211 //
212 //-----------------------------------------------------------------------------------------
213 void CAnimationDoc::InputEvent(const Standard_Integer  /*x*/,
214                                const Standard_Integer  /*y*/,
215                                const Handle(V3d_View)& /*aView*/ )
216 {
217     myAISContext->Select(); 
218 }
219
220 //-----------------------------------------------------------------------------------------
221 //
222 //-----------------------------------------------------------------------------------------
223 void CAnimationDoc::MoveEvent(const Standard_Integer  x       ,
224                                   const Standard_Integer  y       ,
225                                   const Handle(V3d_View)& aView   ) 
226 {
227       myAISContext->MoveTo(x,y,aView);
228 }
229
230 //-----------------------------------------------------------------------------------------
231 //
232 //-----------------------------------------------------------------------------------------
233 void CAnimationDoc::ShiftMoveEvent(const Standard_Integer  x       ,
234                                   const Standard_Integer  y       ,
235                                   const Handle(V3d_View)& aView   ) 
236 {
237       myAISContext->MoveTo(x,y,aView);
238 }
239
240 //-----------------------------------------------------------------------------------------
241 //
242 //-----------------------------------------------------------------------------------------
243 void CAnimationDoc::ShiftDragEvent(const Standard_Integer  x        ,
244                                                                            const Standard_Integer  y        ,
245                                                                            const Standard_Integer  TheState ,
246                                        const Handle(V3d_View)& aView    ) 
247 {
248     static Standard_Integer theButtonDownX=0;
249     static Standard_Integer theButtonDownY=0;
250
251         if (TheState == -1)
252     {
253       theButtonDownX=x;
254       theButtonDownY=y;
255     }
256
257         if (TheState == 0)
258           myAISContext->ShiftSelect(theButtonDownX,theButtonDownY,x,y,aView);  
259 }
260
261
262 //-----------------------------------------------------------------------------------------
263 //
264 //-----------------------------------------------------------------------------------------
265 void CAnimationDoc::ShiftInputEvent(const Standard_Integer  /*x*/,
266                                     const Standard_Integer  /*y*/,
267                                     const Handle(V3d_View)& /*aView*/)
268 {
269   myAISContext->ShiftSelect(); 
270 }
271
272 //-----------------------------------------------------------------------------------------
273 //
274 //-----------------------------------------------------------------------------------------
275 void  CAnimationDoc::Popup(const Standard_Integer  /*x*/,
276                            const Standard_Integer  /*y*/ ,
277                            const Handle(V3d_View)& /*aView*/)
278 {
279 }
280
281 void CAnimationDoc::OnMyTimer() 
282 {
283         // TODO: Add your message handler code here and/or call default
284         
285         Standard_Real angleA;
286         Standard_Real angleB;
287         Standard_Real X;
288         gp_Ax1 Ax1(gp_Pnt(0,0,0),gp_Vec(0,0,1));
289
290         myAngle++;
291
292         angleA = thread*myAngle*M_PI/180;
293         X = Sin(angleA)*3/8;
294         angleB = atan(X / Sqrt(-X * X + 1));
295         Standard_Real decal(25*0.6);
296
297         
298         //Build a transformation on the display
299     gp_Trsf aPropellerTrsf;
300     aPropellerTrsf.SetRotation(Ax1,angleA);
301         myAISContext->SetLocation(myAisPropeller,aPropellerTrsf);
302         
303         gp_Ax3 base(gp_Pnt(3*decal*(1-Cos(angleA)),-3*decal*Sin(angleA),0),gp_Vec(0,0,1),gp_Vec(1,0,0));
304     gp_Trsf aCrankArmTrsf;
305     aCrankArmTrsf.SetTransformation(   base.Rotated(gp_Ax1(gp_Pnt(3*decal,0,0),gp_Dir(0,0,1)),angleB));
306         myAISContext->SetLocation(myAisCrankArm,aCrankArmTrsf);
307
308     gp_Trsf aPistonTrsf;
309     aPistonTrsf.SetTranslation(gp_Vec(-3*decal*(1-Cos(angleA))-8*decal*(1-Cos(angleB)),0,0));
310         myAISContext->SetLocation(myAisPiston,aPistonTrsf);
311
312     myAISContext->UpdateCurrentViewer();
313 }
314
315 void CAnimationDoc::OnShading() 
316 {
317         
318         POSITION position = GetFirstViewPosition();
319         CView* pCurrentView = (CView*)GetNextView(position);
320         ((CAnimationView3D *)pCurrentView)              ->OnStop();
321
322         CShadingDialog aDial(NULL);
323
324         aDial.myvalue=int((myDeviation-0.00003)/0.00003);
325         
326         if (aDial.DoModal()==IDOK) {
327                 Standard_Real dev(aDial.myvalue);
328                 myDeviation = 0.00003+0.00003*dev;
329
330                 myAISContext->SetDeviationCoefficient(myDeviation);
331         TopoDS_Shape Propeller=myAisPropeller->Shape();
332                 BRepTools::Clean(Propeller);
333                 
334         myAisPropeller->Set(Propeller);
335         myAISContext->Deactivate(myAisPropeller);
336         myAISContext->Redisplay(myAisPropeller);
337         }
338         ((CAnimationView3D *)pCurrentView)->OnRestart();
339 }
340
341
342 void CAnimationDoc::OnThread() 
343 {
344         POSITION position = GetFirstViewPosition();
345     CView* pCurrentView = (CView*)GetNextView(position);
346         ((CAnimationView3D *)pCurrentView)              ->OnStop();
347         CThreadDialog aThreadDial(NULL);
348         if (aThreadDial.DoModal()==IDOK) {
349                 thread = aThreadDial.m_Angle;
350         }
351
352         ((CAnimationView3D *)pCurrentView)->OnRestart();
353 }
354
355
356 void CAnimationDoc::OnFileLoadgrid() 
357 {
358         // TODO: Add your command handler code here
359
360   CFileDialog dlg(TRUE,
361                   NULL,
362                   NULL,
363                   OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
364                   L"Points Files (*.pnt;*.pnts)|*.pnt; *.pnts|All Files (*.*)|*.*||",
365                   NULL );
366
367         CString initdir(((OCC_App*) AfxGetApp())->GetInitDataDir());
368         initdir += "\\Data\\";
369
370         dlg.m_ofn.lpstrInitialDir = initdir;
371
372
373   if (dlg.DoModal() == IDOK)
374     {
375       CString C = dlg.GetPathName();
376       SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
377
378       Handle(Geom_BSplineSurface) mySurface;
379
380       if(grid2surf(C,mySurface ))
381         {
382           //To set the minimum value of the surface to Z=0 
383           Standard_Real Tolerance = 0.0;
384           Bnd_Box B;
385       TopoDS_Face myFace;
386           GeomAdaptor_Surface GAS(mySurface);
387
388           
389           BndLib_AddSurface::Add(GAS,Tolerance,B);
390
391           Standard_Real Xmin,Xmax,Ymin,Ymax,Zmin,Zmax;
392           B.Get(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
393           TopoDS_Solid Box = BRepPrimAPI_MakeBox(gp_Pnt(Xmin,Ymin,0),Xmax-Xmin,Ymax-Ymin,50).Solid();
394
395           gp_Vec V(gp_Pnt(0,0,Zmin),gp_Pnt(0,0,0));
396           
397           gp_Trsf T;
398           T.SetTranslation(V);
399           mySurface->Transform(T);
400           BRepBuilderAPI_MakeFace aMkFace(mySurface, Precision::Confusion());
401           myFace = aMkFace.Face();
402
403           // Remove all other shapes
404           myAISContext->RemoveAll();
405
406           Handle(AIS_Shape) myAISSurface = new AIS_Shape(myFace);
407
408           myAISContext->Display(myAISSurface, Standard_False);
409           myAISContext->Deactivate(myAISSurface,Standard_False);
410           myAISContext->SetColor (myAISSurface,Quantity_NOC_WHITE,Standard_False);
411           myAISContext->SetMaterial (myAISSurface,Graphic3d_NOM_STONE,Standard_False);
412           myAISContext->SetDisplayMode (myAISSurface,1,Standard_False);
413           myAISContext->SetDeviationCoefficient (0.001);
414         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
415         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
416         CAnimationView3D *pView = (CAnimationView3D *) pChild->GetActiveView();
417         pView->FitAll();
418
419       Bnd_Box Boite;
420       BRepBndLib::Add(myFace, Boite);
421       Boite.Get(m_Xmin, m_Ymin, m_Zmin, m_Xmax, m_Ymax, m_Zmax);
422           
423           m_bIsGridLoaded = TRUE;
424         }       
425       SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
426 }
427
428
429 }
430
431 void CAnimationDoc::OnUpdateWalkWalkthru(CCmdUI* pCmdUI) 
432 {
433         // TODO: Add your command update UI handler code here
434   CString aValue;
435   pCmdUI->SetCheck (aValue.GetEnvironmentVariable (L"CSF_WALKTHROUGH") ? 1 : 0);
436 }