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